Finished implementing zoom button drag feature zoom_button_drag
authorKaj Wallin <kaj.wallin@ixonos.com>
Tue, 25 May 2010 11:56:25 +0000 (14:56 +0300)
committerKaj Wallin <kaj.wallin@ixonos.com>
Tue, 25 May 2010 11:56:25 +0000 (14:56 +0300)
Reviewed by:

src/common.h
src/src.pro
src/ui/imagebutton.h
src/ui/mainwindow.cpp
src/ui/panelcommon.h
src/ui/zoombutton.cpp [new file with mode: 0644]
src/ui/zoombutton.h [new file with mode: 0644]
src/ui/zoombuttonpanel.cpp
src/ui/zoombuttonpanel.h

index 9c295f1..c9669f1 100644 (file)
@@ -34,4 +34,5 @@ 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 7049872..2304a01 100644 (file)
@@ -41,7 +41,8 @@ SOURCES += main.cpp \
     map/gpslocationitem.cpp \
     ui/zoombuttonpanel.cpp \
     ui/userinfo.cpp \
-    ui/sidepanel.cpp
+    ui/sidepanel.cpp \
+    ui/zoombutton.cpp
 HEADERS += ui/mainwindow.h \
     map/mapengine.h \
     map/mapview.h \
@@ -82,7 +83,8 @@ HEADERS += ui/mainwindow.h \
     ui/zoombuttonpanel.h \
     common.h \
     ui/userinfo.h \
-    ui/sidepanel.h
+    ui/sidepanel.h \
+    ui/zoombutton.h
 QT += network \
     webkit
 
index df47c61..a7c2f0b 100644 (file)
@@ -96,9 +96,9 @@ public:
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
-private:
-    QIcon::Mode m_buttonMode; ///< Button mode (Normal, Selected etc...)
-    QSize m_buttonSize; ///< Button size
+protected:
+    QIcon::Mode m_buttonMode;   ///< Button mode (Normal, Selected etc...)
+    QSize m_buttonSize;         ///< Button size
 };
 
 #endif // IMAGEBUTTON_H
index 125dee1..1f37081 100644 (file)
@@ -117,6 +117,9 @@ void MainWindow::buildFriendListPanel()
     connect(m_mapView, SIGNAL(viewResized(QSize)),
             m_friendsListPanel, SLOT(screenResized(QSize)));
 
+    connect(m_mapView, SIGNAL(viewResized(QSize)),
+            m_zoomButtonPanel, SLOT(screenResized(QSize)));
+
     connect(m_mapView, SIGNAL(viewResizedNewSize(int, int)),
             friendsListPanelSidebar, SLOT(reDrawSidebar(int, int)));
 }
@@ -233,6 +236,11 @@ 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());
 }
 
 void MainWindow::createMenus()
index a4579ca..f39cc00 100644 (file)
 
 enum Side {LEFT, RIGHT};  ///< Enumerator for panel sideness
 
-const int SIDEBAR_WIDTH = 23;
-const int SIDEBAR_HEIGHT = 424;
+const int DRAG_INIT_TIME = 1000;    ///< How long buttons must be pressed to start drag mode
 
-const int SLIDINGBAR_WIDTH = 24;
-const int SLIDINGBAR_HEIGHT = 424;
+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 PANEL_PEEK_AMOUNT = 25; ///< Amount of pixels shown when panel is closed
 
@@ -48,16 +50,19 @@ 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
+        DEFAULT_SCREEN_WIDTH - PANEL_PEEK_AMOUNT
+        - 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
+        DEFAULT_SCREEN_WIDTH - FRIENDPANEL_WIDTH
+        - 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 ZOOM_BUTTON_PANEL_POSITION_X = 10 + PANEL_PEEK_AMOUNT; ///< Horizontal position of zoom panel
+const int ZOOM_BUTTON_PANEL_POSITION_X = 10
+                                         + PANEL_PEEK_AMOUNT; ///< Horizontal position of zoom panel
 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
 
