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);
prev_app_scale = cscl;
}
if (e->type() == QEvent::EnabledChange) {
_updateBack();
}
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>
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) {
PIPropertyStorage ret;
@@ -203,7 +217,7 @@ template <typename K, typename T> inline PIByteArray & operator <<(PIByteArray &
t.reserve(v.size());
QMapIterator<K, T> it(v);
while (it.hasNext()) {it.next(); t[it.key()] = it.value();}
s >> t;
s << t;
return s;}
template <typename K, typename T> inline PIByteArray & operator >>(PIByteArray & s, QMap<K, T> & v) {
v.clear();

View File

@@ -4,6 +4,7 @@
#include <QEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QScrollBar>
#include <QDebug>
#include <qmath.h>
@@ -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;

View File

@@ -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);