1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtgradientview.h"
5#include "qtgradientmanager.h"
6#include "qtgradientdialog.h"
7#include "qtgradientutils.h"
8
9#include <QtWidgets/QMessageBox>
10
11#include <QtGui/QAction>
12#include <QtGui/QClipboard>
13#include <QtGui/QPainter>
14
15QT_BEGIN_NAMESPACE
16
17using namespace Qt::StringLiterals;
18
19void QtGradientView::slotGradientAdded(const QString &id, const QGradient &gradient)
20{
21 QListWidgetItem *item = new QListWidgetItem(QtGradientUtils::gradientPixmap(gradient), id, m_ui.listWidget);
22 item->setToolTip(id);
23 item->setSizeHint(QSize(72, 84));
24 item->setFlags(item->flags() | Qt::ItemIsEditable);
25
26 m_idToItem[id] = item;
27 m_itemToId[item] = id;
28}
29
30void QtGradientView::slotGradientRenamed(const QString &id, const QString &newId)
31{
32 if (!m_idToItem.contains(id))
33 return;
34
35 QListWidgetItem *item = m_idToItem.value(id);
36 item->setText(newId);
37 item->setToolTip(newId);
38 m_itemToId[item] = newId;
39 m_idToItem.remove(id);
40 m_idToItem[newId] = item;
41}
42
43void QtGradientView::slotGradientChanged(const QString &id, const QGradient &newGradient)
44{
45 if (!m_idToItem.contains(id))
46 return;
47
48 QListWidgetItem *item = m_idToItem.value(id);
49 item->setIcon(QtGradientUtils::gradientPixmap(gradient: newGradient));
50}
51
52void QtGradientView::slotGradientRemoved(const QString &id)
53{
54 if (!m_idToItem.contains(id))
55 return;
56
57 QListWidgetItem *item = m_idToItem.value(id);
58 delete item;
59 m_itemToId.remove(item);
60 m_idToItem.remove(id);
61}
62
63void QtGradientView::slotNewGradient()
64{
65 bool ok;
66 QListWidgetItem *item = m_ui.listWidget->currentItem();
67 QGradient grad = QLinearGradient();
68 if (item)
69 grad = m_manager->gradients().value(m_itemToId.value(item));
70 QGradient gradient = QtGradientDialog::getGradient(&ok, grad, this);
71 if (!ok)
72 return;
73
74 QString id = m_manager->addGradient(id: tr(s: "Grad"), gradient);
75 m_ui.listWidget->setCurrentItem(m_idToItem.value(id));
76}
77
78void QtGradientView::slotEditGradient()
79{
80 bool ok;
81 QListWidgetItem *item = m_ui.listWidget->currentItem();
82 if (!item)
83 return;
84
85 const QString id = m_itemToId.value(item);
86 QGradient grad = m_manager->gradients().value(key: id);
87 QGradient gradient = QtGradientDialog::getGradient(&ok, grad, this);
88 if (!ok)
89 return;
90
91 m_manager->changeGradient(id, newGradient: gradient);
92}
93
94void QtGradientView::slotRemoveGradient()
95{
96 QListWidgetItem *item = m_ui.listWidget->currentItem();
97 if (!item)
98 return;
99
100 if (QMessageBox::question(this, tr(s: "Remove Gradient"),
101 tr(s: "Are you sure you want to remove the selected gradient?"),
102 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel) != QMessageBox::Yes)
103 return;
104
105 const QString id = m_itemToId.value(item);
106 m_manager->removeGradient(id);
107}
108
109void QtGradientView::slotRenameGradient()
110{
111 QListWidgetItem *item = m_ui.listWidget->currentItem();
112 if (!item)
113 return;
114
115 m_ui.listWidget->editItem(item);
116}
117
118void QtGradientView::slotRenameGradientItem(QListWidgetItem *item)
119{
120 if (!item)
121 return;
122
123 const QString id = m_itemToId.value(item);
124 m_manager->renameGradient(id, newId: item->text());
125}
126
127void QtGradientView::slotCurrentItemChanged(QListWidgetItem *item)
128{
129 m_editAction->setEnabled(item);
130 m_renameAction->setEnabled(item);
131 m_removeAction->setEnabled(item);
132 emit currentGradientChanged(m_itemToId.value(item));
133}
134
135void QtGradientView::slotGradientActivated(QListWidgetItem *item)
136{
137 const QString id = m_itemToId.value(item);
138 if (!id.isEmpty())
139 emit gradientActivated(id);
140}
141
142QtGradientView::QtGradientView(QWidget *parent)
143 : QWidget(parent)
144{
145 m_manager = nullptr;
146
147 m_ui.setupUi(this);
148
149 m_ui.listWidget->setViewMode(QListView::IconMode);
150 m_ui.listWidget->setMovement(QListView::Static);
151 m_ui.listWidget->setTextElideMode(Qt::ElideRight);
152 m_ui.listWidget->setResizeMode(QListWidget::Adjust);
153 m_ui.listWidget->setIconSize(QSize(64, 64));
154 m_ui.listWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
155
156 QPalette pal = m_ui.listWidget->viewport()->palette();
157 int pixSize = 18;
158 QPixmap pm(2 * pixSize, 2 * pixSize);
159
160 QColor c1 = palette().color(QPalette::Midlight);
161 QColor c2 = palette().color(QPalette::Dark);
162 QPainter pmp(&pm);
163 pmp.fillRect(x: 0, y: 0, w: pixSize, h: pixSize, b: c1);
164 pmp.fillRect(x: pixSize, y: pixSize, w: pixSize, h: pixSize, b: c1);
165 pmp.fillRect(x: 0, y: pixSize, w: pixSize, h: pixSize, b: c2);
166 pmp.fillRect(x: pixSize, y: 0, w: pixSize, h: pixSize, b: c2);
167
168 pal.setBrush(acr: QPalette::Base, abrush: QBrush(pm));
169 m_ui.listWidget->viewport()->setPalette(pal);
170
171 connect(m_ui.listWidget, &QListWidget::itemDoubleClicked, this, &QtGradientView::slotGradientActivated);
172 connect(m_ui.listWidget, &QListWidget::itemChanged, this, &QtGradientView::slotRenameGradientItem);
173 connect(m_ui.listWidget, &QListWidget::currentItemChanged, this, &QtGradientView::slotCurrentItemChanged);
174
175 m_newAction = new QAction(QIcon(":/qt-project.org/qtgradienteditor/images/plus.png"_L1), tr("New..."), this);
176 m_editAction = new QAction(QIcon(":/qt-project.org/qtgradienteditor/images/edit.png"_L1), tr("Edit..."), this);
177 m_renameAction = new QAction(tr("Rename"), this);
178 m_removeAction = new QAction(QIcon(":/qt-project.org/qtgradienteditor/images/minus.png"_L1), tr("Remove"), this);
179
180 connect(m_newAction, &QAction::triggered, this, &QtGradientView::slotNewGradient);
181 connect(m_editAction, &QAction::triggered, this, &QtGradientView::slotEditGradient);
182 connect(m_removeAction, &QAction::triggered, this, &QtGradientView::slotRemoveGradient);
183 connect(m_renameAction, &QAction::triggered, this, &QtGradientView::slotRenameGradient);
184
185 m_ui.listWidget->addAction(m_newAction);
186 m_ui.listWidget->addAction(m_editAction);
187 m_ui.listWidget->addAction(m_renameAction);
188 m_ui.listWidget->addAction(m_removeAction);
189
190 m_ui.newButton->setDefaultAction(m_newAction);
191 m_ui.editButton->setDefaultAction(m_editAction);
192 m_ui.renameButton->setDefaultAction(m_renameAction);
193 m_ui.removeButton->setDefaultAction(m_removeAction);
194
195 m_ui.listWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
196}
197
198void QtGradientView::setGradientManager(QtGradientManager *manager)
199{
200 if (m_manager == manager)
201 return;
202
203 if (m_manager) {
204 disconnect(sender: m_manager, signal: &QtGradientManager::gradientAdded,
205 receiver: this, slot: &QtGradientView::slotGradientAdded);
206 disconnect(sender: m_manager, signal: &QtGradientManager::gradientRenamed,
207 receiver: this, slot: &QtGradientView::slotGradientRenamed);
208 disconnect(sender: m_manager, signal: &QtGradientManager::gradientChanged,
209 receiver: this, slot: &QtGradientView::slotGradientChanged);
210 disconnect(sender: m_manager, signal: &QtGradientManager::gradientRemoved,
211 receiver: this, slot: &QtGradientView::slotGradientRemoved);
212
213 m_ui.listWidget->clear();
214 m_idToItem.clear();
215 m_itemToId.clear();
216 }
217
218 m_manager = manager;
219
220 if (!m_manager)
221 return;
222
223 QMap<QString, QGradient> gradients = m_manager->gradients();
224 for (auto itGrad = gradients.cbegin(), end = gradients.cend(); itGrad != end; ++itGrad)
225 slotGradientAdded(id: itGrad.key(), gradient: itGrad.value());
226
227 connect(m_manager, &QtGradientManager::gradientAdded,
228 this, &QtGradientView::slotGradientAdded);
229 connect(m_manager, &QtGradientManager::gradientRenamed,
230 this, &QtGradientView::slotGradientRenamed);
231 connect(m_manager, &QtGradientManager::gradientChanged,
232 this, &QtGradientView::slotGradientChanged);
233 connect(m_manager, &QtGradientManager::gradientRemoved,
234 this, &QtGradientView::slotGradientRemoved);
235}
236
237QtGradientManager *QtGradientView::gradientManager() const
238{
239 return m_manager;
240}
241
242void QtGradientView::setCurrentGradient(const QString &id)
243{
244 QListWidgetItem *item = m_idToItem.value(id);
245 if (!item)
246 return;
247
248 m_ui.listWidget->setCurrentItem(item);
249}
250
251QString QtGradientView::currentGradient() const
252{
253 return m_itemToId.value(m_ui.listWidget->currentItem());
254}
255
256QT_END_NAMESPACE
257

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