add more spacing
[personal-kdebase.git] / workspace / plasma / applets / system-monitor / system-monitor.cpp
blob82dc07214fc8b0167c607d74ac36e024df810d46
1 /*
2 * Copyright (C) 2007 Petri Damsten <damu@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "system-monitor.h"
20 #include "monitorbutton.h"
21 #include "applet.h"
22 #include <QTimer>
23 #include <QGraphicsLinearLayout>
24 #include <KPushButton>
25 #include <Plasma/Containment>
26 #include <Plasma/Corona>
28 #define APPLETS 5
29 static const char *sm_applets[][2] = {
30 { "media-flash", "sm_temperature" },
31 //{ "media-flash", "sm_ram" },
32 { "cpu", "sm_cpu" },
33 { "hwinfo", "sm_hwinfo" },
34 { "network-workgroup", "sm_net" },
35 { "drive-harddisk", "sm_hdd" }
38 SystemMonitor::SystemMonitor(QObject *parent, const QVariantList &args)
39 : Plasma::PopupApplet(parent, args), m_layout(0), m_buttons(0), m_widget(0)
41 resize(234 + 20 + 23, 80 + 20 + 25);
42 setMinimumSize(QSize(234 + 20 + 23, 32 + 20 + 25));
43 setAspectRatioMode(Plasma::IgnoreAspectRatio);
46 SystemMonitor::~SystemMonitor()
48 QStringList appletNames;
49 foreach (Plasma::Applet *applet, m_applets) {
50 appletNames << applet->objectName();
51 applet->destroy();
54 KConfigGroup cg = config();
55 cg.writeEntry("applets", appletNames);
58 void SystemMonitor::init()
60 KConfigGroup cg = config();
61 QStringList appletNames = cg.readEntry("applets", QStringList());
63 m_widget = new QGraphicsWidget(this);
64 m_layout = new QGraphicsLinearLayout(Qt::Vertical);
65 m_layout->setContentsMargins(0, 0, 0, 0);
66 m_buttons = new QGraphicsLinearLayout(Qt::Horizontal);
67 m_buttons->setContentsMargins(0, 0, 0, 0);
68 m_buttons->setSpacing(5);
70 for (int i = 0; i < APPLETS; ++i) {
71 MonitorButton *button = new MonitorButton(m_widget);
72 button->nativeWidget()->setObjectName(sm_applets[i][1]);
73 button->nativeWidget()->setCheckable(true);
74 button->setImage(sm_applets[i][0]);
75 if (appletNames.contains(sm_applets[i][1])) {
76 button->nativeWidget()->setChecked(true);
78 connect(button->nativeWidget(), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
79 m_buttons->addItem(button);
81 m_layout->addItem(m_buttons);
82 foreach (const QString& applet, appletNames) {
83 addApplet(applet);
85 m_widget->setLayout(m_layout);
86 checkGeometry();
88 m_widget->setMinimumSize(QSize(234 + 20 + 23, 32 + 20 + 25));
89 setPopupIcon("utilities-system-monitor");
92 void SystemMonitor::toggled(bool toggled)
94 removeApplet(sender()->objectName());
95 if (toggled) {
96 addApplet(sender()->objectName());
100 void SystemMonitor::addApplet(const QString &name)
102 kDebug() << "";
103 if (name.isEmpty()) {
104 return;
106 SM::Applet* applet = qobject_cast<SM::Applet*>(Plasma::Applet::load(name, 0, QVariantList() << "SM"));
107 if (applet) {
108 m_applets.append(applet);
109 connect(applet, SIGNAL(geometryChecked()), this, SLOT(checkGeometry()));
110 connect(applet, SIGNAL(destroyed(QObject*)), this, SLOT(appletRemoved(QObject*)));
111 applet->setFlag(QGraphicsItem::ItemIsMovable, false);
112 applet->init();
113 applet->setBackgroundHints(Plasma::Applet::NoBackground);
114 applet->setParentItem(m_widget);
115 applet->setObjectName(name);
116 m_layout->addItem(applet);
117 //checkGeometry(applet->preferredSize().height());
121 void SystemMonitor::removeApplet(const QString &name)
123 foreach (SM::Applet *applet, m_applets) {
124 if (applet->objectName() == name) {
125 applet->destroy();
130 void SystemMonitor::appletRemoved(QObject *object)
132 SM::Applet *applet = static_cast<SM::Applet*>(object);
134 foreach (SM::Applet *a, m_applets) {
135 if (a == applet) {
136 m_layout->removeItem(applet);
137 m_applets.removeAll(applet);
138 checkGeometry();
143 void SystemMonitor::checkGeometry()
145 QSizeF margins = size() - contentsRect().size();
146 qreal minHeight = 32 + 20 + 25; // m_buttons->minimumHeight();
147 //kDebug() << minHeight;
149 foreach (SM::Applet *applet, m_applets) {
150 //kDebug() << applet->minSize() << applet->minimumSize()
151 // << applet->metaObject()->className() << applet->size() - applet->contentsRect().size();
152 minHeight += applet->minSize().height() + m_layout->spacing();
154 m_widget->setMinimumSize(DEFAULT_MINIMUM_WIDTH, minHeight);
156 QSizeF s(m_widget->size().width(), minHeight);
157 if (m_applets.count() == 0) {
158 // I want to be sure...
159 s.setHeight(minHeight);
162 if (formFactor() != Plasma::Horizontal && formFactor() != Plasma::Vertical) {
163 setMinimumSize(m_widget->minimumSize() + margins);
165 resize(s + margins);
166 m_widget->resize(s);
167 update();
169 kDebug() << m_widget->size().height() << m_layout->geometry().height();
170 foreach (SM::Applet *applet, m_applets) {
171 kDebug() << applet->metaObject()->className() << applet->size().height();
173 for (int i = 0; i < m_layout->count(); ++i) {
174 kDebug() << m_layout->itemAt(i)->geometry().top() << m_layout->itemAt(i)->geometry().height();
179 QList<QAction*> SystemMonitor::contextualActions()
181 QList<QAction*> result;
182 foreach (Plasma::Applet *applet, m_applets) {
183 result << applet->action("configure");
185 return result;
188 QGraphicsWidget *SystemMonitor::graphicsWidget()
190 return m_widget;
193 #include "system-monitor.moc"