Files
xtree.ng.zwo/application/xqchildmodel.cpp
2025-08-13 18:30:47 +02:00

156 lines
4.7 KiB
C++

/***************************************************************************
source::worx xtree
Copyright © 2024-2025 c.holzheuer
christoph.holzheuer@gmail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
***************************************************************************/
#include <znode_factory.h>
#include <xqchildmodel.h>
#include <xqselectionmodel.h>
#include <xqitemdelegate.h>
#include <xqappdata.h>
#include <xqtreeview.h>
#include <xqitemfactory.h>
XQChildModel::XQChildModel( QObject *parent )
: XQModel{parent}
{
}
XQChildModel::~XQChildModel()
{
}
/**
* @brief XQChildModel::initModel: Create the own modelstructure
* using subtree 'Components'
*/
void XQChildModel::initModel(const QString& modelName)
{
// #0: Wir suchen die Model-Beschreibung
XQNodePtr modelSheet = _itemFactory.findModelSheet( modelName ); // throws
// #1: Wie erzeugen die Model-Struktur: Jedes Kind beschreibt einen
// XML-Datentyp, z.B. <Panel atr1="..." />, <Battery .../>
// Jeder XML-Knoten entspricht einer Zeile im späteren Model, jedes
// Attribut wird einem eigenen Feld (XQItem) abgebildet.
for( auto& sheetNode : modelSheet->children() )
{
XQItemList list;
// Die Kinder des Knotens beschreiben die einzelnen
// Attribute des XML-Datenknotens
for( const auto& attrNode : sheetNode->children() )
{
// ??
//if(attrNode->has_children() )
// continue;
XQItem* headerItem = _itemFactory.makeHeaderItem( attrNode );
list.append( headerItem );
}
// für jeden XML-Knotentyp erzeugen wir eine section
addSection(list, sheetNode );
/*
// empty row
XQNodePtr contentNode = XQNode::make_node( sheetNode->tag_name() );
XQItemList emptyRow = _itemFactory.createEmptyRow( contentNode, sheetNode );
appendRow( emptyRow );
*/
} // for
}
void XQChildModel::setContent( const XQNodePtr& contentRoot )
{
// __fix: set object name ??
qDebug() << " --- create Model Data: " << contentRoot->to_string();
// Die Datenbasis als shared_ptr sichern
_contentRoot = contentRoot;
// Wir gehen über alle Einträge, die verschiedenen Typen
// haben, hier: <Panel>. <Battery> ...
for (const auto& contentEntry : _contentRoot->children())
{
// Das ist hier der Typ des Eintrags: Panel, Battery ...
QString key = contentEntry->tag_name();
// 'silent failure' hier der Datenbaum kann auch Knoten enthalten
// die nicht für uns gedacht sind.
if (!_sections.hasValidSection(key))
continue;
const XQModelSection& section = _sections.at( key );
int newRow = _sections.lastRow(section);
qDebug() << " --- AHJA: " << key << " -- last Row dazu: " << newRow;
XQItemList list = _itemFactory.createDataRow( contentEntry, section.sheetRootNode );
//section.headerItem().appendRow( list );
insertRow( newRow, list);
} // for
}
void XQChildModel::createModelContentChildRow( QStandardItem* parent, XQNodePtr contentNode )
{
/*
parent->appendRow( { new XQItem("Optionals", XQItemType::HeaderStyle ), new XQItem( "Value", XQItemType::HeaderStyle )} );
for( auto& child : contentNode->children() )
{
XQItemList list;
list.append( new XQItem( child->attribute("DataItem"), XQItemType::StaticStyle ) );
list.append( new XQItem( child->attribute("DataValue"), XQItemType::StaticStyle ) );
parent->appendRow( list );
}
*/
}
void XQChildModel::initContextMenu()
{
// __fixme! add a menu title
_contextMenu->clear();
const QModelIndex& curIdx = _treeView->currentIndex();
bool hasSel = curIdx.isValid() && _treeView->selectionModel()->hasSelection();
bool canPaste = _clipBoard.canPaste( curIdx );
_contextMenu->addAction( "icn11Dummy", "Undo", XQCommand::cmdUndo, _undoStack->canUndo() );
_contextMenu->addAction( "icn17Dummy", "Redo", XQCommand::cmdRedo, _undoStack->canRedo() );
_contextMenu->addAction( "icn58Dummy", "Cut", XQCommand::cmdCut, hasSel );
_contextMenu->addAction( "icn61Dummy", "Paste", XQCommand::cmdPaste, canPaste );
_contextMenu->addAction( "icn55Dummy", "Copy", XQCommand::cmdCopy, hasSel );
//_contextMenu->addAction( "icn35Dummy", "Move", XQCommand::cmdMove, hasSel );
_contextMenu->addAction( "icn70Dummy", "New", XQCommand::cmdNew, hasSel );
_contextMenu->addAction( "icn50Dummy", "Delete", XQCommand::cmdDelete, hasSel );
// __fixme! set 'toggle section <name>' entry
//contextMenu.actions().first()->setText("<name>");
_contextMenu->addAction( "icn29Dummy", "Toggle Section", XQCommand::cmdToggleSection, hasSel);
}