-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsquareImageLabel.cpp
297 lines (274 loc) · 10.5 KB
/
squareImageLabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// Copyright 2010, 2011 Tom Klein.
//
// This file is part of cstitch.
//
// cstitch is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "squareImageLabel.h"
#include "utility.h"
#include <QtCore/QDebug>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
void squareImageLabel::paintEvent(QPaintEvent* event) {
QPainter painter(this);
// if we're not currently drawing a mouse drag edit, draw the underlying
// image
if (!drawingSquares_ && !drawingHashes_) {
if (imageIsFlat()) { // paint the entire image at once
const QRectF viewRectangle(QRectF(event->rect()));
painter.drawPixmap(viewRectangle, scaledImage_, viewRectangle);
if (imageIsOriginal_) {
// no decorations for the original image
event->accept();
return;
}
}
else { // draw a square image
// we only draw the portion of the image in the viewing rectangle
const QRect viewRect = event->rect();
const int xBoxStart = viewRect.x()/scaledDimension_;
const int xStart = xBoxStart * scaledDimension_;
const int xEnd = viewRect.x() + viewRect.width();
const int yBoxStart = viewRect.y()/scaledDimension_;
const int yStart = yBoxStart * scaledDimension_;
const int yEnd = viewRect.y() + viewRect.height();
const int originalDimension = baseImage_.width()/xSquareCount_;
for (int j = yStart, yBox = yBoxStart;
j < yEnd; j += scaledDimension_, ++yBox) {
for (int i = xStart, xBox = xBoxStart;
i < xEnd; i += scaledDimension_, ++xBox) {
painter.drawPixmap
(QPoint(i, j),
colorSquares_[baseImage_.pixel(xBox*originalDimension,
yBox*originalDimension)]);
}
}
}
}
if (drawingSquares_) {
// there's no need to redraw squares we've already drawn, so start
// with the new ones
// NOTE: this code assumes there could be more than one addSquare
// between paint events. Our code calls update after each addSquare,
// so we would expect a paint event after each add, but paintEvent
// documentation says paintEvents can get bundled for speed
// optimization, so to be on the safe side, don't assume they haven't
// been!
for (int i = lastSquareDrawn_ + 1, size = drawSquares_.size();
i < size; ++i) {
const int boxX = drawSquares_[i].x();
const int boxY = drawSquares_[i].y();
const int xStart = boxX * scaledDimension_;
const int xEnd = xStart + scaledDimension_;
const int xWidth = xEnd - xStart;
const int yStart = boxY * scaledDimension_;
const int yEnd = yStart + scaledDimension_;
const int yWidth = yEnd - yStart;
painter.fillRect(QRect(xStart, yStart, xWidth, yWidth),
QColor(squareColor_));
}
lastSquareDrawn_ = drawSquares_.size() - 1;
}
else if (!hashSquaresToBeRemoved_.isEmpty()) {
for (int i = 0, size = hashSquaresToBeRemoved_.size();
i < size; ++i) {
const int boxX = hashSquaresToBeRemoved_[i].x();
const int boxY = hashSquaresToBeRemoved_[i].y();
const qreal xStart = boxX * scaledDimension_;
const qreal xEnd = xStart + scaledDimension_;
const qreal yStart = boxY * scaledDimension_;
const qreal yEnd = yStart + scaledDimension_;
const int xStartInt = static_cast<int>(xStart);
const int xEndInt = static_cast<int>(xEnd);
const int xWidth = xEndInt - xStartInt;
const int yStartInt = static_cast<int>(yStart);
const int yEndInt = static_cast<int>(yEnd);
const int yWidth = yEndInt - yStartInt;
QColor fillColor = hashSquaresToBeRemoved_[i].color();
painter.fillRect(QRect(xStartInt, yStartInt, xWidth, yWidth),
fillColor);
}
hashSquaresToBeRemoved_.clear();
}
else if (!hashSquares_.isEmpty()) { // don't just check drawingHashes_
// there are two reasons to draw hashes here: either we're actively
// dragging a detail draw, or the detail tool is active and there are
// some dragged hashes but we're not actively dragging and we're
// being redrawn for some other reason (like a scroll)
int hashSquaresStart = 0;
if (drawingHashes_) {
hashSquaresStart = lastHashDrawn_ + 1;
}
for (int i = hashSquaresStart, size = hashSquares_.size();
i < size; ++i) {
const int boxX = hashSquares_[i].x();
const int boxY = hashSquares_[i].y();
const qreal xStart = boxX * scaledDimension_;
const qreal xEnd = xStart + scaledDimension_;
const qreal yStart = boxY * scaledDimension_;
const qreal yEnd = yStart + scaledDimension_;
const int xStartInt = static_cast<int>(xStart);
const int xEndInt = static_cast<int>(xEnd);
const int yStartInt = static_cast<int>(yStart);
const int yEndInt = static_cast<int>(yEnd);
painter.setPen(hashSquares_[i].color());
painter.drawLine(xStartInt, yStartInt,
xEndInt, yEndInt);
painter.drawLine(xStartInt, yEndInt,
xEndInt, yStartInt);
}
lastHashDrawn_ = hashSquares_.size() - 1;
}
if (gridOn_ && scaledDimension_ > 1) {
painter.setPen(QPen(QColor(gridColor_), 1));
const QRect eventRectangle(event->rect());
const int xStart = eventRectangle.x();
const int xEnd = xStart + eventRectangle.width();
const int yStart = eventRectangle.y();
const int yEnd = yStart + eventRectangle.height();
for (int i = (xStart/scaledDimension_)*scaledDimension_; i <= xEnd;
i += scaledDimension_) {
painter.drawLine(QPoint(i, yStart), QPoint(i, yEnd));
}
for (int j = (yStart/scaledDimension_)*scaledDimension_; j <= yEnd;
j += scaledDimension_) {
painter.drawLine(QPoint(xStart, j), QPoint(xEnd, j));
}
}
}
void squareImageLabel::removeHashSquare(const pixel& p) {
// (pixels identify on coordinates, not colors)
if (hashSquares_.removeOne(p)) {
hashSquaresToBeRemoved_.push_back(p);
--lastHashDrawn_;
}
else {
qWarning() << "Pixel not found on remove" << hashSquares_.size() <<
p.x() << p.y() << ctos(p.color());
}
}
bool squareImageLabel::clearHashes() {
drawingHashes_ = false;
lastHashDrawn_ = -1;
if (!hashSquaresToBeRemoved_.isEmpty()) {
repaint();
hashSquaresToBeRemoved_.clear();
}
if (hashSquares_.isEmpty()) {
return false;
}
else {
hashSquares_.clear();
update();
return true;
}
}
void squareImageLabel::setNewImage(const QImage& image,
const QList<QRgb>& colors,
int xSquareCount, int ySquareCount,
bool imageIsOriginal) {
baseImage_ = image;
imageIsOriginal_ = imageIsOriginal;
setAttribute(Qt::WA_OpaquePaintEvent, !image.hasAlphaChannel());
resize(image.size());
xSquareCount_ = xSquareCount;
ySquareCount_ = ySquareCount;
scaledDimension_ = image.width()/xSquareCount_;
generateColorSquares(colors);
if (imageIsFlat()) {
scaledImage_ = QPixmap::fromImage(baseImage_);
}
else {
scaledImage_ = QPixmap();
}
}
void squareImageLabel::updateImage(const QImage& image, const QList<QRgb>& colors,
const QRect& updateRectangle) {
baseImage_ = image;
generateColorSquares(colors);
if (updateRectangle.isNull()) {
update();
}
else {
update(updateRectangle);
}
}
void squareImageLabel::setImageSize(const QSize& newSize) {
if (imageIsFlat()) {
xSquareCount_ = newSize.width();
ySquareCount_ = newSize.height();
scaledImage_ = QPixmap::fromImage(baseImage_).scaled(newSize);
}
else {
scaledDimension_ = qMax(newSize.width()/xSquareCount_, 1);
Q_ASSERT_X(newSize.width() % scaledDimension_ == 0 &&
newSize.height() % scaledDimension_ == 0, "setImageSize",
QString("Bad scaled square image size: (" +
::itoqs(newSize.width()) +
", " + ::itoqs(newSize.height()) + ") :: " +
::itoqs(xSquareCount_) + "; " +
::itoqs(baseImage_.size().width()) + "x" +
::itoqs(baseImage_.size().height())).
toStdString().c_str());
generateColorSquares(colorSquares_.keys());
}
resize(newSize);
update();
}
void squareImageLabel::setImageWidth(int newWidth) {
scaledDimension_ = qMax(newWidth/xSquareCount_, 1);
if (imageIsFlat()) {
scaledImage_ = QPixmap::fromImage(baseImage_).scaledToWidth(newWidth);
}
else {
Q_ASSERT_X(newWidth % scaledDimension_ == 0, "setImageWidth",
QString("Bad scaled square image width: " +
::itoqs(newWidth) + " " +
::itoqs(scaledDimension_) + "; " +
::itoqs(baseImage_.size().width()) + "x" +
::itoqs(baseImage_.size().height())).
toStdString().c_str());
generateColorSquares(colorSquares_.keys());
}
resize(size());
update();
}
void squareImageLabel::setImageHeight(int newHeight) {
scaledDimension_ = qMax(newHeight/ySquareCount_, 1);
if (imageIsFlat()) {
scaledImage_ = QPixmap::fromImage(baseImage_).scaledToHeight(newHeight);
}
else {
Q_ASSERT_X(newHeight % scaledDimension_ == 0, "setImageHeight",
QString("Bad scaled square image height: " +
::itoqs(newHeight) + " " +
::itoqs(ySquareCount_) + "; " +
::itoqs(baseImage_.size().width()) + "x" +
::itoqs(baseImage_.size().height())).
toStdString().c_str());
generateColorSquares(colorSquares_.keys());
}
resize(size());
update();
}
void squareImageLabel::generateColorSquares(const QList<QRgb>& colors) {
colorSquares_.clear();
for (int i = 0, size = colors.size(); i < size; ++i) {
QPixmap thisSquare(scaledDimension_, scaledDimension_);
const QRgb thisColor = colors[i];
thisSquare.fill(thisColor);
colorSquares_[thisColor] = thisSquare;
}
}