forked from MadFishTheOne/qtpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panelwindow.cpp
371 lines (321 loc) · 8.95 KB
/
panelwindow.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "panelwindow.h"
#include <QtGui/QResizeEvent>
#include <QtGui/QApplication>
#include <QtGui/QDesktopWidget>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QGraphicsView>
#include <QtGui/QMenu>
#include "x11support.h"
#include "dpisupport.h"
#include "panelapplication.h"
#include "applicationsmenuapplet.h"
#include "dockapplet.h"
#include "trayapplet.h"
#include "clockapplet.h"
PanelWindowGraphicsItem::PanelWindowGraphicsItem(PanelWindow* panelWindow)
: m_panelWindow(panelWindow)
{
setZValue(-10.0); // Background.
setAcceptedMouseButtons(Qt::RightButton);
}
PanelWindowGraphicsItem::~PanelWindowGraphicsItem()
{
}
QRectF PanelWindowGraphicsItem::boundingRect() const
{
return QRectF(0.0, 0.0, m_panelWindow->width(), m_panelWindow->height());
}
void PanelWindowGraphicsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0, 0, 0, 128));
painter->drawRect(boundingRect());
static const int borderThickness = 3;
if(m_panelWindow->verticalAnchor() == PanelWindow::Min)
{
QLinearGradient gradient(0.0, m_panelWindow->height() - borderThickness, 0.0, m_panelWindow->height());
gradient.setSpread(QGradient::RepeatSpread);
gradient.setColorAt(0.0, QColor(255, 255, 255, 0));
gradient.setColorAt(1.0, QColor(255, 255, 255, 128));
painter->setBrush(QBrush(gradient));
painter->drawRect(0.0, m_panelWindow->height() - borderThickness, m_panelWindow->width(), borderThickness);
}
else
{
QLinearGradient gradient(0.0, 0.0, 0.0, borderThickness);
gradient.setSpread(QGradient::RepeatSpread);
gradient.setColorAt(0.0, QColor(255, 255, 255, 128));
gradient.setColorAt(1.0, QColor(255, 255, 255, 0));
painter->setBrush(QBrush(gradient));
painter->drawRect(0.0, 0.0, m_panelWindow->width(), borderThickness);
}
}
void PanelWindowGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
}
void PanelWindowGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
if(isUnderMouse())
{
m_panelWindow->showPanelContextMenu(QPoint(static_cast<int>(event->pos().x()), static_cast<int>(event->pos().y())));
}
}
PanelWindow::PanelWindow()
: m_dockMode(false), m_screen(0), m_horizontalAnchor(Center), m_verticalAnchor(Min), m_orientation(Horizontal), m_layoutPolicy(Normal)
{
setStyleSheet("background-color: transparent");
setAttribute(Qt::WA_TranslucentBackground);
m_scene = new QGraphicsScene();
m_scene->setBackgroundBrush(QBrush(Qt::NoBrush));
m_panelItem = new PanelWindowGraphicsItem(this);
m_scene->addItem(m_panelItem);
m_view = new QGraphicsView(m_scene, this);
m_view->setStyleSheet("border-style: none;");
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_view->setRenderHint(QPainter::Antialiasing);
m_view->move(0, 0);
m_applets.append(new ApplicationsMenuApplet(this));
m_applets.append(new DockApplet(this));
m_applets.append(new TrayApplet(this));
m_applets.append(new ClockApplet(this));
resize(adjustHardcodedPixelSize(512), adjustHardcodedPixelSize(48));
}
PanelWindow::~PanelWindow()
{
while(!m_applets.isEmpty())
{
delete m_applets[m_applets.size() - 1];
m_applets.resize(m_applets.size() - 1);
}
delete m_view;
delete m_panelItem;
delete m_scene;
}
bool PanelWindow::init()
{
for(int i = 0; i < m_applets.size();)
{
if(!m_applets[i]->init())
m_applets.remove(i);
else
i++;
}
}
void PanelWindow::setDockMode(bool dockMode)
{
m_dockMode = dockMode;
setAttribute(Qt::WA_X11NetWmWindowTypeDock, m_dockMode);
if(!m_dockMode)
{
// No need to reserve space anymore.
X11Support::removeWindowProperty(winId(), "_NET_WM_STRUT");
X11Support::removeWindowProperty(winId(), "_NET_WM_STRUT_PARTIAL");
}
// When in dock mode, panel should appear on all desktops.
unsigned long desktop = m_dockMode ? 0xFFFFFFFF : 0;
X11Support::setWindowPropertyCardinal(winId(), "_NET_WM_DESKTOP", desktop);
updateLayout();
updatePosition();
}
void PanelWindow::setScreen(int screen)
{
m_screen = screen;
updateLayout();
updatePosition();
}
void PanelWindow::setHorizontalAnchor(Anchor horizontalAnchor)
{
m_horizontalAnchor = horizontalAnchor;
updatePosition();
}
void PanelWindow::setVerticalAnchor(Anchor verticalAnchor)
{
m_verticalAnchor = verticalAnchor;
updatePosition();
}
void PanelWindow::setOrientation(Orientation orientation)
{
m_orientation = orientation;
}
void PanelWindow::setLayoutPolicy(LayoutPolicy layoutPolicy)
{
m_layoutPolicy = layoutPolicy;
updateLayout();
}
void PanelWindow::updatePosition()
{
if(!m_dockMode)
return;
QRect screenGeometry = QApplication::desktop()->screenGeometry(m_screen);
int x;
switch(m_horizontalAnchor)
{
case Min:
x = screenGeometry.left();
break;
case Center:
x = (screenGeometry.left() + screenGeometry.right() + 1 - width())/2;
break;
case Max:
x = screenGeometry.right() - width() + 1;
break;
default:
Q_ASSERT(false);
break;
}
int y;
switch(m_verticalAnchor)
{
case Min:
y = screenGeometry.top();
break;
case Center:
y = (screenGeometry.top() + screenGeometry.bottom() + 1 - height())/2;
break;
case Max:
y = screenGeometry.bottom() - height() + 1;
break;
default:
Q_ASSERT(false);
break;
}
move(x, y);
// Update reserved space.
if(m_dockMode)
{
QVector<unsigned long> values; // Values for setting _NET_WM_STRUT_PARTIAL property.
values.fill(0, 12);
switch(m_horizontalAnchor)
{
case Min:
values[0] = x + width();
values[4] = y;
values[5] = y + height();
break;
case Max:
values[1] = QApplication::desktop()->width() - x;
values[6] = y;
values[7] = y + height();
break;
default:
break;
}
switch(m_verticalAnchor)
{
case Min:
values[2] = y + height();
values[8] = x;
values[9] = x + width();
break;
case Max:
values[3] = QApplication::desktop()->height() - y;
values[10] = x;
values[11] = x + width();
break;
default:
break;
}
X11Support::setWindowPropertyCardinalArray(winId(), "_NET_WM_STRUT_PARTIAL", values);
values.resize(4);
X11Support::setWindowPropertyCardinalArray(winId(), "_NET_WM_STRUT", values);
}
// Update "blur behind" hint.
QVector<unsigned long> values;
values.resize(4);
values[0] = 0;
values[1] = 0;
values[2] = width();
values[3] = height();
X11Support::setWindowPropertyCardinalArray(winId(), "_KDE_NET_WM_BLUR_BEHIND_REGION", values);
}
const QFont& PanelWindow::font() const
{
return PanelApplication::instance()->panelFont();
}
int PanelWindow::textBaseLine()
{
QFontMetrics metrics(font());
return (height() - metrics.height())/2 + metrics.ascent();
}
void PanelWindow::resizeEvent(QResizeEvent* event)
{
m_view->resize(event->size());
m_view->setSceneRect(0, 0, event->size().width(), event->size().height());
updateLayout();
updatePosition();
}
void PanelWindow::updateLayout()
{
// TODO: Vertical orientation support.
static const int spacing = adjustHardcodedPixelSize(4);
if(m_layoutPolicy != Normal && !m_dockMode)
{
int desiredSize = 0;
if(m_layoutPolicy == AutoSize)
{
for(int i = 0; i < m_applets.size(); i++)
{
if(m_applets[i]->desiredSize().width() >= 0)
desiredSize += m_applets[i]->desiredSize().width();
else
desiredSize += 64; // Spacer applets don't really make sense on auto-size panel.
}
desiredSize += spacing*(m_applets.size() - 1);
if(desiredSize < 0)
desiredSize = 0;
}
if(m_layoutPolicy == FillSpace)
{
QRect screenGeometry = QApplication::desktop()->screenGeometry(m_screen);
desiredSize = screenGeometry.width();
}
if(desiredSize != width())
resize(desiredSize, height());
}
// Get total amount of space available for "spacer" applets (that take all available free space).
int freeSpace = width() - spacing*(m_applets.size() - 1);
int numSpacers = 0;
for(int i = 0; i < m_applets.size(); i++)
{
if(m_applets[i]->desiredSize().width() >= 0)
freeSpace -= m_applets[i]->desiredSize().width();
else
numSpacers++;
}
int spaceForOneSpacer = numSpacers > 0 ? (freeSpace/numSpacers) : 0;
// Calculate rectangles for each applet.
int spacePos = 0;
for(int i = 0; i < m_applets.size(); i++)
{
QPoint appletPosition(spacePos, 0);
QSize appletSize = m_applets[i]->desiredSize();
if(appletSize.width() < 0)
{
if(numSpacers > 1)
{
appletSize.setWidth(spaceForOneSpacer);
freeSpace -= spaceForOneSpacer;
numSpacers--;
}
else
{
appletSize.setWidth(freeSpace);
freeSpace = 0;
numSpacers--;
}
}
appletSize.setHeight(height());
m_applets[i]->setPosition(appletPosition);
m_applets[i]->setSize(appletSize);
spacePos += appletSize.width() + spacing;
}
}
void PanelWindow::showPanelContextMenu(const QPoint& point)
{
QMenu menu;
menu.addAction(QIcon::fromTheme("preferences-desktop"), "Configure...", PanelApplication::instance(), SLOT(showConfigurationDialog()));
menu.addAction(QIcon::fromTheme("application-exit"), "Quit panel", QApplication::instance(), SLOT(quit()));
menu.exec(pos() + point);
}