Added a GuiUtilities class to contain useful non-MainWindow-related GUI functions

Mon, 31 Aug 2015 23:18:17 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 31 Aug 2015 23:18:17 +0300
changeset 974
b2fa5f89798a
parent 973
c1b60c7d5135
child 975
24ba5aa3393f

Added a GuiUtilities class to contain useful non-MainWindow-related GUI functions

CMakeLists.txt file | annotate | diff | comparison | revisions
src/colors.h file | annotate | diff | comparison | revisions
src/configDialog.cpp file | annotate | diff | comparison | revisions
src/dialogs/colorselector.cpp file | annotate | diff | comparison | revisions
src/guiutilities.cpp file | annotate | diff | comparison | revisions
src/guiutilities.h file | annotate | diff | comparison | revisions
src/hierarchyelement.cpp file | annotate | diff | comparison | revisions
src/hierarchyelement.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/toolsets/extprogramtoolset.cpp file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Mon Aug 31 22:40:49 2015 +0300
+++ b/CMakeLists.txt	Mon Aug 31 23:18:17 2015 +0300
@@ -42,6 +42,7 @@
 	src/editHistory.cpp
 	src/glRenderer.cpp
 	src/glCompiler.cpp
+	src/guiutilities.cpp
 	src/hierarchyelement.cpp
 	src/ldDocument.cpp
 	src/ldObject.cpp
@@ -102,6 +103,7 @@
 	src/format.h
 	src/ldpaths.h
 	src/hierarchyelement.h
+	src/guiutilities.h
 	src/dialogs/colorselector.h
 	src/dialogs/ldrawpathdialog.h
 	src/dialogs/newpartdialog.h
--- a/src/colors.h	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/colors.h	Mon Aug 31 23:18:17 2015 +0300
@@ -18,7 +18,7 @@
 
 #pragma once
 #include <QColor>
