Skip to content

Commit b0acdd3

Browse files
authored
Fixed a problem in rectangular annotation (#699)
Fixes #698
1 parent de8df57 commit b0acdd3

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/imageview.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ ImageView::ImageView(QWidget* parent):
5151
autoZoomFit_(false),
5252
smoothOnZoom_(true),
5353
isSVG(false),
54-
currentTool(ToolNone),
55-
nextNumber(1),
54+
currentTool_(ToolNone),
55+
nextNumber_(1),
5656
showOutline_(false) {
5757

5858
setViewportMargins(0, 0, 0, 0);
@@ -125,19 +125,19 @@ void ImageView::mouseDoubleClickEvent(QMouseEvent* event) {
125125
}
126126

127127
void ImageView::mousePressEvent(QMouseEvent * event) {
128-
if(currentTool == ToolNone) {
128+
if(currentTool_ == ToolNone) {
129129
QGraphicsView::mousePressEvent(event);
130130
if(cursorTimer_) {
131131
cursorTimer_->stop();
132132
}
133133
}
134134
else {
135-
startPoint = mapToScene(event->position().toPoint()).toPoint();
135+
startPoint_ = mapToScene(event->position().toPoint()).toPoint();
136136
}
137137
}
138138

139139
void ImageView::mouseReleaseEvent(QMouseEvent* event) {
140-
if(currentTool == ToolNone) {
140+
if(currentTool_ == ToolNone) {
141141
QGraphicsView::mouseReleaseEvent(event);
142142
if(cursorTimer_) {
143143
cursorTimer_->start(CURSOR_HIDE_DELY);
@@ -150,29 +150,32 @@ void ImageView::mouseReleaseEvent(QMouseEvent* event) {
150150
painter.setRenderHint(QPainter::Antialiasing, true);
151151
painter.setPen(QPen(Qt::red, 5));
152152

153-
switch (currentTool) {
153+
switch (currentTool_) {
154154
case ToolArrow:
155-
drawArrow(painter, startPoint, endPoint, M_PI / 8, 25);
155+
drawArrow(painter, startPoint_, endPoint, M_PI / 8, 25);
156156
break;
157-
case ToolRectangle:
157+
case ToolRectangle: {
158158
// Draw the rectangle in the image and scene at the same time
159-
painter.drawRect(QRect(startPoint, endPoint));
160-
annotations.append(scene_->addRect(QRect(startPoint, endPoint), painter.pen()));
159+
QRect r = QRect(startPoint_, endPoint).normalized();
160+
painter.drawRect(r);
161+
annotations_.append(scene_->addRect(r, painter.pen()));
161162
break;
162-
case ToolCircle:
163+
}
164+
case ToolCircle: {
163165
// Draw the circle in the image and scene at the same time
164-
painter.drawEllipse(QRect(startPoint, endPoint));
165-
annotations.append(scene_->addEllipse(QRect(startPoint, endPoint), painter.pen()));
166+
QRect r = QRect(startPoint_, endPoint).normalized();
167+
painter.drawEllipse(r);
168+
annotations_.append(scene_->addEllipse(r, painter.pen()));
166169
break;
167-
case ToolNumber:
168-
{
170+
}
171+
case ToolNumber: {
169172
// Set the font
170173
QFont font;
171174
font.setPixelSize(32);
172175
painter.setFont(font);
173176

174177
// Calculate the dimensions of the text
175-
QString text = QStringLiteral("%1").arg(nextNumber++);
178+
QString text = QStringLiteral("%1").arg(nextNumber_++);
176179
QRectF textRect = painter.boundingRect(image_.rect(), 0, text);
177180
textRect.moveTo(endPoint);
178181

@@ -190,7 +193,7 @@ void ImageView::mouseReleaseEvent(QMouseEvent* event) {
190193
painter.drawPath(path);
191194
// Draw the circle in the sence
192195
auto item = scene_->addPath(path, painter.pen(), QBrush(Qt::red));
193-
annotations.append(item);
196+
annotations_.append(item);
194197

195198
// Draw the text in the image
196199
painter.setPen(Qt::white);
@@ -308,7 +311,7 @@ void ImageView::rotateImage(bool clockwise) {
308311
// Since, in the case of SVG and GIF, annotations are not parts of the QImage and
309312
// because they might have been added at any time, they need to be transformed
310313
// by considering their previous transformations separately.
311-
for(const auto& annotation : std::as_const(annotations)) {
314+
for(const auto& annotation : std::as_const(annotations_)) {
312315
prevTrans = annotation->transform();
313316
annotation->setTransform(transform, false);
314317
annotation->setTransform(prevTrans, true);
@@ -319,11 +322,11 @@ void ImageView::rotateImage(bool clockwise) {
319322
QTransform transform;
320323
transform.rotate(clockwise ? 90.0 : -90.0);
321324
image_ = image_.transformed(transform, Qt::SmoothTransformation);
322-
int tmp = nextNumber; // restore it (may be restet by setImage())
325+
int tmp = nextNumber_; // restore it (may be reset by setImage())
323326
/* when this is GIF or SVG, we need to transform its corresponding QImage
324327
without showing it to have right measures for auto-zooming and other things */
325328
setImage(image_, !gifMovie_ && !isSVG);
326-
nextNumber = tmp;
329+
nextNumber_ = tmp;
327330
}
328331
}
329332

@@ -346,7 +349,7 @@ void ImageView::flipImage(bool horizontal) {
346349
outlineItem_->setTransform(transform, false);
347350
outlineItem_->setTransform(prevTrans, true);
348351
}
349-
for(const auto& annotation : std::as_const(annotations)) {
352+
for(const auto& annotation : std::as_const(annotations_)) {
350353
prevTrans = annotation->transform();
351354
annotation->setTransform(transform, false);
352355
annotation->setTransform(prevTrans, true);
@@ -360,9 +363,9 @@ void ImageView::flipImage(bool horizontal) {
360363
else {
361364
image_ = image_.mirrored(false, true);
362365
}
363-
int tmp = nextNumber;
366+
int tmp = nextNumber_;
364367
setImage(image_, !gifMovie_ && !isSVG);
365-
nextNumber = tmp;
368+
nextNumber_ = tmp;
366369
}
367370
}
368371

@@ -371,7 +374,7 @@ bool ImageView::resizeImage(const QSize& newSize) {
371374
if(newSize == imgSize) {
372375
return false;
373376
}
374-
int tmp = nextNumber;
377+
int tmp = nextNumber_;
375378
if(!isSVG) { // with SVG, we get a sharp image below
376379
image_ = image_.scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
377380
setImage(image_, !gifMovie_);
@@ -389,7 +392,7 @@ bool ImageView::resizeImage(const QSize& newSize) {
389392
outlineItem_->setTransform(transform, false);
390393
outlineItem_->setTransform(prevTrans, true);
391394
}
392-
for(const auto& annotation : std::as_const(annotations)) {
395+
for(const auto& annotation : std::as_const(annotations_)) {
393396
prevTrans = annotation->transform();
394397
annotation->setTransform(transform, false);
395398
annotation->setTransform(prevTrans, true);
@@ -406,7 +409,7 @@ bool ImageView::resizeImage(const QSize& newSize) {
406409
imageItem->paint(&painter, &opt);
407410
painter.restore();
408411
// draw annotations
409-
for(const auto& annotation : std::as_const(annotations)) {
412+
for(const auto& annotation : std::as_const(annotations_)) {
410413
painter.save();
411414
painter.setTransform(annotation->transform());
412415
annotation->paint(&painter, &opt);
@@ -425,7 +428,7 @@ bool ImageView::resizeImage(const QSize& newSize) {
425428
}
426429
}
427430
}
428-
nextNumber = tmp;
431+
nextNumber_ = tmp;
429432
return true;
430433
}
431434

@@ -759,7 +762,7 @@ void ImageView::hideCursor(bool enable) {
759762
}
760763

761764
void ImageView::activateTool(Tool tool) {
762-
currentTool = tool;
765+
currentTool_ = tool;
763766
viewport()->setCursor(tool == ToolNone ?
764767
Qt::OpenHandCursor :
765768
Qt::CrossCursor);
@@ -774,7 +777,7 @@ void ImageView::drawArrow(QPainter &painter,
774777
// Draw the line in the inmage
775778
painter.drawLine(start, end);
776779
// Draw the line in the scene
777-
annotations.append(scene_->addLine(QLine(start, end), painter.pen()));
780+
annotations_.append(scene_->addLine(QLine(start, end), painter.pen()));
778781

779782
// Calculate the angle of the line
780783
QPoint delta = end - start;
@@ -794,8 +797,8 @@ void ImageView::drawArrow(QPainter &painter,
794797
painter.drawLine(end, end + tip1);
795798
painter.drawLine(end, end + tip2);
796799
// Draw the two lines in the scene
797-
annotations.append(scene_->addLine(QLine(end, end+tip1), painter.pen()));
798-
annotations.append(scene_->addLine(QLine(end, end+tip2), painter.pen()));
800+
annotations_.append(scene_->addLine(QLine(end, end+tip1), painter.pen()));
801+
annotations_.append(scene_->addLine(QLine(end, end+tip2), painter.pen()));
799802
}
800803

801804
void ImageView::resetView() {
@@ -807,17 +810,17 @@ void ImageView::resetView() {
807810
}
808811
}
809812
// remove annotations
810-
if(!annotations.isEmpty()) {
813+
if(!annotations_.isEmpty()) {
811814
if(!scene_->items().isEmpty()) { // WARNING: This is not enough to guard against dangling pointers.
812-
for(const auto& annotation : std::as_const(annotations)) {
815+
for(const auto& annotation : std::as_const(annotations_)) {
813816
scene_->removeItem(annotation);
814817
}
815-
qDeleteAll(annotations.begin(), annotations.end());
818+
qDeleteAll(annotations_.begin(), annotations_.end());
816819
}
817-
annotations.clear();
820+
annotations_.clear();
818821
}
819822
// reset numbering
820-
nextNumber = 1;
823+
nextNumber_ = 1;
821824
}
822825

823826
} // namespace LxImage

src/imageview.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ private Q_SLOTS:
143143
bool autoZoomFit_;
144144
bool smoothOnZoom_;
145145
bool isSVG; // is the image an SVG file?
146-
Tool currentTool; // currently selected tool
147-
QPoint startPoint; // starting point for the tool
148-
QList<QGraphicsItem *> annotations; //annotation items which have been drawn in the scene
149-
int nextNumber;
146+
Tool currentTool_; // currently selected tool
147+
QPoint startPoint_; // starting point for the tool
148+
QList<QGraphicsItem *> annotations_; //annotation items which have been drawn in the scene
149+
int nextNumber_;
150150
bool showOutline_;
151151
};
152152

0 commit comments

Comments
 (0)