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 /*===========================================================================*\
43 * *
44 * $Revision: 13581 $ *
45 * $LastChangedBy: moebius $ *
46 * $Date: 2012-01-30 14:22:52 +0100 (Mo, 30 Jan 2012) $ *
47 * *
48 \*===========================================================================*/
49 
50 #include "ImageDialog.hh"
51 
52 #include <iostream>
53 
54 ImageDialog::ImageDialog(QImage _image, QWidget *parent) :
55  QDialog(parent),
56  imageWidth_(_image.width() ),
57  imageHeight_(_image.height() ),
58  dragging_(false)
59 {
60  setupUi(this);
61 
62  pixmap_ = QPixmap::fromImage(_image).scaled(QSize(400,400),Qt::KeepAspectRatio,Qt::SmoothTransformation);
63 
64  image->setPixmap(pixmap_);
65 
66  // Set the correct boundaries for the spinboxes
67  minX->setMaximum(imageWidth_);
68  maxX->setMaximum(imageWidth_);
69  maxX->setValue(imageWidth_);
70  minY->setMaximum(imageHeight_);
71  maxY->setMaximum(imageHeight_);
72  maxY->setValue(imageHeight_);
73 
74  // Connect the spin boxes
75  connect(minX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
76  connect(maxX,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
77  connect(minY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
78  connect(maxY,SIGNAL(valueChanged(int )),this,SLOT(slidersChanged()));
79 
80  connect(image,SIGNAL(mouseButtonMoveEvent ( QPoint )) ,this,SLOT(mouseButtonMoveEvent ( QPoint )));
81  connect(image,SIGNAL(mouseButtonPressEvent ( QPoint )) ,this,SLOT(mouseButtonPressEvent ( QPoint )));
82  connect(image,SIGNAL(mouseButtonReleaseEvent ( QPoint )),this,SLOT(mouseButtonReleaseEvent ( QPoint )));
83 
84  setMouseTracking ( true);
85 
86 }
87 
88 
89 void ImageDialog::slidersChanged() {
90 
91  // Copy to internal buffer
92  buffer_ = pixmap_;
93 
94  QPainter painter( &buffer_);
95 
96  QPen pen(Qt::red);
97 
98  pen.setWidth(2);
99  painter.setPen( pen );
100 
101  painter.setBrush(QBrush(Qt::red,Qt::NoBrush));
102 
103  const int minXVal = minX->value();
104  const int maxXVal = maxX->value();
105  const int minYVal = minY->value();
106  const int maxYVal = maxY->value();
107 
108  // Calculate the scaling factor that is used to fit the image to the widget
109  const double scale = (double)pixmap_.width() / (double)imageWidth_;
110 
111  // Working on the actual pixmap -> no offsets!
112  painter.drawRect( minXVal * scale , minYVal * scale , (maxXVal-minXVal) * scale, (maxYVal-minYVal) * scale);
113 
114  painter.end();
115 
116  image->setPixmap(buffer_);
117 }
118 
119 void ImageDialog::mouseButtonMoveEvent ( QPoint _p) {
120  if ( dragging_ ) {
121 
122  // Calculate the right values for the position.
123  // The image is scaled to 400x400
124  // But the aspect ratio is kept. So we need to calculate the new sizes.
125 
126 
127 
128  // Calculate the free space above and before the real image
129  // As we are working on the pixmap widget, we need to consider these offsets to calculate
130  // coordinates inside the real pixmap.
131  int xoffset = (400 - image->pixmap()->width() ) / 2;
132  int yoffset = (400 - image->pixmap()->height()) / 2;
133 
134  // Calculate the scaling factor that is used to fit the image to the widget
135  const double scale = (double)imageWidth_ / (double)image->pixmap()->width();
136 
137  int newXMin = (std::min(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
138  int newXMax = (std::max(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
139 
140  int newYMin = (std::min(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
141  int newYMax = (std::max(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
142 
143  minX->blockSignals(true);
144  maxX->blockSignals(true);
145  minY->blockSignals(true);
146  maxY->blockSignals(true);
147 
148  minX->setValue(newXMin);
149  maxX->setValue(newXMax);
150  minY->setValue(newYMin);
151  maxY->setValue(newYMax);
152 
153  minX->blockSignals(false);
154  maxX->blockSignals(false);
155  minY->blockSignals(false);
156  maxY->blockSignals(false);
157 
158  slidersChanged();
159  }
160 }
161 
162 void ImageDialog::mouseButtonPressEvent ( QPoint _p ) {
163  dragStartPoint_ = _p;
164  dragging_ = true;
165 }
166 
167 void ImageDialog::mouseButtonReleaseEvent ( QPoint _p ) {
168  dragging_ = false;
169 }
170 
171 
172 
173 
174