Reworked properties a bit

Thu, 13 Jun 2013 00:48:53 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 13 Jun 2013 00:48:53 +0300
changeset 274
d232fe4d88a6
parent 273
0a9141118630
child 275
7b5afec27688

Reworked properties a bit

src/common.h file | annotate | diff | comparison | revisions
src/dialogs.cpp file | annotate | diff | comparison | revisions
src/dialogs.h file | annotate | diff | comparison | revisions
src/extprogs.cpp file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/file.h file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui_editactions.cpp file | annotate | diff | comparison | revisions
src/history.cpp file | annotate | diff | comparison | revisions
src/history.h file | annotate | diff | comparison | revisions
src/historyDialog.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- a/src/common.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/common.h	Thu Jun 13 00:48:53 2013 +0300
@@ -89,63 +89,45 @@
 #define PROP_NAME(GET) m_##GET
 
 #define READ_ACCESSOR(T, GET) \
-	T const& GET () const { return PROP_NAME (GET); }
+	T const& GET () const
 
-#define SET_ACCESSOR(T, GET, SET) \
-	T const& SET (T val) { PROP_NAME (GET) = val; return PROP_NAME (GET); }
+#define SET_ACCESSOR(T, SET) \
+	void SET (T val)
 
 // Read-only, private property with a get accessor
+#define DECLARE_READ_PROPERTY(T, GET, SET) \
+private: \
+	T PROP_NAME (GET); \
+	SET_ACCESSOR (T, SET); \
+public: \
+	READ_ACCESSOR (T, GET);
+
+// Read/write private property with get and set accessors
+#define DECLARE_PROPERTY(T, GET, SET) \
+private: \
+	T PROP_NAME (GET); \
+public: \
+	READ_ACCESSOR (T, GET); \
+	SET_ACCESSOR (T, SET);
+
+#define DEFINE_PROPERTY(T, CLASS, GET, SET) \
+	READ_ACCESSOR (T, CLASS::GET) { return PROP_NAME (GET); } \
+	SET_ACCESSOR (T, CLASS::SET) { PROP_NAME (GET) = val; }
+
+// Shortcuts
+#define PROPERTY(T, GET, SET) \
+private: \
+	T PROP_NAME (GET); \
+public: \
+	READ_ACCESSOR (T, GET) { return PROP_NAME (GET); } \
+	SET_ACCESSOR (T, SET) { PROP_NAME (GET) = val; }
+
 #define READ_PROPERTY(T, GET, SET) \
 private: \
 	T PROP_NAME (GET); \
-	SET_ACCESSOR (T, GET, SET) \
-public: \
-	READ_ACCESSOR (T, GET)
-
-// Same as above except not const
-#define MUTABLE_READ_PROPERTY(T, GET) \
-private: \
-	T PROP_NAME(GET); \
-public: \
-	T& GET () { return PROP_NAME(GET); }
-
-// Read/write private property with get and set accessors
-#define PROPERTY(T, GET, SET) \
-private: \
-	T PROP_NAME(GET); \
-public: \
-	READ_ACCESSOR(T, GET) \
-	SET_ACCESSOR(T, GET, SET)
-
-// Property that triggers a callback when it is changed
-#define CALLBACK_PROPERTY(T, GET, SET, CALLBACK) \
-private: \
-	T PROP_NAME(GET); \
+	SET_ACCESSOR (T, SET) { PROP_NAME (GET) = val; } \
 public: \
-	READ_ACCESSOR(T, GET) \
-	void CALLBACK (); \
-	T const& SET (T val) { \
-		PROP_NAME(GET) = val; \
-		CALLBACK (); \
-		return m_##GET; \
-	}
-
-// Property with thread locking, use when multiple threads access the same property
-// Comes with a callback function for detecting when the value is changed.
-#define THREAD_PROPERTY(T, GET, SET) \
-private: \
-	T PROP_NAME (GET); \
-	QMutex m_threadLock_##GET; \
-public: \
-	READ_ACCESSOR(T, GET) \
-	void callback_##SET (); \
-	T const& SET (T val) { \
-		m_threadLock_##GET.lock (); \
-		PROP_NAME (GET) = val; \
-		callback_##SET (); \
-		m_threadLock_##GET.unlock (); \
-		return m_##GET; \
-	}
+	READ_ACCESSOR (T, GET) { return PROP_NAME (GET); }
 
 #ifdef null
 #undef null
