-rw-r--r-- | library/menubutton.cpp | 71 |
1 files changed, 50 insertions, 21 deletions
diff --git a/library/menubutton.cpp b/library/menubutton.cpp index 6b1fa2b..f5c832c 100644 --- a/library/menubutton.cpp +++ b/library/menubutton.cpp | |||
@@ -1,187 +1,216 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of 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 | Similar in function to a QComboBox. | 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, | ||
30 | and optionally an icon. | ||
31 | |||
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 | ||
34 | currentText(). | ||
35 | |||
36 | Menu items are inserted with the \link MenuButton::MenuButton() | ||
37 | constructor\endlink, insertItem() or insertItems(). Separators are | ||
38 | inserted with insertSeparator(). All the items in the menu can be | ||
39 | removed by calling clear(). | ||
40 | |||
41 | Items can be selected programmatically using select(). When a menu | ||
42 | item is selected (programmatically or by the user), the selected() | ||
43 | signal is emitted. | ||
44 | |||
45 | \ingroup qtopiaemb | ||
29 | */ | 46 | */ |
30 | 47 | ||
31 | /*! | 48 | /*! |
32 | \fn void MenuButton::selected(int index) | 49 | \overload void MenuButton::selected(int index) |
33 | 50 | ||
34 | This signal is emitted when the item at \a index is selected. | 51 | This signal is emitted when the item at position \a index is selected. |
35 | */ | 52 | */ |
36 | 53 | ||
37 | /*! | 54 | /*! |
38 | \fn void MenuButton::selected(const QString& text) | 55 | \fn void MenuButton::selected(const QString& text) |
39 | 56 | ||
40 | This signal is emitted when the item with the given \a text is selected. | 57 | This signal is emitted when the item with the label \a text is selected. |
41 | */ | 58 | */ |
42 | 59 | ||
43 | 60 | ||
44 | /*! | 61 | /*! |
45 | Constructs a MenuButton with the given \a items added (see insertItems()). | 62 | Constructs a MenuButton. A menu item is created (see insertItem() |
46 | The standard \a parent an \a name arguments are passed to the base class. | 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 | ||
65 | class. | ||
47 | */ | 66 | */ |
48 | MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) : | 67 | MenuButton::MenuButton( const QStringList& items, QWidget* parent, const char* name) : |
49 | QPushButton(parent,name) | 68 | QPushButton(parent,name) |
50 | { | 69 | { |
51 | init(); | 70 | init(); |
52 | insertItems(items); | 71 | insertItems(items); |
53 | } | 72 | } |
54 | 73 | ||
55 | /*! | 74 | /*! |
56 | Constructs an empty MenuButton. | 75 | Constructs an empty MenuButton. |
57 | 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. |
58 | 77 | ||
59 | \sa insertItems() | 78 | \sa insertItem() insertItems() |
60 | */ | 79 | */ |
61 | MenuButton::MenuButton( QWidget* parent, const char* name) : | 80 | MenuButton::MenuButton( QWidget* parent, const char* name) : |
62 | QPushButton(parent,name) | 81 | QPushButton(parent,name) |
63 | { | 82 | { |
64 | init(); | 83 | init(); |
65 | } | 84 | } |
66 | 85 | ||
67 | void MenuButton::init() | 86 | void MenuButton::init() |
68 | { | 87 | { |
69 | setAutoDefault(FALSE); | 88 | setAutoDefault(FALSE); |
70 | pop = new QPopupMenu(this); | 89 | pop = new QPopupMenu(this); |
71 | nitems=0; | 90 | nitems=0; |
72 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); | 91 | connect(pop, SIGNAL(activated(int)), this, SLOT(select(int))); |
73 | setPopup(pop); | 92 | setPopup(pop); |
74 | //setPopupDelay(0); | 93 | //setPopupDelay(0); |
75 | } | 94 | } |
76 | 95 | ||
77 | /*! | 96 | /*! |
78 | Removes all items from the button and menu. | 97 | Removes all the menu items from the button and menu. |
79 | */ | 98 | */ |
80 | void MenuButton::clear() | 99 | void MenuButton::clear() |
81 | { | 100 | { |
82 | delete pop; | 101 | delete pop; |
83 | init(); | 102 | init(); |
84 | } | 103 | } |
85 | 104 | ||
86 | /*! | 105 | /*! |
87 | Inserts string \a items into the menu. The string "--" represents | 106 | A menu item is created (see insertItem()) for each string in the \a |
88 | a separator. | 107 | items string list. If any string is "--" a separator (see |
108 | insertSeparator()) is inserted in its place. | ||
89 | */ | 109 | */ |
90 | void MenuButton::insertItems( const QStringList& items ) | 110 | void MenuButton::insertItems( const QStringList& items ) |
91 | { | 111 | { |
92 | QStringList::ConstIterator it=items.begin(); | 112 | QStringList::ConstIterator it=items.begin(); |
93 | for (; it!=items.end(); ++it) { | 113 | for (; it!=items.end(); ++it) { |
94 | if ( (*it) == "--" ) | 114 | if ( (*it) == "--" ) |
95 | insertSeparator(); | 115 | insertSeparator(); |
96 | else | 116 | else |
97 | insertItem(*it); | 117 | insertItem(*it); |
98 | } | 118 | } |
99 | } | 119 | } |
100 | 120 | ||
101 | /*! | 121 | /*! |
102 | Inserts an \a icon and \a text into the menu. | 122 | Inserts a menu item with the icon \a icon and label \a text into |
123 | the menu. | ||
124 | |||
125 | \sa insertItems() | ||
103 | */ | 126 | */ |
104 | void MenuButton::insertItem( const QIconSet& icon, const QString& text ) | 127 | void MenuButton::insertItem( const QIconSet& icon, const QString& text=QString::null ) |
105 | { | 128 | { |
106 | pop->insertItem(icon, text, nitems++); | 129 | pop->insertItem(icon, text, nitems++); |
107 | if ( nitems==1 ) select(0); | 130 | if ( nitems==1 ) select(0); |
108 | } | 131 | } |
109 | 132 | ||
110 | /*! | 133 | /*! |
111 | Inserts \a text into the menu. | 134 | \overload |
135 | Inserts a menu item with the label \a text into the menu. | ||
136 | |||
137 | \sa insertItems() | ||
112 | */ | 138 | */ |
113 | void MenuButton::insertItem( const QString& text ) | 139 | void MenuButton::insertItem( const QString& text ) |
114 | { | 140 | { |
115 | pop->insertItem(text, nitems++); | 141 | pop->insertItem(text, nitems++); |
116 | if ( nitems==1 ) select(0); | 142 | if ( nitems==1 ) select(0); |
117 | } | 143 | } |
118 | 144 | ||
119 | /*! | 145 | /*! |
120 | Inserts a visual separator into the menu. | 146 | Inserts a separator into the menu. |
147 | |||
148 | \sa insertItems() | ||
121 | */ | 149 | */ |
122 | void MenuButton::insertSeparator() | 150 | void MenuButton::insertSeparator() |
123 | { | 151 | { |
124 | pop->insertSeparator(); | 152 | pop->insertSeparator(); |
125 | } | 153 | } |
126 | 154 | ||
127 | /*! | 155 | /*! |
128 | Selects the items with text \a s. | 156 | Selects the items with label text \a s. |
129 | */ | 157 | */ |
130 | void MenuButton::select(const QString& s) | 158 | void MenuButton::select(const QString& s) |
131 | { | 159 | { |
132 | for (int i=0; i<nitems; i++) { | 160 | for (int i=0; i<nitems; i++) { |
133 | if ( pop->text(i) == s ) { | 161 | if ( pop->text(i) == s ) { |
134 | select(i); | 162 | select(i); |
135 | break; | 163 | break; |
136 | } | 164 | } |
137 | } | 165 | } |
138 | } | 166 | } |
139 | 167 | ||
140 | /*! | 168 | /*! |
141 | Selects the item at index \a s. | 169 | \overload |
170 | Selects the item at index position \a s. | ||
142 | */ | 171 | */ |
143 | void MenuButton::select(int s) | 172 | void MenuButton::select(int s) |
144 | { | 173 | { |
145 | cur = s; | 174 | cur = s; |
146 | updateLabel(); | 175 | updateLabel(); |
147 | if ( pop->iconSet(cur) ) | 176 | if ( pop->iconSet(cur) ) |
148 | setIconSet(*pop->iconSet(cur)); | 177 | setIconSet(*pop->iconSet(cur)); |
149 | emit selected(cur); | 178 | emit selected(cur); |
150 | emit selected(currentText()); | 179 | emit selected(currentText()); |
151 | } | 180 | } |
152 | 181 | ||
153 | /*! | 182 | /*! |
154 | Returns the index of the current item. | 183 | Returns the index position of the current item. |
155 | */ | 184 | */ |
156 | int MenuButton::currentItem() const | 185 | int MenuButton::currentItem() const |
157 | { | 186 | { |
158 | return cur; | 187 | return cur; |
159 | } | 188 | } |
160 | 189 | ||
161 | /*! | 190 | /*! |
162 | Returns the text of the current item. | 191 | Returns the label text of the current item. |
163 | */ | 192 | */ |
164 | QString MenuButton::currentText() const | 193 | QString MenuButton::currentText() const |
165 | { | 194 | { |
166 | return pop->text(cur); | 195 | return pop->text(cur); |
167 | } | 196 | } |
168 | 197 | ||
169 | /*! | 198 | /*! |
170 | Sets the label. If \a label is empty, the | 199 | Sets the menubutton's label. If \a label is empty, the |
171 | current item text is displayed, otherwise \a label should contain | 200 | current item text is displayed, otherwise \a label should contain |
172 | "%1", which will be replaced by the current item text. | 201 | "%1", which will be replaced by the current item text. |
173 | */ | 202 | */ |
174 | void MenuButton::setLabel(const QString& label) | 203 | void MenuButton::setLabel(const QString& label) |
175 | { | 204 | { |
176 | lab = label; | 205 | lab = label; |
177 | updateLabel(); | 206 | updateLabel(); |
178 | } | 207 | } |
179 | 208 | ||
180 | void MenuButton::updateLabel() | 209 | void MenuButton::updateLabel() |
181 | { | 210 | { |
182 | QString t = pop->text(cur); | 211 | QString t = pop->text(cur); |
183 | if ( !lab.isEmpty() ) | 212 | if ( !lab.isEmpty() ) |
184 | t = lab.arg(t); | 213 | t = lab.arg(t); |
185 | setText(t); | 214 | setText(t); |
186 | } | 215 | } |
187 | 216 | ||