20 #include "kaccelmanager.h"
22 #include <tqapplication.h>
23 #include <tqcheckbox.h>
24 #include <tqcombobox.h>
25 #include <tqgroupbox.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>
39 #include <tqtextview.h>
41 #include <tqwidgetstack.h>
43 #include <kstdaction.h>
44 #include <kstaticdeleter.h>
48 #include "kaccelmanager_private.h"
49 #include "../kdeui/kstdaction_p.h"
50 #include "../kutils/kmultitabbar.h"
74 class KAcceleratorManagerPrivate
78 static void manage(TQWidget *widget);
79 static bool programmers_mode;
80 static bool standardName(
const TQString &str);
83 TQString t2 = as.accelerated();
84 TQString t1 = as.originalText();
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>";
92 changed_string +=
"<tr><td>" + TQStyleSheet::escape(t1) +
"</td>";
93 changed_string +=
"<td>" + TQStyleSheet::escape(t2) +
"</td></tr>";
99 static TQString changed_string;
100 static TQString added_string;
101 static TQString removed_string;
102 static TQMap<TQWidget *, int> ignored_widgets;
107 typedef TQPtrList<Item> ItemList;
110 static void traverseChildren(TQWidget *widget, Item *item);
112 static void manageWidget(TQWidget *widget, Item *item);
113 static void manageMenuBar(TQMenuBar *mbar, Item *item);
114 static void manageTabBar(TQTabBar *bar, Item *item);
116 static void calculateAccelerators(Item *item, TQString &used);
122 Item() : m_widget(0), m_children(0), m_index(-1) {}
125 void addChild(Item *item);
129 ItemList *m_children;
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;
142 TQMap<TQWidget*, int> KAcceleratorManagerPrivate::ignored_widgets;
144 bool KAcceleratorManagerPrivate::standardName(
const TQString &str)
147 kaccmp_sns_d.
setObject(kaccmp_sns,
new TQStringList(KStdAction::internal_stdNames()));
148 return kaccmp_sns->contains(str);
151 KAcceleratorManagerPrivate::Item::~Item()
157 void KAcceleratorManagerPrivate::Item::addChild(Item *item)
160 m_children =
new ItemList;
161 m_children->setAutoDelete(
true);
164 m_children->append(item);
167 void KAcceleratorManagerPrivate::manage(TQWidget *widget)
171 kdDebug(131) <<
"null pointer given to manage" <<
endl;
175 if (dynamic_cast<TQPopupMenu*>(widget))
178 KPopupAccelManager::manage(static_cast<TQPopupMenu*>(widget));
182 Item *root =
new Item;
184 manageWidget(widget, root);
187 calculateAccelerators(root, used);
192 void KAcceleratorManagerPrivate::calculateAccelerators(Item *item, TQString &used)
194 if (!item->m_children)
198 KAccelStringList contents;
199 for (Item *it = item->m_children->first(); it != 0;
200 it = item->m_children->next())
202 contents << it->m_content;
210 for (Item *it = item->m_children->first(); it != 0;
211 it = item->m_children->next())
215 TQTabBar *tabBar =
dynamic_cast<TQTabBar*
>(it->m_widget);
218 if (checkChange(contents[cnt]))
219 tabBar->tabAt(it->m_index)->setText(contents[cnt].accelerated());
222 TQMenuBar *menuBar =
dynamic_cast<TQMenuBar*
>(it->m_widget);
225 if (it->m_index >= 0)
227 TQMenuItem *mitem = menuBar->findItem(menuBar->idAt(it->m_index));
230 checkChange(contents[cnt]);
231 mitem->setText(contents[cnt].accelerated());
237 if ( dynamic_cast<TQGroupBox*>( it->m_widget ) )
240 if ( dynamic_cast<TQLabel*>( it->m_widget ) && it->m_widget->inherits(
"KURLLabel") )
243 int tprop = it->m_widget->metaObject()->findProperty(
"text",
true);
245 if (checkChange(contents[cnt]))
246 it->m_widget->setProperty(
"text", contents[cnt].accelerated());
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());
255 for (Item *it = item->m_children->first(); it != 0;
256 it = item->m_children->next())
258 if (it->m_widget && it->m_widget->isVisibleTo( item->m_widget ) )
259 calculateAccelerators(it, used);
264 void KAcceleratorManagerPrivate::traverseChildren(TQWidget *widget, Item *item)
266 TQObjectList *childList = widget->queryList(TQWIDGET_OBJECT_NAME_STRING, 0,
false,
false);
267 for ( TQObject *it = childList->first(); it; it = childList->next() )
269 TQWidget *w = TQT_TQWIDGET(it);
271 if ( !w->isVisibleTo( widget ) || ( w->isTopLevel() &&
dynamic_cast<TQPopupMenu*
>(w) == NULL ) )
274 if ( KAcceleratorManagerPrivate::ignored_widgets.
find( w ) != KAcceleratorManagerPrivate::ignored_widgets.end() )
277 manageWidget(w, item);
282 void KAcceleratorManagerPrivate::manageWidget(TQWidget *w, Item *item)
286 TQTabBar *tabBar =
dynamic_cast<TQTabBar*
>(w);
289 manageTabBar(tabBar, item);
293 TQWidgetStack *wds =
dynamic_cast<TQWidgetStack*
>( w );
296 QWidgetStackAccelManager::manage( wds );
300 TQPopupMenu *popupMenu =
dynamic_cast<TQPopupMenu*
>(w);
304 KPopupAccelManager::manage(popupMenu);
308 TQWidgetStack *wdst =
dynamic_cast<TQWidgetStack*
>( w );
311 QWidgetStackAccelManager::manage( wdst );
315 TQMenuBar *menuBar =
dynamic_cast<TQMenuBar*
>(w);
318 manageMenuBar(menuBar, item);
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")))
328 TQLabel *
label =
dynamic_cast<TQLabel*
>(w);
330 if ( !label->buddy() )
333 if ( label->textFormat() == Qt::RichText ||
334 ( label->textFormat() == Qt::AutoText &&
335 TQStyleSheet::mightBeRichText( label->text() ) ) )
340 if (w->isFocusEnabled() || label ||
dynamic_cast<TQGroupBox*
>(w) || dynamic_cast<TQRadioButton*>( w ))
344 int tprop = w->metaObject()->findProperty(
"text",
true);
346 const TQMetaProperty* p = w->metaObject()->property( tprop,
true );
347 if ( p && p->isValid() )
348 w->qt_property( tprop, 1, &variant );
354 tprop = w->metaObject()->findProperty(
"title",
true);
356 const TQMetaProperty* p = w->metaObject()->property( tprop,
true );
357 if ( p && p->isValid() )
358 w->qt_property( tprop, 1, &variant );
362 if (variant.isValid())
363 content = variant.toString();
365 if (!content.isEmpty())
372 if (dynamic_cast<TQPushButton*>(w) ||
dynamic_cast<TQCheckBox*
>(w) || dynamic_cast<TQRadioButton*>(w) ||
dynamic_cast<TQLabel*
>(w))
376 if (dynamic_cast<TQGroupBox*>(w))
380 if (w->inherits(
"KDialogBaseButton"))
387 traverseChildren(w, item);
390 void KAcceleratorManagerPrivate::manageTabBar(TQTabBar *bar, Item *item)
392 for (
int i=0; i<bar->count(); i++)
394 TQString content = bar->tabAt(i)->text();
395 if (content.isEmpty())
406 void KAcceleratorManagerPrivate::manageMenuBar(TQMenuBar *mbar, Item *item)
411 for (uint i=0; i<mbar->count(); ++i)
413 mitem = mbar->findItem(mbar->idAt(i));
418 if (mitem->isSeparator())
437 KPopupAccelManager::manage(mitem->popup());
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);
465 void KAcceleratorManager::last_manage(TQString &added, TQString &changed, TQString &removed)
467 added = KAcceleratorManagerPrivate::added_string;
468 changed = KAcceleratorManagerPrivate::changed_string;
469 removed = KAcceleratorManagerPrivate::removed_string;
479 KAccelString::KAccelString(
const TQString &input,
int initialWeight)
480 : m_pureText(input), m_weight()
482 m_orig_accel = m_pureText.find(
"(!)&");
483 if (m_orig_accel != -1)
484 m_pureText.remove(m_orig_accel, 4);
486 m_orig_accel = m_pureText.find(
"(&&)");
487 if (m_orig_accel != -1)
488 m_pureText.replace(m_orig_accel, 4,
"&");
490 m_origText = m_pureText;
492 if (m_pureText.contains(
'\t'))
493 m_pureText = m_pureText.left(m_pureText.find(
'\t'));
495 m_orig_accel = m_accel = stripAccelerator(m_pureText);
497 if (initialWeight == -1)
500 calculateWeights(initialWeight);
506 TQString KAccelString::accelerated()
const
508 TQString result = m_origText;
509 if (result.isEmpty())
512 if (KAcceleratorManagerPrivate::programmers_mode)
514 if (m_accel != m_orig_accel) {
515 int oa = m_orig_accel;
518 result.insert(m_accel,
"(!)&");
519 if (m_accel < m_orig_accel)
522 if (m_orig_accel >= 0)
523 result.replace(oa, 1,
"(&&)");
526 if (m_accel >= 0 && m_orig_accel != m_accel) {
527 result.remove(m_orig_accel, 1);
528 result.insert(m_accel,
"&");
535 TQChar KAccelString::accelerator()
const
537 if ((m_accel < 0) || (m_accel > (
int)m_pureText.length()))
540 return m_pureText[m_accel].lower();
544 void KAccelString::calculateWeights(
int initialWeight)
546 m_weight.resize(m_pureText.length());
549 bool start_character =
true;
551 while (pos<m_pureText.length())
553 TQChar c = m_pureText[pos];
555 int weight = initialWeight+1;
565 start_character =
false;
573 if ((
int)pos == accel()) {
576 if (KAcceleratorManagerPrivate::standardName(m_origText)) {
582 if (!c.isLetterOrNumber())
585 start_character =
true;
588 m_weight[pos] = weight;
595 int KAccelString::stripAccelerator(TQString &text)
602 p = text.find(
'&', p)+1;
604 if (p <= 0 || p >= (
int)text.length())
624 int KAccelString::maxWeight(
int &index,
const TQString &used)
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)
641 void KAccelString::dump()
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;
685 kdDebug(131) <<
"findAccelerators\n";
686 KAccelStringList accel_strings = result;
689 for (KAccelStringList::Iterator it = result.begin(); it != result.end(); ++it) {
694 for (uint cnt=0; cnt<accel_strings.count(); ++cnt)
696 int max = 0, index = -1, accel = -1;
699 for (uint i=0; i<accel_strings.count(); ++i)
702 int m = accel_strings[i].maxWeight(a, used);
718 result[index].setAccel(accel);
719 used.append(result[index].accelerator());
734 KPopupAccelManager::KPopupAccelManager(TQPopupMenu *popup)
735 : TQObject(popup), m_popup(popup), m_count(-1)
738 connect(popup, TQT_SIGNAL(aboutToShow()), TQT_SLOT(aboutToShow()));
742 void KPopupAccelManager::aboutToShow()
749 if (m_count != (
int)m_popup->count())
751 findMenuEntries(m_entries);
752 calculateAccelerators();
753 m_count = m_popup->count();
757 KAccelStringList entries;
758 findMenuEntries(entries);
759 if (entries != m_entries)
762 calculateAccelerators();
768 void KPopupAccelManager::calculateAccelerators()
775 setMenuEntries(m_entries);
779 void KPopupAccelManager::findMenuEntries(KAccelStringList &list)
787 for (uint i=0; i<m_popup->count(); i++)
789 mitem = m_popup->findItem(m_popup->idAt(i));
790 if (mitem->isSeparator())
797 if (s.contains(
'\t'))
804 KPopupAccelManager::manage(mitem->popup());
809 void KPopupAccelManager::setMenuEntries(
const KAccelStringList &list)
814 for (uint i=0; i<m_popup->count(); i++)
816 mitem = m_popup->findItem(m_popup->idAt(i));
817 if (mitem->isSeparator())
820 if (KAcceleratorManagerPrivate::checkChange(list[cnt]))
821 mitem->setText(list[cnt].accelerated());
827 void KPopupAccelManager::manage(TQPopupMenu *popup)
830 if (popup->child(0,
"KPopupAccelManager",
false) == 0 )
834 void QWidgetStackAccelManager::manage( TQWidgetStack *stack )
836 if ( stack->child( 0,
"QWidgetStackAccelManager",
false ) == 0 )
837 new QWidgetStackAccelManager( stack );
840 QWidgetStackAccelManager::QWidgetStackAccelManager(TQWidgetStack *stack)
841 : TQObject(stack), m_stack(stack)
843 aboutToShow(stack->visibleWidget());
844 connect(stack, TQT_SIGNAL(aboutToShow(TQWidget *)), TQT_SLOT(aboutToShow(TQWidget *)));
847 bool QWidgetStackAccelManager::eventFilter ( TQObject * watched, TQEvent * e )
849 if ( e->type() == TQEvent::Show && tqApp->activeWindow() ) {
851 watched->removeEventFilter(
this );
856 void QWidgetStackAccelManager::aboutToShow(TQWidget *child)
860 kdDebug(131) <<
"null pointer given to aboutToShow" <<
endl;
864 child->installEventFilter(
this );
869 KAcceleratorManagerPrivate::ignored_widgets[widget] = 1;
872 #include "kaccelmanager_private.moc"