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"
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
)
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);
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
);
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()));
84 BackgroundDialog::~BackgroundDialog()
89 void BackgroundDialog::cleanup()
91 //FIXME could be bad if we get hidden and reshown
96 void BackgroundDialog::reloadConfig()
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());
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
);
117 // Load wallpaper plugins
118 QString currentPlugin
;
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();
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
) {
146 m_wallpaperMode
->addItem(KIcon(info
.icon()), info
.name(),
147 QVariant::fromValue(WallpaperInfo(info
.pluginName(), QString())));
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
)
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
) {
180 if (wallpaperInfo
.first
.isEmpty()) {
185 m_wallpaper
= Plasma::Wallpaper::load(wallpaperInfo
.first
);
186 m_preview
->setWallpaper(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
);
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()
217 PlasmaApp::self()->setActiveOpacity(m_activeSlider
->value() / 10.0);
218 PlasmaApp::self()->setIdleOpacity(m_idleSlider
->value() / 10.0);
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
);
231 KConfigGroup cfg
= wallpaperConfig(m_wallpaper
->pluginName());
232 m_wallpaper
->save(cfg
);
235 m_containment
->setWallpaper(wallpaperPlugin
, wallpaperMode
);