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

kdecore

  • kdecore
kshortcut.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001,2002 Ellis Whitehead <ellis@kde.org>
3 
4  This library 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 library 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 GNU
12  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 #include "kshortcut.h"
21 #include "kkeynative.h"
22 #include "kkeyserver.h"
23 
24 #include <tqevent.h>
25 #include <tqstringlist.h>
26 
27 #include <kdebug.h>
28 #include <kglobal.h>
29 #include <klocale.h>
30 #include <ksimpleconfig.h>
31 
32 //----------------------------------------------------
33 
34 static KKey* g_pspec = 0;
35 static KKeySequence* g_pseq = 0;
36 static KShortcut* g_pcut = 0;
37 
38 //----------------------------------------------------
39 // KKey
40 //----------------------------------------------------
41 
42 KKey::KKey() { clear(); }
43 KKey::KKey( uint key, uint modFlags ) { init( key, modFlags ); }
44 KKey::KKey( int keyQt ) { init( keyQt ); }
45 KKey::KKey( const TQKeySequence& seq ) { init( seq ); }
46 KKey::KKey( const TQKeyEvent* pEvent ) { init( pEvent ); }
47 KKey::KKey( const KKey& key ) { init( key ); }
48 KKey::KKey( const TQString& sKey ) { init( sKey ); }
49 
50 KKey::~KKey()
51 {
52 }
53 
54 void KKey::clear()
55 {
56  m_sym = 0;
57  m_mod = 0;
58 }
59 
60 bool KKey::init( uint key, uint modFlags )
61 {
62  m_sym = key;
63  m_mod = modFlags;
64  return true;
65 }
66 
67 bool KKey::init( int keyQt )
68 {
69  //KKeyServer::Sym sym;
70 
71  //if( sym.initQt( keyQt )
72  if( KKeyServer::keyQtToSym( keyQt, m_sym )
73  && KKeyServer::keyQtToMod( keyQt, m_mod ) )
74  return true;
75  else {
76  m_sym = 0;
77  m_mod = 0;
78  return false;
79  }
80 }
81 
82 bool KKey::init( const TQKeySequence& key )
83 {
84  // TODO: if key.count() > 1, should we return failure?
85  return init( (int) key );
86 }
87 
88 bool KKey::init( const TQKeyEvent* pEvent )
89 {
90  int keyQt = pEvent->key();
91  if( pEvent->state() & TQt::ShiftButton ) keyQt |= Qt::SHIFT;
92  if( pEvent->state() & TQt::ControlButton ) keyQt |= Qt::CTRL;
93  if( pEvent->state() & TQt::AltButton ) keyQt |= Qt::ALT;
94  if( pEvent->state() & TQt::MetaButton ) keyQt |= Qt::META;
95  return init( keyQt );
96 }
97 
98 bool KKey::init( const KKey& key )
99 {
100  m_sym = key.m_sym;
101  m_mod = key.m_mod;
102  return true;
103 }
104 
105 bool KKey::init( const TQString& sSpec )
106 {
107  clear();
108 
109  TQString sKey = sSpec.stripWhiteSpace();
110  if( sKey.startsWith( "default(" ) && sKey.endsWith( ")" ) )
111  sKey = sKey.mid( 8, sKey.length() - 9 );
112  // i.e., "Ctrl++" = "Ctrl+Plus"
113  if( sKey.endsWith( "++" ) )
114  sKey = sKey.left( sKey.length() - 1 ) + "plus";
115  TQStringList rgs = TQStringList::split( '+', sKey, true );
116 
117  uint i;
118  // Check for modifier keys first.
119  for( i = 0; i < rgs.size(); i++ ) {
120  TQString s = rgs[i].lower();
121  if( s == "shift" ) m_mod |= KKey::SHIFT;
122  else if( s == "ctrl" ) m_mod |= KKey::CTRL;
123  else if( s == "alt" ) m_mod |= KKey::ALT;
124  else if( s == "win" ) m_mod |= KKey::WIN;
125  else if( s == "meta" ) m_mod |= KKey::WIN;
126  else {
127  uint m = KKeyServer::stringUserToMod( s );
128  if( m != 0 ) m_mod |= m;
129  else break;
130  }
131  }
132  // If there is one non-blank key left:
133  if( (i == rgs.size() - 1 && !rgs[i].isEmpty()) ) {
134  KKeyServer::Sym sym( rgs[i] );
135  m_sym = sym.m_sym;
136  }
137 
138  if( m_sym == 0 )
139  m_mod = 0;
140 
141  kdDebug(125) << "KKey::init( \"" << sSpec << "\" ):"
142  << " m_sym = " << TQString::number(m_sym, 16)
143  << ", m_mod = " << TQString::number(m_mod, 16) << endl;
144 
145  return m_sym != 0;
146 }
147 
148 bool KKey::isNull() const { return m_sym == 0; }
149 uint KKey::sym() const { return m_sym; }
150 uint KKey::modFlags() const { return m_mod; }
151 
152 int KKey::compare( const KKey& spec ) const
153 {
154  if( m_sym != spec.m_sym )
155  return m_sym - spec.m_sym;
156  if( m_mod != spec.m_mod )
157  return m_mod - spec.m_mod;
158  return 0;
159 }
160 
161 int KKey::keyCodeQt() const
162 {
163  return KKeyNative( *this ).keyCodeQt();
164 }
165 
166 TQString KKey::toString() const
167 {
168  TQString s;
169 
170  s = KKeyServer::modToStringUser( m_mod );
171  if( !s.isEmpty() )
172  s += '+';
173  s += KKeyServer::Sym(m_sym).toString();
174 
175  return s;
176 }
177 
178 TQString KKey::toStringInternal() const
179 {
180  //kdDebug(125) << "KKey::toStringInternal(): this = " << this
181  // << " mod = " << TQString::number(m_mod, 16)
182  // << " key = " << TQString::number(m_sym, 16) << endl;
183  TQString s;
184 
185  s = KKeyServer::modToStringInternal( m_mod );
186  if( !s.isEmpty() )
187  s += '+';
188  s += KKeyServer::Sym(m_sym).toStringInternal();
189  return s;
190 }
191 
192 KKey& KKey::null()
193 {
194  if( !g_pspec )
195  g_pspec = new KKey;
196  if( !g_pspec->isNull() )
197  g_pspec->clear();
198  return *g_pspec;
199 }
200 
201 TQString KKey::modFlagLabel( ModFlag modFlag )
202 {
203  return KKeyServer::modToStringUser( modFlag );
204 }
205 
206 //---------------------------------------------------------------------
207 // KKeySequence
208 //---------------------------------------------------------------------
209 
210 KKeySequence::KKeySequence() { clear(); }
211 KKeySequence::KKeySequence( const TQKeySequence& seq ) { init( seq ); }
212 KKeySequence::KKeySequence( const KKey& key ) { init( key ); }
213 KKeySequence::KKeySequence( const KKeySequence& seq ) { init( seq ); }
214 KKeySequence::KKeySequence( const TQString& s ) { init( s ); }
215 
216 KKeySequence::~KKeySequence()
217 {
218 }
219 
220 void KKeySequence::clear()
221 {
222  m_nKeys = 0;
223  m_bTriggerOnRelease = false;
224 }
225 
226 bool KKeySequence::init( const TQKeySequence& seq )
227 {
228  clear();
229  if( !seq.isEmpty() ) {
230  for( uint i = 0; i < seq.count(); i++ ) {
231  m_rgvar[i].init( seq[i] );
232  if( m_rgvar[i].isNull() )
233  return false;
234  }
235  m_nKeys = seq.count();
236  m_bTriggerOnRelease = false;
237  }
238  return true;
239 }
240 
241 bool KKeySequence::init( const KKey& key )
242 {
243  if( !key.isNull() ) {
244  m_nKeys = 1;
245  m_rgvar[0].init( key );
246  m_bTriggerOnRelease = false;
247  } else
248  clear();
249  return true;
250 }
251 
252 bool KKeySequence::init( const KKeySequence& seq )
253 {
254  m_bTriggerOnRelease = false;
255  m_nKeys = seq.m_nKeys;
256  for( uint i = 0; i < m_nKeys; i++ ) {
257  if( seq.m_rgvar[i].isNull() ) {
258  kdDebug(125) << "KKeySequence::init( seq ): key[" << i << "] is null." << endl;
259  m_nKeys = 0;
260  return false;
261  }
262  m_rgvar[i] = seq.m_rgvar[i];
263  }
264  return true;
265 }
266 
267 bool KKeySequence::init( const TQString& s )
268 {
269  m_bTriggerOnRelease = false;
270  //kdDebug(125) << "KKeySequence::init( " << s << " )" << endl;
271  TQStringList rgs = TQStringList::split( ',', s );
272  if( s == "none" || rgs.size() == 0 ) {
273  clear();
274  return true;
275  } else if( rgs.size() <= MAX_KEYS ) {
276  m_nKeys = rgs.size();
277  for( uint i = 0; i < m_nKeys; i++ ) {
278  m_rgvar[i].init( KKey(rgs[i]) );
279  //kdDebug(125) << "\t'" << rgs[i] << "' => " << m_rgvar[i].toStringInternal() << endl;
280  }
281  return true;
282  } else {
283  clear();
284  return false;
285  }
286 }
287 
288 uint KKeySequence::count() const
289 {
290  return m_nKeys;
291 }
292 
293 const KKey& KKeySequence::key( uint i ) const
294 {
295  if( i < m_nKeys )
296  return m_rgvar[i];
297  else
298  return KKey::null();
299 }
300 
301 bool KKeySequence::isTriggerOnRelease() const
302  { return m_bTriggerOnRelease; }
303 
304 bool KKeySequence::setKey( uint iKey, const KKey& key )
305 {
306  if( iKey <= m_nKeys && iKey < MAX_KEYS ) {
307  m_rgvar[iKey].init( key );
308  if( iKey == m_nKeys )
309  m_nKeys++;
310  return true;
311  } else
312  return false;
313 }
314 
315 bool KKeySequence::isNull() const
316 {
317  return m_nKeys == 0;
318 }
319 
320 bool KKeySequence::startsWith( const KKeySequence& seq ) const
321 {
322  if( m_nKeys < seq.m_nKeys )
323  return false;
324 
325  for( uint i = 0; i < seq.m_nKeys; i++ ) {
326  if( m_rgvar[i] != seq.m_rgvar[i] )
327  return false;
328  }
329 
330  return true;
331 }
332 
333 int KKeySequence::compare( const KKeySequence& seq ) const
334 {
335  for( uint i = 0; i < m_nKeys && i < seq.m_nKeys; i++ ) {
336  int ret = m_rgvar[i].compare( seq.m_rgvar[i] );
337  if( ret != 0 )
338  return ret;
339  }
340  if( m_nKeys != seq.m_nKeys )
341  return m_nKeys - seq.m_nKeys;
342  else
343  return 0;
344 }
345 
346 TQKeySequence KKeySequence::qt() const
347 {
348  int k[4] = { 0, 0, 0, 0 };
349 
350  for( uint i = 0; i < count(); i++ )
351  k[i] = KKeyNative(key(i)).keyCodeQt();
352  TQKeySequence seq( k[0], k[1], k[2], k[3] );
353  return seq;
354 }
355 
356 int KKeySequence::keyCodeQt() const
357 {
358  return (count() == 1) ? KKeyNative(key(0)).keyCodeQt() : 0;
359 }
360 
361 TQString KKeySequence::toString() const
362 {
363  if( m_nKeys < 1 ) return TQString::null;
364 
365  TQString s;
366  s = m_rgvar[0].toString();
367  for( uint i = 1; i < m_nKeys; i++ ) {
368  s += ",";
369  s += m_rgvar[i].toString();
370  }
371 
372  return s;
373 }
374 
375 TQString KKeySequence::toStringInternal() const
376 {
377  if( m_nKeys < 1 ) return TQString::null;
378 
379  TQString s;
380  s = m_rgvar[0].toStringInternal();
381  for( uint i = 1; i < m_nKeys; i++ ) {
382  s += ",";
383  s += m_rgvar[i].toStringInternal();
384  }
385 
386  return s;
387 }
388 
389 KKeySequence& KKeySequence::null()
390 {
391  if( !g_pseq )
392  g_pseq = new KKeySequence;
393  if( !g_pseq->isNull() )
394  g_pseq->clear();
395  return *g_pseq;
396 }
397 
398 //---------------------------------------------------------------------
399 // KShortcut
400 //---------------------------------------------------------------------
401 
402 KShortcut::KShortcut() { clear(); }
403 KShortcut::KShortcut( int keyQt ) { init( keyQt ); }
404 KShortcut::KShortcut( const TQKeySequence& key ) { init( key ); }
405 KShortcut::KShortcut( const KKey& key ) { init( key ); }
406 KShortcut::KShortcut( const KKeySequence& seq ) { init( seq ); }
407 KShortcut::KShortcut( const KShortcut& cut ) { init( cut ); }
408 KShortcut::KShortcut( const char* ps ) { init( TQString(ps) ); }
409 KShortcut::KShortcut( const TQString& s ) { init( s ); }
410 
411 KShortcut::~KShortcut()
412 {
413 }
414 
415 void KShortcut::clear()
416 {
417  m_nSeqs = 0;
418 }
419 
420 bool KShortcut::init( int keyQt )
421 {
422  if( keyQt ) {
423  m_nSeqs = 1;
424  m_rgseq[0].init( TQKeySequence(keyQt) );
425  } else
426  clear();
427  return true;
428 }
429 
430 bool KShortcut::init( const TQKeySequence& key )
431 {
432  m_nSeqs = 1;
433  m_rgseq[0].init( key );
434  return true;
435 }
436 
437 bool KShortcut::init( const KKey& spec )
438 {
439  m_nSeqs = 1;
440  m_rgseq[0].init( spec );
441  return true;
442 }
443 
444 bool KShortcut::init( const KKeySequence& seq )
445 {
446  m_nSeqs = 1;
447  m_rgseq[0] = seq;
448  return true;
449 }
450 
451 bool KShortcut::init( const KShortcut& cut )
452 {
453  m_nSeqs = cut.m_nSeqs;
454  for( uint i = 0; i < m_nSeqs; i++ )
455  m_rgseq[i] = cut.m_rgseq[i];
456  return true;
457 }
458 
459 bool KShortcut::init( const TQString& s )
460 {
461  bool bRet = true;
462  TQStringList rgs = TQStringList::split( ';', s );
463 
464  if( s == "none" || rgs.size() == 0 )
465  clear();
466  else if( rgs.size() <= MAX_SEQUENCES ) {
467  m_nSeqs = rgs.size();
468  for( uint i = 0; i < m_nSeqs; i++ ) {
469  TQString& sSeq = rgs[i];
470  if( sSeq.startsWith( "default(" ) )
471  sSeq = sSeq.mid( 8, sSeq.length() - 9 );
472  m_rgseq[i].init( sSeq );
473  //kdDebug(125) << "*\t'" << sSeq << "' => " << m_rgseq[i].toStringInternal() << endl;
474  }
475  } else {
476  clear();
477  bRet = false;
478  }
479 
480  if( !s.isEmpty() ) {
481  TQString sDebug;
482  TQTextStream os( &sDebug, IO_WriteOnly );
483  os << "KShortcut::init( \"" << s << "\" ): ";
484  for( uint i = 0; i < m_nSeqs; i++ ) {
485  os << " m_rgseq[" << i << "]: ";
486  KKeyServer::Variations vars;
487  vars.init( m_rgseq[i].key(0), true );
488  for( uint j = 0; j < vars.count(); j++ )
489  os << TQString::number(vars.m_rgkey[j].keyCodeQt(),16) << ',';
490  }
491  kdDebug(125) << sDebug << endl;
492  }
493 
494  return bRet;
495 }
496 
497 uint KShortcut::count() const
498 {
499  return m_nSeqs;
500 }
501 
502 const KKeySequence& KShortcut::seq( uint i ) const
503 {
504  return (i < m_nSeqs) ? m_rgseq[i] : KKeySequence::null();
505 }
506 
507 int KShortcut::keyCodeQt() const
508 {
509  if( m_nSeqs >= 1 )
510  return m_rgseq[0].keyCodeQt();
511  return TQKeySequence();
512 }
513 
514 bool KShortcut::isNull() const
515 {
516  return m_nSeqs == 0;
517 }
518 
519 int KShortcut::compare( const KShortcut& cut ) const
520 {
521  for( uint i = 0; i < m_nSeqs && i < cut.m_nSeqs; i++ ) {
522  int ret = m_rgseq[i].compare( cut.m_rgseq[i] );
523  if( ret != 0 )
524  return ret;
525  }
526  return m_nSeqs - cut.m_nSeqs;
527 }
528 
529 bool KShortcut::contains( const KKey& key ) const
530 {
531  return contains( KKeySequence(key) );
532 }
533 
534 bool KShortcut::contains( const KKeyNative& keyNative ) const
535 {
536  KKey key = keyNative.key();
537  key.simplify();
538 
539  for( uint i = 0; i < count(); i++ ) {
540  if( !m_rgseq[i].isNull()
541  && m_rgseq[i].count() == 1
542  && m_rgseq[i].key(0) == key )
543  return true;
544  }
545  return false;
546 }
547 
548 bool KShortcut::contains( const KKeySequence& seq ) const
549 {
550  for( uint i = 0; i < count(); i++ ) {
551  if( !m_rgseq[i].isNull() && m_rgseq[i] == seq )
552  return true;
553  }
554  return false;
555 }
556 
557 bool KShortcut::setSeq( uint iSeq, const KKeySequence& seq )
558 {
559  // TODO: check if seq is null, and act accordingly.
560  if( iSeq <= m_nSeqs && iSeq < MAX_SEQUENCES ) {
561  m_rgseq[iSeq] = seq;
562  if( iSeq == m_nSeqs )
563  m_nSeqs++;
564  return true;
565  } else
566  return false;
567 }
568 
569 void KShortcut::remove( const KKeySequence& seq )
570 {
571  if (seq.isNull()) return;
572 
573  for( uint iSeq = 0; iSeq < m_nSeqs; iSeq++ )
574  {
575  if (m_rgseq[iSeq] == seq)
576  {
577  for( uint jSeq = iSeq + 1; jSeq < m_nSeqs; jSeq++)
578  m_rgseq[jSeq-1] = m_rgseq[jSeq];
579  m_nSeqs--;
580  }
581  }
582 }
583 
584 bool KShortcut::append( const KKeySequence& seq )
585 {
586  if( m_nSeqs < MAX_SEQUENCES ) {
587  if( !seq.isNull() ) {
588  m_rgseq[m_nSeqs] = seq;
589  m_nSeqs++;
590  }
591  return true;
592  } else
593  return false;
594 }
595 
596 bool KShortcut::append( const KKey& spec )
597 {
598  if( m_nSeqs < MAX_SEQUENCES ) {
599  m_rgseq[m_nSeqs].init( spec );
600  m_nSeqs++;
601  return true;
602  } else
603  return false;
604 }
605 
606 bool KShortcut::append( const KShortcut& cut )
607 {
608  uint seqs = m_nSeqs, co = cut.count();
609  for( uint i=0; i<co; i++ ) {
610  if (!contains(cut.seq(i))) seqs++;
611  }
612  if( seqs > MAX_SEQUENCES ) return false;
613 
614  for( uint i=0; i<co; i++ ) {
615  const KKeySequence& seq = cut.seq(i);
616  if(!contains(seq)) {
617  m_rgseq[m_nSeqs] = seq;
618  m_nSeqs++;
619  }
620  }
621  return true;
622 }
623 
624 KShortcut::operator TQKeySequence () const
625 {
626  if( count() >= 1 )
627  return m_rgseq[0].qt();
628  else
629  return TQKeySequence();
630 }
631 
632 TQString KShortcut::toString() const
633 {
634  TQString s;
635 
636  for( uint i = 0; i < count(); i++ ) {
637  s += m_rgseq[i].toString();
638  if( i < count() - 1 )
639  s += ';';
640  }
641 
642  return s;
643 }
644 
645 TQString KShortcut::toStringInternal( const KShortcut* pcutDefault ) const
646 {
647  TQString s;
648 
649  for( uint i = 0; i < count(); i++ ) {
650  const KKeySequence& seq = m_rgseq[i];
651  if( pcutDefault && i < pcutDefault->count() && seq == (*pcutDefault).seq(i) ) {
652  s += "default(";
653  s += seq.toStringInternal();
654  s += ")";
655  } else
656  s += seq.toStringInternal();
657  if( i < count() - 1 )
658  s += ';';
659  }
660 
661  return s;
662 }
663 
664 KShortcut& KShortcut::null()
665 {
666  if( !g_pcut )
667  g_pcut = new KShortcut;
668  if( !g_pcut->isNull() )
669  g_pcut->clear();
670  return *g_pcut;
671 }

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |