Finished zoom panel drag feature fixes
authorKaj Wallin <kaj.wallin@ixonos.com>
Wed, 26 May 2010 13:12:25 +0000 (16:12 +0300)
committerKaj Wallin <kaj.wallin@ixonos.com>
Wed, 26 May 2010 13:12:25 +0000 (16:12 +0300)
Reviewed by: Marko Niemelä

src/common.h
src/ui/imagebutton.cpp
src/ui/mainwindow.cpp
src/ui/panelcommon.h
src/ui/zoombutton.cpp
src/ui/zoombutton.h
src/ui/zoombuttonpanel.cpp
src/ui/zoombuttonpanel.h

index c9669f1..9c295f1 100644 (file)
@@ -34,5 +34,4 @@ const QColor COLOR_GRAY = QColor(152, 152, 152);                           ///<
 const QFont NOKIA_FONT_NORMAL = QFont("Nokia Sans", 18, QFont::Normal);    ///< Normal font
 const QFont NOKIA_FONT_SMALL = QFont("Nokia Sans", 13, QFont::Normal);     ///< Small font
 
-const QString ZOOMPANEL_POSITION = "ZoomPanelPosition";
 #endif // COMMON_H
index 2f4dac8..321423d 100644 (file)
@@ -66,6 +66,9 @@ void ImageButton::mousePressEvent(QMouseEvent *event)
     if(m_buttonMode != QIcon::Disabled) {
         QPushButton::mousePressEvent(event);
         setMode(QIcon::Selected);
+    } else {
+        setDown(true);
+        emit pressed();
     }
 }
 
@@ -76,6 +79,9 @@ void ImageButton::mouseReleaseEvent(QMouseEvent *event)
     if(m_buttonMode != QIcon::Disabled) {
         QPushButton::mouseReleaseEvent(event);
         setMode(QIcon::Normal);
+    } else {
+        setDown(false);
+        emit released();
     }
 }
 
index c4e54d9..d5d1134 100644 (file)
@@ -116,9 +116,6 @@ void MainWindow::buildFriendListPanel()
             this, SIGNAL(findFriend(QPointF)));
 
     connect(m_mapView, SIGNAL(viewResized(QSize)),
-            m_zoomButtonPanel, SLOT(screenResized(QSize)));
-
-    connect(m_mapView, SIGNAL(viewResized(QSize)),
             m_friendsListPanel, SLOT(screenResized(QSize)));
 
     connect(this, SIGNAL(locationItemClicked(QList<QString>)),
@@ -223,8 +220,7 @@ void MainWindow::buildZoomButtonPanel()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_zoomButtonPanel = new ZoomButtonPanel(this, ZOOM_BUTTON_PANEL_POSITION_X,
-                                            ZOOM_BUTTON_PANEL_POSITION_Y);
+    m_zoomButtonPanel = new ZoomButtonPanel(this);
 
     connect(m_zoomButtonPanel->zoomInButton(), SIGNAL(clicked()),
             this, SIGNAL(zoomIn()));
@@ -241,10 +237,8 @@ void MainWindow::buildZoomButtonPanel()
     connect(this, SIGNAL(minZoomLevelReached()),
             m_zoomButtonPanel, SLOT(disableZoomOutButton()));
 
-    QSettings settings(DIRECTORY_NAME, FILE_NAME);
-    m_zoomButtonPanel->move(settings.value(ZOOMPANEL_POSITION,
-                                           QPoint(ZOOM_BUTTON_PANEL_POSITION_X,
-                                                  ZOOM_BUTTON_PANEL_POSITION_Y)).toPoint());
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_zoomButtonPanel, SLOT(screenResized(QSize)));
 }
 
 void MainWindow::createMenus()
index f39cc00..481fb87 100644 (file)
 
 enum Side {LEFT, RIGHT};  ///< Enumerator for panel sideness
 
-const int DRAG_INIT_TIME = 1000;    ///< How long buttons must be pressed to start drag mode
+const int DRAG_INIT_TIME = 1000;        ///< How long buttons must be pressed to start drag mode
+const int FORCE_RELEASE_TIME = 10000;   ///< How long mouse can be grabbed
 
-const int SIDEBAR_WIDTH = 23;       ///< Width of the sidebar
-const int SIDEBAR_HEIGHT = 424;     ///< Height of the sidebar
+const int SIDEBAR_WIDTH = 23;           ///< Width of the sidebar
+const int SIDEBAR_HEIGHT = 424;         ///< Height of the sidebar
 
-const int SLIDINGBAR_WIDTH = 24;    ///< Width of the slidingbar
-const int SLIDINGBAR_HEIGHT = 424;  ///< Height of the slidingbar
+const int SLIDINGBAR_WIDTH = 24;        ///< Width of the slidingbar
+const int SLIDINGBAR_HEIGHT = 424;      ///< Height of the slidingbar
 
-const int PANEL_PEEK_AMOUNT = 25; ///< Amount of pixels shown when panel is closed
+const int PANEL_PEEK_AMOUNT = 25;       ///< Amount of pixels shown when panel is closed
 
-const int FRIENDPANEL_WIDTH  = 430; ///< Width of the friends list panel
+const int FRIENDPANEL_WIDTH  = 430;     ///< Width of the friends list panel
 const int FRIENDPANEL_HEIGHT = DEFAULT_SCREEN_HEIGHT; ///< Height of the friends list panel
 
