Refactor GuiUtilities

Fri, 11 Nov 2016 13:59:27 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 11 Nov 2016 13:59:27 +0200
changeset 1043
0091a761daf2
parent 1042
b54b78ac41a5
child 1044
b5106ca8705e

Refactor GuiUtilities

src/dialogs/configdialog.h file | annotate | diff | comparison | revisions
src/guiutilities.cpp file | annotate | diff | comparison | revisions
src/guiutilities.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
--- a/src/dialogs/configdialog.h	Fri Nov 11 13:34:51 2016 +0200
+++ b/src/dialogs/configdialog.h	Fri Nov 11 13:59:27 2016 +0200
@@ -68,14 +68,13 @@
 	explicit ConfigDialog (QWidget* parent = nullptr, Tab defaulttab = (Tab) 0, Qt::WindowFlags f = 0);
 	virtual ~ConfigDialog();
 
-	QList<ColorToolbarItem> quickColors;
-
 private:
 	class Ui_ConfigDialog& ui;
 	QList<QListWidgetItem*> quickColorItems;
 	QMap<QPushButton*, QColor> m_buttonColors;
 	ExternalProgramWidgets m_externalProgramWidgets[NumExternalPrograms];
 	class QSettings* m_settings;
+	QVector<ColorToolbarItem> quickColors;
 
 	void applySettings();
 	void addShortcut (QAction* act);
--- a/src/guiutilities.cpp	Fri Nov 11 13:34:51 2016 +0200
+++ b/src/guiutilities.cpp	Fri Nov 11 13:59:27 2016 +0200
@@ -27,91 +27,116 @@
 	QObject (parent),
 	HierarchyElement (parent) {}
 
-QIcon GuiUtilities::makeColorIcon (LDColor ldcolor, int size)
+/*
+ * makeColorIcon
+ *
+ * Creates an icon to represent an LDraw color.
+ */
+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();
+	QImage image {size, size, QImage::Format_ARGB32};
+	QPainter painter {&image};
+	QColor color;
 
-	if (ldcolor == MainColor)
+	if (ldColor == MainColor)
 	{
-		// Use the user preferences for main color here
+		// Use the user preferences for the main color.
 		color = m_config->mainColor();
-		color.setAlphaF (m_config->mainColorAlpha());
+		color.setAlphaF(m_config->mainColorAlpha());
 	}
+	else
+		color = ldColor.faceColor();
 
 	// Paint the icon border
-	painter.fillRect (QRect (0, 0, size, size), ldcolor.edgeColor());
+	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));
+	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));
+	painter.fillRect (QRect {1, 1, size - 2, size - 2}, color);
+	return QIcon {QPixmap::fromImage(image)};
 }
 
+/*
+ * fillUsedColorsToComboBox
+ *
+ * Fills the provided combo box with the colors used in the current document.
+ */
 void GuiUtilities::fillUsedColorsToComboBox (QComboBox* box)
 {
-	QMap<LDColor, int> counts;
+	QMap<LDColor, int> frequencies;
 
-	for (LDObject* obj : currentDocument()->objects())
+	for (LDObject* object : 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;
+		if (object->isColored() and object->color().isValid())
+		{
+			if (not frequencies.contains(object->color()))
+				frequencies[object->color()] = 1;
+			else
+				frequencies[object->color()] += 1;
+		}
 	}
 
 	box->clear();
 	int row = 0;
+	QMapIterator<LDColor, int> iterator {frequencies};
 
-	QMapIterator<LDColor, int> it (counts);
-	while (it.hasNext())
+	while (iterator.hasNext())
 	{
-		it.next();
-		QIcon ico = 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());
+		iterator.next();
+		LDColor color = iterator.key();
+		int frequency = iterator.value();
+		QIcon icon = makeColorIcon(color, 16);
+		box->addItem(icon, format("[%1] %2 (%3 object%4)", color.index(), color.name(), frequency, plural(frequency)));
+		box->setItemData(row, color.index());
 		++row;
 	}
 }
 
+/*
+ * mainColorRepresentation
+ *
+ * Returns the user-preferred appearance for the main color.
+ */
 QColor GuiUtilities::mainColorRepresentation()
 {
-	QColor col (m_config->mainColor());
+	QColor result = {m_config->mainColor()};
 
-	if (not col.isValid())
-		return QColor (0, 0, 0);
-
-	col.setAlpha (m_config->mainColorAlpha() * 255.f);
-	return col;
+	if (result.isValid())
+	{
+		result.setAlpha(m_config->mainColorAlpha() * 255.f);
+		return result;
+	}
+	else
+	{
+		return QColor {0, 0, 0};
+	}
 }
 
