+/*
+ Situare - A location system for Facebook
+ Copyright (C) 2010 Ixonos Plc. Authors:
+
+ Kaj Wallin - kaj.wallin@ixonos.com
+ Katri Kaikkonen - katri.kaikkonen@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 <QDebug>
+#include <QPainter>
+#include <QSettings>
+
+#include "indicatorbutton.h"
+#include "math.h"
+#include "common.h"
+#include "panelcommon.h"
+
+const int ROUNDING_RADIUS = 9; ///< Roundness of the rounded edge
+const qreal OPACITY = 0.13; ///< Opacity of the background in percents
+const int BUTTON_WIDTH = 66; ///< Button width
+const int BUTTON_HEIGHT = 66; ///< Button height
+
+IndicatorButton::IndicatorButton(QWidget *parent)
+ : QToolButton(parent),
+ m_isDraggable(false)
+{
+ setIcon(QIcon::fromTheme(QLatin1String("general_fullsize")));
+ setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
+
+ QPalette pal = palette();
+ QColor backgroundColor = pal.color(backgroundRole());
+ backgroundColor.setAlpha(floor(OPACITY * 255));
+ pal.setColor(backgroundRole(), backgroundColor);
+ setPalette(pal);
+
+ setAutoFillBackground(true);
+
+ m_dragStartTimer = new QTimer(this);
+ m_dragStartTimer->setSingleShot(true);
+ m_dragStartTimer->setInterval(DRAG_INIT_TIME);
+
+ m_forceReleaseTimer = new QTimer(this);
+ m_forceReleaseTimer->setSingleShot(true);
+ m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
+
+ connect(this, SIGNAL(pressed()),
+ m_dragStartTimer, SLOT(start()));
+ connect(this, SIGNAL(released()),
+ m_dragStartTimer, SLOT(stop()));
+
+ connect(m_dragStartTimer, SIGNAL(timeout()),
+ this, SLOT(timerExpired()));
+ connect(m_forceReleaseTimer, SIGNAL(timeout()),
+ this, SLOT(forceMouseRelease()));
+
+ connect(this, SIGNAL(triggered(bool)),
+ this, SIGNAL(autoCenteringTriggered(bool)));
+
+ // Create mask to round the corners of the background
+ QRegion maskbase(rect());
+
+ // left top corner
+ QRegion leftTopBase(QRect(0, 0, ROUNDING_RADIUS, ROUNDING_RADIUS));
+ QRegion leftTopRounding(QRect(0, 0, 2 * ROUNDING_RADIUS, 2 * ROUNDING_RADIUS), QRegion::Ellipse);
+ QRegion leftTopClipped = leftTopBase.subtract(leftTopRounding);
+
+ // right top corner
+ QRegion rightTopBase(QRect(rect().width() - ROUNDING_RADIUS, 0,
+ rect().width(), ROUNDING_RADIUS));
+ QRegion rightTopRounding(QRect(rect().width() - 2 * ROUNDING_RADIUS, 0,
+ 2 * ROUNDING_RADIUS, 2 * ROUNDING_RADIUS), QRegion::Ellipse);
+ QRegion rightTopClipped = rightTopBase.subtract(rightTopRounding);
+
+ // left bottom corner
+ QRegion leftBottomBase(QRect(0, rect().height() - ROUNDING_RADIUS,
+ ROUNDING_RADIUS, rect().height()));
+ QRegion leftBottomRounding(QRect(0, rect().height() - 2 * ROUNDING_RADIUS,
+ 2 * ROUNDING_RADIUS, 2 * ROUNDING_RADIUS), QRegion::Ellipse);
+ QRegion leftBottomClipped = leftBottomBase.subtract(leftBottomRounding);
+
+ // right bottom corner
+ QRegion rightBottomBase(QRect(rect().width() - ROUNDING_RADIUS, rect().height()- ROUNDING_RADIUS,
+ rect().height() - ROUNDING_RADIUS, rect().width()- ROUNDING_RADIUS));
+ QRegion rightBottomRounding(QRect(rect().width() - 2 * ROUNDING_RADIUS, rect().height() - 2 * ROUNDING_RADIUS,
+ 2 * ROUNDING_RADIUS, 2 * ROUNDING_RADIUS), QRegion::Ellipse);
+ QRegion rightBottomClipped = rightBottomBase.subtract(rightBottomRounding);
+
+ QRegion cutter = leftTopClipped.unite(rightTopClipped.unite(leftBottomClipped.unite(rightBottomClipped)));
+
+ QRegion mask = maskbase.subtract(cutter);
+
+ setMask(mask);
+}
+
+void IndicatorButton::mousePressEvent(QMouseEvent *event)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (event->button() == Qt::LeftButton) {
+ m_dragPosition = event->pos();
+ }
+ m_dragStartTimer->start();
+ QWidget::mousePressEvent(event);
+}
+
+void IndicatorButton::mouseMoveEvent(QMouseEvent *event)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ 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(!rect().contains(event->pos()))
+ m_dragStartTimer->stop();
+
+ QWidget::mouseMoveEvent(event);
+}
+
+void IndicatorButton::mouseReleaseEvent(QMouseEvent *event)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_dragStartTimer->stop();
+
+ Q_UNUSED(event);
+ if(m_isDraggable) {
+ setDraggable(false);
+// QSettings settings(DIRECTORY_NAME, FILE_NAME);
+// settings.setValue(ZOOMPANEL_POSITION, pos());
+ releaseMouse();
+ }
+ QWidget::mouseReleaseEvent(event);
+}
+
+void IndicatorButton::setDraggable(bool mode, QPoint eventPosition)
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ m_isDraggable = mode;
+
+ if(mode) {
+ setAutoFillBackground(true);
+ grabMouse();
+ m_forceReleaseTimer->start();
+ m_dragPosition = eventPosition;
+ } else {
+ setAutoFillBackground(false);
+ releaseMouse();
+ m_forceReleaseTimer->stop();
+
+ }
+}
+
+const QPoint& IndicatorButton::eventPosition()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ return m_eventPosition;
+}
+
+void IndicatorButton::forceMouseRelease()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ releaseMouse();
+ setDraggable(false);
+}
+
+void IndicatorButton::timerExpired()
+{
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if(this->isDown())
+ m_dragPosition = this->eventPosition();
+
+ setDraggable(true, m_dragPosition);
+}