From 7f48ae303e86c9b37b5457c19f17cf0940442d78 Mon Sep 17 00:00:00 2001 From: Nicholas Shiell Date: Mon, 5 Jun 2023 15:34:05 +0100 Subject: [PATCH] Fix bad storing of diagrams - not breaking each one by database and minor refactor --- database_dossier/__init__.py | 30 +++-------- database_dossier/database.py | 78 +++++++++++++++++++--------- database_dossier/store.py | 6 +-- database_dossier/ui/types/diagram.py | 9 +++- 4 files changed, 71 insertions(+), 52 deletions(-) diff --git a/database_dossier/__init__.py b/database_dossier/__init__.py index 99836a2..7714182 100644 --- a/database_dossier/__init__.py +++ b/database_dossier/__init__.py @@ -28,7 +28,6 @@ from .updater import show_please_update from .database import ConnectionList, DatabaseException, test_connection - class MainWindow(QMainWindow, WindowMixin): table_views = [ 'table_view_data', @@ -140,7 +139,7 @@ def setup_diagram(self): print(table_name) ) - self.diagram.bind('state_change', self.store_diagram_state) + self.diagram.bind('state_change', self.connections.store_active_diagram) self.diagram.setup() colors = self.palette().color @@ -151,10 +150,6 @@ def setup_diagram(self): } - def store_diagram_state(self, positions): - self.connections[self.connections.active_connection_index]['diagram'] = positions - - def show_diagram(self, new_index): if new_index != 1: return None @@ -169,25 +164,14 @@ def show_schema_in_diagram(self): cons = self.connections try: - active_schema = cons.active_schema - except: + self.diagram.populate( + schema = cons.active_schema, + position_overrides = cons.active_database_positions, + colors = cons.active_table_colors + ) + except DatabaseException: return None - should_override = ( - 'diagram' in cons[cons.active_connection_index] and - cons[cons.active_connection_index]['diagram'] is not None - ) - - if should_override: - self.diagram.position_overrides = cons[ - self.connections.active_connection_index - ]['diagram'] - - self.diagram.colors = { - k : cons.find_color_for_table(k) for k in cons.active_schema - } - - self.diagram.schema = active_schema def select_sql_fragment(self, start_point, end_point): text_cursor = self.text_edit_sql.textCursor() diff --git a/database_dossier/database.py b/database_dossier/database.py index 53bccbf..50fe198 100644 --- a/database_dossier/database.py +++ b/database_dossier/database.py @@ -252,30 +252,60 @@ def active_schema(self): return tables - def find_color_for_table(self, table, as_hex=True): - def fix_color_to_hex(color): - if color < 50: - color = 0 - elif color > 150: - color = 150 - - return hex(int(color))[2:].rjust(2, '0').upper() - - l = len(table) - h = int(hashlib.md5(table.encode()).hexdigest(), 16) - h = float(str(float(str(h)))[0:5]) - rgb = colorsys.hls_to_rgb(h, l, 20 + (l / 10)) - - if as_hex: - hexadecimal = '#' + ''.join([fix_color_to_hex(c) for c in rgb]) - return hexadecimal - # hex(int(rgb[0]))[2:] + hex(rgb[1])[2:] + hex(rgb[2])[2:] - - return { - 'red' : rgb[0], - 'green' : rgb[1], - 'blue' : rgb[2] - } + @property + def active_table_colors(self): + return {k : hex_color_string(k) for k in self.active_schema} + + + def store_active_diagram(self, positions): + con = self.active_connection + if not con: + return None + + if not con['database']: + return None + + if 'diagrams' not in con: + con['diagrams'] = {} + + if con['database'] not in con['diagrams']: + con['diagrams'][con['database']] = {} + + con['diagrams'][con['database']] = positions + + @property + def active_database_positions(self): + con = self.active_connection + if not con: + return {} + + if 'diagrams' not in con: + return {} + + if con['diagrams'] == None: + return {} + + if con['database'] not in con['diagrams']: + return {} + + return con['diagrams'][con['database']] + + +def hex_color_string(string): + def fix_color_to_hex(color): + if color < 50: + color = 0 + elif color > 150: + color = 150 + + return hex(int(color))[2:].rjust(2, '0').upper() + + l = len(string) + h = int(hashlib.md5(string.encode()).hexdigest(), 16) + h = float(str(float(str(h)))[0:5]) + rgb = colorsys.hls_to_rgb(h, l, 20 + (l / 10)) + + return '#' + ''.join([fix_color_to_hex(c) for c in rgb]) def list_databases(lst, connection_item): diff --git a/database_dossier/store.py b/database_dossier/store.py index 002142c..9544db5 100644 --- a/database_dossier/store.py +++ b/database_dossier/store.py @@ -100,8 +100,8 @@ def parse_connection(self, connection): 'table': connection['table'] if valid(connection, 'table', str, 0, 200) else None, - 'diagram': connection['diagram'] - if valid(connection, 'diagram', dict) else None, + 'diagrams': connection['diagrams'] + if valid(connection, 'diagrams', dict) else {}, }) @@ -146,7 +146,7 @@ def connections_for_persist(self): 'port', 'database', 'table', - 'diagram' + 'diagrams' ] cons = [] diff --git a/database_dossier/ui/types/diagram.py b/database_dossier/ui/types/diagram.py index dee83f8..a8884c3 100644 --- a/database_dossier/ui/types/diagram.py +++ b/database_dossier/ui/types/diagram.py @@ -103,6 +103,13 @@ def execute_javascript(self, javascript): self.q_webview.page().mainFrame().evaluateJavaScript(javascript) + def populate(self, schema, position_overrides, colors): + self.position_overrides = position_overrides + self.colors = colors + # must be last + self.schema = schema + + @property def schema(self): return self._schema @@ -162,8 +169,6 @@ def query(self, indexUriData): self.trigger('state_change', [json.loads(indexUriData[offset:])]) -#border_color = self.palette().color(QPalette.Link).name() - @property def doc_dir(self): if not self._doc_dir: