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"
23 #include <QGraphicsLinearLayout>
24 #include <KPushButton>
25 #include <Plasma/Containment>
26 #include <Plasma/Corona>
29 static const char *sm_applets
[][2] = {
30 { "media-flash", "sm_temperature" },
31 //{ "media-flash", "sm_ram" },
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();
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
) {
85 m_widget
->setLayout(m_layout
);
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());
96 addApplet(sender()->objectName());
100 void SystemMonitor::addApplet(const QString
&name
)
103 if (name
.isEmpty()) {
106 SM::Applet
* applet
= qobject_cast
<SM::Applet
*>(Plasma::Applet::load(name
, 0, QVariantList() << "SM"));
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);
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
) {
130 void SystemMonitor::appletRemoved(QObject
*object
)
132 SM::Applet
*applet
= static_cast<SM::Applet
*>(object
);
134 foreach (SM::Applet
*a
, m_applets
) {
136 m_layout
->removeItem(applet
);
137 m_applets
.removeAll(applet
);
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
);
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");
188 QGraphicsWidget
*SystemMonitor::graphicsWidget()
193 #include "system-monitor.moc"