diff --git a/sources/titleblock/templatecommands.cpp b/sources/titleblock/templatecommands.cpp index 94cfe3848..b7e3db26b 100644 --- a/sources/titleblock/templatecommands.cpp +++ b/sources/titleblock/templatecommands.cpp @@ -848,3 +848,78 @@ void ChangeTemplateInformationsCommand::undo() { void ChangeTemplateInformationsCommand::redo() { tbtemplate_ -> setInformation(new_information_); } + +/** + Constructor + @param tb_template Changed title block template + @param parent Parent command +*/ +PasteTemplateCellsCommand::PasteTemplateCellsCommand(TitleBlockTemplate *tb_template, QUndoCommand *parent) : + TitleBlockTemplateCommand(tb_template, parent) +{ +} + +/** + Destructor +*/ +PasteTemplateCellsCommand::~PasteTemplateCellsCommand() { +} + +/** + Update the label describing this command +*/ +void PasteTemplateCellsCommand::updateText() { + setText(QObject::tr("Coller %n cellule(s)", "undo caption", erased_cells_.count())); +} + +/** + Undo a paste action. +*/ +void PasteTemplateCellsCommand::undo() { + foreach (TitleBlockCell *cell, erased_cells_.keys()) { + cell -> loadContentFromCell(erased_cells_.value(cell)); + } + if (view_) { + view_ -> refresh(); + } +} + +/** + Redo a paste action. +*/ +void PasteTemplateCellsCommand::redo() { + // paste the first cell ony + foreach (TitleBlockCell *cell, erased_cells_.keys()) { + cell -> loadContentFromCell(pasted_cells_.value(cell)); + } + if (view_) { + view_ -> refresh(); + } +} + +/** + @param cell Pointer to the cell impacted by te paste operation + @param new_cell_content Content pasted to the cell +*/ +void PasteTemplateCellsCommand::addPastedCell(TitleBlockCell *cell, const TitleBlockCell &new_cell_content) { + pasted_cells_.insert(cell, new_cell_content); +} + +/** + @param cell Pointer to the cell impacted by te paste operation + @param former_cell_content Content of the cell before the paste operation +*/ +void PasteTemplateCellsCommand::addErasedCell(TitleBlockCell *cell, const TitleBlockCell &former_cell_content) { + erased_cells_.insert(cell, former_cell_content); + updateText(); +} + +/** + This is a convenience function equivalent to: + addErasedCell(cell, before) + addPastedCell(cell, after) +*/ +void PasteTemplateCellsCommand::addCell(TitleBlockCell *cell, const TitleBlockCell &before, const TitleBlockCell &after) { + addPastedCell(cell, after); + addErasedCell(cell, before); +} diff --git a/sources/titleblock/templatecommands.h b/sources/titleblock/templatecommands.h index 3c7238c24..57c8c2702 100644 --- a/sources/titleblock/templatecommands.h +++ b/sources/titleblock/templatecommands.h @@ -250,4 +250,34 @@ class ChangeTemplateInformationsCommand : public QUndoCommand { /// Informations after they were modified QString new_information_; }; + + +/** + This class represents the action of pasting a cells set. +*/ +class PasteTemplateCellsCommand : public TitleBlockTemplateCommand { + // constructors, destructor + public: + PasteTemplateCellsCommand(TitleBlockTemplate *, QUndoCommand * = 0); + virtual ~PasteTemplateCellsCommand(); + private: + PasteTemplateCellsCommand(const PasteTemplateCellsCommand &); + + // methods + public: + virtual void undo(); + virtual void redo(); + virtual void addPastedCell(TitleBlockCell *, const TitleBlockCell &); + virtual void addErasedCell(TitleBlockCell *, const TitleBlockCell &); + virtual void addCell(TitleBlockCell *, const TitleBlockCell &, const TitleBlockCell &); + protected: + virtual void updateText(); + + // attributes + public: + /// Pasted cells + QHash pasted_cells_; + /// Existing cells impacted by the paste operation + QHash erased_cells_; +}; #endif diff --git a/sources/titleblock/templateview.cpp b/sources/titleblock/templateview.cpp index 2d4f3ed17..158da677a 100644 --- a/sources/titleblock/templateview.cpp +++ b/sources/titleblock/templateview.cpp @@ -159,7 +159,44 @@ void TitleBlockTemplateView::copy() { Import the cells described in the clipboard. */ void TitleBlockTemplateView::paste() { - /// TODO + // retrieve the clipboard content and parse it as XML + QClipboard *clipboard = QApplication::clipboard(); + QDomDocument xml_import; + + if (!xml_import.setContent(clipboard -> text().trimmed())) { + return; + } + + // ensure the XML document describes cells that can be pasted + if (xml_import.documentElement().tagName() != "titleblocktemplate-partial") { + return; + } + + // load pasted cells + QList pasted_cells; + QDomElement paste_root = xml_import.documentElement(); + for (QDomElement e = paste_root.firstChildElement() ; !e.isNull() ; e = e.nextSiblingElement()) { + if (e.tagName() == "empty" || e.tagName() == "field" || e.tagName() == "logo") { + TitleBlockCell cell; + cell.loadContentFromXml(e); + pasted_cells << cell; + } + } + + // paste the first cell only + if (!pasted_cells.count()) return; + + // onto the first selected one + TitleBlockTemplateVisualCell *selected_cell = selectedCellsSet().topLeftCell(); + if (!selected_cell) return; + + TitleBlockCell *erased_cell = selected_cell -> cell(); + if (!erased_cell) return; + + PasteTemplateCellsCommand *paste_command = new PasteTemplateCellsCommand(tbtemplate_); + paste_command -> addCell(erased_cell, *erased_cell, pasted_cells.first()); + requestGridModification(paste_command); + /// TODO paste more cells, using some kind of heuristic to place them? } /**