From b8ed3d89546c5dfc93f98cdce43fa7cc5ac3898f Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Mon, 2 Sep 2024 11:47:37 +0200 Subject: [PATCH] Scenario: Add X, Y position for tree display. --- src/Model/Scenario.py | 40 +++++++++++++++++++++++++------ src/Model/Study.py | 2 +- src/View/Scenarios/GraphWidget.py | 2 ++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Model/Scenario.py b/src/Model/Scenario.py index f49491c8..a15fc4c2 100644 --- a/src/Model/Scenario.py +++ b/src/Model/Scenario.py @@ -29,12 +29,14 @@ class Scenario(SQLSubModel): id: int = -1, name: str = "", description: str = "", + x: int = 0.0, y: int = 0.0, revision: int = 0, parent=None): super(Scenario, self).__init__() self._set_id(id) + self._x, self._y = x, y self._name = name self._description = description self._revision = revision @@ -54,11 +56,21 @@ class Scenario(SQLSubModel): def id(self): return self._id + @property + def x(self): + return self._x + + @property + def y(self): + return self._y + @classmethod def _db_create(cls, execute): execute(""" CREATE TABLE scenario( id INTEGER PRIMARY KEY, + x REAL NOT NULL DEFAULT 1000, + y REAL NOT NULL DEFAULT 1000, name TEXT NOT NULL, description TEXT NOT NULL, revision INTEGER NOT NULL, @@ -73,10 +85,10 @@ class Scenario(SQLSubModel): def _db_add_default(cls, execute): execute( "INSERT OR REPLACE INTO " + - "scenario(id, name, description, revision, parent_id) " + - "VALUES (" + - " 0, 'default', 'Default scenario',\n" + - " 0, NULL" + + "scenario(id, x, y, name, description, revision, parent_id) " + + "VALUES (\n" + + " 0, 1000, 1000, 'default', 'Default scenario',\n" + + " 0, NULL\n" + ")" ) @@ -96,6 +108,17 @@ class Scenario(SQLSubModel): cls._db_create(execute) cls._db_add_default(execute) + if major == "0" and minor == "1": + if int(release) < 2: + execute( + "ALTER TABLE scenario " + + "ADD COLUMN x REAL NOT NULL DEFAULT 1000" + ) + execute( + f"ALTER TABLE scenario " + + "ADD COLUMN x REAL NOT NULL DEFAULT 1000" + ) + return True @classmethod @@ -116,7 +139,7 @@ class Scenario(SQLSubModel): scenarios = {} table = execute( - "SELECT id, name, description, revision, parent_id " + + "SELECT id, x, y, name, description, revision, parent_id " + "FROM scenario " + "ORDER BY id ASC" ) @@ -125,6 +148,7 @@ class Scenario(SQLSubModel): it = iter(row) id = next(it) + x, y = next(it), next(it) name = next(it) desc = next(it) revi = next(it) @@ -134,7 +158,8 @@ class Scenario(SQLSubModel): parent = scenarios[parent] new = cls( - id=id, name=name, description=desc, + id=id, x=x, y=y, + name=name, description=desc, revision=revi, parent=parent ) scenarios[id] = new @@ -148,9 +173,10 @@ class Scenario(SQLSubModel): execute( "INSERT OR REPLACE INTO " + - "scenario(id, name, description, revision, parent_id) " + + "scenario(id, x, y, name, description, revision, parent_id) " + "VALUES (" + f"{self._id}, " + + f"{self.x}, {self.y} " + f"'{self._db_format(self.name)}', " + f"'{self._db_format(self.description)}', " + f"{self._revision}, " + diff --git a/src/Model/Study.py b/src/Model/Study.py index 0869e3cd..80e47484 100644 --- a/src/Model/Study.py +++ b/src/Model/Study.py @@ -43,7 +43,7 @@ class Study(SQLModel): def __init__(self, filename=None, init_new=True): # Metadata - self._version = "0.1.1" + self._version = "0.1.2" self.creation_date = datetime.now() self.last_modification_date = datetime.now() self.last_save_date = datetime.now() diff --git a/src/View/Scenarios/GraphWidget.py b/src/View/Scenarios/GraphWidget.py index f0c63882..27986f27 100644 --- a/src/View/Scenarios/GraphWidget.py +++ b/src/View/Scenarios/GraphWidget.py @@ -53,6 +53,8 @@ class ScenarioItem(QGraphicsTextItem): self.graph = graph_widget self.scenario = scenario + self.setPos(QPointF(self.scenario.x, self.scenario.y)) + self.setPlainText(self.scenario.name) self.setDefaultTextColor(Qt.black)