- changed the unorthodox color selection grid into a push button grid

Sat, 06 Sep 2014 15:45:07 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 06 Sep 2014 15:45:07 +0300
changeset 878
945e44575b3e
parent 877
7c50b61ad5b6
child 879
c5b3bc08e609

- changed the unorthodox color selection grid into a push button grid

changelog.txt file | annotate | diff | comparison | revisions
src/colorSelector.cc file | annotate | diff | comparison | revisions
src/colorSelector.h file | annotate | diff | comparison | revisions
ui/colorsel.ui file | annotate | diff | comparison | revisions
--- a/changelog.txt	Thu Sep 04 11:48:05 2014 +0300
+++ b/changelog.txt	Sat Sep 06 15:45:07 2014 +0300
@@ -83,6 +83,7 @@
 		color/type of the existing selection does not need to be uniform anymore.
 !	- Removed the Non-CA license option. The CA license option is now a checkbox (disabling it 
 		means no license at all).
+!	- The color selection prompt now uses a grid of pushbuttons instead of an unorthodox grid of colors
 
 
 
--- a/src/colorSelector.cc	Thu Sep 04 11:48:05 2014 +0300
+++ b/src/colorSelector.cc	Sat Sep 06 15:45:07 2014 +0300
@@ -33,33 +33,70 @@
 #include "ui_colorsel.h"
 
 static const int g_numColumns = 16;
-static const int g_squareSize = 32;
 
 EXTERN_CFGENTRY (String, MainColor)
 EXTERN_CFGENTRY (Float, MainColorAlpha)
 
 // =============================================================================
 //
-ColorSelector::ColorSelector (LDColor defval, QWidget* parent) : QDialog (parent)
+ColorSelector::ColorSelector (LDColor defaultvalue, QWidget* parent) :
+	QDialog (parent)
 {
 	m_firstResize = true;
 	ui = new Ui_ColorSelUI;
 	ui->setupUi (this);
+	setSelection (defaultvalue);
 
-	m_scene = new QGraphicsScene;
-	ui->viewport->setScene (m_scene);
-	setSelection (defval);
+	QGridLayout* layout = new QGridLayout (this);
+
+	// Spawn color selector buttons
+	for (int i = 0; i < CountLDConfigColors(); ++i)
+	{
+		LDColor ldcolor = LDColor::fromIndex (i);
+		QPushButton* button = new QPushButton (this);
+		button->setMinimumSize (QSize (32, 32));
+		button->setMaximumSize (button->minimumSize());
+
+		if (ldcolor != null)
+		{
+			QString colorname;
+			QColor color (ldcolor.faceColor());
+
+			if (i == MainColorIndex)
+			{
+				color = QColor (cfg::MainColor);
+				color.setAlphaF (cfg::MainColorAlpha);
+			}
 
-	// not really an icon but eh
-	m_scene->setBackgroundBrush (GetIcon ("checkerboard"));
-	drawScene();
+			QString color2name (Luma (color) < 80 ? "white" : "black");
+			button->setAutoFillBackground (true);
+			button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5",
+				color.red(), color.green(), color.blue(), color.alpha(), color2name));
+			button->setCheckable (true);
+			button->setText (QString::number (ldcolor.index()));
+			button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name()));
+			m_buttons[i] = button;
+			m_buttonsReversed[button] = i;
+			connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked()));
 
-	int width = viewportWidth();
-	ui->viewport->setMinimumWidth (width);
-	ui->viewport->setMaximumWidth (width);
+			if (ldcolor == selection())
+				button->setChecked (true);
+		}
+		else
+		{
+			button->setEnabled (false);
+		}
 
+		layout->addWidget (button, i / g_numColumns, i % g_numColumns);
+	}
+
+	QWidget* widget = new QWidget();
+	widget->setLayout (layout);
+	ui->definedColors->setWidget (widget);
 	connect (ui->directColor, SIGNAL (clicked (bool)), this, SLOT (chooseDirectColor()));
 
+	ui->definedColors->setMinimumWidth (ui->definedColors->widget()->width() + 16);
+
 #ifdef TRANSPARENT_DIRECT_COLORS
 	connect (ui->transparentDirectColor, SIGNAL (clicked (bool)), this, SLOT (transparentCheckboxClicked()));
 #else
@@ -78,67 +115,30 @@
 
 // =============================================================================
 //
