-rw-r--r-- | library/categoryselect.cpp | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/library/categoryselect.cpp b/library/categoryselect.cpp new file mode 100644 index 0000000..dc5d1fa --- a/dev/null +++ b/library/categoryselect.cpp | |||
@@ -0,0 +1,315 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2001 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 <qpe/categories.h> | ||
22 | |||
23 | #include <qdialog.h> | ||
24 | #include <qlayout.h> | ||
25 | #include <qtoolbutton.h> | ||
26 | |||
27 | #include "categorywidget.h" | ||
28 | #include "categoryselect.h" | ||
29 | |||
30 | |||
31 | class CategoryComboPrivate | ||
32 | { | ||
33 | public: | ||
34 | CategoryComboPrivate(QObject *o) | ||
35 | : mCat( o ) | ||
36 | { | ||
37 | } | ||
38 | QArray<int> mAppCats; | ||
39 | QString mStrAppName; | ||
40 | QString mStrVisibleName; | ||
41 | Categories mCat; | ||
42 | }; | ||
43 | |||
44 | class CategorySelectPrivate | ||
45 | { | ||
46 | public: | ||
47 | CategorySelectPrivate( const QArray<int> &cats) | ||
48 | : mRec( cats ), | ||
49 | usingAll( false ) | ||
50 | { | ||
51 | } | ||
52 | CategorySelectPrivate() | ||
53 | { | ||
54 | } | ||
55 | QArray<int> mRec; | ||
56 | bool usingAll; | ||
57 | QString mVisibleName; | ||
58 | }; | ||
59 | |||
60 | CategoryCombo::CategoryCombo( QWidget *parent, const char *name ) | ||
61 | : QComboBox( parent, name ) | ||
62 | { | ||
63 | d = new CategoryComboPrivate(this); | ||
64 | } | ||
65 | |||
66 | void CategoryCombo::initCombo( const QArray<int> &recCats, | ||
67 | const QString &appName ) | ||
68 | { | ||
69 | initCombo( recCats, appName, appName ); | ||
70 | } | ||
71 | |||
72 | void CategoryCombo::initCombo( const QArray<int> &recCats, | ||
73 | const QString &appName, | ||
74 | const QString &visibleName ) | ||
75 | { | ||
76 | d->mStrAppName = appName; | ||
77 | d->mStrVisibleName = visibleName; | ||
78 | clear(); | ||
79 | QStringList slApp; | ||
80 | |||
81 | QObject::connect( this, SIGNAL(activated(int)), | ||
82 | this, SLOT(slotValueChanged(int)) ); | ||
83 | bool loadOk = d->mCat.load( categoryFileName() ); | ||
84 | slApp = d->mCat.labels( d->mStrAppName, TRUE, Categories::UnfiledLabel ); | ||
85 | d->mAppCats = d->mCat.ids( d->mStrAppName ); | ||
86 | |||
87 | int i, | ||
88 | j, | ||
89 | saveMe, | ||
90 | recCount; | ||
91 | QStringList::Iterator it; | ||
92 | // now add in all the items... | ||
93 | recCount = recCats.count(); | ||
94 | saveMe = -1; | ||
95 | if ( recCount > 1 && loadOk ) { | ||
96 | it = slApp.begin(); | ||
97 | insertItem( *it ); | ||
98 | ++it; | ||
99 | for ( j = 0; it != slApp.end(); ++it, j++ ) { | ||
100 | // grr... we have to go through and compare... | ||
101 | if ( j < int(d->mAppCats.size()) ) { | ||
102 | for ( i = 0; i < recCount; i++ ) { | ||
103 | if ( recCats[i] == d->mAppCats[j] ) { | ||
104 | (*it).append( tr(" (Multi.)") ); | ||
105 | if ( saveMe < 0 ) | ||
106 | saveMe = j; | ||
107 | // no need to continue through the list. | ||
108 | break; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | insertItem( *it ); | ||
113 | } | ||
114 | } else | ||
115 | insertStringList( slApp ); | ||
116 | |||
117 | if ( recCount > 0 && loadOk ) { | ||
118 | for ( i = 0; i < int(d->mAppCats.size()); i++ ) { | ||
119 | if ( d->mAppCats[i] == recCats[0] ) { | ||
120 | setCurrentItem( i + 1 ); | ||
121 | break; | ||
122 | } | ||
123 | } | ||
124 | } else | ||
125 | setCurrentItem( 0 ); // unfiled | ||
126 | QObject::connect( this, SIGNAL(activated(int)), | ||
127 | this, SLOT(slotValueChanged(int)) ); | ||
128 | } | ||
129 | |||
130 | CategoryCombo::~CategoryCombo() | ||
131 | { | ||
132 | delete d; | ||
133 | } | ||
134 | |||
135 | int CategoryCombo::currentCategory() const | ||
136 | { | ||
137 | int returnMe; | ||
138 | returnMe = currentItem(); | ||
139 | // unfiled is now 0... | ||
140 | if ( returnMe == 0 ) | ||
141 | returnMe = -1; | ||
142 | else if ( returnMe > (int)d->mAppCats.count() ) // only happen on "All" | ||
143 | returnMe = -2; | ||
144 | else | ||
145 | returnMe = d->mAppCats[returnMe - 1]; | ||
146 | return returnMe; | ||
147 | } | ||
148 | |||
149 | void CategoryCombo::setCurrentCategory( int newCatUid ) | ||
150 | { | ||
151 | int i; | ||
152 | for ( i = 0; i < int(d->mAppCats.size()); i++ ) { | ||
153 | if ( d->mAppCats[i] == newCatUid ) | ||
154 | setCurrentItem( i ); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | void CategoryCombo::setCurrentText( const QString &str ) | ||
159 | { | ||
160 | int i; | ||
161 | int stop; | ||
162 | stop = count(); | ||
163 | for ( i = 0; i < stop; i++ ) { | ||
164 | if ( text( i ) == str ) { | ||
165 | setCurrentItem( i ); | ||
166 | break; | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | void CategoryCombo::slotValueChanged( int ) | ||
172 | { | ||
173 | emit sigCatChanged( currentCategory() ); | ||
174 | } | ||
175 | |||
176 | CategorySelect::CategorySelect( QWidget *parent, const char *name ) | ||
177 | : QHBox( parent, name ), | ||
178 | cmbCat( 0 ), | ||
179 | cmdCat( 0 ), | ||
180 | d( 0 ) | ||
181 | { | ||
182 | d = new CategorySelectPrivate(); | ||
183 | init(); | ||
184 | } | ||
185 | |||
186 | CategorySelect::CategorySelect( const QArray<int> &vl, | ||
187 | const QString &appName, QWidget *parent, | ||
188 | const char *name ) | ||
189 | : QHBox( parent, name ) | ||
190 | { | ||
191 | d = new CategorySelectPrivate( vl ); | ||
192 | init(); | ||
193 | setCategories( vl, appName, appName ); | ||
194 | } | ||
195 | |||
196 | CategorySelect::CategorySelect( const QArray<int> &vl, | ||
197 | const QString &appName, | ||
198 | const QString &visibleName, | ||
199 | QWidget *parent, const char *name ) | ||
200 | : QHBox( parent, name ) | ||
201 | { | ||
202 | d = new CategorySelectPrivate( vl ); | ||
203 | init(); | ||
204 | setCategories( vl, appName, visibleName ); | ||
205 | } | ||
206 | |||
207 | CategorySelect::~CategorySelect() | ||
208 | { | ||
209 | delete d; | ||
210 | } | ||
211 | |||
212 | void CategorySelect::slotDialog() | ||
213 | { | ||
214 | QDialog editDlg( this, 0, TRUE ); | ||
215 | editDlg.setCaption( tr("Edit Categories") ); | ||
216 | QVBoxLayout *vb = new QVBoxLayout( &editDlg ); | ||
217 | QScrollView *sv = new QScrollView( &editDlg ); | ||
218 | sv->setResizePolicy( QScrollView::AutoOneFit ); | ||
219 | sv->setHScrollBarMode( QScrollView::AlwaysOff ); | ||
220 | vb->addWidget( sv ); | ||
221 | CategoryWidget ce( d->mRec, mStrAppName, d->mVisibleName, &editDlg ); | ||
222 | sv->addChild( &ce ); | ||
223 | editDlg.showMaximized(); | ||
224 | |||
225 | if ( editDlg.exec() ) { | ||
226 | d->mRec = ce.newCategories(); | ||
227 | cmbCat->initCombo( d->mRec, mStrAppName ); | ||
228 | } | ||
229 | } | ||
230 | |||
231 | void CategorySelect::slotNewCat( int newUid ) | ||
232 | { | ||
233 | if ( newUid != -1 ) { | ||
234 | bool alreadyIn = false; | ||
235 | for ( uint it = 0; it < d->mRec.count(); ++it ) { | ||
236 | if ( d->mRec[it] == newUid ) { | ||
237 | alreadyIn = true; | ||
238 | break; | ||
239 | } | ||
240 | } | ||
241 | if ( !alreadyIn ) { | ||
242 | d->mRec.resize( 1 ); | ||
243 | d->mRec[ 0 ] = newUid; | ||
244 | } | ||
245 | } else | ||
246 | d->mRec.resize(0); // now Unfiled. | ||
247 | emit signalSelected( currentCategory() ); | ||
248 | } | ||
249 | |||
250 | void CategorySelect::setCategories( const QArray<int> &rec, | ||
251 | const QString &appName ) | ||
252 | { | ||
253 | setCategories( rec, appName, appName ); | ||
254 | } | ||
255 | |||
256 | void CategorySelect::setCategories( const QArray<int> &rec, | ||
257 | const QString &appName, | ||
258 | const QString &visibleName ) | ||
259 | { | ||
260 | d->mRec = rec; | ||
261 | d->mVisibleName = visibleName; | ||
262 | mStrAppName = appName; | ||
263 | cmbCat->initCombo( rec, appName ); | ||
264 | } | ||
265 | |||
266 | void CategorySelect::init() | ||
267 | { | ||
268 | cmbCat = new CategoryCombo( this ); | ||
269 | QObject::connect( cmbCat, SIGNAL(sigCatChanged(int)), | ||
270 | this, SLOT(slotNewCat(int)) ); | ||
271 | cmdCat = new QToolButton( this ); | ||
272 | QObject::connect( cmdCat, SIGNAL(clicked()), this, SLOT(slotDialog()) ); | ||
273 | cmdCat->setTextLabel( "...", FALSE ); | ||
274 | cmdCat->setUsesTextLabel( true ); | ||
275 | cmdCat->setMaximumSize( cmdCat->sizeHint() ); | ||
276 | cmdCat->setFocusPolicy( TabFocus ); | ||
277 | } | ||
278 | |||
279 | |||
280 | int CategorySelect::currentCategory() const | ||
281 | { | ||
282 | return cmbCat->currentCategory(); | ||
283 | } | ||
284 | |||
285 | void CategorySelect::setCurrentCategory( int newCatUid ) | ||
286 | { | ||
287 | cmbCat->setCurrentCategory( newCatUid ); | ||
288 | } | ||
289 | |||
290 | |||
291 | const QArray<int> &CategorySelect::currentCategories() const | ||
292 | { | ||
293 | return d->mRec; | ||
294 | } | ||
295 | |||
296 | void CategorySelect::setRemoveCategoryEdit( bool remove ) | ||
297 | { | ||
298 | if ( remove ) { | ||
299 | cmdCat->setEnabled( FALSE ); | ||
300 | cmdCat->hide(); | ||
301 | } else { | ||
302 | cmdCat->setEnabled( TRUE ); | ||
303 | cmdCat->show(); | ||
304 | } | ||
305 | } | ||
306 | |||
307 | void CategorySelect::setAllCategories( bool add ) | ||
308 | { | ||
309 | d->usingAll = add; | ||
310 | if ( add ) { | ||
311 | cmbCat->insertItem( tr( "All" ), cmbCat->count() ); | ||
312 | cmbCat->setCurrentItem( cmbCat->count() - 1 ); | ||
313 | } else | ||
314 | cmbCat->removeItem( cmbCat->count() - 1 ); | ||
315 | } | ||