Files
xtree.old.ng/model/xqcommand.cpp
2025-08-06 16:44:08 +02:00

135 lines
3.4 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 <xqcommand.h>
#include <xqmodel.h>
#include <xqtreeview.h>
void XQNodeStore::dumpList( const QString& title ) const
{
if( !title.isEmpty() )
qDebug() << " --- " << title;
for( const auto& entry : *this )
qDebug() << " -- dumpList: itemPos: " << entry.itemPos << " nodePos: " << entry.nodePos << " id: " << ( entry.contentNode ? entry.contentNode->_id : 0 ) << " used: " << ( entry.contentNode ? entry.contentNode.use_count() : 0 );
}
XQCommand::XQCommand(CmdType cmdType, XQModel* modelView )
: _cmdType{ cmdType }, _model(modelView)
{
}
XQCommand::~XQCommand()
{
qDebug() << " --- command destructor: " << toString();
}
XQCommand::CmdType XQCommand::commandType() const
{
return _cmdType;
}
void XQCommand::setCommandType( XQCommand::CmdType cmdType )
{
_cmdType = cmdType;
}
//! ruft 'onCommandRedo' 'meines' models auf.
void XQCommand::redo()
{
_model->onCommandRedo( *this );
}
//! ruft 'onCommandUndo' 'meines' models auf.
void XQCommand::undo()
{
_model->onCommandUndo( *this );
}
//! gibt den urpsrungs-index dieses commands zurück.
const QModelIndex& XQCommand::originIndex() const
{
return _originIndex;
}
//! merkt sich den ersten QModelIndex der von diesem command
//! betroffenen items. zusätzlich wird der command-text erzeugt.
void XQCommand::setOriginIndex( const QModelIndex& origin )
{
QString cmdText("%1: %2 (%3)");
QString name = origin.data().toString();
QString items("%1 item%2");
int mySize = size();
items = items.arg(mySize).arg(mySize > 1 ? "s" : "");
cmdText = cmdText.arg( toString(), name, items );
_originIndex = origin;
setText(cmdText);
}
//! erzeugt aus den 'selected indices' eine liste mit der jewiligen knotenposition,
//! der item-zeile und dem content-knoten.
void XQCommand::saveNodes( const QModelIndexList& list )
{
clear();
// über jede zeil
for( auto entry : list )
{
// knoten holen
const XQNodePtr& contentNode = XQItem::xqItemFromIndex( entry ).contentNode();
// hier speichern wir den original knoten, nicht einen clone, wie im clipboard.
push_back( {entry.row(), contentNode->own_pos(), contentNode } );
}
}
//! erzeugt einen string aus dem command-type, fürs debuggen.
QString XQCommand::toString()
{
static QMap<CmdType,QString> s_CmdTypeMap
{
{ cmdTextEdit, "cmdTextEdit" },
{ cmdInvalid, "cmdInvalid" },
{ cmdCut, "cmdCut" },
{ cmdPaste, "cmdPaste" },
{ cmdPasteSelf, "cmdPasteSelf" },
{ cmdNew, "cmdNew" },
{ cmdUndo, "cmdUndo" },
{ cmdRedo, "cmdRedo" },
{ cmdCopy, "cmdCopy" },
{ cmdMove, "cmdMove" },
{ cmdDelete, "cmdDelete" },
{ cmdToggleSection, "cmdToggleSection" },
{ cmdExtern, "cmdExtern" }
};
if( !s_CmdTypeMap.contains( _cmdType ))
return QString(" cmdType missmatch");
return s_CmdTypeMap[_cmdType];
}