Changeset View
Changeset View
Standalone View
Standalone View
plugins/dockers/gamutmask/KisGamutMaskChooser.cpp
- This file was added.
1 | #include "KisGamutMaskChooser.h" | ||||
---|---|---|---|---|---|
2 | | ||||
3 | #include <QWidget> | ||||
4 | #include <QVBoxLayout> | ||||
5 | #include <QAbstractItemDelegate> | ||||
6 | | ||||
7 | #include <KoResourceServer.h> | ||||
8 | #include <KoResourceServerProvider.h> | ||||
9 | #include <KoResourceItemChooser.h> | ||||
10 | #include <KoResourceServerAdapter.h> | ||||
11 | | ||||
12 | | ||||
13 | /// The resource item delegate for rendering the resource preview | ||||
14 | class KisGamutMaskDelegate: public QAbstractItemDelegate | ||||
15 | { | ||||
16 | public: | ||||
17 | KisGamutMaskDelegate(QObject * parent = 0) : QAbstractItemDelegate(parent) {} | ||||
18 | ~KisGamutMaskDelegate() override {} | ||||
19 | /// reimplemented | ||||
20 | void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override; | ||||
21 | /// reimplemented | ||||
22 | QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const override { | ||||
23 | return option.decorationSize; | ||||
24 | } | ||||
25 | }; | ||||
26 | | ||||
27 | void KisGamutMaskDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const | ||||
28 | { | ||||
29 | painter->save(); | ||||
30 | painter->setRenderHint(QPainter::SmoothPixmapTransform, true); | ||||
31 | | ||||
32 | if (!index.isValid()) | ||||
33 | return; | ||||
34 | | ||||
35 | KoResource* resource = static_cast<KoResource*>(index.internalPointer()); | ||||
36 | KoGamutMask* mask = static_cast<KoGamutMask*>(resource); | ||||
37 | | ||||
38 | if (!mask) { | ||||
39 | return; | ||||
40 | } | ||||
41 | | ||||
42 | QImage preview = mask->image(); | ||||
43 | | ||||
44 | if(preview.isNull()) { | ||||
45 | return; | ||||
46 | } | ||||
47 | | ||||
48 | QRect paintRect = option.rect.adjusted(1, 1, -1, -1); | ||||
49 | painter->drawImage(paintRect.x(), paintRect.y(), | ||||
50 | preview.scaled(paintRect.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); | ||||
51 | painter->restore(); | ||||
52 | } | ||||
53 | | ||||
54 | | ||||
55 | KisGamutMaskChooser::KisGamutMaskChooser(QWidget *parent) : QWidget(parent) | ||||
56 | { | ||||
57 | KoResourceServer<KoGamutMask>* rServer = KoResourceServerProvider::instance()->gamutMaskServer(); | ||||
58 | QSharedPointer<KoAbstractResourceServerAdapter> adapter(new KoResourceServerAdapter<KoGamutMask>(rServer)); | ||||
59 | m_itemChooser = new KoResourceItemChooser(adapter, this); | ||||
60 | m_itemChooser->setItemDelegate(new KisGamutMaskDelegate(this)); | ||||
61 | m_itemChooser->showTaggingBar(true); | ||||
62 | m_itemChooser->showButtons(false); | ||||
63 | | ||||
64 | QVBoxLayout* layout = new QVBoxLayout(this); | ||||
65 | layout->setContentsMargins(0,0,0,0); | ||||
66 | | ||||
67 | layout->addWidget(m_itemChooser); | ||||
68 | setLayout(layout); | ||||
69 | | ||||
70 | connect(m_itemChooser, SIGNAL(resourceSelected(KoResource*)), this, SLOT(resourceSelected(KoResource*))); | ||||
71 | } | ||||
72 | | ||||
73 | KisGamutMaskChooser::~KisGamutMaskChooser() | ||||
74 | { | ||||
75 | | ||||
76 | } | ||||
77 | | ||||
78 | void KisGamutMaskChooser::resourceSelected(KoResource* resource) | ||||
79 | { | ||||
80 | emit sigGamutMaskSelected(static_cast<KoGamutMask*>(resource)); | ||||
81 | } |