42 #include "ImageDialog.hh" 48 ImageDialog::ImageDialog(QImage _image, QWidget *parent) :
50 imageWidth_(_image.width() ),
51 imageHeight_(_image.height() ),
56 pixmap_ = QPixmap::fromImage(_image).scaled(QSize(400,400),Qt::KeepAspectRatio,Qt::SmoothTransformation);
58 image->setPixmap(pixmap_);
61 minX->setMaximum(imageWidth_);
62 maxX->setMaximum(imageWidth_);
63 maxX->setValue(imageWidth_);
64 minY->setMaximum(imageHeight_);
65 maxY->setMaximum(imageHeight_);
66 maxY->setValue(imageHeight_);
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()));
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 )));
78 setMouseTracking (
true);
83 void ImageDialog::slidersChanged() {
88 QPainter painter( &buffer_);
93 painter.setPen( pen );
95 painter.setBrush(QBrush(Qt::red,Qt::NoBrush));
97 const int minXVal = minX->value();
98 const int maxXVal = maxX->value();
99 const int minYVal = minY->value();
100 const int maxYVal = maxY->value();
103 const double scale = (double)pixmap_.width() / (double)imageWidth_;
106 painter.drawRect( minXVal * scale , minYVal * scale , (maxXVal-minXVal) * scale, (maxYVal-minYVal) * scale);
110 image->setPixmap(buffer_);
113 void ImageDialog::mouseButtonMoveEvent ( QPoint _p) {
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;
129 int xoffset = (400 - image->pixmap(Qt::ReturnByValue).width() ) / 2;
130 int yoffset = (400 - image->pixmap(Qt::ReturnByValue).height()) / 2;
136 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) 137 const double scale = (double)imageWidth_ / (
double)image->pixmap()->width();
139 const double scale = (double)imageWidth_ / (
double)image->pixmap(Qt::ReturnByValue).width();
143 int newXMin = (std::min(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
144 int newXMax = (std::max(dragStartPoint_.x(),_p.x()) - xoffset ) * scale;
146 int newYMin = (std::min(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
147 int newYMax = (std::max(dragStartPoint_.y(),_p.y()) - yoffset ) * scale;
149 minX->blockSignals(
true);
150 maxX->blockSignals(
true);
151 minY->blockSignals(
true);
152 maxY->blockSignals(
true);
154 minX->setValue(newXMin);
155 maxX->setValue(newXMax);
156 minY->setValue(newYMin);
157 maxY->setValue(newYMax);
159 minX->blockSignals(
false);
160 maxX->blockSignals(
false);
161 minY->blockSignals(
false);
162 maxY->blockSignals(
false);
168 void ImageDialog::mouseButtonPressEvent ( QPoint _p ) {
169 dragStartPoint_ = _p;
173 void ImageDialog::mouseButtonReleaseEvent ( QPoint _p ) {