Browse Source

Main window state is saved

Marko Pintera 13 years ago
parent
commit
a201831303

+ 27 - 0
CamelotEditor/Include/CmEditorPrefs.h

@@ -3,9 +3,29 @@
 #include "CmEditorPrerequisites.h"
 #include "CmModule.h"
 #include <QtCore/QString>
+#include "3rdParty/pugixml/pugixml.hpp"
 
 namespace CamelotEditor
 {
+	struct WindowLayoutDesc
+	{
+		WindowLayoutDesc()
+			:width(0), height(0), left(0), top(0), 
+			screenIdx(-1), maximized(true), docked(false)
+		{
+
+		}
+
+		QString name;
+		UINT32 width;
+		UINT32 height;
+		UINT32 left;
+		UINT32 top;
+		UINT32 screenIdx;
+		bool maximized;
+		bool docked;
+	};
+
 	class EditorPrefs : public CamelotEngine::Module<EditorPrefs>
 	{
 	public:
@@ -17,6 +37,9 @@ namespace CamelotEditor
 		void setLastUsedProjectDirectory(const QString& value);
 		const QString& getLastUsedProjectDirectory() const;
 
+		void setMainWindowLayout(const WindowLayoutDesc& desc);
+		const WindowLayoutDesc& getMainWindowLayout() const;
+
 		void save(const QString& path, bool overwrite = true) const;
 		void load(const QString& path);
 
@@ -24,6 +47,10 @@ namespace CamelotEditor
 		vector<QString>::type mRecentlyUsedProjects;
 		QString mLastUsedProjectDirectory;
 
+		WindowLayoutDesc mMainWindowLayout;
+
+		void saveWindowLayout(pugi::xml_node parentNode, const WindowLayoutDesc& desc) const;
+		WindowLayoutDesc loadWindowLayout(pugi::xml_node node) const;
 		void clear();
 	};
 

+ 4 - 0
CamelotEditor/Include/CmQtEditor.h

@@ -36,6 +36,10 @@ namespace CamelotEditor
 
 		QMenu* findOrCreateMenu(const QString& name);
 
+		void changeEvent(QEvent* event);
+		void moveEvent(QMoveEvent* event);
+		void resizeEvent(QResizeEvent* event);
+
 		void openProject();
 		void saveProject();
 		void exitEditor();

+ 69 - 1
CamelotEditor/Source/CmEditorPrefs.cpp

@@ -1,7 +1,6 @@
 #include "CmEditorPrefs.h"
 #include "CmFileSystem.h"
 #include <QtCore/QDir>
-#include "3rdParty/pugixml/pugixml.hpp"
 
 using namespace pugi;
 
@@ -45,6 +44,17 @@ namespace CamelotEditor
 		return mLastUsedProjectDirectory;
 	}
 
+	void EditorPrefs::setMainWindowLayout(const WindowLayoutDesc& desc)
+	{
+		mMainWindowLayout = desc;
+		mMainWindowLayout.name = "MainWindow";
+	}
+
+	const WindowLayoutDesc& EditorPrefs::getMainWindowLayout() const
+	{
+		return mMainWindowLayout;
+	}
+
 	void EditorPrefs::save(const QString& path, bool overwrite) const
 	{
 		String stdPath = path.toStdString();
@@ -74,9 +84,52 @@ namespace CamelotEditor
 		xml_attribute pathAttrib = lastUsedProjectDirectory.append_attribute("path");
 		pathAttrib.set_value(mLastUsedProjectDirectory.toStdString().c_str());
 
+		xml_node openWindows = camelotEditor.append_child("OpenWindows");
+		saveWindowLayout(openWindows, mMainWindowLayout);
+
+		// TODO - Add non-main windows
+
 		xmldoc.save_file(stdPath.c_str());
 	}
 
