From c3dcf36a20fc90a0da226529a74e218b5be4c681 Mon Sep 17 00:00:00 2001 From: peri4 Date: Sun, 14 Jan 2024 11:45:28 +0300 Subject: [PATCH] version 2.22.0 blockview small code clean add PIValueTree to BLockItem, BlockItemPin and BlockBusItem add QAD::CursorOverrider::restore() fix QCodeEdit escape key while block selection --- CMakeLists.txt | 4 +- libs/blockview/CMakeLists.txt | 2 +- libs/blockview/blockbusitem.cpp | 7 +++- libs/blockview/blockbusitem.h | 3 ++ libs/blockview/blockitem.cpp | 72 ++++++++++++++++++++++++++++----- libs/blockview/blockitem.h | 39 +++++------------- libs/blockview/blockitempin.h | 5 +++ libs/utils/qad_types.cpp | 25 ++++++++++++ libs/utils/qad_types.h | 17 +++++++- libs/widgets/qcodeedit.cpp | 9 ++++- 10 files changed, 135 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aa1cd8b..f80e48a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake cmake_policy(SET CMP0072 NEW) # FindOpenGL prefers GLVND by default project(QAD) set(QAD_MAJOR 2) -set(QAD_MINOR 21) -set(QAD_REVISION 1) +set(QAD_MINOR 22) +set(QAD_REVISION 0) set(QAD_SUFFIX ) set(QAD_COMPANY SHS) set(QAD_DOMAIN org.SHS) diff --git a/libs/blockview/CMakeLists.txt b/libs/blockview/CMakeLists.txt index 7b9a4b4..ab11cf1 100644 --- a/libs/blockview/CMakeLists.txt +++ b/libs/blockview/CMakeLists.txt @@ -1 +1 @@ -qad_library(blockview "Gui;Widgets" "qad_widgets") +qad_library(blockview "Gui;Widgets" "qad_widgets;qad_piqt") diff --git a/libs/blockview/blockbusitem.cpp b/libs/blockview/blockbusitem.cpp index a22e413..1c0de4d 100644 --- a/libs/blockview/blockbusitem.cpp +++ b/libs/blockview/blockbusitem.cpp @@ -1,6 +1,7 @@ #include "blockview.h" #include +#include BlockBusItem::BlockBusItem(bool temp): QGraphicsObject(), PropertyStorage() { @@ -369,8 +370,9 @@ void BlockBusItem::clearBusState() { QByteArray BlockBusItem::save() const { ChunkStream cs; - cs << cs.chunk(1, busType()) << cs.chunk(2, busName()) << cs.chunk(3, width()) << cs.chunk(4, pen()) << cs.chunk(5, brush()) - << cs.chunk(6, pol) << cs.chunk(7, segments) << cs.chunk(8, props) << cs.chunk(9, im_bus_scale) << cs.chunk(10, im_end_scale); + cs.add(1, busType()).add(2, busName()).add(3, width()).add(4, pen()).add(5, brush()); + cs.add(6, pol).add(7, segments).add(8, props).add(9, im_bus_scale).add(10, im_end_scale); + cs.add(11, piqSerialize(value_tree)); return cs.data(); } @@ -391,6 +393,7 @@ void BlockBusItem::load(const QByteArray & data) { case 8: props = cs.getData>(); break; case 9: im_bus_scale = cs.getData(); break; case 10: im_end_scale = cs.getData(); break; + case 11: value_tree = piqDeserialize(cs.getData()); break; } } updateGeometry(); diff --git a/libs/blockview/blockbusitem.h b/libs/blockview/blockbusitem.h index ac781a0..f6cc74a 100644 --- a/libs/blockview/blockbusitem.h +++ b/libs/blockview/blockbusitem.h @@ -110,6 +110,8 @@ public: void saveState(); void restoreState(); + PIValueTree & values() { return value_tree; } + enum { Type = UserType + 2 }; @@ -151,6 +153,7 @@ protected: QImage im_bus, im_end; QPolygonF pol, bpol, pol_s; Qt::KeyboardModifiers mm_mods; + PIValueTree value_tree; bool temp_, square_node; double pen_width, grid_step, im_bus_scale, im_end_scale; int selPoint, selSegment, max_ep, bus_type, state_; diff --git a/libs/blockview/blockitem.cpp b/libs/blockview/blockitem.cpp index 4b9f7b6..79411c6 100644 --- a/libs/blockview/blockitem.cpp +++ b/libs/blockview/blockitem.cpp @@ -1,6 +1,7 @@ #include "blockview.h" #include +#include #define BLOCKITEM_DEFAULT_PIN_MARGIN 20 @@ -59,7 +60,7 @@ void BlockItem::_resize(QSizeF s) { void BlockItem::_moveToTop(bool only_decors) { qreal dy = -g_main.rect().center().y() + 10; if (!only_decors) moveBy(0., dy); - foreach(QGraphicsItem * d, decors_) + for (QGraphicsItem * d: decors_) d->moveBy(0., -dy); } @@ -153,7 +154,7 @@ void BlockItem::clearDecors() { bool pbs = false; if (scene()) pbs = scene()->blockSignals(true); if (scene()) { - foreach(QGraphicsItem * i, decors_) + for (QGraphicsItem * i: decors_) scene()->sendEvent(i, new QGraphicsSceneEvent(QEvent::Close)); } qDeleteAll(decors_); @@ -217,12 +218,12 @@ void BlockItem::loadModel(const QByteArray & data) { case 4: setColor(cs.getData()); break; case 5: cs.get(tp); - foreach(BlockItemPin * p, tp) + for (BlockItemPin * p: tp) addPin(p); break; case 6: cs.get(dl); - foreach(QGraphicsItem * d, dl) + for (QGraphicsItem * d: dl) addDecor(d); break; case 7: setPinsMargin(cs.getData()); break; @@ -240,12 +241,13 @@ void BlockItem::loadModel(const QByteArray & data) { QByteArray BlockItem::save() const { ChunkStream cs; QMap> pp; - foreach(BlockItemPin * p, pins()) { + for (BlockItemPin * p: pins()) { // qDebug() << "save pin" << p->text() << "->" << p->properties().size(); pp[p->text()] = p->properties(); } cs.add(1, pos()).add(2, rotation()).add(3, props).add(5, pp).add(6, size()); cs.add(10, data(2000)).add(11, data(2001)).add(12, prop_bindings).add(0xFF, _blockitem_current_version_); + cs.add(13, piqSerialize(value_tree)); return cs.data(); } @@ -254,6 +256,7 @@ void BlockItem::load(const QByteArray & data) { if (data.isEmpty()) return; ChunkStream cs(data); QMap> _p; + value_tree = {}; int version = -1; while (!cs.atEnd()) { switch (cs.read()) { @@ -263,7 +266,7 @@ void BlockItem::load(const QByteArray & data) { case 5: cs.get(_p); // qDebug() << "load pins" << _p.size(); - foreach(BlockItemPin * p, pins()) { + for (BlockItemPin * p: pins()) { // qDebug() << "load pin" << p->text() << "->" << _p.contains(p->text()); if (_p.contains(p->text())) p->properties() = _p[p->text()]; } @@ -272,6 +275,7 @@ void BlockItem::load(const QByteArray & data) { case 10: setData(2000, cs.getData()); break; case 11: setData(2001, cs.getData()); break; case 12: prop_bindings = cs.getData>>(); break; + case 13: value_tree = piqDeserialize(cs.getData()); break; case 0xFF: cs.get(version); break; } } @@ -290,7 +294,7 @@ BlockItem * BlockItem::copy() const { ret->props = props; ret->setPinsMargin(pinsMargin()); QVector mp = pins(); - foreach(BlockItemPin * p, mp) { + for (BlockItemPin * p: mp) { BlockItemPin * np = new BlockItemPin(); np->setBusType(p->busType()); np->setAlignment(p->alignment()); @@ -309,14 +313,14 @@ BlockItem * BlockItem::copy() const { QList BlockItem::connectedBuses() const { QList ret; - foreach(BlockItemPin * p, pins()) + for (BlockItemPin * p: pins()) ret << p->connectedBuses(); return ret; } BlockItemPin * BlockItem::pinByText(const QString & t) const { - foreach(BlockItemPin * p, pins()) + for (BlockItemPin * p: pins()) if (p->text() == t) return p; return 0; } @@ -324,17 +328,38 @@ BlockItemPin * BlockItem::pinByText(const QString & t) const { BlockItemPin * BlockItem::pinAtBus(BlockBusItem * bus) const { if (bus == 0) return 0; - foreach(BlockItemPin * p, pins()) + for (BlockItemPin * p: pins()) if (p->connectedBuses().contains(bus)) return p; return 0; } +void BlockItem::setColor(QColor c) { + col = c; + _resize(size()); +} + QRectF BlockItem::sceneRect() const { return g_main.mapRectToScene(g_main.boundingRect()); } +void BlockItem::setWidth(qreal w) { + setSize(QSizeF(w, size().height())); +} + + +void BlockItem::setHeight(qreal h) { + setSize(QSizeF(size().width(), h)); +} + + +void BlockItem::setPinsMargin(int marg) { + if (marg > 1 && marg < 256) pins_margin = marg; + arrangePins(); +} + + QRectF BlockItem::boundingRect() const { return g_main.mapRectToParent(g_main.boundingRect()); } @@ -357,7 +382,7 @@ void BlockItem::hoverLeaveEvent(QGraphicsSceneHoverEvent * e) { void BlockItem::arrangePins() { QVector pl = pins(); pins_.clear(); - foreach(BlockItemPin * p, pl) + for (BlockItemPin * p: pl) pins_[p->alignment()] << p; QVector cp = pins_.value(Qt::AlignBottom); for (int i = 0; i < cp.size(); ++i) @@ -525,3 +550,28 @@ QRectF BlockItem::selectionRect() const { void BlockItem::setSelectionRect(const QRectF & r) { g_selection.setRect(r); } + + +QDataStream & operator<<(QDataStream & s, const BlockItemPin * p) { + ChunkStream cs; + cs.add(1, int(p->alignment())).add(2, p->busType()).add(3, p->text()).add(4, p->toolTip()); + cs.add(5, piqSerialize(p->value_tree)); + s << cs.data(); + return s; +} + + +QDataStream & operator>>(QDataStream & s, BlockItemPin *& p) { + ChunkStream cs(s); + p = new BlockItemPin(); + while (!cs.atEnd()) { + switch (cs.read()) { + case 1: p->setAlignment((Qt::Alignment)cs.getData()); break; + case 2: p->setBusType(cs.getData()); break; + case 3: p->setText(cs.getData()); break; + case 4: p->setToolTip(cs.getData()); break; + case 5: p->value_tree = piqDeserialize(cs.getData()); break; + } + } + return s; +} diff --git a/libs/blockview/blockitem.h b/libs/blockview/blockitem.h index 771e286..207c364 100644 --- a/libs/blockview/blockitem.h +++ b/libs/blockview/blockitem.h @@ -24,6 +24,7 @@ #include "qad_blockview_export.h" #include +#include class QAD_BLOCKVIEW_EXPORT BlockItem @@ -58,10 +59,7 @@ public: BlockItemPin * pinByText(const QString & t) const; BlockItemPin * pinAtBus(BlockBusItem * bus) const; QColor color() const { return col; } - void setColor(QColor c) { - col = c; - _resize(size()); - } + void setColor(QColor c); QSizeF size() const { return g_main.rect().size(); } QRectF sceneRect() const; qreal width() const { return size().width(); } @@ -69,12 +67,9 @@ public: int pinsMargin() const { return pins_margin; } void setSize(QSizeF s) { _resize(s); } void setSize(qreal w, qreal h) { setSize(QSizeF(w, h)); } - void setWidth(qreal w) { setSize(QSizeF(w, size().height())); } - void setHeight(qreal h) { setSize(QSizeF(size().width(), h)); } - void setPinsMargin(int marg) { - if (marg > 1 && marg < 256) pins_margin = marg; - arrangePins(); - } + void setWidth(qreal w); + void setHeight(qreal h); + void setPinsMargin(int marg); QByteArray saveModel(); void loadModel(const QByteArray & data); @@ -93,6 +88,8 @@ public: QStringList getBindNames() const; QStringList getBindProps() const; + PIValueTree & values() { return value_tree; } + enum { Type = UserType + 1 }; @@ -128,6 +125,7 @@ private: QPropertyAnimation anim_thick, anim_sel; QElapsedTimer t_sel; + PIValueTree value_tree; bool is_hovered = false, is_blockeditor = false; signals: @@ -136,25 +134,8 @@ signals: }; -inline QDataStream & operator<<(QDataStream & s, const BlockItemPin * p) { - ChunkStream cs; - cs << cs.chunk(1, int(p->alignment())) << cs.chunk(2, p->busType()) << cs.chunk(3, p->text()) << cs.chunk(4, p->toolTip()); - s << cs.data(); - return s; -} -inline QDataStream & operator>>(QDataStream & s, BlockItemPin *& p) { - ChunkStream cs(s); - p = new BlockItemPin(); - while (!cs.atEnd()) { - switch (cs.read()) { - case 1: p->setAlignment((Qt::Alignment)cs.getData()); break; - case 2: p->setBusType(cs.getData()); break; - case 3: p->setText(cs.getData()); break; - case 4: p->setToolTip(cs.getData()); break; - } - } - return s; -} +QDataStream & operator<<(QDataStream & s, const BlockItemPin * p); +QDataStream & operator>>(QDataStream & s, BlockItemPin *& p); inline QDataStream & operator<<(QDataStream & s, const BlockItem * b) { diff --git a/libs/blockview/blockitempin.h b/libs/blockview/blockitempin.h index d6ad387..ff9ff75 100644 --- a/libs/blockview/blockitempin.h +++ b/libs/blockview/blockitempin.h @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -45,6 +46,8 @@ class QAD_BLOCKVIEW_EXPORT BlockItemPin , public PropertyStorage { friend class BlockView; friend class BlockItem; + friend QDataStream & operator<<(QDataStream & s, const BlockItemPin * p); + friend QDataStream & operator>>(QDataStream & s, BlockItemPin *& p); Q_OBJECT Q_PROPERTY(double pinSize READ pinSize WRITE resizePin DESIGNABLE false SCRIPTABLE false) @@ -92,6 +95,7 @@ public: void enlargePin(bool enlarge); + PIValueTree & values() { return value_tree; } BlockItem * parent() const { return parent_; } QList connectedBuses() const { return buses_; } @@ -119,6 +123,7 @@ protected: BlockItem * parent_; Qt::Alignment align; QBrush br[6]; + PIValueTree value_tree; private slots: void animationAccept(); diff --git a/libs/utils/qad_types.cpp b/libs/utils/qad_types.cpp index f9fde7e..bd83d75 100644 --- a/libs/utils/qad_types.cpp +++ b/libs/utils/qad_types.cpp @@ -56,6 +56,8 @@ __QADTypesRegistrator__::__QADTypesRegistrator__() { __QADTypesRegistrator__ __registrator__(1); +// QAD::Enum + QAD::Enum::Enum(const QMetaEnum & meta, int selected) { enum_name = meta.name(); for (int i = 0; i < meta.keyCount(); ++i) { @@ -146,6 +148,8 @@ QAD::Enum & QAD::Enum::operator<<(const QStringList & v) { } +// QAD::IODevice + QString QAD::IODevice::toString() const { QString s; if (__QADTypesRegistrator__::instance()->toString_funcs.contains(qMetaTypeId())) { @@ -157,6 +161,27 @@ QString QAD::IODevice::toString() const { } +// QAD::CursorOverrider + +QAD::CursorOverrider::CursorOverrider(const QCursor & c) { + QApplication::setOverrideCursor(c); +} + + +QAD::CursorOverrider::~CursorOverrider() { + restore(); +} + + +void QAD::CursorOverrider::restore() { + if (!on) return; + on = false; + QApplication::restoreOverrideCursor(); +} + + +// global + #if QT_VERSION_MAJOR <= 5 QVariant::Type #else diff --git a/libs/utils/qad_types.h b/libs/utils/qad_types.h index 0f95276..69d07c7 100644 --- a/libs/utils/qad_types.h +++ b/libs/utils/qad_types.h @@ -47,12 +47,14 @@ class QMetaEnum; namespace QAD { + struct QAD_UTILS_EXPORT Enumerator { Enumerator(int v = 0, const QString & n = QString()): value(v), name(n) {} int value; QString name; }; + struct QAD_UTILS_EXPORT Enum { Enum(const QString & n = QString()): enum_name(n) {} Enum(const QMetaEnum & meta, int selected = 0); @@ -73,6 +75,7 @@ struct QAD_UTILS_EXPORT Enum { Enum & operator<<(const QStringList & v); }; + struct QAD_UTILS_EXPORT File { File(const QString & p = QString(), const QString & f = QString(), bool abs = false, bool save_mode = false) : file(p) @@ -86,6 +89,7 @@ struct QAD_UTILS_EXPORT File { bool is_save; }; + struct QAD_UTILS_EXPORT Dir { Dir(const QString & d = QString(), bool abs = false): dir(d), is_abs(abs) {} QString toString() const { return dir; } @@ -93,6 +97,7 @@ struct QAD_UTILS_EXPORT Dir { bool is_abs; }; + struct QAD_UTILS_EXPORT IODevice { IODevice(const QString & device_prefix = QString(), const PropertyStorage & device_properties = PropertyStorage(), @@ -110,22 +115,30 @@ struct QAD_UTILS_EXPORT IODevice { PropertyStorage props; }; + struct QAD_UTILS_EXPORT MathVector { MathVector(const QVector & vec = QVector()) { v = vec; } QVector v; }; + struct QAD_UTILS_EXPORT MathMatrix { MathMatrix(const QVector> & mat = QVector>()) { m = mat; } QVector> m; // [Row][Column] }; + class QAD_UTILS_EXPORT CursorOverrider { public: - CursorOverrider(const QCursor & c = Qt::WaitCursor) { QApplication::setOverrideCursor(c); } - ~CursorOverrider() { QApplication::restoreOverrideCursor(); } + CursorOverrider(const QCursor & c = Qt::WaitCursor); + ~CursorOverrider(); + void restore(); + +private: + bool on = true; }; + } // namespace QAD Q_DECLARE_METATYPE(QAD::Enumerator) diff --git a/libs/widgets/qcodeedit.cpp b/libs/widgets/qcodeedit.cpp index 386ae75..f3e0f5c 100644 --- a/libs/widgets/qcodeedit.cpp +++ b/libs/widgets/qcodeedit.cpp @@ -608,8 +608,15 @@ bool QCodeEdit::codeKeyEvent(QKeyEvent * ke) { hideHelp(); if (completer->isVisible()) { completer->hide(); - } else { + return true; + } + if (hasBlockSelection()) { + cancelBlockSelection(); + return true; + } + if (ui->widgetSearch->isVisible()) { hideSearch(); + return true; } break; case Qt::Key_Up: