-rw-r--r-- | library/menubutton.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/library/menubutton.cpp b/library/menubutton.cpp index f5c832c..007761f 100644 --- a/library/menubutton.cpp +++ b/library/menubutton.cpp | |||
@@ -1,216 +1,216 @@ | |||
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 | 20 | ||
21 | #include "menubutton.h" | 21 | #include "menubutton.h" |
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 | init(); | 70 | init(); |
71 | insertItems(items); | 71 | insertItems(items); |
72 | } | 72 | } |
73 | 73 | ||
74 | /*! | 74 | /*! |
75 | Constructs an empty MenuButton. | 75 | Constructs an empty MenuButton. |
76 | The standard \a parent an \a name arguments are passed to the base class. | 76 | The standard \a parent an \a name arguments are passed to the base class. |
77 | 77 | ||
78 | \sa insertItem() insertItems() | 78 | \sa insertItem() insertItems() |
79 | */ | 79 | */ |
80 | MenuButton::MenuButton( QWidget* parent, const char* name) : | 80 | MenuButton::MenuButton( QWidget* parent, const char* name) : |
81 | QPushButton(parent,name) | 81 | QPushButton(parent,name) |
82 | { | 82 | { |
83 | init(); | 83 | init(); |
84 | } | 84 | } |
85 | 85 | ||
86 | void MenuButton::init() | 86 | void MenuButton::init() |
87 | { | 87 | { |
88 | setAutoDefault(FALSE); | 88 | setAutoDefault(FALSE); |
89 | pop = new QPopupMenu(this); | 89 | pop = new QPopupMenu(this); |
90 | nitems=0; | 90 | nitems=0; |
91 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); | 91 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); |
92 | setPopup(pop); | 92 | setPopup(pop); |
93 | //setPopupDelay(0); | 93 | //setPopupDelay(0); |
94 | } | 94 | } |
95 | 95 | ||
96 | /*! | 96 | /*! |
97 | Removes all the menu items from the button and menu. | 97 | Removes all the menu items from the button and menu. |
98 | */ | 98 | */ |
99 | void MenuButton::clear() | 99 | void MenuButton::clear() |
100 | { | 100 | { |
101 | delete pop; | 101 | delete pop; |
102 | init(); | 102 | init(); |
103 | } | 103 | } |
104 | 104 | ||
105 | /*! | 105 | /*! |
106 | A menu item is created (see insertItem()) for each string in the \a | 106 | A menu item is created (see insertItem()) for each string in the \a |
107 | items string list. If any string is "--" a separator (see | 107 | items string list. If any string is "--" a separator (see |
108 | insertSeparator()) is inserted in its place. | 108 | insertSeparator()) is inserted in its place. |
109 | */ | 109 | */ |
110 | void MenuButton::insertItems( const QStringList& items ) | 110 | void MenuButton::insertItems( const QStringList& items ) |
111 | { | 111 | { |
112 | QStringList::ConstIterator it=items.begin(); | 112 | QStringList::ConstIterator it=items.begin(); |
113 | for (; it!=items.end(); ++it) { | 113 | for (; it!=items.end(); ++it) { |
114 | if ( (*it) == "--" ) | 114 | if ( (*it) == "--" ) |
115 | insertSeparator(); | 115 | insertSeparator(); |
116 | else | 116 | else |
117 | insertItem(*it); | 117 | insertItem(*it); |
118 | } | 118 | } |
119 | } | 119 | } |
120 | 120 | ||
121 | /*! | 121 | /*! |
122 | Inserts a menu item with the icon \a icon and label \a text into | 122 | Inserts a menu item with the icon \a icon and label \a text into |
123 | the menu. | 123 | the menu. |
124 | 124 | ||
125 | \sa insertItems() | 125 | \sa insertItems() |
126 | */ | 126 | */ |
127 | void MenuButton::insertItem( const QIconSet& icon, const QString& text=QString::null ) | 127 | void MenuButton::insertItem( const QIconSet& icon, const QString& text ) |
128 | { | 128 | { |
129 | pop->insertItem(icon, text, nitems++); | 129 | pop->insertItem(icon, text, nitems++); |
130 | if ( nitems==1 ) select(0); | 130 | if ( nitems==1 ) select(0); |
131 | } | 131 | } |
132 | 132 | ||
133 | /*! | 133 | /*! |
134 | \overload | 134 | \overload |
135 | Inserts a menu item with the label \a text into the menu. | 135 | Inserts a menu item with the label \a text into the menu. |
136 | 136 | ||
137 | \sa insertItems() | 137 | \sa insertItems() |
138 | */ | 138 | */ |
139 | void MenuButton::insertItem( const QString& text ) | 139 | void MenuButton::insertItem( const QString& text ) |
140 | { | 140 | { |
141 | pop->insertItem(text, nitems++); | 141 | pop->insertItem(text, nitems++); |
142 | if ( nitems==1 ) select(0); | 142 | if ( nitems==1 ) select(0); |
143 | } | 143 | } |
144 | 144 | ||
145 | /*! | 145 | /*! |
146 | Inserts a separator into the menu. | 146 | Inserts a separator into the menu. |
147 | 147 | ||
148 | \sa insertItems() | 148 | \sa insertItems() |
149 | */ | 149 | */ |
150 | void MenuButton::insertSeparator() | 150 | void MenuButton::insertSeparator() |
151 | { | 151 | { |
152 | pop->insertSeparator(); | 152 | pop->insertSeparator(); |
153 | } | 153 | } |
154 | 154 | ||
155 | /*! | 155 | /*! |
156 | Selects the items with label text \a s. | 156 | Selects the items with label text \a s. |
157 | */ | 157 | */ |
158 | void MenuButton::select(const QString& s) | 158 | void MenuButton::select(const QString& s) |
159 | { | 159 | { |
160 | for (int i=0; i<nitems; i++) { | 160 | for (int i=0; i<nitems; i++) { |
161 | if ( pop->text(i) == s ) { | 161 | if ( pop->text(i) == s ) { |
162 | select(i); | 162 | select(i); |
163 | break; | 163 | break; |
164 | } | 164 | } |
165 | } | 165 | } |
166 | } | 166 | } |
167 | 167 | ||
168 | /*! | 168 | /*! |
169 | \overload | 169 | \overload |
170 | Selects the item at index position \a s. | 170 | Selects the item at index position \a s. |
171 | */ | 171 | */ |
172 | void MenuButton::select(int s) | 172 | void MenuButton::select(int s) |
173 | { | 173 | { |
174 | cur = s; | 174 | cur = s; |
175 | updateLabel(); | 175 | updateLabel(); |
176 | if ( pop->iconSet(cur) ) | 176 | if ( pop->iconSet(cur) ) |
177 | setIconSet(*pop->iconSet(cur)); | 177 | setIconSet(*pop->iconSet(cur)); |
178 | emit selected(cur); | 178 | emit selected(cur); |
179 | emit selected(currentText()); | 179 | emit selected(currentText()); |
180 | } | 180 | } |
181 | 181 | ||
182 | /*! | 182 | /*! |
183 | Returns the index position of the current item. | 183 | Returns the index position of the current item. |
184 | */ | 184 | */ |
185 | int MenuButton::currentItem() const | 185 | int MenuButton::currentItem() const |
186 | { | 186 | { |
187 | return cur; | 187 | return cur; |
188 | } | 188 | } |
189 | 189 | ||
190 | /*! | 190 | /*! |
191 | Returns the label text of the current item. | 191 | Returns the label text of the current item. |
192 | */ | 192 | */ |
193 | QString MenuButton::currentText() const | 193 | QString MenuButton::currentText() const |
194 | { | 194 | { |
195 | return pop->text(cur); | 195 | return pop->text(cur); |
196 | } | 196 | } |
197 | 197 | ||
198 | /*! | 198 | /*! |
199 | Sets the menubutton's label. If \a label is empty, the | 199 | Sets the menubutton's label. If \a label is empty, the |
200 | current item text is displayed, otherwise \a label should contain | 200 | current item text is displayed, otherwise \a label should contain |
201 | "%1", which will be replaced by the current item text. | 201 | "%1", which will be replaced by the current item text. |
202 | */ | 202 | */ |
203 | void MenuButton::setLabel(const QString& label) | 203 | void MenuButton::setLabel(const QString& label) |
204 | { | 204 | { |
205 | lab = label; | 205 | lab = label; |
206 | updateLabel(); | 206 | updateLabel(); |
207 | } | 207 | } |
208 | 208 | ||
209 | void MenuButton::updateLabel() | 209 | void MenuButton::updateLabel() |
210 | { | 210 | { |
211 | QString t = pop->text(cur); | 211 | QString t = pop->text(cur); |
212 | if ( !lab.isEmpty() ) | 212 | if ( !lab.isEmpty() ) |
213 | t = lab.arg(t); | 213 | t = lab.arg(t); |
214 | setText(t); | 214 | setText(t); |
215 | } | 215 | } |
216 | 216 | ||