• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

kaccelgen.h
Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2000 Keunwoo Lee <klee@cs.washington.edu>
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB. If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #ifndef KACCELGEN_H
00021 #define KACCELGEN_H
00022 
00023 #include <kdeui_export.h>
00024 
00025 #include <QtCore/QMap>
00026 #include <QtCore/QString>
00027 #include <QtCore/QStringList>
00028 
00080 namespace KAccelGen
00081 {
00082 
00083 // HELPERS
00084 
00088 template <class Iter>
00089 class Deref
00090 {
00091 public:
00092     static QString deref(Iter i) { return *i; }
00093 };
00094 
00099 template <class Iter>
00100 class Deref_Key
00101 {
00102 public:
00103     static QString deref(Iter i) { return i.key(); }
00104 };
00105 
00113 inline bool
00114 isLegalAccelerator(const QString& str, int index)
00115 {
00116     return index >= 0 && index < str.length()
00117         && str[index].isLetterOrNumber();
00118 }
00119 
00128 template <class Iter, class Deref>
00129 inline void
00130 loadPredefined(Iter begin, Iter end, QMap<QChar,bool>& keys)
00131 {
00132     for (Iter i = begin; i != end; ++i) {
00133         QString item = Deref::deref(i);
00134         int user_ampersand = item.indexOf(QLatin1Char('&'));
00135         if( user_ampersand >= 0 ) {
00136             // Sanity check.  Note that we don't try to find an
00137             // accelerator if the user shoots him/herself in the foot
00138             // by adding a bad '&'.
00139             if( isLegalAccelerator(item, user_ampersand+1) ) {
00140                 keys.insert(item[user_ampersand+1], true);
00141             }
00142         }
00143     }
00144 }
00145 
00146 
00147 // ///////////////////////////////////////////////////////////////////
00148 // MAIN USER FUNCTIONS
00149 
00150 
00165 template <class Iter, class Iter_Deref >
00166 void
00167 generate(Iter begin, Iter end, QStringList& target)
00168 {
00169     // Will keep track of used accelerator chars
00170     QMap<QChar,bool> used_accels;
00171 
00172     // Prepass to detect manually user-coded accelerators
00173     loadPredefined<Iter,Iter_Deref>(begin, end, used_accels);
00174 
00175     // Main pass
00176     for (Iter i = begin; i != end; ++i) {
00177         QString item = Iter_Deref::deref(i);
00178 
00179         // Attempt to find a good accelerator, but only if the user
00180         // has not manually hardcoded one.
00181         int user_ampersand = item.indexOf(QLatin1Char('&'));
00182         if( user_ampersand < 0 || item[user_ampersand+1] == QLatin1Char('&')) {
00183             bool found = false;
00184             int j;
00185 
00186             // Check word-starting letters first.
00187             for( j=0; j < item.length(); ++j ) {
00188                 if( isLegalAccelerator(item, j)
00189                     && !used_accels.contains(item[j])
00190                     && (0 == j || (j > 0 && item[j-1].isSpace())) ) {
00191                     found = true;
00192                     break;
00193                 }
00194             }
00195 
00196             if( !found ) {
00197                 // No word-starting letter; search for any letter.
00198                 for( j=0; j < item.length(); ++j ) {
00199                     if( isLegalAccelerator(item, j)
00200                         && !used_accels.contains(item[j]) ) {
00201                         found = true;
00202                         break;
00203                     }
00204                 }
00205             }
00206 
00207             if( found ) {
00208                 // Both upper and lower case marked as used
00209                 used_accels.insert(item[j].toUpper(),true);
00210                 used_accels.insert(item[j].toLower(),true);
00211                 item.insert(j,QLatin1Char('&'));
00212             }
00213         }
00214 
00215         target.append( item );
00216     }
00217 }
00218 
00227 template <class Iter>
00228 inline void
00229 generateFromKeys(Iter begin, Iter end, QStringList& target)
00230 {
00231     generate< Iter, Deref_Key<Iter> >(begin, end, target);
00232 }
00233 
00234 
00241 inline void
00242 generate(const QStringList& source, QStringList& target)
00243 {
00244     generate<QStringList::ConstIterator, Deref<QStringList::ConstIterator> >(source.begin(), source.end(), target);
00245 }
00246 
00253 template <class Key>
00254 inline void
00255 generateFromValues(const QMap<Key,QString>& source, QStringList& target)
00256 {
00257     generate<typename QMap<Key,QString>::ConstIterator, Deref_Key<typename QMap<Key,QString>::ConstIterator> >(source.begin(), source.end(), target);
00258 }
00259 
00266 template <class Data>
00267 inline void
00268 generateFromKeys(const QMap<QString,Data>& source, QStringList& target)
00269 {
00270     generateFromKeys(source.begin(), source.end(), target);
00271 }
00272 
00273 
00274 } // end namespace KAccelGen
00275 
00276 #endif
00277 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:53:00 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal