Finished zoom panel drag feature fixes
[situare] / src / ui / zoombuttonpanel.cpp
index c012710..e411d3d 100644 (file)
@@ -29,7 +29,7 @@
 #include "zoombutton.h"
 #include "common.h"
 
-ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
+ZoomButtonPanel::ZoomButtonPanel(QWidget *parent)
     : QWidget(parent),
       m_isDraggable(false),
       m_panelLayout(this),
@@ -41,8 +41,6 @@ ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     m_zoomInButton = new ZoomButton(this, ":/res/images/zoom_in.png");
     m_zoomOutButton = new ZoomButton(this, ":/res/images/zoom_out.png");
 
-    m_zoomInButton->setObjectName("ZoomInButton");
-    m_zoomOutButton->setObjectName("ZoomOutButton");
     m_panelLayout.setMargin(0);
     m_panelLayout.setSpacing(0);
     m_panelLayout.setVerticalSpacing(ZOOM_BUTTON_PANEL_BUTTON_SPACING);
@@ -51,73 +49,62 @@ ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     m_panelLayout.addWidget(m_zoomInButton, 0, 0);
     m_panelLayout.addWidget(m_zoomOutButton, 1, 0);
 
-    move(x, y);
+    QSettings settings(DIRECTORY_NAME, FILE_NAME);
+    move(settings.value(ZOOMPANEL_POSITION,
+                        QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
+                               ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint());
 
     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);
-
     m_dragStartTimer = new QTimer(this);
     m_dragStartTimer->setSingleShot(true);
     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
 
-    connect(m_zoomInButton, SIGNAL(pressEvent()),
+    m_forceReleaseTimer = new QTimer(this);
+    m_forceReleaseTimer->setSingleShot(true);
+    m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
+
+    connect(m_zoomInButton, SIGNAL(pressed()),
             m_dragStartTimer, SLOT(start()));
-    connect(m_zoomInButton, SIGNAL(releaseEvent()),
+    connect(m_zoomInButton, SIGNAL(released()),
             m_dragStartTimer, SLOT(stop()));
-    connect(m_zoomOutButton, SIGNAL(pressEvent()),
+    connect(m_zoomOutButton, SIGNAL(pressed()),
             m_dragStartTimer, SLOT(start()));
-    connect(m_zoomOutButton, SIGNAL(releaseEvent()),
+    connect(m_zoomOutButton, SIGNAL(released()),
             m_dragStartTimer, SLOT(stop()));
 
-    connect(m_zoomInButton, SIGNAL(eventPosition(QPoint)),
-            this, SLOT(setDragPosition(QPoint)));
-    connect(m_zoomOutButton, SIGNAL(eventPosition(QPoint)),
-            this, SLOT(setDragPosition(QPoint)));
-
     connect(m_dragStartTimer, SIGNAL(timeout()),
             this, SLOT(timerExpired()));
-
+    connect(m_forceReleaseTimer, SIGNAL(timeout()),
+            this, SLOT(forceMouseRelease()));
 }
 
 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    if(this->rect().contains(event->pos())) {
-        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();
-            }
-        }
-        else {
-            m_dragPosition = event->pos();
+    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() - width() - SIDEBAR_WIDTH)
+                newLocation.rx() =  m_screenSize.width() - width() - SIDEBAR_WIDTH;
+
+            if (newLocation.y() < 0)
+                newLocation.ry() = 0;
+            else if (newLocation.y() > m_screenSize.height() - height())
+                newLocation.ry() = m_screenSize.height() - height();
+
+            move(newLocation);
         }
-    }
-    else {
-        if(m_isDraggable)
-            mouseReleaseEvent(event);
-        else
+    } else {
+        if(!rect().contains(event->pos()))
             m_dragStartTimer->stop();
     }
-
+    QWidget::mouseMoveEvent(event);
 }
 
 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
@@ -126,9 +113,9 @@ void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
 
     if (event->button() == Qt::LeftButton) {
         m_dragPosition = event->pos();
-        event->accept();
     }
     m_dragStartTimer->start();
+    QWidget::mousePressEvent(event);
 }
 
 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
@@ -141,7 +128,10 @@ void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
         settings.setValue(ZOOMPANEL_POSITION, pos());
         releaseMouse();
         m_dragStartTimer->stop();
+        m_zoomInButton->setDown(false);
+        m_zoomOutButton->setDown(false);
     }
+    QWidget::mouseReleaseEvent(event);
 }
 
 const ZoomButton* ZoomButtonPanel::zoomInButton()
@@ -184,9 +174,9 @@ void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(mode == true) {
-        m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, false);
-        m_isDraggable = mode;
+    m_isDraggable = mode;
+
+    if(mode) {
         setAutoFillBackground(true);
 
         m_zoomInMode = m_zoomInButton->mode();
@@ -195,11 +185,9 @@ void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
         m_zoomInButton->setMode(QIcon::Disabled);
         m_zoomOutButton->setMode(QIcon::Disabled);
         grabMouse();
+        m_forceReleaseTimer->start();
         m_dragPosition = eventPosition;
-    }
-    else {
-        m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
-        m_isDraggable = mode;
+    } else {
         setAutoFillBackground(false);
         if(m_zoomInMode == QIcon::Selected)
             m_zoomInButton->setMode(QIcon::Normal);
@@ -210,28 +198,43 @@ void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
         else
             m_zoomOutButton->setMode(m_zoomOutMode);
         releaseMouse();
+        m_forceReleaseTimer->stop();
         m_zoomInButton->setDown(false);
         m_zoomOutButton->setDown(false);
     }
 }
 
-void ZoomButtonPanel::screenResized(const QSize &size)
+void ZoomButtonPanel::screenResized(const QSize &newSize)
 {
     qDebug() << __PRETTY_FUNCTION__;
-
-    m_screenSize = size;
+    m_screenSize = newSize;
+
+    QPoint resizedPosition = pos();
+    if(resizedPosition.x() > (newSize.width() - rect().width()) - SIDEBAR_WIDTH)
+        resizedPosition.rx() = newSize.width() - rect().width() - SIDEBAR_WIDTH;
+    else if (resizedPosition.x() < SIDEBAR_WIDTH)
+        resizedPosition.rx() = SIDEBAR_WIDTH;
+    if(resizedPosition.y() > (newSize.height() - rect().height()))
+        resizedPosition.ry() = newSize.height() - rect().height();
+    else if (resizedPosition.y() < 0)
+        resizedPosition.ry() = 0;
+    move(resizedPosition);
 }
 
-void ZoomButtonPanel::setDragPosition(QPoint eventPosition)
-{
+void ZoomButtonPanel::forceMouseRelease() {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_dragPosition = eventPosition;
+    releaseMouse();
+    setDraggable(false);
 }
 
 void ZoomButtonPanel::timerExpired()
 {
     qDebug() << __PRETTY_FUNCTION__;
+    if(m_zoomInButton->isDown())
+        m_dragPosition = m_zoomInButton->eventPosition();
+    if(m_zoomOutButton->isDown())
+        m_dragPosition = m_zoomOutButton->eventPosition();
 
     setDraggable(true, m_dragPosition);
 }