Update mussa to build on ubuntu 10.04 with qt 4.6.2 +boost 1.40.0.1
[mussa.git] / qui / ImageScaler.cpp
1 #include <QAction>
2 #include <QCheckBox>
3 #include <QLabel>
4 #include <QSpinBox>
5 #include <QGridLayout>
6
7 //#include <iostream>
8 #include <cmath>
9
10 #include "ImageScaler.hpp"
11
12 ImageScaler::ImageScaler(QWidget *parent)
13   : QFrame(parent)
14 {
15   lockAspectRatio = false;
16   aspectRatio = 1.0;
17   minWidth = 1;
18   minHeight = 1;
19   curWidth = 1;
20   curHeight = 1;
21   
22   setFrameStyle(QFrame::StyledPanel);
23    
24   lockButton = new QCheckBox();
25   lockButton->setIcon(QIcon(":icons/lock.png"));
26   lockButton->setToolTip(tr("Lock aspect ratio"));
27   lockButton->setWhatsThis(tr("Lock aspect ratio"));
28   connect(lockButton, SIGNAL(stateChanged(int)), 
29           this, SLOT(setLockAspectRatio(int)));
30
31                              
32   
33   widthBox = new QSpinBox();
34   widthBox->setMinimum(minWidth);
35   setMaxWidth(4048);
36   connect(this, SIGNAL(widthChanged(int)),
37           widthBox, SLOT(setValue(int)));
38   connect(widthBox, SIGNAL(valueChanged(int)),
39           this, SLOT(setWidth(int)));
40
41   widthLabel = new QLabel(tr("Width:"));
42   //widthLabel.setBuddy(widthBox);
43
44   heightBox = new QSpinBox();
45   heightBox->setMinimum(1);
46   setMaxHeight(4048);
47   connect(this, SIGNAL(heightChanged(int)),
48             heightBox, SLOT(setValue(int)));
49   connect(heightBox, SIGNAL(valueChanged(int)),
50           this, SLOT(setHeight(int)));
51
52   heightLabel = new QLabel(tr("Height:"));
53   //heightLabel.setBuddy(heightBox);
54
55   QGridLayout *layout = new QGridLayout;
56   layout->addWidget(widthLabel, 0, 0);
57   layout->addWidget(widthBox, 0, 1);
58   layout->addWidget(heightLabel, 1, 0);
59   layout->addWidget(heightBox, 1, 1);
60   layout->addWidget(lockButton, 0, 2, 1, 2, Qt::AlignCenter);
61   setLayout(layout);
62
63   setWidth(curWidth);
64   setHeight(curHeight);
65 }
66
67
68 int ImageScaler::getMaxWidth() const
69 {
70   return maxWidth;
71 }
72
73 int ImageScaler::getMaxHeight() const
74 {
75   return maxHeight;
76 }
77
78 int ImageScaler::getWidth() const
79 {
80   return widthBox->value();
81 }
82
83 int ImageScaler::getHeight() const
84 {
85   return heightBox->value();
86 }
87
88
89 void ImageScaler::setMaxWidth(int newValue)
90 {
91   maxWidth = newValue;
92   widthBox->setMaximum(maxWidth);
93   emit maxWidthChanged(maxWidth);
94 }
95
96 void ImageScaler::setMaxHeight(int newValue)
97 {
98   maxHeight = newValue;
99   heightBox->setMaximum(maxHeight);
100   emit maxHeightChanged(maxHeight);
101 }
102
103 void ImageScaler::setLockAspectRatio(int state)
104 {
105   if (state == Qt::Checked)
106   {
107     setLockAspectRatio(true);
108   }
109   else if (state == Qt::Unchecked)
110   {
111     setLockAspectRatio(false);
112   }
113 }
114
115 void ImageScaler::setLockAspectRatio(bool state)
116 {
117   if (state == true)
118   {
119     lockAspectRatio = true;
120     aspectRatio = (float) curHeight / (float) curWidth;
121   }
122   else if (state == false)
123   {
124     lockAspectRatio = false;
125   }
126 }
127
128 void ImageScaler::setWidth(int newValue)
129 {
130
131   if (curWidth == newValue)
132   {
133     return;
134   }
135
136   // Make sure newValue is within legal range.
137   if (newValue < minWidth || newValue > maxWidth)
138   {
139     return;
140   }
141   
142   if (!lockAspectRatio)
143   {
144     curWidth = newValue;
145     emit widthChanged(newValue);
146     return;
147   }
148   else if (lockAspectRatio)
149   {
150
151     if (curHeight <= 0)
152     {
153       curWidth = newValue;
154       emit widthChanged(newValue);
155       return;
156     }
157
158     int newHeight = (int) rint((double) aspectRatio * (double) newValue);
159
160     if (newValue != curWidth)
161     {
162       curWidth = newValue;
163       emit widthChanged(newValue);
164     }  
165     curHeight = newHeight;
166     emit heightChanged(newHeight);
167   }
168 }
169
170 void ImageScaler::setHeight(int newValue)
171 {
172   
173   if (curHeight == newValue)
174   {
175     return;
176   }
177
178   // Make sure newValue is within legal range.
179   if (newValue < minHeight || newValue > maxHeight)
180   {
181     return;
182   }
183   
184   // Just change current height if not in locked 
185   // aspect ratio mode.
186   if (!lockAspectRatio)
187   {
188     curHeight = newValue;
189     emit heightChanged(newValue);
190     return;
191   }
192   // Handle locked aspect ratio mode.
193   else if (lockAspectRatio)
194   {    
195     if (curWidth <= 0)
196     {
197       curHeight = newValue;
198       emit heightChanged(newValue);
199       return;
200     }
201    
202     int newWidth = (int) rint( (1.0 /(double) aspectRatio) * (double) newValue);
203    
204     if (newValue != curHeight)
205     {
206       curHeight = newValue;
207       emit heightChanged(newValue);
208     }  
209     curWidth = newWidth;
210     emit widthChanged(newWidth);
211   }
212 }