Applets
applet.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "applet.h"
00020 #include <Plasma/DataEngine>
00021 #include <Plasma/Containment>
00022 #include <Plasma/Frame>
00023 #include <Plasma/IconWidget>
00024 #include <KIcon>
00025 #include <KDebug>
00026 #include <QGraphicsLinearLayout>
00027
00028 namespace SM {
00029
00030 QHash< QString, QList<uint> > Applet::s_configIds;
00031
00032 Applet::Applet(QObject *parent, const QVariantList &args)
00033 : Plasma::Applet(parent, args),
00034 m_interval(10000),
00035 m_preferredItemHeight(42),
00036 m_titleSpacer(false),
00037 m_header(0),
00038 m_engine(0),
00039 m_ratioOrientation(Qt::Vertical),
00040 m_orientation(Qt::Vertical),
00041 m_noSourcesIcon(0),
00042 m_mode(Desktop),
00043 m_detail(Low),
00044 m_minimumWidth(DEFAULT_MINIMUM_WIDTH),
00045 m_mainLayout(0),
00046 m_configId(0)
00047 {
00048 if (args.count() > 0) {
00049 if (args[0].toString() == "SM") {
00050 m_mode = Monitor;
00051 }
00052 }
00053 QString name = pluginName();
00054
00055 while (s_configIds[name].contains(m_configId)) {
00056 ++m_configId;
00057 }
00058 s_configIds[name] << m_configId;
00059 }
00060
00061 Applet::~Applet()
00062 {
00063 s_configIds[pluginName()].removeAll(m_configId);
00064 deleteMeters();
00065 }
00066
00067 KConfigGroup Applet::persistentConfig() const
00068 {
00069 KConfigGroup cg = globalConfig();
00070 return KConfigGroup(cg.config(), QString("General_%1").arg(m_configId));
00071 }
00072
00073 void Applet::constraintsEvent(Plasma::Constraints constraints)
00074 {
00075 if (constraints & Plasma::FormFactorConstraint) {
00076 if (m_mode == Monitor) {
00077 setBackgroundHints(NoBackground);
00078 m_orientation = Qt::Vertical;
00079 } else {
00080 SM::Applet::Mode mode = m_mode;
00081 switch (formFactor()) {
00082 case Plasma::Planar:
00083 case Plasma::MediaCenter:
00084 mode = Desktop;
00085 m_orientation = Qt::Vertical;
00086 break;
00087 case Plasma::Horizontal:
00088 mode = Panel;
00089 m_orientation = Qt::Horizontal;
00090 break;
00091 case Plasma::Vertical:
00092 mode = Panel;
00093 m_orientation = Qt::Vertical;
00094 break;
00095 }
00096 if (mode != m_mode) {
00097 m_mode = mode;
00098 m_ratioOrientation = m_orientation;
00099 connectToEngine();
00100 }
00101 }
00102 } else if (constraints & Plasma::SizeConstraint) {
00103 Detail detail;
00104 if (size().width() > 250 && size().height() / m_items.count() > 150) {
00105 detail = High;
00106 } else {
00107 detail = Low;
00108 }
00109 if (m_detail != detail && m_mode != Monitor) {
00110 m_detail = detail;
00111 setDetail(m_detail);
00112 }
00113 if (m_keepRatio.count() > 0) {
00114 foreach (QGraphicsWidget* item, m_keepRatio) {
00115 QSizeF size = QSizeF(qMin(item->size().width(), contentsRect().size().width()),
00116 qMin(item->size().height(), contentsRect().size().height()));
00117
00118 if (size == QSizeF(0, 0)) {
00119 continue;
00120 }
00121 qreal ratio = item->preferredSize().height() / item->preferredSize().width();
00122 if (m_ratioOrientation == Qt::Vertical) {
00123 size = QSizeF(size.width(), size.width() * ratio);
00124 } else {
00125 size = QSizeF(size.height() * (1.0 / ratio), size.height());
00126 }
00127 item->setPreferredSize(size);
00128 if (m_mode == Panel) {
00129 item->setMaximumSize(size);
00130 item->setMinimumSize(size);
00131 }
00132 }
00133 for (int i = mainLayout()->count() - 1; i >= 0; --i) {
00134 QGraphicsLayoutItem* item = mainLayout()->itemAt(i);
00135 if (item) {
00136 QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
00137 if (l) {
00138 l->invalidate();
00139 }
00140 }
00141 }
00142 }
00143 }
00144 }
00145
00146 void Applet::setDetail(Detail detail)
00147 {
00148 Q_UNUSED(detail);
00149 }
00150
00151 void Applet::setTitle(const QString& title, bool spacer)
00152 {
00153 m_title = title;
00154 m_titleSpacer = spacer;
00155 if (m_header) {
00156 m_header->setText(m_title);
00157 }
00158 }
00159
00160 QGraphicsLinearLayout* Applet::mainLayout()
00161 {
00162 if (!m_mainLayout) {
00163 m_mainLayout = new QGraphicsLinearLayout(m_orientation);
00164 m_mainLayout->setContentsMargins(0, 0, 0, 0);
00165 m_mainLayout->setSpacing(0);
00166 setLayout(m_mainLayout);
00167 }
00168 return m_mainLayout;
00169 }
00170
00171 void Applet::connectToEngine()
00172 {
00173 deleteMeters();
00174 disconnectSources();
00175
00176 mainLayout()->setOrientation(m_orientation);
00177 if (m_mode != Panel) {
00178 m_header = new Plasma::Frame(this);
00179 m_header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
00180 m_header->setText(m_title);
00181 mainLayout()->addItem(m_header);
00182 }
00183 if (m_items.count() == 0){
00184 displayNoAvailableSources();
00185 return;
00186 }
00187 foreach (const QString &item, m_items) {
00188 if (addMeter(item)) {
00189 connectSource(item);
00190 }
00191 }
00192 if (m_titleSpacer) {
00193 mainLayout()->addStretch();
00194 }
00195 checkGeometry();
00196 mainLayout()->activate();
00197 constraintsEvent(Plasma::SizeConstraint);
00198 setDetail(m_detail);
00199 }
00200
00201 void Applet::checkGeometry()
00202 {
00203 if (m_mode != Panel) {
00204 QSizeF minSize;
00205 qreal height = 0;
00206
00207 if (m_header) {
00208 height = m_header->minimumSize().height();
00209 }
00210 m_min.setHeight(height + m_items.count() * m_preferredItemHeight);
00211 m_min.setWidth(m_minimumWidth);
00212 if (m_mode != Monitor) {
00213 m_max = QSizeF();
00214 m_min += size() - contentsRect().size();
00215 } else {
00216
00217 setBackgroundHints(NoBackground);
00218 m_max = QSizeF(QSizeF().width(), minSize.height());
00219 }
00220
00221
00222 m_pref = m_min;
00223
00224 setAspectRatioMode(Plasma::IgnoreAspectRatio);
00225 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
00226 update();
00227 } else {
00228 int x = 1;
00229 int y = 1;
00230 QSizeF size = containment()->size();
00231 qreal s;
00232
00233 if (m_orientation == Qt::Horizontal) {
00234 x = m_items.count();
00235 s = size.height();
00236 } else {
00237 y = m_items.count();
00238 s = size.width();
00239 }
00240 m_min = QSizeF(16 * x, 16 * y);
00241 m_max = m_pref = QSizeF(s * x, s * y);
00242 setAspectRatioMode(Plasma::KeepAspectRatio);
00243 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00244 }
00245 setMinimumSize(m_min);
00246 setPreferredSize(m_pref);
00247 setMaximumSize(m_max);
00248
00249 emit geometryChecked();
00250 }
00251
00252 void Applet::connectSource(const QString& source)
00253 {
00254 if (m_engine) {
00255 m_engine->connectSource(source, this, m_interval);
00256 m_connectedSources << source;
00257 }
00258 }
00259
00260 void Applet::disconnectSources()
00261 {
00262 Plasma::DataEngine *engine = dataEngine("soliddevice");
00263 if (engine) {
00264 foreach (const QString &source, m_connectedSources) {
00265 engine->disconnectSource(source, this);
00266 }
00267 }
00268 m_connectedSources.clear();
00269 }
00270
00271 void Applet::deleteMeters(QGraphicsLinearLayout* layout)
00272 {
00273 if (!layout) {
00274 layout = mainLayout();
00275 m_meters.clear();
00276 m_plotters.clear();
00277 m_keepRatio.clear();
00278 m_header = 0;
00279 }
00280 for (int i = layout->count() - 1; i >= 0; --i) {
00281 QGraphicsLayoutItem* item = layout->itemAt(i);
00282 if (item) {
00283 QGraphicsLinearLayout* l = dynamic_cast<QGraphicsLinearLayout *>(item);
00284 if (l) {
00285 deleteMeters(l);
00286 }
00287 }
00288 layout->removeAt(i);
00289 delete item;
00290 }
00291 }
00292
00293 void Applet::displayNoAvailableSources()
00294 {
00295 KIcon appletIcon(icon());
00296 m_noSourcesIcon = new Plasma::IconWidget(appletIcon, "", this);
00297 mainLayout()->addItem(m_noSourcesIcon);
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 }