Removed timer from zoombutton, created new signals and fixed some logics
authorKaj Wallin <kaj.wallin@ixonos.com>
Wed, 26 May 2010 10:11:41 +0000 (13:11 +0300)
committerKaj Wallin <kaj.wallin@ixonos.com>
Wed, 26 May 2010 10:11:41 +0000 (13:11 +0300)
Reviewed by:

src/ui/zoombutton.cpp
src/ui/zoombutton.h
src/ui/zoombuttonpanel.cpp
src/ui/zoombuttonpanel.h

index 143fbac..ee0e629 100644 (file)
@@ -29,41 +29,39 @@ ZoomButton::ZoomButton(QWidget *parent, QString normalIconPictureFileName,
                        QString selectedIconPictureFileName)
     : ImageButton(parent, normalIconPictureFileName, selectedIconPictureFileName)
 {
-    m_dragStartTimer = new QTimer(this);
-    m_dragStartTimer->setSingleShot(true);
-    connect(m_dragStartTimer, SIGNAL(timeout()),
-            this, SLOT(timerExpired()));
+}
+
+void ZoomButton::mouseMoveEvent(QMouseEvent *event)
+{
+    if(this->rect().contains(event->pos())) {
+        m_eventPosition = mapToParent(event->pos());
+        emit eventPosition(m_eventPosition);
+    }
+    else {
+        ImageButton::mouseReleaseEvent(event);
+        emit releaseEvent();
+    }
 }
 
 void ZoomButton::mousePressEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_buttonMode != QIcon::Disabled) {
-        QAbstractButton::mousePressEvent(event);
-        setMode(QIcon::Selected);
-    }
+    ImageButton::mousePressEvent(event);
+
 
     m_eventPosition = mapToParent(event->pos());
 
-    m_dragStartTimer->start(DRAG_INIT_TIME);
+    emit pressEvent();
+    emit eventPosition(m_eventPosition);
 }
 
 void ZoomButton::mouseReleaseEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_buttonMode != QIcon::Disabled) {
-        QAbstractButton::mouseReleaseEvent(event);
-        setMode(QIcon::Normal);
-    }
+    ImageButton::mouseReleaseEvent(event);
 
-    m_dragStartTimer->stop();
-}
-void ZoomButton::timerExpired()
-{
-    qDebug() << __PRETTY_FUNCTION__;
+    emit releaseEvent();
 
-    setMode(QIcon::Normal);
-    emit startDragMode(true, m_eventPosition);
 }
index 4dc3564..f23a047 100644 (file)
@@ -51,6 +51,13 @@ public:
  ******************************************************************************/
 protected:
     /**
+     * @brief Event handler for mouse move events
+     *
+     * @param event Mouse event
+     */
+    void mouseMoveEvent(QMouseEvent *event);
+
+    /**
      * @brief Event handler for mouse press events
      *
      * @param event Mouse event
@@ -65,25 +72,12 @@ protected:
     void mouseReleaseEvent(QMouseEvent *event);
 
 /*******************************************************************************
- * MEMBER FUNCTIONS AND SLOTS
- ******************************************************************************/
-private slots:
-    /**
-     * @brief Slot to emit signal to zoom button panel to start drag mode after timer timeout
-     */
-    void timerExpired();
-
-/*******************************************************************************
  * SIGNALS
  ******************************************************************************/
 signals:
