libplasma
label.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
00020 #include "label.h"
00021
00022 #include <QLabel>
00023 #include <QPainter>
00024 #include <QDir>
00025
00026 #include <KMimeType>
00027
00028 #include "theme.h"
00029 #include "svg.h"
00030
00031 namespace Plasma
00032 {
00033
00034 class LabelPrivate
00035 {
00036 public:
00037 LabelPrivate(Label *label)
00038 : q(label),
00039 svg(0)
00040 {
00041 }
00042
00043 ~LabelPrivate()
00044 {
00045 delete svg;
00046 }
00047
00048 void setPixmap(Label *q)
00049 {
00050 if (imagePath.isEmpty()) {
00051 return;
00052 }
00053
00054 KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
00055 QPixmap pm(q->size().toSize());
00056
00057 if (mime->is("image/svg+xml")) {
00058 svg = new Svg();
00059 QPainter p(&pm);
00060 svg->paint(&p, pm.rect());
00061 } else {
00062 pm = QPixmap(absImagePath);
00063 }
00064
00065 static_cast<QLabel*>(q->widget())->setPixmap(pm);
00066 }
00067
00068 void setPalette()
00069 {
00070 QLabel *native = q->nativeWidget();
00071 QColor color = Theme::defaultTheme()->color(Theme::TextColor);
00072 QPalette p = native->palette();
00073 p.setColor(QPalette::Normal, QPalette::WindowText, color);
00074 p.setColor(QPalette::Inactive, QPalette::WindowText, color);
00075 native->setPalette(p);
00076 }
00077
00078 Label *q;
00079 QString imagePath;
00080 QString absImagePath;
00081 Svg *svg;
00082 };
00083
00084 Label::Label(QGraphicsWidget *parent)
00085 : QGraphicsProxyWidget(parent),
00086 d(new LabelPrivate(this))
00087 {
00088 QLabel* native = new QLabel;
00089
00090 connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(setPalette()));
00091 native->setAttribute(Qt::WA_NoSystemBackground);
00092 native->setWordWrap(true);
00093 setWidget(native);
00094 d->setPalette();
00095 }
00096
00097 Label::~Label()
00098 {
00099 delete d;
00100 }
00101
00102 void Label::setText(const QString &text)
00103 {
00104 static_cast<QLabel*>(widget())->setText(text);
00105 }
00106
00107 QString Label::text() const
00108 {
00109 return static_cast<QLabel*>(widget())->text();
00110 }
00111
00112 void Label::setImage(const QString &path)
00113 {
00114 if (d->imagePath == path) {
00115 return;
00116 }
00117
00118 delete d->svg;
00119 d->svg = 0;
00120 d->imagePath = path;
00121
00122 bool absolutePath = !path.isEmpty() &&
00123 #ifdef Q_WS_WIN
00124 !QDir::isRelativePath(path)
00125 #else
00126 (path[0] == '/' || path.startsWith(":/"))
00127 #endif
00128 ;
00129
00130 if (absolutePath) {
00131 d->absImagePath = path;
00132 } else {
00133
00134 d->absImagePath = Theme::defaultTheme()->imagePath(path);
00135 }
00136
00137 d->setPixmap(this);
00138 }
00139
00140 QString Label::image() const
00141 {
00142 return d->imagePath;
00143 }
00144
00145 void Label::setStyleSheet(const QString &stylesheet)
00146 {
00147 widget()->setStyleSheet(stylesheet);
00148 }
00149
00150 QString Label::styleSheet()
00151 {
00152 return widget()->styleSheet();
00153 }
00154
00155 QLabel* Label::nativeWidget() const
00156 {
00157 return static_cast<QLabel*>(widget());
00158 }
00159
00160 void Label::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
00161 {
00162 Q_UNUSED(sourceName)
00163
00164 QStringList texts;
00165 foreach (const QVariant& v, data) {
00166 if (v.canConvert(QVariant::String)) {
00167 texts << v.toString();
00168 }
00169 }
00170
00171 setText(texts.join(" "));
00172 }
00173
00174 void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
00175 {
00176 d->setPixmap(this);
00177 QGraphicsProxyWidget::resizeEvent(event);
00178 }
00179
00180 }
00181
00182 #include <label.moc>
00183