-#include "main.h"
+#include "basics.h"
 
 class LDColor
 {
--- a/src/configDialog.cpp	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/configDialog.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -38,6 +38,7 @@
 #include "dialogs/colorselector.h"
 #include "glRenderer.h"
 #include "ui_config.h"
+#include "guiutilities.h"
 
 const char* g_extProgPathFilter =
 #ifdef _WIN32
@@ -331,7 +332,7 @@
 			if (color.isValid())
 			{
 				item->setText (color.name());
-				item->setIcon (MakeColorIcon (color, 16));
+				item->setIcon (guiUtilities()->makeColorIcon (color, 16));
 			}
 			else
 			{
--- a/src/dialogs/colorselector.cpp	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/dialogs/colorselector.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -21,9 +21,10 @@
 #include <QMouseEvent>
 #include <QScrollBar>
 #include <QColorDialog>
+#include "../colors.h"
+#include "../guiutilities.h"
 #include "../main.h"
 #include "../mainwindow.h"
-#include "../colors.h"
 #include "../miscallenous.h"
 #include "colorselector.h"
 #include "ui_colorselector.h"
@@ -139,7 +140,7 @@
 
 	ui.colorLabel->setText (format ("%1 - %2", selection().indexString(),
 		(selection().isDirect() ? "<direct color>" : selection().name())));
-	ui.iconLabel->setPixmap (MakeColorIcon (selection(), 16).pixmap (16, 16));
+	ui.iconLabel->setPixmap (guiUtilities()->makeColorIcon (selection(), 16).pixmap (16, 16));
 
 #ifdef TRANSPARENT_DIRECT_COLORS
 	ui.transparentDirectColor->setEnabled (selection().isDirect());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/guiutilities.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -0,0 +1,83 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2015 Teemu 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/>.
+ */
+
+#include <QComboBox>
+#include <QPainter>
+#include "colors.h"
+#include "guiutilities.h"
+#include "ldDocument.h"
+#include "mainwindow.h"
+
+GuiUtilities::GuiUtilities (QObject* parent) :
+	QObject (parent),
+	HierarchyElement (parent) {}
+
+QIcon GuiUtilities::makeColorIcon (LDColor ldcolor, int size)
+{
+	// Create an image object and link a painter to it.
+	QImage img (size, size, QImage::Format_ARGB32);
+	QPainter painter (&img);
+	QColor color = ldcolor.faceColor();
+
+	if (ldcolor == MainColor)
+	{
+		// Use the user preferences for main color here
+		color = m_config->mainColor();
+		color.setAlphaF (m_config->mainColorAlpha());
+	}
+
+	// Paint the icon border
+	painter.fillRect (QRect (0, 0, size, size), ldcolor.edgeColor());
+
+	// Paint the checkerboard background, visible with translucent icons
+	painter.drawPixmap (QRect (1, 1, size - 2, size - 2), GetIcon ("checkerboard"), QRect (0, 0, 8, 8));
+
+	// Paint the color above the checkerboard
+	painter.fillRect (QRect (1, 1, size - 2, size - 2), color);
+	return QIcon (QPixmap::fromImage (img));
+}
+
+void GuiUtilities::fillUsedColorsToComboBox (QComboBox* box)
+{
+	QMap<LDColor, int> counts;
+
+	for (LDObject* obj : CurrentDocument()->objects())
+	{
+		if (not obj->isColored() or not obj->color().isValid())
+			continue;
+
+		if (not counts.contains (obj->color()))
+			counts[obj->color()] = 1;
+		else
+			counts[obj->color()] += 1;
+	}
+
+	box->clear();
+	int row = 0;
+
+	QMapIterator<LDColor, int> it (counts);
+	while (it.hasNext())
+	{
+		it.next();
+		QIcon ico = g_win->guiUtilities()->makeColorIcon (it.key(), 16);
+		box->addItem (ico, format ("[%1] %2 (%3 object%4)",
+			it.key(), it.key().name(), it.value(), plural (it.value())));
+		box->setItemData (row, it.key().index());
+		++row;
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/guiutilities.h	Mon Aug 31 23:18:17 2015 +0300
@@ -0,0 +1,31 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2015 Teemu 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/>.
+ */
+
+#pragma once
+#include "hierarchyelement.h"
+#include "main.h"
+
+class GuiUtilities : public QObject, public HierarchyElement
+{
+	Q_OBJECT
+public:
+	explicit GuiUtilities (QObject* parent);
+
+	QIcon makeColorIcon (LDColor ldcolor, int size);
+	void fillUsedColorsToComboBox (class QComboBox* box);
+};
--- a/src/hierarchyelement.cpp	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/hierarchyelement.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -2,6 +2,7 @@
 #include <QMetaObject>
 #include "hierarchyelement.h"
 #include "mainwindow.h"
+#include "guiutilities.h"
 
 HierarchyElement::HierarchyElement (QObject* parent) :
 	m_window (nullptr),
@@ -24,3 +25,9 @@
 
 	m_config = m_window->configBag();
 }
+
+
+GuiUtilities* HierarchyElement::guiUtilities() const
+{
+	return m_window->guiUtilities();
+}
--- a/src/hierarchyelement.h	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/hierarchyelement.h	Mon Aug 31 23:18:17 2015 +0300
@@ -3,6 +3,7 @@
 
 class MainWindow;
 class ConfigurationValueBag;
+class GuiUtilities;
 
 //
 // Objects that are to take part in the MainWindow's hierarchy multiple-inherit from this class to get a few useful
@@ -13,6 +14,8 @@
 public:
 	HierarchyElement (QObject* parent);
 
+	GuiUtilities* guiUtilities() const;
+
 protected:
 	MainWindow* m_window;
 	ConfigurationValueBag* m_config;
--- a/src/mainwindow.cpp	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/mainwindow.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -52,6 +52,7 @@
 #include "editmodes/abstractEditMode.h"
 #include "toolsets/extprogramtoolset.h"
 #include "toolsets/toolset.h"
+#include "guiutilities.h"
 
 static bool g_isSelectionLocked = false;
 static QMap<QAction*, QKeySequence> g_defaultShortcuts;
@@ -66,6 +67,7 @@
 MainWindow::MainWindow (QWidget* parent, Qt::WindowFlags flags) :
 	QMainWindow (parent, flags),
 	m_configOptions (this),
+	m_guiUtilities (new GuiUtilities (this)),
 	ui (*new Ui_MainWindow),
 	m_externalPrograms (nullptr),
 	m_settings (makeSettings (this))
@@ -289,7 +291,7 @@
 		else
 		{
 			QToolButton* colorButton = new QToolButton;
-			colorButton->setIcon (MakeColorIcon (entry.color(), 16));
+			colorButton->setIcon (m_guiUtilities->makeColorIcon (entry.color(), 16));
 			colorButton->setIconSize (QSize (16, 16));
 			colorButton->setToolTip (entry.color().name());
 
@@ -916,64 +918,6 @@
 
 // ---------------------------------------------------------------------------------------------------------------------
 //
-QIcon MakeColorIcon (LDColor colinfo, const int size)
-{
-	// Create an image object and link a painter to it.
-	QImage img (size, size, QImage::Format_ARGB32);
-	QPainter paint (&img);
-	QColor col = colinfo.faceColor();
-
-	if (colinfo == MainColor)
-	{
-		// Use the user preferences for main color here
-		col = g_win->configBag()->mainColor();
-		col.setAlphaF (g_win->configBag()->mainColorAlpha());
-	}
-
-	// Paint the icon border
-	paint.fillRect (QRect (0, 0, size, size), colinfo.edgeColor());
-
-	// Paint the checkerboard background, visible with translucent icons
-	paint.drawPixmap (QRect (1, 1, size - 2, size - 2), GetIcon ("checkerboard"), QRect (0, 0, 8, 8));
-
-	// Paint the color above the checkerboard
-	paint.fillRect (QRect (1, 1, size - 2, size - 2), col);
-	return QIcon (QPixmap::fromImage (img));
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
-void MakeColorComboBox (QComboBox* box)
-{
-	std::map<LDColor, int> counts;
-
-	for (LDObject* obj : CurrentDocument()->objects())
-	{
-		if (not obj->isColored() or not obj->color().isValid())
-			continue;
-
-		if (counts.find (obj->color()) == counts.end())
-			counts[obj->color()] = 1;
-		else
-			counts[obj->color()]++;
-	}
-
-	box->clear();
-	int row = 0;
-
-	for (const auto& pair : counts)
-	{
-		QIcon ico = MakeColorIcon (pair.first, 16);
-		box->addItem (ico, format ("[%1] %2 (%3 object%4)",
-			pair.first, pair.first.name(), pair.second, plural (pair.second)));
-		box->setItemData (row, pair.first.index());
-
-		++row;
-	}
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
 void MainWindow::updateDocumentList()
 {
 	m_updatingTabs = true;
--- a/src/mainwindow.h	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/mainwindow.h	Mon Aug 31 23:18:17 2015 +0300
@@ -173,6 +173,11 @@
 		return m_externalPrograms;
 	}
 
+	class GuiUtilities* guiUtilities()
+	{
+		return m_guiUtilities;
+	}
+
 public slots:
 	void updatePrimitives();
 	void changeCurrentFile();
@@ -188,6 +193,7 @@
 	struct ToolInfo { QMetaMethod method; Toolset* object; };
 
 	ConfigurationValueBag m_configOptions;
+	class GuiUtilities* m_guiUtilities;
 	GLRenderer*			m_renderer;
 	LDObjectList		m_sel;
 	QList<LDQuickColor>	m_quickColors;
@@ -232,12 +238,6 @@
 //! Displays an error prompt with the given \c message
 void Critical (const QString& message);
 
-//! Makes an icon of \c size x \c size pixels to represent \c colinfo
-QIcon MakeColorIcon (LDColor colinfo, const int size);
-
-//! Fills the given combo-box with color information
-void MakeColorComboBox (QComboBox* box);
-
 //! \returns a QImage from the given raw GL \c data
 QImage GetImageFromScreencap (uchar* data, int w, int h);
 
--- a/src/toolsets/extprogramtoolset.cpp	Mon Aug 31 22:40:49 2015 +0300
+++ b/src/toolsets/extprogramtoolset.cpp	Mon Aug 31 23:18:17 2015 +0300
@@ -26,6 +26,7 @@
 #include <QGridLayout>
 #include <QSettings>
 #include <QFileInfo>
+#include "../guiutilities.h"
 #include "../main.h"
 #include "../miscallenous.h"
 #include "../mainwindow.h"
@@ -456,11 +457,10 @@
 	QDialog* dlg = new QDialog;
 	Ui::IntersectorUI ui;
 	ui.setupUi (dlg);
-
-	MakeColorComboBox (ui.cmb_incol);
-	MakeColorComboBox (ui.cmb_cutcol);
-	ui.cb_repeat->setWhatsThis ("If this is set, " APPNAME " runs Intersector a second time with inverse files to cut the "
-								" cutter group with the input group. Both groups are cut by the intersection.");
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_incol);
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_cutcol);
+	ui.cb_repeat->setWhatsThis ("If this is set, " APPNAME " runs Intersector a second time with inverse files to cut "
+								" the cutter group with the input group. Both groups are cut by the intersection.");
 	ui.cb_edges->setWhatsThis ("Makes " APPNAME " try run Isecalc to create edgelines for the intersection.");
 
 	LDColor inCol, cutCol;
@@ -556,8 +556,8 @@
 	QDialog* dlg = new QDialog;
 	Ui::CovererUI ui;
 	ui.setupUi (dlg);
-	MakeColorComboBox (ui.cmb_col1);
-	MakeColorComboBox (ui.cmb_col2);
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col1);
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col2);
 
 	LDColor in1Col, in2Col;
 
@@ -621,8 +621,8 @@
 	QDialog* dlg = new QDialog;
 	ui.setupUi (dlg);
 
-	MakeColorComboBox (ui.cmb_col1);
-	MakeColorComboBox (ui.cmb_col2);
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col1);
+	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col2);
 
 	LDColor in1Col, in2Col;
 

mercurial