6ff47efaa0812c24f74e3991128f491451939c5a
[situare] / src / ui / zoombuttonpanel.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Pekka Nissinen - pekka.nissinen@ixonos.com
6        Kaj Wallin - kaj.wallin@ixonos.com
7
8    Situare is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    version 2 as published by the Free Software Foundation.
11
12    Situare is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Situare; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20    USA.
21 */
22
23 #include <QDebug>
24 #include <QPainter>
25 #include <QSettings>
26
27 #include "zoombuttonpanel.h"
28
29 #include "common.h"
30 #include "panelcommon.h"
31 #include "zoombutton.h"
32
33 const int ROUNDING_RADIUS = 9; ///< Roundness of the background edges
34
35 ZoomButtonPanel::ZoomButtonPanel(QWidget *parent)
36     : QWidget(parent),
37       m_isDraggable(false),
38       m_panelLayout(this),
39       m_zoomInButton(0),
40       m_zoomOutButton(0)
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43
44     m_zoomInButton = new ZoomButton(this, ":/res/images/zoom_in.png");
45     m_zoomOutButton = new ZoomButton(this, ":/res/images/zoom_out.png");
46
47     m_panelLayout.setMargin(0);
48     m_panelLayout.setSpacing(0);
49     m_panelLayout.setVerticalSpacing(ZOOM_BUTTON_PANEL_BUTTON_SPACING);
50     m_panelLayout.setSizeConstraint(QLayout::SetFixedSize);
51
52     m_panelLayout.addWidget(m_zoomInButton, 0, 0);
53     m_panelLayout.addWidget(m_zoomOutButton, 1, 0);
54
55     QSettings settings(DIRECTORY_NAME, FILE_NAME);
56     QPoint savedLocation = settings.value(ZOOMPANEL_POSITION,
57                                           QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
58                                                  ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint();
59
60     if(savedLocation.x() > DEFAULT_SCREEN_WIDTH || savedLocation.y() > DEFAULT_SCREEN_HEIGHT) {
61         savedLocation.rx() = ZOOM_BUTTON_PANEL_POSITION_X;
62         savedLocation.ry() = ZOOM_BUTTON_PANEL_POSITION_Y;
63     }
64
65     move(savedLocation);
66
67     m_dragStartTimer = new QTimer(this);
68     m_dragStartTimer->setSingleShot(true);
69     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
70
71     m_forceReleaseTimer = new QTimer(this);
72     m_forceReleaseTimer->setSingleShot(true);
73     m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
74
75     connect(m_zoomInButton, SIGNAL(pressed()),
76             m_dragStartTimer, SLOT(start()));
77     connect(m_zoomInButton, SIGNAL(released()),
78             m_dragStartTimer, SLOT(stop()));
79     connect(m_zoomOutButton, SIGNAL(pressed()),
80             m_dragStartTimer, SLOT(start()));
81     connect(m_zoomOutButton, SIGNAL(released()),
82             m_dragStartTimer, SLOT(stop()));
83
84     connect(m_dragStartTimer, SIGNAL(timeout()),
85             this, SLOT(timerExpired()));
86     connect(m_forceReleaseTimer, SIGNAL(timeout()),
87             this, SLOT(forceMouseRelease()));
88 }
89
90 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     if(m_isDraggable) {
95         if (event->buttons() & Qt::LeftButton) {
96             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
97
98             if (newLocation.x() < 0)
99                 newLocation.rx() = 0;
100             else if (newLocation.x() > m_screenSize.width() - width() - PANEL_BAR_WIDTH)
101                 newLocation.rx() =  m_screenSize.width() - width() - PANEL_BAR_WIDTH;
102
103             if (newLocation.y() < 0)
104                 newLocation.ry() = 0;
105             else if (newLocation.y() > m_screenSize.height() - height())
106                 newLocation.ry() = m_screenSize.height() - height();
107
108             move(newLocation);
109         }
110     } else {
111         if(!rect().contains(event->pos()))
112             m_dragStartTimer->stop();
113     }
114 }
115
116 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
117 {
118     qDebug() << __PRETTY_FUNCTION__;
119
120     if (event->button() == Qt::LeftButton)
121         m_dragPosition = event->pos();
122
123     m_dragStartTimer->start();
124 }
125
126 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129     
130     m_dragStartTimer->stop();
131
132     Q_UNUSED(event);
133     if(m_isDraggable) {
134         setDraggable(false);
135
136         QSettings settings(DIRECTORY_NAME, FILE_NAME);
137         settings.setValue(ZOOMPANEL_POSITION, pos());
138
139         releaseMouse();
140
141         m_zoomInButton->setDown(false);
142         m_zoomOutButton->setDown(false);
143     }
144 }
145
146 void ZoomButtonPanel::paintEvent(QPaintEvent *event)
147 {
148     qDebug() << __PRETTY_FUNCTION__;
149
150     Q_UNUSED(event);
151
152     QPainter painter(this);
153
154     if(m_isDraggable) {
155         QPainterPath backgroundPath;
156         backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
157         painter.setRenderHint(QPainter::Antialiasing);
158         painter.fillPath(backgroundPath, QBrush(Qt::Dense4Pattern));
159     }
160 }
161
162 const ZoomButton* ZoomButtonPanel::zoomInButton()
163 {
164     qDebug() << __PRETTY_FUNCTION__;
165
166     return m_zoomInButton;
167 }
168
169 const ZoomButton* ZoomButtonPanel::zoomOutButton()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     return m_zoomOutButton;
174 }
175
176 void ZoomButtonPanel::disableZoomInButton()
177 {
178     qDebug() << __PRETTY_FUNCTION__;
179
180     m_zoomInButton->setMode(QIcon::Disabled);
181 }
182
183 void ZoomButtonPanel::disableZoomOutButton()
184 {
185     qDebug() << __PRETTY_FUNCTION__;
186
187     m_zoomOutButton->setMode(QIcon::Disabled);
188 }
189
190 void ZoomButtonPanel::resetButtons()
191 {
192     qDebug() << __PRETTY_FUNCTION__;
193
194     m_zoomInButton->setMode(QIcon::Normal);
195     m_zoomOutButton->setMode(QIcon::Normal);
196 }
197
198 void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
199 {
200     qDebug() << __PRETTY_FUNCTION__;
201
202     m_isDraggable = mode;
203
204     if(mode) {
205         emit draggingModeTriggered();
206
207         m_zoomInMode = m_zoomInButton->mode();
208         m_zoomOutMode = m_zoomOutButton->mode();
209         m_zoomInButton->setMode(QIcon::Disabled);
210         m_zoomOutButton->setMode(QIcon::Disabled);
211
212         grabMouse();
213
214         m_forceReleaseTimer->start();
215         m_dragPosition = eventPosition;
216     } else {
217         if(m_zoomInMode == QIcon::Selected)
218             m_zoomInButton->setMode(QIcon::Normal);
219         else
220             m_zoomInButton->setMode(m_zoomInMode);
221
222         if(m_zoomOutMode == QIcon::Selected)
223             m_zoomOutButton->setMode(QIcon::Normal);
224         else
225             m_zoomOutButton->setMode(m_zoomOutMode);
226
227         releaseMouse();
228
229         m_forceReleaseTimer->stop();
230         m_zoomInButton->setDown(false);
231         m_zoomOutButton->setDown(false);
232     }
233     update();
234 }
235
236 void ZoomButtonPanel::screenResized(const QSize &newSize)
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239
240     int oldHeight = 0;
241     int oldWidth = 0;
242
243     if(m_screenSize.height() < 0)
244         oldHeight = DEFAULT_NON_FULLSCREEN_HEIGHT;
245     else
246         oldHeight = m_screenSize.height();
247
248     if(m_screenSize.width() < 0)
249         oldWidth = DEFAULT_SCREEN_WIDTH;
250     else
251         oldWidth = m_screenSize.width();
252
253     m_screenSize = newSize;
254
255     QPoint resizedPosition = pos();
256
257     if(resizedPosition.x() < 0)
258         resizedPosition.rx() = 0;
259     else if(resizedPosition.x() > (newSize.width() - rect().width()))
260         resizedPosition.rx() = newSize.width() - rect().width();
261
262     if(resizedPosition.y() < 0)
263         resizedPosition.ry() = 0;
264     else if(resizedPosition.y() > (newSize.height() - rect().height()))
265         resizedPosition.ry() = newSize.height() - rect().height();
266
267     if((pos().y() + rect().center().y()) > (oldHeight / 2))
268         resizedPosition.ry() = newSize.height() - (oldHeight - pos().y());
269
270     if((pos().x() + rect().center().x()) > (oldWidth / 2))
271         resizedPosition.rx() = newSize.width() - (oldWidth - pos().x());
272
273     move(resizedPosition);
274 }
275
276 void ZoomButtonPanel::forceMouseRelease()
277 {
278     qDebug() << __PRETTY_FUNCTION__;
279
280     releaseMouse();
281     setDraggable(false);
282 }
283
284 void ZoomButtonPanel::timerExpired()
285 {
286     qDebug() << __PRETTY_FUNCTION__;
287     
288     if(m_zoomInButton->isDown())
289         m_dragPosition = m_zoomInButton->eventPosition();
290
291     if(m_zoomOutButton->isDown())
292         m_dragPosition = m_zoomOutButton->eventPosition();
293
294     setDraggable(true, m_dragPosition);
295 }