/***************************************************************************

    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 <xqsimpleclipboard.h>
#include <xqviewmodel.h>


//! true, wenn paste an er stelle 'curIdx' möglich ist.

bool XQSimpleClipBoard::canPaste( const QModelIndex& curIdx ) const
{
  bool pasteOk = false;
  if( !isEmpty() )
  {
    XQItem& item = XQItem::xqItemFromIndex(curIdx);
    // __fixme! header items haben keinen ZNode!
    qDebug() << " --- can paste: " << item.contentNode()->tag_name() << " nodelist:  " << front().contentNode->tag_name();
    // paste is only allowed for the same component.type, which
    // is coded in the tag_type
    pasteOk = item.contentNode()->tag_name() == front().contentNode->tag_name();
  }
  else
  {
    qDebug() << " -- ClipBoard: nodelist empty!";
  }
  return pasteOk;
}


//! erzeugt eine positions-list aus der liste selectierter indicies.
//! Der mit seiner position zusammen gespeicherter knoten muss hier
//! gekloned werden.

void XQSimpleClipBoard::saveNodes( const QModelIndexList& list )
{
  clear();
  for( auto entry : list )
  {
    XQNodePtr contentNode = XQItem::xqItemFromIndex( entry ).contentNode();
    qDebug() << " --- Cloned: " << contentNode->friendly_name();
    // im clipboard brauchen wir eine eltern-lose kopie des knotens
    push_back( {entry.row(), contentNode->own_pos(), contentNode->clone() } );
  }
}

