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.
20 #include <Plasma/DataEngine>
21 #include <Plasma/Containment>
22 #include <Plasma/Frame>
23 #include <Plasma/IconWidget>
26 #include <QGraphicsLinearLayout>
30 QHash
< QString
, QList
<uint
> > Applet::s_configIds
;
32 Applet::Applet(QObject
*parent
, const QVariantList
&args
)
33 : Plasma::Applet(parent
, args
),
35 m_preferredItemHeight(42),
39 m_ratioOrientation(Qt::Vertical
),
40 m_orientation(Qt::Vertical
),
44 m_minimumWidth(DEFAULT_MINIMUM_WIDTH
),
48 if (args
.count() > 0) {
49 if (args
[0].toString() == "SM") {
53 QString name
= pluginName();
55 while (s_configIds
[name
].contains(m_configId
)) {
58 s_configIds
[name
] << m_configId
;
63 s_configIds
[pluginName()].removeAll(m_configId
);
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
;
80 SM::Applet::Mode mode
= m_mode
;
81 switch (formFactor()) {
83 case Plasma::MediaCenter
:
85 m_orientation
= Qt::Vertical
;
87 case Plasma::Horizontal
:
89 m_orientation
= Qt::Horizontal
;
91 case Plasma::Vertical
:
93 m_orientation
= Qt::Vertical
;
98 m_ratioOrientation
= m_orientation
;
102 } else if (constraints
& Plasma::SizeConstraint
) {
104 if (size().width() > 250 && size().height() / m_items
.count() > 150) {
109 if (m_detail
!= detail
&& m_mode
!= Monitor
) {
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)) {
121 qreal ratio
= item
->preferredSize().height() / item
->preferredSize().width();
122 if (m_ratioOrientation
== Qt::Vertical
) {
123 size
= QSizeF(size
.width(), size
.width() * ratio
);
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
);
136 QGraphicsLinearLayout
* l
= dynamic_cast<QGraphicsLinearLayout
*>(item
);
146 void Applet::setDetail(Detail detail
)
151 void Applet::setTitle(const QString
& title
, bool spacer
)
154 m_titleSpacer
= spacer
;
156 m_header
->setText(m_title
);
160 QGraphicsLinearLayout
* Applet::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
);
171 void Applet::connectToEngine()
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();
187 foreach (const QString
&item
, m_items
) {
188 if (addMeter(item
)) {
193 mainLayout()->addStretch();
196 mainLayout()->activate();
197 constraintsEvent(Plasma::SizeConstraint
);
201 void Applet::checkGeometry()
203 if (m_mode
!= Panel
) {
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
) {
214 m_min
+= size() - contentsRect().size();
217 setBackgroundHints(NoBackground
);
218 m_max
= QSizeF(QSizeF().width(), minSize
.height());
220 //kDebug() << minSize << m_preferredItemHeight << height
221 // << m_minimumHeight << metaObject()->className();
224 setAspectRatioMode(Plasma::IgnoreAspectRatio
);
225 setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Expanding
);
230 QSizeF size
= containment()->size();
233 if (m_orientation
== Qt::Horizontal
) {
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
)
255 m_engine
->connectSource(source
, this, m_interval
);
256 m_connectedSources
<< source
;
260 void Applet::disconnectSources()
262 Plasma::DataEngine
*engine
= dataEngine("soliddevice");
264 foreach (const QString
&source
, m_connectedSources
) {
265 engine
->disconnectSource(source
, this);
268 m_connectedSources
.clear();
271 void Applet::deleteMeters(QGraphicsLinearLayout
* layout
)
274 layout
= mainLayout();
280 for (int i
= layout
->count() - 1; i
>= 0; --i
) {
281 QGraphicsLayoutItem
* item
= layout
->itemAt(i
);
283 QGraphicsLinearLayout
* l
= dynamic_cast<QGraphicsLinearLayout
*>(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
306 case Qt::MinimumSize:
309 case Qt::MaximumSize:
312 case Qt::PreferredSize:
317 kDebug() << which << result;