Developer Documentation
ImageDialog.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 #include "ImageDialog.hh"
43 
44 #include <iostream>
45 
46 #include <QPainter>
47 
48 ImageDialog::ImageDialog(QImage _image, QWidget *parent) :
49  QDialog(parent),
50  imageWidth_(_image.width() ),
51  imageHeight_(_image.height() ),
52  dragging_(false)
53 {
54  setupUi(this);
55 
56  pixmap_ = QPixmap::fromImage(_image).scaled(QSize(400,400),Qt::KeepAspectRatio,Qt::SmoothTransformation);
57 
58  image->setPixmap(pixmap_);
59 
60  // Set the correct boundaries for the spinboxes
61  minX->setMaximum(imageWidth_);
62  maxX->setMaximum(imageWidth_);
63  maxX->setValue(imageWidth_);
64  minY->setMaximum(imageHeight_);
65  maxY->setMaximum(imageHeight_);
66  maxY->setValue(imageHeight_);
67 
68  // Connect the spin boxes
69  connect(minX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
70  connect(maxX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
71  connect(minY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
72  connect(maxY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
73 
74  connect(image,SIGNAL(mouseButtonMoveEvent ( QPoint )) ,this,SLOT(mouseButtonMoveEvent ( QPoint )));
75  connect(image,SIGNAL(mouseButtonPressEvent ( QPoint )) ,this,SLOT(mouseButtonPressEvent ( QPoint )));
76  connect(image,SIGNAL(mouseButtonReleaseEvent ( QPoint )),this,SLOT(mouseButtonReleaseEvent ( QPoint )));
77 
78  setMouseTracking ( true);
79 
80 }
81 
82 
83 void ImageDialog::slidersChanged() {
84 
85  // Copy to internal buffer
86  buffer_ = pixmap_;
87 
88  QPainter painter( &buffer_);
89 
90  QPen pen(Qt::red);
91 
92  pen.setWidth(2);
93  painter.setPen( pen );
94 
95  painter.setBrush(QBrush(Qt::red,Qt::NoBrush));
96 
97  const int minXVal = minX->value();
98  const int maxXVal = maxX->value();
99  const int minYVal = minY->value();
100  const int maxYVal = maxY->value();
101 
102  // Calculate the scaling factor that is used to fit the image to the widget
103  const double scale = (double)pixmap_.width() / (double)imageWidth_;
104 
105  // Working on the actual pixmap -> no offsets!
106  painter.drawRect( minXVal * scale , minYVal * scale , (maxXVal-minXVal) * scale, (maxYVal-minYVal) * scale);
107 
108  painter.end();
109 
110  image->setPixmap(buffer_);
111 }
112 
113 void ImageDialog::mouseButtonMoveEvent ( QPoint _p) {
114  if ( dragging_ ) {
115 
116  // Calculate the right values for the position.
117  // The image is scaled to 400x400
118  // But the aspect ratio is kept. So we need to calculate the new sizes.
119 
120 
121 
122  // Calculate the free space above and before the real image
123  // As we are working on the pixmap widget, we need to consider these offsets to calculate
124  // coordinates inside the real pixmap.
125  #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
126  int xoffset = (400 - image->pixmap()->width() ) / 2;
127  int yoffset = (400 - image->pixmap()->height()) / 2;
128  #else
129  int xoffset = (400 - image->pixmap(Qt::ReturnByValue).width() ) / 2;
130  int yoffset = (400 - image->pixmap(Qt::ReturnByValue).height()) / 2;
131  #endif
132 
133 
134  // Calculate the scaling factor that is used to fit the image to the widget
135 
136  #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
137  const double scale = (double)imageWidth_ / (double)image->pixmap()->width();
138  #else
139  const double scale = (double)imageWidth_ / (double)image->pixmap(Qt::ReturnByValue).width();
140  #endif
141 
142 
143  int newXMin = (std::min(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
144  int newXMax = (std::max(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
145 
146  int newYMin = (std::min(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
147  int newYMax = (std::max(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
148 
149  minX->blockSignals(true);
150  maxX->blockSignals(true);
151  minY->blockSignals(true);
152  maxY->blockSignals(true);
153 
154  minX->setValue(newXMin);
155  maxX->setValue(newXMax);
156  minY->setValue(newYMin);
157  maxY->setValue(newYMax);
158 
159  minX->blockSignals(false);
160  maxX->blockSignals(false);
161  minY->blockSignals(false);
162  maxY->blockSignals(false);
163 
164  slidersChanged();
165  }
166 }
167 
168 void ImageDialog::mouseButtonPressEvent ( QPoint _p ) {
169  dragStartPoint_ = _p;
170  dragging_ = true;
171 }
172 
173 void ImageDialog::mouseButtonReleaseEvent ( QPoint _p ) {
174  dragging_ = false;
175 }
176 
177 
178 
179 
180