22 #include <QtGui/QApplication>
23 #include <QtGui/QMainWindow>
24 #include <QtGui/QCheckBox>
25 #include <QtGui/QComboBox>
26 #include <QtGui/QGroupBox>
27 #include <QtGui/QLabel>
28 #include <QtGui/QLineEdit>
29 #include <QtGui/QMenuBar>
30 #include <QtGui/qmenudata.h>
31 #include <QtCore/QMetaClassInfo>
32 #include <QtCore/QObject>
34 #include <QtGui/QPushButton>
35 #include <QtGui/QRadioButton>
36 #include <QtGui/QDoubleSpinBox>
37 #include <QtGui/QTabBar>
38 #include <QtGui/QTextEdit>
39 #include <QtGui/QWidget>
40 #include <QStackedWidget>
41 #include <QDockWidget>
42 #include <QTextDocument>
46 #include <kdeversion.h>
50 #include <kstandardaction_p.h>
74 class KAcceleratorManagerPrivate
78 static void manage(
QWidget *widget);
79 static bool programmers_mode;
80 static bool standardName(
const QString &str);
87 if (as.
accel() == -1) {
88 removed_string +=
"<tr><td>" + Qt::escape(t1) +
"</td></tr>";
90 added_string +=
"<tr><td>" + Qt::escape(t2) +
"</td></tr>";
92 changed_string +=
"<tr><td>" + Qt::escape(t1) +
"</td>";
93 changed_string +=
"<td>" + Qt::escape(t2) +
"</td></tr>";
110 static void traverseChildren(
QWidget *widget, Item *item);
112 static void manageWidget(
QWidget *widget, Item *item);
113 static void manageMenuBar(
QMenuBar *mbar, Item *item);
114 static void manageTabBar(
QTabBar *bar, Item *item);
115 static void manageDockWidget(QDockWidget *dock, Item *item);
117 static void calculateAccelerators(Item *item,
QString &used);
123 Item() : m_widget(0), m_children(0), m_index(-1) {}
126 void addChild(Item *item);
130 ItemList *m_children;
137 bool KAcceleratorManagerPrivate::programmers_mode =
false;
138 QString KAcceleratorManagerPrivate::changed_string;
139 QString KAcceleratorManagerPrivate::added_string;
140 QString KAcceleratorManagerPrivate::removed_string;
142 QMap<
QWidget*,
int> KAcceleratorManagerPrivate::ignored_widgets;
144 bool KAcceleratorManagerPrivate::standardName(const
QString &str)
146 return kaccmp_sns->contains(str);
149 KAcceleratorManagerPrivate::Item::~Item()
152 while (!m_children->isEmpty())
153 delete m_children->takeFirst();
159 void KAcceleratorManagerPrivate::Item::addChild(Item *item)
162 m_children =
new ItemList;
165 m_children->append(item);
168 void KAcceleratorManagerPrivate::manage(QWidget *widget)
172 kDebug(240) <<
"null pointer given to manage";
176 if (KAcceleratorManagerPrivate::ignored_widgets.contains(widget)) {
180 if (qobject_cast<QMenu*>(widget))
189 manageWidget(widget, root);
192 calculateAccelerators(root, used);
197 void KAcceleratorManagerPrivate::calculateAccelerators(Item *item,
QString &used)
199 if (!item->m_children)
204 foreach(Item *it, *item->m_children)
206 contents << it->m_content;
214 foreach(Item *it, *item->m_children)
218 QDockWidget *dock = qobject_cast<QDockWidget*>(it->m_widget);
221 if (checkChange(contents[cnt]))
222 dock->setWindowTitle(contents[cnt].accelerated());
228 if (checkChange(contents[cnt]))
229 tabBar->setTabText(it->m_index, contents[cnt].accelerated());
235 if (it->m_index >= 0)
237 QAction *maction = menuBar->actions()[it->m_index];
240 checkChange(contents[cnt]);
241 maction->setText(contents[cnt].accelerated());
249 if (groupBox && !groupBox->isCheckable())
252 int tprop = it->m_widget->metaObject()->indexOfProperty(
"text");
254 if (checkChange(contents[cnt]))
255 it->m_widget->setProperty(
"text", contents[cnt].accelerated());
257 tprop = it->m_widget->metaObject()->indexOfProperty(
"title");
258 if (tprop != -1 && checkChange(contents[cnt]))
259 it->m_widget->setProperty(
"title", contents[cnt].accelerated());
264 foreach(Item *it, *item->m_children)
266 if (it->m_widget && it->m_widget->isVisibleTo( item->m_widget ) )
267 calculateAccelerators(it, used);
272 void KAcceleratorManagerPrivate::traverseChildren(QWidget *widget, Item *item)
275 foreach ( QWidget *w , childList ) {
277 if(qobject_cast<QWidget *>(w->parent()) != widget)
continue;
279 if ( !w->isVisibleTo( widget ) || (w->isTopLevel() && qobject_cast<
QMenu*>(w) == NULL) )
282 if ( KAcceleratorManagerPrivate::ignored_widgets.contains( w ) )
285 manageWidget(w, item);
289 void KAcceleratorManagerPrivate::manageWidget(QWidget *w, Item *item)
296 manageTabBar(tabBar, item);
300 QStackedWidget *wds = qobject_cast<QStackedWidget*>( w );
307 QDockWidget *dock = qobject_cast<QDockWidget*>( w );
311 manageDockWidget(dock, item);
323 QStackedWidget *wdst = qobject_cast<QStackedWidget*>( w );
333 manageMenuBar(menuBar, item);
337 if (qobject_cast<QComboBox*>(w) || qobject_cast<QLineEdit*>(w) ||
338 w->inherits(
"Q3TextEdit") ||
340 qobject_cast<QAbstractSpinBox*>(w) || w->inherits(
"KMultiTabBar" ) )
343 if ( w->inherits(
"KUrlRequester") ) {
344 traverseChildren(w, item);
351 if ( !label->buddy() )
354 if ( label->textFormat() == Qt::RichText ||
355 ( label->textFormat() == Qt::AutoText &&
356 Qt::mightBeRichText( label->text() ) ) )
361 if (w->focusPolicy() != Qt::NoFocus || label || qobject_cast<
QGroupBox*>(w) || qobject_cast<QRadioButton*>( w ))
365 int tprop = w->metaObject()->indexOfProperty(
"text");
367 QMetaProperty p = w->metaObject()->property( tprop );
368 if ( p.isValid() && p.isWritable() )
369 variant = p.read (w);
375 tprop = w->metaObject()->indexOfProperty(
"title");
377 QMetaProperty p = w->metaObject()->property( tprop );
378 if ( p.isValid() && p.isWritable() )
379 variant = p.read (w);
383 if (variant.isValid())
384 content = variant.toString();
386 if (!content.isEmpty())
393 if (qobject_cast<QPushButton*>(w) || qobject_cast<QCheckBox*>(w) || qobject_cast<QRadioButton*>(w) || qobject_cast<
QLabel*>(w))
401 if (groupBox->isCheckable())
411 traverseChildren(w, item);
414 void KAcceleratorManagerPrivate::manageTabBar(
QTabBar *bar, Item *item)
419 QWidget* parentWidget = bar->parentWidget();
428 for (
int i=0; i<bar->count(); i++)
430 QString content = bar->tabText(i);
431 if (content.isEmpty())
442 void KAcceleratorManagerPrivate::manageDockWidget(QDockWidget *dock, Item *item)
454 QString content = dock->windowTitle();
455 if (content.isEmpty())
465 void KAcceleratorManagerPrivate::manageMenuBar(
QMenuBar *mbar, Item *item)
470 for (
int i=0; i<mbar->actions().count(); ++i)
472 maction = mbar->actions()[i];
477 if (maction->isSeparator())
511 KAcceleratorManagerPrivate::changed_string.clear();
512 KAcceleratorManagerPrivate::added_string.clear();
513 KAcceleratorManagerPrivate::removed_string.clear();
514 KAcceleratorManagerPrivate::programmers_mode = programmers_mode;
515 KAcceleratorManagerPrivate::manage(widget);
520 added = KAcceleratorManagerPrivate::added_string;
521 changed = KAcceleratorManagerPrivate::changed_string;
522 removed = KAcceleratorManagerPrivate::removed_string;
533 : m_pureText(input), m_weight()
535 m_orig_accel = m_pureText.indexOf(
"(!)&");
536 if (m_orig_accel != -1)
537 m_pureText.remove(m_orig_accel, 4);
539 m_orig_accel = m_pureText.indexOf(
"(&&)");
540 if (m_orig_accel != -1)
541 m_pureText.replace(m_orig_accel, 4,
"&");
543 m_origText = m_pureText;
545 if (m_pureText.contains(
'\t'))
546 m_pureText = m_pureText.left(m_pureText.indexOf(
'\t'));
548 m_orig_accel = m_accel = stripAccelerator(m_pureText);
550 if (initialWeight == -1)
562 if (result.isEmpty())
565 if (KAcceleratorManagerPrivate::programmers_mode)
567 if (m_accel != m_orig_accel) {
568 int oa = m_orig_accel;
571 result.insert(m_accel,
"(!)&");
572 if (m_accel < m_orig_accel)
575 if (m_orig_accel >= 0)
576 result.replace(oa, 1,
"(&&)");
579 if (m_accel >= 0 && m_orig_accel != m_accel) {
580 if (m_orig_accel != -1)
581 result.remove(m_orig_accel, 1);
582 result.insert(m_accel,
"&");
591 if ((m_accel < 0) || (m_accel > (
int)m_pureText.length()))
594 return m_pureText[m_accel].toLower();
600 m_weight.resize(m_pureText.length());
603 bool start_character =
true;
605 while (pos<m_pureText.length())
607 QChar c = m_pureText[pos];
609 int weight = initialWeight+1;
619 start_character =
false;
627 if ((
int)pos ==
accel()) {
630 if (KAcceleratorManagerPrivate::standardName(m_origText)) {
636 if (!c.isLetterOrNumber())
639 start_character =
true;
642 m_weight[pos] = weight;
649 int KAccelString::stripAccelerator(
QString &text)
656 p = text.indexOf(
'&', p)+1;
658 if (p <= 0 || p >= (
int)text.length())
683 for (
int pos=0; pos<m_pureText.length(); ++pos)
684 if (used.indexOf(m_pureText[pos], 0, Qt::CaseInsensitive) == -1 && m_pureText[pos].toLatin1() != 0)
685 if (m_weight[pos] > max)
695 void KAccelString::dump()
698 for (
int i=0; i<m_weight.count(); ++i)
699 s +=
QString(
"%1(%2) ").arg(
pure()[i]).arg(m_weight[i]);
742 for (KAccelStringList::Iterator it = result.begin(); it != result.end(); ++it) {
747 for (
int cnt=0; cnt<accel_strings.count(); ++cnt)
749 int max = 0, index = -1, accel = -1;
752 for (
int i=0; i<accel_strings.count(); ++i)
755 int m = accel_strings[i].maxWeight(a, used);
771 result[index].setAccel(accel);
772 used.append(result[index].accelerator());
788 :
QObject(popup), m_popup(popup), m_count(-1)
791 connect(popup, SIGNAL(aboutToShow()), SLOT(aboutToShow()));
795 void KPopupAccelManager::aboutToShow()
802 if (m_count != (
int)m_popup->actions().count())
804 findMenuEntries(m_entries);
805 calculateAccelerators();
806 m_count = m_popup->actions().count();
811 findMenuEntries(entries);
812 if (entries != m_entries)
815 calculateAccelerators();
821 void KPopupAccelManager::calculateAccelerators()
828 setMenuEntries(m_entries);
839 foreach (
QAction *maction, m_popup->actions())
841 if (maction->isSeparator())
848 if (s.contains(
'\t'))
863 foreach (
QAction *maction, m_popup->actions())
865 if (maction->isSeparator())
868 if (KAcceleratorManagerPrivate::checkChange(list[cnt]))
869 maction->setText(list[cnt].accelerated());
889 :
QObject(stack), m_stack(stack)
891 currentChanged(stack->currentIndex());
892 connect(stack, SIGNAL(currentChanged(
int)), SLOT(currentChanged(
int)));
895 bool QWidgetStackAccelManager::eventFilter (
QObject * watched, QEvent * e )
897 if ( e->type() == QEvent::Show && qApp->activeWindow() ) {
899 watched->removeEventFilter(
this );
904 void QWidgetStackAccelManager::currentChanged(
int child)
906 if (child < 0 || child >= static_cast<QStackedWidget*>(parent())->count())
912 static_cast<QStackedWidget*
>(parent())->widget(child)->installEventFilter(
this );
917 KAcceleratorManagerPrivate::ignored_widgets[widget] = 1;
920 #include "kacceleratormanager_private.moc"
const QString & pure() const
A string class handling accelerators.
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
QString accelerated() const
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
static void last_manage(QString &added, QString &changed, QString &removed)
static void setNoAccel(QWidget *widget)
Use this method for a widget (and its children) you want no accels to be set on.
QString originalText() const
K_GLOBAL_STATIC_WITH_ARGS(QStringList, kaccmp_sns,(KStandardAction::internal_stdNames())) QMap< QWidget *
static void findAccelerators(KAccelStringList &result, QString &used)
int maxWeight(int &index, const QString &used) const
void calculateWeights(int initialWeight)
QChar accelerator() const
static void manage(QWidget *widget, bool programmers_mode=false)
Manages the accelerators of a widget.
int originalAccel() const