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

KDEUI

  • kdeui
  • util
kaccelgen.h
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2000 Keunwoo Lee <klee@cs.washington.edu>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef KACCELGEN_H
21 #define KACCELGEN_H
22 
23 #include <kdeui_export.h>
24 
25 #include <QtCore/QMap>
26 #include <QtCore/QString>
27 #include <QtCore/QStringList>
28 
80 namespace KAccelGen
81 {
82 
83 // HELPERS
84 
88 template <class Iter>
89 class Deref
90 {
91 public:
92  static QString deref(Iter i) { return *i; }
93 };
94 
99 template <class Iter>
100 class Deref_Key
101 {
102 public:
103  static QString deref(Iter i) { return i.key(); }
104 };
105 
113 inline bool
114 isLegalAccelerator(const QString& str, int index)
115 {
116  return index >= 0 && index < str.length()
117  && str[index].isLetterOrNumber();
118 }
119 
128 template <class Iter, class Deref>
129 inline void
130 loadPredefined(Iter begin, Iter end, QMap<QChar,bool>& keys)
131 {
132  for (Iter i = begin; i != end; ++i) {
133  QString item = Deref::deref(i);
134  int user_ampersand = item.indexOf(QLatin1Char('&'));
135  if( user_ampersand >= 0 ) {
136  // Sanity check. Note that we don't try to find an
137  // accelerator if the user shoots him/herself in the foot
138  // by adding a bad '&'.
139  if( isLegalAccelerator(item, user_ampersand+1) ) {
140  keys.insert(item[user_ampersand+1], true);
141  }
142  }
143  }
144 }
145 
146 
147 // ///////////////////////////////////////////////////////////////////
148 // MAIN USER FUNCTIONS
149 
150 
165 template <class Iter, class Iter_Deref >
166 void
167 generate(Iter begin, Iter end, QStringList& target)
168 {
169  // Will keep track of used accelerator chars
170  QMap<QChar,bool> used_accels;
171 
172  // Prepass to detect manually user-coded accelerators
173  loadPredefined<Iter,Iter_Deref>(begin, end, used_accels);
174 
175  // Main pass
176  for (Iter i = begin; i != end; ++i) {
177  QString item = Iter_Deref::deref(i);
178 
179  // Attempt to find a good accelerator, but only if the user
180  // has not manually hardcoded one.
181  int user_ampersand = item.indexOf(QLatin1Char('&'));
182  if( user_ampersand < 0 || item[user_ampersand+1] == QLatin1Char('&')) {
183  bool found = false;
184  int j;
185 
186  // Check word-starting letters first.
187  for( j=0; j < item.length(); ++j ) {
188  if( isLegalAccelerator(item, j)
189  && !used_accels.contains(item[j])
190  && (0 == j || (j > 0 && item[j-1].isSpace())) ) {
191  found = true;
192  break;
193  }
194  }
195 
196  if( !found ) {
197  // No word-starting letter; search for any letter.
198  for( j=0; j < item.length(); ++j ) {
199  if( isLegalAccelerator(item, j)
200  && !used_accels.contains(item[j]) ) {
201  found = true;
202  break;
203  }
204  }
205  }
206 
207  if( found ) {
208  // Both upper and lower case marked as used
209  used_accels.insert(item[j].toUpper(),true);
210  used_accels.insert(item[j].toLower(),true);
211  item.insert(j,QLatin1Char('&'));
212  }
213  }
214 
215  target.append( item );
216  }
217 }
218 
227 template <class Iter>
228 inline void
229 generateFromKeys(Iter begin, Iter end, QStringList& target)
230 {
231  generate< Iter, Deref_Key<Iter> >(begin, end, target);
232 }
233 
234 
241 inline void
242 generate(const QStringList& source, QStringList& target)
243 {
244  generate<QStringList::ConstIterator, Deref<QStringList::ConstIterator> >(source.begin(), source.end(), target);
245 }
246 
253 template <class Key>
254 inline void
255 generateFromValues(const QMap<Key,QString>& source, QStringList& target)
256 {
257  generate<typename QMap<Key,QString>::ConstIterator, Deref_Key<typename QMap<Key,QString>::ConstIterator> >(source.begin(), source.end(), target);
258 }
259 
266 template <class Data>
267 inline void
268 generateFromKeys(const QMap<QString,Data>& source, QStringList& target)
269 {
270  generateFromKeys(source.begin(), source.end(), target);
271 }
272 
273 
274 } // end namespace KAccelGen
275 
276 #endif
277 
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Thu Sep 25 2014 04:19:50 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdelibs-4.11.5 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