Added clipboard, added delete action

Thu, 21 Mar 2013 02:45:53 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 21 Mar 2013 02:45:53 +0200
changeset 55
b08f10e668e5
parent 54
60f328f352c9
child 56
64899ff3bcb3

Added clipboard, added delete action

file.cpp file | annotate | diff | comparison | revisions
file.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
gui.h file | annotate | diff | comparison | revisions
ldtypes.h file | annotate | diff | comparison | revisions
--- a/file.cpp	Thu Mar 21 00:37:15 2013 +0200
+++ b/file.cpp	Thu Mar 21 02:45:53 2013 +0200
@@ -425,4 +425,21 @@
 	
 	const ulong ulSpot = g_qWindow->getInsertionPoint ();
 	objects.insert (objects.begin() + ulSpot, obj);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void OpenFile::forgetObject (LDObject* obj) {
+	// Find the index for the given object
+	ulong ulIndex;
+	for (ulIndex = 0; ulIndex < (ulong)objects.size(); ++ulIndex)
+		if (objects[ulIndex] == obj)
+			break; // found it
+	
+	if (ulIndex >= objects.size ())
+		return; // was not found
+	
+	// Erase it from memory
+	objects.erase (objects.begin() + ulIndex);
 }
\ No newline at end of file
--- a/file.h	Thu Mar 21 00:37:15 2013 +0200
+++ b/file.h	Thu Mar 21 02:45:53 2013 +0200
@@ -42,6 +42,9 @@
 	
 	// Adds an object to this file at the appropriate location.
 	void addObject (LDObject* obj);
+	
+	// Deletes the given object from the object chain.
+	void forgetObject (LDObject* obj);
 };
 
 // Close all current loaded files and start off blank.
