Finished with the color selection dialog

Wed, 20 Mar 2013 20:44:04 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 20 Mar 2013 20:44:04 +0200
changeset 49
242f6ea0f5e5
parent 48
113eb6446c61
child 50
7fd213c6b844

Finished with the color selection dialog

gui.cpp file | annotate | diff | comparison | revisions
icons/colorcursor.png file | annotate | diff | comparison | revisions
zz_colorSelectDialog.cpp file | annotate | diff | comparison | revisions
zz_colorSelectDialog.h file | annotate | diff | comparison | revisions
--- a/gui.cpp	Wed Mar 20 19:41:37 2013 +0200
+++ b/gui.cpp	Wed Mar 20 20:44:04 2013 +0200
@@ -28,6 +28,7 @@
 #include "zz_addObjectDialog.h"
 #include "misc.h"
 #include "zz_colorSelectDialog.h"
+#include "colors.h"
 
 #define MAKE_ACTION(OBJECT, DISPLAYNAME, IMAGENAME, DESCR) \
 	qAct_##OBJECT = new QAction (QIcon ("./icons/" IMAGENAME ".png"), tr (DISPLAYNAME), this); \
@@ -548,6 +549,8 @@
 }
 
 void ForgeWindow::slot_testColorSelect () {
-	ColorSelectDialog dlg;
-	dlg.exec ();
+	short dColor;
+	if (ColorSelectDialog::staticDialog (dColor, -1, this)) {
+		printf ("you selected %s!\n", g_LDColors[dColor]->zName.chars());
+	}
 }
\ No newline at end of file
Binary file icons/colorcursor.png has changed
--- a/zz_colorSelectDialog.cpp	Wed Mar 20 19:41:37 2013 +0200
+++ b/zz_colorSelectDialog.cpp	Wed Mar 20 20:44:04 2013 +0200
@@ -23,6 +23,7 @@
 #include <qicon.h>
 #include <qboxlayout.h>
 #include <qgraphicsitem.h>
+#include <qevent.h>
 #include "zz_colorSelectDialog.h"
 #include "colors.h"
 
