generated from itdominator/Python-With-Gtk-Template
100 lines
2.9 KiB
Python
100 lines
2.9 KiB
Python
|
# Python imports
|
||
|
import os
|
||
|
|
||
|
# Lib imports
|
||
|
import gi
|
||
|
gi.require_version('Gtk', '3.0')
|
||
|
from gi.repository import Gtk
|
||
|
|
||
|
# Application imports
|
||
|
|
||
|
|
||
|
class CreateNotebookWidget(Gtk.Dialog):
|
||
|
def __init__(self):
|
||
|
super(CreateNotebookWidget, self).__init__()
|
||
|
|
||
|
self._nb_path = settings.get_notebooks_path()
|
||
|
|
||
|
self._setup_styling()
|
||
|
self._setup_signals()
|
||
|
self._subscribe_to_events()
|
||
|
self._load_widgets()
|
||
|
|
||
|
|
||
|
def _setup_styling(self):
|
||
|
...
|
||
|
|
||
|
def _setup_signals(self):
|
||
|
self.set_default_geometry(280, 80)
|
||
|
self.set_size_request(280, 80)
|
||
|
self.set_modal(True)
|
||
|
self.set_transient_for( settings.get_main_window() )
|
||
|
|
||
|
def _subscribe_to_events(self):
|
||
|
event_system.subscribe("load_notebook", self._load_notebook)
|
||
|
event_system.subscribe("create_notebook", self._create_notebook)
|
||
|
|
||
|
def _load_widgets(self):
|
||
|
area = self.get_content_area()
|
||
|
label = Gtk.Label(label="Create A New Notebook")
|
||
|
nb_entry = Gtk.Entry()
|
||
|
create_btn = Gtk.Button(label="Create")
|
||
|
cancel_btn = Gtk.Button(label="Cancel")
|
||
|
|
||
|
create_btn.set_image( Gtk.Image.new_from_icon_name("gtk-add", 4) )
|
||
|
cancel_btn.set_image( Gtk.Image.new_from_icon_name("gtk-cancel", 4) )
|
||
|
nb_entry.set_max_length(15)
|
||
|
nb_entry.set_placeholder_text("Notebook Name")
|
||
|
|
||
|
area.add(label)
|
||
|
area.add(nb_entry)
|
||
|
|
||
|
self.add_action_widget(cancel_btn, 1)
|
||
|
self.add_action_widget(create_btn, 0)
|
||
|
|
||
|
area.show_all()
|
||
|
|
||
|
|
||
|
def _load_notebook(self):
|
||
|
notebooks = os.listdir(self._nb_path)
|
||
|
if len(notebooks) == 0:
|
||
|
if self._create_notebook(True) in (-1, 1):
|
||
|
return
|
||
|
|
||
|
notebooks = os.listdir(self._nb_path)
|
||
|
for notebook in notebooks:
|
||
|
path = os.path.join(self._nb_path, notebook)
|
||
|
if os.path.isdir(path):
|
||
|
# TODO: Add to a selection window then load choice
|
||
|
settings.set_active_notebook(path) # NOTE: Temporary
|
||
|
...
|
||
|
|
||
|
# settings.set_active_notebook(path)
|
||
|
|
||
|
|
||
|
def _create_notebook(self, is_first_notebook = False):
|
||
|
response = self.run()
|
||
|
self.hide()
|
||
|
|
||
|
if response == 0:
|
||
|
nb_entry = self.get_content_area().get_children()[1]
|
||
|
notebook = nb_entry.get_text().strip()
|
||
|
|
||
|
if not notebook in ("", None):
|
||
|
path = os.path.join(self._nb_path, notebook)
|
||
|
try:
|
||
|
logger.info(f"Creating Notebook at: {path}")
|
||
|
os.mkdir(path)
|
||
|
settings.set_active_notebook(path)
|
||
|
logger.info(f"Created and loaded Notebook: {notebook}")
|
||
|
except Exception as e:
|
||
|
logger.info(f"Notebook creation failed! ")
|
||
|
response = -1
|
||
|
else:
|
||
|
response = 1
|
||
|
|
||
|
if response == 1:
|
||
|
logger.info(f"Canceled notebook creation...")
|
||
|
|
||
|
return response
|