Added graphics and removed auto centering notifications
[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 #include "panelcommon.h"
29 #include "zoombutton.h"
30 #include "common.h"
31
32 ZoomButtonPanel::ZoomButtonPanel(QWidget *parent)
33     : QWidget(parent),
34       m_isDraggable(false),
35       m_panelLayout(this),
36       m_zoomInButton(0),
37       m_zoomOutButton(0)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_zoomInButton = new ZoomButton(this, ":/res/images/zoom_in.png");
42     m_zoomOutButton = new ZoomButton(this, ":/res/images/zoom_out.png");
43
44     m_panelLayout.setMargin(0);
45     m_panelLayout.setSpacing(0);
46     m_panelLayout.setVerticalSpacing(ZOOM_BUTTON_PANEL_BUTTON_SPACING);
47     m_panelLayout.setSizeConstraint(QLayout::SetFixedSize);
48
49     m_panelLayout.addWidget(m_zoomInButton, 0, 0);
50     m_panelLayout.addWidget(m_zoomOutButton, 1, 0);
51
52     QSettings settings(DIRECTORY_NAME, FILE_NAME);
53     move(settings.value(ZOOMPANEL_POSITION,
54                         QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
55                                ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint());
56
57     QPalette pal = palette();
58     pal.setColor(QPalette::Background, QColor(0, 0, 0, 128));
59     setPalette(pal);
60
61     m_dragStartTimer = new QTimer(this);
62     m_dragStartTimer->setSingleShot(true);
63     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
64
65     m_forceReleaseTimer = new QTimer(this);
66     m_forceReleaseTimer->setSingleShot(true);
67     m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
68
69     connect(m_zoomInButton, SIGNAL(pressed()),
70             m_dragStartTimer, SLOT(start()));
71     connect(m_zoomInButton, SIGNAL(released()),
72             m_dragStartTimer, SLOT(stop()));
73     connect(m_zoomOutButton, SIGNAL(pressed()),
74             m_dragStartTimer, SLOT(start()));
75     connect(m_zoomOutButton, SIGNAL(released()),
76             m_dragStartTimer, SLOT(stop()));
77
78     connect(m_dragStartTimer, SIGNAL(timeout()),
79             this, SLOT(timerExpired()));
80     connect(m_forceReleaseTimer, SIGNAL(timeout()),
81             this, SLOT(forceMouseRelease()));
82 }
83
84 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     if(m_isDraggable) {
89         if (event->buttons() & Qt::LeftButton) {
90             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
91
92             if (newLocation.x() < SIDEBAR_WIDTH)
93                 newLocation.rx() = SIDEBAR_WIDTH;
94             else if (newLocation.x() > m_screenSize.width() - width() - SIDEBAR_WIDTH)
95                 newLocation.rx() =  m_screenSize.width() - width() - SIDEBAR_WIDTH;
96
97             if (newLocation.y() < 0)
98                 newLocation.ry() = 0;
99             else if (newLocation.y() > m_screenSize.height() - height())
100                 newLocation.ry() = m_screenSize.height() - height();
101
102             move(newLocation);
103         }
104     } else {
105         if(!rect().contains(event->pos()))
106             m_dragStartTimer->stop();
107     }
108     QWidget::mouseMoveEvent(event);
109 }
110
111 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
112 {
113     qDebug() << __PRETTY_FUNCTION__;
114
115     if (event->button() == Qt::LeftButton) {
116         m_dragPosition = event->pos();
117     }
118     m_dragStartTimer->start();
119     QWidget::mousePressEvent(event);
120 }
121
122 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
123 {
124     qDebug() << __PRETTY_FUNCTION__;
125     
126     m_dragStartTimer->stop();
127
128     Q_UNUSED(event);
129     if(m_isDraggable) {
130         setDraggable(false);
131         QSettings settings(DIRECTORY_NAME, FILE_NAME);
132         settings.setValue(ZOOMPANEL_POSITION, pos());
133         releaseMouse();
134         m_zoomInButton->setDown(false);
135         m_zoomOutButton->setDown(false);
136     }
137     QWidget::mouseReleaseEvent(event);
138 }
139
140 const ZoomButton* ZoomButtonPanel::zoomInButton()
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     return m_zoomInButton;
145 }
146
147 const ZoomButton* ZoomButtonPanel::zoomOutButton()
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     return m_zoomOutButton;
152 }
153
154 void ZoomButtonPanel::disableZoomInButton()
155 {
156     qDebug() << __PRETTY_FUNCTION__;
157
158     m_zoomInButton->setMode(QIcon::Disabled);
159 }
160
161 void ZoomButtonPanel::disableZoomOutButton()
162 {
163     qDebug() << __PRETTY_FUNCTION__;
164
165     m_zoomOutButton->setMode(QIcon::Disabled);
166 }
167
168 void ZoomButtonPanel::resetButtons()
169 {
170     qDebug() << __PRETTY_FUNCTION__;
171
172     m_zoomInButton->setMode(QIcon::Normal);
173     m_zoomOutButton->setMode(QIcon::Normal);
174 }
175
176 void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
177 {
178     qDebug() << __PRETTY_FUNCTION__;
179
180     m_isDraggable = mode;
181
182     if(mode) {
183         setAutoFillBackground(true);
184
185         m_zoomInMode = m_zoomInButton->mode();
186         m_zoomOutMode = m_zoomOutButton->mode();
187
188         m_zoomInButton->setMode(QIcon::Disabled);
189         m_zoomOutButton->setMode(QIcon::Disabled);
190         grabMouse();
191         m_forceReleaseTimer->start();
192         m_dragPosition = eventPosition;
193     } else {
194         setAutoFillBackground(false);
195         if(m_zoomInMode == QIcon::Selected)
196             m_zoomInButton->setMode(QIcon::Normal);
197         else
198             m_zoomInButton->setMode(m_zoomInMode);
199         if(m_zoomOutMode == QIcon::Selected)
200             m_zoomOutButton->setMode(QIcon::Normal);
201         else
202             m_zoomOutButton->setMode(m_zoomOutMode);
203         releaseMouse();
204         m_forceReleaseTimer->stop();
205         m_zoomInButton->setDown(false);
206         m_zoomOutButton->setDown(false);
207     }
208 }
209
210 void ZoomButtonPanel::screenResized(const QSize &newSize)
211 {
212     qDebug() << __PRETTY_FUNCTION__;
213
214     m_screenSize = newSize;
215
216     QPoint resizedPosition = pos();
217     if(resizedPosition.x() > (newSize.width() - rect().width()))
218         resizedPosition.rx() = newSize.width() - rect().width();
219     else if (resizedPosition.x() < SIDEBAR_WIDTH)
220         resizedPosition.rx() = SIDEBAR_WIDTH;
221     if(resizedPosition.y() > (newSize.height() - rect().height()))
222         resizedPosition.ry() = newSize.height() - rect().height();
223     else if (resizedPosition.y() < 0)
224         resizedPosition.ry() = 0;
225     move(resizedPosition);
226 }
227
228 void ZoomButtonPanel::forceMouseRelease()
229 {
230     qDebug() << __PRETTY_FUNCTION__;
231
232     releaseMouse();
233     setDraggable(false);
234 }
235
236 void ZoomButtonPanel::timerExpired()
237 {
238     qDebug() << __PRETTY_FUNCTION__;
239     
240     if(m_zoomInButton->isDown())
241         m_dragPosition = m_zoomInButton->eventPosition();
242     if(m_zoomOutButton->isDown())
243         m_dragPosition = m_zoomOutButton->eventPosition();
244
245     setDraggable(true, m_dragPosition);
246 }