kaccelgen.h
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include <qmap.h>
00021 #include <qstring.h>
00022 #include <qstringlist.h>
00023 
00075 namespace KAccelGen
00076 {
00077 
00078 
00079 
00083 template <class Iter>
00084 class Deref
00085 {
00086 public:
00087     static QString deref(Iter i) { return *i; }
00088 };
00089 
00094 template <class Iter>
00095 class Deref_Key
00096 {
00097 public:
00098     static QString deref(Iter i) { return i.key(); }
00099 };
00100 
00108 inline bool
00109 isLegalAccelerator(const QString& str, uint index)
00110 {
00111     return index < str.length()
00112         && str[index].isLetterOrNumber();
00113 }
00114 
00123 template <class Iter, class Deref>
00124 inline void
00125 loadPredefined(Iter begin, Iter end, QMap<QChar,bool>& keys)
00126 {
00127     for (Iter i = begin; i != end; ++i) {
00128         QString item = Deref::deref(i);
00129         int user_ampersand = item.find(QChar('&'));
00130         if( user_ampersand >= 0 ) {
00131             
00132             
00133             
00134             if( isLegalAccelerator(item, user_ampersand+1) ) {
00135                 keys.insert(item[user_ampersand+1], true);
00136             }
00137         }
00138     }
00139 }
00140 
00141 
00142 
00143 
00144 
00145 
00160 template <class Iter, class Iter_Deref >
00161 void
00162 generate(Iter begin, Iter end, QStringList& target)
00163 {
00164     
00165     QMap<QChar,bool> used_accels;
00166 
00167     
00168     loadPredefined<Iter,Iter_Deref>(begin, end, used_accels);
00169 
00170     
00171     for (Iter i = begin; i != end; ++i) {
00172         QString item = Iter_Deref::deref(i);
00173 
00174         
00175         
00176         int user_ampersand = item.find(QChar('&'));
00177         if( user_ampersand < 0 ) {
00178             bool found = false;
00179             uint found_idx;
00180             uint j;
00181 
00182             
00183             for( j=0; j < item.length(); ++j ) {
00184                 if( isLegalAccelerator(item, j)
00185                     && !used_accels.contains(item[j])
00186                     && (0 == j || j > 0 && item[j-1].isSpace()) ) {
00187                     found = true;
00188                     found_idx = j;
00189                     break;
00190                 }
00191             }
00192 
00193             if( !found ) {
00194                 
00195                 for( j=0; j < item.length(); ++j ) {
00196                     if( isLegalAccelerator(item, j)
00197                         && !used_accels.contains(item[j]) ) {
00198                         found = true;
00199                         found_idx = j;
00200                         break;
00201                     }
00202                 }
00203             }
00204 
00205             if( found ) {
00206                 
00207                 used_accels.insert(item[j].upper(),true);
00208                 used_accels.insert(item[j].lower(),true);
00209                 item.insert(j,QChar('&'));
00210             }
00211         }
00212 
00213         target.append( item );
00214     }
00215 }
00216 
00225 template <class Iter>
00226 inline void
00227 generateFromKeys(Iter begin, Iter end, QStringList& target)
00228 {
00229     generate< Iter, Deref_Key<Iter> >(begin, end, target);
00230 }
00231 
00232 
00239 inline void
00240 generate(const QStringList& source, QStringList& target)
00241 {
00242     generate<QStringList::ConstIterator, Deref<QStringList::ConstIterator> >(source.begin(), source.end(), target);
00243 }
00244 
00251 template <class Key>
00252 inline void
00253 generateFromValues(const QMap<Key,QString>& source, QStringList& target)
00254 {
00255     generate<QMapConstIterator<Key,QString>, Deref_Key<QMapConstIterator<Key,QString> > >(source.begin(), source.end(), target);
00256 }
00257 
00264 template <class Data>
00265 inline void
00266 generateFromKeys(const QMap<QString,Data>& source, QStringList& target)
00267 {
00268     generateFromKeys(source.begin(), source.end(), target);
00269 }
00270 
00271 
00272 } 
 
This file is part of the documentation for kdeui Library Version 3.2.0.