Refactored the color toolbar editing into a new model/view system

Wed, 26 Dec 2018 16:38:38 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Dec 2018 16:38:38 +0200
changeset 1432
4cc687851fbb
parent 1431
2e0e2c696605
child 1433
bd3a9e237ef5

Refactored the color toolbar editing into a new model/view system

CMakeLists.txt file | annotate | diff | comparison | revisions
src/colors.h file | annotate | diff | comparison | revisions
src/configurationoptions.txt file | annotate | diff | comparison | revisions
src/dialogs/colortoolbareditor.cpp file | annotate | diff | comparison | revisions
src/dialogs/colortoolbareditor.h file | annotate | diff | comparison | revisions
src/dialogs/colortoolbareditor.ui file | annotate | diff | comparison | revisions
src/dialogs/configdialog.cpp file | annotate | diff | comparison | revisions
src/dialogs/configdialog.h file | annotate | diff | comparison | revisions
src/dialogs/configdialog.ui 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
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Fri Dec 21 21:43:06 2018 +0200
+++ b/CMakeLists.txt	Wed Dec 26 16:38:38 2018 +0200
@@ -62,6 +62,7 @@
 	src/version.cpp
 	src/algorithms/geometry.cpp
 	src/algorithms/invert.cpp
+	src/dialogs/colortoolbareditor.cpp
 	src/dialogs/colorselector.cpp
 	src/dialogs/configdialog.cpp
 	src/dialogs/circularprimitiveeditor.cpp
@@ -137,6 +138,7 @@
 	src/algorithms/geometry.h
 	src/algorithms/invert.h
 	src/dialogs/colorselector.h
+	src/dialogs/colortoolbareditor.h
 	src/dialogs/configdialog.h
 	src/dialogs/circularprimitiveeditor.h
 	src/dialogs/externalprogrampathdialog.h
