126 lines
3.1 KiB
C++
126 lines
3.1 KiB
C++
/*
|
|
QGLView
|
|
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 this programap-> If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "scroll_spin_box.h"
|
|
#include "ui_scroll_spin_box.h"
|
|
#include <evalspinbox.h>
|
|
#include <qad_types.h>
|
|
#include <QStyleOptionSpinBox>
|
|
#include <QApplication>
|
|
#include <QMouseEvent>
|
|
#include <QStyle>
|
|
#include <QDebug>
|
|
|
|
|
|
ScrollSpinBox::ScrollSpinBox(QWidget * parent): QWidget(parent) {
|
|
ui = new Ui::ScrollSpinBox();
|
|
ui->setupUi(this);
|
|
ui->spin->setPrecision(3);
|
|
ui->handle->installEventFilter(this);
|
|
connect(ui->spin, SIGNAL(valueChanged(double)), this, SIGNAL(valueChanged(double)));
|
|
last_value = 0.;
|
|
sensivity_ = 0.2;
|
|
scroll_scale = sensivity_ / 10;
|
|
canceled = false;
|
|
}
|
|
|
|
|
|
EvalSpinBox * ScrollSpinBox::spin() {
|
|
return ui->spin;
|
|
}
|
|
|
|
|
|
double ScrollSpinBox::value() const {
|
|
return ui->spin->value();
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::changeEvent(QEvent * e) {
|
|
QWidget::changeEvent(e);
|
|
switch (e->type()) {
|
|
case QEvent::LanguageChange:
|
|
ui->retranslateUi(this);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::resizeEvent(QResizeEvent * e) {
|
|
QStyleOptionSpinBox so;
|
|
so.initFrom(ui->spin);
|
|
QRect r = qApp->style()->subControlRect(QStyle::CC_SpinBox, &so, QStyle::SC_SpinBoxUp);
|
|
ui->handle->setFixedWidth(qMax<int>(ui->handle->height() / 3, r.width()));
|
|
scroll_scale = sensivity_ / appScale(this) / 10;
|
|
}
|
|
|
|
|
|
bool ScrollSpinBox::eventFilter(QObject * o, QEvent * e) {
|
|
switch (e->type()) {
|
|
case QEvent::MouseButtonPress:
|
|
mousePress((QMouseEvent*)e);
|
|
return true;
|
|
case QEvent::MouseButtonRelease:
|
|
mouseRelease((QMouseEvent*)e);
|
|
return true;
|
|
case QEvent::MouseMove:
|
|
mouseMove((QMouseEvent*)e);
|
|
return true;
|
|
default: break;
|
|
}
|
|
return QWidget::eventFilter(o, e);
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::mousePress(QMouseEvent * e) {
|
|
if (canceled) return;
|
|
if (e->button() == Qt::RightButton) {
|
|
canceled = true;
|
|
ui->spin->setExpression(last_text);
|
|
}
|
|
if (e->button() == Qt::LeftButton) {
|
|
down_pos = e->pos();
|
|
last_text = ui->spin->expression();
|
|
last_value = qAbs(ui->spin->value());
|
|
if (last_value == 0.) last_value = 1.;
|
|
}
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::mouseRelease(QMouseEvent * e) {
|
|
if (e->buttons() == Qt::NoButton) canceled = false;
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::mouseMove(QMouseEvent * e) {
|
|
if (canceled) return;
|
|
if (e->buttons().testFlag(Qt::LeftButton)) {
|
|
double dv = (down_pos.y() - e->pos().y()) * scroll_scale;
|
|
if (dv != 0.) {
|
|
QCursor::setPos(ui->handle->mapToGlobal(down_pos));
|
|
ui->spin->setValue(ui->spin->value() + last_value * dv);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void ScrollSpinBox::setValue(double v) {
|
|
ui->spin->setValue(v);
|
|
}
|