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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/***************************************************************************
* Copyright (C) 2005 by Daniel Stöckel *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "kommando.h"
#include <klocale.h>
#include <X11/Xlib.h>
#include <X11/Xmu/WinUtil.h>
#include <kpixmapeffect.h>
#include "configuration.h"
Kommando::Kommando()
: QWidget( 0, "Kommando", Qt::WDestructiveClose |
Qt::WStyle_Customize |
Qt::WStyle_NoBorder),
actMenu(0),
navbutton(this),
oldNavbuttonIconType(0)
{
mTopLevelMenus.setAutoDelete(true);
//set up the widget's properties
resize();
setBackgroundMode( NoBackground );
//set up the navbutton
Config& config = Config::getSingleton();
navbutton.move(config.menuRadius(),config.menuRadius());
connect(&navbutton, SIGNAL(clicked()), this, SLOT(slotNavClick()));
}
void Kommando::slotGlobAccel()
{
toggle();
}
void Kommando::show( )
{
Window rep_root, rep_child;
int rep_rootx, rep_rooty;
unsigned int rep_mask;
int mousex, mousey;
Display* dpy = qt_xdisplay();
Window win = qt_xrootwin();
XClassHint hint;
hint.res_class = 0;
hint.res_name = 0;
//Get mouse coursor position and the application name of the window under the coursor
XQueryPointer (dpy, win, &rep_root, &rep_child, &rep_rootx, &rep_rooty, &mousex, &mousey, &rep_mask);
rep_child = XmuClientWindow(dpy,rep_child);
if(XGetClassHint(dpy,rep_child,&hint) != 0){
setActTopLevelMenu(hint.res_class);
} else {
setActTopLevelMenu("default");
}
if(hint.res_class){
XFree(hint.res_class);
}
if(hint.res_name){
XFree(hint.res_name);
}
Config& config = Config::getSingleton();
move(mousex-config.menuRadius(),mousey-config.menuRadius());
//get the part of the screen the widget is drawn onto and apply a fade effect to it
mScreenshot = QPixmap::grabWindow( qt_xrootwin(), x(), y(), width(), height() );
KPixmapEffect::fade( mScreenshot, config.opacity(), config.tintColor() );
QWidget::show();
}
void Kommando::hide( )
{
selectButton(BUTTON_DESELECT);
QWidget::hide();
}
void Kommando::toggle( )
{
if(!isHidden()){
hide();
} else {
show();
}
}
void Kommando::setActMenu( Menu * newMenu )
{
//disconnect everything
if(actMenu != 0){
actMenu->disconnect();
actMenu->hideButtons();
}
if(newMenu != 0){
if(mTopLevelMenus.containsRef(newMenu)){
navbutton.setIcon("error");
} else {
navbutton.setIcon("back");
}
//initialize the new menu
connect(newMenu, SIGNAL(clicked(int)), this, SLOT(slotOnClick()) );
connect(newMenu, SIGNAL(buttonSelected(int)),this,SLOT(slotButtonSelected(int)));
newMenu->showButtons();
}
actMenu = newMenu;
}
void Kommando::setActTopLevelMenu(const QString& appName )
{
Menu* menu = 0;
for(Menu* it = mTopLevelMenus.first(); it != 0; it = mTopLevelMenus.next()){
if (it->appName() == appName){
menu = it;
break;
}
//get a default menu, assume that a toplevel menu with QString::null or "default" as appname is one
if ((it->appName() == QString::null) || (it->appName() == "default")){
menu = it;
}
}
setActMenu(menu);
}
void Kommando::setTopLevelMenus( const QPtrList<Menu>& newmenus )
{
setActMenu(0);
//No need to clear the list, because autoDelete is turned on in
//mTopLevelMenus
mTopLevelMenus = newmenus;
}
void Kommando::windowActivationChange( bool oldActive )
{
//if the menu loses focus hide it
if(oldActive){
hide();
}
}
void Kommando::paintEvent( QPaintEvent * evt )
{
QPainter p(this);
p.drawPixmap(evt->rect().topLeft(),mScreenshot,evt->rect());
}
void Kommando::wheelEvent( QWheelEvent * evt )
{
actMenu->selectButton(actMenu->selectedButtonNum()+evt->delta()/120);
}
void Kommando::execute( )
{
if(!isHidden()){
Menu* temp = actMenu->execute();
if(temp){
setActMenu(temp);
} else {
hide();
}
}
}
void Kommando::slotOnClick( )
{
execute();
}
void Kommando::slotNavClick( )
{
if(actMenu->selectedButtonNum()==BUTTON_DESELECT){
if(mTopLevelMenus.containsRef(actMenu)){
hide();
} else {
setActMenu(actMenu->parentMenu());
}
} else {
execute();
}
}
void Kommando::slotButtonSelected( int type )
{
if(oldNavbuttonIconType != type){
switch(type){
case 0:
if(mTopLevelMenus.containsRef(actMenu)){
navbutton.setIcon("error");
} else {
navbutton.setIcon("back");
}
break;
case RoundButton::Commando:
navbutton.setIcon("exec");
break;
case RoundButton::Submenu:
navbutton.setIcon("folder");
break;
}
oldNavbuttonIconType=type;
}
}
void Kommando::keyPressEvent( QKeyEvent * evt )
{
switch(evt->key()){
case Qt::Key_Left:
actMenu->selectButton(actMenu->selectedButtonNum()-1);
break;
case Qt::Key_Right:
actMenu->selectButton(actMenu->selectedButtonNum()+1);
break;
case Qt::Key_Return:
slotNavClick();
break;
case Qt::Key_Escape:
if(mTopLevelMenus.containsRef(actMenu)){
hide();
} else {
setActMenu(actMenu->parentMenu());
}
break;
case Qt::Key_1:
case Qt::Key_2:
case Qt::Key_3:
case Qt::Key_4:
case Qt::Key_5:
case Qt::Key_6:
case Qt::Key_7:
case Qt::Key_8:
case Qt::Key_9:
//We want to get the number of the button, so treat the enum as number (yes I know, but it works fine :->)
actMenu->selectButton(evt->key() - Qt::Key_1);
break;
}
}
void Kommando::setNavButtonSize( unsigned short size )
{
navbutton.setRadius(size);
Config& config = Config::getSingleton();
navbutton.move(config.menuRadius(),config.menuRadius());
}
void Kommando::resize( )
{
Config& config = Config::getSingleton();
setFixedSize(config.menuSize(),config.menuSize());
//apply a circular mask
QRegion mask(rect(),QRegion::Ellipse);
setMask(mask);
}
void Kommando::selectButton( int num )
{
if(!isHidden())
actMenu->selectButton(num);
}
#include "kommando.moc"
|