+	void EditorPrefs::saveWindowLayout(xml_node parentNode, const WindowLayoutDesc& desc) const
+	{
+		xml_node windowLayout = parentNode.append_child("WindowLayout");
+		windowLayout.append_attribute("name").set_value(desc.name.toStdString().c_str());
+
+		xml_node windowGeometry = windowLayout.append_child("Geometry");
+		windowGeometry.append_attribute("left").set_value(desc.left);
+		windowGeometry.append_attribute("top").set_value(desc.top);
+		windowGeometry.append_attribute("width").set_value(desc.width);
+		windowGeometry.append_attribute("height").set_value(desc.height);
+
+		xml_node windowScreen = windowLayout.append_child("Screen");
+		windowScreen.append_attribute("fullscreen").set_value(desc.maximized);
+		windowScreen.append_attribute("screenIdx").set_value(desc.screenIdx);
+
+		xml_node dockInfo = windowLayout.append_child("DockInfo");
+		dockInfo.append_attribute("docked").set_value(desc.docked);
+	}
+
+	WindowLayoutDesc EditorPrefs::loadWindowLayout(xml_node node) const
+	{
+		WindowLayoutDesc desc;
+
+		desc.name = node.attribute("name").value();
+
+		desc.left = node.child("Geometry").attribute("left").as_uint();
+		desc.top = node.child("Geometry").attribute("top").as_uint();
+		desc.width = node.child("Geometry").attribute("width").as_uint();
+		desc.height = node.child("Geometry").attribute("height").as_uint();
+
+		desc.maximized = node.child("Screen").attribute("fullscreen").as_bool();
+		desc.screenIdx = node.child("Screen").attribute("screenIdx").as_uint();
+
+		desc.docked = node.child("DockInfo").attribute("docked").as_bool();
+
+		return desc;
+	}
+
 	void EditorPrefs::load(const QString& path)
 	{
 		clear();
@@ -104,12 +157,27 @@ namespace CamelotEditor
 		xml_node lastUsedProjectDirectory = camelotEditor.child("LastUsedProjectDir");
 		xml_attribute pathAttrib = lastUsedProjectDirectory.attribute("path");
 		mLastUsedProjectDirectory = pathAttrib.value();
+
+		xml_node openWindows = camelotEditor.child("OpenWindows");
+
+		bool foundMainWindowLayoutDesc = false;
+		for (xml_node windowLayout = openWindows.child("WindowLayout"); windowLayout; windowLayout = windowLayout.next_sibling("WindowLayout"))
+		{
+			WindowLayoutDesc desc = loadWindowLayout(windowLayout);
+
+			if(!foundMainWindowLayoutDesc)
+			{
+				mMainWindowLayout = desc;
+				foundMainWindowLayoutDesc = true;
+			}
+		}
 	}
 
 	void EditorPrefs::clear()
 	{
 		mRecentlyUsedProjects.clear();
 		mLastUsedProjectDirectory = "";
+		mMainWindowLayout = WindowLayoutDesc();
 	}
 
 	EditorPrefs& gEditorPrefs()

+ 53 - 1
CamelotEditor/Source/CmQtEditor.cpp

@@ -1,10 +1,14 @@
 #include "CmQtEditor.h"
 #include "CmQtDockOverlayWidget.h"
+#include "CmEditorPrefs.h"
 #include <QtWidgets/QMenuBar>
 #include <QtWidgets/QToolBar>
 #include <QtWidgets/QStatusBar>
 #include <QtCore/QLocale>
 #include <QtWidgets/QApplication>
+#include <QtWidgets/QDesktopWidget>
+#include <QtGui/QMoveEvent>
+#include <QtGui/QResizeEvent>
 #include <boost/bind.hpp>
 #include "CmException.h"
 
@@ -23,7 +27,16 @@ namespace CamelotEditor
 
 	void QtEditor::setupUi()
 	{
-		resize(600, 400);
+		WindowLayoutDesc desc = gEditorPrefs().getMainWindowLayout();
+
+		if(desc.maximized)
+		{
+			setWindowState(Qt::WindowMaximized);
+		}
+		else
+		{
+			setGeometry(desc.left, desc.top, desc.width, desc.height);
+		}
 
 		mMenuBar = new QMenuBar(this);
 		setMenuBar(mMenuBar);
@@ -108,6 +121,45 @@ namespace CamelotEditor
 			return iterFind->second;
 	}
 
+	void QtEditor::changeEvent(QEvent* event)
+	{
+		if(event->type() == QEvent::WindowStateChange) 
+		{
+			WindowLayoutDesc desc = gEditorPrefs().getMainWindowLayout();
+			if(isMaximized()) 
+				desc.maximized = true;
+			else 
+				desc.maximized = false;
+
+			desc.screenIdx = QApplication::desktop()->screenNumber(this);
+			gEditorPrefs().setMainWindowLayout(desc);
+		}
+
+		event->accept();
+	}
+
+	void QtEditor::moveEvent(QMoveEvent* event)
+	{
+		WindowLayoutDesc desc = gEditorPrefs().getMainWindowLayout();
+		desc.left = event->pos().x();
+		desc.top = event->pos().y();
+
+		gEditorPrefs().setMainWindowLayout(desc);
+
+		QWidget::moveEvent(event);
+	}
+
+	void QtEditor::resizeEvent(QResizeEvent* event)
+	{
+		WindowLayoutDesc desc = gEditorPrefs().getMainWindowLayout();
+		desc.width = event->size().width();
+		desc.height = event->size().height();
+
+		gEditorPrefs().setMainWindowLayout(desc);
+
+		QWidget::resizeEvent(event);
+	}
+
 	void QtEditor::openProject()
 	{
 		CM_EXCEPT(NotImplementedException, "Not implemented");