Plasma
backgrounddialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "backgrounddialog.h"
00013
00014 #include <QPainter>
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <Plasma/Containment>
00024 #include <Plasma/FrameSvg>
00025 #include <Plasma/Wallpaper>
00026 #include <Plasma/View>
00027
00028
00029 #include "plasmaapp.h"
00030 #include "wallpaperpreview.h"
00031
00032 typedef QPair<QString, QString> WallpaperInfo;
00033 Q_DECLARE_METATYPE(WallpaperInfo)
00034
00035
00036 BackgroundDialog::BackgroundDialog(const QSize& res, Plasma::Containment *c, Plasma::View* view, QWidget* parent)
00037 : KDialog(parent),
00038 m_wallpaper(0),
00039 m_view(view),
00040 m_containment(c),
00041 m_preview(0)
00042 {
00043
00044 setCaption(i18n("Background Settings"));
00045 showButtonSeparator(true);
00046 setButtons(Ok | Cancel | Apply);
00047
00048 QWidget * main = new QWidget(this);
00049 setupUi(main);
00050
00051
00052
00053 qreal previewRatio = (qreal)res.height() / (qreal)res.width();
00054 QSize monitorSize(200, int(200 * previewRatio));
00055
00056 Plasma::FrameSvg *svg = new Plasma::FrameSvg(this);
00057 svg->setImagePath("widgets/monitor");
00058 svg->resizeFrame(monitorSize);
00059 QPixmap monitorPix(monitorSize + QSize(0, svg->elementSize("base").height() - svg->marginSize(Plasma::BottomMargin)));
00060 monitorPix.fill(Qt::transparent);
00061
00062 QPainter painter(&monitorPix);
00063 QPoint standPosition(monitorSize.width()/2 - svg->elementSize("base").width()/2, svg->contentsRect().bottom());
00064 svg->paint(&painter, QRect(standPosition, svg->elementSize("base")), "base");
00065 svg->paintFrame(&painter);
00066 painter.end();
00067
00068 m_monitor->setPixmap(monitorPix);
00069 m_monitor->setWhatsThis(i18n(
00070 "This picture of a monitor contains a preview of "
00071 "what the current settings will look like on your desktop."));
00072 m_preview = new WallpaperPreview(m_monitor);
00073 m_preview->setGeometry(svg->contentsRect().toRect());
00074
00075 connect(this, SIGNAL(finished(int)), this, SLOT(cleanup()));
00076 connect(this, SIGNAL(okClicked()), this, SLOT(saveConfig()));
00077 connect(this, SIGNAL(applyClicked()), this, SLOT(saveConfig()));
00078
00079 setMainWidget(main);
00080 reloadConfig();
00081 adjustSize();
00082 }
00083
00084 BackgroundDialog::~BackgroundDialog()
00085 {
00086 cleanup();
00087 }
00088
00089 void BackgroundDialog::cleanup()
00090 {
00091
00092 delete m_wallpaper;
00093 m_wallpaper = 0;
00094 }
00095
00096 void BackgroundDialog::reloadConfig()
00097 {
00098
00099 m_activeSlider->setValue(PlasmaApp::self()->activeOpacity() * 10);
00100 m_idleSlider->setValue(PlasmaApp::self()->idleOpacity() * 10);
00101
00102 label->setVisible(PlasmaApp::hasComposite());
00103 m_activeSlider->setVisible(PlasmaApp::hasComposite());
00104
00105
00106 disconnect(m_wallpaperMode, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBackgroundMode(int)));
00107 int wallpaperIndex = 0;
00108
00109 bool doWallpaper = m_containment->drawWallpaper() && ! PlasmaApp::hasComposite();
00110 m_wallpaperLabel->setVisible(doWallpaper);
00111 m_wallpaperTypeLabel->setVisible(doWallpaper);
00112 m_wallpaperMode->setVisible(doWallpaper);
00113 m_wallpaperConfig->setVisible(doWallpaper);
00114 m_monitor->setVisible(doWallpaper);
00115 m_preview->setVisible(doWallpaper);
00116 if (doWallpaper) {
00117
00118 QString currentPlugin;
00119 QString currentMode;
00120
00121 Plasma::Wallpaper *currentWallpaper = m_containment->wallpaper();
00122 if (currentWallpaper) {
00123 currentPlugin = currentWallpaper->pluginName();
00124 currentMode = currentWallpaper->renderingMode().name();
00125 }
00126
00127 const KPluginInfo::List plugins = Plasma::Wallpaper::listWallpaperInfo();
00128 m_wallpaperMode->clear();
00129 int i = 0;
00130 QString placeholder = i18n("No Wallpaper");
00131
00132
00133 foreach (const KPluginInfo& info, plugins) {
00134 bool matches = info.pluginName() == currentPlugin;
00135 const QList<KServiceAction>& modes = info.service()->actions();
00136 if (modes.count() > 0) {
00137 foreach (const KServiceAction& mode, modes) {
00138 m_wallpaperMode->addItem(KIcon(mode.icon()), mode.text(),
00139 QVariant::fromValue(WallpaperInfo(info.pluginName(), mode.name())));
00140 if (matches && mode.name() == currentMode) {
00141 wallpaperIndex = i;
00142 }
00143 ++i;
00144 }
00145 } else {
00146 m_wallpaperMode->addItem(KIcon(info.icon()), info.name(),
00147 QVariant::fromValue(WallpaperInfo(info.pluginName(), QString())));
00148 if (matches) {
00149 wallpaperIndex = i;
00150 }
00151 ++i;
00152 }
00153 }
00154 m_wallpaperMode->setCurrentIndex(wallpaperIndex);
00155 changeBackgroundMode(wallpaperIndex);
00156 }
00157
00158 connect(m_wallpaperMode, SIGNAL(currentIndexChanged(int)), this, SLOT(changeBackgroundMode(int)));
00159 }
00160
00161 void BackgroundDialog::changeBackgroundMode(int mode)
00162 {
00163 kDebug();
00164 QWidget* w = 0;
00165 WallpaperInfo wallpaperInfo = m_wallpaperMode->itemData(mode).value<WallpaperInfo>();
00166
00167 if (!m_wallpaperConfig->layout()) {
00168 new QVBoxLayout(m_wallpaperConfig);
00169 }
00170
00171 if (m_wallpaperConfig->layout()->count() > 0) {
00172 delete dynamic_cast<QWidgetItem*>(m_wallpaperConfig->layout()->takeAt(0))->widget();
00173 }
00174
00175 if (m_wallpaper && m_wallpaper->pluginName() != wallpaperInfo.first) {
00176 delete m_wallpaper;
00177 m_wallpaper = 0;
00178 }
00179
00180 if (wallpaperInfo.first.isEmpty()) {
00181 return;
00182 }
00183
00184 if (!m_wallpaper) {
00185 m_wallpaper = Plasma::Wallpaper::load(wallpaperInfo.first);
00186 m_preview->setWallpaper(m_wallpaper);
00187 }
00188
00189 if (m_wallpaper) {
00190 m_wallpaper->setRenderingMode(wallpaperInfo.second);
00191 KConfigGroup cfg = wallpaperConfig(wallpaperInfo.first);
00192 kDebug() << "making a" << wallpaperInfo.first << "in mode" << wallpaperInfo.second;
00193 m_wallpaper->restore(cfg);
00194 w = m_wallpaper->createConfigurationInterface(m_wallpaperConfig);
00195 }
00196
00197 if (!w) {
00198 w = new QWidget(m_wallpaperConfig);
00199 }
00200
00201 m_wallpaperConfig->layout()->addWidget(w);
00202 }
00203
00204 KConfigGroup BackgroundDialog::wallpaperConfig(const QString &plugin)
00205 {
00206 Q_ASSERT(m_containment);
00207
00208
00209 KConfigGroup cfg = m_containment->config();
00210 cfg = KConfigGroup(&cfg, "Wallpaper");
00211 return KConfigGroup(&cfg, plugin);
00212 }
00213
00214 void BackgroundDialog::saveConfig()
00215 {
00216
00217 PlasmaApp::self()->setActiveOpacity(m_activeSlider->value() / 10.0);
00218 PlasmaApp::self()->setIdleOpacity(m_idleSlider->value() / 10.0);
00219
00220
00221 QString wallpaperPlugin = m_wallpaperMode->itemData(m_wallpaperMode->currentIndex()).value<WallpaperInfo>().first;
00222 QString wallpaperMode = m_wallpaperMode->itemData(m_wallpaperMode->currentIndex()).value<WallpaperInfo>().second;
00223
00224 Plasma::Wallpaper *currentWallpaper = m_containment->wallpaper();
00225 if (currentWallpaper) {
00226 KConfigGroup cfg = wallpaperConfig(currentWallpaper->pluginName());
00227 currentWallpaper->save(cfg);
00228 }
00229
00230 if (m_wallpaper) {
00231 KConfigGroup cfg = wallpaperConfig(m_wallpaper->pluginName());
00232 m_wallpaper->save(cfg);
00233 }
00234
00235 m_containment->setWallpaper(wallpaperPlugin, wallpaperMode);
00236 }