--- a/gui.cpp	Thu Mar 21 00:37:15 2013 +0200
+++ b/gui.cpp	Thu Mar 21 02:45:53 2013 +0200
@@ -35,6 +35,8 @@
 	qAct_##OBJECT->setStatusTip (tr (DESCR)); \
 	connect (qAct_##OBJECT, SIGNAL (triggered ()), this, SLOT (slot_##OBJECT ()));
 
+vector<LDObject*> g_Clipboard;
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
@@ -91,6 +93,7 @@
 	MAKE_ACTION (cut,			"Cut",			"cut",			"Cut the current selection to clipboard.")
 	MAKE_ACTION (copy,			"Copy",			"copy",			"Copy the current selection to clipboard.")
 	MAKE_ACTION (paste,			"Paste",		"paste",		"Paste clipboard contents.")
+	MAKE_ACTION (delete,		"Delete",		"delete",		"Delete the selection")
 	
 	MAKE_ACTION (setColor,		"Set Color",	"palette",		"Set the color on given objects.")
 	MAKE_ACTION (inline,		"Inline",		"inline",		"Inline selected subfiles.")
@@ -120,13 +123,11 @@
 	qAct_cut->setShortcut (Qt::CTRL | Qt::Key_X);
 	qAct_copy->setShortcut (Qt::CTRL | Qt::Key_C);
 	qAct_paste->setShortcut (Qt::CTRL | Qt::Key_V);
+	qAct_delete->setShortcut (Qt::Key_Delete);
 	
 	// things not implemented yet
 	QAction* qaDisabledActions[] = {
 		qAct_newSubfile,
-		qAct_cut,
-		qAct_copy,
-		qAct_paste,
 		qAct_about,
 		qAct_inline,
 		qAct_help,
@@ -165,6 +166,7 @@
 	qEditMenu->addAction (qAct_cut);			// Cut
 	qEditMenu->addAction (qAct_copy);			// Copy
 	qEditMenu->addAction (qAct_paste);			// Paste
+	qEditMenu->addAction (qAct_delete);			// Delete
 	qEditMenu->addSeparator ();					// -----
 	qEditMenu->addAction (qAct_setColor);		// Set Color
 	qEditMenu->addSeparator ();					// -----
@@ -205,6 +207,7 @@
 	qEditToolBar->addAction (qAct_cut);
 	qEditToolBar->addAction (qAct_copy);
 	qEditToolBar->addAction (qAct_paste);
+	qEditToolBar->addAction (qAct_delete);
 	qEditToolBar->addAction (qAct_setColor);
 	qEditToolBar->addAction (qAct_inline);
 	qEditToolBar->addAction (qAct_splitQuads);
@@ -315,16 +318,83 @@
 	QMessageBox::aboutQt (this);
 }
 
-void ForgeWindow::slot_cut () {
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+bool ForgeWindow::copyToClipboard () {
+	vector<LDObject*> objs = getSelectedObjects ();
+	
+	if (objs.size() == 0)
+		return false;
+	
+	// Clear the clipboard. However, its contents are dynamically allocated
+	// clones of LDObjects (cannot use pointers to real objects because the
+	// cut operation deletes them!), so we have to delete said objects first.
+	for (std::size_t i = 0; i < g_Clipboard.size(); ++i)
+		delete g_Clipboard[i];
+	
+	g_Clipboard.clear ();
+	
+	for (std::size_t i = 0; i < objs.size(); ++i)
+		g_Clipboard.push_back (objs[i]->makeClone ());
+	
+	return true;
+}
 
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ForgeWindow::deleteSelection () {
+	vector<LDObject*> objs = getSelectedObjects ();
+	
+	// Delete the objects that were being selected
+	for (ulong i = 0; i < (ulong)objs.size(); ++i) {
+		LDObject* obj = objs[i];
+		
+		g_CurrentFile->forgetObject (obj);
+		delete obj;
+	}
 }
 
-void ForgeWindow::slot_copy () {
-
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ForgeWindow::slot_cut () {
+	if (!copyToClipboard ())
+		return;
+	
+	deleteSelection ();
+	
+	qAct_paste->setEnabled (true);
+	refresh ();
 }
 
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ForgeWindow::slot_copy () {
+	if (copyToClipboard ())
+		qAct_paste->setEnabled (true);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 void ForgeWindow::slot_paste () {
+	for (std::size_t i = 0; i < g_Clipboard.size(); ++i) {
+		LDObject* copy = g_Clipboard[i]->makeClone ();
+		g_CurrentFile->addObject (copy);
+	}
 	
+	refresh ();
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ForgeWindow::slot_delete () {
+	deleteSelection ();
+	refresh ();
 }
 
 void ForgeWindow::slot_newVertex () {
--- a/gui.h	Thu Mar 21 00:37:15 2013 +0200
+++ b/gui.h	Thu Mar 21 02:45:53 2013 +0200
@@ -56,7 +56,7 @@
 	
 	// ACTION ARMADA
 	QAction* qAct_new, *qAct_open, *qAct_save, *qAct_saveAs, *qAct_exit;
-	QAction* qAct_cut, *qAct_copy, *qAct_paste;
+	QAction* qAct_cut, *qAct_copy, *qAct_paste, *qAct_delete;
 	QAction* qAct_newSubfile, *qAct_newLine, *qAct_newTriangle, *qAct_newQuad;
 	QAction* qAct_newCondLine, *qAct_newComment, *qAct_newVertex;
 	QAction* qAct_splitQuads, *qAct_setContents, *qAct_inline;
@@ -77,6 +77,8 @@
 	void createMenuActions ();
 	void createMenus ();
     void createToolbars ();
+	bool copyToClipboard ();
+	void deleteSelection ();
 
 private slots:
 	void slot_selectionChanged ();
@@ -102,6 +104,7 @@
 	void slot_cut ();
 	void slot_copy ();
 	void slot_paste ();
+	void slot_delete ();
 	
 	void slot_settings ();
 	
--- a/ldtypes.h	Thu Mar 21 00:37:15 2013 +0200
+++ b/ldtypes.h	Thu Mar 21 02:45:53 2013 +0200
@@ -27,7 +27,10 @@
 	virtual LDObjectType_e getType () const { \
 		return OBJ_##N; \
 	} \
-	virtual str getContents ();
+	virtual str getContents (); \
+	virtual LD##N* makeClone () { \
+		return new LD##N (*this); \
+	}
 
 class QTreeWidgetItem;
 
@@ -79,6 +82,10 @@
 		return "";
 	}
 	
+	virtual LDObject* makeClone () {
+		return new LDObject (*this);
+	}
+	
 	void commonInit ();
 	
 	// Replace this LDObject with another LDObject. This method deletes the

mercurial