blob: 5a25c8ecfce375502c46e10ba92e4262f828333b [file] [log] [blame]
Zack Rusin66ce10a2013-09-10 20:30:59 -04001/**
2 * The source code is based on qpixeltool.cpp.
3 * Original license follows.
4 */
5
6/****************************************************************************
7**
8** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
9** Contact: http://www.qt-project.org/legal
10**
11** This file is part of the tools applications of the Qt Toolkit.
12**
13** $QT_BEGIN_LICENSE:LGPL$
14** Commercial License Usage
15** Licensees holding valid commercial Qt licenses may use this file in
16** accordance with the commercial license agreement provided with the
17** Software or, alternatively, in accordance with the terms contained in
18** a written agreement between you and Digia. For licensing terms and
19** conditions see http://qt.digia.com/licensing. For further information
20** use the contact form at http://qt.digia.com/contact-us.
21**
22** GNU Lesser General Public License Usage
23** Alternatively, this file may be used under the terms of the GNU Lesser
24** General Public License version 2.1 as published by the Free Software
25** Foundation and appearing in the file LICENSE.LGPL included in the
26** packaging of this file. Please review the following information to
27** ensure the GNU Lesser General Public License version 2.1 requirements
28** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
29**
30** In addition, as a special exception, Digia gives you certain additional
31** rights. These rights are described in the Digia Qt LGPL Exception
32** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
33**
34** GNU General Public License Usage
35** Alternatively, this file may be used under the terms of the GNU
36** General Public License version 3.0 as published by the Free Software
37** Foundation and appearing in the file LICENSE.GPL included in the
38** packaging of this file. Please review the following information to
39** ensure the GNU General Public License version 3.0 requirements will be
40** met: http://www.gnu.org/copyleft/gpl.html.
41**
42**
43** $QT_END_LICENSE$
44**
45****************************************************************************/
46
47#include "pixelwidget.h"
48
49#include <qapplication.h>
50#include <qdesktopwidget.h>
51#include <qapplication.h>
52#ifndef QT_NO_CLIPBOARD
53#include <qclipboard.h>
54#endif
55#include <qpainter.h>
56#include <qevent.h>
57#include <qfiledialog.h>
58#include <qmenu.h>
59#include <qactiongroup.h>
60
61#include <qdebug.h>
62
63PixelWidget::PixelWidget(QWidget *parent)
64 : QWidget(parent)
65{
66 setWindowTitle(QLatin1String("PixelTool"));
67 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
68
69 m_gridSize = 1;
70 m_gridActive = 0;
71 m_zoom = 1;
72 m_displayGridSize = false;
73 m_displayGridSizeId = 0;
74
75 m_mouseDown = false;
76
77 m_initialSize = QSize(250, 250);
78
79 setMouseTracking(true);
Zack Rusin8d6c36d2014-04-09 21:54:59 -040080 setAutoFillBackground(true);
Zack Rusin66ce10a2013-09-10 20:30:59 -040081}
82
83PixelWidget::~PixelWidget()
84{
85}
86
87void PixelWidget::setSurface(const QImage &image)
88{
89 m_surface = image;
90 updateGeometry();
91 update();
92}
93
94void PixelWidget::timerEvent(QTimerEvent *event)
95{
96 if (event->timerId() == m_displayGridSizeId) {
97 killTimer(m_displayGridSizeId);
98 m_displayGridSize = false;
99 }
100}
101
102void render_string(QPainter *p, int w, int h, const QString &text, int flags)
103{
104 p->setBrush(QColor(255, 255, 255, 191));
105 p->setPen(Qt::black);
106 QRect bounds;
107 p->drawText(0, 0, w, h, Qt::TextDontPrint | flags, text, &bounds);
108
109 if (bounds.x() == 0) bounds.adjust(0, 0, 10, 0);
110 else bounds.adjust(-10, 0, 0, 0);
111
112 if (bounds.y() == 0) bounds.adjust(0, 0, 0, 10);
113 else bounds.adjust(0, -10, 0, 0);
114
115 p->drawRect(bounds);
116 p->drawText(bounds, flags, text);
117}
118
119void PixelWidget::paintEvent(QPaintEvent *)
120{
121 QPainter p(this);
122
123 int w = width();
124 int h = height();
125
126 p.save();
127 p.scale(zoomValue(), zoomValue());
Zack Rusin8d6c36d2014-04-09 21:54:59 -0400128 p.setCompositionMode(QPainter::CompositionMode_SourceOver);
Zack Rusin66ce10a2013-09-10 20:30:59 -0400129 p.drawImage(0, 0, m_surface);
130 p.scale(1/zoomValue(), 1/zoomValue());
131 p.restore();
132
133 // Draw the grid on top.
134 if (m_gridActive) {
135 p.setPen(m_gridActive == 1 ? Qt::black : Qt::white);
136 int incr = m_gridSize * zoomValue();
137 for (int x=0; x<w; x+=incr)
138 p.drawLine(x, 0, x, h);
139 for (int y=0; y<h; y+=incr)
140 p.drawLine(0, y, w, y);
141 }
142
143 QFont f(QLatin1String("courier"));
144 f.setBold(true);
145 p.setFont(f);
146
147 if (m_displayGridSize) {
148 render_string(&p, w, h,
149 QString::fromLatin1("Grid size: %1").arg(m_gridSize),
150 Qt::AlignBottom | Qt::AlignLeft);
151 }
152
153 if (m_mouseDown && m_dragStart != m_dragCurrent) {
154 int x1 = (m_dragStart.x() / zoomValue()) * zoomValue();
155 int y1 = (m_dragStart.y() / zoomValue()) * zoomValue();
156 int x2 = (m_dragCurrent.x() / zoomValue()) * zoomValue();
157 int y2 = (m_dragCurrent.y() / zoomValue()) * zoomValue();
158 QRect r = QRect(x1, y1, x2 - x1, y2 - y1).normalized();
159 p.setBrush(Qt::NoBrush);
160 p.setPen(QPen(Qt::red, 3, Qt::SolidLine));
161 p.drawRect(r);
162 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
163 p.drawRect(r);
164
165 QRect rect(
166 int(r.x() / zoomValue()),
167 int(r.y() / zoomValue()),
168 int(r.width() / zoomValue()),
169 int(r.height() / zoomValue()));
170 emit gridGeometry(rect);
171 } else {
172 QRect empty;
173 emit gridGeometry(empty);
174 }
175
176
177}
178
179void PixelWidget::keyPressEvent(QKeyEvent *e)
180{
181 switch (e->key()) {
182 case Qt::Key_Plus:
183 increaseZoom();
184 break;
185 case Qt::Key_Minus:
186 decreaseZoom();
187 break;
188 case Qt::Key_PageUp:
189 setGridSize(m_gridSize + 1);
190 break;
191 case Qt::Key_PageDown:
192 setGridSize(m_gridSize - 1);
193 break;
194 case Qt::Key_G:
195 toggleGrid();
196 break;
197 case Qt::Key_C:
198 if (e->modifiers() & Qt::ControlModifier)
199 copyToClipboard();
200 break;
201 case Qt::Key_S:
202 if (e->modifiers() & Qt::ControlModifier) {
203 releaseKeyboard();
204 saveToFile();
205 }
206 break;
207 case Qt::Key_Control:
208 grabKeyboard();
209 break;
velorums206996a2020-05-23 20:38:52 +0200210 default:
211 QWidget::keyPressEvent(e);
212 break;
Zack Rusin66ce10a2013-09-10 20:30:59 -0400213 }
214}
215
216void PixelWidget::keyReleaseEvent(QKeyEvent *e)
217{
218 switch(e->key()) {
219 case Qt::Key_Control:
220 releaseKeyboard();
221 break;
222 default:
223 break;
224 }
225}
226
227void PixelWidget::resizeEvent(QResizeEvent *e)
228{
229 QWidget::resizeEvent(e);
230}
231
232void PixelWidget::mouseMoveEvent(QMouseEvent *e)
233{
234 if (m_mouseDown)
235 m_dragCurrent = e->pos();
236
237 int x = e->x() / zoomValue();
238 int y = e->y() / zoomValue();
239
240 if (x < m_surface.width() && y < m_surface.height() && x >= 0 && y >= 0) {
241 m_currentColor = m_surface.pixel(x, y);
242 } else
243 m_currentColor = QColor();
244
245 emit mousePosition(x, y);
246 update();
247}
248
249void PixelWidget::mousePressEvent(QMouseEvent *e)
250{
251 if (e->button() == Qt::LeftButton)
252 m_mouseDown = true;
253 m_dragStart = e->pos();
254}
255
256void PixelWidget::mouseReleaseEvent(QMouseEvent *)
257{
258 m_mouseDown = false;
259}
260
261void PixelWidget::contextMenuEvent(QContextMenuEvent *e)
262{
263 QMenu menu;
264
265 QAction title(QLatin1String("Surface Pixel Tool"), &menu);
266 title.setEnabled(false);
267
268 // Grid color options...
269 QActionGroup gridGroup(this);
270 QAction whiteGrid(QLatin1String("White grid"), &gridGroup);
271 whiteGrid.setCheckable(true);
272 whiteGrid.setChecked(m_gridActive == 2);
273 whiteGrid.setShortcut(QKeySequence(Qt::Key_G));
274 QAction blackGrid(QLatin1String("Black grid"), &gridGroup);
275 blackGrid.setCheckable(true);
276 blackGrid.setChecked(m_gridActive == 1);
277 blackGrid.setShortcut(QKeySequence(Qt::Key_G));
278 QAction noGrid(QLatin1String("No grid"), &gridGroup);
279 noGrid.setCheckable(true);
280 noGrid.setChecked(m_gridActive == 0);
281 noGrid.setShortcut(QKeySequence(Qt::Key_G));
282
283 // Grid size options
284 QAction incrGrid(QLatin1String("Increase grid size"), &menu);
285 incrGrid.setShortcut(QKeySequence(Qt::Key_PageUp));
286 connect(&incrGrid, SIGNAL(triggered()), this, SLOT(increaseGridSize()));
287 QAction decrGrid(QLatin1String("Decrease grid size"), &menu);
288 decrGrid.setShortcut(QKeySequence(Qt::Key_PageDown));
289 connect(&decrGrid, SIGNAL(triggered()), this, SLOT(decreaseGridSize()));
290
291 // Zoom options
292 QAction incrZoom(QLatin1String("Zoom in"), &menu);
293 incrZoom.setShortcut(QKeySequence(Qt::Key_Plus));
294 connect(&incrZoom, SIGNAL(triggered()), this, SLOT(increaseZoom()));
295 QAction decrZoom(QLatin1String("Zoom out"), &menu);
296 decrZoom.setShortcut(QKeySequence(Qt::Key_Minus));
297 connect(&decrZoom, SIGNAL(triggered()), this, SLOT(decreaseZoom()));
298
299 // Copy to clipboard / save
300 QAction save(QLatin1String("Save as image"), &menu);
301 save.setShortcut(QKeySequence(QLatin1String("Ctrl+S")));
302 connect(&save, SIGNAL(triggered()), this, SLOT(saveToFile()));
303#ifndef QT_NO_CLIPBOARD
304 QAction copy(QLatin1String("Copy to clipboard"), &menu);
305 copy.setShortcut(QKeySequence(QLatin1String("Ctrl+C")));
306 connect(&copy, SIGNAL(triggered()), this, SLOT(copyToClipboard()));
307#endif
308
309 menu.addAction(&title);
310 menu.addSeparator();
311 menu.addAction(&whiteGrid);
312 menu.addAction(&blackGrid);
313 menu.addAction(&noGrid);
314 menu.addSeparator();
315 menu.addAction(&incrGrid);
316 menu.addAction(&decrGrid);
317 menu.addSeparator();
318 menu.addAction(&incrZoom);
319 menu.addAction(&decrZoom);
320 menu.addSeparator();
321 menu.addAction(&save);
322#ifndef QT_NO_CLIPBOARD
323 menu.addAction(&copy);
324#endif
325
326 menu.exec(mapToGlobal(e->pos()));
327
328 if (noGrid.isChecked()) m_gridActive = 0;
329 else if (blackGrid.isChecked()) m_gridActive = 1;
330 else m_gridActive = 2;
331}
332
333QSize PixelWidget::sizeHint() const
334{
335 if (m_surface.isNull())
336 return m_initialSize;
337
338 QSize sz(m_surface.width() * zoomValue(),
339 m_surface.height() * zoomValue());
340 return sz;
341}
342
343void PixelWidget::startGridSizeVisibleTimer()
344{
345 if (m_gridActive) {
346 if (m_displayGridSizeId > 0)
347 killTimer(m_displayGridSizeId);
348 m_displayGridSizeId = startTimer(5000);
349 m_displayGridSize = true;
350 update();
351 }
352}
353
354void PixelWidget::setZoom(int zoom)
355{
356 if (zoom > 0 && zoom != m_zoom) {
357 QPoint pos = m_lastMousePos;
358 m_lastMousePos = QPoint();
359 m_zoom = zoom;
360 m_lastMousePos = pos;
361 m_dragStart = m_dragCurrent = QPoint();
362
363 if (m_zoom == 1)
364 m_gridActive = 0;
365 else if (!m_gridActive)
366 m_gridActive = 1;
367
368 zoomChanged(m_zoom);
369 updateGeometry();
370 update();
371 }
372}
373
374void PixelWidget::toggleGrid()
375{
376 if (++m_gridActive > 2)
377 m_gridActive = 0;
378 update();
379}
380
381void PixelWidget::setGridSize(int gridSize)
382{
383 if (m_gridActive && gridSize > 0) {
384 m_gridSize = gridSize;
385 startGridSizeVisibleTimer();
386 update();
387 }
388}
389
390void PixelWidget::copyToClipboard()
391{
392 QClipboard *cb = QApplication::clipboard();
393 cb->setImage(m_surface);
394}
395
396void PixelWidget::saveToFile()
397{
398 QString name = QFileDialog::getSaveFileName(this, QLatin1String("Save as image"), QString(), QLatin1String("*.png"));
399 if (!name.isEmpty()) {
400 if (!name.endsWith(QLatin1String(".png")))
401 name.append(QLatin1String(".png"));
402 m_surface.save(name, "PNG");
403 }
404}
405
406QColor PixelWidget::colorAtCurrentPosition() const
407{
408 return m_currentColor;
409}
410
411#include "pixelwidget.moc"