summaryrefslogtreecommitdiff
path: root/library/menubutton.cpp
Unidiff
Diffstat (limited to 'library/menubutton.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/menubutton.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/library/menubutton.cpp b/library/menubutton.cpp
new file mode 100644
index 0000000..c34383a
--- a/dev/null
+++ b/library/menubutton.cpp
@@ -0,0 +1,187 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "menubutton.h"
22#include <qpopupmenu.h>
23
24/*!
25 \class MenuButton menubutton.h
26 \brief The MenuButton class is a pushbutton with a menu.
27
28 Similar in function to a QComboBox.
29*/
30
31/*!
32 \fn void MenuButton::selected(int index)
33
34 This signal is emitted when the item at \a index is selected.
35*/
36
37/*!
38 \fn void MenuButton::selected(const QString& text)
39
40 This signal is emitted when the item with the given \a text is selected.
41*/
42
43
44/*!
45 Constructs a MenuButton with the given \a items added (see insertItems()).
46 The standard \a parent an \a name arguments are passed to the base class.
47*/
48MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) :
49 QPushButton(parent,name)
50{
51 init();
52 insertItems(items);
53}
54
55/*!
56 Constructs an empty MenuButton.
57 The standard \a parent an \a name arguments are passed to the base class.
58
59 \sa insertItems()
60*/
61MenuButton::MenuButton( QWidget* parent, const char* name) :
62 QPushButton(parent,name)
63{
64 init();
65}
66
67void MenuButton::init()
68{
69 setAutoDefault(FALSE);
70 pop = new QPopupMenu(this);
71 nitems=0;
72 connect(pop, SIGNAL(activated(int)), this, SLOT(select(int)));
73 setPopup(pop);
74 //setPopupDelay(0);
75}
76
77/*!
78 Removes all items from the button and menu.
79*/
80void MenuButton::clear()
81{
82 delete pop;
83 init();
84}
85
86/*!
87 Inserts string \a items into the menu. The string "--" represents
88 a separator.
89*/
90void MenuButton::insertItems( const QStringList& items )
91{
92 QStringList::ConstIterator it=items.begin();
93 for (; it!=items.end(); ++it) {
94 if ( (*it) == "--" )
95 insertSeparator();
96 else
97 insertItem(*it);
98 }
99}
100
101/*!
102 Inserts an \a icon and \a text into the menu.
103*/
104void MenuButton::insertItem( const QIconSet& icon, const QString& text=QString::null )
105{
106 pop->insertItem(icon, text, nitems++);
107 if ( nitems==1 ) select(0);
108}
109
110/*!
111 Inserts \a text into the menu.
112*/
113void MenuButton::insertItem( const QString& text )
114{
115 pop->insertItem(text, nitems++);
116 if ( nitems==1 ) select(0);
117}
118
119/*!
120 Inserts a visual separator into the menu.
121*/
122void MenuButton::insertSeparator()
123{
124 pop->insertSeparator();
125}
126
127/*!
128 Selects the items with text \a s.
129*/
130void MenuButton::select(const QString& s)
131{
132 for (int i=0; i<nitems; i++) {
133 if ( pop->text(i) == s ) {
134 select(i);
135 break;
136 }
137 }
138}
139
140/*!
141 Selects the item at index \a s.
142*/
143void MenuButton::select(int s)
144{
145 cur = s;
146 updateLabel();
147 if ( pop->iconSet(cur) )
148 setIconSet(*pop->iconSet(cur));
149 emit selected(cur);
150 emit selected(currentText());
151}
152
153/*!
154 Returns the index of the current item.
155*/
156int MenuButton::currentItem() const
157{
158 return cur;
159}
160
161/*!
162 Returns the text of the current item.
163*/
164QString MenuButton::currentText() const
165{
166 return pop->text(cur);
167}
168
169/*!
170 Sets the label. If \a label is empty, the
171 current item text is displayed, otherwise \a label should contain
172 "%1", which will be replaced by the current item text.
173*/
174void MenuButton::setLabel(const QString& label)
175{
176 lab = label;
177 updateLabel();
178}
179
180void MenuButton::updateLabel()
181{
182 QString t = pop->text(cur);
183 if ( !lab.isEmpty() )
184 t = lab.arg(t);
185 setText(t);
186}
187