kaccelgen.h
/* This file is part of the KDE project
Copyright (C) 2000 Keunwoo Lee <klee@cs.washington.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KACCELGEN_H
#define KACCELGEN_H
#include <qmap.h>
#include <qstring.h>
#include <qstringlist.h>
#include <kdelibs_export.h>
namespace KAccelGen
{
// HELPERS
template <class Iter>
class Deref
{
public:
static QString deref(Iter i) { return *i; }
};
template <class Iter>
class Deref_Key
{
public:
static QString deref(Iter i) { return i.key(); }
};
inline bool
isLegalAccelerator(const QString& str, uint index)
{
return index < str.length()
&& str[index].isLetterOrNumber();
}
template <class Iter, class Deref>
inline void
loadPredefined(Iter begin, Iter end, QMap<QChar,bool>& keys)
{
for (Iter i = begin; i != end; ++i) {
QString item = Deref::deref(i);
int user_ampersand = item.find(QChar('&'));
if( user_ampersand >= 0 ) {
// Sanity check. Note that we don't try to find an
// accelerator if the user shoots him/herself in the foot
// by adding a bad '&'.
if( isLegalAccelerator(item, user_ampersand+1) ) {
keys.insert(item[user_ampersand+1], true);
}
}
}
}
// ///////////////////////////////////////////////////////////////////
// MAIN USER FUNCTIONS
template <class Iter, class Iter_Deref >
void
generate(Iter begin, Iter end, QStringList& target)
{
// Will keep track of used accelerator chars
QMap<QChar,bool> used_accels;
// Prepass to detect manually user-coded accelerators
loadPredefined<Iter,Iter_Deref>(begin, end, used_accels);
// Main pass
for (Iter i = begin; i != end; ++i) {
QString item = Iter_Deref::deref(i);
// Attempt to find a good accelerator, but only if the user
// has not manually hardcoded one.
int user_ampersand = item.find(QChar('&'));
if( user_ampersand < 0 || item[user_ampersand+1] == '&') {
bool found = false;
uint found_idx;
uint j;
// Check word-starting letters first.
for( j=0; j < item.length(); ++j ) {
if( isLegalAccelerator(item, j)
&& !used_accels.contains(item[j])
&& (0 == j || (j > 0 && item[j-1].isSpace())) ) {
found = true;
found_idx = j;
break;
}
}
if( !found ) {
// No word-starting letter; search for any letter.
for( j=0; j < item.length(); ++j ) {
if( isLegalAccelerator(item, j)
&& !used_accels.contains(item[j]) ) {
found = true;
found_idx = j;
break;
}
}
}
if( found ) {
// Both upper and lower case marked as used
used_accels.insert(item[j].upper(),true);
used_accels.insert(item[j].lower(),true);
item.insert(j,QChar('&'));
}
}
target.append( item );
}
}
template <class Iter>
inline void
generateFromKeys(Iter begin, Iter end, QStringList& target)
{
generate< Iter, Deref_Key<Iter> >(begin, end, target);
}
inline void
generate(const QStringList& source, QStringList& target)
{
generate<QStringList::ConstIterator, Deref<QStringList::ConstIterator> >(source.begin(), source.end(), target);
}
template <class Key>
inline void
generateFromValues(const QMap<Key,QString>& source, QStringList& target)
{
generate<QMapConstIterator<Key,QString>, Deref_Key<QMapConstIterator<Key,QString> > >(source.begin(), source.end(), target);
}
template <class Data>
inline void
generateFromKeys(const QMap<QString,Data>& source, QStringList& target)
{
generateFromKeys(source.begin(), source.end(), target);
}
} // end namespace KAccelGen
#endif
KAccelGen::generateFromValues
void generateFromValues(const QMap< Key, QString > &source, QStringList &target)
Convenience function; generates accelerators for all the values in a QMap<T,QString>.
Definition: kaccelgen.h:258
KAccelGen::generate
void generate(Iter begin, Iter end, QStringList &target)
Main, maximally flexible template function that assigns accelerators to the elements of a collection ...
Definition: kaccelgen.h:167
KAccelGen::isLegalAccelerator
bool isLegalAccelerator(const QString &str, uint index)
Helper to determine if the given offset in the string could be a legal alphanumeric accelerator...
Definition: kaccelgen.h:114
KAccelGen::Deref
Static dereference class, for use as a template parameter.
Definition: kaccelgen.h:89
KAccelGen::loadPredefined
void loadPredefined(Iter begin, Iter end, QMap< QChar, bool > &keys)
Loads all legal predefined accelerators in the (implicitly specified) collection into the given QMap...
Definition: kaccelgen.h:130
KAccelGen::Deref_Key
Static dereference class that calls the key() method on its target; for use as a template parameter...
Definition: kaccelgen.h:100
KAccelGen::generateFromKeys
void generateFromKeys(Iter begin, Iter end, QStringList &target)
Another convenience function; looks up the key instead of dereferencing directly for the given iterat...
Definition: kaccelgen.h:232

KDE Home | KDE Accessibility Home | Description of Access Keys