Add a basic message log

Mon, 20 Jun 2022 17:27:30 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 17:27:30 +0300
changeset 235
7ef03c2b46ab
parent 234
87ee9824210b
child 236
1fa0e1de9f0a

Add a basic message log

CMakeLists.txt file | annotate | diff | comparison | revisions
src/basics.h file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/documentmanager.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Mon Jun 20 16:59:09 2022 +0300
+++ b/CMakeLists.txt	Mon Jun 20 17:27:30 2022 +0300
@@ -30,6 +30,7 @@
 	src/libraries.cpp
 	src/invert.cpp
 	src/main.cpp
+	src/messagelog.cpp
 	src/model.cpp
 	src/parser.cpp
 	src/polygoncache.cpp
@@ -67,6 +68,7 @@
 	src/invert.h
 	src/ldrawalgorithm.h
 	src/libraries.h
+	src/messagelog.h
 	src/model.h
 	src/parser.h
 	src/polygoncache.h
--- a/src/basics.h	Mon Jun 20 16:59:09 2022 +0300
+++ b/src/basics.h	Mon Jun 20 17:27:30 2022 +0300
@@ -26,6 +26,7 @@
 #include <set>
 #include <type_traits>
 #include <QDataStream>
+#include <QDateTime>
 #include <QDebug>
 #include <QObject>
 #include <QPointF>
@@ -295,3 +296,25 @@
 
 template<typename T>
 using Graph = std::deque<GraphEdge<T>>;
+
+struct Message
+{
+	QDateTime time;
+	enum { Info, Warning, Error } type;
+	QString text;
+};
+
+inline Message logInfo(const QString text)
+{
+	return Message{.time = QDateTime::currentDateTime(), .type = Message::Info, .text = text};
+}
+
+inline Message logWarning(const QString text)
+{
+	return Message{.time = QDateTime::currentDateTime(), .type = Message::Warning, .text = text};
+}
+
+inline Message logError(const QString text)
+{
+	return Message{.time = QDateTime::currentDateTime(), .type = Message::Error, .text = text};
+}
--- a/src/documentmanager.cpp	Mon Jun 20 16:59:09 2022 +0300
+++ b/src/documentmanager.cpp	Mon Jun 20 17:27:30 2022 +0300
@@ -42,6 +42,7 @@
 		.opentype = OpenType::ManuallyOpened,
 	}));
 	this->makePolygonCacheForModel(modelId);
+	Q_EMIT this->message(logInfo(tr("New model %1 created").arg(modelId.value)));
 	return modelId;
 }
 
@@ -135,6 +136,7 @@
 		}));
 		this->makePolygonCacheForModel(modelId);
 		result = modelId;
+		Q_EMIT this->message(logInfo(tr("Opened %1 as model %2").arg(quoted(path)).arg(modelId.value)));
 	}
 	else
 	{
@@ -303,6 +305,7 @@
 		for (ModelId idToPrune : prunable) {
 			auto it = this->openModels.find(idToPrune);
 			if (it != this->openModels.end()) {
+				Q_EMIT this->message(logInfo(tr("Model %1 (%2) pruned").arg(idToPrune.value).arg(it->second.path)));
 				this->openModels.erase(it);
 			}
 			removeFromSet(autoOpened, idToPrune);
--- a/src/documentmanager.h	Mon Jun 20 16:59:09 2022 +0300
+++ b/src/documentmanager.h	Mon Jun 20 17:27:30 2022 +0300
@@ -70,6 +70,8 @@
 		const ModelInfo* info = this->find(modelId);
 		return info ? qobject_cast<T*>(info->payload) : nullptr;
 	}
+Q_SIGNALS:
+	void message(const Message& message);
 private:
 	int modelIdCounter = 0;
 	std::map<ModelId, ModelInfo> openModels;
--- a/src/main.cpp	Mon Jun 20 16:59:09 2022 +0300
+++ b/src/main.cpp	Mon Jun 20 17:27:30 2022 +0300
@@ -413,6 +413,22 @@
 		saveSettings();
 		updateRecentlyOpenedDocumentsMenu();
 	};
+	const auto logMessage = [&ui](const Message& message){
+		QString messagetext = message.time.toString(QObject::tr("[hh:mm:ss]"));
+		switch(message.type) {
+		case Message::Info:
+			messagetext += QObject::tr(" [INFO] ");
+			break;
+		case Message::Warning:
+			messagetext += QObject::tr(" [WARN] ");
+			break;
+		case Message::Error:
+			messagetext += QObject::tr(" [ERR!] ");
+			break;
+		}
+		messagetext += message.text;
+		ui.messageLog->append(messagetext);
+	};
 	const auto openModelForEditing = [&](const ModelId modelId){
 		Model* model = documents.getModelById(modelId);
 		if (model != nullptr) {
@@ -626,6 +642,7 @@
 		}
 	});
 	QObject::connect(ui.actionAboutQt, &QAction::triggered, &app, &QApplication::aboutQt);
+	QObject::connect(&documents, &DocumentManager::message, logMessage);
 	mainWindow.setWindowTitle(title());
 	mainWindow.restoreGeometry(setting<Setting::MainWindowGeometry>());
 	restoreSettings();
--- a/src/mainwindow.ui	Mon Jun 20 16:59:09 2022 +0300
+++ b/src/mainwindow.ui	Mon Jun 20 17:27:30 2022 +0300
@@ -13,6 +13,9 @@
   <property name="windowTitle">
    <string>LDForge</string>
   </property>
+  <property name="dockNestingEnabled">
+   <bool>true</bool>
+  </property>
   <widget class="QWidget" name="centralwidget">
    <layout class="QVBoxLayout" name="verticalLayout_3">
     <item>
@@ -39,7 +42,7 @@
      <x>0</x>
      <y>0</y>
      <width>729</width>
-     <height>29</height>
+     <height>31</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -202,6 +205,25 @@
     </layout>
    </widget>
   </widget>
+  <widget class="QDockWidget" name="dockWidget">
+   <property name="windowTitle">
+    <string>Message log</string>
+   </property>
+   <attribute name="dockWidgetArea">
+    <number>8</number>
+   </attribute>
+   <widget class="QWidget" name="dockWidgetContents">
+    <layout class="QVBoxLayout" name="verticalLayout">
+     <item>
+      <widget class="QTextEdit" name="messageLog">
+       <property name="readOnly">
+        <bool>true</bool>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </widget>
+  </widget>
   <action name="actionQuit">
    <property name="icon">
     <iconset resource="../ldforge.qrc">

mercurial