-//
-// Returns a list of quick colors based on the configuration entry.
-//
-QList<ColorToolbarItem> GuiUtilities::loadQuickColorList()
+/*
+ * loadQuickColorList
+ *
+ * Returns a list of contents for the color toolbar, based on configuration.
+ */
+QVector<ColorToolbarItem> GuiUtilities::loadQuickColorList()
 {
-	QList<ColorToolbarItem> colors;
+	QVector<ColorToolbarItem> colors;
 
-	for (QString colorname : m_config->quickColorToolbar().split (":"))
+	for (QString colorName : m_config->quickColorToolbar().split(":"))
 	{
-		if (colorname == "|")
+		if (colorName == "|")
+		{
 			colors << ColorToolbarItem::makeSeparator();
+		}
 		else
 		{
-			LDColor color = colorname.toInt();
+			LDColor color = colorName.toInt();
 
 			if (color.isValid())
-				colors << ColorToolbarItem (color, nullptr);
+				colors << ColorToolbarItem {color, nullptr};
 		}
 	}
 
 	return colors;
-}
\ No newline at end of file
+}
--- a/src/guiutilities.h	Fri Nov 11 13:34:51 2016 +0200
+++ b/src/guiutilities.h	Fri Nov 11 13:59:27 2016 +0200
@@ -21,6 +21,11 @@
 #include "main.h"
 #include "mainwindow.h"
 
+/*
+ * GuiUtilities
+ *
+ * Contains generic state-aware GUI functions.
+ */
 class GuiUtilities : public QObject, public HierarchyElement
 {
 	Q_OBJECT
@@ -30,5 +35,5 @@
 	QIcon makeColorIcon (LDColor ldcolor, int size);
 	void fillUsedColorsToComboBox (class QComboBox* box);
 	QColor mainColorRepresentation();
-	QList<ColorToolbarItem> loadQuickColorList();
+	QVector<ColorToolbarItem> loadQuickColorList();
 };
--- a/src/mainwindow.cpp	Fri Nov 11 13:34:51 2016 +0200
+++ b/src/mainwindow.cpp	Fri Nov 11 13:59:27 2016 +0200
@@ -1064,7 +1064,7 @@
 
 // ---------------------------------------------------------------------------------------------------------------------
 //
-void MainWindow::setQuickColors (const QList<ColorToolbarItem>& colors)
+void MainWindow::setQuickColors (const QVector<ColorToolbarItem>& colors)
 {
 	m_quickColors = colors;
 	updateColorToolbar();
@@ -1376,4 +1376,4 @@
 void ColorToolbarItem::setToolButton (QToolButton* value)
 {
 	m_toolButton = value;
-}
\ No newline at end of file
+}
--- a/src/mainwindow.h	Fri Nov 11 13:34:51 2016 +0200
+++ b/src/mainwindow.h	Fri Nov 11 13:59:27 2016 +0200
@@ -44,7 +44,7 @@
 class ColorToolbarItem
 {
 public:
-	ColorToolbarItem (LDColor color, QToolButton* toolButton);
+	ColorToolbarItem (LDColor color = LDColor{}, QToolButton* toolButton = nullptr);
 	LDColor color() const;
 	bool isSeparator() const;
 	void setColor (LDColor color);
@@ -102,7 +102,7 @@
 	void saveShortcuts();
 	void scrollToSelection();
 	const LDObjectList& selectedObjects();
-	void setQuickColors (const QList<ColorToolbarItem>& colors);
+	void setQuickColors (const QVector<ColorToolbarItem> &colors);
 	void spawnContextMenu (const QPoint& position);
 	int suggestInsertPoint();
 	void syncSettings();
@@ -138,7 +138,7 @@
 	Grid* m_grid;
 	MathFunctions* m_mathFunctions;
 	LDObjectList m_sel;
-	QList<ColorToolbarItem>	m_quickColors;
+	QVector<ColorToolbarItem>	m_quickColors;
 	QList<QToolButton*>	m_colorButtons;
 	QList<QAction*> m_recentFiles;
 	class Ui_MainWindow& ui;

mercurial