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(id))
65 return;
66
67 QListWidgetItem *item = m_idToItem.value(id);
68 item->setText(newId);
69 item->setToolTip(newId);
70 m_itemToId[item] = newId;
71 m_idToItem.remove(id);
72 m_idToItem[newId] = item;
73}
74
75void QtGradientView::slotGradientChanged(const QString &id, const QGradient &newGradient)
76{
77 if (!m_idToItem.contains(id))
78 return;
79
80 QListWidgetItem *item = m_idToItem.value(id);
81 item->setIcon(QtGradientUtils::gradientPixmap(newGradient));
82}
83
84void QtGradientView::slotGradientRemoved(const QString &id)
85{
86 if (!m_idToItem.contains(id))
87 return;
88
89 QListWidgetItem *item = m_idToItem.value(id);
90 delete item;
91 m_itemToId.remove(item);
92 m_idToItem.remove(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(m_itemToId.value(item));
102 QGradient gradient = QtGradientDialog::getGradient(&ok, grad, this);
103 if (!ok)
104 return;
105
106 QString id = m_manager->addGradient(tr("Grad"), gradient);
107 m_ui.listWidget->setCurrentItem(m_idToItem.value(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(item);
118 QGradient grad = m_manager->gradients().value(id);
119 QGradient gradient = QtGradientDialog::getGradient(&ok, grad, this);
120 if (!ok)
121 return;
122
123 m_manager->changeGradient(id, gradient);
124}
125
126void QtGradientView::slotRemoveGradient()
127{
128 QListWidgetItem *item = m_ui.listWidget->currentItem();
129 if (!item)
130 return;
131
132 if (QMessageBox::question(this, tr("Remove Gradient"),
133 tr("Are you sure you want to remove the selected gradient?"),
134 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel) != QMessageBox::Yes)
135 return;
136
137 const QString id = m_itemToId.value(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(item);
156 m_manager->renameGradient(id, 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(m_itemToId.value(item));
165}
166
167void QtGradientView::slotGradientActivated(QListWidgetItem *item)
168{
169 const QString id = m_itemToId.value(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(QPalette::Midlight);
193 QColor c2 = palette().color(QPalette::Dark);
194 QPainter pmp(&pm);
195 pmp.fillRect(0, 0, pixSize, pixSize, c1);
196 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, c1);
197 pmp.fillRect(0, pixSize, pixSize, pixSize, c2);
198 pmp.fillRect(pixSize, 0, pixSize, pixSize, c2);
199
200 pal.setBrush(QPalette::Base, QBrush(pm));
201 m_ui.listWidget->viewport()->setPalette(pal);
202
203 connect(m_ui.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotGradientActivated(QListWidgetItem*)));
204 connect(m_ui.listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(slotRenameGradient(QListWidgetItem*)));
205 connect(m_ui.listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(slotCurrentItemChanged(QListWidgetItem*)));
206
207 m_newAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/plus.png")), tr("New..."), this);
208 m_editAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/edit.png")), tr("Edit..."), this);
209 m_renameAction = new QAction(tr("Rename"), this);
210 m_removeAction = new QAction(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/minus.png")), tr("Remove"), this);
211
212 connect(m_newAction, SIGNAL(triggered()), this, SLOT(slotNewGradient()));
213 connect(m_editAction, SIGNAL(triggered()), this, SLOT(slotEditGradient()));
214 connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveGradient()));
215 connect(m_renameAction, SIGNAL(triggered()), this, SLOT(slotRenameGradient()));
216
217 m_ui.listWidget->addAction(m_newAction);
218 m_ui.listWidget->addAction(m_editAction);
219 m_ui.listWidget->addAction(m_renameAction);
220 m_ui.listWidget->addAction(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(m_manager, SIGNAL(gradientAdded(QString,QGradient)),
237 this, SLOT(slotGradientAdded(QString,QGradient)));
238 disconnect(m_manager, SIGNAL(gradientRenamed(QString,QString)),
239 this, SLOT(slotGradientRenamed(QString,QString)));
240 disconnect(m_manager, SIGNAL(gradientChanged(QString,QGradient)),
241 this, SLOT(slotGradientChanged(QString,QGradient)));
242 disconnect(m_manager, SIGNAL(gradientRemoved(QString)),
243 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(itGrad.key(), itGrad.value());
258
259 connect(m_manager, SIGNAL(gradientAdded(QString,QGradient)),
260 this, SLOT(slotGradientAdded(QString,QGradient)));
261 connect(m_manager, SIGNAL(gradientRenamed(QString,QString)),
262 this, SLOT(slotGradientRenamed(QString,QString)));
263 connect(m_manager, SIGNAL(gradientChanged(QString,QGradient)),
264 this, SLOT(slotGradientChanged(QString,QGradient)));
265 connect(m_manager, SIGNAL(gradientRemoved(QString)),
266 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(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(m_ui.listWidget->currentItem());
286}
287
288QT_END_NAMESPACE
289