--- a/src/dialogs.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/dialogs.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -161,9 +161,11 @@
 	
 	dsb_search = new QDoubleSpinBox;
 	dsb_search->setRange (-10000.0f, 10000.0f);
+	dsb_search->setDecimals (6);
 	
 	dsb_replacement = new QDoubleSpinBox;
 	dsb_replacement->setRange (-10000.0f, 10000.0f);
+	dsb_replacement->setDecimals (6);
 	
 	cb_any = new QCheckBox ("Any");
 	cb_rel = new QCheckBox ("Relative");
@@ -404,7 +406,6 @@
 	
 	short idx;
 	str author = dlg.le_author->text ();
-	vector<LDObject*>& objs = g_curfile->objs ();
 	
 	idx = dlg.rb_BFC->value ();
 	const LDBFC::Type BFCType =
@@ -418,17 +419,17 @@
 		(idx == NonCA) ? "Not redistributable : see NonCAreadme.txt" :
 		null;
 	
-	objs << new LDComment (dlg.le_name->text ());
-	objs << new LDComment ("Name: <untitled>.dat");
-	objs << new LDComment (fmt ("Author: %s", author.chars()));
-	objs << new LDComment (fmt ("!LDRAW_ORG Unofficial_Part"));
+	*g_curfile << new LDComment (dlg.le_name->text ());
+	*g_curfile << new LDComment ("Name: <untitled>.dat");
+	*g_curfile << new LDComment (fmt ("Author: %s", author.chars()));
+	*g_curfile << new LDComment (fmt ("!LDRAW_ORG Unofficial_Part"));
 	
 	if (license != null)
-		objs << new LDComment (fmt ("!LICENSE %s", license));
+		*g_curfile << new LDComment (fmt ("!LICENSE %s", license));
 	
-	objs << new LDEmpty;
-	objs << new LDBFC (BFCType);
-	objs << new LDEmpty;
+	*g_curfile << new LDEmpty;
+	*g_curfile << new LDBFC (BFCType);
+	*g_curfile << new LDEmpty;
 	
 	g_win->fullRefresh ();
 }
@@ -505,7 +506,12 @@
 	layout->addWidget (dbb_buttons);
 }
 
