implement keyboard zooming into node view
This commit is contained in:
@@ -85,6 +85,16 @@ public:
|
||||
node_view_->SetColorLabel(index);
|
||||
}
|
||||
|
||||
virtual void ZoomIn() override
|
||||
{
|
||||
node_view_->ZoomIn();
|
||||
}
|
||||
|
||||
virtual void ZoomOut() override
|
||||
{
|
||||
node_view_->ZoomOut();
|
||||
}
|
||||
|
||||
public slots:
|
||||
void Select(const QVector<Node*>& nodes)
|
||||
{
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
|
||||
namespace olive {
|
||||
|
||||
const double NodeView::kMinimumScale = 0.1;
|
||||
|
||||
NodeView::NodeView(QWidget *parent) :
|
||||
HandMovableView(parent),
|
||||
graph_(nullptr),
|
||||
@@ -328,6 +330,16 @@ void NodeView::SetColorLabel(int index)
|
||||
}
|
||||
}
|
||||
|
||||
void NodeView::ZoomIn()
|
||||
{
|
||||
ZoomFromKeyboard(1.25);
|
||||
}
|
||||
|
||||
void NodeView::ZoomOut()
|
||||
{
|
||||
ZoomFromKeyboard(0.8);
|
||||
}
|
||||
|
||||
void NodeView::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
super::keyPressEvent(event);
|
||||
@@ -565,26 +577,14 @@ void NodeView::wheelEvent(QWheelEvent *event)
|
||||
if (TimeBasedView::WheelEventIsAZoomEvent(event)) {
|
||||
qreal multiplier = 1.0 + (static_cast<qreal>(event->angleDelta().x() + event->angleDelta().y()) * 0.001);
|
||||
|
||||
double test_scale = scale_ * multiplier;
|
||||
|
||||
if (test_scale > 0.1) {
|
||||
QPointF cursor_pos;
|
||||
QPointF cursor_pos;
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
||||
cursor_pos = event->position();
|
||||
cursor_pos = event->position();
|
||||
#else
|
||||
cursor_pos = event->posF();
|
||||
cursor_pos = event->posF();
|
||||
#endif
|
||||
|
||||
int anchor_x = qRound(double(cursor_pos.x() + horizontalScrollBar()->value()) / scale_ * test_scale - cursor_pos.x());
|
||||
int anchor_y = qRound(double(cursor_pos.y() + verticalScrollBar()->value()) / scale_ * test_scale - cursor_pos.y());
|
||||
|
||||
scale(multiplier, multiplier);
|
||||
|
||||
this->horizontalScrollBar()->setValue(anchor_x);
|
||||
this->verticalScrollBar()->setValue(anchor_y);
|
||||
|
||||
scale_ = test_scale;
|
||||
}
|
||||
ZoomIntoCursorPosition(multiplier, cursor_pos);
|
||||
} else {
|
||||
QWidget::wheelEvent(event);
|
||||
}
|
||||
@@ -807,4 +807,33 @@ void NodeView::DisconnectSelectionChangedSignal()
|
||||
disconnect(&scene_, &QGraphicsScene::selectionChanged, this, &NodeView::UpdateSelectionCache);
|
||||
}
|
||||
|
||||
void NodeView::ZoomIntoCursorPosition(double multiplier, const QPointF& cursor_pos)
|
||||
{
|
||||
double test_scale = scale_ * multiplier;
|
||||
|
||||
if (test_scale > kMinimumScale) {
|
||||
int anchor_x = qRound(double(cursor_pos.x() + horizontalScrollBar()->value()) / scale_ * test_scale - cursor_pos.x());
|
||||
int anchor_y = qRound(double(cursor_pos.y() + verticalScrollBar()->value()) / scale_ * test_scale - cursor_pos.y());
|
||||
|
||||
scale(multiplier, multiplier);
|
||||
|
||||
this->horizontalScrollBar()->setValue(anchor_x);
|
||||
this->verticalScrollBar()->setValue(anchor_y);
|
||||
|
||||
scale_ = test_scale;
|
||||
}
|
||||
}
|
||||
|
||||
void NodeView::ZoomFromKeyboard(double multiplier)
|
||||
{
|
||||
QPoint cursor_pos = mapFromGlobal(QCursor::pos());
|
||||
|
||||
// If the cursor is not currently within the widget, zoom into the center
|
||||
if (!rect().contains(cursor_pos)) {
|
||||
cursor_pos = QPoint(width()/2, height()/2);
|
||||
}
|
||||
|
||||
ZoomIntoCursorPosition(multiplier, cursor_pos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -74,6 +74,10 @@ public:
|
||||
|
||||
void SetColorLabel(int index);
|
||||
|
||||
void ZoomIn();
|
||||
|
||||
void ZoomOut();
|
||||
|
||||
signals:
|
||||
void NodesSelected(const QVector<Node*>& nodes);
|
||||
|
||||
@@ -102,6 +106,10 @@ private:
|
||||
void ConnectSelectionChangedSignal();
|
||||
void DisconnectSelectionChangedSignal();
|
||||
|
||||
void ZoomIntoCursorPosition(double multiplier, const QPointF &cursor_pos);
|
||||
|
||||
void ZoomFromKeyboard(double multiplier);
|
||||
|
||||
NodeGraph* graph_;
|
||||
|
||||
struct AttachedItem {
|
||||
@@ -135,6 +143,8 @@ private:
|
||||
|
||||
double scale_;
|
||||
|
||||
static const double kMinimumScale;
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief Receiver for when the scene's selected items change
|
||||
|
||||
Reference in New Issue
Block a user