From 1ab677774b0f784f26d48866ff5d2506f7549a09 Mon Sep 17 00:00:00 2001 From: peri4 Date: Thu, 5 Aug 2021 00:41:52 +0300 Subject: [PATCH] piqt importand fix with qmap serialization ImageView now supprt view interaction BlockView update background when EnabledChange --- libs/blockview/blockview.cpp | 3 +++ libs/piqt/piqt.h | 16 +++++++++++++- libs/widgets/image_view.cpp | 41 ++++++++++++++++++++---------------- libs/widgets/image_view.h | 8 ++++--- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/libs/blockview/blockview.cpp b/libs/blockview/blockview.cpp index a44b471..9a7d008 100644 --- a/libs/blockview/blockview.cpp +++ b/libs/blockview/blockview.cpp @@ -113,6 +113,9 @@ bool BlockView::event(QEvent * e) { QGraphicsView::scale(cscl / prev_app_scale, cscl / prev_app_scale); prev_app_scale = cscl; } + if (e->type() == QEvent::EnabledChange) { + _updateBack(); + } return QGraphicsView::event(e); } diff --git a/libs/piqt/piqt.h b/libs/piqt/piqt.h index f07a268..0ae3e74 100644 --- a/libs/piqt/piqt.h +++ b/libs/piqt/piqt.h @@ -127,6 +127,20 @@ inline const QVector PI2QVector(const PIVector & v) {QVector ret; ret.r template inline const PIVector Q2PIVector(const QVector & v) {if (v.isEmpty()) return PIVector(); return PIVector(v.constData(), (size_t)v.size());} +template +inline QMap PI2QMap(const PIMap & v) { + QMap ret; + auto it = v.makeIterator(); + while (it.hasNext()) {it.next(); ret[it.key()] = it.value();} + return ret;} +template +inline PIMap Q2PIMap(const QMap & v) { + PIMap ret; + ret.reserve((size_t)v.size()); + QMapIterator it(v); + while (it.hasNext()) {it.next(); ret[it.key()] = it.value();} + return ret;} + inline PIPropertyStorage Q2PIPropertyStorage(const PropertyStorage & props) { PIPropertyStorage ret; @@ -203,7 +217,7 @@ template inline PIByteArray & operator <<(PIByteArray & t.reserve(v.size()); QMapIterator it(v); while (it.hasNext()) {it.next(); t[it.key()] = it.value();} - s >> t; + s << t; return s;} template inline PIByteArray & operator >>(PIByteArray & s, QMap & v) { v.clear(); diff --git a/libs/widgets/image_view.cpp b/libs/widgets/image_view.cpp index bb52119..13533ea 100644 --- a/libs/widgets/image_view.cpp +++ b/libs/widgets/image_view.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,7 @@ ImageView::ImageView(QWidget * parent): QGraphicsView(parent) { viewport()->setAutoFillBackground(false); viewport()->installEventFilter(this); autofit_ = true; + interactive_ = false; } @@ -40,6 +42,11 @@ void ImageView::setPixmap(QPixmap pixmap) { } +void ImageView::setViewInteractive(bool yes) { + interactive_ = yes; +} + + void ImageView::setImage(const QImage & i, bool save) { im_data.clear(); if (i.isNull()) { @@ -87,20 +94,14 @@ void ImageView::mousePressEvent(QMouseEvent * e) { } -void ImageView::mouseMoveEvent(QMouseEvent * e) { - //if (e->buttons().testFlag(Qt::RightButton) && !autofit_ && isInteractive()) return;; - QGraphicsView::mouseMoveEvent(e); +void ImageView::wheelEvent(QWheelEvent * e) { + if (!e->modifiers().testFlag(Qt::ControlModifier) || !viewInteractive()) return; + double scl = 1. + e->angleDelta().y() / 500.; + autofit_ = false; + scale(scl, scl); } -//void ImageView::wheelEvent(QWheelEvent * e) { -// if (!e->modifiers().testFlag(Qt::ControlModifier) || !isInteractive()) return; -// double scl = 1. + e->delta() / 500.; -// //autofit_ = false; -// //scale(scl, scl); -//} - - bool ImageView::eventFilter(QObject * o, QEvent * e) { QMouseEvent * me = (QMouseEvent *)e; switch (e->type()) { @@ -108,16 +109,20 @@ bool ImageView::eventFilter(QObject * o, QEvent * e) { adjustView(); break; case QEvent::MouseButtonPress: + prev_pos = me->pos(); + if (me->buttons().testFlag(Qt::RightButton) && !autofit_ && viewInteractive()) + viewport()->setCursor(Qt::ClosedHandCursor); + break; + case QEvent::MouseButtonRelease: + viewport()->unsetCursor(); prev_pos = me->pos(); break; case QEvent::MouseMove: - if (!me->buttons().testFlag(Qt::RightButton) || autofit_ || !isInteractive()) break; - { - //double scl = 1. / qSqrt(transform().determinant()); - //QPointF dp = QPointF(me->pos() - prev_pos) * scl; - //qDebug() << dp; - //translate(0.00001, 0); - prev_pos = me->pos(); + if (me->buttons().testFlag(Qt::RightButton) && !autofit_ && viewInteractive()) { + QPointF dp = QPointF(me->pos() - prev_pos); + horizontalScrollBar()->setValue(horizontalScrollBar()->value() - dp.x()); + verticalScrollBar()->setValue(verticalScrollBar()->value() - dp.y()); + prev_pos = me->pos(); } break; default: break; diff --git a/libs/widgets/image_view.h b/libs/widgets/image_view.h index d4bce49..1a71067 100644 --- a/libs/widgets/image_view.h +++ b/libs/widgets/image_view.h @@ -29,6 +29,7 @@ class QAD_WIDGETS_EXPORT ImageView: public QGraphicsView { Q_OBJECT Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap) + Q_PROPERTY(bool viewInteractive READ viewInteractive WRITE setViewInteractive) public: ImageView(QWidget * parent = 0); ~ImageView(); @@ -37,27 +38,28 @@ public: void setImage(const QByteArray & i); QByteArray image() const {return im_data;} QPixmap pixmap() const; + bool viewInteractive() const {return interactive_;} void clear(); private: void mouseDoubleClickEvent(QMouseEvent * e); void mousePressEvent(QMouseEvent * e); - void mouseMoveEvent(QMouseEvent * e); -// void wheelEvent(QWheelEvent * e); + void wheelEvent(QWheelEvent * e); bool eventFilter(QObject * o, QEvent * e); void adjustView(); QGraphicsPixmapItem item; QByteArray im_data; QPoint prev_pos; - bool autofit_; + bool autofit_, interactive_; QPixmap map; public slots: void autofit(); void setPixmap(QPixmap pixmap); + void setViewInteractive(bool yes); signals: void clicked(QPointF, Qt::MouseButtons);