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

Plasma

color.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 by Petri Damsten <damu@iki.fi>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "color.h"
00021 
00022 #include <QPainter>
00023 #include <QLinearGradient>
00024 #include <QRadialGradient>
00025 #include <KDebug>
00026 
00027 enum BackgroundMode {
00028     SOLID,
00029     HORIZONTAL,
00030     VERTICAL,
00031     RECTANGULAR,
00032     RADIAL,
00033     TOP_LEFT_DIAGONAL,
00034     TOP_RIGHT_DIAGONAL
00035 };
00036 
00037 Color::Color(QObject *parent, const QVariantList &args)
00038     : Plasma::Wallpaper(parent, args)
00039 {
00040 }
00041 
00042 void Color::init(const KConfigGroup &config)
00043 {
00044     m_color1 = config.readEntry("color1", QColor(Qt::white));
00045     m_color2 = config.readEntry("color2", QColor(Qt::black));
00046     m_backgroundMode = config.readEntry("backgroundMode", (int)SOLID);
00047     emit update(boundingRect());
00048 }
00049 
00050 void Color::paint(QPainter *painter, const QRectF& exposedRect)
00051 {
00052     switch (m_backgroundMode) {
00053     case SOLID: {
00054             painter->fillRect(exposedRect, m_color1);
00055             break;
00056         }
00057 
00058     case HORIZONTAL: {
00059             QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
00060                                                        boundingRect().topRight());
00061             gradient.setColorAt(0, m_color1);
00062             gradient.setColorAt(1, m_color2);
00063             painter->fillRect(exposedRect, gradient);
00064             break;
00065         }
00066 
00067     case VERTICAL: {
00068             QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
00069                                                        boundingRect().bottomLeft());
00070             gradient.setColorAt(0, m_color1);
00071             gradient.setColorAt(1, m_color2);
00072             painter->fillRect(exposedRect, gradient);
00073             break;
00074         }
00075 
00076     case RECTANGULAR: {
00077             // First draw a horizontal gradient covering the whole view/screen
00078             QLinearGradient horizontalGradient = QLinearGradient(boundingRect().topLeft(),
00079                                                                  boundingRect().topRight());
00080             horizontalGradient.setColorAt(0, m_color2);
00081             horizontalGradient.setColorAt(0.5, m_color1);
00082             horizontalGradient.setColorAt(1, m_color2);
00083 
00084             painter->fillRect(exposedRect, horizontalGradient);
00085 
00086             // Then draw two triangles with vertical gradient
00087             QLinearGradient verticalGradient = QLinearGradient(boundingRect().topLeft(),
00088                                                                boundingRect().bottomLeft());
00089             verticalGradient.setColorAt(0, m_color2);
00090             verticalGradient.setColorAt(0.5, m_color1);
00091             verticalGradient.setColorAt(1, m_color2);
00092             painter->setBrush(verticalGradient);
00093             painter->setPen(Qt::NoPen);
00094 
00095             QPolygon triangle = QPolygon(3);
00096 
00097             // Draw a triangle which starts from the top edge to the center
00098             triangle.append(boundingRect().topLeft().toPoint());
00099             triangle.append(boundingRect().topRight().toPoint());
00100             triangle.append(boundingRect().center().toPoint());
00101             painter->drawPolygon(triangle);
00102 
00103             triangle.clear();
00104 
00105             // Draw a triangle which starts from the bottom edge to the center
00106             triangle.append(boundingRect().bottomLeft().toPoint());
00107             triangle.append(boundingRect().bottomRight().toPoint());
00108             triangle.append(boundingRect().center().toPoint());
00109             painter->drawPolygon(triangle);
00110 
00111             break;
00112         }
00113 
00114     case RADIAL: {
00115             // The diameter of the gradient will be the max screen dimension
00116             int maxDimension = qMax(boundingRect().height(), boundingRect().width());
00117 
00118             QRadialGradient gradient = QRadialGradient(boundingRect().center(),
00119                                                        maxDimension / 2,
00120                                                        boundingRect().center());
00121             gradient.setColorAt(0, m_color1);
00122             gradient.setColorAt(1, m_color2);
00123             painter->fillRect(exposedRect, gradient);
00124             break;
00125         }
00126 
00127     case TOP_LEFT_DIAGONAL: {
00128             QLinearGradient gradient = QLinearGradient(boundingRect().topLeft(),
00129                                                        boundingRect().bottomRight());
00130             gradient.setColorAt(0, m_color1);
00131             gradient.setColorAt(1, m_color2);
00132             painter->fillRect(exposedRect, gradient);
00133             break;
00134         }
00135 
00136     case TOP_RIGHT_DIAGONAL: {
00137             QLinearGradient gradient = QLinearGradient(boundingRect().topRight(),
00138                                                        boundingRect().bottomLeft());
00139             gradient.setColorAt(0, m_color1);
00140             gradient.setColorAt(1, m_color2);
00141             painter->fillRect(exposedRect, gradient);
00142             break;
00143         }
00144 
00145     }
00146 }
00147 
00148 QWidget* Color::createConfigurationInterface(QWidget* parent)
00149 {
00150     QWidget *widget = new QWidget(parent);
00151     m_ui.setupUi(widget);
00152 
00153     m_ui.m_color1->setColor(m_color1);
00154     m_ui.m_color2->setColor(m_color2);
00155     m_ui.m_backgroundMode->setCurrentIndex(m_backgroundMode);
00156 
00157     if (m_backgroundMode == SOLID) {
00158         m_ui.m_color2->setEnabled(false);
00159     } else {
00160         m_ui.m_color2->setEnabled(true);
00161     }
00162 
00163     connect(m_ui.m_color1, SIGNAL(changed(const QColor&)), this, SLOT(settingsModified()));
00164     connect(m_ui.m_color2, SIGNAL(changed(const QColor&)), this, SLOT(settingsModified()));
00165     connect(m_ui.m_backgroundMode, SIGNAL(currentIndexChanged(int)), this, SLOT(settingsModified()));
00166 
00167     connect(this, SIGNAL(settingsChanged(bool)), parent, SLOT(settingsChanged(bool)));
00168 
00169     return widget;
00170 }
00171 
00172 void Color::save(KConfigGroup &config)
00173 {
00174     config.writeEntry("color1", m_color1);
00175     config.writeEntry("color2", m_color2);
00176     config.writeEntry("backgroundMode", m_backgroundMode);
00177 }
00178 
00179 void Color::settingsModified()
00180 {
00181     m_color1 = m_ui.m_color1->color();
00182     m_color2 = m_ui.m_color2->color();
00183     m_backgroundMode = m_ui.m_backgroundMode->currentIndex();
00184 
00185     if (m_backgroundMode == SOLID) {
00186         m_ui.m_color2->setEnabled(false);
00187     } else {
00188         m_ui.m_color2->setEnabled(true);
00189     }
00190 
00191     emit settingsChanged(true);
00192     emit update(boundingRect());
00193 }
00194 
00195 #include "color.moc"

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
  • System Settings
  •   SystemSettingsView
Generated for API Reference by doxygen 1.5.9-20090814
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