Qtopia library API Documentation

menubutton.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "menubutton.h"
00022 #include <qpopupmenu.h>
00023 
00067 MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) :
00068     QPushButton(parent,name)
00069 {
00070     useLabel = true;
00071     init();
00072     insertItems(items);
00073 }
00074 
00081 MenuButton::MenuButton( QWidget* parent, const char* name) :
00082     QPushButton(parent,name)
00083 {
00084     init();
00085 }
00086 
00087 void MenuButton::init()
00088 {
00089     setAutoDefault(FALSE);
00090     pop = new QPopupMenu(this);
00091     nitems=0;
00092     connect(pop, SIGNAL(activated(int)), this, SLOT(select(int)));
00093     setPopup(pop);
00094     //setPopupDelay(0);
00095 }
00096 
00100 void MenuButton::clear()
00101 {
00102    delete pop;
00103    init();
00104 }
00105 
00111 void MenuButton::insertItems( const QStringList& items )
00112 {
00113     QStringList::ConstIterator it=items.begin();
00114     for (; it!=items.end(); ++it) {
00115   if ( (*it) == "--" )
00116       insertSeparator();
00117   else
00118       insertItem(*it);
00119     }
00120 }
00121 
00128 void MenuButton::insertItem( const QIconSet& icon, const QString& text )
00129 {
00130     pop->insertItem(icon, text, nitems++);
00131 //    if ( nitems==1 ) select(0);
00132 }
00133 
00140 void MenuButton::insertItem( const QString& text )
00141 {
00142     pop->insertItem(text, nitems++);
00143 //    if ( nitems==1 ) select(0);
00144 }
00145 
00151 void MenuButton::insertSeparator()
00152 {
00153     pop->insertSeparator();
00154 }
00155 
00159 void MenuButton::select(const QString& s)
00160 {
00161     for (int i=0; i<nitems; i++) {
00162   if ( pop->text(i) == s ) {
00163       select(i);
00164       break;
00165   }
00166     }
00167 }
00168 
00173 void MenuButton::select(int s)
00174 {
00175     cur = s;
00176     updateLabel();
00177     if ( pop->iconSet(cur) )
00178   setIconSet(*pop->iconSet(cur));
00179     emit selected(cur);
00180     emit selected(currentText());
00181 }
00182 
00186 int MenuButton::currentItem() const
00187 {
00188     return cur;
00189 }
00190 
00194 QString MenuButton::currentText() const
00195 {
00196     return pop->text(cur);
00197 }
00198 
00204 void MenuButton::setLabel(const QString& label)
00205 {
00206     lab = label;
00207     updateLabel();
00208 }
00209 
00210 void MenuButton::updateLabel()
00211 {
00212     if(useLabel)
00213       {
00214           QString t = pop->text(cur);
00215           if ( !lab.isEmpty() )
00216               t = lab.arg(t);
00217           setText(t);
00218       }
00219 }
00220 
00221 
00225 void MenuButton::remove(int id)
00226 {
00227      pop->removeItem(id);
00228      nitems--;
00229 }
00230 
00234 int MenuButton::count()
00235 {
00236      return nitems;
00237 }
00238 
00242 QString MenuButton::text(int id)
00243 {
00244     return pop->text(id);
00245 }
00246 
00250 void MenuButton::setUseLabel(bool b)
00251 {
00252     useLabel = b;
00253 }
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:06 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001