Krita Source Code Documentation
Loading...
Searching...
No Matches
KisIconWidget Class Reference

#include <kis_iconwidget.h>

+ Inheritance diagram for KisIconWidget:

Classes

struct  Private
 

Public Member Functions

 KisIconWidget (QWidget *parent=0, const QString &name=QString())
 
QSize preferredIconSize () const
 
void setBackgroundColor (const QColor &color)
 
void setResource (KoResourceSP resource)
 
void setThumbnail (const QImage &thumbnail)
 
 ~KisIconWidget () override
 
- Public Member Functions inherited from KisPopupButton
void adjustPosition ()
 adjustPosition adjusts the position of the popup widget based on the position of this button and the size of the widget
 
bool isPopupWidgetVisible ()
 
 KisPopupButton (QWidget *parent)
 
void setArrowVisible (bool v)
 
void setPopupWidget (QWidget *widget)
 
void setPopupWidgetWidth (int w)
 
 ~KisPopupButton () override
 

Protected Member Functions

void paintEvent (QPaintEvent *) override
 
- Protected Member Functions inherited from KisPopupButton
void paintEvent (QPaintEvent *event) override
 
void paintPopupArrow ()
 

Private Attributes

Private *const m_d
 

Additional Inherited Members

- Public Slots inherited from KisPopupButton
void hidePopupWidget ()
 
void setPopupWidgetDetached (bool detach)
 
void setPopupWidgetVisible (bool visible)
 
void showPopupWidget ()
 

Detailed Description

The icon widget is used in the control box where the current color and brush are shown.

Definition at line 20 of file kis_iconwidget.h.

Constructor & Destructor Documentation

◆ KisIconWidget()

KisIconWidget::KisIconWidget ( QWidget * parent = 0,
const QString & name = QString() )

Definition at line 25 of file kis_iconwidget.cc.

26 : KisPopupButton(parent)
27 , m_d(new Private)
28{
29 m_d->backgroundColor = QColor(Qt::transparent);
30 setObjectName(name);
31 m_d->resource = nullptr;
32}
Private *const m_d
KisPopupButton(QWidget *parent)

References KisIconWidget::Private::backgroundColor, m_d, and KisIconWidget::Private::resource.

◆ ~KisIconWidget()

KisIconWidget::~KisIconWidget ( )
override

Definition at line 34 of file kis_iconwidget.cc.

35{
36 delete m_d;
37}

References m_d.

Member Function Documentation

◆ paintEvent()

void KisIconWidget::paintEvent ( QPaintEvent * event)
overrideprotected

Definition at line 69 of file kis_iconwidget.cc.

