[qet] [1560] Title block template editor: implemented paste operation

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1560
Author:   xavier
Date:     2012-03-11 17:06:40 +0100 (Sun, 11 Mar 2012)
Log Message:
-----------
Title block template editor: implemented paste operation

Modified Paths:
--------------
    branches/0.3/sources/titleblock/templatecommands.cpp
    branches/0.3/sources/titleblock/templatecommands.h
    branches/0.3/sources/titleblock/templateview.cpp

Modified: branches/0.3/sources/titleblock/templatecommands.cpp
===================================================================
--- branches/0.3/sources/titleblock/templatecommands.cpp	2012-03-11 16:06:37 UTC (rev 1559)
+++ branches/0.3/sources/titleblock/templatecommands.cpp	2012-03-11 16:06:40 UTC (rev 1560)
@@ -848,3 +848,78 @@
 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);
+}

Modified: branches/0.3/sources/titleblock/templatecommands.h
===================================================================
--- branches/0.3/sources/titleblock/templatecommands.h	2012-03-11 16:06:37 UTC (rev 1559)
+++ branches/0.3/sources/titleblock/templatecommands.h	2012-03-11 16:06:40 UTC (rev 1560)
@@ -250,4 +250,34 @@
 	/// 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<TitleBlockCell *, TitleBlockCell> pasted_cells_;
+	/// Existing cells impacted by the paste operation
+	QHash<TitleBlockCell *, TitleBlockCell> erased_cells_;
+};
 #endif

Modified: branches/0.3/sources/titleblock/templateview.cpp
===================================================================
--- branches/0.3/sources/titleblock/templateview.cpp	2012-03-11 16:06:37 UTC (rev 1559)
+++ branches/0.3/sources/titleblock/templateview.cpp	2012-03-11 16:06:40 UTC (rev 1560)
@@ -159,7 +159,44 @@
 	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<TitleBlockCell> 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?
 }
 
 /**


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/