Modified FullScreenButton class to use simlair drawing methods as the IndicatorButton...
[situare] / src / ui / indicatorbutton.cpp
index 5466cb7..1bd3b22 100644 (file)
 
 enum State {OFF, ON};           ///< Enumerator for led state
 
-const qreal OPACITY = 0.50;     ///< Opacity of the background in percents
 const int ROUNDING_RADIUS = 9;  ///< Roundness of the rounded edge
 const int BUTTON_WIDTH = 66;    ///< Button width
 const int BUTTON_HEIGHT = 66;   ///< Button height
 
+const qreal OPACITY = 0.50;     ///< Opacity of the background in percents
+
 IndicatorButton::IndicatorButton(QWidget *parent)
     : QToolButton(parent),
       m_isDraggable(false)
@@ -63,10 +64,6 @@ IndicatorButton::IndicatorButton(QWidget *parent)
     m_selectedGradient->setColorAt(0.75, QColor(82, 195, 255));
     m_selectedGradient->setColorAt(0.98, QColor(115, 215, 255));
 
-    // Item shape path
-    m_backgroundPath.addRoundedRect(0, 0, this->rect().width(), this->rect().height(),
-                                    ROUNDING_RADIUS, ROUNDING_RADIUS);
-
     m_dragStartTimer = new QTimer(this);
     m_dragStartTimer->setSingleShot(true);
     m_dragStartTimer->setInterval(DRAG_INIT_TIME);
@@ -77,19 +74,16 @@ IndicatorButton::IndicatorButton(QWidget *parent)
 
     connect(this, SIGNAL(pressed()),
             m_dragStartTimer, SLOT(start()));
-
     connect(this, SIGNAL(released()),
             m_dragStartTimer, SLOT(stop()));
+    connect(this, SIGNAL(clicked(bool)),
+            this, SIGNAL(autoCenteringTriggered(bool)));
 
     connect(m_dragStartTimer, SIGNAL(timeout()),
             this, SLOT(timerExpired()));
-
     connect(m_forceReleaseTimer, SIGNAL(timeout()),
             this, SLOT(forceMouseRelease()));
 
-    connect(this, SIGNAL(clicked(bool)),
-            this, SIGNAL(autoCenteringTriggered(bool)));
-
     setCheckable(true);
 }
 
@@ -110,6 +104,7 @@ void IndicatorButton::mousePressEvent(QMouseEvent *event)
 
     m_eventPosition = mapToParent(event->pos());
     m_dragStartTimer->start();
+    setDown(true);
 }
 
 void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
@@ -133,37 +128,35 @@ void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
             move(newLocation);
         }
     } else {
-        if(!rect().contains(event->pos()))
+        if(!rect().contains(event->pos())) {
             m_dragStartTimer->stop();
+            setDown(false);
+        }
     }
-
-    QToolButton::mouseMoveEvent(event);
 }
 
 void IndicatorButton::mouseReleaseEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    Q_UNUSED(event);
-
     m_dragStartTimer->stop();
 
     if(m_isDraggable) {
         setDraggable(false);
         QSettings settings(DIRECTORY_NAME, FILE_NAME);
         settings.setValue(DIRECTION_INDICATOR_BUTTON_POSITION, pos());
-        releaseMouse();
     } else {
-        if(isChecked()) {
-            setChecked(false);
-            emit autoCenteringTriggered(false);
-        } else {
-            setChecked(true);
-            emit autoCenteringTriggered(true);
+        if(this->rect().contains(event->pos())) {
+            if(isChecked()) {
+                setChecked(false);
+                emit autoCenteringTriggered(false);
+            } else {
+                setChecked(true);
+                emit autoCenteringTriggered(true);
+            }
         }
     }
-
-    QToolButton::mouseReleaseEvent(event);
+    setDown(false);
 }
 
 void IndicatorButton::setDraggable(bool mode, QPoint eventPosition)
@@ -173,14 +166,11 @@ void IndicatorButton::setDraggable(bool mode, QPoint eventPosition)
     m_isDraggable = mode;
 
     if(mode) {
-        grabMouse();
         m_forceReleaseTimer->start();
         m_dragPosition = eventPosition;
     } else {
-        releaseMouse();
         m_forceReleaseTimer->stop();
     }
-
     update();
 }
 
@@ -192,10 +182,12 @@ void IndicatorButton::screenResized(const QSize &newSize)
 
     QPoint resizedPosition = pos();
 
+#ifdef Q_WS_MAEMO_5
     if(newSize.height() < DEFAULT_SCREEN_HEIGHT)
         resizedPosition.ry() -= SCREEN_HEIGHT_DIFFERENCE;
     else
         resizedPosition.ry() += SCREEN_HEIGHT_DIFFERENCE;
+#endif // Q_WS_MAEMO_5
 
     move(resizedPosition);
 }
@@ -221,15 +213,18 @@ void IndicatorButton::paintEvent(QPaintEvent *event)
 
     Q_UNUSED(event);
 
+    QPainterPath backgroundPath;
+    backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
+
     QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing);
 
     if(m_isDraggable)
-        painter.fillPath(m_backgroundPath, QBrush(Qt::Dense4Pattern));
+        painter.fillPath(backgroundPath, QBrush(Qt::Dense4Pattern));
     else if (isDown())
-        painter.fillPath(m_backgroundPath, QBrush(*m_selectedGradient));
+        painter.fillPath(backgroundPath, QBrush(*m_selectedGradient));
     else
-        painter.fillPath(m_backgroundPath, QBrush(*m_normalColor));
+        painter.fillPath(backgroundPath, QBrush(*m_normalColor));
 
     if(isChecked())
         painter.drawPixmap((this->width() / 2) - (m_indicatorLeds[ON].width() / 2),
@@ -245,8 +240,5 @@ void IndicatorButton::timerExpired()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(this->isDown())
-        m_dragPosition = this->eventPosition();
-
     setDraggable(true, m_dragPosition);
 }