piqt importand fix with qmap serialization

ImageView now supprt view interaction
BlockView update background when EnabledChange
This commit is contained in:
2021-08-05 00:41:52 +03:00
parent a9203554c5
commit 1ab677774b
4 changed files with 46 additions and 22 deletions

View File

@@ -113,6 +113,9 @@ bool BlockView::event(QEvent * e) {
QGraphicsView::scale(cscl / prev_app_scale, cscl / prev_app_scale); QGraphicsView::scale(cscl / prev_app_scale, cscl / prev_app_scale);
prev_app_scale = cscl; prev_app_scale = cscl;
} }
if (e->type() == QEvent::EnabledChange) {
_updateBack();
}
return QGraphicsView::event(e); return QGraphicsView::event(e);
} }

View File

@@ -127,6 +127,20 @@ inline const QVector<T> PI2QVector(const PIVector<T> & v) {QVector<T> ret; ret.r
template<typename T> template<typename T>
inline const PIVector<T> Q2PIVector(const QVector<T> & v) {if (v.isEmpty()) return PIVector<T>(); return PIVector<T>(v.constData(), (size_t)v.size());} inline const PIVector<T> Q2PIVector(const QVector<T> & v) {if (v.isEmpty()) return PIVector<T>(); return PIVector<T>(v.constData(), (size_t)v.size());}
template <typename K, typename T>
inline QMap<K, T> PI2QMap(const PIMap<K, T> & v) {
QMap<K, T> ret;
auto it = v.makeIterator();
while (it.hasNext()) {it.next(); ret[it.key()] = it.value();}
return ret;}
template <typename K, typename T>
inline PIMap<K, T> Q2PIMap(const QMap<K, T> & v) {
PIMap<K, T> ret;
ret.reserve((size_t)v.size());
QMapIterator<K, T> it(v);
while (it.hasNext()) {it.next(); ret[it.key()] = it.value();}
return ret;}
inline PIPropertyStorage Q2PIPropertyStorage(const PropertyStorage & props) { inline PIPropertyStorage Q2PIPropertyStorage(const PropertyStorage & props) {
PIPropertyStorage ret; PIPropertyStorage ret;
@@ -203,7 +217,7 @@ template <typename K, typename T> inline PIByteArray & operator <<(PIByteArray &
t.reserve(v.size()); t.reserve(v.size());
QMapIterator<K, T> it(v); QMapIterator<K, T> it(v);
while (it.hasNext()) {it.next(); t[it.key()] = it.value();} while (it.hasNext()) {it.next(); t[it.key()] = it.value();}
s >> t; s << t;
return s;} return s;}
template <typename K, typename T> inline PIByteArray & operator >>(PIByteArray & s, QMap<K, T> & v) { template <typename K, typename T> inline PIByteArray & operator >>(PIByteArray & s, QMap<K, T> & v) {
v.clear(); v.clear();

View File

@@ -4,6 +4,7 @@
#include <QEvent> #include <QEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QWheelEvent> #include <QWheelEvent>
#include <QScrollBar>
#include <QDebug> #include <QDebug>
#include <qmath.h> #include <qmath.h>
@@ -22,6 +23,7 @@ ImageView::ImageView(QWidget * parent): QGraphicsView(parent) {
viewport()->setAutoFillBackground(false); viewport()->setAutoFillBackground(false);
viewport()->installEventFilter(this); viewport()->installEventFilter(this);
autofit_ = true; 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) { void ImageView::setImage(const QImage & i, bool save) {
im_data.clear(); im_data.clear();
if (i.isNull()) { if (i.isNull()) {
@@ -87,20 +94,14 @@ void ImageView::mousePressEvent(QMouseEvent * e) {
} }
void ImageView::mouseMoveEvent(QMouseEvent * e) { void ImageView::wheelEvent(QWheelEvent * e) {
//if (e->buttons().testFlag(Qt::RightButton) && !autofit_ && isInteractive()) return;; if (!e->modifiers().testFlag(Qt::ControlModifier) || !viewInteractive()) return;
QGraphicsView::mouseMoveEvent(e); 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) { bool ImageView::eventFilter(QObject * o, QEvent * e) {
QMouseEvent * me = (QMouseEvent *)e; QMouseEvent * me = (QMouseEvent *)e;
switch (e->type()) { switch (e->type()) {
@@ -108,16 +109,20 @@ bool ImageView::eventFilter(QObject * o, QEvent * e) {
adjustView(); adjustView();
break; break;
case QEvent::MouseButtonPress: 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(); prev_pos = me->pos();
break; break;
case QEvent::MouseMove: case QEvent::MouseMove:
if (!me->buttons().testFlag(Qt::RightButton) || autofit_ || !isInteractive()) break; if (me->buttons().testFlag(Qt::RightButton) && !autofit_ && viewInteractive()) {
{ QPointF dp = QPointF(me->pos() - prev_pos);
//double scl = 1. / qSqrt(transform().determinant()); horizontalScrollBar()->setValue(horizontalScrollBar()->value() - dp.x());
//QPointF dp = QPointF(me->pos() - prev_pos) * scl; verticalScrollBar()->setValue(verticalScrollBar()->value() - dp.y());
//qDebug() << dp; prev_pos = me->pos();
//translate(0.00001, 0);
prev_pos = me->pos();
} }
break; break;
default: break; default: break;

View File

@@ -29,6 +29,7 @@ class QAD_WIDGETS_EXPORT ImageView: public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
Q_PROPERTY(bool viewInteractive READ viewInteractive WRITE setViewInteractive)
public: public:
ImageView(QWidget * parent = 0); ImageView(QWidget * parent = 0);
~ImageView(); ~ImageView();
@@ -37,27 +38,28 @@ public:
void setImage(const QByteArray & i); void setImage(const QByteArray & i);
QByteArray image() const {return im_data;} QByteArray image() const {return im_data;}
QPixmap pixmap() const; QPixmap pixmap() const;
bool viewInteractive() const {return interactive_;}
void clear(); void clear();
private: private:
void mouseDoubleClickEvent(QMouseEvent * e); void mouseDoubleClickEvent(QMouseEvent * e);
void mousePressEvent(QMouseEvent * e); void mousePressEvent(QMouseEvent * e);
void mouseMoveEvent(QMouseEvent * e); void wheelEvent(QWheelEvent * e);
// void wheelEvent(QWheelEvent * e);
bool eventFilter(QObject * o, QEvent * e); bool eventFilter(QObject * o, QEvent * e);
void adjustView(); void adjustView();
QGraphicsPixmapItem item; QGraphicsPixmapItem item;
QByteArray im_data; QByteArray im_data;
QPoint prev_pos; QPoint prev_pos;
bool autofit_; bool autofit_, interactive_;
QPixmap map; QPixmap map;
public slots: public slots:
void autofit(); void autofit();
void setPixmap(QPixmap pixmap); void setPixmap(QPixmap pixmap);
void setViewInteractive(bool yes);
signals: signals:
void clicked(QPointF, Qt::MouseButtons); void clicked(QPointF, Qt::MouseButtons);