summaryrefslogtreecommitdiffstats
path: root/style/shortcuthandler.cpp
blob: b880a89fc5aa5618cc2fbe5abe8ed8d62a1def4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
  QtCurve (C) Craig Drummond, 2007 - 2010 [email protected]

  ----

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public
  License version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "shortcuthandler.h"
#include <tqpopupmenu.h>
#include <tqapplication.h>
#include <tqobjectlist.h>
#include <tqmainwindow.h>
#include <tqdialog.h>
#include <tqstyle.h>

ShortcutHandler::ShortcutHandler(TQObject *parent)
               : TQObject(parent)
               , itsAltDown(false)
{
}

ShortcutHandler::~ShortcutHandler()
{
}

bool ShortcutHandler::hasSeenAlt(const TQWidget *widget) const
{
    if(widget && !widget->isEnabled())
        return false;

    if(::tqqt_cast<const TQPopupMenu *>(widget))
        return itsOpenMenus.count() && itsOpenMenus.last()==widget;
    else
        return itsOpenMenus.isEmpty() &&  itsSeenAlt.contains((TQWidget *)(widget->topLevelWidget()));
/*    return false;
    {
        const TQWidget *w=widget;

        while(w)
        {
            if(itsSeenAlt.contains((TQWidget *)w))
                return true;
            w=w->parentWidget();
        }
    }
    if(itsHandlePopupsOnly)
        return false;
    widget = widget->topLevelWidget();
    return itsSeenAlt.contains((TQWidget *)widget);*/
}

bool ShortcutHandler::showShortcut(const TQWidget *widget) const
{
    return itsAltDown && hasSeenAlt(widget);
}

void ShortcutHandler::widgetDestroyed(TQObject *o)
{
    itsUpdated.remove(static_cast<TQWidget *>(o));
    itsOpenMenus.remove(static_cast<TQWidget *>(o));
}

void ShortcutHandler::updateWidget(TQWidget *w)
{
    if(!itsUpdated.contains(w))
    {
        connect(w, SIGNAL(destroyed(TQObject *)), this, SLOT(widgetDestroyed(TQObject *)));
        itsUpdated.append(w);
        w->repaint(TRUE);
    }
}

void ShortcutHandler::setSeenAlt(TQWidget *w)
{
    if(!itsSeenAlt.contains(w))
        itsSeenAlt.append(w);
}

bool ShortcutHandler::eventFilter(TQObject *o, TQEvent *e)
{
    if (!o->isWidgetType())
        return TQObject::eventFilter(o, e);

    TQWidget *widget = ::tqqt_cast<TQWidget*>(o);
    switch(e->type())
    {
        case TQEvent::KeyPress:
            if (Key_Alt==((TQKeyEvent*)e)->key())
            {
                itsAltDown = true;

                if(::tqqt_cast<TQPopupMenu *>(widget))
                {
                    setSeenAlt(widget);
                    updateWidget(widget);
                    if(widget->parentWidget() && widget->parentWidget()->topLevelWidget())
                        itsSeenAlt.append(widget->parentWidget()->topLevelWidget());
                }
                else
                {
                    widget = widget->topLevelWidget();
                    setSeenAlt(widget);

                    // Alt has been pressed - find all widgets that care
                    TQObjectList *l = widget->queryList("TQWidget");
                    TQObjectListIt it( *l );
                    TQWidget *w;
                    while ((w = (TQWidget *)it.current()) != 0)
                    {
                        ++it;
                        if (!(w->isTopLevel() || !w->isVisible())) // || w->style().styleHint(QStyle::SH_UnderlineAccelerator, w)))
                            updateWidget(w);
                    }
                    delete l;
                }
            }
            break;
        case TQEvent::WindowDeactivate:
        case TQEvent::KeyRelease:
            if (TQEvent::WindowDeactivate==e->type() || Key_Alt==static_cast<TQKeyEvent*>(e)->key())
            {
                itsAltDown = false;
                TQValueList<TQWidget *>::const_iterator it(itsUpdated.begin()),
                                                      end(itsUpdated.end());

                for (; it!=end; ++it)
                    (*it)->repaint(TRUE);
                if(!itsUpdated.contains(widget))
                    widget->repaint(TRUE);
                itsSeenAlt.clear();
                itsUpdated.clear();
            }
            break;
        case TQEvent::Show:
            if(::tqqt_cast<TQPopupMenu *>(widget))
            {
                TQWidget *prev=itsOpenMenus.count() ? itsOpenMenus.last() : 0L;
                itsOpenMenus.append(widget);
                if(itsAltDown && prev)
                    prev->repaint(TRUE);
                connect(widget, SIGNAL(destroyed(TQObject *)), this, SLOT(widgetDestroyed(TQObject *)));
            }
            break;
        case TQEvent::Hide:
            if(::tqqt_cast<TQPopupMenu *>(widget))
            {
                itsSeenAlt.remove(widget);
                itsUpdated.remove(widget);
                itsOpenMenus.remove(widget);
                if(itsAltDown)
                {
                    if(itsOpenMenus.count())
                        itsOpenMenus.last()->repaint(TRUE);
                    else if(widget->parentWidget() && widget->parentWidget()->topLevelWidget())
                        widget->parentWidget()->topLevelWidget()->repaint(TRUE);
                }
            }
            break;
        case TQEvent::Close:
            // Reset widget when closing
            itsSeenAlt.remove(widget);
            itsUpdated.remove(widget);
            itsSeenAlt.remove(widget->topLevelWidget());
            itsOpenMenus.remove(widget);
            if(itsAltDown && itsOpenMenus.count())
                itsOpenMenus.last()->repaint(TRUE);
            break;
            break;
        default:
            break;
    }
    return TQObject::eventFilter(o, e);
}

#include "shortcuthandler.moc"