This repository has been archived by the owner on Dec 17, 2017. It is now read-only.
/
desktopswitch.cpp
164 lines (139 loc) · 4.51 KB
/
desktopswitch.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
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Razor - a lightweight, Qt based, desktop toolset
* http://razor-qt.org
*
* Copyright: 2011 Razor team
* Authors:
* Petr Vanek <petr@scribus.info>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include <QButtonGroup>
#include <QToolButton>
#include <QWheelEvent>
#include <QtDebug>
#include <QSignalMapper>
#include <razorqt/xfitman.h>
#include <razorqxt/qxtglobalshortcut.h>
#include <QHBoxLayout>
#include "desktopswitch.h"
#include "desktopswitchbutton.h"
Q_EXPORT_PLUGIN2(DesktopSwitch, DesktopSwitchPluginLibrary)
DesktopSwitch::DesktopSwitch(const IRazorPanelPluginStartupInfo &startupInfo) :
QObject(),
IRazorPanelPlugin(startupInfo),
m_pSignalMapper(new QSignalMapper(this)),
m_desktopCount(1)
{
m_buttons = new QButtonGroup(this);
connect ( m_pSignalMapper, SIGNAL(mapped(int)), this, SLOT(setDesktop(int)));
mWidget.setLayout(new QHBoxLayout(&mWidget));
setup();
}
void DesktopSwitch::setup()
{
// clear current state
foreach (QAbstractButton * b, m_buttons->buttons())
{
// TODO/FIXME: maybe it has to be removed from layout too?
m_pSignalMapper->removeMappings(b);
m_buttons->removeButton(b);
delete b;
}
// create new desktop layout
int firstKey = Qt::Key_F1;
int maxKey = Qt::Key_F35; // max defined in Qt
for (int i = 0; i < m_desktopCount; ++i)
{
QKeySequence sequence;
if (firstKey < maxKey)
{
sequence = QKeySequence(Qt::CTRL + firstKey++);
}
DesktopSwitchButton * m = new DesktopSwitchButton(&mWidget, i, sequence, xfitMan().getDesktopName(i, tr("Desktop %1").arg(i+1)));
m_pSignalMapper->setMapping(m, i);
connect(m, SIGNAL(activated()), m_pSignalMapper, SLOT(map())) ;
mWidget.layout()->addWidget(m);
m_buttons->addButton(m, i);
}
int activeDesk = qMax(xfitMan().getActiveDesktop(), 0);
QAbstractButton * button = m_buttons->button(activeDesk);
if (button)
button->setChecked(true);
connect(m_buttons, SIGNAL(buttonClicked(int)),
this, SLOT(setDesktop(int)));
realign();
}
DesktopSwitch::~DesktopSwitch()
{
}
void DesktopSwitch::x11EventFilter(XEvent* _event)
{
if (_event->type == PropertyNotify)
{
int count = qMax(xfitMan().getNumDesktop(), 1);
if (m_desktopCount != count)
{
qDebug() << "Desktop count changed from" << m_desktopCount << "to" << count;
m_desktopCount = count;
m_desktopNames = xfitMan().getDesktopNames();
setup();
}
if (m_desktopNames != xfitMan().getDesktopNames())
{
m_desktopNames = xfitMan().getDesktopNames();
setup();
}
int activeDesk = qMax(xfitMan().getActiveDesktop(), 0);
m_buttons->button(activeDesk)->setChecked(true);
}
}
void DesktopSwitch::setDesktop(int desktop)
{
xfitMan().setActiveDesktop(desktop);
}
void DesktopSwitch::wheelEvent(QWheelEvent* e)
{
int max = xfitMan().getNumDesktop() - 1;
int delta = e->delta() < 0 ? 1 : -1;
int current = xfitMan().getActiveDesktop() + delta;
if (current > max)
current = 0;
else if (current < 0)
current = max;
xfitMan().setActiveDesktop(current);
}
void DesktopSwitch::realign()
{
int max = 0;
bool isHorizontal = panel()->isHorizontal();
foreach (QAbstractButton *btn, m_buttons->buttons())
{
if (isHorizontal)
max = qMax(max, btn->sizeHint().width());
else
max = qMax(max, btn->sizeHint().height());
}
foreach (QAbstractButton *btn, m_buttons->buttons())
{
if (isHorizontal)
btn->setMinimumWidth(max);
else
btn->setMinimumHeight(max);
}
}