-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinder.py
More file actions
82 lines (73 loc) · 3 KB
/
binder.py
File metadata and controls
82 lines (73 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from PySide6.QtWidgets import QTabWidget
from settings.__init__ import G_QSETTINGS, settings
from utilities.rename_dialog import RenameableMixin
from notebook import Notebook
from os import path, listdir, rename
from style_consants import TAB_PANE_BORDER_COLOR
class Binder(QTabWidget, RenameableMixin):
""" Binders are the root of the notebook workspace. Where each notebook is a file, binders are the folder those
files are in. Binders display each notebook as a tab, and allow the user to add a new notebook. """
_unique_resource_name = "notebook"
def __init__(self):
""" pass a workspace in order to load from a specific folder. Otherwise, the contents of configuration files
will be checked (or registry, in ms windows), and environment variables to override those defaults """
super().__init__()
self.notebooks = []
self.ids = set()
self._just_loaded = False
self.setTabPosition(self.West)
self.tabBarDoubleClicked.connect(self._rename_dialog)
self.setStyleSheet("""
QTabWidget::pane {{
border: 0px;
border-top: 1px solid {};
}}
QTabWidget::tab-bar {{
top: 0;
}}
""".format(TAB_PANE_BORDER_COLOR))
def _try_rename(self, new_id: str, *args):
index = args[0]
if index > len(self.notebooks):
return True
if new_id not in self.ids:
notebook = self.notebooks[index]
self.ids.remove(notebook.id)
rename(
path.join(settings.workspace_dir, "notebook-{}.fnbook".format(notebook.id)),
path.join(settings.workspace_dir, "notebook-{}.fnbook".format(new_id))
)
self.ids.add(new_id)
notebook.id = new_id
self.setTabText(index, new_id)
return True
return False
def load_workspace(self):
self._just_loaded = True
for each in listdir(settings.workspace_dir):
if each.endswith(".fnbook"):
notebook = Notebook.from_file(path.join(settings.workspace_dir, each))
self._add_notebook(notebook)
if len(self.notebooks) == 0:
# Append a starting notebook
notebook = Notebook("My Notebook")
self._add_notebook(notebook)
def _add_notebook(self, book: Notebook):
self.addTab(book, book.id)
self.notebooks.append(book)
self.ids.add(book.id)
def new_notebook(self, name: str) -> bool:
""" Try to create a new notebook with name. If the name already exists, return False"""
if name in self.ids:
return False
nb = Notebook(name)
self._add_notebook(nb)
return True
def save(self):
if self._just_loaded:
self._just_loaded = False
return
G_QSETTINGS.sync()
for each in self.notebooks:
filename = path.join(settings.workspace_dir, "notebook-{}.fnbook".format(each.id))
each.save(filename)