forked from KDE/liquidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPager.cxx
126 lines (97 loc) · 3.64 KB
/
Pager.cxx
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
/*
Copyright 2017 Martin Koller, [email protected]
This file is part of liquidshell.
liquidshell 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 3 of the License, or
(at your option) any later version.
liquidshell 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 liquidshell. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Pager.hxx>
#include <PagerButton.hxx>
#include <DesktopPanel.hxx>
#include <QGridLayout>
#include <QButtonGroup>
#include <QPushButton>
#include <QX11Info>
#include <QAction>
#include <QDebug>
#include <KLocalizedString>
#include <KCMultiDialog>
#include <KWindowSystem>
#include <netwm.h>
//--------------------------------------------------------------------------------
Pager::Pager(DesktopPanel *parent)
: QWidget(parent)
{
group = new QButtonGroup(this);
QGridLayout *grid = new QGridLayout(this);
grid->setSpacing(2);
grid->setContentsMargins(QMargins());
connect(KWindowSystem::self(), &KWindowSystem::numberOfDesktopsChanged, this, &Pager::fill);
connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged,
[this]()
{
if ( KWindowSystem::currentDesktop() <= buttons.count() )
buttons[KWindowSystem::currentDesktop() - 1]->setChecked(true);
}
);
connect(parent, &DesktopPanel::rowsChanged, this, &Pager::fill);
fill();
QAction *action = new QAction(this);
action->setIcon(QIcon::fromTheme("configure"));
action->setText(i18n("Configure Virtual Desktops..."));
addAction(action);
connect(action, &QAction::triggered,
[this]()
{
auto dialog = new KCMultiDialog(parentWidget());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWindowTitle(i18n("Configure Virtual Desktops"));
dialog->addModule("desktop");
dialog->adjustSize();
dialog->show();
}
);
setContextMenuPolicy(Qt::ActionsContextMenu);
}
//--------------------------------------------------------------------------------
void Pager::fill()
{
qDeleteAll(buttons);
buttons.clear();
NETRootInfo ri(QX11Info::connection(), 0, NET::WM2DesktopLayout);
int row = 0, col = 0;
const int MAX_COLUMNS = std::max(1, ri.desktopLayoutColumnsRows().width());
for (int i = 1; i <= KWindowSystem::numberOfDesktops(); i++)
{
PagerButton *b = new PagerButton(i, qobject_cast<DesktopPanel *>(parentWidget()));
b->setCheckable(true);
b->setFocusPolicy(Qt::NoFocus);
group->addButton(b);
buttons.append(b);
if ( i == KWindowSystem::currentDesktop() )
b->setChecked(true);
connect(b, &PagerButton::clicked, this, &Pager::changeDesktop);
static_cast<QGridLayout *>(layout())->addWidget(b, row, col);
col = (col + 1) % MAX_COLUMNS;
if ( col == 0 ) row++;
}
}
//--------------------------------------------------------------------------------
void Pager::changeDesktop(bool checked)
{
if ( !checked )
return;
int desktopNum = qobject_cast<PagerButton *>(sender())->getDesktop();
if ( KWindowSystem::currentDesktop() == desktopNum )
KWindowSystem::setShowingDesktop(!KWindowSystem::showingDesktop());
else
KWindowSystem::setCurrentDesktop(desktopNum);
}
//--------------------------------------------------------------------------------