-void ColorSelector::drawScene()
+void ColorSelector::colorButtonClicked()
 {
-	const int numCols = g_numColumns;
-	const int square = g_squareSize;
-	const int g_maxHeight = (numRows() * square);
-	QRect sceneRect (0, 0, viewportWidth(), g_maxHeight);
-
-	m_scene->setSceneRect (sceneRect);
-	ui->viewport->setSceneRect (sceneRect);
-
-	const double penWidth = 1.0f;
-
-	// Draw the color rectangles.
-	m_scene->clear();
-
-	for (int i = 0; i < CountLDConfigColors(); ++i)
-	{
-		LDColor info = LDColor::fromIndex (i);
-
-		if (info == null)
-			continue;
-
-		const double x = (i % numCols) * square;
-		const double y = (i / numCols) * square;
-		const double w = square - (penWidth / 2);
-
-		QColor col (info.faceColor());
+	QPushButton* button = qobject_cast<QPushButton*> (sender());
+	auto it = m_buttonsReversed.find (button);
+	LDColor color;
 
-		if (i == MainColorIndex)
-		{
-			// Use the user preferences for main color here
-			col = QColor (cfg::MainColor);
-			col.setAlpha (cfg::MainColorAlpha * 255.0f);
-		}
-
-		QPen pen (info.edgeColor(), penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
-		m_scene->addRect (x, y, w, w, pen, col);
-		QGraphicsTextItem* numtext = m_scene->addText (format ("%1", i));
-		numtext->setDefaultTextColor ((Luma (col) < 80) ? Qt::white : Qt::black);
-		numtext->setPos (x, y);
+	if (Q_UNLIKELY (button == null or it == m_buttonsReversed.end()
+		or (color = LDColor::fromIndex (*it)) == null))
+	{
+		print ("colorButtonClicked() called with invalid sender");
+		return;
+	}
 
-		if (selection() and i == selection().index())
-		{
-			auto curspic = m_scene->addPixmap (GetIcon ("colorcursor"));
-			curspic->setPos (x, y);
-		}
-	}
-}
+	if (selection() != null)
+	{
+		auto it2 = m_buttons.find (selection().index());
 
-// =============================================================================
-//
-int ColorSelector::numRows() const
-{
-	return (CountLDConfigColors() / g_numColumns);
-}
+		if (it2 != m_buttons.end())
+			(*it2)->setChecked (false);
+	}
 
-// =============================================================================
-//
-int ColorSelector::viewportWidth() const
-{
-	return g_numColumns * g_squareSize + 21;
+	setSelection (color);
+	button->setChecked (true);
+	drawColorInfo();
 }
 
 // =============================================================================
@@ -168,49 +168,6 @@
 
 // =============================================================================
 //
-void ColorSelector::resizeEvent (QResizeEvent*)
-{
-	// If this is the first resize, check if we need to scroll down to see the
-	// currently selected color. We cannot do this in the constructor because the
-	// height is not set properly there. Though don't do this if we selected a
-	// direct color.
-	if (m_firstResize and selection().index() >= CountLDConfigColors())
-	{
-		int visibleColors = (ui->viewport->height() / g_squareSize) * g_numColumns;
-
-		if (selection() and selection().index() >= visibleColors)
-		{
-			int y = (selection().index() / g_numColumns) * g_squareSize;
-			ui->viewport->verticalScrollBar()->setValue (y);
-		}
-	}
-
-	m_firstResize = false;
-	drawScene();
-}
-
-// =============================================================================
-//
-void ColorSelector::mousePressEvent (QMouseEvent* event)
-{
-	QPointF scenepos = ui->viewport->mapToScene (event->pos());
-
-	int x = (scenepos.x() - (g_squareSize / 2)) / g_squareSize;
-	int y = (scenepos.y() - (g_squareSize / 2)) / g_squareSize;
-	int idx = (y * g_numColumns) + x;
-
-	LDColor col = LDColor::fromIndex (idx);
-
-	if (not col)
-		return;
-
-	setSelection (col);
-	drawScene();
-	drawColorInfo();
-}
-
-// =============================================================================
-//
 void ColorSelector::selectDirectColor (QColor col)
 {
 	int32 idx = (ui->transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000);
--- a/src/colorSelector.h	Thu Sep 04 11:48:05 2014 +0300
+++ b/src/colorSelector.h	Sat Sep 06 15:45:07 2014 +0300
@@ -30,26 +30,21 @@
 	PROPERTY (private,	LDColor,	selection,	setSelection,	STOCK_WRITE)
 
 public:
-	explicit ColorSelector (LDColor defval = null, QWidget* parent = null);
+	explicit ColorSelector (LDColor defaultvalue = null, QWidget* parent = null);
 	virtual ~ColorSelector();
 	static bool selectColor (LDColor& val, LDColor defval = null, QWidget* parent = null);
 
-protected:
-	void mousePressEvent (QMouseEvent* event);
-	void resizeEvent (QResizeEvent* ev);
-
 private:
 	Ui_ColorSelUI*	ui;
-	QGraphicsScene*	m_scene;
+	QMap<int, QPushButton*> m_buttons;
+	QMap<QPushButton*, int> m_buttonsReversed;
 	bool			m_firstResize;
 
-	int numRows() const;
-	int viewportWidth() const;
-	void drawScene();
 	void drawColorInfo();
 	void selectDirectColor (QColor col);
 
 private slots:
+	void colorButtonClicked();
 	void chooseDirectColor();
 	void transparentCheckboxClicked();
 };
--- a/ui/colorsel.ui	Thu Sep 04 11:48:05 2014 +0300
+++ b/ui/colorsel.ui	Sat Sep 06 15:45:07 2014 +0300
@@ -6,25 +6,35 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>402</width>
-    <height>539</height>
+    <width>588</width>
+    <height>404</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Select Color</string>
   </property>
-  <layout class="QVBoxLayout" name="verticalLayout_2">
-   <item>
-    <widget class="QGraphicsView" name="viewport">
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <widget class="QScrollArea" name="definedColors">
      <property name="verticalScrollBarPolicy">
       <enum>Qt::ScrollBarAlwaysOn</enum>
      </property>
      <property name="horizontalScrollBarPolicy">
       <enum>Qt::ScrollBarAlwaysOff</enum>
      </property>
+     <widget class="QWidget" name="definedColorsWidget">
+      <property name="geometry">
+       <rect>
+        <x>0</x>
+        <y>0</y>
+        <width>384</width>
+        <height>287</height>
+       </rect>
+      </property>
+     </widget>
     </widget>
    </item>
-   <item>
+   <item row="1" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QLabel" name="iconLabel">
@@ -76,7 +86,7 @@
      </item>
     </layout>
    </item>
-   <item>
+   <item row="2" column="0">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>

mercurial