summaryrefslogtreecommitdiff
path: root/library/categoryedit_p.cpp
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /library/categoryedit_p.cpp
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'library/categoryedit_p.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/categoryedit_p.cpp227
1 files changed, 227 insertions, 0 deletions
diff --git a/library/categoryedit_p.cpp b/library/categoryedit_p.cpp
new file mode 100644
index 0000000..06e5fec
--- a/dev/null
+++ b/library/categoryedit_p.cpp
@@ -0,0 +1,227 @@
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 "categoryedit_p.h"
22
23#include <qpe/categories.h>
24
25#include <qdir.h>
26#include <qcheckbox.h>
27#include <qlineedit.h>
28#include <qlistview.h>
29#include <qstringlist.h>
30#include <qtoolbutton.h>
31
32#include <sys/types.h>
33#include <sys/stat.h>
34
35#include <stdlib.h>
36
37
38using namespace Qtopia;
39
40class CategoryEditPrivate
41{
42public:
43 CategoryEditPrivate( QWidget *parent, const QString &appName )
44 : mCategories( parent, "" ),
45 mStrApp( appName )
46 {
47 editItem = 0;
48 mCategories.load( categoryFileName() );
49 }
50 Categories mCategories;
51 QListViewItem *editItem;
52 QString mStrApp;
53 QString mVisible;
54};
55
56CategoryEdit::CategoryEdit( QWidget *parent, const char *name )
57 : CategoryEditBase( parent, name )
58{
59 d = 0;
60}
61
62CategoryEdit::CategoryEdit( const QArray<int> &recCats,
63 const QString &appName, const QString &visibleName,
64 QWidget *parent, const char *name )
65 : CategoryEditBase( parent, name )
66{
67 d = 0;
68 setCategories( recCats, appName, visibleName );
69}
70
71void CategoryEdit::setCategories( const QArray<int> &recCats,
72 const QString &appName, const QString &visibleName )
73{
74 if ( !d )
75 d = new CategoryEditPrivate( (QWidget*)parent(), name() );
76 d->mStrApp = appName;
77 d->mVisible = visibleName;
78 QArray<int> cats = d->mCategories.ids( d->mStrApp );
79 lvView->clear();
80 QStringList appCats = d->mCategories.labels( d->mStrApp );
81 QStringList::ConstIterator it;
82 int i, j;
83 for ( i = 0, it = appCats.begin(); it != appCats.end(); i++, ++it ) {
84 QCheckListItem *chk;
85 chk = new QCheckListItem( lvView, (*it), QCheckListItem::CheckBox );
86 if ( !d->mCategories.isGlobal((*it)) )
87 chk->setText( 1, tr(d->mVisible) );
88 else
89 chk->setText( 1, tr("All") );
90 // Is this record using this category, then we should check it
91 for ( j = 0; j < int(recCats.count()); j++ ) {
92 if ( cats[i] == recCats[j] ) {
93 chk->setOn( true );
94 break;
95 }
96 }
97 }
98 lvView->setSorting( 0, TRUE );
99 lvView->sort();
100 if ( lvView->childCount() < 1 )
101 txtCat->setEnabled( FALSE );
102 else {
103 lvView->setSelected( lvView->firstChild(), true );
104 }
105}
106
107CategoryEdit::~CategoryEdit()
108{
109 if ( d )
110 delete d;
111}
112
113void CategoryEdit::slotSetText( QListViewItem *selected )
114{
115 d->editItem = selected;
116 if ( !d->editItem )
117 return;
118 txtCat->setText( d->editItem->text(0) );
119 txtCat->setEnabled( true );
120 if ( d->editItem->text(1) == tr("All") )
121 chkGlobal->setChecked( true );
122 else
123 chkGlobal->setChecked( false );
124}
125
126void CategoryEdit::slotAdd()
127{
128 QString name = tr( "New Category" );
129 bool insertOk = FALSE;
130 int num = 0;
131 while ( !insertOk ) {
132 if ( num++ > 0 )
133 name = tr("New Category ") + QString::number(num);
134 insertOk = d->mCategories.addCategory( d->mStrApp, name );
135 }
136 QCheckListItem *chk;
137 chk = new QCheckListItem( lvView, name, QCheckListItem::CheckBox );
138 if ( !chkGlobal->isChecked() )
139 chk->setText( 1, tr(d->mVisible) );
140 else
141 chk->setText( 1, tr("All") );
142
143 lvView->setSelected( chk, TRUE );
144 txtCat->selectAll();
145}
146
147void CategoryEdit::slotRemove()
148{
149 d->editItem = lvView->selectedItem();
150 if ( d->editItem ) {
151 QListViewItem *sibling = d->editItem->nextSibling();
152
153 d->mCategories.removeCategory( d->mStrApp, d->editItem->text(0) );
154
155 delete d->editItem;
156 d->editItem = 0;
157
158 if ( sibling )
159 lvView->setSelected( sibling, TRUE );
160 }
161 if ( lvView->childCount() < 1 ) {
162 txtCat->clear();
163 txtCat->setEnabled( FALSE );
164 }
165}
166
167void CategoryEdit::slotSetGlobal( bool isChecked )
168{
169 if ( d->editItem ) {
170 if ( isChecked )
171 d->editItem->setText( 1, tr("All") );
172 else
173 d->editItem->setText( 1, tr(d->mVisible) );
174
175 d->mCategories.setGlobal( d->mStrApp, d->editItem->text( 0 ), isChecked );
176 }
177}
178
179void CategoryEdit::slotTextChanged( const QString &strNew )
180{
181 if ( d->editItem ) {
182 if ( chkGlobal->isChecked() )
183 d->mCategories.renameGlobalCategory( d->editItem->text(0), strNew );
184 else
185 d->mCategories.renameCategory( d->mStrApp, d->editItem->text(0), strNew );
186 d->editItem->setText( 0, strNew );
187 }
188}
189
190QArray<int> CategoryEdit::newCategories()
191{
192 QArray<int> a;
193 if ( d ) {
194 d->mCategories.save( categoryFileName() );
195 QListViewItemIterator it( lvView );
196 QValueList<int> l;
197 for ( ; it.current(); ++it ) {
198 if ( reinterpret_cast<QCheckListItem*>(it.current())->isOn() )
199 l.append( d->mCategories.id( d->mStrApp, it.current()->text(0) ) );
200 }
201 uint i = 0;
202 a.resize( l.count() );
203 for ( QValueList<int>::Iterator lit = l.begin(); lit != l.end(); ++lit )
204 a[i++] = *lit;
205 }
206 return a;
207}
208
209void CategoryEdit::accept()
210{
211 // write our categories out...
212 d->mCategories.save( categoryFileName() );
213 // QDialog::accept();
214}
215
216QString categoryFileName()
217{
218 QDir dir = (QString(getenv("HOME")) + "/Settings");
219 if ( !dir.exists() )
220 mkdir( dir.path().local8Bit(), 0700 );
221 return dir.path() + "/" + "Categories" + ".xml";
222}
223
224void CategoryEdit::kludge()
225{
226 lvView->setMaximumHeight( 130 );
227}