Files
xtree/src/model/xqsectionmanager.cpp

208 lines
5.2 KiB
C++
Raw Normal View History

2025-08-22 22:57:06 +02:00
/***************************************************************************
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.
***************************************************************************/
2025-09-05 11:49:36 +02:00
#include <xqsectionmanager.h>
2025-08-22 22:57:06 +02:00
//! kontstruktor. übergibt den start-index und einen model-knoten mit der beschreibung
//! der datenknoten.
XQModelSection::XQModelSection(const QModelIndex& modelIndex, XQNodePtr sheetNode)
2025-09-06 14:07:25 +02:00
: _modelIndex{ modelIndex }, _sectionSheetRootNode{ sheetNode }
2025-08-22 22:57:06 +02:00
{
}
//! elementvergleich.
bool XQModelSection::operator==(const XQModelSection& other) const
{
2025-09-06 14:07:25 +02:00
return _modelIndex == other._modelIndex && _sectionSheetRootNode == other._sectionSheetRootNode;
2025-08-22 22:57:06 +02:00
}
//! true wenn der start-index valide und ein model-knoten vorhanden.
bool XQModelSection::isValid() const
{
2025-09-06 14:07:25 +02:00
return _modelIndex.isValid() && _sectionSheetRootNode;
2025-08-22 22:57:06 +02:00
}
2025-09-05 21:42:40 +02:00
QModelIndex XQModelSection::startIndex() const
2025-08-22 22:57:06 +02:00
{
2025-08-26 17:45:06 +02:00
return _modelIndex.operator QModelIndex();
2025-08-22 22:57:06 +02:00
}
XQNodePtr XQModelSection::sectionRootNode() const
{
2025-09-06 14:07:25 +02:00
return _sectionSheetRootNode;
2025-08-22 22:57:06 +02:00
}
//! Gibt den sheet-node zurück, das ist die model-beschreibung,
//! siehe modelsheet.xml:
//! <section>
//! <header>
//! <data> <- dort
//! __fix! das versteht doch kein mensch!
XQNodePtr XQModelSection::sheetRootNode() const
{
2025-09-06 14:07:25 +02:00
return _sectionSheetRootNode->find_child_by_tag_name( c_ModelSheet );
2025-08-22 22:57:06 +02:00
}
//! Gibt den content root node zurück, das ist der
//! zeiger auf die realen inhalte.
XQNodePtr XQModelSection::contentRootNode() const
{
return _contentRootNode;
}
2025-09-05 11:49:36 +02:00
void XQModelSection::setContentRootNode( const XQNodePtr contentRootNode ) const
2025-08-22 22:57:06 +02:00
{
_contentRootNode = contentRootNode;
}
//! gibt die zeile des start-index zurück.
2025-09-05 21:42:40 +02:00
int XQModelSection::XQModelSection::firstRow() const
2025-08-22 22:57:06 +02:00
{
return _modelIndex.row();
}
//! gibt den 'content type' zurück.
const QString& XQModelSection::contentType() const
{
2025-09-06 14:07:25 +02:00
//qDebug() << " ---AUA & AUS!";
return _sectionSheetRootNode->attribute( c_ContentType );
2025-08-22 22:57:06 +02:00
}
//! gibt das dieser section entsprechende header-item zurück.
XQItem& XQModelSection::XQModelSection::headerItem() const
{
return XQItem::xqItemFromIndex( _modelIndex );
}
//! testet, ob die unter 'sectionKey' eine gültige section vorhanden ist.
2025-09-05 11:49:36 +02:00
bool XQSectionManager::hasValidSection(const QString& sectionKey) const
2025-08-22 22:57:06 +02:00
{
2025-09-05 11:49:36 +02:00
if (!_sections.contains(sectionKey) )
2025-08-22 22:57:06 +02:00
return false;
2025-09-05 11:49:36 +02:00
return _sections.at(sectionKey).isValid();
2025-08-22 22:57:06 +02:00
}
2025-09-05 21:42:40 +02:00
const XQModelSection& XQSectionManager::sectionByKey( const QString& sectionKey ) const
2025-09-05 11:49:36 +02:00
{
if( hasValidSection( sectionKey ) )
return _sections.at(sectionKey);
2025-09-16 16:45:21 +02:00
throw XQException( "No section for key", sectionKey);
2025-09-05 11:49:36 +02:00
}
2025-08-22 22:57:06 +02:00
//! gibt für eine zeile die 'zuständige' section zurück: der bestand an section wird
//! nach der passenden section durchsucht.
2025-09-05 11:49:36 +02:00
const XQModelSection& XQSectionManager::sectionByRow(int itemRow ) const
2025-08-22 22:57:06 +02:00
{
2025-09-06 14:07:25 +02:00
2025-09-09 16:22:59 +02:00
for (const auto& section : _sections)
2025-08-22 22:57:06 +02:00
{
2025-09-09 16:22:59 +02:00
qDebug() << " ---- SEC: " << itemRow << " -> " << section.firstRow() << " : " << lastRow( section );
XQSectionRange range = sectionRange(section);
if( itemRow >= range.firstRow && itemRow <= range.lastRow)
return section;
2025-08-22 22:57:06 +02:00
}
2025-09-16 16:45:21 +02:00
throw XQException( "No section for item row", QString::number(itemRow));
2025-08-22 22:57:06 +02:00
}
2025-09-06 22:13:46 +02:00
2025-09-06 14:07:25 +02:00
const XQModelSection& XQSectionManager::createSection(const QModelIndex& modelIndex, XQNodePtr sheetNode)
2025-09-05 11:49:36 +02:00
{
2025-09-06 14:07:25 +02:00
const QString& sectionKey = sheetNode->attribute(c_ContentType);
2025-09-06 16:06:47 +02:00
//qDebug() << " --- create Section: " << sectionKey << ": " << modelIndex.data().toString();
2025-09-05 11:49:36 +02:00
XQModelSection section(modelIndex, sheetNode );
_sections.addAtKey( sectionKey, section);
return sectionByKey(sectionKey);
}
2025-08-22 22:57:06 +02:00
//! ermittelt die zeile unterhalb des gegebenen modelindex,
//! zum einfügen neuer items ebendort.
2025-09-05 11:49:36 +02:00
int XQSectionManager::lastRow(const XQModelSection& section ) const
2025-08-22 22:57:06 +02:00
{
2025-09-06 14:07:25 +02:00
//qDebug() << " -- last row in section: " << section.startIndex().data().toString() << " --> " << section.startIndex().row();
2025-08-22 22:57:06 +02:00
// row() der section unterhalb dieser
// __fix? index mit speichern?
2025-09-05 11:49:36 +02:00
int index = _sections.indexOf(section);
2025-08-22 22:57:06 +02:00
if (index > -1)
{
// last section? return last row of model
2025-09-05 11:49:36 +02:00
if (index == _sections.size() - 1)
2025-09-09 16:22:59 +02:00
return section.startIndex().model()->rowCount() - 1;
2025-08-22 22:57:06 +02:00
// return row above the row of the next section -> last row of given section
2025-09-09 16:22:59 +02:00
return _sections.at(index+1).firstRow() - 1;
2025-08-22 22:57:06 +02:00
}
return -1;
}
2025-09-05 17:12:38 +02:00
2025-09-05 21:42:40 +02:00
XQSectionRange XQSectionManager::sectionRange(const XQModelSection& section ) const
2025-09-05 17:12:38 +02:00
{
2025-09-05 21:42:40 +02:00
return XQSectionRange{ section.startIndex().row(), lastRow(section) };
2025-09-05 17:12:38 +02:00
}
2025-08-22 22:57:06 +02:00
//! gibt alle sections aus, zum ankucken.
2025-09-05 11:49:36 +02:00
void XQSectionManager::dump() const
2025-08-22 22:57:06 +02:00
{
2025-09-05 11:49:36 +02:00
qDebug() << " --- sections dump(): " <<_sections.size() << " entries.";
for( int i = 0; i<_sections.size(); ++i )
2025-08-22 22:57:06 +02:00
{
2025-09-05 21:42:40 +02:00
QModelIndex idx = _sections.at(i).startIndex();
2025-09-05 11:49:36 +02:00
qDebug() << " --- sections:" << i << "row: " << idx.row() << " keyOf(i): " << _sections.keyOf(i) << " indexData: "<< idx.data().toString() << " itemData: " << XQItem::xqItemFromIndex(idx).data(Qt::DisplayRole).toString();
2025-08-22 22:57:06 +02:00
}
}