Un-templated ButtonBox and renamed to RadioBox. Much better this way.

Sat, 04 May 2013 02:52:33 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 04 May 2013 02:52:33 +0300
changeset 157
635da3135bd4
parent 156
d35b7e440585
child 158
499286fcbf37

Un-templated ButtonBox and renamed to RadioBox. Much better this way.

buttonbox.h file | annotate | diff | comparison | revisions
ldforge.pro file | annotate | diff | comparison | revisions
radiobox.h file | annotate | diff | comparison | revisions
zz_addObjectDialog.cpp file | annotate | diff | comparison | revisions
zz_addObjectDialog.h file | annotate | diff | comparison | revisions
zz_newPartDialog.cpp file | annotate | diff | comparison | revisions
zz_newPartDialog.h file | annotate | diff | comparison | revisions
--- a/buttonbox.h	Sat May 04 01:57:42 2013 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +0,0 @@
-/*
- *  LDForge: LDraw parts authoring CAD
- *  Copyright (C) 2013 Santeri Piippo
- *  
- *  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 3 of the License, or
- *  (at your option) any later version.
- *  
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *  
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef BUTTONBOX_H
-#define BUTTONBOX_H
-
-#include "common.h"
-#include <qwidget.h>
-#include <qbuttongroup.h>
-#include <qgroupbox.h>
-#include <qboxlayout.h>
-
-// =============================================================================
-// ButtonBox<R>
-//
-// Convenience widget - is a groupbox of buttons. Mainly useful for quick creation
-// of radio button groups.
-// =============================================================================
-template<class R> class ButtonBox : public QGroupBox {
-private:
-	std::vector<R*> objects;
-	std::vector<QBoxLayout*> layouts;
-	QBoxLayout* coreLayout;
-	QBoxLayout* currentLayout;
-	QBoxLayout::Direction dir;
-	int currentId;
-	int defaultId;
-	
-public:
-	QButtonGroup* buttonGroup;
-	
-	QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) {
-		return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom;
-	}
-	
-	void init (Qt::Orientation orient) {
-		dir = makeDirection (orient);
-		
-		buttonGroup = new QButtonGroup;
-		currentId = 0;
-		coreLayout = null;
-		
-		// Ensure we have buttons and not lists or timers or cows or
-		// anything like that.
-		R* test = new R;
-		assert (test->inherits ("QAbstractButton"));
-		delete test;
-		
-		coreLayout = new QBoxLayout (makeDirection (orient, true));
-		setLayout (coreLayout);
-		
-		// Init the first row with a break
-		rowBreak ();
-	}
-	
-	explicit ButtonBox (QWidget* parent = null) : QGroupBox (parent) {
-		init (Qt::Vertical);
-	}
-	
-	explicit ButtonBox (const QString& title, QWidget* parent = null) : QGroupBox (title, parent) {
-		init (Qt::Vertical);
-	}
-	
-	explicit ButtonBox (const QGroupBox& box) : QGroupBox (box) {
-		init (Qt::Vertical);
-	}
-	
-	explicit ButtonBox (const QString& title, initlist<char const*> entries, int const defaultId,
-		const Qt::Orientation orient = Qt::Vertical, QWidget* parent = null) :
-		QGroupBox (title, parent), defaultId (defaultId)
-	{
-		init (orient);
-		
-		for (char const* entry : entries) {
-			addButton (entry);
-		}
-	}
-	
-	void rowBreak () {
-		QBoxLayout* newLayout = new QBoxLayout (dir);
-		currentLayout = newLayout;
-		layouts.push_back (newLayout);
-		
-		coreLayout->addLayout (newLayout);
-	}
-	
-	void setCurrentRow (uint row) {
-		currentLayout = layouts[row];
-	}
-	
-	void addButton (const char* entry) {
-		R* button = new R (entry);
-		addButton (button);
-	}
-	
-	void addButton (R* button) {
-		bool const selectThis = (currentId == defaultId);
-		
-		objects.push_back (button);
-		buttonGroup->addButton (button, currentId++);
-		currentLayout->addWidget (button);
-		
-		if (selectThis)
-			button->setChecked (true);
-	}
-	
-	ButtonBox<R>& operator<< (R* button) {
-		addButton (button);
-		return *this;
-	}
-	
-	ButtonBox<R>& operator<< (const char* entry) {
-		addButton (entry);
-		return *this;
-	}
-	
-	int value () {
-		return buttonGroup->checkedId ();
-	}
-	
-	void setValue (int val) {
-		static_cast<R*> (buttonGroup->button (val))->setChecked (true);
-	}
-	
-	R* const& begin () {
-		return objects.begin ();
-	}
-	
-	R* const& end () {
-		return objects.end ();
-	}
-	
-	R* operator[] (uint n) const {
-		return objects[n];
-	}
-	
-	bool exclusive () const {
-		return buttonGroup->exclusive ();
-	}
-	
-	void setExclusive (bool val) {
-		buttonGroup->setExclusive (val);
-	}
-	
-	bool isChecked (uint n) {
-		return objects[n]->checked ();
-	}
-};
-
-#endif // BUTTONBOX_H
\ No newline at end of file
--- a/ldforge.pro	Sat May 04 01:57:42 2013 +0300
+++ b/ldforge.pro	Sat May 04 02:52:33 2013 +0300
@@ -11,7 +11,6 @@
 
 # Input
 HEADERS += bbox.h \
-	buttonbox.h \
 	colors.h \
 	common.h \
 	config.h \
@@ -21,6 +20,7 @@
 	history.h \
 	ldtypes.h \
 	misc.h \
+	radiobox.h \
 	str.h \
 	types.h \
 	zz_aboutDialog.h \
@@ -43,6 +43,7 @@
 	ldtypes.cpp \
 	main.cpp \
 	misc.cpp \
+	radiobox.cpp \
 	str.cpp \ 
 	types.cpp \
 	zz_aboutDialog.cpp \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/radiobox.h	Sat May 04 02:52:33 2013 +0300
@@ -0,0 +1,115 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 Santeri Piippo
+ *  
+ *  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 3 of the License, or
+ *  (at your option) any later version.
+ *  
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *  
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef RADIOBOX_H
+#define RADIOBOX_H
+
+#include "common.h"
+#include <qwidget.h>
+#include <QGroupBox>
+#include <qradiobutton.h>
+#include <qboxlayout.h>
+#include <qbuttongroup.h>
+
+// =============================================================================
+// RadioBox
+//
+// Convenience widget - is a groupbox of radio buttons.
+// =============================================================================
+class RadioBox : public QGroupBox {
+	Q_OBJECT
+	
+public:
+	void init (Qt::Orientation orient);
+	
+	explicit RadioBox (QWidget* parent = null) : QGroupBox (parent) {
+		init (Qt::Vertical);
+	}
+	
+	explicit RadioBox (const QString& title, QWidget* parent = null) : QGroupBox (title, parent) {
+		init (Qt::Vertical);
+	}
+	
+	explicit RadioBox () {
+		init (Qt::Vertical);
+	}
+	
+	explicit RadioBox (const QString& title, initlist<char const*> entries, int const defaultId,
+		const Qt::Orientation orient = Qt::Vertical, QWidget* parent = null);
+	
+	void rowBreak ();
+	void setCurrentRow (uint row);
+	void addButton (const char* entry);
+	void addButton (QRadioButton* button);
+	RadioBox& operator<< (QRadioButton* button);
+	RadioBox& operator<< (const char* entry);
+	
+	int value () const {
+		return buttonGroup->checkedId ();
+	}
+	
+	void setValue (int val) {
+		buttonGroup->button (val)->setChecked (true);
+	}
+	
+	std::vector<QRadioButton*>::iterator begin () {
+		return objects.begin ();
+	}
+	
+	std::vector<QRadioButton*>::iterator end () {
+		return objects.end ();
+	}
+	
+	QRadioButton* operator[] (uint n) const {
+		return objects[n];
+	}
+	
+	bool exclusive () const {
+		return buttonGroup->exclusive ();
+	}
+	
+	void setExclusive (bool val) {
+		buttonGroup->setExclusive (val);
+	}
+	
+	bool isChecked (int n) const {
+		return buttonGroup->checkedId () == n;
+	}
+
+signals:
+	void sig_buttonPressed (int btn);
+	void sig_buttonPressed (QAbstractButton* btn);
+
+private:
+	std::vector<QRadioButton*> objects;
+	std::vector<QBoxLayout*> layouts;
+	QBoxLayout* coreLayout;
+	QBoxLayout* currentLayout;
+	QBoxLayout::Direction dir;
+	int currentId;
+	int defaultId;
+	QButtonGroup* buttonGroup;
+	
+	Q_DISABLE_COPY (RadioBox)
+
+private slots:
+	void slot_buttonPressed (int btn);
+	void slot_buttonPressed (QAbstractButton* btn);
+};
+
+#endif // RADIOBOX_H
\ No newline at end of file
--- a/zz_addObjectDialog.cpp	Sat May 04 01:57:42 2013 +0300
+++ b/zz_addObjectDialog.cpp	Sat May 04 02:52:33 2013 +0300
@@ -80,17 +80,17 @@
 		break;
 	
 	case OBJ_BFC:
-		bb_bfcType = new ButtonBox<QRadioButton> ("Statement", {}, 0, Qt::Horizontal);
+		rb_bfcType = new RadioBox ("Statement", {}, 0, Qt::Horizontal);
 		
 		for (int i = 0; i < LDBFC::NumStatements; ++i) {
 			if (i % (LDBFC::NumStatements / 2) == 0)
-				bb_bfcType->rowBreak ();
+				rb_bfcType->rowBreak ();
 			
-			bb_bfcType->addButton (new QRadioButton (LDBFC::saStatements[i]));
+			rb_bfcType->addButton (new QRadioButton (LDBFC::saStatements[i]));
 		}
 		
 		if (obj)
-			bb_bfcType->setValue ((int) static_cast<LDBFC*> (obj)->eStatement);
+			rb_bfcType->setValue ((int) static_cast<LDBFC*> (obj)->eStatement);
 		break;
 	
 	case OBJ_Subfile:
@@ -153,16 +153,16 @@
 		lb_radSegments = new QLabel ("Segments:");
 		lb_radRingNum = new QLabel ("Ring number:");
 		
-		bb_radType = new ButtonBox<QRadioButton> ("Type", {}, 0, Qt::Vertical);
+		rb_radType = new RadioBox ("Type", {}, 0, Qt::Vertical);
 		
 		for (int i = 0; i < LDRadial::NumTypes; ++i) {
 			if (i % (LDRadial::NumTypes / 2) == 0)
-				bb_radType->rowBreak ();
+				rb_radType->rowBreak ();
 			
-			bb_radType->addButton (new QRadioButton (LDRadial::radialTypeName ((LDRadial::Type) i)));
+			rb_radType->addButton (new QRadioButton (LDRadial::radialTypeName ((LDRadial::Type) i)));
 		}
 		
-		connect (bb_radType->buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_radialTypeChanged (int)));
+		connect (rb_radType, SIGNAL (sig_buttonPressed (int)), this, SLOT (slot_radialTypeChanged (int)));
 		
 		cb_radHiRes = new QCheckBox ("Hi-Res");
 		
@@ -175,7 +175,7 @@
 		if (obj) {
 			LDRadial* rad = static_cast<LDRadial*> (obj);
 			
-			bb_radType->setValue (rad->eRadialType);
+			rb_radType->setValue (rad->eRadialType);
 			sb_radSegments->setValue (rad->dSegments);
 			cb_radHiRes->setChecked ((rad->dDivisions == 48) ? Qt::Checked : Qt::Unchecked);
 			sb_radRingNum->setValue (rad->dRingNum);
@@ -228,11 +228,11 @@
 		break;
 	
 	case OBJ_BFC:
-		layout->addWidget (bb_bfcType, 0, 1);
+		layout->addWidget (rb_bfcType, 0, 1);
 		break;
 	
 	case OBJ_Radial:
-		layout->addWidget (bb_radType, 1, 1, 3, 1);
+		layout->addWidget (rb_radType, 1, 1, 3, 1);
 		layout->addWidget (cb_radHiRes, 1, 2);
 		layout->addWidget (lb_radSegments, 2, 2);
 		layout->addWidget (sb_radSegments, 2, 3);
@@ -397,7 +397,7 @@
 	case OBJ_BFC:
 		{
 			LDBFC* bfc = initObj<LDBFC> (obj);
-			bfc->eStatement = (LDBFC::Type) dlg.bb_bfcType->value ();
+			bfc->eStatement = (LDBFC::Type) dlg.rb_bfcType->value ();
 		}
 		break;
 	
@@ -421,7 +421,7 @@
 			
 			pRad->dDivisions = (dlg.cb_radHiRes->checkState () != Qt::Checked) ? 16 : 48;
 			pRad->dSegments = min<short> (dlg.sb_radSegments->value (), pRad->dDivisions);
-			pRad->eRadialType = (LDRadial::Type) dlg.bb_radType->value ();
+			pRad->eRadialType = (LDRadial::Type) dlg.rb_radType->value ();
 			pRad->dRingNum = dlg.sb_radRingNum->value ();
 			pRad->mMatrix = g_mIdentity;
 		}
@@ -452,9 +452,9 @@
 	if (newObject) {
 		ulong idx = g_ForgeWindow->getInsertionPoint ();
 		g_CurrentFile->insertObj (idx, obj);
-		History::addEntry (new AddHistory ({idx}, {obj->clone ()}));
+		History::addEntry (new AddHistory ({(ulong) idx}, {obj->clone ()}));
 	} else {
-		History::addEntry (new EditHistory ({obj->getIndex (g_CurrentFile)}, {backup}, {obj->clone ()}));
+		History::addEntry (new EditHistory ({(ulong) obj->getIndex (g_CurrentFile)}, {backup}, {obj->clone ()}));
 	}
 	
 	g_ForgeWindow->refresh ();
--- a/zz_addObjectDialog.h	Sat May 04 01:57:42 2013 +0300
+++ b/zz_addObjectDialog.h	Sat May 04 02:52:33 2013 +0300
@@ -20,7 +20,7 @@
 #define ZZ_ADDOBJECTDIALOG_H
 
 #include "gui.h"
-#include "buttonbox.h"
+#include "radiobox.h"
 #include <qdialog.h>
 #include <qlineedit.h>
 #include <qdialogbuttonbox.h>
@@ -50,7 +50,7 @@
 	QPushButton* pb_color;
 	
 	// BFC-related widgets
-	ButtonBox<QRadioButton>* bb_bfcType;
+	RadioBox* rb_bfcType;
 	
 	// Subfile stuff
 	QTreeWidget* tw_subfileList;
@@ -58,7 +58,7 @@
 	
 	// Radial stuff
 	QCheckBox* cb_radHiRes;
-	ButtonBox<QRadioButton>* bb_radType;
+	RadioBox* rb_radType;
 	QSpinBox* sb_radSegments, *sb_radRingNum;
 	QLabel* lb_radType, *lb_radResolution, *lb_radSegments,
 		*lb_radRingNum;
--- a/zz_newPartDialog.cpp	Sat May 04 01:57:42 2013 +0300
+++ b/zz_newPartDialog.cpp	Sat May 04 02:52:33 2013 +0300
@@ -48,21 +48,21 @@
 	lb_author = new QLabel ("Author:");
 	le_author = new QLineEdit;
 	
-	bb_license = new ButtonBox<QRadioButton> ("License", {
+	rb_license = new RadioBox ("License", {
 		"CCAL Redistributable",
 		"Non-redistributable",
 		"Don't append a license",
 	}, LICENSE_CCAL);
 	
-	bb_BFC = new ButtonBox<QRadioButton> ("BFC Winding", {
+	rb_BFC = new RadioBox ("BFC Winding", {
 		"CCW",
 		"CW",
 		"No winding"
 	}, BFCBOX_CCW);
 	
 	QHBoxLayout* boxes = new QHBoxLayout;
-	boxes->addWidget (bb_license);
-	boxes->addWidget (bb_BFC);
+	boxes->addWidget (rb_license);
+	boxes->addWidget (rb_BFC);
 	
 	IMPLEMENT_DIALOG_BUTTONS
 	
@@ -92,13 +92,13 @@
 		str zAuthor = dlg.le_author->text ();
 		vector<LDObject*>& objs = g_CurrentFile->objects;
 		
-		idx = dlg.bb_BFC->value ();
+		idx = dlg.rb_BFC->value ();
 		const LDBFC::Type eBFCType =
 			(idx == BFCBOX_CCW) ? LDBFC::CertifyCCW :
 			(idx == BFCBOX_CW) ? LDBFC::CertifyCW :
 			LDBFC::NoCertify;
 		
-		idx = dlg.bb_license->value ();
+		idx = dlg.rb_license->value ();
 		const char* sLicense =
 			(idx == LICENSE_CCAL) ? "Redistributable under CCAL version 2.0 : see CAreadme.txt" :
 			(idx == LICENSE_NonCA) ? "Not redistributable : see NonCAreadme.txt" :
--- a/zz_newPartDialog.h	Sat May 04 01:57:42 2013 +0300
+++ b/zz_newPartDialog.h	Sat May 04 02:52:33 2013 +0300
@@ -27,7 +27,7 @@
 #include <qradiobutton.h>
 #include <qbuttongroup.h>
 #include "gui.h"
-#include "buttonbox.h"
+#include "radiobox.h"
 
 class NewPartDialog : public QDialog {
 public:
@@ -37,7 +37,7 @@
 	QLabel* lb_brickIcon, *lb_name, *lb_author, *lb_license, *lb_BFC;
 	QLineEdit* le_name, *le_author;
 	
-	ButtonBox<QRadioButton>* bb_license, *bb_BFC;
+	RadioBox* rb_license, *rb_BFC;
 	QDialogButtonBox* bbx_buttons;
 };
 

mercurial