Implemented NetworkCookieJar::clearCookiesSetting()
[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 <QMouseEvent>
25 #include <QPainter>
26 #include <QSettings>
27
28 #include "zoombuttonpanel.h"
29
30 #include "common.h"
31 #include "panelcommon.h"
32 #include "zoombutton.h"
33
34 const int ROUNDING_RADIUS = 9; ///< Roundness of the background edges
35
36 ZoomButtonPanel::ZoomButtonPanel(QWidget *parent)
37     : QWidget(parent),
38       m_isDraggable(false),
39       m_panelLayout(this),
40       m_zoomInButton(0),
41       m_zoomOutButton(0)
42 {
43     qDebug() << __PRETTY_FUNCTION__;
44
45     m_zoomInButton = new ZoomButton(":/res/images/zoom_in.png", this);
46     m_zoomOutButton = new ZoomButton(":/res/images/zoom_out.png", this);
47
48     m_panelLayout.setMargin(0);
49     m_panelLayout.setSpacing(0);
50     m_panelLayout.setVerticalSpacing(ZOOM_BUTTON_PANEL_BUTTON_SPACING);
51     m_panelLayout.setSizeConstraint(QLayout::SetFixedSize);
52
53     m_panelLayout.addWidget(m_zoomInButton, 0, 0);
54     m_panelLayout.addWidget(m_zoomOutButton, 1, 0);
55
56     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
57     QPoint savedLocation = settings.value(ZOOMPANEL_POSITION,
58                                           QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
59                                                  ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint();
60
61     if(savedLocation.x() > DEFAULT_SCREEN_WIDTH || savedLocation.y() > DEFAULT_SCREEN_HEIGHT) {
62         savedLocation.rx() = ZOOM_BUTTON_PANEL_POSITION_X;
63         savedLocation.ry() = ZOOM_BUTTON_PANEL_POSITION_Y;
64     }
65
66     move(savedLocation);
67
68     m_dragStartTimer = new QTimer(this);
69     m_dragStartTimer->setSingleShot(true);
70     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
71
72     m_forceReleaseTimer = new QTimer(this);
73     m_forceReleaseTimer->setSingleShot(true);
74     m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
75
76     connect(m_zoomInButton, SIGNAL(pressed()),
77             m_dragStartTimer, SLOT(start()));
78     connect(m_zoomInButton, SIGNAL(released()),
79             m_dragStartTimer, SLOT(stop()));
80     connect(m_zoomOutButton, SIGNAL(pressed()),
81             m_dragStartTimer, SLOT(start()));
82     connect(m_zoomOutButton, SIGNAL(released()),
83             m_dragStartTimer, SLOT(stop()));
84
85     connect(m_dragStartTimer, SIGNAL(timeout()),
86             this, SLOT(timerExpired()));
87     connect(m_forceReleaseTimer, SIGNAL(timeout()),
88             this, SLOT(forceMouseRelease()));
89 }
90
91 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
92 {
93     qDebug() << __PRETTY_FUNCTION__;
94
95     if(m_isDraggable) {
96         if (event->buttons() & Qt::LeftButton) {
97             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
98
99             if (newLocation.x() < 0)
100                 newLocation.rx() = 0;
101             else if (newLocation.x() > m_screenSize.width() - width() - PANEL_BAR_WIDTH)
102                 newLocation.rx() =  m_screenSize.width() - width() - PANEL_BAR_WIDTH;
103
104             if (newLocation.y() < 0)
105                 newLocation.ry() = 0;
106             else if (newLocation.y() > m_screenSize.height() - height())
107                 newLocation.ry() = m_screenSize.height() - height();
108
109             move(newLocation);
110         }
111     } else {
112         if(!rect().contains(event->pos()))
113             m_dragStartTimer->stop();
114     }
115 }
116
117 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
118 {
119     qDebug() << __PRETTY_FUNCTION__;
120
121     if (event->button() == Qt::LeftButton)
122         m_dragPosition = event->pos();
123
124     m_dragStartTimer->start();
125 }
126
127 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
128 {
129     qDebug() << __PRETTY_FUNCTION__;
130     
131     m_dragStartTimer->stop();
132
133     Q_UNUSED(event);
134     if(m_isDraggable) {
135         setDraggable(false);
136
137         QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
138         settings.setValue(ZOOMPANEL_POSITION, pos());
139
140         releaseMouse();
141
142         m_zoomInButton->setDown(false);
143         m_zoomOutButton->setDown(false);
144     }
145 }
146
147 void ZoomButtonPanel::paintEvent(QPaintEvent *event)
148 {
149     qDebug() << __PRETTY_FUNCTION__;
150
151     Q_UNUSED(event);
152
153     QPainter painter(this);
154
155     if(m_isDraggable) {
156         QPainterPath backgroundPath;
157         backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
158         painter.setRenderHint(QPainter::Antialiasing);
159         painter.fillPath(backgroundPath, QBrush(Qt::Dense4Pattern));
160     }
161 }
162
163 const ZoomButton* ZoomButtonPanel::zoomInButton() const
164 {
165     qDebug() << __PRETTY_FUNCTION__;
166
167     return m_zoomInButton;
168 }
169
170 const ZoomButton* ZoomButtonPanel::zoomOutButton() const
171 {
172     qDebug() << __PRETTY_FUNCTION__;
173
174     return m_zoomOutButton;
175 }
176
177 void ZoomButtonPanel::disableZoomInButton()
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180
181     m_zoomInButton->setEnabled(false);
182 }
183
184 void ZoomButtonPanel::disableZoomOutButton()
185 {
186     qDebug() << __PRETTY_FUNCTION__;
187
188     m_zoomOutButton->setEnabled(false);
189 }
190
191 void ZoomButtonPanel::resetButtons()
192 {
193     qDebug() << __PRETTY_FUNCTION__;
194
195     m_zoomInButton->setEnabled(true);
196     m_zoomOutButton->setEnabled(true);
197 }
198
199 void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
200 {
201     qDebug() << __PRETTY_FUNCTION__;
202
203     m_isDraggable = mode;
204
205     if(mode) {
206         emit draggingModeTriggered();
207
208         m_zoomInMode = m_zoomInButton->isEnabled();
209         m_zoomOutMode = m_zoomOutButton->isEnabled();
210
211         m_zoomInButton->setEnabled(false);
212         m_zoomOutButton->setEnabled(false);
213
214         grabMouse();
215
216         m_forceReleaseTimer->start();
217         m_dragPosition = eventPosition;
218     } else {
219         m_zoomInButton->setEnabled(m_zoomInMode);
220         m_zoomOutButton->setEnabled(m_zoomOutMode);
221
222         releaseMouse();
223
224         m_forceReleaseTimer->stop();
225
226         m_zoomInButton->setDown(false);
227         m_zoomOutButton->setDown(false);
228     }
229     update();
230 }
231
232 void ZoomButtonPanel::screenResized(const QSize &newSize)
233 {
234     qDebug() << __PRETTY_FUNCTION__;
235
236     int oldHeight = 0;
237     int oldWidth = 0;
238
239     if(m_screenSize.height() < 0)
240         oldHeight = DEFAULT_NON_FULLSCREEN_HEIGHT;
241     else
242         oldHeight = m_screenSize.height();
243
244     if(m_screenSize.width() < 0)
245         oldWidth = DEFAULT_SCREEN_WIDTH;
246     else
247         oldWidth = m_screenSize.width();
248
249     m_screenSize = newSize;
250
251     QPoint resizedPosition = pos();
252
253     if(resizedPosition.x() < 0)
254         resizedPosition.rx() = 0;
255     else if(resizedPosition.x() > (newSize.width() - rect().width()))
256         resizedPosition.rx() = newSize.width() - rect().width();
257
258     if(resizedPosition.y() < 0)
259         resizedPosition.ry() = 0;
260     else if(resizedPosition.y() > (newSize.height() - rect().height()))
261         resizedPosition.ry() = newSize.height() - rect().height();
262
263     if((pos().y() + rect().center().y()) > (oldHeight / 2))
264         resizedPosition.ry() = newSize.height() - (oldHeight - pos().y());
265
266     if((pos().x() + rect().center().x()) > (oldWidth / 2))
267         resizedPosition.rx() = newSize.width() - (oldWidth - pos().x());
268
269     move(resizedPosition);
270 }
271
272 void ZoomButtonPanel::forceMouseRelease()
273 {
274     qDebug() << __PRETTY_FUNCTION__;
275
276     releaseMouse();
277     setDraggable(false);
278 }
279
280 void ZoomButtonPanel::timerExpired()
281 {
282     qDebug() << __PRETTY_FUNCTION__;
283     
284     if(m_zoomInButton->isDown())
285         m_dragPosition = m_zoomInButton->eventPosition();
286
287     if(m_zoomOutButton->isDown())
288         m_dragPosition = m_zoomOutButton->eventPosition();
289
290     setDraggable(true, m_dragPosition);
291 }