Enable section toggle
This commit is contained in:
222
src/model/xqsectionmanager.cpp
Normal file
222
src/model/xqsectionmanager.cpp
Normal file
@@ -0,0 +1,222 @@
|
||||
/***************************************************************************
|
||||
|
||||
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 <xqsectionmanager.h>
|
||||
|
||||
|
||||
//! 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;
|
||||
}
|
||||
|
||||
QModelIndex XQModelSection::persistentModelIndex() const
|
||||
{
|
||||
return _modelIndex.operator QModelIndex();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void XQModelSection::setContentRootNode( const XQNodePtr contentRootNode ) const
|
||||
{
|
||||
_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.
|
||||
|
||||
bool XQSectionManager::hasValidSection(const QString& sectionKey) const
|
||||
{
|
||||
if (!_sections.contains(sectionKey) )
|
||||
return false;
|
||||
return _sections.at(sectionKey).isValid();
|
||||
}
|
||||
|
||||
const XQModelSection& XQSectionManager::sectionByKey( const QString& sectionKey )
|
||||
{
|
||||
if( hasValidSection( sectionKey ) )
|
||||
return _sections.at(sectionKey);
|
||||
|
||||
static const XQModelSection s_DummySection;
|
||||
return s_DummySection;
|
||||
|
||||
}
|
||||
|
||||
//! gibt für einen model index die 'zuständige' section zurück.
|
||||
|
||||
const XQModelSection& XQSectionManager::sectionByIndex( const QModelIndex& index ) const
|
||||
{
|
||||
return sectionByRow( index.row() );
|
||||
}
|
||||
|
||||
|
||||
//! gibt für eine zeile die 'zuständige' section zurück: der bestand an section wird
|
||||
//! nach der passenden section durchsucht.
|
||||
|
||||
const XQModelSection& XQSectionManager::sectionByRow(int itemRow ) const
|
||||
{
|
||||
|
||||
int i = _sections.size() - 1;
|
||||
for (; i >= 0; --i)
|
||||
{
|
||||
if ( _sections.at(i).persistentModelIndex().row() < itemRow )
|
||||
return _sections.at(i);
|
||||
}
|
||||
|
||||
static XQModelSection s_DummySection;
|
||||
|
||||
return s_DummySection;
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
//! ermittelt die erste zeile einer section.
|
||||
|
||||
int XQSectionManager::firstRow(const QModelIndex& idx) const
|
||||
{
|
||||
return sectionByRow(idx.row() ).row();
|
||||
}
|
||||
|
||||
|
||||
//! ermittelt die zeile unterhalb des gegebenen modelindex,
|
||||
//! zum einfügen neuer items ebendort.
|
||||
|
||||
int XQSectionManager::lastRow(const QModelIndex& idx) const
|
||||
{
|
||||
return lastRow(sectionByRow(idx.row()));
|
||||
}
|
||||
|
||||
|
||||
//! ermittelt die zeile unterhalb der gegebenen section,
|
||||
//! zum einfügen neuer items ebendort.
|
||||
|
||||
int XQSectionManager::lastRow(const XQModelSection& section ) const
|
||||
{
|
||||
//qDebug() << " -- last row in section: " << section.modelIndex.data().toString() << " --> " << section.modelIndex.row();
|
||||
// row() der section unterhalb dieser
|
||||
// __fix? index mit speichern?
|
||||
int index = _sections.indexOf(section);
|
||||
if (index > -1)
|
||||
{
|
||||
// last section? return last row of model
|
||||
if (index == _sections.size() - 1)
|
||||
return section.persistentModelIndex().model()->rowCount();// - 1;
|
||||
// return row above the row of the next section -> last row of given section
|
||||
return _sections.at(index+1).row();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
//! gibt alle sections aus, zum ankucken.
|
||||
|
||||
void XQSectionManager::dump() const
|
||||
{
|
||||
qDebug() << " --- sections dump(): " <<_sections.size() << " entries.";
|
||||
for( int i = 0; i<_sections.size(); ++i )
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user