2 * The source code is based on qpixeltool.cpp.
3 * Original license follows.
6 /****************************************************************************
8 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
9 ** Contact: http://www.qt-project.org/legal
11 ** This file is part of the tools applications of the Qt Toolkit.
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.
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.
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.
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.
45 ****************************************************************************/
47 #include "pixelwidget.h"
49 #include <qapplication.h>
50 #include <qdesktopwidget.h>
51 #include <qapplication.h>
52 #ifndef QT_NO_CLIPBOARD
53 #include <qclipboard.h>
57 #include <qfiledialog.h>
59 #include <qactiongroup.h>
63 PixelWidget::PixelWidget(QWidget *parent)
66 setWindowTitle(QLatin1String("PixelTool"));
67 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
72 m_displayGridSize = false;
73 m_displayGridSizeId = 0;
77 m_initialSize = QSize(250, 250);
79 setMouseTracking(true);
80 setAttribute(Qt::WA_NoBackground);
83 PixelWidget::~PixelWidget()
87 void PixelWidget::setSurface(const QImage &image)
94 void PixelWidget::timerEvent(QTimerEvent *event)
96 if (event->timerId() == m_displayGridSizeId) {
97 killTimer(m_displayGridSizeId);
98 m_displayGridSize = false;
102 void render_string(QPainter *p, int w, int h, const QString &text, int flags)
104 p->setBrush(QColor(255, 255, 255, 191));
105 p->setPen(Qt::black);
107 p->drawText(0, 0, w, h, Qt::TextDontPrint | flags, text, &bounds);
109 if (bounds.x() == 0) bounds.adjust(0, 0, 10, 0);
110 else bounds.adjust(-10, 0, 0, 0);
112 if (bounds.y() == 0) bounds.adjust(0, 0, 0, 10);
113 else bounds.adjust(0, -10, 0, 0);
116 p->drawText(bounds, flags, text);
119 void PixelWidget::paintEvent(QPaintEvent *)
127 p.scale(zoomValue(), zoomValue());
128 p.drawImage(0, 0, m_surface);
129 p.scale(1/zoomValue(), 1/zoomValue());
132 // Draw the grid on top.
134 p.setPen(m_gridActive == 1 ? Qt::black : Qt::white);
135 int incr = m_gridSize * zoomValue();
136 for (int x=0; x<w; x+=incr)
137 p.drawLine(x, 0, x, h);
138 for (int y=0; y<h; y+=incr)
139 p.drawLine(0, y, w, y);
142 QFont f(QLatin1String("courier"));
146 if (m_displayGridSize) {
147 render_string(&p, w, h,
148 QString::fromLatin1("Grid size: %1").arg(m_gridSize),
149 Qt::AlignBottom | Qt::AlignLeft);
152 if (m_mouseDown && m_dragStart != m_dragCurrent) {
153 int x1 = (m_dragStart.x() / zoomValue()) * zoomValue();
154 int y1 = (m_dragStart.y() / zoomValue()) * zoomValue();
155 int x2 = (m_dragCurrent.x() / zoomValue()) * zoomValue();
156 int y2 = (m_dragCurrent.y() / zoomValue()) * zoomValue();
157 QRect r = QRect(x1, y1, x2 - x1, y2 - y1).normalized();
158 p.setBrush(Qt::NoBrush);
159 p.setPen(QPen(Qt::red, 3, Qt::SolidLine));
161 p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
165 int(r.x() / zoomValue()),
166 int(r.y() / zoomValue()),
167 int(r.width() / zoomValue()),
168 int(r.height() / zoomValue()));
169 emit gridGeometry(rect);
172 emit gridGeometry(empty);
178 void PixelWidget::keyPressEvent(QKeyEvent *e)
188 setGridSize(m_gridSize + 1);
190 case Qt::Key_PageDown:
191 setGridSize(m_gridSize - 1);
197 if (e->modifiers() & Qt::ControlModifier)
201 if (e->modifiers() & Qt::ControlModifier) {
206 case Qt::Key_Control:
212 void PixelWidget::keyReleaseEvent(QKeyEvent *e)
215 case Qt::Key_Control:
223 void PixelWidget::resizeEvent(QResizeEvent *e)
225 QWidget::resizeEvent(e);
228 void PixelWidget::mouseMoveEvent(QMouseEvent *e)
231 m_dragCurrent = e->pos();
233 int x = e->x() / zoomValue();
234 int y = e->y() / zoomValue();
236 if (x < m_surface.width() && y < m_surface.height() && x >= 0 && y >= 0) {
237 m_currentColor = m_surface.pixel(x, y);
239 m_currentColor = QColor();
241 emit mousePosition(x, y);
245 void PixelWidget::mousePressEvent(QMouseEvent *e)
247 if (e->button() == Qt::LeftButton)
249 m_dragStart = e->pos();
252 void PixelWidget::mouseReleaseEvent(QMouseEvent *)
257 void PixelWidget::contextMenuEvent(QContextMenuEvent *e)
261 QAction title(QLatin1String("Surface Pixel Tool"), &menu);
262 title.setEnabled(false);
264 // Grid color options...
265 QActionGroup gridGroup(this);
266 QAction whiteGrid(QLatin1String("White grid"), &gridGroup);
267 whiteGrid.setCheckable(true);
268 whiteGrid.setChecked(m_gridActive == 2);
269 whiteGrid.setShortcut(QKeySequence(Qt::Key_G));
270 QAction blackGrid(QLatin1String("Black grid"), &gridGroup);
271 blackGrid.setCheckable(true);
272 blackGrid.setChecked(m_gridActive == 1);
273 blackGrid.setShortcut(QKeySequence(Qt::Key_G));
274 QAction noGrid(QLatin1String("No grid"), &gridGroup);
275 noGrid.setCheckable(true);
276 noGrid.setChecked(m_gridActive == 0);
277 noGrid.setShortcut(QKeySequence(Qt::Key_G));
280 QAction incrGrid(QLatin1String("Increase grid size"), &menu);
281 incrGrid.setShortcut(QKeySequence(Qt::Key_PageUp));
282 connect(&incrGrid, SIGNAL(triggered()), this, SLOT(increaseGridSize()));
283 QAction decrGrid(QLatin1String("Decrease grid size"), &menu);
284 decrGrid.setShortcut(QKeySequence(Qt::Key_PageDown));
285 connect(&decrGrid, SIGNAL(triggered()), this, SLOT(decreaseGridSize()));
288 QAction incrZoom(QLatin1String("Zoom in"), &menu);
289 incrZoom.setShortcut(QKeySequence(Qt::Key_Plus));
290 connect(&incrZoom, SIGNAL(triggered()), this, SLOT(increaseZoom()));
291 QAction decrZoom(QLatin1String("Zoom out"), &menu);
292 decrZoom.setShortcut(QKeySequence(Qt::Key_Minus));
293 connect(&decrZoom, SIGNAL(triggered()), this, SLOT(decreaseZoom()));
295 // Copy to clipboard / save
296 QAction save(QLatin1String("Save as image"), &menu);
297 save.setShortcut(QKeySequence(QLatin1String("Ctrl+S")));
298 connect(&save, SIGNAL(triggered()), this, SLOT(saveToFile()));
299 #ifndef QT_NO_CLIPBOARD
300 QAction copy(QLatin1String("Copy to clipboard"), &menu);
301 copy.setShortcut(QKeySequence(QLatin1String("Ctrl+C")));
302 connect(©, SIGNAL(triggered()), this, SLOT(copyToClipboard()));
305 menu.addAction(&title);
307 menu.addAction(&whiteGrid);
308 menu.addAction(&blackGrid);
309 menu.addAction(&noGrid);
311 menu.addAction(&incrGrid);
312 menu.addAction(&decrGrid);
314 menu.addAction(&incrZoom);
315 menu.addAction(&decrZoom);
317 menu.addAction(&save);
318 #ifndef QT_NO_CLIPBOARD
319 menu.addAction(©);
322 menu.exec(mapToGlobal(e->pos()));
324 if (noGrid.isChecked()) m_gridActive = 0;
325 else if (blackGrid.isChecked()) m_gridActive = 1;
326 else m_gridActive = 2;
329 QSize PixelWidget::sizeHint() const
331 if (m_surface.isNull())
332 return m_initialSize;
334 QSize sz(m_surface.width() * zoomValue(),
335 m_surface.height() * zoomValue());
339 void PixelWidget::startGridSizeVisibleTimer()
342 if (m_displayGridSizeId > 0)
343 killTimer(m_displayGridSizeId);
344 m_displayGridSizeId = startTimer(5000);
345 m_displayGridSize = true;
350 void PixelWidget::setZoom(int zoom)
352 if (zoom > 0 && zoom != m_zoom) {
353 QPoint pos = m_lastMousePos;
354 m_lastMousePos = QPoint();
356 m_lastMousePos = pos;
357 m_dragStart = m_dragCurrent = QPoint();
361 else if (!m_gridActive)
370 void PixelWidget::toggleGrid()
372 if (++m_gridActive > 2)
377 void PixelWidget::setGridSize(int gridSize)
379 if (m_gridActive && gridSize > 0) {
380 m_gridSize = gridSize;
381 startGridSizeVisibleTimer();
386 void PixelWidget::copyToClipboard()
388 QClipboard *cb = QApplication::clipboard();
389 cb->setImage(m_surface);
392 void PixelWidget::saveToFile()
394 QString name = QFileDialog::getSaveFileName(this, QLatin1String("Save as image"), QString(), QLatin1String("*.png"));
395 if (!name.isEmpty()) {
396 if (!name.endsWith(QLatin1String(".png")))
397 name.append(QLatin1String(".png"));
398 m_surface.save(name, "PNG");
402 QColor PixelWidget::colorAtCurrentPosition() const
404 return m_currentColor;
407 #include "pixelwidget.moc"