Scenario: Add X, Y position for tree display.

scenarios
Pierre-Antoine Rouby 2024-09-02 11:47:37 +02:00
parent b634acec67
commit b8ed3d8954
3 changed files with 36 additions and 8 deletions

View File

@ -29,12 +29,14 @@ class Scenario(SQLSubModel):
id: int = -1, id: int = -1,
name: str = "", name: str = "",
description: str = "", description: str = "",
x: int = 0.0, y: int = 0.0,
revision: int = 0, revision: int = 0,
parent=None): parent=None):
super(Scenario, self).__init__() super(Scenario, self).__init__()
self._set_id(id) self._set_id(id)
self._x, self._y = x, y
self._name = name self._name = name
self._description = description self._description = description
self._revision = revision self._revision = revision
@ -54,11 +56,21 @@ class Scenario(SQLSubModel):
def id(self): def id(self):
return self._id return self._id
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@classmethod @classmethod
def _db_create(cls, execute): def _db_create(cls, execute):
execute(""" execute("""
CREATE TABLE scenario( CREATE TABLE scenario(
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
x REAL NOT NULL DEFAULT 1000,
y REAL NOT NULL DEFAULT 1000,
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT NOT NULL, description TEXT NOT NULL,
revision INTEGER NOT NULL, revision INTEGER NOT NULL,
@ -73,10 +85,10 @@ class Scenario(SQLSubModel):
def _db_add_default(cls, execute): def _db_add_default(cls, execute):
execute( execute(
"INSERT OR REPLACE INTO " + "INSERT OR REPLACE INTO " +
"scenario(id, name, description, revision, parent_id) " + "scenario(id, x, y, name, description, revision, parent_id) " +
"VALUES (" + "VALUES (\n" +
" 0, 'default', 'Default scenario',\n" + " 0, 1000, 1000, 'default', 'Default scenario',\n" +
" 0, NULL" + " 0, NULL\n" +
")" ")"
) )
@ -96,6 +108,17 @@ class Scenario(SQLSubModel):
cls._db_create(execute) cls._db_create(execute)
cls._db_add_default(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 return True
@classmethod @classmethod
@ -116,7 +139,7 @@ class Scenario(SQLSubModel):
scenarios = {} scenarios = {}
table = execute( table = execute(
"SELECT id, name, description, revision, parent_id " + "SELECT id, x, y, name, description, revision, parent_id " +
"FROM scenario " + "FROM scenario " +
"ORDER BY id ASC" "ORDER BY id ASC"
) )
@ -125,6 +148,7 @@ class Scenario(SQLSubModel):
it = iter(row) it = iter(row)
id = next(it) id = next(it)
x, y = next(it), next(it)
name = next(it) name = next(it)
desc = next(it) desc = next(it)
revi = next(it) revi = next(it)
@ -134,7 +158,8 @@ class Scenario(SQLSubModel):
parent = scenarios[parent] parent = scenarios[parent]
new = cls( new = cls(
id=id, name=name, description=desc, id=id, x=x, y=y,
name=name, description=desc,
revision=revi, parent=parent revision=revi, parent=parent
) )
scenarios[id] = new scenarios[id] = new
@ -148,9 +173,10 @@ class Scenario(SQLSubModel):
execute( execute(
"INSERT OR REPLACE INTO " + "INSERT OR REPLACE INTO " +
"scenario(id, name, description, revision, parent_id) " + "scenario(id, x, y, name, description, revision, parent_id) " +
"VALUES (" + "VALUES (" +
f"{self._id}, " + f"{self._id}, " +
f"{self.x}, {self.y} " +
f"'{self._db_format(self.name)}', " + f"'{self._db_format(self.name)}', " +
f"'{self._db_format(self.description)}', " + f"'{self._db_format(self.description)}', " +
f"{self._revision}, " + f"{self._revision}, " +

View File

@ -43,7 +43,7 @@ class Study(SQLModel):
def __init__(self, filename=None, init_new=True): def __init__(self, filename=None, init_new=True):
# Metadata # Metadata
self._version = "0.1.1" self._version = "0.1.2"
self.creation_date = datetime.now() self.creation_date = datetime.now()
self.last_modification_date = datetime.now() self.last_modification_date = datetime.now()
self.last_save_date = datetime.now() self.last_save_date = datetime.now()

View File

@ -53,6 +53,8 @@ class ScenarioItem(QGraphicsTextItem):
self.graph = graph_widget self.graph = graph_widget
self.scenario = scenario self.scenario = scenario
self.setPos(QPointF(self.scenario.x, self.scenario.y))
self.setPlainText(self.scenario.name) self.setPlainText(self.scenario.name)
self.setDefaultTextColor(Qt.black) self.setDefaultTextColor(Qt.black)