1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qtgradientview.h"
41#include "qtgradientmanager.h"
42#include "qtgradientdialog.h"
43#include "qtgradientutils.h"
44#include <QtGui/QClipboard>
45#include <QtGui/QPainter>
46#include <QtWidgets/QAction>
47#include <QtWidgets/QMessageBox>
48
49QT_BEGIN_NAMESPACE
50
51void QtGradientView::slotGradientAdded(const QString &id, const QGradient &gradient)
52{
53 QListWidgetItem *item = new QListWidgetItem(QtGradientUtils::gradientPixmap(gradient), id, m_ui.listWidget);
54 item->setToolTip(id);
55 item->setSizeHint(QSize(72, 84));
56 item->setFlags(item->flags() | Qt::ItemIsEditable);
57
58 m_idToItem[id] = item;
59 m_itemToId[item] = id;
60}
61
62void QtGradientView::slotGradientRenamed(const QString &id, const QString &newId)
63{
64 if (!m_idToItem.contains(akey: id))
65 return;
66
67 QListWidgetItem *item = m_idToItem.value(akey: id);
68 item->setText(newId);
69 item->setToolTip(newId);
70 m_itemToId[item] = newId;
71 m_idToItem.remove(akey: id);
72 m_idToItem[newId] = item;
73}
74
75void QtGradientView::slotGradientChanged(const QString &id, const QGradient &newGradient)
76{
77 if (!m_idToItem.contains(akey: id))
78 return;
79
80 QListWidgetItem *item = m_idToItem.value(akey: id);
81 item->setIcon(QtGradientUtils::gradientPixmap(gradient: newGradient));
82}
83
84void QtGradientView::slotGradientRemoved(const QString &id)
85{
86 if (!m_idToItem.contains(akey: id))
87 return;
88
89 QListWidgetItem *item = m_idToItem.value(akey: id);
90 delete item;
91 m_itemToId.remove(akey: item);
92 m_idToItem.remove(akey: id);
93}
94
95void QtGradientView::slotNewGradient()
96{
97 bool ok;
98 QListWidgetItem *item = m_ui.listWidget->currentItem();
99 QGradient grad = QLinearGradient();
100 if (item)
101 grad = m_manager->gradients().value(akey: m_itemToId.value(akey: item));
102 QGradient gradient = QtGradientDialog::getGradient(ok: &ok, initial: grad, parent: this);
103 if (!ok)
104 return;
105
106 QString id = m_manager->addGradient(id: tr(s: "Grad"), gradient);
107 m_ui.listWidget->setCurrentItem(m_idToItem.value(akey: id));
108}
109
110void QtGradientView::slotEditGradient()
111{
112 bool ok;
113 QListWidgetItem *item = m_ui.listWidget->currentItem();
114 if (!item)
115 return;
116
117 const QString id = m_itemToId.value(akey: item);
118 QGradient grad = m_manager->gradients().value(akey: id);
119 QGradient gradient = QtGradientDialog::getGradient(ok: &ok, initial: grad, parent: this);
120 if (!ok)
121 return;
122
123 m_manager->changeGradient(id, newGradient: gradient);
124}
125
126void QtGradientView::slotRemoveGradient()
127{
128 QListWidgetItem *item = m_ui.listWidget->currentItem();
129 if (!item)
130 return;
131
132 if (QMessageBox::question(parent: this, title: tr(s: "Remove Gradient"),
133 text: tr(s: "Are you sure you want to remove the selected gradient?"),
134 buttons: QMessageBox::Yes | QMessageBox::Cancel, defaultButton: QMessageBox::Cancel) != QMessageBox::Yes)
135 return;
136
137 const QString id = m_itemToId.value(akey: item);
138 m_manager->removeGradient(id);
139}
140
141void QtGradientView::slotRenameGradient()
142{
143 QListWidgetItem *item = m_ui.listWidget->currentItem();
144 if (!item)
145 return;
146
147 m_ui.listWidget->editItem(item);
148}
149
150void QtGradientView::slotRenameGradient(QListWidgetItem *item)
151{
152 if (!item)
153 return;
154
155 const QString id = m_itemToId.value(akey: item);
156 m_manager->renameGradient(id, newId: item->text());
157}
158
159void QtGradientView::slotCurrentItemChanged(QListWidgetItem *item)
160{
161 m_editAction->setEnabled(item);
162 m_renameAction->setEnabled(item);
163 m_removeAction->setEnabled(item);
164 emit currentGradientChanged(id: m_itemToId.value(akey: item));
165}
166
167void QtGradientView::slotGradientActivated(QListWidgetItem *item)
168{
169 const QString id = m_itemToId.value(akey: item);
170 if (!id.isEmpty())
171 emit gradientActivated(id);
172}
173
174QtGradientView::QtGradientView(QWidget *parent)
175 : QWidget(parent)
176{
177 m_manager = 0;
178
179 m_ui.setupUi(this);
180
181 m_ui.listWidget->setViewMode(QListView::IconMode);
182 m_ui.listWidget->setMovement(QListView::Static);
183 m_ui.listWidget->setTextElideMode(Qt::ElideRight);
184 m_ui.listWidget->setResizeMode(QListWidget::Adjust);
185 m_ui.listWidget->setIconSize(QSize(64, 64));
186 m_ui.listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
187
188 QPalette pal = m_ui.listWidget->viewport()->palette();
189 int pixSize = 18;
190 QPixmap pm(2 * pixSize, 2 * pixSize);
191
192 QColor c1 = palette().color(cr: QPalette::Midlight);
193 QColor c2 = palette().color(cr: QPalette::Dark);
194 QPainter pmp(&pm);
195 pmp.fillRect(x: 0, y: 0, w: pixSize, h: pixSize, b: c1);
196 pmp.fillRect(x: pixSize, y: pixSize, w: pixSize, h: pixSize, b: c1);
197 pmp.fillRect(x: 0, y: pixSize, w: pixSize, h: pixSize, b: c2);
198 pmp.fillRect(x: pixSize, y: 0, w: pixSize, h: pixSize, b: c2);
199
200 pal.setBrush(acr: QPalette::Base, abrush: QBrush(pm));
201 m_ui.listWidget->viewport()->setPalette(pal);
202
203 connect(sender: m_ui.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), receiver: this, SLOT(slotGradientActivated(QListWidgetItem*)));
204 connect(sender: m_ui.listWidget, SIGNAL(itemChanged(QListWidgetItem*)), receiver: this, SLOT(slotRenameGradient(QListWidgetItem*)));
205 connect(sender: m_ui.listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), receiver: this, SLOT(slotCurrentItemChanged(QListWidgetItem*)));
206
207 m_newAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/plus.png")), tr(s: "New..."), this);
208 m_editAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/edit.png")), tr(s: "Edit..."), this);
209 m_renameAction = new QAction(tr(s: "Rename"), this);
210 m_removeAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/minus.png")), tr(s: "Remove"), this);
211
212 connect(sender: m_newAction, SIGNAL(triggered()), receiver: this, SLOT(slotNewGradient()));
213 connect(sender: m_editAction, SIGNAL(triggered()), receiver: this, SLOT(slotEditGradient()));
214 connect(sender: m_removeAction, SIGNAL(triggered()), receiver: this, SLOT(slotRemoveGradient()));
215 connect(sender: m_renameAction, SIGNAL(triggered()), receiver: this, SLOT(slotRenameGradient()));
216
217 m_ui.listWidget->addAction(action: m_newAction);
218 m_ui.listWidget->addAction(action: m_editAction);
219 m_ui.listWidget->addAction(action: m_renameAction);
220 m_ui.listWidget->addAction(action: m_removeAction);
221
222 m_ui.newButton->setDefaultAction(m_newAction);
223 m_ui.editButton->setDefaultAction(m_editAction);
224 m_ui.renameButton->setDefaultAction(m_renameAction);
225 m_ui.removeButton->setDefaultAction(m_removeAction);
226
227 m_ui.listWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
228}
229
230void QtGradientView::setGradientManager(QtGradientManager *manager)
231{
232 if (m_manager == manager)
233 return;
234
235 if (m_manager) {
236 disconnect(sender: m_manager, SIGNAL(gradientAdded(QString,QGradient)),
237 receiver: this, SLOT(slotGradientAdded(QString,QGradient)));
238 disconnect(sender: m_manager, SIGNAL(gradientRenamed(QString,QString)),
239 receiver: this, SLOT(slotGradientRenamed(QString,QString)));
240 disconnect(sender: m_manager, SIGNAL(gradientChanged(QString,QGradient)),
241 receiver: this, SLOT(slotGradientChanged(QString,QGradient)));
242 disconnect(sender: m_manager, SIGNAL(gradientRemoved(QString)),
243 receiver: this, SLOT(slotGradientRemoved(QString)));
244
245 m_ui.listWidget->clear();
246 m_idToItem.clear();
247 m_itemToId.clear();
248 }
249
250 m_manager = manager;
251
252 if (!m_manager)
253 return;
254
255 QMap<QString, QGradient> gradients = m_manager->gradients();
256 for (auto itGrad = gradients.cbegin(), end = gradients.cend(); itGrad != end; ++itGrad)
257 slotGradientAdded(id: itGrad.key(), gradient: itGrad.value());
258
259 connect(sender: m_manager, SIGNAL(gradientAdded(QString,QGradient)),
260 receiver: this, SLOT(slotGradientAdded(QString,QGradient)));
261 connect(sender: m_manager, SIGNAL(gradientRenamed(QString,QString)),
262 receiver: this, SLOT(slotGradientRenamed(QString,QString)));
263 connect(sender: m_manager, SIGNAL(gradientChanged(QString,QGradient)),
264 receiver: this, SLOT(slotGradientChanged(QString,QGradient)));
265 connect(sender: m_manager, SIGNAL(gradientRemoved(QString)),
266 receiver: this, SLOT(slotGradientRemoved(QString)));
267}
268
269QtGradientManager *QtGradientView::gradientManager() const
270{
271 return m_manager;
272}
273
274void QtGradientView::setCurrentGradient(const QString &id)
275{
276 QListWidgetItem *item = m_idToItem.value(akey: id);
277 if (!item)
278 return;
279
280 m_ui.listWidget->setCurrentItem(item);
281}
282
283QString QtGradientView::currentGradient() const
284{
285 return m_itemToId.value(akey: m_ui.listWidget->currentItem());
286}
287
288QT_END_NAMESPACE
289

source code of qttools/src/shared/qtgradienteditor/qtgradientview.cpp