Files
xtree/src/model/xqsectionmanager.cpp

223 lines
5.3 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)
: _modelIndex{ modelIndex }, _sectionRootNode{ sheetNode }
{
}
//! elementvergleich.
bool XQModelSection::operator==(const XQModelSection& other) const
{
return _modelIndex == other._modelIndex && _sectionRootNode == other._sectionRootNode;
}
//! true wenn der start-index valide und ein model-knoten vorhanden.
bool XQModelSection::isValid() const
{
return _modelIndex.isValid() && _sectionRootNode;
}
2025-08-26 17:45:06 +02:00
QModelIndex XQModelSection::persistentModelIndex() 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
{
return _sectionRootNode;
}
//! 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
{
return _sectionRootNode->find_child_by_tag_name( c_ModelSheet );
}
//! 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.
int XQModelSection::XQModelSection::row() const
{
return _modelIndex.row();
}
//! gibt den 'content type' zurück.
const QString& XQModelSection::contentType() const
{
return _sectionRootNode->attribute( c_ContentType );
}
//! 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 11:49:36 +02:00
const XQModelSection& XQSectionManager::sectionByKey( const QString& sectionKey )
{
if( hasValidSection( sectionKey ) )
return _sections.at(sectionKey);
static const XQModelSection s_DummySection;
return s_DummySection;
}
2025-08-22 22:57:06 +02:00
//! gibt für einen model index die 'zuständige' section zurück.
2025-09-05 11:49:36 +02:00
const XQModelSection& XQSectionManager::sectionByIndex( const QModelIndex& index ) const
2025-08-22 22:57:06 +02:00
{
2025-09-05 11:49:36 +02:00
return sectionByRow( index.row() );
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-05 11:49:36 +02:00
int i = _sections.size() - 1;
2025-08-22 22:57:06 +02:00
for (; i >= 0; --i)
{
2025-09-05 11:49:36 +02:00
if ( _sections.at(i).persistentModelIndex().row() < itemRow )
return _sections.at(i);
2025-08-22 22:57:06 +02:00
}
static XQModelSection s_DummySection;
return s_DummySection;
}
2025-09-05 11:49:36 +02:00
const XQModelSection& XQSectionManager::createSection(const QString& sectionKey, const QModelIndex& modelIndex, XQNodePtr sheetNode)
{
// 6. jetzt können wir auch die sction erzeugen
XQModelSection section(modelIndex, sheetNode );
_sections.addAtKey( sectionKey, section);
return sectionByKey(sectionKey);
}
2025-08-22 22:57:06 +02:00
//! ermittelt die erste zeile einer section.
2025-09-05 11:49:36 +02:00
int XQSectionManager::firstRow(const QModelIndex& idx) const
2025-08-22 22:57:06 +02:00
{
2025-09-05 11:49:36 +02:00
return sectionByRow(idx.row() ).row();
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 QModelIndex& idx) const
2025-08-22 22:57:06 +02:00
{
2025-09-05 11:49:36 +02:00
return lastRow(sectionByRow(idx.row()));
2025-08-22 22:57:06 +02:00
}
//! ermittelt die zeile unterhalb der gegebenen section,
//! 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
{
//qDebug() << " -- last row in section: " << section.modelIndex.data().toString() << " --> " << section.modelIndex.row();
// 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-08-26 17:45:06 +02:00
return section.persistentModelIndex().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-05 11:49:36 +02:00
return _sections.at(index+1).row();
2025-08-22 22:57:06 +02:00
}
return -1;
}
//! 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 11:49:36 +02:00
QModelIndex idx = _sections.at(i).persistentModelIndex();
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
}
}