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