📄 categoryselect.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** This program is free software; you can redistribute it and/or modify it** under the terms of the GNU General Public License as published by the** Free Software Foundation; either version 2 of the License, or (at your** option) any later version.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** This program is distributed in the hope that it will be useful, but** WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ** See the GNU General Public License for more details.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include <qtopia/categories.h>#include <qtopia/private/palmtoprecord.h>#include <qtopia/qpeapplication.h>#include <qdir.h>#include <qmessagebox.h>#include <qlayout.h>#include <qtoolbutton.h>#include <qfile.h>#include <qpushbutton.h>#include <qobject.h>#include <qtimer.h>#include <qheader.h>#include "categoryedit_p.h"#include "categoryselect.h"#include <stdlib.h>/*! \enum CategorySelect::SelectorWidget Chooses a type of widget to use as the selection widget. \value ComboBox \value ListView*//*! \class CategorySelect \brief The CategorySelect widget allows users to select Categories with a combobox interface. CategorySelect is useful to provide a QComboBox of Categories for filtering (such as in the Contacts table view) or to allow the user to select multiple Categories. The allCategories variable sets whether the CategorySelect is in filtering or selecting mode. In filtering mode, the All and Unfiled categories are added. The In selecting mode, the CategorySelect may either be a QComboBox and a QToolButton or a QListView with checkable items depending on the screen size. CategorySelect automatically updates itself if Categories has been changed elsewhere in the environment. Signals and slots are provided to notify the application of the users selections. A QToolButton is also provided so that users can edit the Categories manually. \ingroup qtopiaemb*/class CategoryCheckListItem : public QCheckListItem {public: CategoryCheckListItem(QListView* parent, const QString& l, int i) : QCheckListItem(parent, l, CheckBox), id(i) { } const int id;};class CategoryComboPrivate{public: CategoryComboPrivate() { } QArray<int> mAppCats; QString mStrAppName; QString mStrVisibleName; Categories mCats;};CategoryCombo::CategoryCombo( QWidget *parent, const char *name , int width) : QComboBox( FALSE, parent, name ){ QSizePolicy p = sizePolicy(); p.setHorData(QSizePolicy::Expanding); setSizePolicy(p); d = new CategoryComboPrivate(); if (width) setFixedWidth(width);}void CategoryCombo::initCombo( const QArray<int> &recCats, const QString &appName ){ initCombo( recCats, appName, appName );}void qpe_translateLabels(QStringList& strs);QString qpe_translateLabel(const QString& s);void CategoryCombo::initCombo( const QArray<int> &recCats, const QString &appName, const QString &visibleName ){ d->mStrAppName = appName; d->mStrVisibleName = visibleName; clear(); QStringList slApp; bool loadOk = d->mCats.load( categoryFileName() ); QObject::connect( this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)) ); slApp = d->mCats.labels( d->mStrAppName, TRUE, Categories::UnfiledLabel ); d->mAppCats = d->mCats.ids( d->mStrAppName, slApp); qpe_translateLabels(slApp); int recCount = recCats.count(); insertStringList( slApp ); if ( recCount == 1 && loadOk ) { for ( int i = 0; i < int(d->mAppCats.size()); i++ ) { if ( d->mAppCats[i] == recCats[0] ) { setCurrentItem( i ); break; } } } else if (recCount == 0 || !loadOk) { setCurrentItem( slApp.count()-1 ); // unfiled }}// this is a new function by SHARP instead of initCombo()QArray<int> CategoryCombo::initComboWithRefind( const QArray<int> &recCats, const QString &appName){ QString visibleName = appName; d->mStrAppName = appName; d->mStrVisibleName = visibleName; clear(); QStringList slApp; QArray<int> results; QObject::disconnect( this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)) ); QObject::connect( this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)) ); bool loadOk = d->mCats.load( categoryFileName() ); slApp = d->mCats.labels( d->mStrAppName, TRUE, Categories::UnfiledLabel ); d->mAppCats = d->mCats.ids( d->mStrAppName, slApp); qpe_translateLabels(slApp); // addition part // make new recCats if (loadOk){ int i,j; int value; int rCount = recCats.count(); int mCount = d->mAppCats.count(); for (i=0; i<rCount; i++){ value = 0; for (j=0; j<mCount; j++){ if (recCats[i] == d->mAppCats[j]){ value = recCats[i]; break; } } if (value != 0){ int tmp = results.size(); results.resize( tmp + 1 ); results[ tmp ] = value; } } } else{ results = recCats.copy(); } // addition end int recCount = results.count(); insertStringList( slApp ); if ( recCount == 1 && loadOk ) { for ( int i = 0; i < int(d->mAppCats.size()); i++ ) { if ( d->mAppCats[i] == results[0] ) { setCurrentItem( i ); break; } } } else if (recCount == 0 || !loadOk) { setCurrentItem( slApp.count()-1 ); // unfiled } // else handled later in CategorySelect::setCategories, //which calls this function/* QObject::connect( this, SIGNAL(activated(int)), this, SLOT(slotValueChanged(int)) );*/ return results;}CategoryCombo::~CategoryCombo(){ delete d;}int CategoryCombo::currentCategory() const{ int i = currentItem(); int r; if ( i == (int)d->mAppCats.count() ) r = -1; else if ( i > (int)d->mAppCats.count() ) // only happen on "All" r = -2; else r = d->mAppCats[i]; return r;}void CategoryCombo::setCurrentCategory( int newCatUid ){ if ( newCatUid == -1 ) { setCurrentItem( d->mAppCats.count() ); } else if ( newCatUid == -2 ) { setCurrentItem( d->mAppCats.count()+1 ); } else { int i; for ( i = 0; i < int(d->mAppCats.size()); i++ ) { if ( d->mAppCats[i] == newCatUid ) setCurrentItem( i ); } }}void CategoryCombo::setCurrentText( const QString &str ){ int i; int stop; stop = count(); for ( i = 0; i < stop; i++ ) { if ( text( i ) == str ) { setCurrentItem( i ); break; } }}void CategoryCombo::slotValueChanged( int ){ emit sigCatChanged( currentCategory() );}class CategorySelectPrivate : public QObject{ Q_OBJECTpublic: CategorySelectPrivate( QObject *parent ) : QObject(parent, "CategorySelectPrivate" ), mChosen(), usingAll( FALSE ), mVisibleName(), editMode( FALSE ), canEdit( TRUE ), type( CategorySelect::ComboBox ), container( 0 ), layout(0), fixedWidth(0), pendingCategorySets( 0 ), catSelector( 0 ) { setType(); } void setType() {#ifndef QTOPIA_DESKTOP type = CategorySelect::ComboBox;#else if ( qApp->desktop()->height() >= 600 && !usingAll ) type = CategorySelect::ListView; else type = CategorySelect::ComboBox;#endif } QArray<int> mChosen; // The checked categories. bool usingAll; QString mVisibleName; bool editMode; bool canEdit; CategorySelect::SelectorWidget type; QWidget *container; QGridLayout *layout; int fixedWidth; int lastCat; int pendingCategorySets; // so far, the QListView is used only for Qtopia Desktop // but could be used for bigger PDA screen sizes // the following methods and variables are used only when // type == ComboBox QListView *catSelector; QPushButton *editButton; Categories cats; QString appName; void reloadCatSelector( const QArray<int>& checked ) { catSelector->clear(); QStringList sl = cats.labels( appName ); // Qtopia2: //QArray<int> id = cats.ids( appName ); // Qtopia0: QArray<int> id = cats.ids( appName, sl ); qpe_translateLabels(sl); int i=0; for (QStringList::ConstIterator it=sl.begin(); it!=sl.end(); ++it,++i) { CategoryCheckListItem *item = new CategoryCheckListItem(catSelector,*it,id[i]); if ( checked.find(id[i])>=0 ) item->setOn(TRUE); } } void reloadCatSelector() { reloadCatSelector( mChosen ); } void pruneDeletedCats(QArray<int>& l) { QArray<int> checkedCats; for ( int i = 0; i < (int) l.count();++i ) if ( cats.label( appName, l[i] ) != QString::null ) { checkedCats.resize( i+1 ); checkedCats[i] = l[i]; } l = checkedCats; } void setComboItem( QComboBox *combo, int item ) { pendingCategorySets++; combo->setCurrentItem( item ); QTimer::singleShot( 0, this, SLOT( turnOnEditDialog() ) ); }public slots: void itemClicked( QListViewItem *i ) { if ( i == 0 ) return; CategoryCheckListItem* item = (CategoryCheckListItem*)i; int id = item->id; if ( item->isOn() ) { if ( mChosen.count() == 0 || mChosen.find(id) < 0 ) { mChosen.resize(mChosen.size()+1); mChosen[(int)mChosen.size()-1] = id; } } else { if ( mChosen.count() > 0 ) { int i = mChosen.find(id); if ( i >= 0 ) { int t = mChosen[(int)mChosen.size()-1]; mChosen[i] = t; mChosen.resize(mChosen.size()-1); } } } } void turnOnEditDialog() { pendingCategorySets--; }};/*! \overload This constructor accepts an array \a vl of integers representing Categories for application \a appName. \a visibleName is the string used when the name of this application is required to be displayed. \a width is an integer used as the fixed width of the widget. The \a parent and \a name parameters are the standard Qt parent parameters.*/CategorySelect::CategorySelect( const QArray<int> &vl, const QString &appName, const QString &visibleName, QWidget *parent, const char *name , int width) : QHBox( parent, name ), cmbCat( 0 ), cmdCat( 0 ), d( 0 ){ init(width);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -