summaryrefslogtreecommitdiff
path: root/src/frontend/keychooser/cscrollbutton.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/keychooser/cscrollbutton.cpp')
-rw-r--r--src/frontend/keychooser/cscrollbutton.cpp116
1 files changed, 57 insertions, 59 deletions
diff --git a/src/frontend/keychooser/cscrollbutton.cpp b/src/frontend/keychooser/cscrollbutton.cpp
index 742bc53..3287eef 100644
--- a/src/frontend/keychooser/cscrollbutton.cpp
+++ b/src/frontend/keychooser/cscrollbutton.cpp
@@ -2,20 +2,14 @@
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
-* Copyright 1999-2008 by the BibleTime developers.
+* Copyright 1999-2009 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/
-
-
#include "cscrollbutton.h"
-
-#include <stdlib.h>
-#include <math.h>
-
-//Qt includes
+#include <cmath>
#include <QEvent>
#include <QApplication>
#include <QCursor>
@@ -23,63 +17,67 @@
#include <QMouseEvent>
#include <QWheelEvent>
-CScrollButton::CScrollButton(QWidget *parent) : QToolButton(parent) {
- setFocusPolicy(Qt::WheelFocus);
- setCursor(Qt::SplitVCursor );
-
- m_isLocked = false;
- connect(this, SIGNAL(pressed() ), SLOT(was_pressed() ));
- connect(this, SIGNAL(released()), SLOT(was_released()));
-}
-
-bool CScrollButton::isLocked( ) const {
- return m_isLocked;
+CScrollButton::CScrollButton(QWidget *parent)
+ : QToolButton(parent), m_isLocked(false)
+{
+ setFocusPolicy(Qt::WheelFocus);
+ setCursor(Qt::SplitVCursor);
}
-void CScrollButton::was_pressed( ) {
- QApplication::setOverrideCursor(Qt::BlankCursor);
- m_isLocked = true;
- lock_Point = get_lock_Point();
-
- emit lock()
- ;
+CScrollButton::~CScrollButton() {
+ // Intentionally empty
}
-void CScrollButton::was_released( ) {
- QApplication::restoreOverrideCursor();
- m_isLocked = false;
-
- emit unlock();
+void CScrollButton::mousePressEvent(QMouseEvent *e) {
+ if (m_isLocked) return;
+ if (e->button() != Qt::LeftButton) return;
+ m_isLocked = true;
+ grabMouse(Qt::BlankCursor);
+ emit lock();
}
-const QPoint CScrollButton::get_lock_Point() const {
- return mapToGlobal( QPoint( width()/2, height()/2 ) );
+void CScrollButton::mouseReleaseEvent(QMouseEvent *e) {
+ if (!m_isLocked) return;
+ if (e->button() != Qt::LeftButton) return;
+ m_isLocked = false;
+ releaseMouse();
+ emit unlock();
}
-void CScrollButton::mouseMoveEvent( QMouseEvent* e ) {
- if (m_isLocked) {
- int vchange = (QCursor::pos().y() - lock_Point.y());
-
- if (abs(vchange) < 10) {
- vchange = (int)((vchange>0 ? 1 : -1) * pow(abs(vchange), 0.3));
- }
- else if (abs(vchange) < 30) {
- vchange = (int)((vchange>0 ? 1 : -1) * pow(abs(vchange), 0.6));
- }
- else if (abs(vchange) < 40) {
- vchange = (int)((vchange>0 ? 1 : -1) * pow(abs(vchange), 1.2));
- }
- else {
- vchange = (int)((vchange>0 ? 1 : -1) * pow(abs(vchange), 2.0));
- }
-
- if (vchange) { //not emit 0
- emit change_requested( vchange );
- }
-
- QCursor::setPos( lock_Point );
- }
- else {
- QToolButton::mouseMoveEvent(e);
- }
+void CScrollButton::mouseMoveEvent(QMouseEvent *e) {
+ if (m_isLocked) {
+ // Recalculate the center of the widget (might change during grab):
+ QPoint center(mapToGlobal(QPoint(width() / 2, height() / 2)));
+
+ // Calculate movement change:
+ int vchange = (e->globalY() - center.y());
+
+ if (vchange != 0) {
+ // Calculate the real change we are going to emit:
+ int avchange(vchange >= 0 ? vchange : -vchange);
+ if (avchange < 10) {
+ avchange = (int) pow(avchange, 0.3);
+ } else if (avchange < 30) {
+ avchange = (int) pow(avchange, 0.6);
+ } else if (avchange < 40) {
+ avchange = (int) pow(avchange, 1.2);
+ } else {
+ avchange = (int) pow(avchange, 2.0);
+ }
+
+ // Emit the change request signal only when necessary:
+ if (avchange != 0) {
+ if (vchange > 0) {
+ emit change_requested(avchange);
+ } else if (vchange < 0) {
+ emit change_requested(-avchange);
+ }
+ }
+ }
+
+ // Move the mouse cursor to the center of this widget:
+ QCursor::setPos(center);
+ } else {
+ QToolButton::mouseMoveEvent(e);
+ }
}