]> git.cworth.org Git - apitrace/blob - gui/pixelwidget.cpp
image: Add floating point support.
[apitrace] / gui / pixelwidget.cpp
1 /**
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
63 PixelWidget::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);
80     setAttribute(Qt::WA_NoBackground);
81 }
82
83 PixelWidget::~PixelWidget()
84 {
85 }
86
87 void PixelWidget::setSurface(const QImage &image)
88 {
89     m_surface = image;
90     updateGeometry();
91     update();
92 }
93
94 void PixelWidget::timerEvent(QTimerEvent *event)
95 {
96     if (event->timerId() == m_displayGridSizeId) {
97         killTimer(m_displayGridSizeId);
98         m_displayGridSize = false;
99     }
100 }
101
102 void 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
119 void 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());
128     p.drawImage(0, 0, m_surface);
129     p.scale(1/zoomValue(), 1/zoomValue());
130     p.restore();
131
132     // Draw the grid on top.
133     if (m_gridActive) {
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);
140     }
141
142     QFont f(QLatin1String("courier"));
143     f.setBold(true);
144     p.setFont(f);
145
146     if (m_displayGridSize) {
147         render_string(&p, w, h,
148             QString::fromLatin1("Grid size: %1").arg(m_gridSize),
149                       Qt::AlignBottom | Qt::AlignLeft);
150     }
151
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));
160         p.drawRect(r);
161         p.setPen(QPen(Qt::black, 1, Qt::SolidLine));
162         p.drawRect(r);
163
164         QRect rect(
165             int(r.x() / zoomValue()),
166             int(r.y() / zoomValue()),
167             int(r.width() / zoomValue()),
168             int(r.height() / zoomValue()));
169         emit gridGeometry(rect);
170     } else {
171         QRect empty;
172         emit gridGeometry(empty);
173     }
174
175
176 }
177
178 void PixelWidget::keyPressEvent(QKeyEvent *e)
179 {
180     switch (e->key()) {
181     case Qt::Key_Plus:
182         increaseZoom();
183         break;
184     case Qt::Key_Minus:
185         decreaseZoom();
186         break;
187     case Qt::Key_PageUp:
188         setGridSize(m_gridSize + 1);
189         break;
190     case Qt::Key_PageDown:
191         setGridSize(m_gridSize - 1);
192         break;
193     case Qt::Key_G:
194         toggleGrid();
195         break;
196     case Qt::Key_C:
197         if (e->modifiers() & Qt::ControlModifier)
198             copyToClipboard();
199         break;
200     case Qt::Key_S:
201         if (e->modifiers() & Qt::ControlModifier) {
202             releaseKeyboard();
203             saveToFile();
204         }
205         break;
206     case Qt::Key_Control:
207         grabKeyboard();
208         break;
209     }
210 }
211
212 void PixelWidget::keyReleaseEvent(QKeyEvent *e)
213 {
214     switch(e->key()) {
215     case Qt::Key_Control:
216         releaseKeyboard();
217         break;
218     default:
219         break;
220     }
221 }
222
223 void PixelWidget::resizeEvent(QResizeEvent *e)
224 {
225     QWidget::resizeEvent(e);
226 }
227
228 void PixelWidget::mouseMoveEvent(QMouseEvent *e)
229 {
230     if (m_mouseDown)
231         m_dragCurrent = e->pos();
232
233     int x = e->x() / zoomValue();
234     int y = e->y() / zoomValue();
235
236     if (x < m_surface.width() && y < m_surface.height() && x >= 0 && y >= 0) {
237         m_currentColor = m_surface.pixel(x, y);
238     } else
239         m_currentColor = QColor();
240
241     emit mousePosition(x, y);
242     update();
243 }
244
245 void PixelWidget::mousePressEvent(QMouseEvent *e)
246 {
247     if (e->button() == Qt::LeftButton)
248         m_mouseDown = true;
249     m_dragStart = e->pos();
250 }
251
252 void PixelWidget::mouseReleaseEvent(QMouseEvent *)
253 {
254     m_mouseDown = false;
255 }
256
257 void PixelWidget::contextMenuEvent(QContextMenuEvent *e)
258 {
259     QMenu menu;
260
261     QAction title(QLatin1String("Surface Pixel Tool"), &menu);
262     title.setEnabled(false);
263
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));
278
279     // Grid size options
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()));
286
287     // Zoom options
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()));
294
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(&copy, SIGNAL(triggered()), this, SLOT(copyToClipboard()));
303 #endif
304
305     menu.addAction(&title);
306     menu.addSeparator();
307     menu.addAction(&whiteGrid);
308     menu.addAction(&blackGrid);
309     menu.addAction(&noGrid);
310     menu.addSeparator();
311     menu.addAction(&incrGrid);
312     menu.addAction(&decrGrid);
313     menu.addSeparator();
314     menu.addAction(&incrZoom);
315     menu.addAction(&decrZoom);
316     menu.addSeparator();
317     menu.addAction(&save);
318 #ifndef QT_NO_CLIPBOARD
319     menu.addAction(&copy);
320 #endif
321
322     menu.exec(mapToGlobal(e->pos()));
323
324     if (noGrid.isChecked()) m_gridActive = 0;
325     else if (blackGrid.isChecked()) m_gridActive = 1;
326     else m_gridActive = 2;
327 }
328
329 QSize PixelWidget::sizeHint() const
330 {
331     if (m_surface.isNull())
332         return m_initialSize;
333
334     QSize sz(m_surface.width() * zoomValue(),
335              m_surface.height() * zoomValue());
336     return sz;
337 }
338
339 void PixelWidget::startGridSizeVisibleTimer()
340 {
341     if (m_gridActive) {
342         if (m_displayGridSizeId > 0)
343             killTimer(m_displayGridSizeId);
344         m_displayGridSizeId = startTimer(5000);
345         m_displayGridSize = true;
346         update();
347     }
348 }
349
350 void PixelWidget::setZoom(int zoom)
351 {
352     if (zoom > 0 && zoom != m_zoom) {
353         QPoint pos = m_lastMousePos;
354         m_lastMousePos = QPoint();
355         m_zoom = zoom;
356         m_lastMousePos = pos;
357         m_dragStart = m_dragCurrent = QPoint();
358
359         if (m_zoom == 1)
360             m_gridActive = 0;
361         else if (!m_gridActive)
362             m_gridActive = 1;
363
364         zoomChanged(m_zoom);
365         updateGeometry();
366         update();
367     }
368 }
369
370 void PixelWidget::toggleGrid()
371 {
372     if (++m_gridActive > 2)
373         m_gridActive = 0;
374     update();
375 }
376
377 void PixelWidget::setGridSize(int gridSize)
378 {
379     if (m_gridActive && gridSize > 0) {
380         m_gridSize = gridSize;
381         startGridSizeVisibleTimer();
382         update();
383     }
384 }
385
386 void PixelWidget::copyToClipboard()
387 {
388     QClipboard *cb = QApplication::clipboard();
389     cb->setImage(m_surface);
390 }
391
392 void PixelWidget::saveToFile()
393 {
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");
399     }
400 }
401
402 QColor PixelWidget::colorAtCurrentPosition() const
403 {
404     return m_currentColor;
405 }
406
407 #include "pixelwidget.moc"