refactored the quick color toolbar

Wed, 04 Apr 2018 12:08:45 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 04 Apr 2018 12:08:45 +0300
changeset 1382
6f31a87006f2
parent 1381
191122e19b53
child 1383
2e7ffaf6239f

refactored the quick color toolbar

src/dialogs/configdialog.cpp file | annotate | diff | comparison | revisions
src/guiutilities.cpp 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.cpp	Wed Apr 04 11:43:15 2018 +0300
+++ b/src/dialogs/configdialog.cpp	Wed Apr 04 12:08:45 2018 +0300
@@ -453,10 +453,10 @@
 	}
 	else
 	{
-		ColorToolbarItem newentry (value, nullptr);
+		ColorToolbarItem newentry {value};
 		item = getSelectedQuickColor();
 		int idx = (item) ? getItemRow (item, quickColorItems) + 1 : countof(quickColorItems);
-		quickColors.insert (idx, newentry);
+		quickColors.insert(idx, newentry);
 		entry = &quickColors[idx];
 	}
 
--- a/src/guiutilities.cpp	Wed Apr 04 11:43:15 2018 +0300
+++ b/src/guiutilities.cpp	Wed Apr 04 12:08:45 2018 +0300
@@ -147,7 +147,7 @@
 			LDColor color = colorName.toInt();
 
 			if (color.isValid())
-				colors << ColorToolbarItem {color, nullptr};
+				colors.append(ColorToolbarItem {color});
 		}
 	}
 
--- a/src/mainwindow.cpp	Wed Apr 04 11:43:15 2018 +0300
+++ b/src/mainwindow.cpp	Wed Apr 04 12:08:45 2018 +0300
@@ -263,16 +263,29 @@
 		}
 		else
 		{
-			QToolButton* colorButton = new QToolButton;
-			colorButton->setIcon (makeColorIcon (entry.color(), 16));
-			colorButton->setIconSize (QSize (16, 16));
-			colorButton->setToolTip (entry.color().name());
-
-			connect (colorButton, SIGNAL (clicked()), this, SLOT (quickColorClicked()));
+			QToolButton* colorButton = new QToolButton {this};
+			colorButton->setIcon(makeColorIcon(entry.color(), 16));
+			colorButton->setIconSize({16, 16});
+			colorButton->setToolTip(entry.color().name());
+			colorButton->setStatusTip(format(
+				tr("Changes the color of selected objects to %1"),
+				entry.color().name()
+			));
 			ui.toolBarColors->addWidget (colorButton);
 			m_colorButtons << colorButton;
+			connect(colorButton,
+				&QToolButton::clicked,
+				[&]()
+				{
+					for (LDObject* object : selectedObjects())
+					{
+						if (object->isColored())
+							object->setColor(entry.color());
+					}
 
-			entry.setToolButton (colorButton);
+					endAction();
+				}
+			);
 		}
 	}
 
@@ -352,37 +365,6 @@
 
 // ---------------------------------------------------------------------------------------------------------------------
 //
-void MainWindow::quickColorClicked()
-{
-	QToolButton* button = static_cast<QToolButton*> (sender());
-	LDColor color = LDColor::nullColor;
-
-	for (const ColorToolbarItem& entry : m_quickColors)
-	{
-		if (entry.toolButton() == button)
-		{
-			color = entry.color();
-			break;
-		}
-	}
-
-	if (not color.isValid())
-		return;
-
-	for (LDObject* obj : selectedObjects())
-	{
-		if (not obj->isColored())
-			continue; // uncolored object
-
-		obj->setColor (color);
-	}
-
-	endAction();
-	refresh();
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
 // Returns the suggested position to place a new object at.
 //
 int MainWindow::suggestInsertPoint()
@@ -1050,13 +1032,12 @@
 
 // ---------------------------------------------------------------------------------------------------------------------
 //
-ColorToolbarItem::ColorToolbarItem (LDColor color, QToolButton* toolButton) :
-	m_color (color),
-	m_toolButton (toolButton) {}
+ColorToolbarItem::ColorToolbarItem(LDColor color) :
+	m_color (color) {}
 
 ColorToolbarItem ColorToolbarItem::makeSeparator()
 {
-	return ColorToolbarItem (LDColor::nullColor, nullptr);
+	return {LDColor::nullColor};
 }
 
 bool ColorToolbarItem::isSeparator() const
@@ -1073,13 +1054,3 @@
 {
 	m_color = color;
 }
-
-QToolButton* ColorToolbarItem::toolButton() const
-{
-	return m_toolButton;
-}
-
-void ColorToolbarItem::setToolButton (QToolButton* value)
-{
-	m_toolButton = value;
-}
--- a/src/mainwindow.h	Wed Apr 04 11:43:15 2018 +0300
+++ b/src/mainwindow.h	Wed Apr 04 12:08:45 2018 +0300
@@ -38,18 +38,15 @@
 class ColorToolbarItem
 {
 public:
-	ColorToolbarItem (LDColor color = LDColor{}, QToolButton* toolButton = nullptr);
+	ColorToolbarItem (LDColor color = {});
 	LDColor color() const;
 	bool isSeparator() const;
 	void setColor (LDColor color);
-	void setToolButton (QToolButton* value);
-	QToolButton* toolButton() const;
 
 	static ColorToolbarItem makeSeparator();
 
 private:
 	LDColor m_color;
-	QToolButton* m_toolButton;
 };
 
 // LDForge's main GUI class.
@@ -147,5 +144,4 @@
 private slots:
 	void finishInitialization();
 	void recentFileClicked();
-	void quickColorClicked();
 };

mercurial