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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
36 namespace Plasma
37 {
38 
39 class FramePrivate : public ThemedWidgetInterface<Frame>
40 {
41 public:
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 
67 void 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 
82 Frame::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 
95 Frame::~Frame()
96 {
97  delete d;
98 }
99 
100 void 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 
120 Frame::Shadow Frame::frameShadow() const
121 {
122  return d->shadow;
123 }
124 
125 void 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 
134 FrameSvg::EnabledBorders Frame::enabledBorders() const
135 {
136  return d->svg->enabledBorders();
137 }
138 
139 void Frame::setText(QString text)
140 {
141  d->text = text;
142  d->syncBorders();
143  updateGeometry();
144  update();
145 }
146 
147 QString Frame::text() const
148 {
149  return d->text;
150 }
151 
152 void 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 
193 QString Frame::image() const
194 {
195  return d->imagePath;
196 }
197 
198 void Frame::setStyleSheet(const QString &styleSheet)
199 {
200  //TODO: implement stylesheets painting
201  d->styleSheet = styleSheet;
202 }
203 
204 QString Frame::styleSheet() const
205 {
206  return d->styleSheet;
207 }
208 
209 QWidget *Frame::nativeWidget() const
210 {
211  return 0;
212 }
213 
214 void 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 
239 void 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 
250 QSizeF 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 
270 void 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::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
QWidget
Plasma::Frame::setFrameShadow
void setFrameShadow(Shadow shadow)
Sets the Frame's shadow style.
Definition: frame.cpp:100
Plasma::Frame::setEnabledBorders
void setEnabledBorders(const FrameSvg::EnabledBorders borders)
Sets what borders should be painted.
Definition: frame.cpp:125
Plasma::Theme::TextColor
the text color to be used by items resting on the background
Definition: theme.h:63
theme.h
Plasma::Frame::Plain
Definition: frame.h:54
Plasma::FrameSvg
Provides an SVG with borders.
Definition: framesvg.h:76
Plasma::Frame::image
QString image() const
Plasma::Frame::Frame
Frame(QGraphicsWidget *parent=0)
Constructs a new Frame.
Definition: frame.cpp:82
Plasma::Frame::styleSheet
QString styleSheet() const
Plasma::Frame::setText
void setText(QString text)
Set the text to display by this Frame.
Definition: frame.cpp:139
Plasma::Frame::frameShadow
Shadow frameShadow() const
Plasma::Frame::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: frame.cpp:214
Plasma::Frame::enabledBorders
FrameSvg::EnabledBorders enabledBorders() const
Convenience method to get the enabled borders.
Definition: frame.cpp:134
frame.h
Plasma::Frame::text
QString text() const
Plasma::Frame::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: frame.cpp:239
Plasma::Frame::nativeWidget
QWidget * nativeWidget() const
Definition: frame.cpp:209
Plasma::Frame::Raised
Definition: frame.h:55
Plasma::Frame::changeEvent
void changeEvent(QEvent *event)
Definition: frame.cpp:270
Plasma::Frame::setImage
void setImage(const QString &path)
Sets the path to an image to display.
Definition: frame.cpp:152
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
framesvg.h
Plasma::Frame::Sunken
Definition: frame.h:56
QStyleOptionGraphicsItem
Plasma::Frame::setStyleSheet
void setStyleSheet(const QString &stylesheet)
Sets the stylesheet used to control the visual display of this Frame.
Definition: frame.cpp:198
Plasma::Svg
A theme aware image-centric SVG class.
Definition: svg.h:56
Plasma::Frame::Shadow
Shadow
Definition: frame.h:53
Plasma::Frame::sizeHint
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
Definition: frame.cpp:250
Plasma::Frame::~Frame
~Frame()
Definition: frame.cpp:95
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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