• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
frame.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 Marco Martin <notmart@gmail.com>
3 *
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 as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "frame.h"
21
22//Qt
23#include <QGraphicsSceneResizeEvent>
24#include <QWidget>
25#include <QDir>
26#include <QPainter>
27
28//KDE
29#include <kmimetype.h>
30
31//Plasma
32#include "framesvg.h"
33#include "private/themedwidgetinterface_p.h"
34#include "theme.h"
35
36namespace Plasma
37{
38
39class FramePrivate : public ThemedWidgetInterface<Frame>
40{
41public:
42 FramePrivate(Frame *parent)
43 : ThemedWidgetInterface<Frame>(parent),
44 svg(0),
45 image(0),
46 pixmap(0)
47 {
48 }
49
50 ~FramePrivate()
51 {
52 delete pixmap;
53 }
54
55 void syncBorders();
56
57 FrameSvg *svg;
58 Frame::Shadow shadow;
59 QString text;
60 QString styleSheet;
61 QString imagePath;
62 QString absImagePath;
63 Svg *image;
64 QPixmap *pixmap;
65};
66
67void FramePrivate::syncBorders()
68{
69 //set margins from the normal element
70 qreal left, top, right, bottom;
71
72 svg->getMargins(left, top, right, bottom);
73
74 if (!text.isNull()) {
75 QFontMetricsF fm(q->font());
76 top += fm.height();
77 }
78
79 q->setContentsMargins(left, top, right, bottom);
80}
81
82Frame::Frame(QGraphicsWidget *parent)
83 : QGraphicsWidget(parent),
84 d(new FramePrivate(this))
85{
86 d->svg = new Plasma::FrameSvg(this);
87 d->svg->setImagePath("widgets/frame");
88 d->svg->setElementPrefix("plain");
89 d->syncBorders();
90
91 connect(d->svg, SIGNAL(repaintNeeded()), SLOT(syncBorders()));
92 d->initTheming();
93}
94
95Frame::~Frame()
96{
97 delete d;
98}
99
100void Frame::setFrameShadow(Shadow shadow)
101{
102 d->shadow = shadow;
103
104 switch (d->shadow) {
105 case Raised:
106 d->svg->setElementPrefix("raised");
107 break;
108 case Sunken:
109 d->svg->setElementPrefix("sunken");
110 break;
111 case Plain:
112 default:
113 d->svg->setElementPrefix("plain");
114 break;
115 }
116
117 d->syncBorders();
118}
119
120Frame::Shadow Frame::frameShadow() const
121{
122 return d->shadow;
123}
124
125void Frame::setEnabledBorders(const FrameSvg::EnabledBorders borders)
126{
127 if (borders != d->svg->enabledBorders()) {
128 d->svg->setEnabledBorders(borders);
129 d->syncBorders();
130 update();
131 }
132}
133
134FrameSvg::EnabledBorders Frame::enabledBorders() const
135{
136 return d->svg->enabledBorders();
137}
138
139void Frame::setText(QString text)
140{
141 d->text = text;
142 d->syncBorders();
143 updateGeometry();
144 update();
145}
146
147QString Frame::text() const
148{
149 return d->text;
150}
151
152void Frame::setImage(const QString &path)
153{
154 if (d->imagePath == path) {
155 return;
156 }
157
158 delete d->image;
159 d->image = 0;
160 d->imagePath = path;
161 delete d->pixmap;
162 d->pixmap = 0;
163
164 bool absolutePath = !path.isEmpty() &&
165 #ifdef Q_WS_WIN
166 !QDir::isRelativePath(path)
167 #else
168 (path[0] == '/' || path.startsWith(QLatin1String(":/")))
169 #endif
170 ;
171
172 if (absolutePath) {
173 d->absImagePath = path;
174 } else {
175 //TODO: package support
176 d->absImagePath = Theme::defaultTheme()->imagePath(path);
177 }
178
179 if (path.isEmpty()) {
180 return;
181 }
182
183 KMimeType::Ptr mime = KMimeType::findByPath(d->absImagePath);
184
185 if (!mime->is("image/svg+xml") && !mime->is("application/x-gzip")) {
186 d->pixmap = new QPixmap(d->absImagePath);
187 } else {
188 d->image = new Plasma::Svg(this);
189 d->image->setImagePath(path);
190 }
191}
192
193QString Frame::image() const
194{
195 return d->imagePath;
196}
197
198void Frame::setStyleSheet(const QString &styleSheet)
199{
200 //TODO: implement stylesheets painting
201 d->styleSheet = styleSheet;
202}
203
204QString Frame::styleSheet() const
205{
206 return d->styleSheet;
207}
208
209QWidget *Frame::nativeWidget() const
210{
211 return 0;
212}
213
214void Frame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
215{
216 Q_UNUSED(option)
217 Q_UNUSED(widget)
218
219 d->svg->paintFrame(painter);
220
221 if (!d->text.isNull()) {
222 QFontMetricsF fm(font());
223 QRectF textRect = d->svg->contentsRect();
224 textRect.setHeight(fm.height());
225 painter->setFont(font());
226 painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor));
227 painter->drawText(textRect, Qt::AlignHCenter|Qt::AlignTop, d->text);
228 }
229
230 if (!d->imagePath.isNull()) {
231 if (d->pixmap && !d->pixmap->isNull()) {
232 painter->drawPixmap(contentsRect(), *d->pixmap, d->pixmap->rect());
233 } else if (d->image) {
234 d->image->paint(painter, contentsRect());
235 }
236 }
237}
238
239void Frame::resizeEvent(QGraphicsSceneResizeEvent *event)
240{
241 d->svg->resizeFrame(event->newSize());
242
243 if (d->image) {
244 d->image->resize(contentsRect().size());
245 }
246
247 QGraphicsWidget::resizeEvent(event);
248}
249
250QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
251{
252 QSizeF hint = QGraphicsWidget::sizeHint(which, constraint);
253
254 if (!d->image && !layout()) {
255 QFontMetricsF fm(font());
256
257 qreal left, top, right, bottom;
258 d->svg->getMargins(left, top, right, bottom);
259
260 hint.setHeight(fm.height() + top + bottom);
261 if (which == Qt::MinimumSize || which == Qt::PreferredSize) {
262 QRectF rect = fm.boundingRect(d->text);
263 hint.setWidth(rect.width() + left + right);
264 }
265 }
266
267 return hint;
268}
269
270void Frame::changeEvent(QEvent *event)
271{
272 d->changeEvent(event);
273 QGraphicsWidget::changeEvent(event);
274}
275
276} // namespace Plasma
277
278#include <frame.moc>
279
Plasma::FrameSvg
Provides an SVG with borders.
Definition: framesvg.h:77
Plasma::Frame::styleSheet
QString styleSheet
Definition: frame.h:49
Plasma::Frame::Shadow
Shadow
Definition: frame.h:53
Plasma::Frame::Raised
@ Raised
Definition: frame.h:55
Plasma::Frame::Sunken
@ Sunken
Definition: frame.h:56
Plasma::Frame::Plain
@ Plain
Definition: frame.h:54
Plasma::Frame::text
QString text
Definition: frame.h:47
Plasma::Svg
A theme aware image-centric SVG class.
Definition: svg.h:57
Plasma::Theme::imagePath
Q_INVOKABLE QString imagePath(const QString &name) const
Retrieve the path for an SVG image in the current theme.
Definition: theme.cpp:794
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::Theme::TextColor
@ TextColor
the text color to be used by items resting on the background
Definition: theme.h:63
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
frame.h
framesvg.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal