reload document when changed v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Jan 2024 00:41:26 +0100
branchv_0
changeset 38 808f05faab30
parent 37 fb673fa1cad5
child 39 2068a992e97e
reload document when changed
OHP3D.cpp
--- a/OHP3D.cpp	Thu Dec 28 01:35:17 2023 +0100
+++ b/OHP3D.cpp	Sat Jan 06 00:41:26 2024 +0100
@@ -191,7 +191,8 @@
 	void log(LogLevel level, std::string message);
 	int setNonBlocking(int fd);
 	void loadVertices();
-	void loadDocuments();
+	void loadDocuments(bool first = true);
+	bool reloadDocument(const std::string& fileName);
 	void loadShaders();
 	void updateVariableLocations();
 	bool reloadShader(const std::string& fileName);
@@ -418,6 +419,7 @@
 							<< std::endl;
 					try {
 						redraw |= reloadShader(fe.fileName);
+						redraw |= reloadDocument(fe.fileName);
 						setTitle();
 					} catch (const std::exception& e) {
 						setTitle("[ERROR]");
@@ -563,7 +565,7 @@
 	}
 }
 
-void OHP3D::Impl::loadDocuments() {
+void OHP3D::Impl::loadDocuments(bool first) {
 	if (cfg.documents.empty())
 		cfg.documents.push_back({getDefaultFile("documents/default.pdf")});
 
@@ -587,6 +589,11 @@
 		renderer.set_image_format(pp::image::format_rgb24);
 		double dpi = cfg.dpi;
 		for (int i = 0, limit = doc->pages(); i < limit; i++) {
+			{
+				std::stringstream title;
+				title << "[loading " << (i + 1) << "/" << limit << "]";
+				setTitle(title.str());
+			}
 			std::shared_ptr<pp::page> page(doc->create_page(i));
 			pp::image pageImage = renderer.render_page(page.get(), dpi, dpi);
 
@@ -602,11 +609,14 @@
 			log(LogLevel::INFO, "  page " + std::to_string(i + 1)
 					+ "/" + std::to_string(limit));
 		}
+		setTitle("");
 
 		auto timingEnd = std::chrono::steady_clock::now();
 		auto timingTotal = std::chrono::duration_cast<std::chrono::microseconds>
 				(timingEnd - timingStart).count();
 
+		if (first) watchedFiles.push_back(fileMonitor.watch(document.fileName));
+
 		std::stringstream timingMsg;
 		timingMsg.imbue(std::locale(""));
 		timingMsg << "PDF to texture load time:"
@@ -618,6 +628,23 @@
 	}
 }
 
+bool OHP3D::Impl::reloadDocument(const std::string& fileName) {
+	bool hasChanges = false;
+	for (std::shared_ptr<Texture> tex : textures) {
+		if (tex->getFileName() == fileName) {
+			hasChanges = true;
+			break;
+		}
+	}
+
+	if (hasChanges) {
+		// TODO: reload only changed document
+		textures.clear();
+		loadDocuments(false);
+	}
+	return hasChanges;
+}
+
 void OHP3D::Impl::loadShaders() {
 	// Vertex Array Object (VAO)
 	GLuint vao;