Warning, file /system/qtcurve/qt4/style/shortcuthandler.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*****************************************************************************
0002  *   Copyright 2007 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
0003  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0004  *                                                                           *
0005  *   This program is free software; you can redistribute it and/or modify    *
0006  *   it under the terms of the GNU Lesser General Public License as          *
0007  *   published by the Free Software Foundation; either version 2.1 of the    *
0008  *   License, or (at your option) version 3, or any later version accepted   *
0009  *   by the membership of KDE e.V. (or its successor approved by the         *
0010  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0011  *   Section 6 of version 3 of the license.                                  *
0012  *                                                                           *
0013  *   This program is distributed in the hope that it will be useful,         *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0016  *   Lesser General Public License for more details.                         *
0017  *                                                                           *
0018  *   You should have received a copy of the GNU Lesser General Public        *
0019  *   License along with this library. If not,                                *
0020  *   see <http://www.gnu.org/licenses/>.                                     *
0021  *****************************************************************************/
0022 
0023 #include "shortcuthandler.h"
0024 #include <QWidget>
0025 #include <QMenu>
0026 #include <QEvent>
0027 #include <QKeyEvent>
0028 #include <QMenuBar>
0029 
0030 namespace QtCurve {
0031 ShortcutHandler::ShortcutHandler(QObject *parent) : QObject(parent),
0032                                                     m_altDown(false)
0033 {
0034 }
0035 
0036 ShortcutHandler::~ShortcutHandler()
0037 {
0038 }
0039 
0040 bool ShortcutHandler::hasSeenAlt(const QWidget *widget) const
0041 {
0042     if (widget && !widget->isEnabled())
0043         return false;
0044 
0045     if (qobject_cast<const QMenu*>(widget)) {
0046         return m_openMenus.count() && m_openMenus.last()==widget;
0047         // const QWidget *w = widget;
0048         // while (w) {
0049         //     if (m_seenAlt.contains((QWidget*)w))
0050         //         return true;
0051         //     w = w->parentWidget();
0052         // }
0053     } else {
0054         return (m_openMenus.isEmpty() &&
0055                 m_seenAlt.contains((QWidget*)(widget->window())));
0056     }
0057     return false;
0058 }
0059 
0060 bool ShortcutHandler::showShortcut(const QWidget *widget) const
0061 {
0062     return m_altDown && hasSeenAlt(widget);
0063 }
0064 
0065 void ShortcutHandler::widgetDestroyed(QObject *o)
0066 {
0067     m_updated.remove(static_cast<QWidget *>(o));
0068     m_openMenus.removeAll(static_cast<QWidget *>(o));
0069 }
0070 
0071 void ShortcutHandler::updateWidget(QWidget *w)
0072 {
0073     if(!m_updated.contains(w))
0074     {
0075         m_updated.insert(w);
0076         w->update();
0077         connect(w, SIGNAL(destroyed(QObject *)), this, SLOT(widgetDestroyed(QObject *)));
0078     }
0079 }
0080 
0081 bool ShortcutHandler::eventFilter(QObject *o, QEvent *e)
0082 {
0083     if (!o->isWidgetType())
0084         return QObject::eventFilter(o, e);
0085 
0086     QWidget *widget = static_cast<QWidget*>(o);
0087     switch (e->type()) {
0088     case QEvent::KeyPress:
0089         if (Qt::Key_Alt == static_cast<QKeyEvent*>(e)->key()) {
0090             m_altDown = true;
0091             if (qobject_cast<QMenu*>(widget)) {
0092                 m_seenAlt.insert(widget);
0093                 updateWidget(widget);
0094                 if (widget->parentWidget() &&
0095                     widget->parentWidget()->window()) {
0096                     m_seenAlt.insert(widget->parentWidget()->window());
0097                 }
0098             } else {
0099                 widget = widget->window();
0100                 m_seenAlt.insert(widget);
0101                 QList<QWidget*> l = widget->findChildren<QWidget*>();
0102                 for (int pos = 0;pos < l.size();++pos) {
0103                     QWidget *w = l.at(pos);
0104                     if (!(w->isWindow() || !w->isVisible())) {
0105                         // || w->style()->styleHint(
0106                         //     QStyle::SH_UnderlineShortcut, 0, w)))
0107                         updateWidget(w);
0108                     }
0109                 }
0110                 QList<QMenuBar*> m = widget->findChildren<QMenuBar*>();
0111                 for (int i = 0; i < m.size(); ++i)
0112                     updateWidget(m.at(i));
0113             }
0114         }
0115         break;
0116     case QEvent::WindowDeactivate:
0117     case QEvent::KeyRelease:
0118         if (QEvent::WindowDeactivate == e->type() ||
0119             Qt::Key_Alt == static_cast<QKeyEvent*>(e)->key()) {
0120             m_altDown = false;
0121             foreach (QWidget *widget, m_updated) {
0122                 widget->update();
0123             }
0124             if (!m_updated.contains(widget))
0125                 widget->update();
0126             m_seenAlt.clear();
0127             m_updated.clear();
0128         }
0129         break;
0130     case QEvent::Show:
0131         if (qobject_cast<QMenu*>(widget)) {
0132             QWidget *prev = m_openMenus.count() ? m_openMenus.last() : 0L;
0133             m_openMenus.append(widget);
0134             if (m_altDown && prev)
0135                 prev->update();
0136             connect(widget, SIGNAL(destroyed(QObject*)),
0137                     this, SLOT(widgetDestroyed(QObject*)));
0138         }
0139         break;
0140     case QEvent::Hide:
0141         if (qobject_cast<QMenu*>(widget)) {
0142             m_seenAlt.remove(widget);
0143             m_updated.remove(widget);
0144             m_openMenus.removeAll(widget);
0145             if (m_altDown) {
0146                 if (m_openMenus.count()) {
0147                     m_openMenus.last()->update();
0148                 } else if (widget->parentWidget() &&
0149                            widget->parentWidget()->window())
0150                     widget->parentWidget()->window()->update();
0151             }
0152         }
0153         break;
0154     case QEvent::Close:
0155         // Reset widget when closing
0156         m_seenAlt.remove(widget);
0157         m_updated.remove(widget);
0158         m_seenAlt.remove(widget->window());
0159         m_openMenus.removeAll(widget);
0160         if (m_altDown) {
0161             if (m_openMenus.count()) {
0162                 m_openMenus.last()->update();
0163             } else if (widget->parentWidget() &&
0164                        widget->parentWidget()->window())
0165                 widget->parentWidget()->window()->update();
0166         }
0167         break;
0168     default:
0169         break;
0170     }
0171     return QObject::eventFilter(o, e);
0172 }
0173 
0174 }