-rw-r--r-- | library/menubutton.cpp | 71 | ||||
-rw-r--r-- | library/menubutton.h | 6 |
2 files changed, 60 insertions, 17 deletions
diff --git a/library/menubutton.cpp b/library/menubutton.cpp index 007761f..4357460 100644 --- a/library/menubutton.cpp +++ b/library/menubutton.cpp | |||
@@ -22,195 +22,232 @@ | |||
22 | #include <qpopupmenu.h> | 22 | #include <qpopupmenu.h> |
23 | 23 | ||
24 | /*! | 24 | /*! |
25 | \class MenuButton menubutton.h | 25 | \class MenuButton menubutton.h |
26 | \brief The MenuButton class is a pushbutton with a menu. | 26 | \brief The MenuButton class is a pushbutton with a menu. |
27 | 27 | ||
28 | When the user presses the menubutton's pushbutton, the menu pops up. | 28 | When the user presses the menubutton's pushbutton, the menu pops up. |
29 | A menu is composed of menu items each of which has a string label, | 29 | A menu is composed of menu items each of which has a string label, |
30 | and optionally an icon. | 30 | and optionally an icon. |
31 | 31 | ||
32 | The index of the item that the user's input device (e.g. stylus) is | 32 | The index of the item that the user's input device (e.g. stylus) is |
33 | pointing at is the currentItem(), whose text is available using | 33 | pointing at is the currentItem(), whose text is available using |
34 | currentText(). | 34 | currentText(). |
35 | 35 | ||
36 | Menu items are inserted with the \link MenuButton::MenuButton() | 36 | Menu items are inserted with the \link MenuButton::MenuButton() |
37 | constructor\endlink, insertItem() or insertItems(). Separators are | 37 | constructor\endlink, insertItem() or insertItems(). Separators are |
38 | inserted with insertSeparator(). All the items in the menu can be | 38 | inserted with insertSeparator(). All the items in the menu can be |
39 | removed by calling clear(). | 39 | removed by calling clear(). |
40 | 40 | ||
41 | Items can be selected programmatically using select(). When a menu | 41 | Items can be selected programmatically using select(). When a menu |
42 | item is selected (programmatically or by the user), the selected() | 42 | item is selected (programmatically or by the user), the selected() |
43 | signal is emitted. | 43 | signal is emitted. |
44 | 44 | ||
45 | \ingroup qtopiaemb | 45 | \ingroup qtopiaemb |
46 | */ | 46 | */ |
47 | 47 | ||
48 | /*! | 48 | /*! |
49 | \overload void MenuButton::selected(int index) | 49 | \overload void MenuButton::selected(int index) |
50 | 50 | ||
51 | This signal is emitted when the item at position \a index is selected. | 51 | This signal is emitted when the item at position \a index is selected. |
52 | */ | 52 | */ |
53 | 53 | ||
54 | /*! | 54 | /*! |
55 | \fn void MenuButton::selected(const QString& text) | 55 | \fn void MenuButton::selected(const QString& text) |
56 | 56 | ||
57 | This signal is emitted when the item with the label \a text is selected. | 57 | This signal is emitted when the item with the label \a text is selected. |
58 | */ | 58 | */ |
59 | 59 | ||
60 | 60 | ||
61 | /*! | 61 | /*! |
62 | Constructs a MenuButton. A menu item is created (see insertItem() | 62 | Constructs a MenuButton. A menu item is created (see insertItem() |
63 | and insertItems()) for each string in the \a items string list. The | 63 | and insertItems()) for each string in the \a items string list. The |
64 | standard \a parent an \a name arguments are passed to the base | 64 | standard \a parent an \a name arguments are passed to the base |
65 | class. | 65 | class. |
66 | */ | 66 | */ |
67 | MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) : | 67 | MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) : |
68 | QPushButton(parent,name) | 68 | QPushButton(parent,name) |
69 | { | 69 | { |
70 | useLabel = true; | ||
70 | init(); | 71 | init(); |
71 | insertItems(items); | 72 | insertItems(items); |
72 | } | 73 | } |
73 | 74 | ||
74 | /*! | 75 | /*! |
75 | Constructs an empty MenuButton. | 76 | Constructs an empty MenuButton. |
76 | The standard \a parent an \a name arguments are passed to the base class. | 77 | The standard \a parent an \a name arguments are passed to the base class. |
77 | 78 | ||
78 | \sa insertItem() insertItems() | 79 | \sa insertItem() insertItems() |
79 | */ | 80 | */ |
80 | MenuButton::MenuButton( QWidget* parent, const char* name) : | 81 | MenuButton::MenuButton( QWidget* parent, const char* name) : |
81 | QPushButton(parent,name) | 82 | QPushButton(parent,name) |
82 | { | 83 | { |
83 | init(); | 84 | init(); |
84 | } | 85 | } |
85 | 86 | ||
86 | void MenuButton::init() | 87 | void MenuButton::init() |
87 | { | 88 | { |
88 | setAutoDefault(FALSE); | 89 | setAutoDefault(FALSE); |
89 | pop = new QPopupMenu(this); | 90 | pop = new QPopupMenu(this); |
90 | nitems=0; | 91 | nitems=0; |
91 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); | 92 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); |
92 | setPopup(pop); | 93 | setPopup(pop); |
93 | //setPopupDelay(0); | 94 | //setPopupDelay(0); |
94 | } | 95 | } |
95 | 96 | ||
96 | /*! | 97 | /*! |
97 | Removes all the menu items from the button and menu. | 98 | Removes all the menu items from the button and menu. |
98 | */ | 99 | */ |
99 | void MenuButton::clear() | 100 | void MenuButton::clear() |
100 | { | 101 | { |
101 | delete pop; | 102 | delete pop; |
102 | init(); | 103 | init(); |
103 | } | 104 | } |
104 | 105 | ||
105 | /*! | 106 | /*! |
106 | A menu item is created (see insertItem()) for each string in the \a | 107 | A menu item is created (see insertItem()) for each string in the \a |
107 | items string list. If any string is "--" a separator (see | 108 | items string list. If any string is "--" a separator (see |
108 | insertSeparator()) is inserted in its place. | 109 | insertSeparator()) is inserted in its place. |
109 | */ | 110 | */ |
110 | void MenuButton::insertItems( const QStringList& items ) | 111 | void MenuButton::insertItems( const QStringList& items ) |
111 | { | 112 | { |
112 | QStringList::ConstIterator it=items.begin(); | 113 | QStringList::ConstIterator it=items.begin(); |
113 | for (; it!=items.end(); ++it) { | 114 | for (; it!=items.end(); ++it) { |
114 | if ( (*it) == "--" ) | 115 | if ( (*it) == "--" ) |
115 | insertSeparator(); | 116 | insertSeparator(); |
116 | else | 117 | else |
117 | insertItem(*it); | 118 | insertItem(*it); |
118 | } | 119 | } |
119 | } | 120 | } |
120 | 121 | ||
121 | /*! | 122 | /*! |
122 | Inserts a menu item with the icon \a icon and label \a text into | 123 | Inserts a menu item with the icon \a icon and label \a text into |
123 | the menu. | 124 | the menu. |
124 | 125 | ||
125 | \sa insertItems() | 126 | \sa insertItems() |
126 | */ | 127 | */ |
127 | void MenuButton::insertItem( const QIconSet& icon, const QString& text ) | 128 | void MenuButton::insertItem( const QIconSet& icon, const QString& text ) |
128 | { | 129 | { |
129 | pop->insertItem(icon, text, nitems++); | 130 | pop->insertItem(icon, text, nitems++); |
130 | if ( nitems==1 ) select(0); | 131 | // if ( nitems==1 ) select(0); |
131 | } | 132 | } |
132 | 133 | ||
133 | /*! | 134 | /*! |
134 | \overload | 135 | \overload |
135 | Inserts a menu item with the label \a text into the menu. | 136 | Inserts a menu item with the label \a text into the menu. |
136 | 137 | ||
137 | \sa insertItems() | 138 | \sa insertItems() |
138 | */ | 139 | */ |
139 | void MenuButton::insertItem( const QString& text ) | 140 | void MenuButton::insertItem( const QString& text ) |
140 | { | 141 | { |
141 | pop->insertItem(text, nitems++); | 142 | pop->insertItem(text, nitems++); |
142 | if ( nitems==1 ) select(0); | 143 | // if ( nitems==1 ) select(0); |
143 | } | 144 | } |
144 | 145 | ||
145 | /*! | 146 | /*! |
146 | Inserts a separator into the menu. | 147 | Inserts a separator into the menu. |
147 | 148 | ||
148 | \sa insertItems() | 149 | \sa insertItems() |
149 | */ | 150 | */ |
150 | void MenuButton::insertSeparator() | 151 | void MenuButton::insertSeparator() |
151 | { | 152 | { |
152 | pop->insertSeparator(); | 153 | pop->insertSeparator(); |
153 | } | 154 | } |
154 | 155 | ||
155 | /*! | 156 | /*! |
156 | Selects the items with label text \a s. | 157 | Selects the items with label text \a s. |
157 | */ | 158 | */ |
158 | void MenuButton::select(const QString& s) | 159 | void MenuButton::select(const QString& s) |
159 | { | 160 | { |
160 | for (int i=0; i<nitems; i++) { | 161 | for (int i=0; i<nitems; i++) { |
161 | if ( pop->text(i) == s ) { | 162 | if ( pop->text(i) == s ) { |
162 | select(i); | 163 | select(i); |
163 | break; | 164 | break; |
164 | } | 165 | } |
165 | } | 166 | } |
166 | } | 167 | } |
167 | 168 | ||
168 | /*! | 169 | /*! |
169 | \overload | 170 | \overload |
170 | Selects the item at index position \a s. | 171 | Selects the item at index position \a s. |
171 | */ | 172 | */ |
172 | void MenuButton::select(int s) | 173 | void MenuButton::select(int s) |
173 | { | 174 | { |
174 | cur = s; | 175 | cur = s; |
175 | updateLabel(); | 176 | updateLabel(); |
176 | if ( pop->iconSet(cur) ) | 177 | if ( pop->iconSet(cur) ) |
177 | setIconSet(*pop->iconSet(cur)); | 178 | setIconSet(*pop->iconSet(cur)); |
178 | emit selected(cur); | 179 | emit selected(cur); |
179 | emit selected(currentText()); | 180 | emit selected(currentText()); |
180 | } | 181 | } |
181 | 182 | ||
182 | /*! | 183 | /*! |
183 | Returns the index position of the current item. | 184 | Returns the index position of the current item. |
184 | */ | 185 | */ |
185 | int MenuButton::currentItem() const | 186 | int MenuButton::currentItem() const |
186 | { | 187 | { |
187 | return cur; | 188 | return cur; |
188 | } | 189 | } |
189 | 190 | ||
190 | /*! | 191 | /*! |
191 | Returns the label text of the current item. | 192 | Returns the label text of the current item. |
192 | */ | 193 | */ |
193 | QString MenuButton::currentText() const | 194 | QString MenuButton::currentText() const |
194 | { | 195 | { |
195 | return pop->text(cur); | 196 | return pop->text(cur); |
196 | } | 197 | } |
197 | 198 | ||
198 | /*! | 199 | /*! |
199 | Sets the menubutton's label. If \a label is empty, the | 200 | Sets the menubutton's label. If \a label is empty, the |
200 | current item text is displayed, otherwise \a label should contain | 201 | current item text is displayed, otherwise \a label should contain |
201 | "%1", which will be replaced by the current item text. | 202 | "%1", which will be replaced by the current item text. |
202 | */ | 203 | */ |
203 | void MenuButton::setLabel(const QString& label) | 204 | void MenuButton::setLabel(const QString& label) |
204 | { | 205 | { |
205 | lab = label; | 206 | lab = label; |
206 | updateLabel(); | 207 | updateLabel(); |
207 | } | 208 | } |
208 | 209 | ||
209 | void MenuButton::updateLabel() | 210 | void MenuButton::updateLabel() |
210 | { | 211 | { |
211 | QString t = pop->text(cur); | 212 | if(useLabel) |
212 | if ( !lab.isEmpty() ) | 213 | { |
213 | t = lab.arg(t); | 214 | QString t = pop->text(cur); |
214 | setText(t); | 215 | if ( !lab.isEmpty() ) |
216 | t = lab.arg(t); | ||
217 | setText(t); | ||
218 | } | ||
215 | } | 219 | } |
216 | 220 | ||
221 | |||
222 | /*! | ||
223 | remove item at id | ||
224 | */ | ||
225 | void MenuButton::remove(int id) | ||
226 | { | ||
227 | pop->removeItem(id); | ||
228 | nitems--; | ||
229 | } | ||
230 | |||
231 | /*! | ||
232 | return count of items in menu | ||
233 | */ | ||
234 | int MenuButton::count() | ||
235 | { | ||
236 | return nitems; | ||
237 | } | ||
238 | |||
239 | /*! | ||
240 | returns text of item id | ||
241 | */ | ||
242 | QString MenuButton::text(int id) | ||
243 | { | ||
244 | return pop->text(id); | ||
245 | } | ||
246 | |||
247 | /*! | ||
248 | sets true or false the use of label | ||
249 | */ | ||
250 | void MenuButton::setUseLabel(bool b) | ||
251 | { | ||
252 | useLabel = b; | ||
253 | } | ||
diff --git a/library/menubutton.h b/library/menubutton.h index 6582b1e..ee5dcf1 100644 --- a/library/menubutton.h +++ b/library/menubutton.h | |||
@@ -1,63 +1,69 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of the Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 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. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | #ifndef MENUBUTTON_H | 20 | #ifndef MENUBUTTON_H |
21 | #define MENUBUTTON_H | 21 | #define MENUBUTTON_H |
22 | 22 | ||
23 | #include <qpushbutton.h> | 23 | #include <qpushbutton.h> |
24 | #include <qstringlist.h> | 24 | #include <qstringlist.h> |
25 | 25 | ||
26 | class MenuButton : public QPushButton { | 26 | class MenuButton : public QPushButton { |
27 | Q_OBJECT | 27 | Q_OBJECT |
28 | public: | 28 | public: |
29 | MenuButton( QWidget* parent, const char* name=0); | 29 | MenuButton( QWidget* parent, const char* name=0); |
30 | MenuButton( const QStringList& items, QWidget* parent, const char* name=0); | 30 | MenuButton( const QStringList& items, QWidget* parent, const char* name=0); |
31 | 31 | ||
32 | void clear(); | 32 | void clear(); |
33 | 33 | ||
34 | |||
34 | int currentItem() const; | 35 | int currentItem() const; |
35 | QString currentText() const; | 36 | QString currentText() const; |
36 | 37 | ||
37 | void insertItems( const QStringList& items ); | 38 | void insertItems( const QStringList& items ); |
38 | void insertItem( const QIconSet& icon, const QString& text=QString::null ); | 39 | void insertItem( const QIconSet& icon, const QString& text=QString::null ); |
39 | void insertItem( const QString& text ); | 40 | void insertItem( const QString& text ); |
40 | void insertSeparator(); | 41 | void insertSeparator(); |
41 | 42 | ||
42 | void setLabel(const QString& label); | 43 | void setLabel(const QString& label); |
44 | int count(); | ||
45 | void remove(int id); | ||
46 | QString text(int id); | ||
47 | void setUseLabel(bool b); | ||
43 | 48 | ||
44 | signals: | 49 | signals: |
45 | void selected(int); | 50 | void selected(int); |
46 | void selected(const QString&); | 51 | void selected(const QString&); |
47 | 52 | ||
48 | public slots: | 53 | public slots: |
49 | void select(int); | 54 | void select(int); |
50 | void select(const QString&); | 55 | void select(const QString&); |
51 | 56 | ||
52 | private: | 57 | private: |
58 | bool useLabel; | ||
53 | void init(); | 59 | void init(); |
54 | QStringList txts; | 60 | QStringList txts; |
55 | QPopupMenu* pop; | 61 | QPopupMenu* pop; |
56 | int nitems; | 62 | int nitems; |
57 | int cur; | 63 | int cur; |
58 | QString lab; | 64 | QString lab; |
59 | void updateLabel(); | 65 | void updateLabel(); |
60 | }; | 66 | }; |
61 | 67 | ||
62 | #endif | 68 | #endif |
63 | 69 | ||