forked from KDE/liquidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopWidget.cxx
373 lines (294 loc) · 11.9 KB
/
DesktopWidget.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
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
/*
Copyright 2017 Martin Koller, kollix@aon.at
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 <DesktopWidget.hxx>
#include <DesktopPanel.hxx>
#include <WeatherApplet.hxx>
#include <DiskUsageApplet.hxx>
#include <ConfigureDesktopDialog.hxx>
#include <OnScreenVolume.hxx>
#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QAction>
#include <QIcon>
#include <QMenu>
#include <QCursor>
#include <QDebug>
#include <KWindowSystem>
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KCMultiDialog>
#include <KHelpMenu>
//--------------------------------------------------------------------------------
int DesktopWidget::appletNum = 0;
//--------------------------------------------------------------------------------
DesktopWidget::DesktopWidget()
: QWidget(nullptr, Qt::WindowDoesNotAcceptFocus)
{
setAttribute(Qt::WA_AlwaysShowToolTips);
setAutoFillBackground(true);
setFixedSize(QApplication::desktop()->size());
setWindowIcon(QIcon::fromTheme("liquidshell"));
KWindowSystem::setType(winId(), NET::Desktop);
panel = new DesktopPanel(nullptr);
panel->show();
connect(panel, &DesktopPanel::resized, this, &DesktopWidget::placePanel);
placePanel();
connect(KWindowSystem::self(), &KWindowSystem::currentDesktopChanged, this, &DesktopWidget::desktopChanged);
connect(KWindowSystem::self(), &KWindowSystem::numberOfDesktopsChanged, this, &DesktopWidget::loadSettings);
setContextMenuPolicy(Qt::ActionsContextMenu);
QAction *action = new QAction(QIcon::fromTheme("configure"), i18n("Configure Wallpaper..."), this);
connect(action, &QAction::triggered, this, &DesktopWidget::configureWallpaper);
addAction(action);
action = new QAction(i18n("Add Applet"), this);
addAction(action);
QMenu *menu = new QMenu;
action->setMenu(menu);
action = menu->addAction(QIcon::fromTheme("weather-clouds"), i18n("Weather"));
connect(action, &QAction::triggered, [this]() { addApplet("Weather"); });
action = menu->addAction(QIcon::fromTheme("drive-harddisk"), i18n("Disk Usage"));
connect(action, &QAction::triggered, [this]() { addApplet("DiskUsage"); });
action = new QAction(QIcon::fromTheme("preferences-desktop-display"), i18n("Configure Display..."), this);
connect(action, &QAction::triggered, this, &DesktopWidget::configureDisplay);
addAction(action);
action = new QAction(i18n("Help"), this);
KHelpMenu *helpMenu = new KHelpMenu(this);
helpMenu->action(KHelpMenu::menuHelpContents)->setVisible(false); // no handbook
helpMenu->action(KHelpMenu::menuWhatsThis)->setVisible(false);
action->setMenu(helpMenu->menu());
addAction(action);
// restore applets
KConfig config;
KConfigGroup group = config.group("DesktopApplets");
QStringList appletNames = group.readEntry("applets", QStringList());
for (const QString &appletName : appletNames)
{
DesktopApplet *applet = nullptr;
if ( appletName.startsWith("Weather_") )
applet = new WeatherApplet(this, appletName);
else if ( appletName.startsWith("DiskUsage_") )
applet = new DiskUsageApplet(this, appletName);
if ( applet )
{
int num = appletName.mid(appletName.indexOf('_') + 1).toInt();
if ( num > appletNum )
appletNum = num;
applet->loadConfig();
applets.append(applet);
connect(applet, &DesktopApplet::removeThis, this, &DesktopWidget::removeApplet);
}
}
loadSettings();
connect(QApplication::desktop(), &QDesktopWidget::primaryScreenChanged, this, &DesktopWidget::placePanel);
connect(QApplication::desktop(), &QDesktopWidget::screenCountChanged,
[this]() { setFixedSize(QApplication::desktop()->size()); desktopChanged(); });
connect(QApplication::desktop(), &QDesktopWidget::resized,
[this]() { setFixedSize(QApplication::desktop()->size()); placePanel(); desktopChanged(); });
new OnScreenVolume(this);
}
//--------------------------------------------------------------------------------
void DesktopWidget::loadSettings()
{
// simple check for default files matching current desktop size
QStringList dirNames = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
"wallpapers", QStandardPaths::LocateDirectory);
QStringList defaultFiles;
const QString geometryString = QString("%1x%2").arg(width()).arg(height());
for (const QString &dirName : dirNames)
{
for (const QString &subdir : QDir(dirName).entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable))
{
QDir dir(dirName + '/' + subdir + "/contents/images");
for (const QString &fileName : dir.entryList(QDir::Files | QDir::Readable))
{
if ( fileName.startsWith(geometryString) )
defaultFiles.append(dir.absoluteFilePath(fileName));
}
}
}
KConfig config;
for (int i = wallpapers.count() + 1; i <= KWindowSystem::numberOfDesktops(); i++)
{
KConfigGroup group = config.group(QString("Desktop_%1").arg(i));
Wallpaper wallpaper;
wallpaper.color = group.readEntry("color", QColor(Qt::black));
wallpaper.mode = group.readEntry("wallpaperMode", QString());
int idx = defaultFiles.count() ? ((i - 1) % defaultFiles.count()) : -1;
wallpaper.fileName = group.readEntry("wallpaper", (idx != -1) ? defaultFiles[idx] : QString());
wallpapers.append(wallpaper);
}
wallpapers.resize(KWindowSystem::numberOfDesktops()); // if we had more before, reduce size
wallpapers.squeeze();
desktopChanged();
}
//--------------------------------------------------------------------------------
void DesktopWidget::configureWallpaper()
{
if ( dialog ) // already open, do nothing
return;
bool showingDesktop = KWindowSystem::showingDesktop();
KWindowSystem::setShowingDesktop(true);
int desktopNum = currentDesktop;
Wallpaper origWallpaper = wallpapers[desktopNum];
dialog = new ConfigureDesktopDialog(nullptr, wallpapers[desktopNum]);
connect(dialog, &ConfigureDesktopDialog::changed,
[=]() { wallpapers[desktopNum] = dialog->getWallpaper(); desktopChanged(); });
connect(dialog, &QDialog::finished,
[=](int result)
{
if ( result == QDialog::Rejected )
{
wallpapers[desktopNum] = origWallpaper;
desktopChanged();
}
else // store in config file
{
const Wallpaper &wallpaper = wallpapers[desktopNum];
KConfig config;
KConfigGroup group = config.group(QString("Desktop_%1").arg(desktopNum + 1));
group.writeEntry("color", wallpaper.color);
group.writeEntry("wallpaper", wallpaper.fileName);
group.writeEntry("wallpaperMode", wallpaper.mode);
}
KWindowSystem::setShowingDesktop(showingDesktop); // restore
dialog->deleteLater();
dialog = nullptr;
});
dialog->show(); // not modal
}
//--------------------------------------------------------------------------------
void DesktopWidget::configureDisplay()
{
KCMultiDialog *dialog = new KCMultiDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->addModule("kcm_kscreen");
dialog->adjustSize();
dialog->setWindowTitle(i18n("Configure Display"));
dialog->show(); // not modal
}
//--------------------------------------------------------------------------------
void DesktopWidget::placePanel()
{
int panelHeight = qMin(panel->sizeHint().height(), panel->height());
QRect r = QApplication::desktop()->screenGeometry();
panel->setGeometry(r.x(), r.height() - panelHeight, r.width(), panelHeight);
KWindowSystem::setStrut(panel->winId(), 0, 0, 0, panelHeight);
}
//--------------------------------------------------------------------------------
void DesktopWidget::desktopChanged()
{
// free memory in previous shown desktop
if ( (currentDesktop >= 0) && (currentDesktop < wallpapers.count()) )
wallpapers[currentDesktop].pixmaps.clear();
currentDesktop = KWindowSystem::currentDesktop() - 1; // num to index
if ( currentDesktop >= wallpapers.count() )
return;
Wallpaper &wallpaper = wallpapers[currentDesktop];
if ( wallpaper.color.isValid() )
{
QPalette pal = palette();
pal.setColor(backgroundRole(), wallpaper.color);
setPalette(pal);
}
if ( !wallpaper.fileName.isEmpty() )
{
QPixmap pixmap(wallpaper.fileName);
if ( !pixmap.isNull() )
{
for (int i = 0; i < QApplication::desktop()->screenCount(); i++)
{
QRect r = QApplication::desktop()->screenGeometry(i);
QPixmap scaledPixmap = pixmap;
if ( wallpaper.mode == "Scaled" )
scaledPixmap = pixmap.scaled(r.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
else if ( wallpaper.mode == "ScaledKeepRatio" )
scaledPixmap = pixmap.scaled(r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
else if ( wallpaper.mode == "ScaledKeepRatioExpand" )
scaledPixmap = pixmap.scaled(r.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
wallpaper.pixmaps.append(scaledPixmap);
}
}
}
update();
// show applets for new desktop
for (DesktopApplet *applet : applets)
{
if ( !applet->isConfiguring() )
applet->setVisible(applet->isOnDesktop(currentDesktop + 1));
}
}
//--------------------------------------------------------------------------------
void DesktopWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
if ( currentDesktop < wallpapers.count() )
{
Wallpaper &wallpaper = wallpapers[currentDesktop];
QPainter painter(this);
for (int i = 0; i < QApplication::desktop()->screenCount(); i++)
{
if ( i < wallpaper.pixmaps.count() )
{
QRect r = QApplication::desktop()->screenGeometry(i);
painter.setClipRect(r);
painter.drawPixmap(r.x() + (r.width() - wallpaper.pixmaps[i].width()) / 2,
r.y() + (r.height() - wallpaper.pixmaps[i].height()) / 2,
wallpaper.pixmaps[i]);
}
}
}
}
//--------------------------------------------------------------------------------
void DesktopWidget::addApplet(const QString &type)
{
DesktopApplet *applet = nullptr;
if ( type == "Weather" )
{
applet = new WeatherApplet(this, QString("%1_%2").arg(type).arg(++appletNum));
}
else if ( type == "DiskUsage" )
{
applet = new DiskUsageApplet(this, QString("%1_%2").arg(type).arg(++appletNum));
}
if ( applet )
{
applets.append(applet);
applet->loadConfig(); // defaults + size
applet->move(QCursor::pos());
applet->saveConfig();
applet->show();
connect(applet, &DesktopApplet::removeThis, this, &DesktopWidget::removeApplet);
}
saveAppletsList();
}
//--------------------------------------------------------------------------------
void DesktopWidget::removeApplet(DesktopApplet *applet)
{
applets.removeOne(applet);
applet->deleteLater();
saveAppletsList();
}
//--------------------------------------------------------------------------------
void DesktopWidget::saveAppletsList()
{
KConfig config;
KConfigGroup group = config.group("DesktopApplets");
QStringList appletNames;
for (DesktopApplet *applet : applets)
appletNames.append(applet->getId());
group.writeEntry("applets", appletNames);
}
//--------------------------------------------------------------------------------