categoryedit_p.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "categoryedit_p.h"
00022
00023 #include <qpe/categories.h>
00024
00025 #include <qdir.h>
00026 #include <qcheckbox.h>
00027 #include <qlineedit.h>
00028 #include <qlistview.h>
00029 #include <qstringlist.h>
00030 #include <qtoolbutton.h>
00031
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034
00035 #include <stdlib.h>
00036
00037
00038 using namespace Qtopia;
00039
00040 class CategoryEditPrivate
00041 {
00042 public:
00043 CategoryEditPrivate( QWidget *parent, const QString &appName )
00044 : mCategories( parent, "" ),
00045 mStrApp( appName )
00046 {
00047 editItem = 0;
00048 mCategories.load( categoryFileName() );
00049 }
00050 Categories mCategories;
00051 QListViewItem *editItem;
00052 QString mStrApp;
00053 QString mVisible;
00054 };
00055
00056 CategoryEdit::CategoryEdit( QWidget *parent, const char *name )
00057 : CategoryEditBase( parent, name )
00058 {
00059 d = 0;
00060 }
00061
00062 CategoryEdit::CategoryEdit( const QArray<int> &recCats,
00063 const QString &appName, const QString &visibleName,
00064 QWidget *parent, const char *name )
00065 : CategoryEditBase( parent, name )
00066 {
00067 d = 0;
00068 setCategories( recCats, appName, visibleName );
00069 }
00070
00071 void CategoryEdit::setCategories( const QArray<int> &recCats,
00072 const QString &appName, const QString &visibleName )
00073 {
00074 if ( !d )
00075 d = new CategoryEditPrivate( (QWidget*)parent(), name() );
00076 d->mStrApp = appName;
00077 d->mVisible = visibleName;
00078
00079 QStringList appCats = d->mCategories.labels( d->mStrApp );
00080 QArray<int> cats = d->mCategories.ids(d->mStrApp, appCats);
00081 lvView->clear();
00082
00083 QStringList::ConstIterator it;
00084 int i, j;
00085 for ( i = 0, it = appCats.begin(); it != appCats.end(); i++, ++it ) {
00086 QCheckListItem *chk;
00087 chk = new QCheckListItem( lvView, (*it), QCheckListItem::CheckBox );
00088 if ( !d->mCategories.isGlobal((*it)) )
00089 chk->setText( 1, tr(d->mVisible) );
00090 else
00091 chk->setText( 1, tr("All") );
00092
00093 for ( j = 0; j < int(recCats.count()); j++ ) {
00094 if ( cats[i] == recCats[j] ) {
00095 chk->setOn( true );
00096 break;
00097 }
00098 }
00099 }
00100 lvView->setSorting( 0, TRUE );
00101 lvView->sort();
00102 if ( lvView->childCount() < 1 )
00103 txtCat->setEnabled( FALSE );
00104 else {
00105 lvView->setSelected( lvView->firstChild(), true );
00106 }
00107 }
00108
00109 CategoryEdit::~CategoryEdit()
00110 {
00111 if ( d )
00112 delete d;
00113 }
00114
00115 void CategoryEdit::slotSetText( QListViewItem *selected )
00116 {
00117 d->editItem = selected;
00118 if ( !d->editItem )
00119 return;
00120 txtCat->setText( d->editItem->text(0) );
00121 txtCat->setEnabled( true );
00122 if ( d->editItem->text(1) == tr("All") )
00123 chkGlobal->setChecked( true );
00124 else
00125 chkGlobal->setChecked( false );
00126 }
00127
00128 void CategoryEdit::slotAdd()
00129 {
00130 QString name = tr( "New Category" );
00131 bool insertOk = FALSE;
00132 int num = 0;
00133 while ( !insertOk ) {
00134 if ( num++ > 0 )
00135 name = tr("New Category ") + QString::number(num);
00136 insertOk = d->mCategories.addCategory( d->mStrApp, name );
00137 }
00138 QCheckListItem *chk;
00139 chk = new QCheckListItem( lvView, name, QCheckListItem::CheckBox );
00140 if ( !chkGlobal->isChecked() )
00141 chk->setText( 1, tr(d->mVisible) );
00142 else
00143 chk->setText( 1, tr("All") );
00144
00145 lvView->setSelected( chk, TRUE );
00146 txtCat->selectAll();
00147 txtCat->setFocus();
00148 }
00149
00150 void CategoryEdit::slotRemove()
00151 {
00152 d->editItem = lvView->selectedItem();
00153 if ( d->editItem ) {
00154 QListViewItem *sibling = d->editItem->nextSibling();
00155
00156 d->mCategories.removeCategory( d->mStrApp, d->editItem->text(0) );
00157
00158 delete d->editItem;
00159 d->editItem = 0;
00160
00161 if ( sibling )
00162 lvView->setSelected( sibling, TRUE );
00163 }
00164 if ( lvView->childCount() < 1 ) {
00165 txtCat->clear();
00166 txtCat->setEnabled( FALSE );
00167 }
00168 }
00169
00170 void CategoryEdit::slotSetGlobal( bool isChecked )
00171 {
00172 if ( d->editItem ) {
00173 if ( isChecked )
00174 d->editItem->setText( 1, tr("All") );
00175 else
00176 d->editItem->setText( 1, tr(d->mVisible) );
00177
00178 d->mCategories.setGlobal( d->mStrApp, d->editItem->text( 0 ), isChecked );
00179 }
00180 }
00181
00182 void CategoryEdit::slotTextChanged( const QString &strNew )
00183 {
00184 if ( d->editItem ) {
00185 if ( chkGlobal->isChecked() )
00186 d->mCategories.renameGlobalCategory( d->editItem->text(0), strNew );
00187 else
00188 d->mCategories.renameCategory( d->mStrApp, d->editItem->text(0), strNew );
00189 d->editItem->setText( 0, strNew );
00190 }
00191 }
00192
00193 QArray<int> CategoryEdit::newCategories()
00194 {
00195 QArray<int> a;
00196 if ( d ) {
00197 d->mCategories.save( categoryFileName() );
00198 QListViewItemIterator it( lvView );
00199 QValueList<int> l;
00200 for ( ; it.current(); ++it ) {
00201 if ( reinterpret_cast<QCheckListItem*>(it.current())->isOn() )
00202 l.append( d->mCategories.id( d->mStrApp, it.current()->text(0) ) );
00203 }
00204 uint i = 0;
00205 a.resize( l.count() );
00206 for ( QValueList<int>::Iterator lit = l.begin(); lit != l.end(); ++lit )
00207 a[i++] = *lit;
00208 }
00209 return a;
00210 }
00211
00212 void CategoryEdit::accept()
00213 {
00214
00215 d->mCategories.save( categoryFileName() );
00216
00217 }
00218
00219 QString categoryFileName()
00220 {
00221 QDir dir = (QString(getenv("HOME")) + "/Settings");
00222 if ( !dir.exists() )
00223 mkdir( dir.path().local8Bit(), 0700 );
00224 return dir.path() + "/" + "Categories" + ".xml";
00225 }
00226
00227 void CategoryEdit::kludge()
00228 {
00229 lvView->setMaximumHeight( 130 );
00230 }
This file is part of the documentation for OPIE Version 1.5.5.