Finished implementing zoom button drag feature
[situare] / src / ui / zoombuttonpanel.cpp
index 1621712..9dfe895 100644 (file)
@@ -3,6 +3,7 @@
    Copyright (C) 2010  Ixonos Plc. Authors:
 
        Pekka Nissinen - pekka.nissinen@ixonos.com
+       Kaj Wallin - kaj.wallin@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
 
 #include <QDebug>
 #include <QPainter>
+#include <QSettings>
 
 #include "zoombuttonpanel.h"
 #include "panelcommon.h"
+#include "zoombutton.h"
+#include "common.h"
 
 ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     : QWidget(parent),
       m_zoomInButton(0),
       m_zoomOutButton(0),
-      m_panelLayout(this),
+      m_isDraggable(false),
       m_x(x),
-      m_y(y)
+      m_y(y),
+      m_panelLayout(this)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_zoomInButton = new ImageButton(this, ":/res/images/zoom_in.png");
-    m_zoomOutButton = new ImageButton(this, ":/res/images/zoom_out.png");
+    m_zoomInButton = new ZoomButton(this, ":/res/images/zoom_in.png");
+    m_zoomOutButton = new ZoomButton(this, ":/res/images/zoom_out.png");
 
     m_panelLayout.setMargin(0);
     m_panelLayout.setSpacing(0);
@@ -51,45 +56,67 @@ ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     QPalette pal = palette();
     pal.setColor(QPalette::Background, QColor(0, 0, 0, 128));
     setPalette(pal);
+
+    m_eventBlocker = new QWidget(this);
+    m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
+    m_eventBlocker->resize(size().width(),
+                           m_zoomInButton->size().height() * 2 + ZOOM_BUTTON_PANEL_BUTTON_SPACING);
+
+    connect(m_zoomInButton, SIGNAL(startDragMode(bool, QPoint)),
+            this, SLOT(setDraggable(bool, QPoint)));
+    connect(m_zoomOutButton, SIGNAL(startDragMode(bool, QPoint)),
+            this, SLOT(setDraggable(bool, QPoint)));
+
+    m_dragStartTimer = new QTimer(this);
+    m_dragStartTimer->setSingleShot(true);
+    connect(m_dragStartTimer, SIGNAL(timeout()),
+            this, SLOT(timerExpired()));
 }
 
-// BUG: Zoom buttons catch the mouse press event and it doesn't propagate to zoombuttonpanel
-void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
+void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (event->button() == Qt::LeftButton) {
-        m_dragPosition = event->pos();
-        qWarning() << "Press:" << event->pos().x() << event->pos().y()
-                   << "DP:" << m_dragPosition.x() << m_dragPosition.y();
-        event->accept();
-        m_inButtonMode = m_zoomInButton->mode();
-        m_outButtonMode = m_zoomOutButton->mode();
-        m_zoomInButton->setMode(QIcon::Disabled);
-        m_zoomOutButton->setMode(QIcon::Disabled);
+    if(m_isDraggable) {
+        if (event->buttons() & Qt::LeftButton) {
+            QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
+            if (newLocation.x() < SIDEBAR_WIDTH) {
+                newLocation.rx() = SIDEBAR_WIDTH;
+            }
+            else if (newLocation.x() > m_screenSize.width()
+                - m_eventBlocker->width()){
+                newLocation.rx() =  m_screenSize.width() - m_eventBlocker->width();
+            }
+            if (newLocation.y() < 0){
+                newLocation.ry() = 0;
+            }
+            else if (newLocation.y() > m_screenSize.height() - m_eventBlocker->height()) {
+                newLocation.ry() = m_screenSize.height() - m_eventBlocker->height();
+            }
+            move(newLocation);
+            event->accept();
+        }
     }
 }
 
-void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
+void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (event->buttons() & Qt::LeftButton) {
-        move(mapToParent(event->pos()) - m_dragPosition);
-        qWarning() << "Move:" << event->pos().x() << event->pos().y();
-        setAutoFillBackground(true);
+    if (event->button() == Qt::LeftButton) {
+        m_dragPosition = event->pos();
         event->accept();
     }
+    m_dragStartTimer->start(DRAG_INIT_TIME);
 }
 
 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
 {
-    qDebug() << __PRETTY_FUNCTION__;
-
-    setAutoFillBackground(false);
-    event->accept();
-    m_zoomInButton->setMode(m_inButtonMode);
-    m_zoomOutButton->setMode(m_outButtonMode);
+    Q_UNUSED(event);
+    setDraggable(false);
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    settings.setValue(ZOOMPANEL_POSITION, pos());
+    releaseMouse();
 }
 
 void ZoomButtonPanel::disableZoomInButton()
@@ -113,3 +140,36 @@ void ZoomButtonPanel::resetButtons()
     m_zoomInButton->setMode(QIcon::Normal);
     m_zoomOutButton->setMode(QIcon::Normal);
 }
+
+void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
+{
+    if(mode == true) {
+        m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, false);
+        m_isDraggable = mode;
+        setAutoFillBackground(true);
+        m_zoomInMode = m_zoomInButton->mode();
+        m_zoomOutMode = m_zoomOutButton->mode();
+        m_zoomInButton->setMode(QIcon::Disabled);
+        m_zoomOutButton->setMode(QIcon::Disabled);
+        grabMouse();
+        m_dragPosition = eventPosition;
+    }
+    else {
+        m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
+        m_isDraggable = mode;
+        setAutoFillBackground(false);
+        m_zoomInButton->setMode(m_zoomInMode);
+        m_zoomOutButton->setMode(m_zoomOutMode);
+        releaseMouse();
+    }
+}
+
+void ZoomButtonPanel::screenResized(const QSize &size)
+{
+    m_screenSize = size;
+}
+
+void ZoomButtonPanel::timerExpired()
+{
+    setDraggable(true, m_dragPosition);
+}