Removed timer from zoombutton, created new signals and fixed some logics
[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, int x, int y)
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_zoomInButton->setObjectName("ZoomInButton");
45     m_zoomOutButton->setObjectName("ZoomOutButton");
46     m_panelLayout.setMargin(0);
47     m_panelLayout.setSpacing(0);
48     m_panelLayout.setVerticalSpacing(ZOOM_BUTTON_PANEL_BUTTON_SPACING);
49     m_panelLayout.setSizeConstraint(QLayout::SetFixedSize);
50
51     m_panelLayout.addWidget(m_zoomInButton, 0, 0);
52     m_panelLayout.addWidget(m_zoomOutButton, 1, 0);
53
54     move(x, y);
55
56     QPalette pal = palette();
57     pal.setColor(QPalette::Background, QColor(0, 0, 0, 128));
58     setPalette(pal);
59
60     m_eventBlocker = new QWidget(this);
61     m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
62     m_eventBlocker->resize(size().width(),
63                            m_zoomInButton->size().height() * 2 + ZOOM_BUTTON_PANEL_BUTTON_SPACING);
64
65     m_dragStartTimer = new QTimer(this);
66     m_dragStartTimer->setSingleShot(true);
67     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
68
69     connect(m_zoomInButton, SIGNAL(pressEvent()),
70             m_dragStartTimer, SLOT(start()));
71     connect(m_zoomInButton, SIGNAL(releaseEvent()),
72             m_dragStartTimer, SLOT(stop()));
73     connect(m_zoomOutButton, SIGNAL(pressEvent()),
74             m_dragStartTimer, SLOT(start()));
75     connect(m_zoomOutButton, SIGNAL(releaseEvent()),
76             m_dragStartTimer, SLOT(stop()));
77
78     connect(m_zoomInButton, SIGNAL(eventPosition(QPoint)),
79             this, SLOT(setDragPosition(QPoint)));
80     connect(m_zoomOutButton, SIGNAL(eventPosition(QPoint)),
81             this, SLOT(setDragPosition(QPoint)));
82
83     connect(m_dragStartTimer, SIGNAL(timeout()),
84             this, SLOT(timerExpired()));
85
86 }
87
88 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     if(m_isDraggable) {
93         if (event->buttons() & Qt::LeftButton) {
94             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
95
96             if (newLocation.x() < SIDEBAR_WIDTH)
97                 newLocation.rx() = SIDEBAR_WIDTH;
98             else if (newLocation.x() > m_screenSize.width() - m_eventBlocker->width())
99                 newLocation.rx() =  m_screenSize.width() - m_eventBlocker->width();
100
101             if (newLocation.y() < 0)
102                 newLocation.ry() = 0;
103             else if (newLocation.y() > m_screenSize.height() - m_eventBlocker->height())
104                 newLocation.ry() = m_screenSize.height() - m_eventBlocker->height();
105
106             move(newLocation);
107             event->accept();
108         }
109     }
110     else {
111         m_dragPosition = event->pos();
112     }
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         event->accept();
123     }
124     m_dragStartTimer->start();
125 }
126
127 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
128 {
129     Q_UNUSED(event);
130     setDraggable(false);
131     QSettings settings(DIRECTORY_NAME, FILE_NAME);
132     settings.setValue(ZOOMPANEL_POSITION, pos());
133     releaseMouse();
134     m_dragStartTimer->stop();
135 }
136
137 const ImageButton* ZoomButtonPanel::zoomInButton()
138 {
139     qDebug() << __PRETTY_FUNCTION__;
140
141     return m_zoomInButton;
142 }
143
144 const ImageButton* ZoomButtonPanel::zoomOutButton()
145 {
146     qDebug() << __PRETTY_FUNCTION__;
147
148     return m_zoomOutButton;
149 }
150
151 void ZoomButtonPanel::disableZoomInButton()
152 {
153     qDebug() << __PRETTY_FUNCTION__;
154
155     m_zoomInButton->setMode(QIcon::Disabled);
156 }
157
158 void ZoomButtonPanel::disableZoomOutButton()
159 {
160     qDebug() << __PRETTY_FUNCTION__;
161
162     m_zoomOutButton->setMode(QIcon::Disabled);
163 }
164
165 void ZoomButtonPanel::resetButtons()
166 {
167     qDebug() << __PRETTY_FUNCTION__;
168
169     m_zoomInButton->setMode(QIcon::Normal);
170     m_zoomOutButton->setMode(QIcon::Normal);
171 }
172
173 void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
174 {
175     if(mode == true) {
176         m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, false);
177         m_isDraggable = mode;
178         setAutoFillBackground(true);
179
180         m_zoomInMode = m_zoomInButton->mode();
181         m_zoomOutMode = m_zoomOutButton->mode();
182
183         m_zoomInButton->setMode(QIcon::Disabled);
184         m_zoomOutButton->setMode(QIcon::Disabled);
185         grabMouse();
186         m_dragPosition = eventPosition;
187     }
188     else {
189         m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
190         m_isDraggable = mode;
191         setAutoFillBackground(false);
192         if(m_zoomInMode == QIcon::Selected)
193             m_zoomInButton->setMode(QIcon::Normal);
194         else
195             m_zoomInButton->setMode(m_zoomInMode);
196         if(m_zoomOutMode == QIcon::Selected)
197             m_zoomOutButton->setMode(QIcon::Normal);
198         else
199             m_zoomOutButton->setMode(m_zoomOutMode);
200         releaseMouse();
201         m_zoomInButton->setDown(false);
202         m_zoomOutButton->setDown(false);
203     }
204 }
205
206 void ZoomButtonPanel::screenResized(const QSize &size)
207 {
208     m_screenSize = size;
209 }
210
211 void ZoomButtonPanel::setDragPosition(QPoint eventPosition)
212 {
213     m_dragPosition = eventPosition;
214 }
215
216 void ZoomButtonPanel::timerExpired()
217 {
218     setDraggable(true, m_dragPosition);
219 }