File indexing completed on 2024-06-16 04:16:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_draggable_tool_button.h"
0008 
0009 #include <QMouseEvent>
0010 
0011 #include "kis_debug.h"
0012 
0013 
0014 KisDraggableToolButton::KisDraggableToolButton(QWidget *parent)
0015     : QToolButton(parent),
0016       m_orientation(Qt::Horizontal)
0017 {
0018 }
0019 
0020 KisDraggableToolButton::~KisDraggableToolButton()
0021 {
0022 }
0023 
0024 void KisDraggableToolButton::beginDrag(const QPoint &pos)
0025 {
0026     m_startPoint = pos;
0027     m_lastPosition = m_startPoint;
0028 }
0029 
0030 int KisDraggableToolButton::continueDrag(const QPoint &pos)
0031 {
0032     QPoint diff = pos - m_startPoint;
0033 
0034     int value = 0;
0035 
0036     qreal tanx = diff.x() != 0 ? qAbs(qreal(diff.y()) / diff.x()) : 100.0;
0037 
0038     if (tanx > 10 && m_orientation == Qt::Horizontal) {
0039         m_orientation = Qt::Vertical;
0040     } else if (tanx < 0.1 && m_orientation == Qt::Vertical) {
0041         m_orientation = Qt::Horizontal;
0042     }
0043 
0044     // people like it more when the they can zoom by dragging in both directions
0045     Q_UNUSED(m_orientation);
0046 
0047     value = diff.x() - diff.y();
0048 
0049     return value;
0050 }
0051 
0052 int KisDraggableToolButton::movementDelta(const QPoint &pos)
0053 {
0054     QPoint diff = pos - m_lastPosition;
0055     m_lastPosition = pos;
0056     return diff.x() - diff.y();
0057 
0058 }
0059 
0060 void KisDraggableToolButton::mousePressEvent(QMouseEvent *e)
0061 {
0062     beginDrag(e->pos());
0063     QToolButton::mousePressEvent(e);
0064 }
0065 
0066 void KisDraggableToolButton::mouseMoveEvent(QMouseEvent *e)
0067 {
0068     int distance = continueDrag(e->pos());
0069     emit offsetChanged(distance);
0070     int delta = movementDelta(e->pos());
0071     emit valueChanged(delta);
0072 
0073     QToolButton::mouseMoveEvent(e);
0074 }