Make quick color toolbar use QToolButtons, make color selector's process of making color icons a method and use that for generic color icons

Thu, 09 May 2013 03:58:51 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 09 May 2013 03:58:51 +0300
changeset 186
a130960bb220
parent 185
6fea53f1ffc2
child 187
ee42f4442566

Make quick color toolbar use QToolButtons, make color selector's process of making color icons a method and use that for generic color icons

src/configDialog.cpp file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui.h file | annotate | diff | comparison | revisions
--- a/src/configDialog.cpp	Thu May 09 02:28:39 2013 +0300
+++ b/src/configDialog.cpp	Thu May 09 03:58:51 2013 +0300
@@ -372,37 +372,37 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void ConfigDialog::updateQuickColorList (quickColorMetaEntry* pSel) {
-	for (QListWidgetItem* qItem : quickColorItems)
-		delete qItem;
+void ConfigDialog::updateQuickColorList (quickColorMetaEntry* sel) {
+	for (QListWidgetItem* item : quickColorItems)
+		delete item;
 	
 	quickColorItems.clear ();
 	
 	// Init table items
 	for (quickColorMetaEntry& entry : quickColorMeta) {
-		QListWidgetItem* qItem = new QListWidgetItem;
+		QListWidgetItem* item = new QListWidgetItem;
 		
 		if (entry.bSeparator) {
-			qItem->setText ("--------");
-			qItem->setIcon (getIcon ("empty"));
+			item->setText ("--------");
+			item->setIcon (getIcon ("empty"));
 		} else {
 			color* col = entry.col;
 			
 			if (col == null) {
-				qItem->setText ("[[unknown color]]");
-				qItem->setIcon (getIcon ("error"));
+				item->setText ("[[unknown color]]");
+				item->setIcon (getIcon ("error"));
 			} else {
-				qItem->setText (col->zName);
-				qItem->setIcon (getIcon ("palette"));
+				item->setText (col->zName);
+				item->setIcon (makeColorIcon (col, 16));
 			}
 		}
 		
-		lw_quickColors->addItem (qItem);
-		quickColorItems.push_back (qItem);
+		lw_quickColors->addItem (item);
+		quickColorItems.push_back (item);
 		
-		if (pSel && &entry == pSel) {
-			lw_quickColors->setCurrentItem (qItem);
-			lw_quickColors->scrollToItem (qItem);
+		if (sel && &entry == sel) {
+			lw_quickColors->setCurrentItem (item);
+			lw_quickColors->scrollToItem (item);
 		}
 	}
 }
--- a/src/gldraw.cpp	Thu May 09 02:28:39 2013 +0300
+++ b/src/gldraw.cpp	Thu May 09 03:58:51 2013 +0300
@@ -1041,8 +1041,6 @@
 	setBackground ();
 	updateSelFlash ();
 	
-	drawGLScene ();
-	swapBuffers ();
 	update ();
 }
 
--- a/src/gui.cpp	Thu May 09 02:28:39 2013 +0300
+++ b/src/gui.cpp	Thu May 09 03:58:51 2013 +0300
@@ -45,6 +45,8 @@
 cfg (str, gui_colortoolbar, "16:24:|:0:1:2:3:4:5:6:7");
 extern_cfg (str, io_recentfiles);
 extern_cfg (bool, gl_axes);
+extern_cfg (str, gl_maincolor);
+extern_cfg (float, gl_maincolor_alpha);
 
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -404,11 +406,13 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void ForgeWindow::updateToolBars () {
+	const QSize iconsize (gui_toolbar_iconsize, gui_toolbar_iconsize);
+	
 	for (QToolBar* bar : m_toolBars)
-		bar->setIconSize (QSize (gui_toolbar_iconsize, gui_toolbar_iconsize));
+		bar->setIconSize (iconsize);
 	
 	// Update the quick color toolbar.
-	for (QPushButton* btn : m_colorButtons)
+	for (QToolButton* btn : m_colorButtons)
 		delete btn;
 	
 	m_colorButtons.clear ();
@@ -420,9 +424,9 @@
 		if (entry.bSeparator)
 			m_colorToolBar->addSeparator ();
 		else {
-			QPushButton* colorButton = new QPushButton;
-			colorButton->setAutoFillBackground (true);
-			colorButton->setStyleSheet (fmt ("background-color: %s", entry.col->zColorString.chars()));
+			QToolButton* colorButton = new QToolButton;
+			colorButton->setIcon (makeColorIcon (entry.col, gui_toolbar_iconsize));
+			colorButton->setIconSize (iconsize);
 			colorButton->setToolTip (entry.col->zName);
 			
 			connect (colorButton, SIGNAL (clicked ()), this, SLOT (slot_quickColor ()));
@@ -737,7 +741,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void ForgeWindow::slot_quickColor () {
-	QPushButton* button = static_cast<QPushButton*> (sender ());
+	QToolButton* button = static_cast<QToolButton*> (sender ());
 	color* col = null;
 	
 	for (quickColorMetaEntry entry : m_colorMeta) {
@@ -994,6 +998,26 @@
 }
 
 // =============================================================================
+QIcon makeColorIcon (color* colinfo, const ushort size) {
+	// Create an image object and link a painter to it.
+	QImage img (size, size, QImage::Format_ARGB32);
+	QPainter paint (&img);
+	
+	QColor col = colinfo->qColor;
+	if (colinfo->index () == maincolor) {
+		// Use the user preferences for main color here
+		col = gl_maincolor.value.chars ();
+		col.setAlpha (gl_maincolor_alpha * 255.0f);
+	}
+	
+	// Paint the icon
+	paint.fillRect (QRect (0, 0, size, size), Qt::black);
+	paint.drawPixmap (QRect (1, 1, size - 2, size - 2), getIcon ("checkerboard"), QRect (0, 0, 8, 8));
+	paint.fillRect (QRect (1, 1, size - 2, size - 2), col);
+	return QIcon (QPixmap::fromImage (img));
+}
+
+// =============================================================================
 void makeColorSelector (QComboBox* box) {
 	std::map<short, ulong> counts;
 	
@@ -1010,19 +1034,10 @@
 	box->clear ();
 	ulong row = 0;
 	for (const auto& pair : counts) {
-		const ushort size = 16;
 		color* col = getColor (pair.first);
 		assert (col != null);
 		
-		// Paint an icon for this color
-		QIcon ico;
-		QImage img (size, size, QImage::Format_ARGB32);
-		QPainter paint (&img);
-		paint.fillRect (QRect (0, 0, size, size), Qt::black);
-		paint.drawPixmap (QRect (1, 1, size - 2, size - 2), getIcon ("checkerboard"), QRect (0, 0, 8, 8));
-		paint.fillRect (QRect (1, 1, size - 2, size - 2), QColor (col->qColor));
-		ico = QIcon (QPixmap::fromImage (img));
-		
+		QIcon ico = makeColorIcon (col, 16);
 		box->addItem (ico, fmt ("[%d] %s (%lu object%s)",
 			pair.first, col->zName.chars (), pair.second, PLURAL (pair.second)));
 		box->setItemData (row, pair.first);
--- a/src/gui.h	Thu May 09 02:28:39 2013 +0300
+++ b/src/gui.h	Thu May 09 03:58:51 2013 +0300
@@ -37,6 +37,7 @@
 class color;
 class QSplitter;
 class DelHistory;
+class QToolButton;
 
 // Stuff for dialogs
 #define IMPLEMENT_DIALOG_BUTTONS \
@@ -78,7 +79,7 @@
 // =============================================================================
 typedef struct {
 	color* col;
-	QPushButton* btn;
+	QToolButton* btn;
 	bool bSeparator;
 } quickColorMetaEntry;
 
@@ -169,7 +170,7 @@
 	std::vector<QToolBar*> m_toolBars;
 	std::vector<LDObject*> m_sel;
 	std::vector<quickColorMetaEntry> m_colorMeta;
-	std::vector<QPushButton*> m_colorButtons;
+	std::vector<QToolButton*> m_colorButtons;
 	std::vector<QAction*> m_recentFiles;
 	
 	void createMenuActions ();
@@ -244,6 +245,7 @@
 bool confirm (str msg);
 void critical (str msg);
 QAction* findAction (str name);
+QIcon makeColorIcon (color* colinfo, const ushort size);
 void makeColorSelector (QComboBox* box);
 
 // -----------------------------------------------------------------------------

mercurial