Qtopia library API Documentation

categoryselect.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2001 Trolltech AS.  All rights reserved.
00003 ** Copyright (C) 2003 zecke introduce a needed symbol
00004 **
00005 ** This file is part of Qtopia Environment.
00006 **
00007 ** This file may be distributed and/or modified under the terms of the
00008 ** GNU General Public License version 2 as published by the Free Software
00009 ** Foundation and appearing in the file LICENSE.GPL included in the
00010 ** packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00016 **
00017 ** Contact info@trolltech.com if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 **********************************************************************/
00021 
00022 #include <qpe/categories.h>
00023 #include <qpe/palmtoprecord.h>
00024 
00025 #include <qmessagebox.h>
00026 #include <qlayout.h>
00027 #include <qtoolbutton.h>
00028 #include <qfile.h>
00029 
00030 #include "categorywidget.h"
00031 #include "categoryselect.h"
00032 
00033 #include <stdlib.h>
00034 
00035 
00070 static QString categoryEdittingFileName()
00071 {
00072     QString str = getenv("HOME");
00073     str +="/.cateditting";
00074     return str;
00075 }
00076 
00077 class CategoryComboPrivate
00078 {
00079 public:
00080     CategoryComboPrivate(QObject *o)
00081     : mCat( o )
00082     {
00083     }
00084     QArray<int> mAppCats;
00085     QString mStrAppName;
00086     QString mStrVisibleName;
00087     Categories mCat;
00088 };
00089 
00090 class CategorySelectPrivate
00091 {
00092 public:
00093     CategorySelectPrivate( const QArray<int> &cats)
00094     : mRec( cats ),
00095       usingAll( false )
00096     {
00097     }
00098     CategorySelectPrivate()
00099     {
00100     }
00101     QArray<int> mRec;
00102     bool usingAll;
00103     QString mVisibleName;
00104 };
00105 
00106 CategoryCombo::CategoryCombo( QWidget *parent, const char *name , int width)
00107     : QComboBox( parent, name )
00108 {
00109     d = new CategoryComboPrivate(this);
00110     if (width)
00111       setFixedWidth(width);
00112 }
00113 
00114 void CategoryCombo::initCombo( const QArray<int> &recCats,
00115                    const QString &appName )
00116 {
00117     initCombo( recCats, appName, appName );
00118 }
00119 
00120 void CategoryCombo::initCombo( const QArray<int> &recCats,
00121                    const QString &appName,
00122                    const QString &visibleName )
00123 {
00124     d->mStrAppName = appName;
00125     d->mStrVisibleName = visibleName;
00126     clear();
00127     QStringList slApp;
00128 
00129     QObject::disconnect( this, SIGNAL(activated(int)),
00130               this, SLOT(slotValueChanged(int)) );
00131 
00132     QObject::connect( this, SIGNAL(activated(int)),
00133               this, SLOT(slotValueChanged(int)) );
00134     bool loadOk = d->mCat.load( categoryFileName() );
00135     slApp = d->mCat.labels( d->mStrAppName, TRUE, Categories::UnfiledLabel );
00136 
00137     d->mAppCats = d->mCat.ids( d->mStrAppName, slApp);
00138 
00139     int i,
00140     j,
00141     saveMe,
00142     recCount;
00143     QStringList::Iterator it;
00144     // now add in all the items...
00145     recCount = recCats.count();
00146     saveMe = -1;
00147     if ( recCount > 1 && loadOk ) {
00148     it = slApp.begin();
00149     for ( j = 0; j< (int)(slApp.count()-1); ++it, j++ ) {
00150         // grr... we have to go through and compare...
00151         if ( j < int(d->mAppCats.size()) ) {
00152         for ( i = 0; i < recCount; i++ ) {
00153             if ( recCats[i] == d->mAppCats[j] ) {
00154             (*it).append( tr(" (Multi.)") );
00155             if ( saveMe < 0 )
00156                 saveMe = j;
00157             // no need to continue through the list.
00158             break;
00159             }
00160         }
00161         }
00162         insertItem( *it );
00163     }
00164     insertItem( *it );
00165     } else
00166     insertStringList( slApp );
00167 
00168     if ( recCount > 0 && loadOk ) {
00169     for ( i = 0; i < int(d->mAppCats.size()); i++ ) {
00170         if ( d->mAppCats[i] == recCats[0] ) {
00171         setCurrentItem( i );
00172         break;
00173         }
00174     }
00175     } else
00176     {
00177     setCurrentItem( slApp.count()-1 );  // unfiled
00178     }
00179 }
00180 
00181 // this is a new function by SHARP instead of initCombo()
00182 QArray<int> CategoryCombo::initComboWithRefind( const QArray<int> &recCats,
00183                    const QString &appName)
00184 {
00185     QString visibleName = appName;
00186     d->mStrAppName = appName;
00187     d->mStrVisibleName = visibleName;
00188     clear();
00189     QStringList slApp;
00190     QArray<int> results;
00191 
00192     QObject::disconnect( this, SIGNAL(activated(int)),
00193               this, SLOT(slotValueChanged(int)) );
00194 
00195     QObject::connect( this, SIGNAL(activated(int)),
00196               this, SLOT(slotValueChanged(int)) );
00197     bool loadOk = d->mCat.load( categoryFileName() );
00198     slApp = d->mCat.labels( d->mStrAppName, TRUE, Categories::UnfiledLabel );
00199 
00200     d->mAppCats = d->mCat.ids( d->mStrAppName, slApp);
00201 
00202     // addition part
00203     // make new recCats
00204     if (loadOk){
00205       int i,j;
00206       int value;
00207       int rCount = recCats.count();
00208       int mCount = d->mAppCats.count();
00209 
00210       for (i=0; i<rCount; i++){
00211     value = 0;
00212     for (j=0; j<mCount; j++){
00213       if (recCats[i] == d->mAppCats[j]){
00214         value = recCats[i];
00215         break;
00216       }
00217     }
00218     if (value != 0){
00219       int tmp = results.size();
00220       results.resize( tmp + 1 );
00221       results[ tmp ] = value;
00222     }
00223       }
00224     }
00225     else{
00226       results = recCats;
00227     }
00228     // addition end
00229 
00230     int i,
00231     j,
00232     saveMe,
00233     recCount;
00234     QStringList::Iterator it;
00235     // now add in all the items...
00236     recCount = results.count();
00237     saveMe = -1;
00238     if ( recCount > 1 && loadOk ) {
00239     it = slApp.begin();
00240     for ( j = 0; j< (int)(slApp.count()-1); ++it, j++ ) {
00241 
00242         // grr... we have to go through and compare...
00243         if ( j < int(d->mAppCats.size()) ) {
00244         for ( i = 0; i < recCount; i++ ) {
00245             if ( results[i] == d->mAppCats[j] ) {
00246             (*it).append( tr(" (Multi.)") );
00247             if ( saveMe < 0 )
00248                 saveMe = j;
00249             // no need to continue through the list.
00250             break;
00251             }
00252         }
00253         }
00254         insertItem( *it );
00255     }
00256     insertItem( *it );
00257     } else
00258     insertStringList( slApp );
00259 
00260     if ( recCount > 0 && loadOk ) {
00261     for ( i = 0; i < int(d->mAppCats.size()); i++ ) {
00262         if ( d->mAppCats[i] == results[0] ) {
00263         setCurrentItem( i );
00264         break;
00265         }
00266     }
00267     } else
00268     {
00269     setCurrentItem( slApp.count()-1 );  // unfiled
00270     }
00271 /*
00272     QObject::connect( this, SIGNAL(activated(int)),
00273               this, SLOT(slotValueChanged(int)) );
00274 */
00275     return results;
00276 }
00277 
00278 
00279 CategoryCombo::~CategoryCombo()
00280 {
00281     delete d;
00282 }
00283 
00284 int CategoryCombo::currentCategory() const
00285 {
00286     int returnMe;
00287     returnMe = currentItem();
00288 
00289     if ( returnMe == (int)d->mAppCats.count() )
00290     returnMe = -1;
00291     else if ( returnMe > (int)d->mAppCats.count() )  // only happen on "All"
00292     returnMe = -2;
00293     else
00294     returnMe =  d->mAppCats[returnMe];
00295     return returnMe;
00296 }
00297 
00298 void CategoryCombo::setCurrentCategory( int newCatUid )
00299 {
00300     int i;
00301     for ( i = 0; i < int(d->mAppCats.size()); i++ ) {
00302     if ( d->mAppCats[i] == newCatUid )
00303         setCurrentItem( i );
00304     }
00305 }
00306 
00307 void CategoryCombo::setCurrentText( const QString &str )
00308 {
00309     int i;
00310     int stop;
00311     stop = count();
00312     for ( i = 0; i < stop; i++ ) {
00313     if ( text( i ) == str ) {
00314         setCurrentItem( i );
00315         break;
00316     }
00317     }
00318 }
00319 
00320 void CategoryCombo::slotValueChanged( int )
00321 {
00322     emit sigCatChanged( currentCategory() );
00323 }
00324 
00330 CategorySelect::CategorySelect( QWidget* parent,  const char* name )
00331     : QHBox( parent, name ),
00332       cmbCat( 0 ),
00333       cmdCat( 0 ),
00334       d( 0 )
00335 {
00336     d = new CategorySelectPrivate();
00337     init(0); // default argument
00338 }
00345 CategorySelect::CategorySelect( QWidget *parent, const char *name,int width)
00346     : QHBox( parent, name ),
00347       cmbCat( 0 ),
00348       cmdCat( 0 ),
00349       d( 0 )
00350 {
00351     d = new CategorySelectPrivate();
00352     init(width);
00353 }
00354 
00360 CategorySelect::CategorySelect( const QArray<int> &vl,
00361                 const QString &appName, QWidget *parent,
00362                 const char *name ,int width)
00363     : QHBox( parent, name )
00364 {
00365     d = new CategorySelectPrivate( vl );
00366 
00367     init(width);
00368 
00369     setCategories( vl, appName, appName );
00370 }
00371 
00379 CategorySelect::CategorySelect( const QArray<int> &vl,
00380                 const QString &appName,
00381                 const QString &visibleName,
00382                 QWidget *parent, const char *name , int width)
00383     : QHBox( parent, name )
00384 {
00385     d = new CategorySelectPrivate( vl );
00386     init(width);
00387     setCategories( vl, appName, visibleName );
00388 }
00389 
00393 CategorySelect::~CategorySelect()
00394 {
00395     delete d;
00396 }
00397 
00401 void CategorySelect::slotDialog()
00402 {
00403     if (QFile::exists( categoryEdittingFileName() )){
00404         QMessageBox::warning(this,tr("Error"),
00405        tr("Sorry, another application is\nediting categories.") );
00406         return;
00407     }
00408 
00409     QFile f( categoryEdittingFileName() );
00410     if ( !f.open( IO_WriteOnly) ){
00411         return;
00412     }
00413 
00414     QDialog editDlg( this, 0, TRUE );
00415     editDlg.setCaption( tr("Edit Categories") );
00416     QVBoxLayout *vb = new QVBoxLayout( &editDlg );
00417     QScrollView *sv = new QScrollView( &editDlg );
00418     sv->setResizePolicy( QScrollView::AutoOneFit );
00419     sv->setHScrollBarMode( QScrollView::AlwaysOff );
00420     vb->addWidget( sv );
00421     CategoryWidget ce( d->mRec, mStrAppName, d->mVisibleName, &editDlg );
00422     sv->addChild( &ce );
00423     editDlg.showMaximized();
00424 
00425     if ( editDlg.exec() ) {
00426     d->mRec = ce.newCategories();
00427     cmbCat->initCombo( d->mRec, mStrAppName );
00428     }
00429 
00430     f.close();
00431     QFile::remove( categoryEdittingFileName() );
00432 }
00433 
00434 
00438 void CategorySelect::slotNewCat( int newUid )
00439 {
00440     if ( newUid != -1 ) {
00441     bool alreadyIn = false;
00442     for ( uint it = 0; it < d->mRec.count(); ++it ) {
00443         if ( d->mRec[it] == newUid ) {
00444         alreadyIn = true;
00445         break;
00446         }
00447     }
00448     if ( !alreadyIn ) {
00449         d->mRec.resize( 1 );
00450         d->mRec[ 0 ] = newUid;
00451     }
00452     } else
00453     d->mRec.resize(0);  // now Unfiled.
00454     emit signalSelected( currentCategory() );
00455 }
00456 
00467 QString CategorySelect::setCategories( const QArray<int> &rec,
00468                     const QString &appName )
00469 {
00470     return setCategories( rec, appName, appName );
00471 }
00472 
00479 QString CategorySelect::setCategories( const QArray<int> &rec,
00480                     const QString &appName,
00481                     const QString &visibleName )
00482 {
00483     d->mVisibleName = visibleName;
00484     mStrAppName = appName;
00485     d->mRec = cmbCat->initComboWithRefind( rec, appName );
00486     return Qtopia::Record::idsToString(d->mRec);
00487 }
00488 
00489 void CategorySelect::init(int width)
00490 {
00491     cmbCat = new CategoryCombo( this, 0, width);
00492 
00493     QObject::connect( cmbCat, SIGNAL(sigCatChanged(int)),
00494               this, SLOT(slotNewCat(int)) );
00495     cmdCat = new QToolButton( this );
00496     QObject::connect( cmdCat, SIGNAL(clicked()), this, SLOT(slotDialog()) );
00497     cmdCat->setTextLabel( "...", FALSE );
00498     cmdCat->setUsesTextLabel( true );
00499     cmdCat->setMaximumSize( cmdCat->sizeHint() );
00500     cmdCat->setFocusPolicy( TabFocus );
00501 }
00502 
00506 int CategorySelect::currentCategory() const
00507 {
00508     return cmbCat->currentCategory();
00509 }
00510 
00511 
00512 void CategorySelect::setCurrentCategory( int newCatUid )
00513 {
00514     cmbCat->setCurrentCategory( newCatUid );
00515 }
00516 
00520 const QArray<int> &CategorySelect::currentCategories() const
00521 {
00522     return d->mRec;
00523 }
00524 
00528 void CategorySelect::setRemoveCategoryEdit( bool remove )
00529 {
00530     if ( remove ) {
00531     cmdCat->setEnabled( FALSE );
00532     cmdCat->hide();
00533     } else {
00534     cmdCat->setEnabled( TRUE );
00535     cmdCat->show();
00536     }
00537 }
00538 
00542 void CategorySelect::setAllCategories( bool add )
00543 {
00544     d->usingAll = add;
00545     if ( add ) {
00546     cmbCat->insertItem( tr( "All" ), cmbCat->count() );
00547     cmbCat->setCurrentItem( cmbCat->count() - 1 );
00548     } else
00549     cmbCat->removeItem( cmbCat->count() - 1 );
00550 }
00551 
00552 // 01.12.21 added
00556 void CategorySelect::setFixedWidth(int width)
00557 {
00558   width -= cmdCat->width();
00559   cmbCat->setFixedWidth(width);
00560 }
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:03 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001