📄 categories.cpp
字号:
return LocalTranslator::translate("Categories-*","Categories", s.latin1()); }}void qpe_translateLabels(QStringList& strs){ for (QStringList::Iterator it = strs.begin(); it!=strs.end(); ++it) { QString sd = (*it); if ( (*it)[0] == '_' ) *it = qpe_translateLabel(*it); else *it = qpe_translateLabel(*it); }}/*! Returns a single string associated with \a catids for applications \a app. The returned string is for display in a combobox or any area that requires one string. If \a catids are empty then "Unfiled" will be returned. If multiple categories are assigned then the behavior depends on the DisplaySingle type. If \a display is set to ShowMulti then " (multi)" appended to the first string. If \a display is set to ShowAll, then a space seperated string is returned with all categories. If ShowFirst is set, the just the first string is returned. Note that the returned string may be translated, so while it is ideal for display to the user, it should not be passed back to Categories functions as a 'label'.*/QString Categories::displaySingle( const QString &app, const QArray<int> &catids, DisplaySingle display ) const{ QStringList strs = mGlobalCats.labels( catids ); strs += mAppCats[app].labels( catids ); qpe_translateLabels(strs); if ( !strs.count() ) return tr("Unfiled"); strs.sort(); QString r; if ( strs.count() > 1 ) { switch ( display ) { case ShowFirst: r = strs.first(); break; case ShowMulti: r = strs.first() + tr(" (multi.)"); break; case ShowAll: r = strs.join(" "); break; } } else r = strs.first(); return r;}/*! Returns all ids associated with the application CategoryGroup \a app and the passed in \a labels in that group.*/QArray<int> Categories::ids( const QString &app, const QStringList &labels) const{ QArray<int> results; QStringList::ConstIterator it; int i; for ( i=0, it=labels.begin(); it!=labels.end(); i++, ++it ) { int value = id( app, *it ); if ( value != 0 ) { int tmp = results.size(); results.resize( tmp + 1 ); results[ tmp ] = value; } } return results;}/*! Returns the id associated with \a cat in application \a app. If the id is not found in the application CategoryGroup, then it searches the global CategoryGroup. If it is not found it either, 0 is returned.*/int Categories::id( const QString &app, const QString &cat ) const{ if ( cat == "_Unfiled" ) return 0; int uid = mGlobalCats.id( cat ); if ( uid != 0 ) return uid; return mAppCats[app].id( cat );}/*! Return TRUE if renaming succeeded; FALSE if \a appname or \a oldName is not found, or if \a newName conflicts with an existing category name in any CategoryGroup. */bool Categories::renameCategory( const QString &appname, const QString &oldName, const QString &newName ){ // renaming to a non unique name is not allowed if ( contains(this, newName) ) return FALSE; QMap< QString, CategoryGroup >::Iterator appIt = mAppCats.find( appname ); if ( appIt != mAppCats.end() ) { CategoryGroup &cats = *appIt; int id = cats.id( oldName ); if ( id != 0 && cats.rename( id, newName ) ) { emit categoryRenamed( *this, appname, id ); return TRUE; } } return renameGlobalCategory( oldName, newName );}/*! Return TRUE if renaming succeeded; FALSE if \a oldName or \a newName is not found, or if \a newName conflicts with an existing category in any CategoryGroup. This function will only rename categories found in the global CategoryGroup. */bool Categories::renameGlobalCategory( const QString &oldName, const QString &newName ){ // renaming to a non unique name is not allowed if ( contains(this, newName) ) return FALSE; int uid = mGlobalCats.id( oldName ); if ( uid != 0 && mGlobalCats.rename( uid, newName ) ) { emit categoryRenamed( *this, QString::null, uid ); return TRUE; } return FALSE;}/*! Changes the grouping of a category. If a category was global and \a global is set to TRUE, then the \a catname will be moved to the \a appname group.*/void Categories::setGlobal( const QString &appname, const QString &catname, bool global ){ // if in global and should be in app; then move it if ( !global && mGlobalCats.contains( catname ) ) { int oldId = mGlobalCats.id(catname); mGlobalCats.remove( catname ); addCategory( appname, catname, oldId ); return; } // if in app and should be in global, then move it if ( !global ) return; // moved from local to global, need to maintain the uid, otherwise // the records belonging to the local will lose their categorization int oldId = id(appname, catname); if ( removeCategory( appname, catname, FALSE ) ) { addGlobalCategory( catname, oldId ); }}/*! Returns TRUE if the \a catname is in the global CategoryGroup, FALSE if not.*/bool Categories::isGlobal( const QString &catname ) const{ return mGlobalCats.contains( catname );}/*! Returns true if the \a catname is associated with any CategoryGroup, including global. */bool Categories::exists( const QString &catname ) const{ if ( isGlobal(catname) ) return TRUE; for ( QMap<QString, CategoryGroup>::ConstIterator appsIt = mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) if ( exists( appsIt.key(), catname ) ) return TRUE; return FALSE;}/*! Returns TRUE if the \a catname is associated with the \a appname CategoryGroup, FALSE if not found. */bool Categories::exists( const QString &appname, const QString &catname) const{ QMap< QString, CategoryGroup >::ConstIterator appIt = mAppCats.find( appname ); if ( appIt == mAppCats.end() ) return FALSE; return (*appIt).contains( catname );}/*! Saves the Categories database to the \a fname. See categoryFileName() for the default file name string used for the shared category database. Returns FALSE if there is error writing the file or TRUE on success. */bool Categories::save( const QString &fname ) const{ QString tempFile; tempFile = qtopia_tempName( fname ); QFile f( tempFile ); QString out; int total_written; if ( !f.open( IO_WriteOnly|IO_Raw ) ) { qWarning("Unable to write to %s", f.name().latin1()); return FALSE; } out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; out += "<!DOCTYPE CategoryList>\n"; out += "<Categories>\n"; for ( QMap<int, QString>::ConstIterator git = mGlobalCats.idMap().begin(); git != mGlobalCats.idMap().end(); ++git ) out += "<Category id=\"" + QString::number(git.key()) + "\"" + // No tr " name=\"" + escapeString(*git) + "\" />\n"; // No tr for ( QMap<QString, CategoryGroup>::ConstIterator appsIt=mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) { const QString &app = appsIt.key(); const QMap<int, QString> &appcats = (*appsIt).idMap(); for ( QMap<int, QString>::ConstIterator appcatit = appcats.begin(); appcatit != appcats.end(); ++appcatit ) out += "<Category id=\"" + QString::number(appcatit.key()) + "\"" + // No tr " app=\"" + escapeString(app) + "\"" + " name=\"" + escapeString(*appcatit) + "\" />\n"; // No tr } out += "</Categories>\n"; QCString cstr = out.utf8(); total_written = f.writeBlock( cstr.data(), cstr.length() ); if ( total_written != int(cstr.length()) ) { f.close(); QFile::remove( tempFile ); qDebug("Failure while writing %s", f.name().latin1() ); return FALSE; } f.close(); qtopia_renameFile( tempFile, fname );#ifndef QT_NO_COP { QCopEnvelope e("QPE/System", "categoriesChanged()" ); }#endif return TRUE;}/*! Loads the Categories database using \a fname. See categoryFileName() for the default file name string used for the shared category database. Returns FALSE if there is error reading the file or TRUE on success. */bool Categories::load( const QString &fname ){ QFile file( fname ); if ( !file.open( IO_ReadOnly ) ) { qWarning("Unable to open %s", fname.latin1()); addGlobalCategory("_Business"); // No tr addGlobalCategory("_Personal"); // No tr save(fname); return FALSE; } clear(); QByteArray ba = file.readAll(); QString data = QString::fromUtf8( ba.data(), ba.size() ); QChar *uc = (QChar *)data.unicode(); int len = data.length(); // QTime t; // t.start(); QString name; QString id; QString app; int i = 0; while ( (i = data.find( "<Category ", i)) != -1 ) { // No tr i += 10; name = QString::null; app = QString::null; while ( 1 ) { // skip white space while ( i < len && (uc[i] == ' ' || uc[i] == '\n' || uc[i] == '\r') ) i++; // if at the end, then done if ( i >= len-2 || (uc[i] == '/' && uc[i+1] == '>') ) break; // we have another attribute read it. int j = i; while ( j < len && uc[j] != '=' ) j++; QString attr = QConstString( uc+i, j-i ).string(); i = ++j; // skip = while ( i < len && uc[i] != '"' ) i++; j = ++i; while ( j < len && uc[j] != '"' ) j++; QString value = Qtopia::plainString( QConstString( uc+i, j-i ).string() ); i = j + 1;// qDebug("attr='%s' value='%s'", attr.latin1(), value.latin1() ); if ( attr == "id" ) id = value; else if ( attr == "app" ) app = value; else if ( attr == "name" ) // No tr name = value; } if ( name.isNull() || id.isNull() ) { qWarning("No name or id in the category"); continue; } if ( app.isNull() ) mGlobalCats.add( id.toInt(), name ); else mAppCats[ app ].add( id.toInt(), name ); } return TRUE;}/*! Clear the categories in memory. Equivelent to creating an empty Categories object.*/void Categories::clear(){ mGlobalCats.clear(); mAppCats.clear();}/*! Dump the contents to standard out. Used for debugging only.*/void Categories::dump() const{ qDebug("\tglobal categories = %s", mGlobalCats.labels().join(", ").latin1() ); for ( QMap<QString, CategoryGroup>::ConstIterator appsIt = mAppCats.begin(); appsIt != mAppCats.end(); ++appsIt ) { const QString &app = appsIt.key(); QStringList appcats = (*appsIt).labels(); qDebug("\tapp = %s\tcategories = %s", app.latin1(), appcats.join(", ").latin1() ); }}QStringList CheckedListView::checked() const{ QStringList strs; for ( QCheckListItem *i = (QCheckListItem *) firstChild(); i; i = (QCheckListItem *)i->nextSibling() ) if ( i->isOn() ) strs += i->text( 0 ); return strs;}void CheckedListView::addCheckableList( const QStringList &options ){ for ( QStringList::ConstIterator it = options.begin(); it != options.end(); ++it ) { (void) new QCheckListItem( this, *it, QCheckListItem::CheckBox ); }}void CheckedListView::setChecked( const QStringList &checked ){ // iterate over all items bool showingChecked = FALSE; for ( QCheckListItem *i = (QCheckListItem *) firstChild(); i; i = (QCheckListItem *)i->nextSibling() ) // see if the item should be checked by searching the // checked list if ( checked.find( i->text( 0 ) ) != checked.end() ) { i->setOn( TRUE ); // make sure it is showing at least one checked item if ( !showingChecked ) { ensureItemVisible( i ); showingChecked = TRUE; } } else i->setOn( FALSE );}/*! \fn Categories &Categories::operator= ( const Categories &c ) Performs deep copy of \a c. *//*! \fn QStringList Categories::globalCategories() const Returns list of all global category labels*//*! \fn const QMap<QString, CategoryGroup> &Categories::appGroupMap() const Returns a map of application names to CategoryGroup. The CategoryGroup class defines a map of ids to category labels and category labels to ids.*//*! \fn const CategoryGroup &Categories::globalGroup() const Returns the global CategoryGroup. The CategoryGroup class defines a map of ids to category labels and category labels to ids.*//*! \fn void Categories::categoryAdded( const Categories &cats, const QString &appname, int uid) Emitted if a category is added. \a cats is a const reference to this object \a appname is the CategoryGroup application name that the category was added to or QString::null if it was global \a uid is the unique identifier associated with the added category*//*! \fn void Categories::categoryRemoved( const Categories &cats, const QString &appname, int uid) Emitted if removed category is removed. \a cats is a const reference to this object \a appname is the CategoryGroup application name that the category was removed from or QString::null if it was the global CategoryGroup \a uid is the unique identifier associated with the removed category*//*! \fn void Categories::categoryRenamed( const Categories &cats, const QString &appname, int uid) Emitted if \a uid in the \a appname CategoryGroup is renamed in \a cats object. \a cats is a const reference to this object \a appname is the CategoryGroup application name that the category was renamed in or QString::null if it was the global CategoryGroup \a uid is the unique identifier associated with the renamed category*//*! \fn Categories::Categories( QObject *parent, const char *name ) Constructor for an empty Categories object. The standard \a parent and \a name parameters are passed on.*//*! \fn Categories::Categories( const Categories ©From ) Constructors a deep copy of \a copyFrom.*//*! \fn Categories::~Categories() Empty destructor. Call save() before destruction if there are changes that need to be saved.*//*! \fn CategoryGroup::clear() \internal*//*! \fn const QMap<int, QString> &CategoryGroup::idMap() const Returns a const reference to the id to label QMap*//*! \fn CategoryGroup::CategoryGroup() \internal*//*! \fn CategoryGroup::CategoryGroup(const CategoryGroup &c) \internal*//*! \enum Categories::ExtraLabels \value NoExtra - only the category labels \value AllUnfiled - add All and Unfiled labels \value AllLabel - add All label \value UnfiledLabel - add Unfiled label*//*! \enum Categories::DisplaySingle \value ShowMulti \value ShowAll \value ShowFirst*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -