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

kdecore

  • kdecore
kaccelmanager.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2002 Matthias H�lzer-Kl�pfel <mhk@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 "kaccelmanager.h"
21 
22 #include <tqapplication.h>
23 #include <tqcheckbox.h>
24 #include <tqcombobox.h>
25 #include <tqgroupbox.h>
26 #include <tqlabel.h>
27 #include <tqlineedit.h>
28 #include <tqmenubar.h>
29 #include <tqmemarray.h>
30 #include <tqmetaobject.h>
31 #include <tqmainwindow.h>
32 #include <tqobjectlist.h>
33 #include <tqpopupmenu.h>
34 #include <tqptrlist.h>
35 #include <tqpushbutton.h>
36 #include <tqradiobutton.h>
37 #include <tqspinbox.h>
38 #include <tqtabbar.h>
39 #include <tqtextview.h>
40 #include <tqwidget.h>
41 #include <tqwidgetstack.h>
42 
43 #include <kstdaction.h>
44 #include <kstaticdeleter.h>
45 #include <kdebug.h>
46 
47 
48 #include "kaccelmanager_private.h"
49 #include "../kdeui/kstdaction_p.h"
50 #include "../kutils/kmultitabbar.h"
51 
52 
53 /*********************************************************************
54 
55  class Item - helper class containing widget information
56 
57  This class stores information about the widgets the need accelerators,
58  as well as about their relationship.
59 
60  *********************************************************************/
61 
62 
63 
64 /*********************************************************************
65 
66  class KAcceleratorManagerPrivate - internal helper class
67 
68  This class does all the work to find accelerators for a hierarchy of
69  widgets.
70 
71  *********************************************************************/
72 
73 
74 class KAcceleratorManagerPrivate
75 {
76 public:
77 
78  static void manage(TQWidget *widget);
79  static bool programmers_mode;
80  static bool standardName(const TQString &str);
81 
82  static bool checkChange(const KAccelString &as) {
83  TQString t2 = as.accelerated();
84  TQString t1 = as.originalText();
85  if (t1 != t2)
86  {
87  if (as.accel() == -1) {
88  removed_string += "<tr><td>" + TQStyleSheet::escape(t1) + "</td></tr>";
89  } else if (as.originalAccel() == -1) {
90  added_string += "<tr><td>" + TQStyleSheet::escape(t2) + "</td></tr>";
91  } else {
92  changed_string += "<tr><td>" + TQStyleSheet::escape(t1) + "</td>";
93  changed_string += "<td>" + TQStyleSheet::escape(t2) + "</td></tr>";
94  }
95  return true;
96  }
97  return false;
98  }
99  static TQString changed_string;
100  static TQString added_string;
101  static TQString removed_string;
102  static TQMap<TQWidget *, int> ignored_widgets;
103 
104 private:
105  class Item;
106 public:
107  typedef TQPtrList<Item> ItemList;
108 
109 private:
110  static void traverseChildren(TQWidget *widget, Item *item);
111 
112  static void manageWidget(TQWidget *widget, Item *item);
113  static void manageMenuBar(TQMenuBar *mbar, Item *item);
114  static void manageTabBar(TQTabBar *bar, Item *item);
115 
116  static void calculateAccelerators(Item *item, TQString &used);
117 
118  class Item
119  {
120  public:
121 
122  Item() : m_widget(0), m_children(0), m_index(-1) {}
123  ~Item();
124 
125  void addChild(Item *item);
126 
127  TQWidget *m_widget;
128  KAccelString m_content;
129  ItemList *m_children;
130  int m_index;
131 
132  };
133 };
134 
135 
136 bool KAcceleratorManagerPrivate::programmers_mode = false;
137 TQString KAcceleratorManagerPrivate::changed_string;
138 TQString KAcceleratorManagerPrivate::added_string;
139 TQString KAcceleratorManagerPrivate::removed_string;
140 static TQStringList *kaccmp_sns = 0;
141 static KStaticDeleter<TQStringList> kaccmp_sns_d;
142 TQMap<TQWidget*, int> KAcceleratorManagerPrivate::ignored_widgets;
143 
144 bool KAcceleratorManagerPrivate::standardName(const TQString &str)
145 {
146  if (!kaccmp_sns)
147  kaccmp_sns_d.setObject(kaccmp_sns, new TQStringList(KStdAction::internal_stdNames()));
148  return kaccmp_sns->contains(str);
149 }
150 
151 KAcceleratorManagerPrivate::Item::~Item()
152 {
153  delete m_children;
154 }
155 
156 
157 void KAcceleratorManagerPrivate::Item::addChild(Item *item)
158 {
159  if (!m_children) {
160  m_children = new ItemList;
161  m_children->setAutoDelete(true);
162  }
163 
164  m_children->append(item);
165 }
166 
167 void KAcceleratorManagerPrivate::manage(TQWidget *widget)
168 {
169  if (!widget)
170  {
171  kdDebug(131) << "null pointer given to manage" << endl;
172  return;
173  }
174 
175  if (dynamic_cast<TQPopupMenu*>(widget))
176  {
177  // create a popup accel manager that can deal with dynamic menus
178  KPopupAccelManager::manage(static_cast<TQPopupMenu*>(widget));
179  return;
180  }
181 
182  Item *root = new Item;
183 
184  manageWidget(widget, root);
185 
186  TQString used;
187  calculateAccelerators(root, used);
188  delete root;
189 }
190 
191 
192 void KAcceleratorManagerPrivate::calculateAccelerators(Item *item, TQString &used)
193 {
194  if (!item->m_children)
195  return;
196 
197  // collect the contents
198  KAccelStringList contents;
199  for (Item *it = item->m_children->first(); it != 0;
200  it = item->m_children->next())
201  {
202  contents << it->m_content;
203  }
204 
205  // find the right accelerators
206  KAccelManagerAlgorithm::findAccelerators(contents, used);
207 
208  // write them back into the widgets
209  int cnt = -1;
210  for (Item *it = item->m_children->first(); it != 0;
211  it = item->m_children->next())
212  {
213  cnt++;
214 
215  TQTabBar *tabBar = dynamic_cast<TQTabBar*>(it->m_widget);
216  if (tabBar)
217  {
218  if (checkChange(contents[cnt]))
219  tabBar->tabAt(it->m_index)->setText(contents[cnt].accelerated());
220  continue;
221  }
222  TQMenuBar *menuBar = dynamic_cast<TQMenuBar*>(it->m_widget);
223  if (menuBar)
224  {
225  if (it->m_index >= 0)
226  {
227  TQMenuItem *mitem = menuBar->findItem(menuBar->idAt(it->m_index));
228  if (mitem)
229  {
230  checkChange(contents[cnt]);
231  mitem->setText(contents[cnt].accelerated());
232  }
233  continue;
234  }
235  }
236  // we possibly reserved an accel, but we won't set it as it looks silly
237  if ( dynamic_cast<TQGroupBox*>( it->m_widget ) )
238  continue;
239  // links look weird with ampersands
240  if ( dynamic_cast<TQLabel*>( it->m_widget ) && it->m_widget->inherits("KURLLabel") )
241  continue;
242 
243  int tprop = it->m_widget->metaObject()->findProperty("text", true);
244  if (tprop != -1) {
245  if (checkChange(contents[cnt]))
246  it->m_widget->setProperty("text", contents[cnt].accelerated());
247  } else {
248  tprop = it->m_widget->metaObject()->findProperty("title", true);
249  if (tprop != -1 && checkChange(contents[cnt]))
250  it->m_widget->setProperty("title", contents[cnt].accelerated());
251  }
252  }
253 
254  // calculate the accelerators for the children
255  for (Item *it = item->m_children->first(); it != 0;
256  it = item->m_children->next())
257  {
258  if (it->m_widget && it->m_widget->isVisibleTo( item->m_widget ) )
259  calculateAccelerators(it, used);
260  }
261 }
262 
263 
264 void KAcceleratorManagerPrivate::traverseChildren(TQWidget *widget, Item *item)
265 {
266  TQObjectList *childList = widget->queryList(TQWIDGET_OBJECT_NAME_STRING, 0, false, false);
267  for ( TQObject *it = childList->first(); it; it = childList->next() )
268  {
269  TQWidget *w = TQT_TQWIDGET(it);
270 
271  if ( !w->isVisibleTo( widget ) || ( w->isTopLevel() && dynamic_cast<TQPopupMenu*>(w) == NULL ) )
272  continue;
273 
274  if ( KAcceleratorManagerPrivate::ignored_widgets.find( w ) != KAcceleratorManagerPrivate::ignored_widgets.end() )
275  continue;
276 
277  manageWidget(w, item);
278  }
279  delete childList;
280 }
281 
282 void KAcceleratorManagerPrivate::manageWidget(TQWidget *w, Item *item)
283 {
284  // first treat the special cases
285 
286  TQTabBar *tabBar = dynamic_cast<TQTabBar*>(w);
287  if (tabBar)
288  {
289  manageTabBar(tabBar, item);
290  return;
291  }
292 
293  TQWidgetStack *wds = dynamic_cast<TQWidgetStack*>( w );
294  if ( wds )
295  {
296  QWidgetStackAccelManager::manage( wds );
297  // return;
298  }
299 
300  TQPopupMenu *popupMenu = dynamic_cast<TQPopupMenu*>(w);
301  if (popupMenu)
302  {
303  // create a popup accel manager that can deal with dynamic menus
304  KPopupAccelManager::manage(popupMenu);
305  return;
306  }
307 
308  TQWidgetStack *wdst = dynamic_cast<TQWidgetStack*>( w );
309  if ( wdst )
310  {
311  QWidgetStackAccelManager::manage( wdst );
312  // return;
313  }
314 
315  TQMenuBar *menuBar = dynamic_cast<TQMenuBar*>(w);
316  if (menuBar)
317  {
318  manageMenuBar(menuBar, item);
319  return;
320  }
321 
322  if (dynamic_cast<TQComboBox*>(w) || dynamic_cast<TQLineEdit*>(w) ||
323  dynamic_cast<TQTextEdit*>(w) || dynamic_cast<TQTextView*>(w) ||
324  dynamic_cast<TQSpinBox*>(w) || static_cast<KMultiTabBar*>(w->qt_cast("KMultiTabBar")))
325  return;
326 
327  // now treat 'ordinary' widgets
328  TQLabel *label = dynamic_cast<TQLabel*>(w);
329  if ( label ) {
330  if ( !label->buddy() )
331  label = 0;
332  else {
333  if ( label->textFormat() == Qt::RichText ||
334  ( label->textFormat() == Qt::AutoText &&
335  TQStyleSheet::mightBeRichText( label->text() ) ) )
336  label = 0;
337  }
338  }
339 
340  if (w->isFocusEnabled() || label || dynamic_cast<TQGroupBox*>(w) || dynamic_cast<TQRadioButton*>( w ))
341  {
342  TQString content;
343  TQVariant variant;
344  int tprop = w->metaObject()->findProperty("text", true);
345  if (tprop != -1) {
346  const TQMetaProperty* p = w->metaObject()->property( tprop, true );
347  if ( p && p->isValid() )
348  w->qt_property( tprop, 1, &variant );
349  else
350  tprop = -1;
351  }
352 
353  if (tprop == -1) {
354  tprop = w->metaObject()->findProperty("title", true);
355  if (tprop != -1) {
356  const TQMetaProperty* p = w->metaObject()->property( tprop, true );
357  if ( p && p->isValid() )
358  w->qt_property( tprop, 1, &variant );
359  }
360  }
361 
362  if (variant.isValid())
363  content = variant.toString();
364 
365  if (!content.isEmpty())
366  {
367  Item *i = new Item;
368  i->m_widget = w;
369 
370  // put some more weight on the usual action elements
371  int weight = KAccelManagerAlgorithm::DEFAULT_WEIGHT;
372  if (dynamic_cast<TQPushButton*>(w) || dynamic_cast<TQCheckBox*>(w) || dynamic_cast<TQRadioButton*>(w) || dynamic_cast<TQLabel*>(w))
373  weight = KAccelManagerAlgorithm::ACTION_ELEMENT_WEIGHT;
374 
375  // don't put weight on group boxes, as usually the contents are more important
376  if (dynamic_cast<TQGroupBox*>(w))
377  weight = KAccelManagerAlgorithm::GROUP_BOX_WEIGHT;
378 
379  // put a lot of extra weight on the KDialogBaseButton's
380  if (w->inherits("KDialogBaseButton"))
381  weight += KAccelManagerAlgorithm::DIALOG_BUTTON_EXTRA_WEIGHT;
382 
383  i->m_content = KAccelString(content, weight);
384  item->addChild(i);
385  }
386  }
387  traverseChildren(w, item);
388 }
389 
390 void KAcceleratorManagerPrivate::manageTabBar(TQTabBar *bar, Item *item)
391 {
392  for (int i=0; i<bar->count(); i++)
393  {
394  TQString content = bar->tabAt(i)->text();
395  if (content.isEmpty())
396  continue;
397 
398  Item *it = new Item;
399  item->addChild(it);
400  it->m_widget = bar;
401  it->m_index = i;
402  it->m_content = KAccelString(content);
403  }
404 }
405 
406 void KAcceleratorManagerPrivate::manageMenuBar(TQMenuBar *mbar, Item *item)
407 {
408  TQMenuItem *mitem;
409  TQString s;
410 
411  for (uint i=0; i<mbar->count(); ++i)
412  {
413  mitem = mbar->findItem(mbar->idAt(i));
414  if (!mitem)
415  continue;
416 
417  // nothing to do for separators
418  if (mitem->isSeparator())
419  continue;
420 
421  s = mitem->text();
422  if (!s.isEmpty())
423  {
424  Item *it = new Item;
425  item->addChild(it);
426  it->m_content =
427  KAccelString(s,
428  // menu titles are important, so raise the weight
429  KAccelManagerAlgorithm::MENU_TITLE_WEIGHT);
430 
431  it->m_widget = mbar;
432  it->m_index = i;
433  }
434 
435  // have a look at the popup as well, if present
436  if (mitem->popup())
437  KPopupAccelManager::manage(mitem->popup());
438  }
439 }
440 
441 
442 /*********************************************************************
443 
444  class KAcceleratorManager - main entry point
445 
446  This class is just here to provide a clean public API...
447 
448  *********************************************************************/
449 
450 void KAcceleratorManager::manage(TQWidget *widget)
451 {
452  KAcceleratorManager::manage(widget, false);
453 }
454 
455 void KAcceleratorManager::manage(TQWidget *widget, bool programmers_mode)
456 {
457  kdDebug(131) << "KAcceleratorManager::manage\n";
458  KAcceleratorManagerPrivate::changed_string = TQString::null;
459  KAcceleratorManagerPrivate::added_string = TQString::null;
460  KAcceleratorManagerPrivate::removed_string = TQString::null;
461  KAcceleratorManagerPrivate::programmers_mode = programmers_mode;
462  KAcceleratorManagerPrivate::manage(widget);
463 }
464 
465 void KAcceleratorManager::last_manage(TQString &added, TQString &changed, TQString &removed)
466 {
467  added = KAcceleratorManagerPrivate::added_string;
468  changed = KAcceleratorManagerPrivate::changed_string;
469  removed = KAcceleratorManagerPrivate::removed_string;
470 }
471 
472 
473 /*********************************************************************
474 
475  class KAccelString - a string with weighted characters
476 
477  *********************************************************************/
478 
479 KAccelString::KAccelString(const TQString &input, int initialWeight)
480  : m_pureText(input), m_weight()
481 {
482  m_orig_accel = m_pureText.find("(!)&");
483  if (m_orig_accel != -1)
484  m_pureText.remove(m_orig_accel, 4);
485 
486  m_orig_accel = m_pureText.find("(&&)");
487  if (m_orig_accel != -1)
488  m_pureText.replace(m_orig_accel, 4, "&");
489 
490  m_origText = m_pureText;
491 
492  if (m_pureText.contains('\t'))
493  m_pureText = m_pureText.left(m_pureText.find('\t'));
494 
495  m_orig_accel = m_accel = stripAccelerator(m_pureText);
496 
497  if (initialWeight == -1)
498  initialWeight = KAccelManagerAlgorithm::DEFAULT_WEIGHT;
499 
500  calculateWeights(initialWeight);
501 
502  // dump();
503 }
504 
505 
506 TQString KAccelString::accelerated() const
507 {
508  TQString result = m_origText;
509  if (result.isEmpty())
510  return result;
511 
512  if (KAcceleratorManagerPrivate::programmers_mode)
513  {
514  if (m_accel != m_orig_accel) {
515  int oa = m_orig_accel;
516 
517  if (m_accel >= 0) {
518  result.insert(m_accel, "(!)&");
519  if (m_accel < m_orig_accel)
520  oa += 4;
521  }
522  if (m_orig_accel >= 0)
523  result.replace(oa, 1, "(&&)");
524  }
525  } else {
526  if (m_accel >= 0 && m_orig_accel != m_accel) {
527  result.remove(m_orig_accel, 1);
528  result.insert(m_accel, "&");
529  }
530  }
531  return result;
532 }
533 
534 
535 TQChar KAccelString::accelerator() const
536 {
537  if ((m_accel < 0) || (m_accel > (int)m_pureText.length()))
538  return TQChar();
539 
540  return m_pureText[m_accel].lower();
541 }
542 
543 
544 void KAccelString::calculateWeights(int initialWeight)
545 {
546  m_weight.resize(m_pureText.length());
547 
548  uint pos = 0;
549  bool start_character = true;
550 
551  while (pos<m_pureText.length())
552  {
553  TQChar c = m_pureText[pos];
554 
555  int weight = initialWeight+1;
556 
557  // add special weight to first character
558  if (pos == 0)
559  weight += KAccelManagerAlgorithm::FIRST_CHARACTER_EXTRA_WEIGHT;
560 
561  // add weight to word beginnings
562  if (start_character)
563  {
564  weight += KAccelManagerAlgorithm::WORD_BEGINNING_EXTRA_WEIGHT;
565  start_character = false;
566  }
567 
568  // add decreasing weight to left characters
569  if (pos < 50)
570  weight += (50-pos);
571 
572  // try to preserve the wanted accelarators
573  if ((int)pos == accel()) {
574  weight += KAccelManagerAlgorithm::WANTED_ACCEL_EXTRA_WEIGHT;
575  // kdDebug(131) << "wanted " << m_pureText << " " << KAcceleratorManagerPrivate::standardName(m_origText) << endl;
576  if (KAcceleratorManagerPrivate::standardName(m_origText)) {
577  weight += KAccelManagerAlgorithm::STANDARD_ACCEL;
578  }
579  }
580 
581  // skip non typeable characters
582  if (!c.isLetterOrNumber())
583  {
584  weight = 0;
585  start_character = true;
586  }
587 
588  m_weight[pos] = weight;
589 
590  ++pos;
591  }
592 }
593 
594 
595 int KAccelString::stripAccelerator(TQString &text)
596 {
597  // Note: this code is derived from TQAccel::shortcutKey
598  int p = 0;
599 
600  while (p >= 0)
601  {
602  p = text.find('&', p)+1;
603 
604  if (p <= 0 || p >= (int)text.length())
605  return -1;
606 
607  if (text[p] != '&')
608  {
609  TQChar c = text[p];
610  if (c.isPrint())
611  {
612  text.remove(p-1,1);
613  return p-1;
614  }
615  }
616 
617  p++;
618  }
619 
620  return -1;
621 }
622 
623 
624 int KAccelString::maxWeight(int &index, const TQString &used)
625 {
626  int max = 0;
627  index = -1;
628 
629  for (uint pos=0; pos<m_pureText.length(); ++pos)
630  if (used.find(m_pureText[pos], 0, FALSE) == -1 && m_pureText[pos].latin1() != 0)
631  if (m_weight[pos] > max)
632  {
633  max = m_weight[pos];
634  index = pos;
635  }
636 
637  return max;
638 }
639 
640 
641 void KAccelString::dump()
642 {
643  TQString s;
644  for (uint i=0; i<m_weight.count(); ++i)
645  s += TQString("%1(%2) ").arg(pure()[i]).arg(m_weight[i]);
646  kdDebug() << "s " << s << endl;
647 }
648 
649 
650 /*********************************************************************
651 
652  findAccelerators - the algorithm determining the new accelerators
653 
654  The algorithm is very crude:
655 
656  * each character in each widget text is assigned a weight
657  * the character with the highest weight over all is picked
658  * that widget is removed from the list
659  * the weights are recalculated
660  * the process is repeated until no more accelerators can be found
661 
662  The algorithm has some advantages:
663 
664  * it favors 'nice' accelerators (first characters in a word, etc.)
665  * it is quite fast, O(N�)
666  * it is easy to understand :-)
667 
668  The disadvantages:
669 
670  * it does not try to find as many accelerators as possible
671 
672  TODO:
673 
674  * The result is always correct, but not neccesarily optimal. Perhaps
675  it would be a good idea to add another algorithm with higher complexity
676  that gets used when this one fails, i.e. leaves widgets without
677  accelerators.
678 
679  * The weights probably need some tweaking so they make more sense.
680 
681  *********************************************************************/
682 
683 void KAccelManagerAlgorithm::findAccelerators(KAccelStringList &result, TQString &used)
684 {
685  kdDebug(131) << "findAccelerators\n";
686  KAccelStringList accel_strings = result;
687 
688  // initally remove all accelerators
689  for (KAccelStringList::Iterator it = result.begin(); it != result.end(); ++it) {
690  (*it).setAccel(-1);
691  }
692 
693  // pick the highest bids
694  for (uint cnt=0; cnt<accel_strings.count(); ++cnt)
695  {
696  int max = 0, index = -1, accel = -1;
697 
698  // find maximum weight
699  for (uint i=0; i<accel_strings.count(); ++i)
700  {
701  int a;
702  int m = accel_strings[i].maxWeight(a, used);
703  if (m>max)
704  {
705  max = m;
706  index = i;
707  accel = a;
708  }
709  }
710 
711  // stop if no more accelerators can be found
712  if (index < 0)
713  return;
714 
715  // insert the accelerator
716  if (accel >= 0)
717  {
718  result[index].setAccel(accel);
719  used.append(result[index].accelerator());
720  }
721 
722  // make sure we don't visit this one again
723  accel_strings[index] = KAccelString();
724  }
725 }
726 
727 
728 /*********************************************************************
729 
730  class KPopupAccelManager - managing TQPopupMenu widgets dynamically
731 
732  *********************************************************************/
733 
734 KPopupAccelManager::KPopupAccelManager(TQPopupMenu *popup)
735  : TQObject(popup), m_popup(popup), m_count(-1)
736 {
737  aboutToShow(); // do one check and then connect to show
738  connect(popup, TQT_SIGNAL(aboutToShow()), TQT_SLOT(aboutToShow()));
739 }
740 
741 
742 void KPopupAccelManager::aboutToShow()
743 {
744  // Note: we try to be smart and avoid recalculating the accelerators
745  // whenever possible. Unfortunately, there is no way to know if an
746  // item has been added or removed, so we can not do much more than
747  // to compare the items each time the menu is shown :-(
748 
749  if (m_count != (int)m_popup->count())
750  {
751  findMenuEntries(m_entries);
752  calculateAccelerators();
753  m_count = m_popup->count();
754  }
755  else
756  {
757  KAccelStringList entries;
758  findMenuEntries(entries);
759  if (entries != m_entries)
760  {
761  m_entries = entries;
762  calculateAccelerators();
763  }
764  }
765 }
766 
767 
768 void KPopupAccelManager::calculateAccelerators()
769 {
770  // find the new accelerators
771  TQString used;
772  KAccelManagerAlgorithm::findAccelerators(m_entries, used);
773 
774  // change the menu entries
775  setMenuEntries(m_entries);
776 }
777 
778 
779 void KPopupAccelManager::findMenuEntries(KAccelStringList &list)
780 {
781  TQMenuItem *mitem;
782  TQString s;
783 
784  list.clear();
785 
786  // read out the menu entries
787  for (uint i=0; i<m_popup->count(); i++)
788  {
789  mitem = m_popup->findItem(m_popup->idAt(i));
790  if (mitem->isSeparator())
791  continue;
792 
793  s = mitem->text();
794 
795  // in full menus, look at entries with global accelerators last
796  int weight = 50;
797  if (s.contains('\t'))
798  weight = 0;
799 
800  list.append(KAccelString(s, weight));
801 
802  // have a look at the popup as well, if present
803  if (mitem->popup())
804  KPopupAccelManager::manage(mitem->popup());
805  }
806 }
807 
808 
809 void KPopupAccelManager::setMenuEntries(const KAccelStringList &list)
810 {
811  TQMenuItem *mitem;
812 
813  uint cnt = 0;
814  for (uint i=0; i<m_popup->count(); i++)
815  {
816  mitem = m_popup->findItem(m_popup->idAt(i));
817  if (mitem->isSeparator())
818  continue;
819 
820  if (KAcceleratorManagerPrivate::checkChange(list[cnt]))
821  mitem->setText(list[cnt].accelerated());
822  cnt++;
823  }
824 }
825 
826 
827 void KPopupAccelManager::manage(TQPopupMenu *popup)
828 {
829  // don't add more than one manager to a popup
830  if (popup->child(0, "KPopupAccelManager", false) == 0 )
831  new KPopupAccelManager(popup);
832 }
833 
834 void QWidgetStackAccelManager::manage( TQWidgetStack *stack )
835 {
836  if ( stack->child( 0, "QWidgetStackAccelManager", false ) == 0 )
837  new QWidgetStackAccelManager( stack );
838 }
839 
840 QWidgetStackAccelManager::QWidgetStackAccelManager(TQWidgetStack *stack)
841  : TQObject(stack), m_stack(stack)
842 {
843  aboutToShow(stack->visibleWidget()); // do one check and then connect to show
844  connect(stack, TQT_SIGNAL(aboutToShow(TQWidget *)), TQT_SLOT(aboutToShow(TQWidget *)));
845 }
846 
847 bool QWidgetStackAccelManager::eventFilter ( TQObject * watched, TQEvent * e )
848 {
849  if ( e->type() == TQEvent::Show && tqApp->activeWindow() ) {
850  KAcceleratorManager::manage( TQT_TQWIDGET(tqApp->activeWindow()) );
851  watched->removeEventFilter( this );
852  }
853  return false;
854 }
855 
856 void QWidgetStackAccelManager::aboutToShow(TQWidget *child)
857 {
858  if (!child)
859  {
860  kdDebug(131) << "null pointer given to aboutToShow" << endl;
861  return;
862  }
863 
864  child->installEventFilter( this );
865 }
866 
867 void KAcceleratorManager::setNoAccel( TQWidget *widget )
868 {
869  KAcceleratorManagerPrivate::ignored_widgets[widget] = 1;
870 }
871 
872 #include "kaccelmanager_private.moc"

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. |