Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/KisPaletteListWidget.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2013 Sven Langkamp <sven.langkamp@gmail.com> | ||||
3 | * Copyright (c) 2018 Michael Zhou <simeirxh@gmail.com> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or modify | ||||
6 | * it under the terms of the GNU General Public License as published by | ||||
7 | * the Free Software Foundation; either version 2 of the License, or | ||||
8 | * (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QPointer> | ||||
21 | #include <QScopedPointer> | ||||
22 | #include <QGridLayout> | ||||
23 | #include <QSet> | ||||
24 | #include <QStringList> | ||||
25 | #include <QFile> | ||||
26 | #include <QMessageBox> | ||||
27 | #include <QInputDialog> | ||||
28 | #include <QDir> | ||||
29 | | ||||
30 | #include <kis_icon.h> | ||||
31 | #include <KoFileDialog.h> | ||||
32 | | ||||
33 | #include <ui_WdgPaletteListWidget.h> | ||||
34 | #include "KisPaletteListWidget.h" | ||||
35 | #include "KisPaletteListWidget_p.h" | ||||
36 | | ||||
37 | KisPaletteListWidget::KisPaletteListWidget(QWidget *parent) | ||||
38 | : QWidget(parent) | ||||
39 | , m_ui(new Ui_WdgPaletteListWidget) | ||||
40 | , m_d(new KisPaletteListWidgetPrivate(this)) | ||||
41 | { | ||||
42 | m_d->allowModification = false; | ||||
43 | | ||||
44 | m_d->actAdd.reset(new QAction(KisIconUtils::loadIcon("list-add"), | ||||
45 | i18n("Add a new palette"))); | ||||
46 | m_d->actRemove.reset(new QAction(KisIconUtils::loadIcon("list-remove"), | ||||
47 | i18n("Remove current palette"))); | ||||
48 | m_d->actImport.reset(new QAction(KisIconUtils::loadIcon("document-import"), | ||||
49 | i18n("Import a new palette from file"))); | ||||
50 | m_d->actExport.reset(new QAction(KisIconUtils::loadIcon("document-export"), | ||||
51 | i18n("Export current palette to file"))); | ||||
52 | m_d->model->setColumnCount(1); | ||||
53 | | ||||
54 | m_ui->setupUi(this); | ||||
55 | m_ui->bnAdd->setDefaultAction(m_d->actAdd.data()); | ||||
56 | m_ui->bnRemove->setDefaultAction(m_d->actRemove.data()); | ||||
57 | m_ui->bnImport->setDefaultAction(m_d->actImport.data()); | ||||
58 | m_ui->bnExport->setDefaultAction(m_d->actExport.data()); | ||||
59 | | ||||
60 | m_ui->bnAdd->setEnabled(false); | ||||
61 | m_ui->bnRemove->setEnabled(false); | ||||
62 | m_ui->bnImport->setEnabled(false); | ||||
63 | m_ui->bnExport->setEnabled(false); | ||||
64 | | ||||
65 | connect(m_d->actAdd.data(), SIGNAL(triggered()), SLOT(slotAdd())); | ||||
66 | connect(m_d->actRemove.data(), SIGNAL(triggered()), SLOT(slotRemove())); | ||||
67 | connect(m_d->actImport.data(), SIGNAL(triggered()), SLOT(slotImport())); | ||||
68 | connect(m_d->actExport.data(), SIGNAL(triggered()), SLOT(slotExport())); | ||||
69 | | ||||
70 | m_d->itemChooser->setItemDelegate(m_d->delegate.data()); | ||||
71 | m_d->itemChooser->setRowHeight(40); | ||||
72 | m_d->itemChooser->setColumnCount(1); | ||||
73 | m_d->itemChooser->showButtons(false); | ||||
74 | m_d->itemChooser->showTaggingBar(true); | ||||
75 | m_ui->viewPalette->setLayout(new QHBoxLayout(m_ui->viewPalette)); | ||||
76 | m_ui->viewPalette->layout()->addWidget(m_d->itemChooser.data()); | ||||
77 | | ||||
78 | connect(m_d->itemChooser.data(), SIGNAL(resourceSelected(KoResource *)), SLOT(slotPaletteResourceSelected(KoResource*))); | ||||
79 | } | ||||
80 | | ||||
81 | KisPaletteListWidget::~KisPaletteListWidget() | ||||
82 | { } | ||||
83 | | ||||
84 | void KisPaletteListWidget::slotPaletteResourceSelected(KoResource *r) | ||||
85 | { | ||||
86 | KoColorSet *g = static_cast<KoColorSet*>(r); | ||||
87 | emit sigPaletteSelected(g); | ||||
88 | if (!m_d->allowModification) { return; } | ||||
89 | if (g->isEditable()) { | ||||
90 | m_ui->bnRemove->setEnabled(true); | ||||
91 | } else { | ||||
92 | m_ui->bnRemove->setEnabled(false); | ||||
93 | } | ||||
94 | } | ||||
95 | | ||||
96 | void KisPaletteListWidget::slotAdd() | ||||
97 | { | ||||
98 | if (!m_d->allowModification) { return; } | ||||
99 | emit sigAddPalette(); | ||||
100 | m_d->itemChooser->setCurrentItem(m_d->rAdapter->resources().size()-1, 0); | ||||
101 | } | ||||
102 | | ||||
103 | void KisPaletteListWidget::slotRemove() | ||||
104 | { | ||||
105 | if (!m_d->allowModification) { return; } | ||||
106 | if (m_d->itemChooser->currentResource()) { | ||||
107 | KoColorSet *cs = static_cast<KoColorSet*>(m_d->itemChooser->currentResource()); | ||||
108 | emit sigRemovePalette(cs); | ||||
109 | } | ||||
110 | m_d->itemChooser->setCurrentItem(0, 0); | ||||
111 | } | ||||
112 | | ||||
113 | void KisPaletteListWidget::slotImport() | ||||
114 | { | ||||
115 | if (!m_d->allowModification) { return; } | ||||
116 | emit sigImportPalette(); | ||||
117 | m_d->itemChooser->setCurrentItem(m_d->rAdapter->resources().size()-1, 0); | ||||
118 | } | ||||
119 | | ||||
120 | void KisPaletteListWidget::slotExport() | ||||
121 | { | ||||
122 | if (!m_d->allowModification) { return; } | ||||
123 | emit sigExportPalette(static_cast<KoColorSet*>(m_d->itemChooser->currentResource())); | ||||
124 | } | ||||
125 | | ||||
126 | void KisPaletteListWidget::setAllowModification(bool allowModification) | ||||
127 | { | ||||
128 | m_d->allowModification = allowModification; | ||||
129 | m_ui->bnAdd->setEnabled(allowModification); | ||||
130 | m_ui->bnImport->setEnabled(allowModification); | ||||
131 | m_ui->bnExport->setEnabled(allowModification); | ||||
132 | KoColorSet *cs = static_cast<KoColorSet*>(m_d->itemChooser->currentResource()); | ||||
133 | m_ui->bnRemove->setEnabled(allowModification && cs && cs->isEditable()); | ||||
134 | } | ||||
135 | | ||||
136 | /************************* KisPaletteListWidgetPrivate **********************/ | ||||
137 | | ||||
138 | KisPaletteListWidgetPrivate::KisPaletteListWidgetPrivate(KisPaletteListWidget *a_c) | ||||
139 | : c(a_c) | ||||
140 | , rAdapter(new KoResourceServerAdapter<KoColorSet>(KoResourceServerProvider::instance()->paletteServer())) | ||||
141 | , itemChooser(new KoResourceItemChooser(rAdapter, a_c)) | ||||
142 | , model(new Model(rAdapter, a_c)) | ||||
143 | , delegate(new Delegate(a_c)) | ||||
144 | { } | ||||
145 | | ||||
146 | KisPaletteListWidgetPrivate::~KisPaletteListWidgetPrivate() | ||||
147 | { } | ||||
148 | | ||||
149 | /******************* KisPaletteListWidgetPrivate::Delegate ******************/ | ||||
150 | | ||||
151 | KisPaletteListWidgetPrivate::Delegate::Delegate(QObject *parent) | ||||
152 | : QAbstractItemDelegate(parent) | ||||
153 | { } | ||||
154 | | ||||
155 | KisPaletteListWidgetPrivate::Delegate::~Delegate() | ||||
156 | { } | ||||
157 | | ||||
158 | void KisPaletteListWidgetPrivate::Delegate::paint(QPainter * painter, | ||||
159 | const QStyleOptionViewItem & option, | ||||
160 | const QModelIndex & index) const | ||||
161 | { | ||||
162 | painter->save(); | ||||
163 | if (!index.isValid()) | ||||
164 | return; | ||||
165 | | ||||
166 | KoResource* resource = static_cast<KoResource*>(index.internalPointer()); | ||||
167 | KoColorSet* colorSet = static_cast<KoColorSet*>(resource); | ||||
168 | | ||||
169 | QRect previewRect(option.rect.x() + 2, | ||||
170 | option.rect.y() + 2, | ||||
171 | option.rect.height() - 4, | ||||
172 | option.rect.height() - 4); | ||||
173 | | ||||
174 | painter->drawImage(previewRect, colorSet->image()); | ||||
175 | | ||||
176 | if (option.state & QStyle::State_Selected) { | ||||
177 | painter->fillRect(option.rect, option.palette.highlight()); | ||||
178 | painter->drawImage(previewRect, colorSet->image()); | ||||
179 | painter->setPen(option.palette.highlightedText().color()); | ||||
180 | } else { | ||||
181 | painter->setBrush(option.palette.text().color()); | ||||
182 | } | ||||
183 | painter->drawText(option.rect.x() + previewRect.width() + 10, | ||||
184 | option.rect.y() + painter->fontMetrics().ascent() + 5, | ||||
185 | colorSet->name()); | ||||
186 | | ||||
187 | painter->restore(); | ||||
188 | } | ||||
189 | | ||||
190 | inline QSize KisPaletteListWidgetPrivate::Delegate::sizeHint(const QStyleOptionViewItem & option, | ||||
191 | const QModelIndex &) const | ||||
192 | { | ||||
193 | return option.decorationSize; | ||||
194 | } |