@@ -36,9 +37,10 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-ColorSelectDialog::ColorSelectDialog (QWidget* parent) {
+ColorSelectDialog::ColorSelectDialog (short dDefault, QWidget* parent) : QDialog (parent) {
 	qScene = new QGraphicsScene;
 	qView = new QGraphicsView (qScene);
+	dSelColor = dDefault;
 	
 	// not really an icon but eh
 	qScene->setBackgroundBrush (QPixmap ("icons/checkerboard.png"));
@@ -46,11 +48,43 @@
 	qScene->setSceneRect (0, 0, g_lWidth, g_lMaxHeight);
 	qView->setSceneRect (0, 0, g_lWidth, g_lMaxHeight);
 	
+	drawScene ();
+	
+	IMPLEMENT_DIALOG_BUTTONS
+	
+	// Set the size of the view
+	const long lWidth = g_lWidth + 21; // HACK
+	qView->setMaximumWidth (lWidth);
+	qView->setMinimumWidth (lWidth);
+	qView->setMaximumHeight (g_lHeight);
+	qView->setMinimumHeight (g_lHeight);
+	
+	qColorInfo = new QLabel;
+	drawColorInfo ();
+	
+	QHBoxLayout* qLayout = new QHBoxLayout;
+	qLayout->addWidget (qColorInfo);
+	qLayout->addWidget (qButtons);
+	
+	QVBoxLayout* qLayout2 = new QVBoxLayout;
+	qLayout2->addWidget (qView);
+	qLayout2->addLayout (qLayout);
+	setLayout (qLayout2);
+	
+	setWindowIcon (QIcon ("icons/palette.png"));
+	setWindowTitle (APPNAME_DISPLAY " - choose a color");
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ColorSelectDialog::drawScene () {
 	const double fPenWidth = 1.0f;
 	QPen qPen (Qt::black, fPenWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
 	
 	// Draw the color rectangles.
-	for (ulong i = 0; i < MAX_COLORS; ++i) {
+	qScene->clear ();
+	for (short i = 0; i < MAX_COLORS; ++i) {
 		color* meta = g_LDColors[i];
 		if (!meta)
 			continue;
@@ -70,33 +104,55 @@
 		QGraphicsTextItem* qText = qScene->addText (str::mkfmt ("%lu", i).chars());
 		qText->setDefaultTextColor ((bDark) ? Qt::white : Qt::black);
 		qText->setPos (x, y);
+		
+		if (i == dSelColor) {
+			QGraphicsPixmapItem* qCursorPic;
+			qCursorPic = qScene->addPixmap (QPixmap ("icons/colorcursor.png"));
+			qCursorPic->setPos (x, y);
+		}
+	}
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void ColorSelectDialog::drawColorInfo () {
+	if (dSelColor == -1) {
+		qColorInfo->setText ("---");
+		return;
 	}
 	
-	IMPLEMENT_DIALOG_BUTTONS
-	
-	// Set the size of the view
-	const long lWidth = g_lWidth + 21; // HACK
-	qView->setMaximumWidth (lWidth);
-	qView->setMinimumWidth (lWidth);
-	qView->setMaximumHeight (g_lHeight);
-	qView->setMinimumHeight (g_lHeight);
-	
-	QVBoxLayout* qLayout = new QVBoxLayout;
-	qLayout->addWidget (qView);
-	qLayout->addWidget (qButtons);
-	setLayout (qLayout);
-	
-	setWindowIcon (QIcon ("icons/palette.png"));
+	qColorInfo->setText (str::mkfmt ("%d - %s",
+		dSelColor, g_LDColors[dSelColor]->zName.chars()));
 }
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-bool ColorSelectDialog::staticDialog (short& dValue, QWidget* parent) {
-	ColorSelectDialog dlg;
+void ColorSelectDialog::mousePressEvent (QMouseEvent* event) {
+	QPointF qPoint = qView->mapToScene (event->pos ());
+	
+	ulong x = ((ulong)qPoint.x () - (g_dSquareSize / 2)) / g_dSquareSize;
+	ulong y = ((ulong)qPoint.y () - (g_dSquareSize / 2)) / g_dSquareSize;
+	ulong idx = (y * g_dNumColumns) + x;
+	
+	color* col = g_LDColors[idx];
+	if (!col)
+		return;
 	
-	if (dlg.exec ()) {
-		dValue = dValue;
+	dSelColor = idx;
+	drawScene ();
+	drawColorInfo ();
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+bool ColorSelectDialog::staticDialog (short& dValue, short dDefault, QWidget* parent) {
+	ColorSelectDialog dlg (dDefault, parent);
+	
+	if (dlg.exec () && dlg.dSelColor != -1) {
+		dValue = dlg.dSelColor;
 		return true;
 	}
 	
--- a/zz_colorSelectDialog.h	Wed Mar 20 19:41:37 2013 +0200
+++ b/zz_colorSelectDialog.h	Wed Mar 20 20:44:04 2013 +0200
@@ -18,13 +18,26 @@
 
 #include <qdialog.h>
 #include <qdialogbuttonbox.h>
+#include <qgraphicsscene.h>
+#include <qlabel.h>
 
 class ColorSelectDialog : public QDialog {
+	Q_OBJECT
+	
 public:
-	explicit ColorSelectDialog (QWidget* parent = nullptr);
-	static bool staticDialog (short& dValue, QWidget* parent = nullptr);
+	explicit ColorSelectDialog (short dDefault = -1, QWidget* parent = nullptr);
+	static bool staticDialog (short& dValue, short dDefault = -1, QWidget* parent = nullptr);
 	
 	QGraphicsScene* qScene;
 	QGraphicsView* qView;
+	QLabel* qColorInfo;
 	QDialogButtonBox* qButtons;
+	short dSelColor;
+	
+private:
+	void drawScene ();
+	void drawColorInfo ();
+	
+private slots:
+	void mousePressEvent (QMouseEvent* event);
 };
\ No newline at end of file

mercurial