Skip to content

Commit

Permalink
Fix bad storing of diagrams - not breaking each one by database and m…
Browse files Browse the repository at this point in the history
…inor refactor
  • Loading branch information
nshiell committed Jun 5, 2023
1 parent e5ace5d commit 7f48ae3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 52 deletions.
30 changes: 7 additions & 23 deletions database_dossier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down
78 changes: 54 additions & 24 deletions database_dossier/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions database_dossier/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {},
})


Expand Down Expand Up @@ -146,7 +146,7 @@ def connections_for_persist(self):
'port',
'database',
'table',
'diagram'
'diagrams'
]

cons = []
Expand Down
9 changes: 7 additions & 2 deletions database_dossier/ui/types/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7f48ae3

Please sign in to comment.