diff --git a/src/ui/zoombutton.cpp b/src/ui/zoombutton.cpp
new file mode 100644 (file)
index 0000000..0fefdff
--- /dev/null
@@ -0,0 +1,69 @@
+ /*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        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
+    version 2 as published by the Free Software Foundation.
+
+    Situare is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Situare; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+    USA.
+ */
+
+
+#include "zoombutton.h"
+#include <QDebug>
+#include <QTimer>
+#include "panelcommon.h"
+
+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::mousePressEvent(QMouseEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_buttonMode != QIcon::Disabled) {
+        QAbstractButton::mousePressEvent(event);
+        setMode(QIcon::Selected);
+    }
+
+    eventPosition = mapToParent(event->pos());
+
+    m_dragStartTimer->start(DRAG_INIT_TIME);
+}
+
+void ZoomButton::mouseReleaseEvent(QMouseEvent *event)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if(m_buttonMode != QIcon::Disabled) {
+        QAbstractButton::mouseReleaseEvent(event);
+        setMode(QIcon::Normal);
+    }
+
+    m_dragStartTimer->stop();
+}
+void ZoomButton::timerExpired()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    setMode(QIcon::Normal);
+    emit startDragMode(true, eventPosition);
+}
diff --git a/src/ui/zoombutton.h b/src/ui/zoombutton.h
new file mode 100644 (file)
index 0000000..b444827
--- /dev/null
@@ -0,0 +1,96 @@
+ /*
+    Situare - A location system for Facebook
+    Copyright (C) 2010  Ixonos Plc. Authors:
+
+        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
+    version 2 as published by the Free Software Foundation.
+
+    Situare is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Situare; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+    USA.
+ */
+
+#include "imagebutton.h"
+
+#ifndef ZOOMBUTTON_H
+#define ZOOMBUTTON_H
+
+class QTimer;
+
+/**
+ * @brief A class for Zoom Buttons
+ *
+ * @author Kaj Wallin - kaj.wallin (at) ixonos.com
+ */
+class ZoomButton : public ImageButton
+{
+    Q_OBJECT
+
+public:
+    /**
+     * @brief Constructor
+     *
+     * @param parent Parent
+     * @param normalIconPictureFileName Normal state Icon image file name
+     * @param selectedIconPictureFileName Selected state Icon image file name (optional)
+     */
+    ZoomButton(QWidget *parent = 0, QString normalIconPictureFileName = "",
+               QString selectedIconPictureFileName = "");
+
+/*******************************************************************************
+ * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
+ ******************************************************************************/
+protected:
+    /**
+     * @brief Event handler for mouse press events
+     *
+     * @param event Mouse event
+     */
+    void mousePressEvent(QMouseEvent *event);
+
+    /**
+     * @brief Event handler for mouse release events
+     *
+     * @param event Mouse event
+     */
+    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);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    QTimer *m_dragStartTimer;   ///< Mouse press timer, initiates drag mode
+    QPoint eventPosition;       ///< Position of mousePressEvent
+};
+
+#endif // ZOOMBUTTON_H
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);
+}
index 828cba4..07e1eb4 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
@@ -33,6 +34,7 @@
  * @brief Panel for zoom buttons
  *
  * @author Pekka Nissinen - pekka.nissinen (at) ixonos.com
+ * @author Kaj Wallin - kaj.wallin (at) ixonos.com
  */
 class ZoomButtonPanel : public QWidget
 {
@@ -49,6 +51,31 @@ public:
     ZoomButtonPanel(QWidget *parent = 0, int x = 0, int y = 0);
 
 /*******************************************************************************
+ * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
+ ******************************************************************************/
+protected:
+    /**
+     * @brief Move event for the zoom button panel
+     *
+     * @param event Event
+     */
+    void mouseMoveEvent(QMouseEvent *event);
+
+    /**
+     * @brief Press event for the zoom button panel
+     *
+     * @param event Event
+     */
+    void mousePressEvent(QMouseEvent *event);
+
+    /**
+     * @brief Event handler for mouse release events
+     *
+     * @param event Mouse event
+     */
+    void mouseReleaseEvent(QMouseEvent *event);
+
+/*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
 public slots:
@@ -67,26 +94,44 @@ public slots:
      */
     void resetButtons();
 
-protected:
-    void mouseMoveEvent(QMouseEvent *event);
-    void mousePressEvent(QMouseEvent *event);
-    void mouseReleaseEvent(QMouseEvent *event);
+    /**
+     * @brief Toggle zoom panel draggability
+     */
+    void setDraggable(bool mode, QPoint eventPosition = QPoint(0,0));
 
+    void screenResized(const QSize &size);
+
+private slots:
+    /**
+     * @brief Slot to emit signal to zoom button panel to start drag mode after timer timeout
+     */
+    void timerExpired();
 
 /*******************************************************************************
  * DATA MEMBERS
  ******************************************************************************/
 public:
-    ImageButton *m_zoomInButton;     ///< Button for zoom in
-    ImageButton *m_zoomOutButton;    ///< Button for zoom out
+    ImageButton *m_zoomInButton;    ///< Button for zoom in
+    ImageButton *m_zoomOutButton;   ///< Button for zoom out
 
 private:
-    QGridLayout m_panelLayout;  ///< Panel layout
-    QPoint m_dragPosition;
-    QIcon::Mode m_inButtonMode;
-    QIcon::Mode m_outButtonMode;
-    int m_x;                    ///< Panel x coordinate
-    int m_y;                    ///< Panel y coordinate
+    bool m_isDraggable;             ///< Boolean for tracking the draggability state
+
+    int m_x;                        ///< Panel x coordinate
+    int m_y;                        ///< Panel y coordinate
+
+    QGridLayout m_panelLayout;      ///< Panel layout
+
+    QPoint m_dragPosition;          ///< Location from where the widget is grabbed
+
+    QIcon::Mode m_zoomInMode;       ///< Store for zoom in button mode before dragging
+    QIcon::Mode m_zoomOutMode;      ///< Store for zoom out button mode before dragging
+
+    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
 };
 
 #endif // ZOOMBUTTONPANEL_H