• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

backgrounddialog.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
00003   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
00004   Copyright (c) 2008 by Petri Damsten <damu@iki.fi>
00005 
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010 */
00011 
00012 #include "backgrounddialog.h"
00013 
00014 #include <QPainter>
00015 //#include <QFile>
00016 //#include <QAbstractItemView>
00017 //#include <QStandardItemModel>
00018 
00019 //#include <KStandardDirs>
00020 //#include <KDesktopFile>
00021 //#include <KColorScheme>
00022 
00023 #include <Plasma/Containment>
00024 #include <Plasma/FrameSvg>
00025 #include <Plasma/Wallpaper>
00026 #include <Plasma/View>
00027 //#include <Plasma/Corona>
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     //setWindowIcon(KIcon("preferences-desktop-wallpaper"));
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     // Size of monitor image: 200x186
00052     // Geometry of "display" part of monitor image: (23,14)-[151x115]
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     //FIXME could be bad if we get hidden and reshown
00092     delete m_wallpaper;
00093     m_wallpaper = 0;
00094 }
00095 
00096 void BackgroundDialog::reloadConfig()
00097 {
00098     //transparency
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     // Wallpaper
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         // Load wallpaper plugins
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         //m_wallpaperMode->addItem(KIcon(), i18n("No Wallpaper"),
00132         //                         QVariant::fromValue(WallpaperInfo(QString(), QString())));
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     //FIXME: we have details about the structure of the containment config duplicated here!
00209     KConfigGroup cfg = m_containment->config();
00210     cfg = KConfigGroup(&cfg, "Wallpaper");
00211     return KConfigGroup(&cfg, plugin);
00212 }
00213 
00214 void BackgroundDialog::saveConfig()
00215 {
00216     //transparency
00217     PlasmaApp::self()->setActiveOpacity(m_activeSlider->value() / 10.0);
00218     PlasmaApp::self()->setIdleOpacity(m_idleSlider->value() / 10.0);
00219 
00220     // Wallpaper
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 }

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal