• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

QtExtraComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • qtextracomponents
qiconitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Marco Martin <mart@kde.org>
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 "qiconitem.h"
21 
22 #include <KIcon>
23 #include <KIconLoader>
24 #include <KIconEffect>
25 #include <QPainter>
26 
27 
28 QIconItem::QIconItem(QDeclarativeItem *parent)
29  : QDeclarativeItem(parent),
30  m_smooth(false),
31  m_state(DefaultState)
32 {
33  setFlag(QGraphicsItem::ItemHasNoContents, false);
34 }
35 
36 
37 QIconItem::~QIconItem()
38 {
39 }
40 
41 void QIconItem::setIcon(const QVariant &icon)
42 {
43  if(icon.canConvert<QIcon>()) {
44  m_icon = icon.value<QIcon>();
45  } else if(icon.canConvert<QString>()) {
46  m_icon = KIcon(icon.toString());
47  } else {
48  m_icon = QIcon();
49  }
50  update();
51 }
52 
53 QIcon QIconItem::icon() const
54 {
55  return m_icon;
56 }
57 
58 QIconItem::State QIconItem::state() const
59 {
60  return m_state;
61 }
62 
63 void QIconItem::setState(QIconItem::State state)
64 {
65  if (m_state == state) {
66  return;
67  }
68 
69  m_state = state;
70  emit stateChanged(state);
71  update();
72 }
73 
74 int QIconItem::implicitWidth() const
75 {
76  return KIconLoader::global()->currentSize(KIconLoader::Desktop);
77 }
78 
79 int QIconItem::implicitHeight() const
80 {
81  return KIconLoader::global()->currentSize(KIconLoader::Desktop);
82 }
83 
84 void QIconItem::setSmooth(const bool smooth)
85 {
86  if (smooth == m_smooth) {
87  return;
88  }
89  m_smooth = smooth;
90  update();
91 }
92 
93 bool QIconItem::smooth() const
94 {
95  return m_smooth;
96 }
97 
98 void QIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
99 {
100  Q_UNUSED(option);
101  Q_UNUSED(widget);
102 
103  if (m_icon.isNull()) {
104  return;
105  }
106  //do without painter save, faster and the support can be compiled out
107  const bool wasAntiAlias = painter->testRenderHint(QPainter::Antialiasing);
108  const bool wasSmoothTransform = painter->testRenderHint(QPainter::SmoothPixmapTransform);
109  painter->setRenderHint(QPainter::Antialiasing, m_smooth);
110  painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
111 
112  if (m_state == ActiveState) {
113  QPixmap result = m_icon.pixmap(boundingRect().size().toSize());
114  result = KIconLoader::global()->iconEffect()->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
115  painter->drawPixmap(0, 0, result);
116  } else {
117  m_icon.paint(painter, boundingRect().toRect(), Qt::AlignCenter, isEnabled() ? QIcon::Normal : QIcon::Disabled);
118  }
119 
120  painter->setRenderHint(QPainter::Antialiasing, wasAntiAlias);
121  painter->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothTransform);
122 }
123 
124 
125 #include "qiconitem.moc"
QVariant::canConvert
bool canConvert(Type t) const
QIconItem::stateChanged
void stateChanged(State state)
QWidget
QIconItem::setIcon
void setIcon(const QVariant &icon)
Definition: qiconitem.cpp:41
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
QVariant::value
T value() const
QIconItem::state
QIconItem::State state() const
QIconItem::implicitHeight
int implicitHeight() const
qiconitem.h
QIconItem::smooth
bool smooth() const
QGraphicsItem::update
void update(const QRectF &rect)
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QIconItem::QIconItem
QIconItem(QDeclarativeItem *parent=0)
Definition: qiconitem.cpp:28
QIconItem::State
State
Definition: qiconitem.h:41
QString
QIcon::paint
void paint(QPainter *painter, const QRect &rect, QFlags< Qt::AlignmentFlag > alignment, Mode mode, State state) const
QPixmap
QIconItem::~QIconItem
~QIconItem()
Definition: qiconitem.cpp:37
QIconItem::setSmooth
void setSmooth(const bool smooth)
Definition: qiconitem.cpp:84
QGraphicsItem::isEnabled
bool isEnabled() const
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
QIcon::isNull
bool isNull() const
QIconItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: qiconitem.cpp:98
QIconItem::icon
QIcon icon() const
QPainter::testRenderHint
bool testRenderHint(RenderHint hint) const
QStyleOptionGraphicsItem
QDeclarativeItem
QIconItem::implicitWidth
int implicitWidth() const
QVariant::toString
QString toString() const
QIconItem::setState
void setState(State state)
Definition: qiconitem.cpp:63
QIcon
QIconItem::ActiveState
The default state.
Definition: qiconitem.h:43
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

QtExtraComponents

Skip menu "QtExtraComponents"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

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