70{
71 Q_UNUSED(event);
72
73 const qint32 border = 3;
74 const qint32 iconWidth = width() - (border*2);
75 const qint32 iconHeight = height() - (border*2);
76
77 auto makeIcon = [&](std::function<void (QPainter &)> paintFn) {
78 QPixmap pixmap(iconWidth * devicePixelRatioF(), iconHeight * devicePixelRatioF());
79 pixmap.setDevicePixelRatio(devicePixelRatioF());
81 QPainter p(&pixmap);
82
83 // Round off the corners of the preview
84 QRegion clipRegion(0, 0, iconWidth, iconHeight);
85 clipRegion -= QRegion(0, 0, 1, 1);
86 clipRegion -= QRegion(iconWidth - 1, 0, 1, 1);
87 clipRegion -= QRegion(iconWidth - 1, iconHeight - 1, 1, 1);
88 clipRegion -= QRegion(0, iconHeight - 1, 1, 1);
89
90 p.setClipRegion(clipRegion);
91 p.setClipping(true);
92 paintFn(p);
93 return pixmap;
94 };
95
96 auto paintThumbnailIcon = [&](QPainter &p) {
97 QImage img = QImage(iconWidth*devicePixelRatioF(), iconHeight*devicePixelRatioF(), QImage::Format_ARGB32);
98 img.setDevicePixelRatio(devicePixelRatioF());
99 img.fill(m_d->backgroundColor);
100 if (m_d->thumbnail.width() < iconWidth || m_d->thumbnail.height() < iconHeight) {
101 QPainter paint2;
102 paint2.begin(&img);
103 for (int x = 0; x < iconWidth; x += m_d->thumbnail.width()) {
104 for (int y = 0; y < iconHeight; y+= m_d->thumbnail.height()) {
105 paint2.drawImage(x, y, m_d->thumbnail);
106 }
107 }
108 } else {
109 img = m_d->thumbnail.scaled(iconWidth*devicePixelRatioF(), iconHeight*devicePixelRatioF(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
110 }
111 p.drawImage(QRect(0, 0, iconWidth, iconHeight), img);
112 };
113
114 auto paintResourceIcon = [&](QPainter &p) {
115 QImage img = QImage(iconWidth*devicePixelRatioF(), iconHeight*devicePixelRatioF(), QImage::Format_ARGB32);
116 img.setDevicePixelRatio(devicePixelRatioF());
117 img.fill(m_d->backgroundColor);
118 if (m_d->resource->image().width() < iconWidth || m_d->resource->image().height() < iconHeight) {
119 QPainter paint2;
120 paint2.begin(&img);
121 for (int x = 0; x < iconWidth; x += m_d->resource->image().width()) {
122 for (int y = 0; y < iconHeight; y += m_d->resource->image().height()) {
123 paint2.drawImage(x, y, m_d->resource->image());
124 }
125 }
126 } else {
127 img = m_d->resource->image().scaled(iconWidth*devicePixelRatioF(), iconHeight*devicePixelRatioF(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
128 }
129 p.drawImage(QRect(0, 0, iconWidth, iconHeight), img);
130 };
131
132 bool useCustomIcon = false;
133
134 const bool isCachedPixmapOutdated = m_d->cachedIconPixmap.isNull()
135 || m_d->cachedIconPixmap.width() != iconWidth
136 || m_d->cachedIconPixmap.height() != iconHeight
137 || m_d->cachedIconPixmap.devicePixelRatio() != devicePixelRatioF();
138
139 if (!m_d->thumbnail.isNull()) {
140 if (isCachedPixmapOutdated) {
141 m_d->cachedIconPixmap = makeIcon(paintThumbnailIcon);
142 }
143 useCustomIcon = true;
144 } else if (m_d->resource) {
145 if (isCachedPixmapOutdated || m_d->cachedResourceImageKey != m_d->resource->image().cacheKey()) {
146 m_d->cachedIconPixmap = makeIcon(paintResourceIcon);
147 }
148 useCustomIcon = true;
149 }
150
151 QStylePainter ps(this);
152 QStyleOptionToolButton opt;
153 initStyleOption(&opt);
154 if (useCustomIcon) {
155 opt.iconSize = QSize(iconWidth, iconHeight);
156 opt.icon = QIcon(m_d->cachedIconPixmap);
157 opt.toolButtonStyle = Qt::ToolButtonIconOnly;
158 }
159 ps.drawComplexControl(QStyle::CC_ToolButton, opt);
160}
const Params2D p
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)

References KisIconWidget::Private::backgroundColor, KisIconWidget::Private::cachedIconPixmap, KisIconWidget::Private::cachedResourceImageKey, m_d, p, KisIconWidget::Private::resource, KisIconWidget::Private::thumbnail, and void().

◆ preferredIconSize()

QSize KisIconWidget::preferredIconSize ( ) const

Definition at line 58 of file kis_iconwidget.cc.

59{
60 const qint32 cw = width();
61 const qint32 ch = height();
62 const qint32 border = 3;
63 const qint32 iconWidth = cw - (border*2);
64 const qint32 iconHeight = ch - (border*2);
65
66 return QSize(iconWidth, iconHeight)*devicePixelRatioF();
67}

◆ setBackgroundColor()

void KisIconWidget::setBackgroundColor ( const QColor & color)

Definition at line 53 of file kis_iconwidget.cc.

54{
55 m_d->backgroundColor = color;
56}

References KisIconWidget::Private::backgroundColor, and m_d.

◆ setResource()

void KisIconWidget::setResource ( KoResourceSP resource)

Definition at line 46 of file kis_iconwidget.cc.

47{
48 m_d->resource = resource;
49 m_d->cachedIconPixmap = QPixmap();
50 update();
51}
bool update(QSpinBox *spinBox)

References KisIconWidget::Private::cachedIconPixmap, m_d, and KisIconWidget::Private::resource.

◆ setThumbnail()

void KisIconWidget::setThumbnail ( const QImage & thumbnail)

Definition at line 39 of file kis_iconwidget.cc.

40{
41 m_d->thumbnail = thumbnail;
42 m_d->cachedIconPixmap = QPixmap();
43 update();
44}

References KisIconWidget::Private::cachedIconPixmap, m_d, and KisIconWidget::Private::thumbnail.

Member Data Documentation

◆ m_d

Private* const KisIconWidget::m_d
private

Definition at line 38 of file kis_iconwidget.h.


The documentation for this class was generated from the following files: