From 5d2fb1b3783b333a66182a2aff66e849006af799 Mon Sep 17 00:00:00 2001 From: "PANIK\\chris" Date: Thu, 4 Sep 2025 17:01:01 +0200 Subject: [PATCH] Added new-undo == delete --- src/items/xqitemdelegate.cpp | 87 ++++++++++++++++++++++------------- src/items/xqitemdelegate.h | 20 ++++---- src/model/xqviewmodel.cpp | 65 ++++++++++++++------------ src/model/xqviewmodel.h | 2 +- src/nodes/znode.h | 20 +++++--- src/nodes/znode_factory.h | 2 +- src/widgets/xqquickwidget.cpp | 2 +- src/widgets/xqquickwidget.h | 2 +- 8 files changed, 116 insertions(+), 84 deletions(-) diff --git a/src/items/xqitemdelegate.cpp b/src/items/xqitemdelegate.cpp index ceae9fe..00925e9 100644 --- a/src/items/xqitemdelegate.cpp +++ b/src/items/xqitemdelegate.cpp @@ -28,6 +28,8 @@ #include +//! firz + class XQItemEditorFactory : public QItemEditorFactory { public: @@ -60,6 +62,7 @@ public: }; +//! firz XQItemDelegate::XQItemDelegate( XQViewModel& modelView) : _modelView{modelView} @@ -69,6 +72,8 @@ XQItemDelegate::XQItemDelegate( XQViewModel& modelView) } +//! firz + XQTreeTable* XQItemDelegate::treeTable() const { return _modelView.treeTable(); @@ -81,6 +86,8 @@ XQItem& XQItemDelegate::xqItemFromIndex( const QModelIndex& index ) const } +//! firz + void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { if( !index.isValid() ) @@ -108,12 +115,11 @@ void XQItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option break; } // switch - - QStyledItemDelegate::paint(painter, option, index); } + //! einen section header im header-style zeichnen void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const @@ -149,7 +155,10 @@ void XQItemDelegate::drawHeaderStyle(QPainter* painter, const QStyleOptionViewIt } } -void XQItemDelegate::drawProgressBarStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const + +//! firz + +void XQItemDelegate::drawProgressBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { int progress = index.data(XQItem::ContentRole ).toInt(); @@ -167,7 +176,10 @@ void XQItemDelegate::drawProgressBarStyle(QPainter *painter, const QStyleOptionV } -void XQItemDelegate::drawComboBoxStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const + +//! firz + +void XQItemDelegate::drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QWidget* srcWidget = qobject_cast(option.styleObject); @@ -196,6 +208,8 @@ void XQItemDelegate::drawComboBoxStyle(QPainter *painter, const QStyleOptionView painter->restore(); } +//! firz + void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { @@ -213,7 +227,9 @@ void XQItemDelegate::drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewI } -QSize XQItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +//! firz + +QSize XQItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { return QStyledItemDelegate::sizeHint(option, index); } @@ -238,53 +254,55 @@ QWidget* XQItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewIte } -//! +//! Füttert einen editor mit den model-daten + void XQItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { XQItem& item = xqItemFromIndex( index ); XQItem::EditorType edType = item.editorType(); - if( edType != XQItem::NoEditorType ) + if( edType == XQItem::NoEditorType ) + return; + + switch( edType ) { - switch( edType ) + case XQItemType::ComboBoxType : + { + QComboBox* comboBox = qobject_cast(editor); + comboBox->setModel( item.fixedChoices()); + comboBox->setCurrentText( item.data().toString() ); + comboBox->showPopup(); + return; + } + + default: + + // wir benutzen hier die DisplayRole wenn der Inhalt schon formatiert ist. + int role = item.renderStyle() == XQItem::FormattedStyle ? Qt::DisplayRole : Qt::EditRole; + QVariant value = index.data(role); + + QByteArray userProp = editor->metaObject()->userProperty().name(); + if (!userProp.isEmpty()) { - case XQItemType::ComboBoxType : - { - QComboBox* comboBox = qobject_cast(editor); - comboBox->setModel( item.fixedChoices()); - comboBox->setCurrentText( item.data().toString() ); - comboBox->showPopup(); - return; - } - - default: - //QStyledItemDelegate::setEditorData(editor, index); - // wir benutzen hier die DisplayRole wenn der Inhalt schon formatiert ist. - int role = item.renderStyle() == XQItem::FormattedStyle ? Qt::DisplayRole : Qt::EditRole; - QVariant value = index.data(role); - - QByteArray userProp = editor->metaObject()->userProperty().name(); - if (!userProp.isEmpty()) - { - if (!value.isValid()) - value = QVariant(editor->property(userProp).metaType()); - editor->setProperty(userProp, value); - } - + if (!value.isValid()) + value = QVariant(editor->property(userProp).metaType()); + editor->setProperty(userProp, value); } } } -void XQItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const + +//! Schreibt die daten aus dem editor ins model zurück + +void XQItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { XQItem& item = xqItemFromIndex( index ); switch( item.editorType() ) { - case XQItem::ComboBoxType : { QComboBox* comboBox = qobject_cast(editor); @@ -299,6 +317,9 @@ void XQItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, co QStyledItemDelegate::setModelData(editor, model, index); } + +//! firz + void XQItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { //qDebug() << " --- update Editor Geometry"; diff --git a/src/items/xqitemdelegate.h b/src/items/xqitemdelegate.h index 63b9ecf..05b29a5 100644 --- a/src/items/xqitemdelegate.h +++ b/src/items/xqitemdelegate.h @@ -37,19 +37,19 @@ public: XQTreeTable* treeTable() const; XQItem& xqItemFromIndex( const QModelIndex& index ) const; - void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; - QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; - QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; - void setEditorData(QWidget *editor, const QModelIndex &index) const override; - void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; - void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override; + QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + void setEditorData(QWidget* editor, const QModelIndex& index) const override; + void setModelData(QWidget* editor, QAbstractItemModel *model, const QModelIndex& index) const override; + void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override; protected: - void drawHeaderStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; - void drawProgressBarStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; - void drawComboBoxStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; - void drawSpinBoxStyle(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + void drawHeaderStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + void drawProgressBarStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + void drawComboBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + void drawSpinBoxStyle(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; XQViewModel& _modelView; diff --git a/src/model/xqviewmodel.cpp b/src/model/xqviewmodel.cpp index 4c94393..0eaf106 100644 --- a/src/model/xqviewmodel.cpp +++ b/src/model/xqviewmodel.cpp @@ -180,6 +180,28 @@ void XQViewModel::onToggleSection(const QString& sectionKey ) qDebug() << " --- onToggleSection: " << sectionKey; } + +void XQViewModel::toggleSection( const XQModelSection& section ) +{ + if(_treeTable) + { + const QModelIndex& index = section.persistentModelIndex(); + qDebug() << " ---- toggle section: FIRZ: " << index.isValid() << " : " << index.data().toString() << " : " << section.contentType();//_sections.keyOf( sec ); + int fstRow = _sections.firstRow( index ); + int lstRow = _sections.lastRow( index ); + _treeTable->toggleRowsHidden(fstRow, lstRow ); + emit sectionToggled( section ); + } + + emit sectionToggled( section ); + +} + +void XQViewModel::toggleSection( const QString& sectionKey ) +{ + +} + /* //! SLOT als weiterleitung vom SIGNAL itemchanged @@ -447,50 +469,33 @@ void XQViewModel::cmdNew( const XQCommand& command ) qDebug() << " --- node own pos: " << node->own_pos(); // we create a new data node - XQNodePtr newNode = XQNode::make_node( node->tag_name(), node->tag_value(), node->parent() ); + XQNodePtr newNode = XQNode::make_node( node->tag_name(), node->tag_value() ); // store node in node->parent() newNode->add_me_at( node->own_pos(), node->parent() ); - // store node also in 'command' to enable undo + + + //... const XQModelSection& section = _sections.sectionFromIndex( origin ); - // create new item row + // neue, leere zeile erzeugen ... XQItemList list =_itemFactory.makeRow( section.sheetRootNode(), newNode ); - - // add it to the treeview ... + // ... zur treeview hinzufügen ... insertRow( origin.row(), list ); - - // ... and make it ... - treeTable()->setCurrentIndex( list[0]->index() ); - // ... editable - treeTable()->edit( list[0]->index() ); + // ... editierbar machen ... + QModelIndex newIndex = list[0]->index(); + treeTable()->setCurrentIndex( newIndex ); + treeTable()->edit( newIndex ); + // ,,, und fürs undo speichern + const_cast(command).saveNodes( {newIndex} ); } -void XQViewModel::toggleSection( const XQModelSection& section ) -{ - if(_treeTable) - { - const QModelIndex& index = section.persistentModelIndex(); - qDebug() << " ---- toggle section: FIRZ: " << index.isValid() << " : " << index.data().toString() << " : " << section.contentType();//_sections.keyOf( sec ); - int fstRow = _sections.firstRow( index ); - int lstRow = _sections.lastRow( index ); - _treeTable->toggleRowsHidden(fstRow, lstRow ); - emit sectionToggled( section ); - } - - emit sectionToggled( section ); - -} - -void XQViewModel::toggleSection( const QString& sectionKey ) -{ - -} //! entfernt die neu angelegte zeile. void XQViewModel::cmdNewUndo( const XQCommand& command ) { + cmdDelete( command ); } diff --git a/src/model/xqviewmodel.h b/src/model/xqviewmodel.h index 0c1d384..858a997 100644 --- a/src/model/xqviewmodel.h +++ b/src/model/xqviewmodel.h @@ -84,7 +84,7 @@ public: Derzeit wird die default-implementierung von data/setData genutzt. hier wäre dann die Stelle um setData & data an externe 'handler' umzubiegen, siehe giovannies 'model-injection' - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override { return QStandardItemModel::data( index, role ); } diff --git a/src/nodes/znode.h b/src/nodes/znode.h index 3ed2d68..7aeb6d0 100644 --- a/src/nodes/znode.h +++ b/src/nodes/znode.h @@ -78,6 +78,7 @@ namespace znode zweak_node _parent; znode_list _children; + // functor, der auf pointer gleichheit prüft. struct match_node { match_node( zbasic_node* match ) @@ -95,9 +96,11 @@ namespace znode public: //! shortcut auf std::make_shared... - static zshared_node make_node( str_cref arg1, str_cref arg2 = "" , zshared_cref parent = nullptr ) + //! beachte: der eltern-knoten wird hier nicht gesetzt, der neue knoten + //! wird nirgends eingefügt. + static zshared_node make_node( str_cref arg1, str_cref arg2 = "" ) { - return std::make_shared( arg1, arg2, parent ); + return std::make_shared( arg1, arg2 ); } //! leerer konstruktor @@ -136,7 +139,7 @@ namespace znode zbasic_node(const zbasic_node&) = delete; zbasic_node& operator=(const zbasic_node&) = delete; - // 'move' geht (shared_from_this bleibt gültig) + //! 'move' geht (shared_from_this bleibt gültig) zbasic_node(zbasic_node&&) noexcept = default; zbasic_node& operator=(zbasic_node&&) noexcept = default; @@ -178,29 +181,31 @@ namespace znode return _parent.lock(); } + //! gibt den nachfolge-knoten oder 'end()' zurück. zshared_node sibling() { if( parent() ) { znode_list& childs = _parent->_children; auto it = std::find( childs.begin(), childs.end(), this->shared_from_this() ); - if( ++it != childs.end()) - return *(it); + return *(it); } - - return zshared_node(); + throw std::runtime_error("sibling(): no parent node"); } + //! gibt den vector mit kind-knoten zurück inline const znode_list& children() const { return _children; } + //! testet, ob kinder vorhanden sind. bool has_children() const { return !children().empty(); } + //! gibt das erste kind zurück zshared_node first_child() { if(!children().empty()) @@ -208,6 +213,7 @@ namespace znode return nullptr; } + //! gibt das letzte kind oder nullptr zurück zshared_node last_child() { if(!children().empty()) diff --git a/src/nodes/znode_factory.h b/src/nodes/znode_factory.h index 9c1cb2f..6640b05 100644 --- a/src/nodes/znode_factory.h +++ b/src/nodes/znode_factory.h @@ -100,7 +100,7 @@ namespace znode //parent->add_child( new_node ); //zbasic_node* new_node = new zbasic_node( node.name(), node.child_value(), parent ); - zshared_node new_node = zbasic_node::make_node( xml_node.name(), xml_node.child_value(), parent ); + zshared_node new_node = zbasic_node::make_node( xml_node.name(), xml_node.child_value() ); parent->add_child( new_node ); if( !xml_node.attributes().empty() ) diff --git a/src/widgets/xqquickwidget.cpp b/src/widgets/xqquickwidget.cpp index 482dd1d..2d1ff8e 100644 --- a/src/widgets/xqquickwidget.cpp +++ b/src/widgets/xqquickwidget.cpp @@ -14,7 +14,7 @@ #include -XQQuickWidget::XQQuickWidget(QWidget *parent) +XQQuickWidget::XQQuickWidget(QWidget* parent) : QQuickWidget(parent) { diff --git a/src/widgets/xqquickwidget.h b/src/widgets/xqquickwidget.h index 2232014..1c42213 100644 --- a/src/widgets/xqquickwidget.h +++ b/src/widgets/xqquickwidget.h @@ -23,7 +23,7 @@ class XQQuickWidget : public QQuickWidget public: - XQQuickWidget(QWidget *parent = nullptr); + XQQuickWidget(QWidget* parent = nullptr); }; #endif // XQQUICKWIDGET_H