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