Merge branch 'master' into zoom_button_drag
[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_zoomInButton(0),
35       m_zoomOutButton(0),
36       m_isDraggable(false),
37       m_panelLayout(this)
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     move(x, y);
53
54     QPalette pal = palette();
55     pal.setColor(QPalette::Background, QColor(0, 0, 0, 128));
56     setPalette(pal);
57
58     m_eventBlocker = new QWidget(this);
59     m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
60     m_eventBlocker->resize(size().width(),
61                            m_zoomInButton->size().height() * 2 + ZOOM_BUTTON_PANEL_BUTTON_SPACING);
62
63     connect(m_zoomInButton, SIGNAL(startDragMode(bool, QPoint)),
64             this, SLOT(setDraggable(bool, QPoint)));
65     connect(m_zoomOutButton, SIGNAL(startDragMode(bool, QPoint)),
66             this, SLOT(setDraggable(bool, QPoint)));
67
68     m_dragStartTimer = new QTimer(this);
69     m_dragStartTimer->setSingleShot(true);
70     connect(m_dragStartTimer, SIGNAL(timeout()),
71             this, SLOT(timerExpired()));
72 }
73
74 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
75 {
76     qDebug() << __PRETTY_FUNCTION__;
77
78     if(m_isDraggable) {
79         if (event->buttons() & Qt::LeftButton) {
80             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
81             if (newLocation.x() < SIDEBAR_WIDTH) {
82                 newLocation.rx() = SIDEBAR_WIDTH;
83             }
84             else if (newLocation.x() > m_screenSize.width()
85                 - m_eventBlocker->width()){
86                 newLocation.rx() =  m_screenSize.width() - m_eventBlocker->width();
87             }
88             if (newLocation.y() < 0){
89                 newLocation.ry() = 0;
90             }
91             else if (newLocation.y() > m_screenSize.height() - m_eventBlocker->height()) {
92                 newLocation.ry() = m_screenSize.height() - m_eventBlocker->height();
93             }
94             move(newLocation);
95             event->accept();
96         }
97     }
98 }
99
100 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
101 {
102     qDebug() << __PRETTY_FUNCTION__;
103
104     if (event->button() == Qt::LeftButton) {
105         m_dragPosition = event->pos();
106         event->accept();
107     }
108     m_dragStartTimer->start(DRAG_INIT_TIME);
109 }
110
111 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
112 {
113     Q_UNUSED(event);
114     setDraggable(false);
115     QSettings settings(DIRECTORY_NAME, FILE_NAME);
116     settings.setValue(ZOOMPANEL_POSITION, pos());
117     releaseMouse();
118 }
119
120 const ImageButton* ZoomButtonPanel::zoomInButton()
121 {
122     qDebug() << __PRETTY_FUNCTION__;
123
124     return m_zoomInButton;
125 }
126
127 const ImageButton* ZoomButtonPanel::zoomOutButton()
128 {
129     qDebug() << __PRETTY_FUNCTION__;
130
131     return m_zoomOutButton;
132 }
133
134 void ZoomButtonPanel::disableZoomInButton()
135 {
136     qDebug() << __PRETTY_FUNCTION__;
137
138     m_zoomInButton->setMode(QIcon::Disabled);
139 }
140
141 void ZoomButtonPanel::disableZoomOutButton()
142 {
143     qDebug() << __PRETTY_FUNCTION__;
144
145     m_zoomOutButton->setMode(QIcon::Disabled);
146 }
147
148 void ZoomButtonPanel::resetButtons()
149 {
150     qDebug() << __PRETTY_FUNCTION__;
151
152     m_zoomInButton->setMode(QIcon::Normal);
153     m_zoomOutButton->setMode(QIcon::Normal);
154 }
155
156 void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
157 {
158     if(mode == true) {
159         m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, false);
160         m_isDraggable = mode;
161         setAutoFillBackground(true);
162         m_zoomInMode = m_zoomInButton->mode();
163         m_zoomOutMode = m_zoomOutButton->mode();
164         m_zoomInButton->setMode(QIcon::Disabled);
165         m_zoomOutButton->setMode(QIcon::Disabled);
166         grabMouse();
167         m_dragPosition = eventPosition;
168     }
169     else {
170         m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
171         m_isDraggable = mode;
172         setAutoFillBackground(false);
173         m_zoomInButton->setMode(m_zoomInMode);
174         m_zoomOutButton->setMode(m_zoomOutMode);
175         releaseMouse();
176     }
177 }
178
179 void ZoomButtonPanel::screenResized(const QSize &size)
180 {
181     m_screenSize = size;
182 }
183
184 void ZoomButtonPanel::timerExpired()
185 {
186     setDraggable(true, m_dragPosition);
187 }