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