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

Applets

system-monitor.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 "system-monitor.h"
00020 #include "monitorbutton.h"
00021 #include "applet.h"
00022 #include <QTimer>
00023 #include <QGraphicsLinearLayout>
00024 #include <KPushButton>
00025 #include <Plasma/Containment>
00026 #include <Plasma/Corona>
00027 
00028 #define APPLETS 5
00029 static const char *sm_applets[][2] = {
00030     { "media-flash", "sm_temperature" },
00031     //{ "media-flash", "sm_ram" },
00032     { "cpu", "sm_cpu" },
00033     { "hwinfo", "sm_hwinfo" },
00034     { "network-workgroup", "sm_net" },
00035     { "drive-harddisk", "sm_hdd" }
00036 };
00037 
00038 SystemMonitor::SystemMonitor(QObject *parent, const QVariantList &args)
00039     : Plasma::PopupApplet(parent, args), m_layout(0), m_buttons(0), m_widget(0)
00040 {
00041     resize(234 + 20 + 23, 80 + 20 + 25);
00042     setMinimumSize(QSize(234 + 20 + 23, 32 + 20 + 25));
00043     setAspectRatioMode(Plasma::IgnoreAspectRatio);
00044 }
00045 
00046 SystemMonitor::~SystemMonitor()
00047 {
00048     QStringList appletNames;
00049     foreach (Plasma::Applet *applet, m_applets) {
00050         appletNames << applet->objectName();
00051         applet->destroy();
00052     }
00053 
00054     KConfigGroup cg = config();
00055     cg.writeEntry("applets", appletNames);
00056 }
00057 
00058 void SystemMonitor::init()
00059 {
00060     KConfigGroup cg = config();
00061     QStringList appletNames = cg.readEntry("applets", QStringList());
00062 
00063     m_widget = new QGraphicsWidget(this);
00064     m_layout = new QGraphicsLinearLayout(Qt::Vertical);
00065     m_layout->setContentsMargins(0, 0, 0, 0);
00066     m_buttons = new QGraphicsLinearLayout(Qt::Horizontal);
00067     m_buttons->setContentsMargins(0, 0, 0, 0);
00068     m_buttons->setSpacing(5);
00069 
00070     for (int i = 0; i < APPLETS; ++i) {
00071         MonitorButton *button = new MonitorButton(m_widget);
00072         button->nativeWidget()->setObjectName(sm_applets[i][1]);
00073         button->nativeWidget()->setCheckable(true);
00074         button->setImage(sm_applets[i][0]);
00075         if (appletNames.contains(sm_applets[i][1])) {
00076             button->nativeWidget()->setChecked(true);
00077         }
00078         connect(button->nativeWidget(), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
00079         m_buttons->addItem(button);
00080     }
00081     m_layout->addItem(m_buttons);
00082     foreach (const QString& applet, appletNames) {
00083         addApplet(applet);
00084     }
00085     m_widget->setLayout(m_layout);
00086     checkGeometry();
00087 
00088     m_widget->setMinimumSize(QSize(234 + 20 + 23, 32 + 20 + 25));
00089     setPopupIcon("utilities-system-monitor");
00090 }
00091 
00092 void SystemMonitor::toggled(bool toggled)
00093 {
00094     removeApplet(sender()->objectName());
00095     if (toggled) {
00096         addApplet(sender()->objectName());
00097     }
00098 }
00099 
00100 void SystemMonitor::addApplet(const QString &name)
00101 {
00102     kDebug() << "";
00103     if (name.isEmpty()) {
00104         return;
00105     }
00106     SM::Applet* applet = qobject_cast<SM::Applet*>(Plasma::Applet::load(name, 0, QVariantList() << "SM"));
00107     if (applet) {
00108         m_applets.append(applet);
00109         connect(applet, SIGNAL(geometryChecked()), this, SLOT(checkGeometry()));
00110         connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletRemoved(QObject*)));
00111         applet->setFlag(QGraphicsItem::ItemIsMovable, false);
00112         applet->init();
00113         applet->setBackgroundHints(Plasma::Applet::NoBackground);
00114         applet->setParentItem(m_widget);
00115         applet->setObjectName(name);
00116         m_layout->addItem(applet);
00117         //checkGeometry(applet->preferredSize().height());
00118     }
00119 }
00120 
00121 void SystemMonitor::removeApplet(const QString &name)
00122 {
00123     foreach (SM::Applet *applet, m_applets) {
00124         if (applet->objectName() == name) {
00125             applet->destroy();
00126         }
00127     }
00128 }
00129 
00130 void SystemMonitor::appletRemoved(QObject *object)
00131 {
00132     SM::Applet *applet = static_cast<SM::Applet*>(object);
00133 
00134     foreach (SM::Applet *a, m_applets) {
00135         if (a == applet) {
00136             m_layout->removeItem(applet);
00137             m_applets.removeAll(applet);
00138             checkGeometry();
00139         }
00140     }
00141 }
00142 
00143 void SystemMonitor::checkGeometry()
00144 {
00145     QSizeF margins = size() - contentsRect().size();
00146     qreal minHeight = 32 + 20 + 25; // m_buttons->minimumHeight();
00147     //kDebug() << minHeight;
00148 
00149     foreach (SM::Applet *applet, m_applets) {
00150         //kDebug() << applet->minSize() << applet->minimumSize()
00151         //         << applet->metaObject()->className() << applet->size() - applet->contentsRect().size();
00152         minHeight += applet->minSize().height() + m_layout->spacing();
00153     }
00154     m_widget->setMinimumSize(DEFAULT_MINIMUM_WIDTH, minHeight);
00155 
00156     QSizeF s(m_widget->size().width(), minHeight);
00157     if (m_applets.count() == 0) {
00158         // I want to be sure...
00159         s.setHeight(minHeight);
00160     }
00161 
00162     if (formFactor() != Plasma::Horizontal && formFactor() != Plasma::Vertical) {
00163         setMinimumSize(m_widget->minimumSize() + margins);
00164     }
00165     resize(s + margins);
00166     m_widget->resize(s);
00167     update();
00168     /*
00169     kDebug() << m_widget->size().height() << m_layout->geometry().height();
00170     foreach (SM::Applet *applet, m_applets) {
00171         kDebug() << applet->metaObject()->className() << applet->size().height();
00172     }
00173     for (int i = 0; i < m_layout->count(); ++i) {
00174         kDebug() << m_layout->itemAt(i)->geometry().top() << m_layout->itemAt(i)->geometry().height();
00175     }
00176     */
00177 }
00178 
00179 QList<QAction*> SystemMonitor::contextualActions()
00180 {
00181     QList<QAction*> result;
00182     foreach (Plasma::Applet *applet, m_applets) {
00183         result << applet->action("configure");
00184     }
00185     return result;
00186 }
00187 
00188 QGraphicsWidget *SystemMonitor::graphicsWidget()
00189 {
00190     return m_widget;
00191 }
00192 
00193 #include "system-monitor.moc"

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