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