-const int USERPANEL_WIDTH  = 300; ///< Width of the user panel
+const int USERPANEL_WIDTH  = 300;       ///< Width of the user panel
 const int USERPANEL_HEIGHT = DEFAULT_SCREEN_HEIGHT; ///< Height of the user panetl
 
 const int MARGIN_CORRECTION = 1; ///< Amount of correction to make panels touch the window borders
 const int MARGIN_CORRECTION2 = 2; ///< Amount of correction to make panels touch the window borders
 
-const int PANEL_TOP_Y = 0; ///< Y coordinate for top of both panels
-const int TOP_CORNER_X = 0;  ///< X coordinate for top left corner
+const int PANEL_TOP_Y = 0;              ///< Y coordinate for top of both panels
+const int TOP_CORNER_X = 0;             ///< X coordinate for top left corner
 
 const int FRIENDPANEL_CLOSED_X =
         DEFAULT_SCREEN_WIDTH - PANEL_PEEK_AMOUNT
-        - SLIDINGBAR_WIDTH; ///< X location of the friend list panel when closed
+        - SLIDINGBAR_WIDTH;             ///< X location of the friend list panel when closed
 const int FRIENDPANEL_OPENED_X =
         DEFAULT_SCREEN_WIDTH - FRIENDPANEL_WIDTH
-        - SLIDINGBAR_WIDTH; ///< X location of the friend list panel when opened
+        - SLIDINGBAR_WIDTH;             ///< X location of the friend list panel when opened
 
 const int USERPANEL_CLOSED_X =
         2 - USERPANEL_WIDTH + PANEL_PEEK_AMOUNT;  ///< X location of the user panel when closed
-const int USERPANEL_OPENED_X = 0;     ///< X location of the user panel when opened
+const int USERPANEL_OPENED_X = 0;       ///< X location of the user panel when opened
 
 
 const int ZOOM_BUTTON_PANEL_POSITION_X = 10
@@ -66,4 +67,6 @@ const int ZOOM_BUTTON_PANEL_POSITION_X = 10
 const int ZOOM_BUTTON_PANEL_POSITION_Y = 10; ///< Vertical position of zoom panel
 const int ZOOM_BUTTON_PANEL_BUTTON_SPACING = 4; ///< Size of a zoom button spacing
 
+const QString ZOOMPANEL_POSITION = "Zoom_Panel_Position";
+
 #endif // PANELCOMMON_H
index dbfc6b3..f1d8439 100644 (file)
@@ -34,16 +34,11 @@ ZoomButton::ZoomButton(QWidget *parent, QString normalIconPictureFileName,
 void ZoomButton::mouseMoveEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
-    //qWarning() << "ZB release";
 
-    if(this->rect().contains(event->pos())) {
+    if(rect().contains(event->pos())) {
         m_eventPosition = mapToParent(event->pos());
-        emit eventPosition(m_eventPosition);
-    }
-    else {
-        ImageButton::mouseReleaseEvent(event);
-        emit releaseEvent();
     }
+    ImageButton::mouseMoveEvent(event);
 }
 
 void ZoomButton::mousePressEvent(QMouseEvent *event)
@@ -53,17 +48,11 @@ void ZoomButton::mousePressEvent(QMouseEvent *event)
     ImageButton::mousePressEvent(event);
 
     m_eventPosition = mapToParent(event->pos());
-
-    emit pressEvent();
-    emit eventPosition(m_eventPosition);
 }
 
-void ZoomButton::mouseReleaseEvent(QMouseEvent *event)
+const QPoint& ZoomButton::eventPosition()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    ImageButton::mouseReleaseEvent(event);
-
-    emit releaseEvent();
-
+    return m_eventPosition;
 }
index f23a047..745981f 100644 (file)
@@ -64,27 +64,18 @@ protected:
      */
     void mousePressEvent(QMouseEvent *event);
 
-    /**
-     * @brief Event handler for mouse release events
-     *
-     * @param event Mouse event
-     */
-    void mouseReleaseEvent(QMouseEvent *event);
-
 /*******************************************************************************
- * SIGNALS
+ * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
-signals:
-    void pressEvent();
-    void releaseEvent();
-    void eventPosition(QPoint m_eventPosition);
+public:
+    const QPoint& eventPosition();
 
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
 private:
     QTimer *m_dragStartTimer;   ///< Mouse press timer, initiates drag mode
-    QPoint m_eventPosition;       ///< Position of mousePressEvent
+    QPoint m_eventPosition;     ///< Position of mousePressEvent
 };
 
 #endif // ZOOMBUTTON_H
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);
 }
index 962c4af..8da59de 100644 (file)
@@ -47,7 +47,7 @@ public:
      * @param x Panel x coordinate
      * @param y Panel y coordinate
      */
-    ZoomButtonPanel(QWidget *parent = 0, int x = 0, int y = 0);
+    ZoomButtonPanel(QWidget *parent = 0);
 
 /*******************************************************************************
  * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
@@ -121,8 +121,10 @@ public slots:
     void screenResized(const QSize &size);
 
 private slots:
-
-    void setDragPosition(QPoint eventPosition);
+    /**
+     * @brief Safeguard slot to release mouse grab if something goes horribly wrong
+     */
+    void forceMouseRelease();
 
     /**
      * @brief Slot that handles drag initialization once timer has timed out
@@ -145,8 +147,7 @@ private:
     QSize m_screenSize;             ///< Store for the screen size
 
     QTimer *m_dragStartTimer;       ///< Timer to init draggability of the zoom panel
-
-    QWidget *m_eventBlocker;        ///< Overlaying widget that catches the mouse events
+    QTimer *m_forceReleaseTimer;    ///< Timer to run forceMouseRelease;
 
     ZoomButton *m_zoomInButton;    ///< Button for zoom in
     ZoomButton *m_zoomOutButton;   ///< Button for zoom out