-    /**
-     * @brief Signal for starting drag mode
-     *
-     * @param mode True to start drag mode
-     * @param eventPosition Position of mousePressEvent mapped to parent
-     */
-    void startDragMode(bool mode, QPoint eventPosition);
+    void pressEvent();
+    void releaseEvent();
+    void eventPosition(QPoint m_eventPosition);
 
 /*******************************************************************************
  * DATA MEMBERS
index 63e57c3..ab5aed0 100644 (file)
 
 ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     : QWidget(parent),
-      m_zoomInButton(0),
-      m_zoomOutButton(0),
       m_isDraggable(false),
-      m_panelLayout(this)
+      m_panelLayout(this),
+      m_zoomInButton(0),
+      m_zoomOutButton(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     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);
@@ -60,15 +62,27 @@ ZoomButtonPanel::ZoomButtonPanel(QWidget *parent, int x, int y)
     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);
+    m_dragStartTimer->setInterval(DRAG_INIT_TIME);
+
+    connect(m_zoomInButton, SIGNAL(pressEvent()),
+            m_dragStartTimer, SLOT(start()));
+    connect(m_zoomInButton, SIGNAL(releaseEvent()),
+            m_dragStartTimer, SLOT(stop()));
+    connect(m_zoomOutButton, SIGNAL(pressEvent()),
+            m_dragStartTimer, SLOT(start()));
+    connect(m_zoomOutButton, SIGNAL(releaseEvent()),
+            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()));
+
 }
 
 void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
@@ -78,23 +92,25 @@ void ZoomButtonPanel::mouseMoveEvent(QMouseEvent *event)
     if(m_isDraggable) {
         if (event->buttons() & Qt::LeftButton) {
             QPoint newLocation = mapToParent(event->pos()) - m_dragPosition;
-            if (newLocation.x() < SIDEBAR_WIDTH) {
+
+            if (newLocation.x() < SIDEBAR_WIDTH)
                 newLocation.rx() = SIDEBAR_WIDTH;
-            }
-            else if (newLocation.x() > m_screenSize.width()
-                - m_eventBlocker->width()){
+            else if (newLocation.x() > m_screenSize.width() - m_eventBlocker->width())
                 newLocation.rx() =  m_screenSize.width() - m_eventBlocker->width();
-            }
-            if (newLocation.y() < 0){
+
+            if (newLocation.y() < 0)
                 newLocation.ry() = 0;
-            }
-            else if (newLocation.y() > m_screenSize.height() - m_eventBlocker->height()) {
+            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();
+    }
+
 }
 
 void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
@@ -105,7 +121,7 @@ void ZoomButtonPanel::mousePressEvent(QMouseEvent *event)
         m_dragPosition = event->pos();
         event->accept();
     }
-    m_dragStartTimer->start(DRAG_INIT_TIME);
+    m_dragStartTimer->start();
 }
 
 void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
@@ -115,6 +131,7 @@ void ZoomButtonPanel::mouseReleaseEvent(QMouseEvent *event)
     QSettings settings(DIRECTORY_NAME, FILE_NAME);
     settings.setValue(ZOOMPANEL_POSITION, pos());
     releaseMouse();
+    m_dragStartTimer->stop();
 }
 
 const ImageButton* ZoomButtonPanel::zoomInButton()
@@ -159,8 +176,10 @@ void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
         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();
@@ -170,9 +189,17 @@ void ZoomButtonPanel::setDraggable(bool mode, QPoint eventPosition)
         m_eventBlocker->setAttribute(Qt::WA_TransparentForMouseEvents, true);
         m_isDraggable = mode;
         setAutoFillBackground(false);
-        m_zoomInButton->setMode(m_zoomInMode);
-        m_zoomOutButton->setMode(m_zoomOutMode);
+        if(m_zoomInMode == QIcon::Selected)
+            m_zoomInButton->setMode(QIcon::Normal);
+        else
+            m_zoomInButton->setMode(m_zoomInMode);
+        if(m_zoomOutMode == QIcon::Selected)
+            m_zoomOutButton->setMode(QIcon::Normal);
+        else
+            m_zoomOutButton->setMode(m_zoomOutMode);
         releaseMouse();
+        m_zoomInButton->setDown(false);
+        m_zoomOutButton->setDown(false);
     }
 }
 
@@ -181,6 +208,11 @@ void ZoomButtonPanel::screenResized(const QSize &size)
     m_screenSize = size;
 }
 
+void ZoomButtonPanel::setDragPosition(QPoint eventPosition)
+{
+    m_dragPosition = eventPosition;
+}
+
 void ZoomButtonPanel::timerExpired()
 {
     setDraggable(true, m_dragPosition);
index 913b32f..715c231 100644 (file)
@@ -27,7 +27,7 @@
 #include <QGridLayout>
 #include <QGraphicsItem>
 
-#include "imagebutton.h"
+#include "zoombutton.h"
 
 /**
  * @brief Panel for zoom buttons
@@ -121,18 +121,17 @@ public slots:
     void screenResized(const QSize &size);
 
 private slots:
+
+    void setDragPosition(QPoint eventPosition);
+
     /**
-     * @brief Slot to emit signal to zoom button panel to start drag mode after timer timeout
+     * @brief Slot that handles drag initialization once timer has timed out
      */
     void timerExpired();
 
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
-public:
-    ImageButton *m_zoomInButton;    ///< Button for zoom in
-    ImageButton *m_zoomOutButton;   ///< Button for zoom out
-
 private:
     bool m_isDraggable;             ///< Boolean for tracking the draggability state
 
@@ -148,6 +147,9 @@ private:
     QTimer *m_dragStartTimer;       ///< Timer to init draggability of the zoom panel
 
     QWidget *m_eventBlocker;        ///< Overlaying widget that catches the mouse events
+
+    ZoomButton *m_zoomInButton;    ///< Button for zoom in
+    ZoomButton *m_zoomOutButton;   ///< Button for zoom out
 };
 
 #endif // ZOOMBUTTONPANEL_H