-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.cpp
646 lines (564 loc) · 21.3 KB
/
panel.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "panel.h"
#include "initializer.h"
#include "applets/appmenu/appmenu.h"
#include "applets/datetime/datetime.h"
#include "applets/kblayout/kblayout.h"
#include "applets/usermenu/usermenu.h"
#include "applets/volume/volume.h"
#include "applets/windowlist/windowlist.h"
#include "applets/workspaces/workspaces.h"
#include "applets/launcher/launcher.h"
#include "applets/localipv4/localipv4.h"
#include "applets/mpris/mpris.h"
#include "applets/battery/battery.h"
#include "applets/snitray/snitray.h"
#include "applets/clioutput/clioutput.h"
#include "applets/splitter/splitter.h"
#include "applets/spacer/spacer.h"
QDBusMessage msg;
QDBusConnection panelSessionBus = QDBusConnection::sessionBus();
void Panel::setPanelFlags() {
this->setAttribute(Qt::WA_X11NetWmWindowTypeDock);
this->setAttribute(Qt::WA_AlwaysShowToolTips);
}
void Panel::setPanelGeometry() {
// Set panel screen
QString screenName = mCfgMan->mPanels.at(mPanelID - 1).screen;
QList<QScreen*> screens = QGuiApplication::screens();
foreach (QScreen* screen, screens) {
if (screen->name() == screenName) {
mPanelScreen = screen;
}
}
if (!mPanelScreen) {
mPanelScreen = QGuiApplication::primaryScreen();
}
mScreenGeometry = mPanelScreen->geometry();
mScreenWidth = mPanelScreen->size().width();
mScreenHeight = mPanelScreen->size().height();
// Panel should update geometry when screen resolution is changed
this->connect(mPanelScreen, &QScreen::geometryChanged, this, [this]() {
qDebug() << "Screen geometry changed.";
setPanelGeometry();
});
/* Getting panel thickness
* Height - horizontal panel
* Width - vertical panel */
mPanelThickness = mCfgMan->mPanels.at(mPanelID - 1).thickness;
// Location, Layout & Strut
int ax = mScreenGeometry.x(), ay = mScreenGeometry.y();
mPanelLocation = mCfgMan->mPanels.at(mPanelID - 1).location;
mAxisShift = mCfgMan->mPanels.at(mPanelID - 1).shift;
int topStrut = 0, bottomStrut = 0, leftStrut = 0, rightStrut = 0;
if (mPanelLocation == Top) {
mPanelLayout = Horizontal;
if (mAxisShift != 0) {
ax += mAxisShift;
}
topStrut = mPanelThickness;
}
else if (mPanelLocation == Bottom) {
mPanelLayout = Horizontal;
ay = mScreenGeometry.y() + mScreenHeight - mPanelThickness;
if (mAxisShift != 0) {
ax += mAxisShift;
}
bottomStrut = mPanelThickness;
}
else if (mPanelLocation == Left) {
mPanelLayout = Vertical;
if (mAxisShift != 0) {
ay += mAxisShift;
}
leftStrut = mPanelThickness;
}
else { // Right
mPanelLayout = Vertical;
ax = mScreenGeometry.x() + mScreenWidth - mPanelThickness;
if (mAxisShift != 0) {
ay += mAxisShift;
}
rightStrut = mPanelThickness;
}
// Size
if (mPanelLayout == Horizontal) {
mPanelHeight = mPanelThickness;
if (mCfgMan->mPanels.at(mPanelID - 1).expand) {
mPanelWidth = mScreenWidth;
}
else {
/* User requested not to stretch panel.
* We set default panel width to 0, hence it will
* adjust its width automatically after adding applets. */
mPanelWidth = 0;
}
}
else {
mPanelWidth = mPanelThickness;
if (mCfgMan->mPanels.at(mPanelID - 1).expand) {
mPanelHeight = mScreenHeight;
}
else {
/* User requested not to stretch panel.
* We set default panel height to 0, hence it will
* adjust its height automatically after adding applets. */
mPanelHeight = 0;
}
}
// Avoiding intersection of two panels (i.e., left and top)
for (int i = 0; i < mPrevPanels.length(); ++i) {
Panel* prevPanel = mPrevPanels.at(i);
typedef PanelLocation pl;
pl l1 = prevPanel->mPanelLocation, l2 = this->mPanelLocation;
if ((l1 == pl::Top && l2 == pl::Left) ||
(l1 == pl::Top && l2 == pl::Right)) {
ay += prevPanel->mPanelThickness;
mPanelHeight -= prevPanel->mPanelThickness;
}
else if ((l1 == pl::Bottom && l2 == pl::Left) ||
(l1 == pl::Bottom && l2 == pl::Right)) {
mPanelHeight -= prevPanel->mPanelThickness;
}
else if ((l1 == pl::Left && l2 == pl::Top) ||
(l1 == pl::Left && l2 == pl::Bottom)) {
ax += prevPanel->mPanelThickness;
mPanelWidth -= prevPanel->mPanelThickness;
}
else if ((l1 == pl::Right && l2 == pl::Top) ||
(l1 == pl::Right && l2 == pl::Bottom)) {
mPanelWidth -= prevPanel->mPanelThickness;
}
else if (l1 == l2) {
qDebug() << "Panels" << prevPanel->mPanelID << "and" << this->mPanelID << "coincided.";
}
}
this->setGeometry(ax, ay, mPanelWidth, mPanelHeight);
this->move(ax, ay);
this->setFixedSize(mPanelWidth, mPanelHeight);
if (!mCfgMan->mPanels.at(mPanelID - 1).enableAutoHide) {
KWindowSystem::setExtendedStrut(mPanelWId,
leftStrut,
mScreenGeometry.y(),
mScreenGeometry.y() + mScreenGeometry.height(),
rightStrut,
mScreenGeometry.y(),
mScreenGeometry.y() + mScreenGeometry.height(),
topStrut,
mScreenGeometry.x(),
mScreenGeometry.x() + mScreenGeometry.width(),
bottomStrut,
mScreenGeometry.x(),
mScreenGeometry.x() + mScreenGeometry.width());
qDebug() << "Strut:" << leftStrut << rightStrut << topStrut << bottomStrut;
qDebug() << "left strut restrictions:" << mScreenGeometry.y() << '-' << mScreenGeometry.y() + mScreenGeometry.height();
qDebug() << "right strut restrictions:" << mScreenGeometry.y() << '-' << mScreenGeometry.y() + mScreenGeometry.height();
qDebug() << "top strut restrictions:" << mScreenGeometry.x() << '-' << mScreenGeometry.x() + mScreenGeometry.width();
qDebug() << "bottom strut restrictions:" << mScreenGeometry.x() << '-' << mScreenGeometry.x() + mScreenGeometry.width();
qDebug() << this->geometry().x() << this->geometry().y();
qDebug() << ax << ay;
qDebug() << mPanelWidth << mPanelHeight;
}
KWindowSystem::setOnAllDesktops(mPanelWId, true);
connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged, this, [this]() {
KWindowSystem::setOnDesktop(mPanelWId, KWindowSystem::currentDesktop());
});
mCountWorkspaces = KWindowSystem::numberOfDesktops();
}
void Panel::setOnCenter() {
if (mPanelLayout == Horizontal) {
int panelWidth = this->width();
int screenWidth = mScreenGeometry.width();
int relativeX = (screenWidth - panelWidth) / 2;
int absoluteX = mScreenGeometry.x() + relativeX;
this->move(absoluteX, this->y());
}
else {
int panelHeight = this->height();
int screenHeight = mScreenGeometry.height();
int relativeY = (screenHeight - panelHeight) / 2;
int absoluteY = mScreenGeometry.y() + relativeY;
this->move(this->x(), absoluteY);
}
}
void Panel::setRepeatingActions() {
// here we bring to life QTimers for updating applets data
foreach (QObject* applet, mAppletList) {
Applet* appletObj = static_cast<Applet*>(applet);
if (appletObj->mAppletType == Dynamic) {
static_cast<DynamicApplet*>(appletObj)->activate();
}
}
}
void Panel::setPanelUI() {
#if mPanelLayout == 0
#include <QHBoxLayout>
#else
#include <QVBoxLayout>
#endif
// Create layout
int margin = mCfgMan->mPanels.at(mPanelID - 1).margin;
mPanelFrame = new QFrame();
mPanelFrame->setObjectName("mPanelFrame");
mPanelFrame->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
if (mPanelLayout == Horizontal) {
QHBoxLayout* panelLayout = new QHBoxLayout(this);
panelLayout->setContentsMargins(0, 0, 0, 0);
panelLayout->setSpacing(0);
panelLayout->addWidget(mPanelFrame);
mBoxLayout = new QHBoxLayout();
mBoxLayout->setContentsMargins(margin, 1, margin, 1);
mPanelFrame->setLayout(mBoxLayout);
}
else {
QVBoxLayout* panelLayout = new QVBoxLayout(this);
panelLayout->setContentsMargins(0, 0, 0, 0);
panelLayout->addWidget(mPanelFrame);
mBoxLayout = new QVBoxLayout(mPanelFrame);
mBoxLayout->setContentsMargins(1, margin, 1, margin);
}
mSpacing = mCfgMan->mPanels.at(mPanelID - 1).spacing;
mBoxLayout->setSpacing(mSpacing);
// Set font
mPanelFont.setFamily(mCfgMan->mFontFamily);
mPanelFont.setPointSize(mCfgMan->mFontSize);
this->setFont(mPanelFont);
mFontMetrics = new QFontMetrics(mPanelFont);
// Set theme
mStylesheet = mCfgMan->mStylesheet;
QString stylesheetPath = QString("/usr/share/plainDE/styles/%1").arg(mStylesheet);
QFile stylesheetReader(stylesheetPath);
stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream styleSheet(&stylesheetReader);
this->setStyleSheet(styleSheet.readAll());
// Set panel background image
QString bgrPath = mCfgMan->mPanels.at(mPanelID - 1).backgroundImagePath;
QString frameStylesheet = QString("QFrame#mPanelFrame { border-image: url(%1) "
"0 0 0 0 stretch stretch; }").arg(bgrPath);
mPanelFrame->setStyleSheet(frameStylesheet);
// Set tooltips font
this->setStyleSheet(this->styleSheet() + " QToolTip { font-size: " + QString::number(mPanelFont.pointSize()) + "px; }");
// Set accent
mAccentColor = mCfgMan->mAccent;
// Set opacity
mOpacity = mCfgMan->mPanels.at(mPanelID - 1).opacity;
if (!mPanelTransparent) {
this->setWindowOpacity(mOpacity);
}
// Set default icon theme
QIcon::setThemeName(mCfgMan->mIconTheme);
mLauncherIconSize = mCfgMan->mPanels.at(mPanelID - 1).launcherIconSize;
}
void Panel::addApplets() {
// Initializing applets
foreach (QVariant applet, mCfgMan->mPanels.at(mPanelID - 1).applets) {
QString appletName = applet.toString();
qDebug() << appletName;
if (!appletName.compare("appmenu")) { // org.plainDE.appMenu
Applet* applet = new AppMenuApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("sni")) { // org.plainDE.sniTray
Applet* applet = new SNITrayApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("battery")) { // org.plainDE.battery
if (BatteryApplet::deviceHasBattery()) {
Applet* applet = new BatteryApplet(mCfgMan, this);
mAppletList.append(applet);
}
else {
qDebug() << "This device does not have a battery. "
"Deactivating Battery applet...";
}
}
else if (!appletName.compare("mpris")) { // org.plainDE.mpris
Applet* applet = new MPRISApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("volume")) { // org.plainDE.volume
Applet* applet = new VolumeApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("kblayout")) { // org.plainDE.kbLayout
Applet* applet = new KbLayoutApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("datetime")) { // org.plainDE.dateTime
Applet* applet = new DateTimeApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("usermenu")) { // org.plainDE.userMenu
Applet* applet = new UserMenuApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("windowlist")) { // org.plainDE.windowList
Applet* applet = new WindowListApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("localipv4")) { // org.plainDE.localIPv4
Applet* applet = new LocalIPv4Applet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("workspaces")) { // org.plainDE.workspaces
Applet* applet = new WorkspacesApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (appletName.startsWith("launcher:")) { // org.plainDE.launcher
Applet* applet = new LauncherApplet(mCfgMan, this, appletName);
mAppletList.append(applet);
}
else if (appletName.startsWith("clioutput:")) { // org.plainDE.cliOutput
QString name = appletName.split(':')[1];
Applet* applet = new CLIOutputApplet(mCfgMan, this, name);
mAppletList.append(applet);
}
else if (!appletName.compare("splitter")) { // org.plainDE.splitter
Applet* applet = new SplitterApplet(mCfgMan, this);
mAppletList.append(applet);
}
else if (!appletName.compare("spacer")) {
Applet* applet = new SpacerApplet(mCfgMan, this);
mAppletList.append(applet);
}
else {
qDebug() << "Unknown applet:" << applet;
}
}
// Adding applets to panel
foreach (QObject* applet, mAppletList) {
Applet* appletObj = static_cast<Applet*>(applet);
appletObj->externalWidgetSetup();
mBoxLayout->addWidget(appletObj->mExternalWidget);
}
}
void Panel::setTransparency() {
if (mStylesheet.contains("transparent")) {
this->setAttribute(Qt::WA_TranslucentBackground);
this->setAttribute(Qt::WA_NoSystemBackground);
this->setAttribute(Qt::WA_TransparentForMouseEvents);
this->setPalette(Qt::transparent);
this->setAutoFillBackground(false);
this->setAttribute(Qt::WA_OpaquePaintEvent);
mPanelTransparent = true;
}
}
void Panel::setAppletsActions() {
foreach (QObject* applet, mAppletList) {
static_cast<Applet*>(applet)->internalWidgetSetup();
}
}
void Panel::animation(AnimationType type) {
unsigned short ax = 0, ay = 0;
if (mPanelLayout == Horizontal) {
ax = this->geometry().x();
}
else if (mPanelLayout == Vertical) {
ay = this->geometry().y();
}
QPoint startValue, endValue;
switch (mPanelLocation) {
case Top:
if (type == Show) {
startValue = QPoint(ax, -mPanelThickness);
endValue = QPoint(ax, 0);
}
else if (type == Hide) {
startValue = QPoint(ax, 0);
endValue = QPoint(ax, -mPanelThickness);
}
else {
startValue = QPoint(ax, 0);
endValue = QPoint(ax, -mPanelThickness + 2);
mAutoHidden = true;
}
break;
case Bottom:
if (type == Show) {
startValue = QPoint(ax, mScreenHeight);
endValue = QPoint(ax, mScreenHeight - mPanelThickness);
}
else if (type == Hide) {
startValue = QPoint(ax, mScreenHeight - mPanelThickness);
endValue = QPoint(ax, mScreenHeight);
}
else {
startValue = QPoint(ax, mScreenHeight - mPanelThickness);
endValue = QPoint(ax, mScreenHeight - 2);
mAutoHidden = true;
}
break;
case Left:
if (type == Show) {
startValue = QPoint(-mPanelThickness, ay);
endValue = QPoint(0, ay);
}
else if (type == Hide) {
startValue = QPoint(0, ay);
endValue = QPoint(-mPanelThickness, ay);
}
else {
startValue = QPoint(0, ay);
endValue = QPoint(-mPanelThickness + 2, ay);
mAutoHidden = true;
}
break;
case Right:
if (type == Show) {
startValue = QPoint(mScreenWidth, ay);
endValue = QPoint(mScreenWidth - mPanelThickness, ay);
}
else if (type == Hide) {
startValue = QPoint(mScreenWidth - mPanelThickness, ay);
endValue = QPoint(mScreenWidth, ay);
}
else {
startValue = QPoint(mScreenWidth - mPanelThickness, ay);
endValue = QPoint(mScreenWidth - 2, ay);
mAutoHidden = true;
}
break;
}
if (mCfgMan->mEnableAnimation) {
QPropertyAnimation* panelAnimation = new QPropertyAnimation(this, "pos");
panelAnimation->setDuration(250);
panelAnimation->setStartValue(startValue);
panelAnimation->setEndValue(endValue);
panelAnimation->start();
this->connect(panelAnimation, &QPropertyAnimation::finished, this, [this]() {
QThread::msleep(250);
emit animationFinished();
});
}
else {
this->move(endValue.x(), endValue.y());
emit animationFinished();
}
}
void Panel::autoHideSetup() {
if (mCfgMan->mPanels.at(mPanelID - 1).enableAutoHide) {
setAttribute(Qt::WA_Hover, true);
mAutoHideTimer = new QTimer();
mAutoHideTimer->setInterval(
mCfgMan->mPanels.at(mPanelID - 1).autoHideInterval);
mAutoHideTimer->setSingleShot(true);
connect(mAutoHideTimer, &QTimer::timeout, this, [this]() {
animation(AutoHide);
});
if (mCfgMan->mEnableAnimation) {
connect(this, &Panel::animationFinished, this, [this]() {
if (!mEnableAutoHide) {
mAutoHideTimer->start();
mEnableAutoHide = true;
}
});
}
else {
mAutoHideTimer->start();
mEnableAutoHide = true;
}
}
}
void Panel::highlight() {
mPanelFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
QTimer::singleShot(5000, Qt::CoarseTimer, this, [this]() {
mPanelFrame->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
});
}
void Panel::testpoint(QObject* execHolder) {
/* This function is intended for testing your code.
* You can use it in order to test your applet or some features.
* It is ran in the very end of the panel initialization sequence
* (see Panel constructor below).
* execHolder is Initializer class instance.
* Use it as a parent for QProcess.
* If you want to get config.json value, use mCfgMan object fields.
* For example:
* * mCfgMan->mAccent
* * mCfgMan->mUseTriangularTabs
* * etc
* If you want to get an inner entry of current panel, use this syntax:
* mCfgMan->mPanels.at(mPanelID - 1)
* For example:
* * mCfgMan->mPanels.at(mPanelID - 1).spacing
* * mCfgMan->mPanels.at(mPanelID - 1).opacity
* * etc */
// here you can put your code to test
}
Panel::Panel(QObject* parent,
ConfigManager* cfgMan,
int id,
QApplication* app,
QList<Panel*> prevPanels): QWidget(nullptr) {
qDebug() << "--------------------------------------";
qDebug() << "Panel " << id;
mApplication = app;
mCfgMan = cfgMan;
mPanelID = id;
mExecHolder = parent;
// we need this property to avoid interference of two panels
// (i.e., left and top)
mPrevPanels = prevPanels;
mMprisCards = new QList<QWidget*>;
this->setWindowTitle("plainPanel");
this->setObjectName("panel");
setPanelFlags();
setPanelGeometry();
setPanelUI();
mPanelPID = app->applicationPid();
addApplets();
setTransparency();
mPanelWId = this->winId();
this->show();
setPanelGeometry();
setAppletsActions();
setRepeatingActions();
if (mCfgMan->mPanels.at(mPanelID - 1).setOnCenter) {
setOnCenter();
}
animation(Show);
autoHideSetup();
this->connect(static_cast<Initializer*>(mExecHolder),
&Initializer::panelShouldQuit, this, [this]() {
foreach (QProcess* process, mProcesses) {
if (process != NULL) {
delete process;
}
}
mProcesses.clear();
animation(Hide);
});
testpoint(mExecHolder);
}
void Panel::enterEvent(QEvent *event) {
if (mEnableAutoHide) {
mAutoHideTimer->stop();
if (mAutoHidden) {
animation(Show);
mAutoHidden = false;
}
}
event->accept();
}
void Panel::leaveEvent(QEvent *event) {
if (mEnableAutoHide) {
QRect geometry = this->geometry();
if (mScreenGeometry.contains(geometry) &&
!geometry.contains(mapFromGlobal(QCursor::pos()))) {
mAutoHideTimer->start();
}
}
event->accept();
}
void Panel::resizeEvent(QResizeEvent* event) {
if (mCfgMan->mPanels.at(mPanelID - 1).setOnCenter) {
setOnCenter();
}
event->accept();
}
Panel::~Panel() {
foreach (QObject* applet, mAppletList) {
delete applet;
}
this->hide();
qDebug() << "Panel" << mPanelID << "destructed.";
}