-void OpenProgressDialog::callback_setNumLines () {
+READ_ACCESSOR (ulong, OpenProgressDialog::numLines) {
+	return m_numLines;
+}
+
+SET_ACCESSOR (ulong, OpenProgressDialog::setNumLines) {
+	m_numLines = val;
 	progressBar->setRange (0, numLines ());
 	updateValues ();
 }
--- a/src/dialogs.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/dialogs.h	Thu Jun 13 00:48:53 2013 +0300
@@ -169,7 +169,7 @@
 class OpenProgressDialog : public QDialog {
 	Q_OBJECT
 	READ_PROPERTY (ulong, progress, setProgress)
-	CALLBACK_PROPERTY (ulong, numLines, setNumLines, callback_setNumLines)
+	DECLARE_PROPERTY (ulong, numLines, setNumLines)
 	
 public:
 	explicit OpenProgressDialog (QWidget* parent = null, Qt::WindowFlags f = 0);
--- a/src/extprogs.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/extprogs.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -154,7 +154,7 @@
 // =============================================================================
 void writeColorGroup (const short colnum, str fname) {
 	vector<LDObject*> objects;
-	for (LDObject*& obj : g_curfile->objs ()) {
+	for (LDObject*& obj : *g_curfile) {
 		if (obj->isColored () == false || obj->color () != colnum)
 			continue;
 		
--- a/src/file.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/file.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -92,7 +92,7 @@
 LDOpenFile::LDOpenFile () {
 	setImplicit (true);
 	setSavePos (-1);
-	history ().setFile (this);
+	m_history.setFile (this);
 }
 
 // =============================================================================
--- a/src/file.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/file.h	Thu Jun 13 00:48:53 2013 +0300
@@ -48,10 +48,10 @@
 class LDOpenFile {
 	PROPERTY (str, name, setName)
 	PROPERTY (bool, implicit, setImplicit)
-	MUTABLE_READ_PROPERTY (vector<LDObject*>, objs) // TODO: make this private!
+	READ_PROPERTY (vector<LDObject*>, objs, setObjects)
 	PROPERTY (vector<LDObject*>, cache, setCache)
 	PROPERTY (long, savePos, setSavePos)
-	MUTABLE_READ_PROPERTY (History, history)
+	READ_PROPERTY (History, history, setHistory)
 	READ_PROPERTY (vector<LDObject*>, vertices, setVertices)
 	
 public:
@@ -80,12 +80,23 @@
 	ulong numObjs () const { return m_objs.size (); }
 	void setObject (ulong idx, LDObject* obj);
 	
+	LDOpenFile& operator<< (LDObject* obj) {
+		addObject (obj);
+		return *this;
+	}
+	
 	it begin () { return PROP_NAME (objs).begin (); }
 	c_it begin () const { return PROP_NAME (objs).begin (); }
 	it end () { return PROP_NAME (objs).end (); }
 	c_it end () const { return PROP_NAME (objs).end (); }
 	
 	static void closeUnused ();
+	
+	void openHistory () { m_history.open (); }
+	void closeHistory () { m_history.close (); }
+	void undo () { m_history.undo (); }
+	void redo () { m_history.redo (); }
+	void clearHistory () { m_history.clear (); }
 };
 
 // Close all current loaded files and start off blank.
--- a/src/gldraw.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/gldraw.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -1189,7 +1189,13 @@
 }
 
 // =============================================================================
-void GLRenderer::callback_setEditMode () {
+READ_ACCESSOR (EditMode, GLRenderer::editMode) {
+	return m_editMode;
+}
+
+SET_ACCESSOR (EditMode, GLRenderer::setEditMode) {
+	m_editMode = val;
+	
 	switch (editMode ()) {
 	case Select:
 		unsetCursor ();
@@ -1268,11 +1274,11 @@
 	}
 	
 	if (obj) {
-		file ()->history ().open ();
+		file ()->openHistory ();
 		file ()->addObject (obj);
 		compileObject (obj);
 		g_win->fullRefresh ();
-		file ()->history ().close ();
+		file ()->closeHistory ();
 	}
 	
 	m_drawedVerts.clear ();
--- a/src/gldraw.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/gldraw.h	Thu Jun 13 00:48:53 2013 +0300
@@ -59,7 +59,7 @@
 	PROPERTY (double, zoom, setZoom)
 	PROPERTY (LDOpenFile*, file, setFile)
 	READ_PROPERTY (bool, picking, setPicking)
-	CALLBACK_PROPERTY (EditMode, editMode, setEditMode, callback_setEditMode)
+	DECLARE_PROPERTY (EditMode, editMode, setEditMode)
 	
 public:
 	enum Camera { Top, Front, Left, Bottom, Back, Right, Free };
--- a/src/gui.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/gui.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -529,34 +529,35 @@
 EXTERN_ACTION (undo);
 EXTERN_ACTION (redo);
 void ForgeWindow::slot_action () {
+	// Open the history so we can record the edits done during this action.
 	if (sender () != ACTION (undo) && sender () != ACTION (redo))
-		g_curfile->history ().open ();
+		g_curfile->openHistory ();
 	
 	// Get the action that triggered this slot.
 	QAction* qAct = static_cast<QAction*> (sender ());
 	
 	// Find the meta for the action.
-	actionmeta* pMeta = null;
+	actionmeta* meta = null;
 	
-	for (actionmeta meta : g_actionMeta) {
-		if (meta.qAct == null)
+	for (actionmeta& it : g_actionMeta) {
+		if (it.qAct == null)
 			break;
 		
-		if (*meta.qAct == qAct) {
-			pMeta = &meta;
+		if (*it.qAct == qAct) {
+			meta = &it;
 			break;
 		}
 	}
 	
-	if (!pMeta) {
+	if (!meta) {
 		logf (LOG_Warning, "unknown signal sender %p!\n", qAct);
 		return;
 	}
 	
 	// We have the meta, now call the handler.
-	(*pMeta->handler) ();
+	(*meta->handler) ();
 	
-	g_curfile->history ().close ();
+	g_curfile->closeHistory ();
 }
 
 // =============================================================================
--- a/src/gui_editactions.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/gui_editactions.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -366,11 +366,11 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 MAKE_ACTION (undo, "Undo", "undo", "Undo a step.", CTRL (Z)) {
-	g_curfile->history ().undo ();
+	g_curfile->undo ();
 }
 
 MAKE_ACTION (redo, "Redo", "redo", "Redo a step.", CTRL_SHIFT (Z)) {
-	g_curfile->history ().redo ();
+	g_curfile->redo ();
 }
 
 MAKE_ACTION (showHistory, "Edit History", "history", "Show the history dialog.", (0)) {
--- a/src/history.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/history.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -64,7 +64,7 @@
 	
 }
 
-void History::updateActions () {
+void History::updateActions () const {
 	ACTION (undo)->setEnabled (pos () != -1);
 	ACTION (redo)->setEnabled (pos () < (long) m_changesets.size () - 1);
 }
--- a/src/history.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/history.h	Thu Jun 13 00:48:53 2013 +0300
@@ -51,7 +51,7 @@
 	void undo ();
 	void redo ();
 	void clear ();
-	void updateActions ();
+	void updateActions () const;
 	
 	void open ();
 	void close ();
--- a/src/historyDialog.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/historyDialog.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -23,8 +23,6 @@
 #include "colors.h"
 #include "file.h"
 
-History* g_history = null;
-
 EXTERN_ACTION (undo);
 EXTERN_ACTION (redo);
 
@@ -32,8 +30,6 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 HistoryDialog::HistoryDialog (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f) {
-	g_history = &g_curfile->history ();
-	
 	historyList = new QListWidget;
 	undoButton = new QPushButton ("Undo");
 	redoButton = new QPushButton ("Redo");
@@ -85,7 +81,7 @@
 	historyList->addItem (qItem);
 	
 #if 0
-	for (AbstractHistoryEntry* entry : g_history->entries ()) {
+	for (AbstractHistoryEntry* entry : g_curfile->history ().entries ()) {
 		str text;
 		QIcon entryIcon;
 		
@@ -107,14 +103,14 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void HistoryDialog::slot_undo () {
-	g_curfile->history ().undo ();
+	g_curfile->undo ();
 	updateButtons ();
 	updateSelection ();
 }
 
 // =============================================================================
 void HistoryDialog::slot_redo () {
-	g_curfile->history ().redo ();
+	g_curfile->redo ();
 	updateButtons ();
 	updateSelection ();
 }
@@ -128,7 +124,7 @@
 	if (!confirm ("Are you sure you want to clear the edit history?\nThis cannot be undone."))
 		return; // Canceled
 	
-	g_curfile->history ().clear ();
+	g_curfile->clearHistory ();
 	populateList ();
 	updateButtons ();
 }
@@ -156,15 +152,12 @@
 	// the index now
 	index--;
 	
-	if (index == g_curfile->history ().pos ())
-		return;
-	
 	// Seek to the selected edit by repeadetly undoing or redoing.
-	while (g_history->pos () != index) {
-		if (g_history->pos () > index)
-			g_history->undo ();
+	while (g_curfile->history ().pos () != index) {
+		if (g_curfile->history ().pos () > index)
+			g_curfile->undo ();
 		else
-			g_history->redo ();
+			g_curfile->redo ();
 	}
 	
 	updateButtons ();
--- a/src/ldtypes.cpp	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/ldtypes.cpp	Thu Jun 13 00:48:53 2013 +0300
@@ -171,7 +171,8 @@
 	tri2->coords[2] = coords[3];
 	
 	// The triangles also inherit the quad's color
-	tri1->setColor (tri2->setColor (color ()));
+	tri1->setColor (color ());
+	tri2->setColor (color ());
 	
 	vector<LDTriangle*> triangles;
 	triangles << tri1;
@@ -184,7 +185,7 @@
 // =============================================================================
 void LDObject::replace (LDObject* replacement) {
 	// Replace the instance of the old object with the new object
-	for (LDObject*& obj : g_curfile->objs ()) {
+	for (LDObject*& obj : *g_curfile) {
 		if (obj == this) {
 			obj = replacement;
 			break;
@@ -199,7 +200,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void LDObject::swap (LDObject* other) {
-	for (LDObject*& obj : g_curfile->objs ()) {
+	for (LDObject*& obj : *g_curfile) {
 		if (obj == this)
 			obj = other;
 		else if (obj == other)
--- a/src/types.h	Mon Jun 10 16:17:24 2013 +0300
+++ b/src/types.h	Thu Jun 13 00:48:53 2013 +0300
@@ -45,14 +45,14 @@
 using std::size_t;
 
 enum Axis { X, Y, Z };
-static const Axis g_Axes[3] = {X, Y, Z};
+static const Axis g_Axes[3] = { X, Y, Z };
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 // matrix
 // 
-// A templated, mathematical N x N matrix
+// A mathematical 3 x 3 matrix
 // =============================================================================
 class matrix {
 public:

mercurial