• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
tdeaccelgen.h
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 TDEACCELGEN_H
21#define TDEACCELGEN_H
22
23#include <tqmap.h>
24#include <tqstring.h>
25#include <tqstringlist.h>
26
27#include <tdelibs_export.h>
28
80namespace TDEAccelGen
81{
82
83// HELPERS
84
88template <class Iter>
89class Deref
90{
91public:
92 static TQString deref(Iter i) { return *i; }
93};
94
99template <class Iter>
100class Deref_Key
101{
102public:
103 static TQString deref(Iter i) { return i.key(); }
104};
105
113inline bool
114isLegalAccelerator(const TQString& str, uint index)
115{
116 return index < str.length()
117 && str[index].isLetterOrNumber();
118}
119
128template <class Iter, class Deref>
129inline void
130loadPredefined(Iter begin, Iter end, TQMap<TQChar,bool>& keys)
131{
132 for (Iter i = begin; i != end; ++i) {
133 TQString item = Deref::deref(i);
134 int user_ampersand = item.find(TQChar('&'));
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
165template <class Iter, class Iter_Deref >
166void
167generate(Iter begin, Iter end, TQStringList& target)
168{
169 // Will keep track of used accelerator chars
170 TQMap<TQChar,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 TQString 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.find(TQChar('&'));
182 if( user_ampersand < 0 || item[user_ampersand+1] == '&') {
183 bool found = false;
184 uint found_idx;
185 uint j;
186
187 // Check word-starting letters first.
188 for( j=0; j < item.length(); ++j ) {
189 if( isLegalAccelerator(item, j)
190 && !used_accels.contains(item[j])
191 && (0 == j || (j > 0 && item[j-1].isSpace())) ) {
192 found = true;
193 found_idx = j;
194 break;
195 }
196 }
197
198 if( !found ) {
199 // No word-starting letter; search for any letter.
200 for( j=0; j < item.length(); ++j ) {
201 if( isLegalAccelerator(item, j)
202 && !used_accels.contains(item[j]) ) {
203 found = true;
204 found_idx = j;
205 break;
206 }
207 }
208 }
209
210 if( found ) {
211 // Both upper and lower case marked as used
212 used_accels.insert(item[j].upper(),true);
213 used_accels.insert(item[j].lower(),true);
214 item.insert(j,TQChar('&'));
215 }
216 }
217
218 target.append( item );
219 }
220}
221
230template <class Iter>
231inline void
232generateFromKeys(Iter begin, Iter end, TQStringList& target)
233{
234 generate< Iter, Deref_Key<Iter> >(begin, end, target);
235}
236
237
244inline void
245generate(const TQStringList& source, TQStringList& target)
246{
247 generate<TQStringList::ConstIterator, Deref<TQStringList::ConstIterator> >(source.begin(), source.end(), target);
248}
249
256template <class Key>
257inline void
258generateFromValues(const TQMap<Key,TQString>& source, TQStringList& target)
259{
260 generate<TQMapConstIterator<Key,TQString>, Deref_Key<TQMapConstIterator<Key,TQString> > >(source.begin(), source.end(), target);
261}
262
269template <class Data>
270inline void
271generateFromKeys(const TQMap<TQString,Data>& source, TQStringList& target)
272{
273 generateFromKeys(source.begin(), source.end(), target);
274}
275
276
277} // end namespace TDEAccelGen
278
279#endif
280
TDEAccelGen::Deref_Key
Static dereference class that calls the key() method on its target; for use as a template parameter.
Definition: tdeaccelgen.h:101
TDEAccelGen::Deref
Static dereference class, for use as a template parameter.
Definition: tdeaccelgen.h:90
TDEAccelGen
Provides functions that, given a collection of QStrings, will automatically and intelligently assign ...
Definition: tdeaccelgen.h:81
TDEAccelGen::generateFromValues
void generateFromValues(const TQMap< Key, TQString > &source, TQStringList &target)
Convenience function; generates accelerators for all the values in a TQMap<T,TQString>.
Definition: tdeaccelgen.h:258
TDEAccelGen::loadPredefined
void loadPredefined(Iter begin, Iter end, TQMap< TQChar, bool > &keys)
Loads all legal predefined accelerators in the (implicitly specified) collection into the given TQMap...
Definition: tdeaccelgen.h:130
TDEAccelGen::generateFromKeys
void generateFromKeys(Iter begin, Iter end, TQStringList &target)
Another convenience function; looks up the key instead of dereferencing directly for the given iterat...
Definition: tdeaccelgen.h:232
TDEAccelGen::generate
void generate(Iter begin, Iter end, TQStringList &target)
Main, maximally flexible template function that assigns accelerators to the elements of a collection ...
Definition: tdeaccelgen.h:167
TDEAccelGen::isLegalAccelerator
bool isLegalAccelerator(const TQString &str, uint index)
Helper to determine if the given offset in the string could be a legal alphanumeric accelerator.
Definition: tdeaccelgen.h:114

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.