1 /***************************************************************************
2 File : ImageDialog.cpp
3 Project : SciDAVis
4 --------------------------------------------------------------------
5 Copyright : (C) 2006 by Ion Vasilief
6 Email (use @ for *) : ion_vasilief*yahoo.fr
7 Description : Image geometry dialog
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26 * Boston, MA 02110-1301 USA *
27 * *
28 ***************************************************************************/
29 #include "ImageDialog.h"
30
31 #include <QLayout>
32 #include <QGroupBox>
33 #include <QLabel>
34
ImageDialog(QWidget * parent,Qt::WindowFlags fl)35 ImageDialog::ImageDialog(QWidget *parent, Qt::WindowFlags fl) : QDialog(parent, fl)
36 {
37 setWindowTitle(tr("Image Geometry"));
38
39 QGroupBox *gb1 = new QGroupBox(tr("Origin"));
40 boxX = new QSpinBox();
41 boxX->setRange(0, 2000);
42 boxX->setSuffix(tr(" pixels"));
43
44 boxY = new QSpinBox();
45 boxY->setRange(0, 2000);
46 boxY->setSuffix(tr(" pixels"));
47
48 QGridLayout *gl1 = new QGridLayout(gb1);
49 gl1->addWidget(new QLabel(tr("X= ")), 0, 0);
50 gl1->addWidget(boxX, 0, 1);
51 gl1->addWidget(new QLabel(tr("Y= ")), 1, 0);
52 gl1->addWidget(boxY, 1, 1);
53 gl1->setRowStretch(2, 1);
54
55 QGroupBox *gb2 = new QGroupBox(tr("Size"));
56 boxWidth = new QSpinBox();
57 boxWidth->setRange(0, 2000);
58 boxWidth->setSuffix(tr(" pixels"));
59
60 boxHeight = new QSpinBox();
61 boxHeight->setRange(0, 2000);
62 boxHeight->setSuffix(tr(" pixels"));
63
64 QGridLayout *gl2 = new QGridLayout(gb2);
65 gl2->addWidget(new QLabel(tr("width= ")), 0, 0);
66 gl2->addWidget(boxWidth, 0, 1);
67
68 gl2->addWidget(new QLabel(tr("height= ")), 2, 0);
69 gl2->addWidget(boxHeight, 2, 1);
70
71 keepRatioBox = new QCheckBox(tr("Keep aspect ratio"));
72 keepRatioBox->setChecked(true);
73 gl2->addWidget(keepRatioBox, 3, 1);
74
75 gl2->setRowStretch(4, 1);
76
77 QBoxLayout *bl1 = new QBoxLayout(QBoxLayout::LeftToRight);
78 bl1->addWidget(gb1);
79 bl1->addWidget(gb2);
80
81 buttonApply = new QPushButton(tr("&Apply"));
82 buttonOk = new QPushButton(tr("&Ok"));
83 buttonCancel = new QPushButton(tr("&Cancel"));
84
85 QBoxLayout *bl2 = new QBoxLayout(QBoxLayout::LeftToRight);
86 bl2->addStretch();
87 bl2->addWidget(buttonApply);
88 bl2->addWidget(buttonOk);
89 bl2->addWidget(buttonCancel);
90
91 QVBoxLayout *vl = new QVBoxLayout(this);
92 vl->addLayout(bl1);
93 vl->addLayout(bl2);
94
95 connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
96 connect(buttonApply, SIGNAL(clicked()), this, SLOT(update()));
97 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
98 }
99
setOrigin(const QPoint & o)100 void ImageDialog::setOrigin(const QPoint &o)
101 {
102 boxX->setValue(o.x());
103 boxY->setValue(o.y());
104 }
105
setSize(const QSize & size)106 void ImageDialog::setSize(const QSize &size)
107 {
108 boxWidth->setValue(size.width());
109 boxHeight->setValue(size.height());
110 aspect_ratio = (double)size.width() / (double)size.height();
111
112 connect(boxWidth, SIGNAL(valueChanged(int)), this, SLOT(adjustHeight(int)));
113 connect(boxHeight, SIGNAL(valueChanged(int)), this, SLOT(adjustWidth(int)));
114 }
115
adjustHeight(int width)116 void ImageDialog::adjustHeight(int width)
117 {
118 if (keepRatioBox->isChecked()) {
119 disconnect(boxHeight, SIGNAL(valueChanged(int)), this, SLOT(adjustWidth(int)));
120 boxHeight->setValue(int(width / aspect_ratio));
121 connect(boxHeight, SIGNAL(valueChanged(int)), this, SLOT(adjustWidth(int)));
122 } else
123 aspect_ratio = (double)width / double(boxHeight->value());
124 }
125
adjustWidth(int height)126 void ImageDialog::adjustWidth(int height)
127 {
128 if (keepRatioBox->isChecked()) {
129 disconnect(boxWidth, SIGNAL(valueChanged(int)), this, SLOT(adjustHeight(int)));
130 boxWidth->setValue(int(height * aspect_ratio));
131 connect(boxWidth, SIGNAL(valueChanged(int)), this, SLOT(adjustHeight(int)));
132 } else
133 aspect_ratio = double(boxWidth->value()) / (double)height;
134 }
135
update()136 void ImageDialog::update()
137 {
138 emit setGeometry(boxX->value(), boxY->value(), boxWidth->value(), boxHeight->value());
139 }
140
accept()141 void ImageDialog::accept()
142 {
143 update();
144 close();
145 }
146