pineapple-pictures/app/navigatorview.cpp

81 lines
1.9 KiB
C++
Raw Permalink Normal View History

2019-10-03 17:57:14 +08:00
#include "navigatorview.h"
2019-10-04 09:54:13 +08:00
#include "graphicsview.h"
2019-10-06 14:58:01 +08:00
#include "opacityhelper.h"
2019-10-04 09:54:13 +08:00
2019-10-03 17:57:14 +08:00
#include <QMouseEvent>
2019-10-04 09:54:13 +08:00
#include <QDebug>
2019-10-03 17:57:14 +08:00
NavigatorView::NavigatorView(QWidget *parent)
: QGraphicsView (parent)
2019-10-04 09:54:13 +08:00
, m_viewportRegion(this->rect())
2019-10-06 14:58:01 +08:00
, m_opacityHelper(new OpacityHelper(this))
2019-10-03 17:57:14 +08:00
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
2019-10-04 21:34:20 +08:00
setStyleSheet("background-color: rgba(0, 0, 0, 120);"
2019-10-03 17:57:14 +08:00
"border-radius: 3px;");
}
2019-10-04 09:54:13 +08:00
// doesn't take or manage its ownership
void NavigatorView::setMainView(GraphicsView *mainView)
{
m_mainView = mainView;
}
2019-10-06 14:58:01 +08:00
void NavigatorView::setOpacity(qreal opacity, bool animated)
{
m_opacityHelper->setOpacity(opacity, animated);
}
2019-10-04 09:54:13 +08:00
void NavigatorView::updateMainViewportRegion()
{
if (m_mainView != nullptr) {
m_viewportRegion = mapFromScene(m_mainView->mapToScene(m_mainView->rect()));
}
}
2019-10-04 20:41:27 +08:00
void NavigatorView::mousePressEvent(QMouseEvent *event)
{
m_mouseDown = true;
if (m_mainView) {
m_mainView->centerOn(mapToScene(event->pos()));
2019-10-04 21:34:20 +08:00
update();
2019-10-04 20:41:27 +08:00
}
return QGraphicsView::mousePressEvent(event);
}
void NavigatorView::mouseMoveEvent(QMouseEvent *event)
{
if (m_mouseDown && m_mainView) {
m_mainView->centerOn(mapToScene(event->pos()));
2019-10-04 21:34:20 +08:00
update();
2019-10-04 20:41:27 +08:00
}
return QGraphicsView::mouseMoveEvent(event);
}
void NavigatorView::mouseReleaseEvent(QMouseEvent *event)
{
m_mouseDown = false;
return QGraphicsView::mouseReleaseEvent(event);
}
2019-10-03 17:57:14 +08:00
void NavigatorView::wheelEvent(QWheelEvent *event)
{
event->ignore();
return QGraphicsView::wheelEvent(event);
}
2019-10-04 09:54:13 +08:00
void NavigatorView::paintEvent(QPaintEvent *event)
{
QGraphicsView::paintEvent(event);
QPainter painter(viewport());
painter.setPen(QPen(Qt::gray, 2));
painter.drawRect(m_viewportRegion.boundingRect());
}