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

Applets

applet.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 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 version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "applet.h"
00020 #include <Plasma/DataEngine>
00021 #include <Plasma/Containment>
00022 #include <Plasma/Frame>
00023 #include <Plasma/IconWidget>
00024 #include <KIcon>
00025 #include <KDebug>
00026 #include <QGraphicsLinearLayout>
00027 
00028 namespace SM {
00029 
00030 QHash< QString, QList<uint> > Applet::s_configIds;
00031 
00032 Applet::Applet(QObject *parent, const QVariantList &args)
00033    : Plasma::Applet(parent, args),
00034      m_interval(10000),
00035      m_preferredItemHeight(42),
00036      m_titleSpacer(false),
00037      m_header(0),
00038      m_engine(0),
00039      m_ratioOrientation(Qt::Vertical),
00040      m_orientation(Qt::Vertical),
00041      m_noSourcesIcon(0),
00042      m_mode(Desktop),
00043      m_detail(Low),
00044      m_minimumWidth(DEFAULT_MINIMUM_WIDTH),
00045      m_mainLayout(0),
00046      m_configId(0)
00047 {
00048     if (args.count() > 0) {
00049         if (args[0].toString() == "SM") {
00050             m_mode = Monitor;
00051         }
00052     }
00053     QString name = pluginName();
00054 
00055     while (s_configIds[name].contains(m_configId)) {
00056         ++m_configId;
00057     }
00058     s_configIds[name] << m_configId;
00059 }
00060 
00061 Applet::~Applet()
00062 {
00063     s_configIds[pluginName()].removeAll(m_configId);
00064     deleteMeters();
00065 }
00066 
00067 KConfigGroup Applet::persistentConfig() const
00068 {
00069     KConfigGroup cg = globalConfig();
00070     return KConfigGroup(cg.config(), QString("General_%1").arg(m_configId));
00071 }
00072 
00073 void Applet::constraintsEvent(Plasma::Constraints constraints)
00074 {
00075     if (constraints & Plasma::FormFactorConstraint) {
00076         if (m_mode == Monitor) {
00077             setBackgroundHints(NoBackground);
00078             m_orientation = Qt::Vertical;
00079         } else {
00080             SM::Applet::Mode mode = m_mode;
00081             switch (formFactor()) {
00082                 case Plasma::Planar:
00083                 case Plasma::MediaCenter:
00084                     mode = Desktop;
00085                     m_orientation = Qt::Vertical;
00086                     break;
00087                 case Plasma::Horizontal:
00088                     mode = Panel;
00089                     m_orientation = Qt::Horizontal;
00090                     break;
00091                 case Plasma::Vertical:
00092                     mode = Panel;
00093                     m_orientation = Qt::Vertical;
00094                     break;
00095             }
00096             if (mode != m_mode) {
00097                 m_mode = mode;
00098                 m_ratioOrientation = m_orientation;
00099                 connectToEngine();
00100             }
00101         }
00102     } else if (constraints & Plasma::SizeConstraint) {
00103         Detail detail;
00104         if (size().width() > 250 && size().height() / m_items.count() > 150) {
00105             detail = High;
00106         } else {
00107             detail = Low;
00108         }
00109         if (m_detail != detail && m_mode != Monitor) {
00110             m_detail = detail;
00111             setDetail(m_detail);
00112         }
00113         if (m_keepRatio.count() > 0) {
00114             foreach (QGraphicsWidget* item, m_keepRatio) {
00115                 QSizeF size = QSizeF(qMin(item->size().width(), contentsRect().size().width()),
00116                                      qMin(item->size().height(), contentsRect().size().height()));
00117 
00118                 if (size == QSizeF(0, 0)) {
00119                     continue;
00120                 }
00121                 qreal ratio = item->preferredSize().height() / item->preferredSize().width();
00122                 if (m_ratioOrientation == Qt::Vertical) {
00123                     size = QSizeF(size.width(), size.width() * ratio);
00124                 } else {
00125                     size = QSizeF(size.height() * (1.0 / ratio), size.height());
00126                 }
00127                 item->setPreferredSize(size);
00128                 if (m_mode == Panel) {
00129                     item->setMaximumSize(size);
00130                     item->setMinimumSize(size);
00131                 }
00132             }
00133             for (int i = mainLayout()->count() - 1; i >= 0; --i) {
00134                 QGraphicsLayoutItem* item = mainLayout()->itemAt(i);
00135                 if (item) {
00136                     QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
00137                     if (l) {
00138                         l->invalidate();
00139                     }
00140                 }
00141             }
00142         }
00143     }
00144 }
00145 
00146 void Applet::setDetail(Detail detail)
00147 {
00148     Q_UNUSED(detail);
00149 }
00150 
00151 void Applet::setTitle(const QString& title, bool spacer)
00152 {
00153     m_title = title;
00154     m_titleSpacer = spacer;
00155     if (m_header) {
00156         m_header->setText(m_title);
00157     }
00158 }
00159 
00160 QGraphicsLinearLayout* Applet::mainLayout()
00161 {
00162    if (!m_mainLayout) {
00163       m_mainLayout = new QGraphicsLinearLayout(m_orientation);
00164       m_mainLayout->setContentsMargins(0, 0, 0, 0);
00165       m_mainLayout->setSpacing(0);
00166       setLayout(m_mainLayout);
00167    }
00168    return m_mainLayout;
00169 }
00170 
00171 void Applet::connectToEngine()
00172 {
00173     deleteMeters();
00174     disconnectSources();
00175 
00176     mainLayout()->setOrientation(m_orientation);
00177     if (m_mode != Panel) {
00178         m_header = new Plasma::Frame(this);
00179         m_header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
00180         m_header->setText(m_title);
00181         mainLayout()->addItem(m_header);
00182     }
00183     if (m_items.count() == 0){
00184         displayNoAvailableSources();
00185         return;
00186     }
00187     foreach (const QString &item, m_items) {
00188         if (addMeter(item)) {
00189             connectSource(item);
00190         }
00191     }
00192     if (m_titleSpacer) {
00193         mainLayout()->addStretch();
00194     }
00195     checkGeometry();
00196     mainLayout()->activate();
00197     constraintsEvent(Plasma::SizeConstraint);
00198     setDetail(m_detail);
00199 }
00200 
00201 void Applet::checkGeometry()
00202 {
00203     if (m_mode != Panel) {
00204         QSizeF minSize;
00205         qreal height = 0;
00206 
00207         if (m_header) {
00208             height = m_header->minimumSize().height();
00209         }
00210         m_min.setHeight(height + m_items.count() * m_preferredItemHeight);
00211         m_min.setWidth(m_minimumWidth);
00212         if (m_mode != Monitor) {
00213             m_max = QSizeF();
00214             m_min += size() - contentsRect().size();
00215         } else {
00216             // Reset margins
00217             setBackgroundHints(NoBackground);
00218             m_max = QSizeF(QSizeF().width(), minSize.height());
00219         }
00220         //kDebug() << minSize << m_preferredItemHeight << height
00221         //         << m_minimumHeight << metaObject()->className();
00222         m_pref = m_min;
00223 
00224         setAspectRatioMode(Plasma::IgnoreAspectRatio);
00225         setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00226         update();
00227     } else {
00228         int x = 1;
00229         int y = 1;
00230         QSizeF size = containment()->size();
00231         qreal s;
00232 
00233         if (m_orientation == Qt::Horizontal) {
00234             x = m_items.count();
00235             s = size.height();
00236         } else {
00237             y = m_items.count();
00238             s = size.width();
00239         }
00240         m_min = QSizeF(16 * x, 16 * y);
00241         m_max = m_pref = QSizeF(s * x, s * y);
00242         setAspectRatioMode(Plasma::KeepAspectRatio);
00243         setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00244     }
00245     setMinimumSize(m_min);
00246     setPreferredSize(m_pref);
00247     setMaximumSize(m_max);
00248     //kDebug() << m_min << m_pref << m_max << metaObject()->className();
00249     emit geometryChecked();
00250 }
00251 
00252 void Applet::connectSource(const QString& source)
00253 {
00254    if (m_engine) {
00255       m_engine->connectSource(source, this, m_interval);
00256       m_connectedSources << source;
00257    }
00258 }
00259 
00260 void Applet::disconnectSources()
00261 {
00262    Plasma::DataEngine *engine = dataEngine("soliddevice");
00263    if (engine) {
00264       foreach (const QString &source, m_connectedSources) {
00265          engine->disconnectSource(source, this);
00266       }
00267    }
00268    m_connectedSources.clear();
00269 }
00270 
00271 void Applet::deleteMeters(QGraphicsLinearLayout* layout)
00272 {
00273     if (!layout) {
00274         layout = mainLayout();
00275         m_meters.clear();
00276         m_plotters.clear();
00277         m_keepRatio.clear();
00278         m_header = 0;
00279     }
00280     for (int i = layout->count() - 1; i >= 0; --i) {
00281         QGraphicsLayoutItem* item = layout->itemAt(i);
00282         if (item) {
00283             QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
00284             if (l) {
00285                 deleteMeters(l);
00286             }
00287         }
00288         layout->removeAt(i);
00289         delete item;
00290     }
00291 }
00292 
00293 void Applet::displayNoAvailableSources()
00294 {
00295     KIcon appletIcon(icon());
00296     m_noSourcesIcon = new Plasma::IconWidget(appletIcon, "", this);
00297     mainLayout()->addItem(m_noSourcesIcon);
00298 }
00299 
00300 /*
00301 QSizeF Applet::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
00302 {
00303     QSizeF result;
00304     Q_UNUSED(constraint)
00305     switch (which) {
00306         case Qt::MinimumSize:
00307             result = m_min;
00308             break;
00309         case Qt::MaximumSize:
00310             result = m_max;
00311             break;
00312         case Qt::PreferredSize:
00313         default:
00314             result = m_pref;
00315             break;
00316     }
00317     kDebug() << which << result;
00318     return result;
00319 }
00320 */
00321 }

Applets

Skip menu "Applets"
  • 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