not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / system-monitor / applet.cpp
blobd42957a9101134d836ff112d8efaca4d7b42dadc
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 "applet.h"
20 #include <Plasma/DataEngine>
21 #include <Plasma/Containment>
22 #include <Plasma/Frame>
23 #include <Plasma/IconWidget>
24 #include <KIcon>
25 #include <KDebug>
26 #include <QGraphicsLinearLayout>
28 namespace SM {
30 QHash< QString, QList<uint> > Applet::s_configIds;
32 Applet::Applet(QObject *parent, const QVariantList &args)
33 : Plasma::Applet(parent, args),
34 m_interval(10000),
35 m_preferredItemHeight(42),
36 m_titleSpacer(false),
37 m_header(0),
38 m_engine(0),
39 m_ratioOrientation(Qt::Vertical),
40 m_orientation(Qt::Vertical),
41 m_noSourcesIcon(0),
42 m_mode(Desktop),
43 m_detail(Low),
44 m_minimumWidth(DEFAULT_MINIMUM_WIDTH),
45 m_mainLayout(0),
46 m_configId(0)
48 if (args.count() > 0) {
49 if (args[0].toString() == "SM") {
50 m_mode = Monitor;
53 QString name = pluginName();
55 while (s_configIds[name].contains(m_configId)) {
56 ++m_configId;
58 s_configIds[name] << m_configId;
61 Applet::~Applet()
63 s_configIds[pluginName()].removeAll(m_configId);
64 deleteMeters();
67 KConfigGroup Applet::persistentConfig() const
69 KConfigGroup cg = globalConfig();
70 return KConfigGroup(cg.config(), QString("General_%1").arg(m_configId));
73 void Applet::constraintsEvent(Plasma::Constraints constraints)
75 if (constraints & Plasma::FormFactorConstraint) {
76 if (m_mode == Monitor) {
77 setBackgroundHints(NoBackground);
78 m_orientation = Qt::Vertical;
79 } else {
80 SM::Applet::Mode mode = m_mode;
81 switch (formFactor()) {
82 case Plasma::Planar:
83 case Plasma::MediaCenter:
84 mode = Desktop;
85 m_orientation = Qt::Vertical;
86 break;
87 case Plasma::Horizontal:
88 mode = Panel;
89 m_orientation = Qt::Horizontal;
90 break;
91 case Plasma::Vertical:
92 mode = Panel;
93 m_orientation = Qt::Vertical;
94 break;
96 if (mode != m_mode) {
97 m_mode = mode;
98 m_ratioOrientation = m_orientation;
99 connectToEngine();
102 } else if (constraints & Plasma::SizeConstraint) {
103 Detail detail;
104 if (size().width() > 250 && size().height() / m_items.count() > 150) {
105 detail = High;
106 } else {
107 detail = Low;
109 if (m_detail != detail && m_mode != Monitor) {
110 m_detail = detail;
111 setDetail(m_detail);
113 if (m_keepRatio.count() > 0) {
114 foreach (QGraphicsWidget* item, m_keepRatio) {
115 QSizeF size = QSizeF(qMin(item->size().width(), contentsRect().size().width()),
116 qMin(item->size().height(), contentsRect().size().height()));
118 if (size == QSizeF(0, 0)) {
119 continue;
121 qreal ratio = item->preferredSize().height() / item->preferredSize().width();
122 if (m_ratioOrientation == Qt::Vertical) {
123 size = QSizeF(size.width(), size.width() * ratio);
124 } else {
125 size = QSizeF(size.height() * (1.0 / ratio), size.height());
127 item->setPreferredSize(size);
128 if (m_mode == Panel) {
129 item->setMaximumSize(size);
130 item->setMinimumSize(size);
133 for (int i = mainLayout()->count() - 1; i >= 0; --i) {
134 QGraphicsLayoutItem* item = mainLayout()->itemAt(i);
135 if (item) {
136 QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
137 if (l) {
138 l->invalidate();
146 void Applet::setDetail(Detail detail)
148 Q_UNUSED(detail);
151 void Applet::setTitle(const QString& title, bool spacer)
153 m_title = title;
154 m_titleSpacer = spacer;
155 if (m_header) {
156 m_header->setText(m_title);
160 QGraphicsLinearLayout* Applet::mainLayout()
162 if (!m_mainLayout) {
163 m_mainLayout = new QGraphicsLinearLayout(m_orientation);
164 m_mainLayout->setContentsMargins(0, 0, 0, 0);
165 m_mainLayout->setSpacing(0);
166 setLayout(m_mainLayout);
168 return m_mainLayout;
171 void Applet::connectToEngine()
173 deleteMeters();
174 disconnectSources();
176 mainLayout()->setOrientation(m_orientation);
177 if (m_mode != Panel) {
178 m_header = new Plasma::Frame(this);
179 m_header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
180 m_header->setText(m_title);
181 mainLayout()->addItem(m_header);
183 if (m_items.count() == 0){
184 displayNoAvailableSources();
185 return;
187 foreach (const QString &item, m_items) {
188 if (addMeter(item)) {
189 connectSource(item);
192 if (m_titleSpacer) {
193 mainLayout()->addStretch();
195 checkGeometry();
196 mainLayout()->activate();
197 constraintsEvent(Plasma::SizeConstraint);
198 setDetail(m_detail);
201 void Applet::checkGeometry()
203 if (m_mode != Panel) {
204 QSizeF minSize;
205 qreal height = 0;
207 if (m_header) {
208 height = m_header->minimumSize().height();
210 m_min.setHeight(height + m_items.count() * m_preferredItemHeight);
211 m_min.setWidth(m_minimumWidth);
212 if (m_mode != Monitor) {
213 m_max = QSizeF();
214 m_min += size() - contentsRect().size();
215 } else {
216 // Reset margins
217 setBackgroundHints(NoBackground);
218 m_max = QSizeF(QSizeF().width(), minSize.height());
220 //kDebug() << minSize << m_preferredItemHeight << height
221 // << m_minimumHeight << metaObject()->className();
222 m_pref = m_min;
224 setAspectRatioMode(Plasma::IgnoreAspectRatio);
225 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
226 update();
227 } else {
228 int x = 1;
229 int y = 1;
230 QSizeF size = containment()->size();
231 qreal s;
233 if (m_orientation == Qt::Horizontal) {
234 x = m_items.count();
235 s = size.height();
236 } else {
237 y = m_items.count();
238 s = size.width();
240 m_min = QSizeF(16 * x, 16 * y);
241 m_max = m_pref = QSizeF(s * x, s * y);
242 setAspectRatioMode(Plasma::KeepAspectRatio);
243 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
245 setMinimumSize(m_min);
246 setPreferredSize(m_pref);
247 setMaximumSize(m_max);
248 //kDebug() << m_min << m_pref << m_max << metaObject()->className();
249 emit geometryChecked();
252 void Applet::connectSource(const QString& source)
254 if (m_engine) {
255 m_engine->connectSource(source, this, m_interval);
256 m_connectedSources << source;
260 void Applet::disconnectSources()
262 Plasma::DataEngine *engine = dataEngine("soliddevice");
263 if (engine) {
264 foreach (const QString &source, m_connectedSources) {
265 engine->disconnectSource(source, this);
268 m_connectedSources.clear();
271 void Applet::deleteMeters(QGraphicsLinearLayout* layout)
273 if (!layout) {
274 layout = mainLayout();
275 m_meters.clear();
276 m_plotters.clear();
277 m_keepRatio.clear();
278 m_header = 0;
280 for (int i = layout->count() - 1; i >= 0; --i) {
281 QGraphicsLayoutItem* item = layout->itemAt(i);
282 if (item) {
283 QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
284 if (l) {
285 deleteMeters(l);
288 layout->removeAt(i);
289 delete item;
293 void Applet::displayNoAvailableSources()
295 KIcon appletIcon(icon());
296 m_noSourcesIcon = new Plasma::IconWidget(appletIcon, "", this);
297 mainLayout()->addItem(m_noSourcesIcon);
301 QSizeF Applet::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
303 QSizeF result;
304 Q_UNUSED(constraint)
305 switch (which) {
306 case Qt::MinimumSize:
307 result = m_min;
308 break;
309 case Qt::MaximumSize:
310 result = m_max;
311 break;
312 case Qt::PreferredSize:
313 default:
314 result = m_pref;
315 break;
317 kDebug() << which << result;
318 return result;