Add model icons back into the list view

Wed, 08 Jun 2022 23:14:55 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Jun 2022 23:14:55 +0300
changeset 208
930928b760a2
parent 207
5315358a7d91
child 209
c93e4a1eaadb

Add model icons back into the list view

src/model.cpp file | annotate | diff | comparison | revisions
--- a/src/model.cpp	Wed Jun 08 23:02:04 2022 +0300
+++ b/src/model.cpp	Wed Jun 08 23:14:55 2022 +0300
@@ -16,8 +16,54 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <QPixmap>
 #include "model.h"
 
+static const char* iconPathForElement(const ModelElement& element)
+{
+	return std::visit(overloaded{
+		[](const Colored<SubfileReference>&) {
+			return ":/icons/linetype-subfile.png";
+		},
+		[](const Colored<LineSegment>&) {
+			return ":/icons/linetype-edgeline.png";
+		},
+		[](const Colored<Triangle>&) {
+			return ":/icons/linetype-triangle.png";
+		},
+		[](const Colored<Quadrilateral>&) {
+			return ":/icons/linetype-quadrilateral.png";
+		},
+		[](const Colored<ConditionalEdge>&) {
+			return ":/icons/linetype-conditionaledge.png";
+		},
+		[](const Comment&) {
+			return ":/icons/chatbubble-ellipses-outline.png";
+		},
+		[](const Empty&) {
+			return "";
+		},
+		[](const ParseError&) {
+			return ":/icons/linetype-errorline.png";
+		},
+	}, element);
+}
+
+static QPixmap iconForElement(const ModelElement& element)
+{
+	// We avoid processing the same image over and over again by storing it
+	// in a static constant. However, we need one per each possible type
+	// of ModelElement, so we put the pixmap constant inside a templated lambda,
+	// which gets instiated once for each type of ModelElement.
+	return std::visit([](auto&& element){
+		static const QPixmap pixmap = QPixmap::fromImage(
+			QImage{iconPathForElement(element)}
+				.scaledToHeight(24, Qt::SmoothTransformation)
+		);
+		return pixmap;
+	}, element);
+}
+
 QString modelElementToString(const ModelElement &element)
 {
 	return std::visit(overloaded{
@@ -141,14 +187,13 @@
 QVariant Model::data(const QModelIndex &index, int role) const
 {
 	const int i = index.row();
+	const ModelElement& element = this->body[i].data;
 	switch(role)
 	{
-	/*
 	case Qt::DecorationRole:
-		return QPixmap{object->iconName()}.scaledToHeight(24);
-	*/
+		return iconForElement(element);
 	case Qt::DisplayRole:
-		return modelElementToString(this->body[i].data);
+		return modelElementToString(element);
 	/*
 	case Qt::ForegroundRole:
 		return object->textRepresentationForeground();

mercurial