not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / screensaver / backgrounddialog.cpp
blobe644f742c4d368a77de943264ab5b292453d07ea
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4 Copyright (c) 2008 by Petri Damsten <damu@iki.fi>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
12 #include "backgrounddialog.h"
14 #include <QPainter>
15 //#include <QFile>
16 //#include <QAbstractItemView>
17 //#include <QStandardItemModel>
19 //#include <KStandardDirs>
20 //#include <KDesktopFile>
21 //#include <KColorScheme>
23 #include <Plasma/Containment>
24 #include <Plasma/FrameSvg>
25 #include <Plasma/Wallpaper>
26 #include <Plasma/View>
27 //#include <Plasma/Corona>
29 #include "plasmaapp.h"
30 #include "wallpaperpreview.h"
32 typedef QPair<QString, QString> WallpaperInfo;
33 Q_DECLARE_METATYPE(WallpaperInfo)
36 BackgroundDialog::BackgroundDialog(const QSize& res, Plasma::Containment *c, Plasma::View* view, QWidget* parent)
37 : KDialog(parent),
38 m_wallpaper(0),
39 m_view(view),
40 m_containment(c),
41 m_preview(0)
43 //setWindowIcon(KIcon("preferences-desktop-wallpaper"));
44 setCaption(i18n("Background Settings"));
45 showButtonSeparator(true);
46 setButtons(Ok | Cancel | Apply);
48 QWidget * main = new QWidget(this);
49 setupUi(main);
51 // Size of monitor image: 200x186
52 // Geometry of "display" part of monitor image: (23,14)-[151x115]
53 qreal previewRatio = (qreal)res.height() / (qreal)res.width();
54 QSize monitorSize(200, int(200 * previewRatio));
56 Plasma::FrameSvg *svg = new Plasma::FrameSvg(this);
57 svg->setImagePath("widgets/monitor");
58 svg->resizeFrame(monitorSize);
59 QPixmap monitorPix(monitorSize + QSize(0, svg->elementSize("base").height() - svg->marginSize(Plasma::BottomMargin)));
60 monitorPix.fill(Qt::transparent);
62 QPainter painter(&monitorPix);
63 QPoint standPosition(monitorSize.width()/2 - svg->elementSize("base").width()/2, svg->contentsRect().bottom());
64 svg->paint(&painter, QRect(standPosition, svg->elementSize("base")), "base");
65 svg->paintFrame(&painter);
66 painter.end();
68 m_monitor->setPixmap(monitorPix);
69 m_monitor->setWhatsThis(i18n(
70 "This picture of a monitor contains a preview of "
71 "what the current settings will look like on your desktop."));
72 m_preview = new WallpaperPreview(m_monitor);
73 m_preview->setGeometry(svg->contentsRect().toRect());
75 connect(this, SIGNAL(finished(int)), this, SLOT(cleanup()));
76 connect(this, SIGNAL(okClicked()), this, SLOT(saveConfig()));
77 connect(this, SIGNAL(applyClicked()), this, SLOT(saveConfig()));
79 setMainWidget(main);
80 reloadConfig();
81 adjustSize();
84 BackgroundDialog::~BackgroundDialog()
86 cleanup();
89 void BackgroundDialog::cleanup()
91 //FIXME could be bad if we get hidden and reshown
92 delete m_wallpaper;
93 m_wallpaper = 0;
96 void BackgroundDialog::reloadConfig()
98 //transparency
99 m_activeSlider->setValue(PlasmaApp::self()->activeOpacity() * 10);
100 m_idleSlider->setValue(PlasmaApp::self()->idleOpacity() * 10);
102 label->setVisible(PlasmaApp::hasComposite());
103 m_activeSlider->setVisible(PlasmaApp::hasComposite());
105 // Wallpaper
106 disconnect(m_wallpaperMode, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBackgroundMode(int)));
107 int wallpaperIndex = 0;
109 bool doWallpaper = m_containment->drawWallpaper() && ! PlasmaApp::hasComposite();
110 m_wallpaperLabel->setVisible(doWallpaper);
111 m_wallpaperTypeLabel->setVisible(doWallpaper);
112 m_wallpaperMode->setVisible(doWallpaper);
113 m_wallpaperConfig->setVisible(doWallpaper);
114 m_monitor->setVisible(doWallpaper);
115 m_preview->setVisible(doWallpaper);
116 if (doWallpaper) {
117 // Load wallpaper plugins
118 QString currentPlugin;
119 QString currentMode;
121 Plasma::Wallpaper *currentWallpaper = m_containment->wallpaper();
122 if (currentWallpaper) {
123 currentPlugin = currentWallpaper->pluginName();
124 currentMode = currentWallpaper->renderingMode().name();
127 const KPluginInfo::List plugins = Plasma::Wallpaper::listWallpaperInfo();
128 m_wallpaperMode->clear();
129 int i = 0;
130 QString placeholder = i18n("No Wallpaper");
131 //m_wallpaperMode->addItem(KIcon(), i18n("No Wallpaper"),
132 // QVariant::fromValue(WallpaperInfo(QString(), QString())));
133 foreach (const KPluginInfo& info, plugins) {
134 bool matches = info.pluginName() == currentPlugin;
135 const QList<KServiceAction>& modes = info.service()->actions();
136 if (modes.count() > 0) {
137 foreach (const KServiceAction& mode, modes) {
138 m_wallpaperMode->addItem(KIcon(mode.icon()), mode.text(),
139 QVariant::fromValue(WallpaperInfo(info.pluginName(), mode.name())));
140 if (matches && mode.name() == currentMode) {
141 wallpaperIndex = i;
143 ++i;
145 } else {
146 m_wallpaperMode->addItem(KIcon(info.icon()), info.name(),
147 QVariant::fromValue(WallpaperInfo(info.pluginName(), QString())));
148 if (matches) {
149 wallpaperIndex = i;
151 ++i;
154 m_wallpaperMode->setCurrentIndex(wallpaperIndex);
155 changeBackgroundMode(wallpaperIndex);
158 connect(m_wallpaperMode, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBackgroundMode(int)));
161 void BackgroundDialog::changeBackgroundMode(int mode)
163 kDebug();
164 QWidget* w = 0;
165 WallpaperInfo wallpaperInfo = m_wallpaperMode->itemData(mode).value<WallpaperInfo>();
167 if (!m_wallpaperConfig->layout()) {
168 new QVBoxLayout(m_wallpaperConfig);
171 if (m_wallpaperConfig->layout()->count() > 0) {
172 delete dynamic_cast<QWidgetItem*>(m_wallpaperConfig->layout()->takeAt(0))->widget();
175 if (m_wallpaper && m_wallpaper->pluginName() != wallpaperInfo.first) {
176 delete m_wallpaper;
177 m_wallpaper = 0;
180 if (wallpaperInfo.first.isEmpty()) {
181 return;
184 if (!m_wallpaper) {
185 m_wallpaper = Plasma::Wallpaper::load(wallpaperInfo.first);
186 m_preview->setWallpaper(m_wallpaper);
189 if (m_wallpaper) {
190 m_wallpaper->setRenderingMode(wallpaperInfo.second);
191 KConfigGroup cfg = wallpaperConfig(wallpaperInfo.first);
192 kDebug() << "making a" << wallpaperInfo.first << "in mode" << wallpaperInfo.second;
193 m_wallpaper->restore(cfg);
194 w = m_wallpaper->createConfigurationInterface(m_wallpaperConfig);
197 if (!w) {
198 w = new QWidget(m_wallpaperConfig);
201 m_wallpaperConfig->layout()->addWidget(w);
204 KConfigGroup BackgroundDialog::wallpaperConfig(const QString &plugin)
206 Q_ASSERT(m_containment);
208 //FIXME: we have details about the structure of the containment config duplicated here!
209 KConfigGroup cfg = m_containment->config();
210 cfg = KConfigGroup(&cfg, "Wallpaper");
211 return KConfigGroup(&cfg, plugin);
214 void BackgroundDialog::saveConfig()
216 //transparency
217 PlasmaApp::self()->setActiveOpacity(m_activeSlider->value() / 10.0);
218 PlasmaApp::self()->setIdleOpacity(m_idleSlider->value() / 10.0);
220 // Wallpaper
221 QString wallpaperPlugin = m_wallpaperMode->itemData(m_wallpaperMode->currentIndex()).value<WallpaperInfo>().first;
222 QString wallpaperMode = m_wallpaperMode->itemData(m_wallpaperMode->currentIndex()).value<WallpaperInfo>().second;
224 Plasma::Wallpaper *currentWallpaper = m_containment->wallpaper();
225 if (currentWallpaper) {
226 KConfigGroup cfg = wallpaperConfig(currentWallpaper->pluginName());
227 currentWallpaper->save(cfg);
230 if (m_wallpaper) {
231 KConfigGroup cfg = wallpaperConfig(m_wallpaper->pluginName());
232 m_wallpaper->save(cfg);
235 m_containment->setWallpaper(wallpaperPlugin, wallpaperMode);