@@ -191,6 +193,7 @@
 set (LDFORGE_FORMS
 	src/dialogs/aboutdialog.ui
 	src/dialogs/colorselector.ui
+	src/dialogs/colortoolbareditor.ui
 	src/dialogs/configdialog.ui
 	src/dialogs/covererdialog.ui
 	src/dialogs/circularprimitiveeditor.ui
--- a/src/colors.h	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/colors.h	Wed Dec 26 16:38:38 2018 +0200
@@ -118,3 +118,9 @@
 
 static const LDColor MainColor {16};
 static const LDColor EdgeColor {24};
+static const QVector<LDColor> defaultColorToolbar {
+	LDColor(4), LDColor(25), LDColor(14), LDColor(27),
+	LDColor(2), LDColor(3), LDColor(11), LDColor(1), LDColor(22),
+	LDColor::nullColor,
+	LDColor(0), LDColor(72), LDColor(71), LDColor(15)
+};
--- a/src/configurationoptions.txt	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/configurationoptions.txt	Wed Dec 26 16:38:38 2018 +0200
@@ -22,7 +22,7 @@
 option RotationPointType = 0
 option CustomRotationPoint = Vertex {}
 option ColorizeObjectsList = true
-option QuickColorToolbar = "4:25:14:27:2:3:11:1:22:|:0:72:71:15"
+option QuickColorToolbar = QVector<LDColor> {defaultColorToolbar}
 option ListImplicitFiles = false
 option RecentFiles = QStringList {}
 option TryDownloadMissingFiles = false
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/colortoolbareditor.cpp	Wed Dec 26 16:38:38 2018 +0200
@@ -0,0 +1,298 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2018 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 <QListView>
+#include "colortoolbareditor.h"
+#include "ui_colortoolbareditor.h"
+#include "guiutilities.h"
+#include "colorselector.h"
+
+/*
+ * Constructs a new color toolbar editor widget.
+ */
+ColorToolbarEditor::ColorToolbarEditor(QWidget *parent) :
+	QWidget {parent},
+	colorToolbar {config::quickColorToolbar()},
+	model {colorToolbar},
+	ui {*new Ui_ColorToolbarEditor}
+{
+	ui.setupUi(this);
+	ui.colorToolbarView->setModel(&model);
+	connect(ui.quickColor_add, &QAbstractButton::clicked, this, &ColorToolbarEditor::addColor);
+	connect(ui.quickColor_remove, &QAbstractButton::clicked, this, &ColorToolbarEditor::removeColor);
+	connect(ui.quickColor_edit, &QAbstractButton::clicked, this, &ColorToolbarEditor::editColor);
+	connect(ui.quickColor_addSep, &QAbstractButton::clicked, this, &ColorToolbarEditor::addSeparator);
+	connect(ui.quickColor_moveUp, &QAbstractButton::clicked, this, &ColorToolbarEditor::moveColor);
+	connect(ui.quickColor_moveDown, &QAbstractButton::clicked, this, &ColorToolbarEditor::moveColor);
+	connect(ui.quickColor_clear, &QAbstractButton::clicked, this, &ColorToolbarEditor::clearColors);
+}
+
+/*
+ * Destroys the color toolbar editor widget.
+ */
+ColorToolbarEditor::~ColorToolbarEditor()
+{
+	delete &ui;
+}
+
+/*
+ * Returns where a new color toolbar entry should be inserted to.
+ * If the user has selected an entry, the new entry is placed below it.
+ * Otherwise it goes to the end of the toolbar.
+ */
+int ColorToolbarEditor::newItemPosition()
+{
+	QModelIndexList const indexes = ui.colorToolbarView->selectionModel()->selectedIndexes();
+
+	if (indexes.size() > 0)
+		return indexes.last().row() + 1;
+	else
+		return model.rowCount();
+}
+
+/*
+ * Adds a new color toolbar entry
+ */
+void ColorToolbarEditor::addColor()
+{
+	LDColor value;
+
+	if (not ColorSelector::selectColor (this, value, LDColor::nullColor))
+		return;
+
+	int const position = newItemPosition();
+	model.insertRow(position);
+	model.setColorAt(model.index(position), value);
+}
+
+/*
+ * Changes an existing color toolbar entry
+ */
+void ColorToolbarEditor::editColor()
+{
+	QModelIndexList const indexes = ui.colorToolbarView->selectionModel()->selectedIndexes();
+
+	if (indexes.size() > 0)
+	{
+		QModelIndex const& position = indexes[0];
+		LDColor const color = model.colorAt(position);
+
+		if (color == LDColor::nullColor)
+			return; // don't color separators
+
+		LDColor newColor;
+
+		if (ColorSelector::selectColor(this, newColor, color))
+			model.setColorAt(position, newColor);
+	}
+}
+
+//
+// Remove a quick color
+//
+void ColorToolbarEditor::removeColor()
+{
+	QModelIndexList const selection = ui.colorToolbarView->selectionModel()->selectedIndexes();
+
+	if (not selection.empty())
+		model.removeRow(selection[0].row());
+}
+
+//
+// Move a quick color up/down
+//
+void ColorToolbarEditor::moveColor()
+{
+	bool const up = (static_cast<QPushButton*>(sender()) == ui.quickColor_moveUp);
+	QModelIndexList const selection = ui.colorToolbarView->selectionModel()->selectedIndexes();
+
+	if (not selection.empty())
+		model.moveColor(selection[0], up);
+}
+
+/*
+ * Adds a new separator into the color toolbar.
+ */
+void ColorToolbarEditor::addSeparator()
+{
+	int const position = newItemPosition();
+	model.insertRow(position);
+	model.setColorAt(model.index(position), LDColor::nullColor);
+}
+
+/*
+ * Clears the color toolbar.
+ */
+void ColorToolbarEditor::clearColors()
+{
+	model.removeRows(0, model.rowCount());
+}
+
+/*
+ * Saves the changes done in the color toolbar editor
+ */
+void ColorToolbarEditor::saveChanges()
+{
+	config::setQuickColorToolbar(colorToolbar);
+}
+
+/*
+ * Constructs a new model for editing the color toolbar.
+ */
+ColorToolbarModel::ColorToolbarModel(QVector<LDColor> &colorToolbar) :
+	colorToolbar {colorToolbar} {}
+
+/*
+ * Returns the amount of entries in the color toolbar.
+ */
+int ColorToolbarModel::rowCount(QModelIndex const&) const
+{
+	return colorToolbar.size();
+}
+
+/*
+ * Returns data of the color toolbar.
+ */
+QVariant ColorToolbarModel::data(const QModelIndex& index, int role) const
+{
+	int const row = index.row();
+
+	if (row < 0 or row >= rowCount())
+		return {};
+
+	LDColor const color = colorToolbar[row];
+
+	switch(role)
+	{
+	case Qt::DecorationRole:
+		if (color == LDColor::nullColor)
+			return {};
+		else
+			return makeColorIcon(color, 16);
+
+	case Qt::DisplayRole:
+		if (color == LDColor::nullColor)
+			return "";
+		else
+			return color.name();
+
+	default:
+		return {};
+	}
+}
+
+/*
+ * Returns a color in the color toolbar.
+ */
+LDColor ColorToolbarModel::colorAt(QModelIndex const& index) const
+{
+	if (isValidIndex(index))
+		return colorToolbar[index.row()];
+	else
+		return {};
+}
+
+/*
+ * Changes a color in the color toolbar.
+ */
+void ColorToolbarModel::setColorAt(QModelIndex const& index, LDColor newColor)
+{
+	if (isValidIndex(index))
+	{
+		colorToolbar[index.row()] = newColor;
+		emit dataChanged(index, index);
+	}
+}
+
+/*
+ * Moves a color up or down in the color toolbar.
+ */
+void ColorToolbarModel::moveColor(const QModelIndex &index, bool const up)
+{
+	int const position = index.row();
+	int const destination = position + (up ? -1 : 1);
+	int const end = destination + (up ? 0 : 1);
+
+	if (isValidRow(position) and isValidRow(destination))
+	{
+		emit beginMoveRows({}, position, position, {}, end);
+		qSwap(colorToolbar[destination], colorToolbar[position]);
+		emit endMoveRows();
+	}
+}
+
+/*
+ * Inserts entries in the color toolbar.
+ */
+bool ColorToolbarModel::insertRows(int row, int count, QModelIndex const&)
+{
+	if (row >= 0 and row <= colorToolbar.size())
+	{
+		emit beginInsertRows({}, row, count);
+		while (count > 0)
+		{
+			colorToolbar.insert(row, {});
+			count -= 1;
+			row += 1;
+		}
+		emit endInsertRows();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+/*
+ * Removes entries in the color toolbar.
+ */
+bool ColorToolbarModel::removeRows(int row, int count, QModelIndex const&)
+{
+	if (row >= 0 and row + count <= colorToolbar.size())
+	{
+		emit beginRemoveRows({}, row, row);
+		while (count > 0)
+		{
+			colorToolbar.removeAt(row);
+			count -= 1;
+		}
+		emit endRemoveRows();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+/*
+ * Returns whether or not the specified index is valid in the color toolbar.
+ */
+bool ColorToolbarModel::isValidIndex(const QModelIndex &index) const
+{
+	return isValidRow(index.row());
+}
+
+/*
+ * Returns whether or not the specified row is valid in the color toolbar.
+ */
+bool ColorToolbarModel::isValidRow(int row) const
+{
+	return row >= 0 and row < colorToolbar.size();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/colortoolbareditor.h	Wed Dec 26 16:38:38 2018 +0200
@@ -0,0 +1,66 @@
+/*
+ *  LDForge: LDraw parts authoring CAD
+ *  Copyright (C) 2013 - 2018 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 <QWidget>
+#include <QAbstractListModel>
+#include "colors.h"
+
+class ColorToolbarModel : public QAbstractListModel
+{
+	Q_OBJECT
+
+public:
+	ColorToolbarModel(QVector<LDColor>& colorToolbar);
+	int rowCount(QModelIndex const& = {}) const override;
+	QVariant data(QModelIndex const& index, int role) const override;
+	LDColor colorAt(QModelIndex const& index) const;
+	void setColorAt(QModelIndex const& index, LDColor newColor);
+	bool isValidIndex(QModelIndex const& index) const;
+	void moveColor(const QModelIndex &index, const bool up);
+	bool insertRows(int row, int count, QModelIndex const& = {}) override;
+	bool removeRows(int row, int count, QModelIndex const& = {}) override;
+
+	bool isValidRow(int row) const;
+private:
+	QVector<LDColor>& colorToolbar;
+};
+
+class ColorToolbarEditor : public QWidget
+{
+	Q_OBJECT
+
+public:
+	explicit ColorToolbarEditor(QWidget *parent = nullptr);
+	~ColorToolbarEditor();
+
+public slots:
+	void addColor();
+	void editColor();
+	void removeColor();
+	void moveColor();
+	void addSeparator();
+	void clearColors();
+	void saveChanges();
+
+private:
+	QVector<LDColor> colorToolbar;
+	ColorToolbarModel model;
+	class Ui_ColorToolbarEditor &ui;
+	int newItemPosition();
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dialogs/colortoolbareditor.ui	Wed Dec 26 16:38:38 2018 +0200
@@ -0,0 +1,149 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>ColorToolbarEditor</class>
+ <widget class="QWidget" name="ColorToolbarEditor">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>713</width>
+    <height>478</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QGroupBox" name="groupBox_3">
+     <property name="whatsThis">
+      <string>Here you can alter the layout of the quick colors toolbar. Use the controls to add, remove or edit the colors used. You can also add separators in between colors.
+
+Usually this contains MainColor, EdgeColor and some auxiliary colors used to group objects.</string>
+     </property>
+     <property name="title">
+      <string>Color Toolbar</string>
+     </property>
+     <layout class="QHBoxLayout" name="horizontalLayout_6">
+      <item>
+       <widget class="QListView" name="colorToolbarView">
+        <property name="verticalScrollBarPolicy">
+         <enum>Qt::ScrollBarAsNeeded</enum>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <layout class="QVBoxLayout" name="verticalLayout_5">
+        <item>
+         <widget class="QPushButton" name="quickColor_add">
+          <property name="text">
+           <string>Add Color</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/palette.png</normaloff>:/icons/palette.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_addSep">
+          <property name="text">
+           <string>Add Separator</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_edit">
+          <property name="text">
+           <string>Edit</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/mode-draw.png</normaloff>:/icons/mode-draw.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="Line" name="line_2">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_moveUp">
+          <property name="text">
+           <string>Move Up</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/arrow-up.png</normaloff>:/icons/arrow-up.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_moveDown">
+          <property name="text">
+           <string>Move Down</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/arrow-down.png</normaloff>:/icons/arrow-down.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="Line" name="line">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_remove">
+          <property name="text">
+           <string>Remove</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/delete.png</normaloff>:/icons/delete.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="quickColor_clear">
+          <property name="text">
+           <string>Clear List</string>
+          </property>
+          <property name="icon">
+           <iconset resource="../../ldforge.qrc">
+            <normaloff>:/icons/delete.png</normaloff>:/icons/delete.png</iconset>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="verticalSpacer_3">
+          <property name="orientation">
+           <enum>Qt::Vertical</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>20</width>
+            <height>40</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+       </layout>
+      </item>
+     </layout>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources>
+  <include location="../../ldforge.qrc"/>
+  <include location="../../ldforge.qrc"/>
+ </resources>
+ <connections/>
+</ui>
--- a/src/dialogs/configdialog.cpp	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/dialogs/configdialog.cpp	Wed Dec 26 16:38:38 2018 +0200
@@ -129,20 +129,11 @@
 
 	ui.shortcutsList->setSortingEnabled (true);
 	ui.shortcutsList->sortItems();
-	quickColors = guiUtilities()->loadQuickColorList();
-	updateQuickColorList();
 	initExtProgs();
 	selectPage (defaulttab);
 	connect (ui.shortcut_set, SIGNAL (clicked()), this, SLOT (slot_setShortcut()));
 	connect (ui.shortcut_reset, SIGNAL (clicked()), this, SLOT (slot_resetShortcut()));
 	connect (ui.shortcut_clear, SIGNAL (clicked()), this, SLOT (slot_clearShortcut()));
-	connect (ui.quickColor_add, SIGNAL (clicked()), this, SLOT (slot_setColor()));
-	connect (ui.quickColor_remove, SIGNAL (clicked()), this, SLOT (slot_delColor()));
-	connect (ui.quickColor_edit, SIGNAL (clicked()), this, SLOT (slot_setColor()));
-	connect (ui.quickColor_addSep, SIGNAL (clicked()), this, SLOT (slot_addColorSeparator()));
-	connect (ui.quickColor_moveUp, SIGNAL (clicked()), this, SLOT (slot_moveColor()));
-	connect (ui.quickColor_moveDown, SIGNAL (clicked()), this, SLOT (slot_moveColor()));
-	connect (ui.quickColor_clear, SIGNAL (clicked()), this, SLOT (slot_clearColors()));
 	connect (ui.findDownloadPath, SIGNAL (clicked (bool)), this, SLOT (slot_findDownloadFolder()));
 	connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)),
 		this, SLOT (buttonClicked (QAbstractButton*)));
@@ -318,9 +309,7 @@
 		settingsObject().setValue(confname, value);
 	});
 
-	// Rebuild the quick color toolbar
-	m_window->setQuickColors (quickColors);
-	config::setQuickColorToolbar (quickColorString());
+	ui.colorToolbarEditor->saveChanges();
 	config::setLibraries(this->libraries);
 
 	// Ext program settings
@@ -343,10 +332,7 @@
 	}
 
 	settingsObject().sync();
-	m_documents->loadLogoedStuds();
-	m_window->renderer()->setBackground();
-	m_window->doFullRefresh();
-	m_window->updateDocumentList();
+	emit settingsChanged();
 }
 
 //
@@ -372,152 +358,6 @@
 }
 
 //
-// Update the list of color toolbar items in the quick color tab.
-//
-void ConfigDialog::updateQuickColorList (ColorToolbarItem* sel)
-{
-	for (QListWidgetItem * item : quickColorItems)
-		delete item;
-
-	quickColorItems.clear();
-
-	// Init table items
-	for (ColorToolbarItem& entry : quickColors)
-	{
-		QListWidgetItem* item = new QListWidgetItem;
-
-		if (entry.isSeparator())
-		{
-			item->setText ("<hr />");
-			item->setIcon (MainWindow::getIcon ("empty"));
-		}
-		else
-		{
-			LDColor color = entry.color();
-
-			if (color.isValid())
-			{
-				item->setText (color.name());
-				item->setIcon (makeColorIcon (color, 16));
-			}
-			else
-			{
-				item->setText ("[[unknown color]]");
-				item->setIcon (MainWindow::getIcon ("error"));
-			}
-		}
-
-		ui.quickColorList->addItem (item);
-		quickColorItems << item;
-
-		if (sel and &entry == sel)
-		{
-			ui.quickColorList->setCurrentItem (item);
-			ui.quickColorList->scrollToItem (item);
-		}
-	}
-}
-
-//
-// Quick colors: add or edit button was clicked.
-//
-void ConfigDialog::slot_setColor()
-{
-	ColorToolbarItem* entry = nullptr;
-	QListWidgetItem* item = nullptr;
-	const bool isNew = static_cast<QPushButton*> (sender()) == ui.quickColor_add;
-
-	if (not isNew)
-	{
-		item = getSelectedQuickColor();
-
-		if (not item)
-			return;
-
-		int i = getItemRow (item, quickColorItems);
-		entry = &quickColors[i];
-
-		if (entry->isSeparator() == true)
-			return; // don't color separators
-	}
-
-	LDColor defaultValue = entry ? entry->color() : LDColor::nullColor;
-	LDColor value;
-
-	if (not ColorSelector::selectColor (this, value, defaultValue))
-		return;
-
-	if (entry)
-	{
-		entry->setColor (value);
-	}
-	else
-	{
-		ColorToolbarItem newentry {value};
-		item = getSelectedQuickColor();
-		int idx = (item) ? getItemRow (item, quickColorItems) + 1 : countof(quickColorItems);
-		quickColors.insert(idx, newentry);
-		entry = &quickColors[idx];
-	}
-
-	updateQuickColorList (entry);
-}
-
-//
-// Remove a quick color
-//
-void ConfigDialog::slot_delColor()
-{
-	if (ui.quickColorList->selectedItems().isEmpty())
-		return;
-
-	QListWidgetItem* item = ui.quickColorList->selectedItems() [0];
-	quickColors.removeAt (getItemRow (item, quickColorItems));
-	updateQuickColorList();
-}
-
-//
-// Move a quick color up/down
-//
-void ConfigDialog::slot_moveColor()
-{
-	const bool up = (static_cast<QPushButton*> (sender()) == ui.quickColor_moveUp);
-
-	if (ui.quickColorList->selectedItems().isEmpty())
-		return;
-
-	QListWidgetItem* item = ui.quickColorList->selectedItems() [0];
-	int idx = getItemRow (item, quickColorItems);
-	int dest = up ? (idx - 1) : (idx + 1);
-
-	if (dest < 0 or dest >= countof(quickColorItems))
-		return; // destination out of bounds
-
-	qSwap (quickColors[dest], quickColors[idx]);
-	updateQuickColorList (&quickColors[dest]);
-}
-
-//
-//
-// Add a separator to quick colors
-//
-void ConfigDialog::slot_addColorSeparator()
-{
-	quickColors << ColorToolbarItem::makeSeparator();
-	updateQuickColorList (&quickColors[countof(quickColors) - 1]);
-}
-
-//
-//
-// Clear all quick colors
-//
-void ConfigDialog::slot_clearColors()
-{
-	quickColors.clear();
-	updateQuickColorList();
-}
-
-//
 //
 void ConfigDialog::setButtonColor()
 {
@@ -569,17 +409,6 @@
 }
 
 //
-// Which quick color is currently selected?
-//
-QListWidgetItem* ConfigDialog::getSelectedQuickColor()
-{
-	if (ui.quickColorList->selectedItems().isEmpty())
-		return nullptr;
-
-	return ui.quickColorList->selectedItems() [0];
-}
-
-//
 // Get the list of shortcuts selected
 //
 QVector<ShortcutListItem*> ConfigDialog::getShortcutSelection()
@@ -691,27 +520,6 @@
 }
 
 //
-// Gets the configuration string of the quick color toolbar
-//
-QString ConfigDialog::quickColorString()
-{
-	QString val;
-
-	for (const ColorToolbarItem& entry : quickColors)
-	{
-		if (not val.isEmpty())
-			val += ':';
-
-		if (entry.isSeparator())
-			val += '|';
-		else
-			val += format ("%1", entry.color().index());
-	}
-
-	return val;
-}
-
-//
 //
 KeySequenceDialog::KeySequenceDialog (QKeySequence seq, QWidget* parent, Qt::WindowFlags f) :
 	QDialog (parent, f), seq (seq)
--- a/src/dialogs/configdialog.h	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/dialogs/configdialog.h	Wed Dec 26 16:38:38 2018 +0200
@@ -67,23 +67,22 @@
 
 	static const char* const externalProgramPathFilter;
 
+signals:
+	void settingsChanged();
+
 private:
 	class Ui_ConfigDialog& ui;
 	QVector<QListWidgetItem*> quickColorItems;
 	QMap<QPushButton*, QColor> m_buttonColors;
 	ExternalProgramWidgets m_externalProgramWidgets[NumExternalPrograms];
-	QVector<ColorToolbarItem> quickColors;
 	class LibrariesModel* librariesModel;
 	Libraries libraries;
 
 	void applySettings();
 	void addShortcut (QAction* act);
 	void setButtonBackground (QPushButton* button, QString value);
-	void updateQuickColorList (ColorToolbarItem* sel = nullptr);
 	void setShortcutText (ShortcutListItem* item);
 	int getItemRow (QListWidgetItem* item, QVector<QListWidgetItem*>& haystack);
-	QString quickColorString();
-	QListWidgetItem* getSelectedQuickColor();
 	QVector<ShortcutListItem*> getShortcutSelection();
 	void initExtProgs();
 	void applyToWidgetOptions (std::function<void (QWidget*, QString)> func);
@@ -93,11 +92,6 @@
 	void slot_setShortcut();
 	void slot_resetShortcut();
 	void slot_clearShortcut();
-	void slot_setColor();
-	void slot_delColor();
-	void slot_addColorSeparator();
-	void slot_moveColor();
-	void slot_clearColors();
 	void slot_setExtProgPath();
 	void slot_findDownloadFolder();
 	void buttonClicked (QAbstractButton* button);
--- a/src/dialogs/configdialog.ui	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/dialogs/configdialog.ui	Wed Dec 26 16:38:38 2018 +0200
@@ -84,7 +84,7 @@
      <item>
       <widget class="QStackedWidget" name="m_pages">
        <property name="currentIndex">
-        <number>0</number>
+        <number>5</number>
        </property>
        <widget class="QWidget" name="page_3">
         <layout class="QVBoxLayout" name="verticalLayout_13">
@@ -677,129 +677,7 @@
        <widget class="QWidget" name="page_6">
         <layout class="QVBoxLayout" name="verticalLayout_16">
          <item>
-          <widget class="QGroupBox" name="groupBox_3">
-           <property name="whatsThis">
-            <string>Here you can alter the layout of the quick colors toolbar. Use the controls to add, remove or edit the colors used. You can also add separators in between colors.
-
-Usually this contains MainColor, EdgeColor and some auxiliary colors used to group objects.</string>
-           </property>
-           <property name="title">
-            <string>Color Toolbar</string>
-           </property>
-           <layout class="QHBoxLayout" name="horizontalLayout_6">
-            <item>
-             <widget class="QListWidget" name="quickColorList">
-              <property name="verticalScrollBarPolicy">
-               <enum>Qt::ScrollBarAsNeeded</enum>
-              </property>
-             </widget>
-            </item>
-            <item>
-             <layout class="QVBoxLayout" name="verticalLayout_5">
-              <item>
-               <widget class="QPushButton" name="quickColor_add">
-                <property name="text">
-                 <string>Add Color</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/palette.png</normaloff>:/icons/palette.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_addSep">
-                <property name="text">
-                 <string>Add Separator</string>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_edit">
-                <property name="text">
-                 <string>Edit</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/mode-draw.png</normaloff>:/icons/mode-draw.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="Line" name="line_2">
-                <property name="orientation">
-                 <enum>Qt::Horizontal</enum>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_moveUp">
-                <property name="text">
-                 <string>Move Up</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/arrow-up.png</normaloff>:/icons/arrow-up.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_moveDown">
-                <property name="text">
-                 <string>Move Down</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/arrow-down.png</normaloff>:/icons/arrow-down.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="Line" name="line">
-                <property name="orientation">
-                 <enum>Qt::Horizontal</enum>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_remove">
-                <property name="text">
-                 <string>Remove</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/delete.png</normaloff>:/icons/delete.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <widget class="QPushButton" name="quickColor_clear">
-                <property name="text">
-                 <string>Clear List</string>
-                </property>
-                <property name="icon">
-                 <iconset resource="../../ldforge.qrc">
-                  <normaloff>:/icons/delete.png</normaloff>:/icons/delete.png</iconset>
-                </property>
-               </widget>
-              </item>
-              <item>
-               <spacer name="verticalSpacer_3">
-                <property name="orientation">
-                 <enum>Qt::Vertical</enum>
-                </property>
-                <property name="sizeHint" stdset="0">
-                 <size>
-                  <width>20</width>
-                  <height>40</height>
-                 </size>
-                </property>
-               </spacer>
-              </item>
-             </layout>
-            </item>
-           </layout>
-          </widget>
+          <widget class="ColorToolbarEditor" name="colorToolbarEditor" native="true"/>
          </item>
         </layout>
        </widget>
@@ -1170,6 +1048,14 @@
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>ColorToolbarEditor</class>
+   <extends>QWidget</extends>
+   <header>dialogs/colortoolbareditor.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources>
   <include location="../../ldforge.qrc"/>
   <include location="../../ldforge.qrc"/>
--- a/src/guiutilities.cpp	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/guiutilities.cpp	Wed Dec 26 16:38:38 2018 +0200
@@ -134,26 +134,9 @@
  *
  * Returns a list of contents for the color toolbar, based on configuration.
  */
-QVector<ColorToolbarItem> GuiUtilities::loadQuickColorList()
+QVector<LDColor> GuiUtilities::loadQuickColorList()
 {
-	QVector<ColorToolbarItem> colors;
-
-	for (QString colorName : config::quickColorToolbar().split(":"))
-	{
-		if (colorName == "|")
-		{
-			colors << ColorToolbarItem::makeSeparator();
-		}
-		else
-		{
-			LDColor color {colorName.toInt()};
-
-			if (color.isValid())
-				colors.append(ColorToolbarItem {color});
-		}
-	}
-
-	return colors;
+    return config::quickColorToolbar();
 }
 
 
--- a/src/guiutilities.h	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/guiutilities.h	Wed Dec 26 16:38:38 2018 +0200
@@ -34,7 +34,7 @@
 
 	void fillUsedColorsToComboBox (class QComboBox* box);
 	QColor mainColorRepresentation();
-	QVector<ColorToolbarItem> loadQuickColorList();
+    QVector<LDColor> loadQuickColorList();
 };
 
 QIcon makeColorIcon(LDColor color, int size);
--- a/src/mainwindow.cpp	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/mainwindow.cpp	Wed Dec 26 16:38:38 2018 +0200
@@ -89,7 +89,6 @@
 	connect(m_documents, &DocumentManager::documentCreated, this, &MainWindow::newDocument);
 	connect(m_documents, SIGNAL(documentClosed(LDDocument*)), this, SLOT(documentClosed(LDDocument*)));
 
-	m_quickColors = m_guiUtilities->loadQuickColorList();
 	updateActions();
 
 	// Connect all actions and save default sequences
@@ -254,21 +253,22 @@
 	ui.toolBarColors->addAction (ui.actionUncolor);
 	ui.toolBarColors->addSeparator();
 
-	for (ColorToolbarItem& entry : m_quickColors)
+	for (LDColor entry : config::quickColorToolbar())
 	{
-		if (entry.isSeparator())
+		if (entry == LDColor::nullColor)
 		{
+			// Null color is used as a separator
 			ui.toolBarColors->addSeparator();
 		}
 		else
 		{
 			QToolButton* colorButton = new QToolButton {this};
-			colorButton->setIcon(makeColorIcon(entry.color(), 16));
+			colorButton->setIcon(makeColorIcon(entry, 16));
 			colorButton->setIconSize({16, 16});
-			colorButton->setToolTip(entry.color().name());
+			colorButton->setToolTip(entry.name());
 			colorButton->setStatusTip(format(
 				tr("Changes the color of selected objects to %1"),
-				entry.color().name()
+				entry.name()
 			));
 			ui.toolBarColors->addWidget (colorButton);
 			m_colorButtons << colorButton;
@@ -279,7 +279,7 @@
 					for (LDObject* object : selectedObjects())
 					{
 						if (object->isColored())
-							object->setColor(entry.color());
+							object->setColor(entry);
 					}
 
 					endAction();
@@ -728,14 +728,6 @@
 
 // ---------------------------------------------------------------------------------------------------------------------
 //
-void MainWindow::setQuickColors (const QVector<ColorToolbarItem>& colors)
-{
-	m_quickColors = colors;
-	updateColorToolbar();
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
 void MainWindow::closeTab (int tabindex)
 {
 	auto iterator = m_documents->findDocumentByName(m_tabs->tabData (tabindex).toString());
@@ -825,6 +817,15 @@
 		openDocumentForEditing(document);
 }
 
+void MainWindow::settingsChanged()
+{
+	m_documents->loadLogoedStuds();
+	updateColorToolbar();
+	renderer()->setBackground();
+	doFullRefresh();
+	updateDocumentList();
+}
+
 void MainWindow::openDocumentForEditing(LDDocument* document)
 {
 	if (document->isFrozen())
@@ -995,28 +996,3 @@
 {
 	m_selections[m_currentDocument]->select(selection, QItemSelectionModel::ClearAndSelect);
 }
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
-ColorToolbarItem::ColorToolbarItem(LDColor color) :
-	m_color (color) {}
-
-ColorToolbarItem ColorToolbarItem::makeSeparator()
-{
-	return {LDColor::nullColor};
-}
-
-bool ColorToolbarItem::isSeparator() const
-{
-	return color() == LDColor::nullColor;
-}
-
-LDColor ColorToolbarItem::color() const
-{
-	return m_color;
-}
-
-void ColorToolbarItem::setColor (LDColor color)
-{
-	m_color = color;
-}
--- a/src/mainwindow.h	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/mainwindow.h	Wed Dec 26 16:38:38 2018 +0200
@@ -35,20 +35,6 @@
 class DocumentManager;
 class LDDocument;
 
-class ColorToolbarItem
-{
-public:
-	ColorToolbarItem (LDColor color = {});
-	LDColor color() const;
-	bool isSeparator() const;
-	void setColor (LDColor color);
-
-	static ColorToolbarItem makeSeparator();
-
-private:
-	LDColor m_color;
-};
-
 // LDForge's main GUI class.
 class MainWindow : public QMainWindow
 {
@@ -88,7 +74,6 @@
 	void select(const QModelIndex& objectIndex);
 	QModelIndexList selectedIndexes() const;
 	QSet<LDObject*> selectedObjects() const;
-	void setQuickColors (const QVector<ColorToolbarItem> &colors);
 	void spawnContextMenu (const QPoint& position);
 	int suggestInsertPoint();
 	Q_SLOT void updateActions();
@@ -112,6 +97,7 @@
 	void documentClosed(LDDocument* document);
 	void updateTitle();
 	void newDocument (LDDocument* document, bool cache = false);
+	void settingsChanged();
 
 protected:
 	void closeEvent (QCloseEvent* event);
@@ -124,7 +110,6 @@
 	QMap<LDDocument*, QItemSelectionModel*> m_selections;
 	PrimitiveManager* m_primitives;
 	Grid* m_grid;
-	QVector<ColorToolbarItem>	m_quickColors;
 	QVector<QToolButton*>	m_colorButtons;
 	QVector<QAction*> m_recentFiles;
 	class Ui_MainWindow& ui;
--- a/src/toolsets/filetoolset.cpp	Fri Dec 21 21:43:06 2018 +0200
+++ b/src/toolsets/filetoolset.cpp	Wed Dec 26 16:38:38 2018 +0200
@@ -90,7 +90,9 @@
 
 void FileToolset::settings()
 {
-	(new ConfigDialog {m_window})->exec();
+	ConfigDialog dialog {m_window};
+	connect(&dialog, &ConfigDialog::settingsChanged, m_window, &MainWindow::settingsChanged);
+	dialog.exec();
 }
 
 void FileToolset::exit()

mercurial