Load models in a new window

In case of an untouched new model, the model is loaded in the current
window.
This commit is contained in:
Arjan Molenaar 2020-01-20 21:24:50 +01:00
parent 21ecd9ab5f
commit 618eeabc06
7 changed files with 131 additions and 134 deletions

View File

@ -87,17 +87,17 @@ def run(args):
def app_activate(app):
if not Application.has_sessions():
session = new_session(app)
file_manager = session.get_service("file_manager")
file_manager.action_new()
app.open([], "__new__")
def app_open(app, files, n_files, hint):
print(f"Open files {files} with '{hint}'.")
assert n_files == 1
for file in files:
session = new_session(app)
file_manager = session.get_service("file_manager")
file_manager.load(file.get_path())
session = new_session(app)
file_manager = session.get_service("file_manager")
if hint == "__new__":
file_manager.new()
else:
assert n_files == 1
for file in files:
file_manager.load(file.get_path())
def app_shutdown(app):
Application.shutdown()

104
gaphor/ui/appfilemanager.py Normal file
View File

@ -0,0 +1,104 @@
import logging
from gi.repository import Gio
from gaphor.abc import ActionProvider, Service
from gaphor.core import action, gettext
from gaphor.ui.filedialog import FileDialog
from gaphor.ui.questiondialog import QuestionDialog
log = logging.getLogger(__name__)
FILTERS = [
{"name": gettext("Gaphor Models"), "pattern": "*.gaphor"},
{"name": gettext("All Files"), "pattern": "*"},
]
class AppFileManager(Service, ActionProvider):
"""
Handle application level file loading
"""
def __init__(self, session):
self.session = session
def shutdown(self):
pass
@property
def application(self):
return Gio.Application.get_default()
def load(self, filename):
if self.active_session_is_new():
file_manager = self.session.get_service("file_manager")
file_manager.load(filename)
else:
self.application.open([Gio.File.new_for_path(filename)], "")
def new(self):
if self.active_session_is_new():
file_manager = self.session.get_service("file_manager")
file_manager.new()
else:
self.application.open([], "__new__")
def active_session_is_new(self):
"""
If it's a new model, there is no state change (undo & redo)
and no file name is defined.
"""
undo_manager = self.session.get_service("undo_manager")
file_manager = self.session.get_service("file_manager")
return (
not undo_manager.can_undo()
and not undo_manager.can_redo()
and not file_manager.filename
)
@property
def main_window(self):
return self.session.get_service("main_window")
def file_dialog(self, title):
file_dialog = FileDialog(title, filters=FILTERS, parent=self.main_window.window)
filename = file_dialog.selection
file_dialog.destroy()
log.debug(filename)
return filename
@action(name="app.file-new", shortcut="<Primary>n")
def action_new(self):
"""The new model menu action. This action will create a new
UML model. This will trigger a FileManagerStateChange event."""
self.new()
@action(name="app.file-new-template")
def action_new_from_template(self):
"""This menu action opens the new model from template dialog."""
filename = self.file_dialog(gettext("New Gaphor Model From Template"))
if filename:
self.load(filename)
self.filename = None
@action(name="app.file-open", shortcut="<Primary>o")
def action_open(self):
"""This menu action opens the standard model open dialog."""
filename = self.file_dialog(gettext("Open Gaphor Model"))
if filename:
self.load(filename)
@action(name="app.file-open-recent")
def action_open_recent(self, filename: str):
self.load(filename)

View File

@ -60,6 +60,18 @@ class FileManager(Service, ActionProvider):
filename = property(get_filename, set_filename)
def new(self):
element_factory = self.element_factory
element_factory.flush()
with element_factory.block_events():
model = element_factory.create(UML.Package)
model.name = gettext("New model")
diagram = element_factory.create(UML.Diagram)
diagram.package = model
diagram.name = gettext("main")
self.filename = None
element_factory.model_ready()
def load(self, filename):
"""Load the Gaphor model from the supplied file name. A status window
displays the loading progress. The load generator updates the progress
@ -176,127 +188,6 @@ class FileManager(Service, ActionProvider):
finally:
status_window.destroy()
def _open_dialog(self, title):
"""Open a file chooser dialog to select a model
file to open."""
filesel = Gtk.FileChooserDialog(
title=title,
action=Gtk.FileChooserAction.OPEN,
buttons=(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
),
)
filesel.set_transient_for(self.main_window.window)
filter = Gtk.FileFilter()
filter.set_name("Gaphor models")
filter.add_pattern("*.gaphor")
filesel.add_filter(filter)
filter = Gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
filesel.add_filter(filter)
if self.filename:
filesel.set_current_name(self.filename)
response = filesel.run()
filename = filesel.get_filename()
filesel.destroy()
if not filename or response != Gtk.ResponseType.OK:
return
return filename
@action(name="file-new", shortcut="<Primary>n")
def action_new(self):
"""The new model menu action. This action will create a new
UML model. This will trigger a FileManagerStateChange event."""
element_factory = self.element_factory
main_window = self.main_window
if element_factory.size():
dialog = QuestionDialog(
gettext(
"Opening a new model will flush the"
" currently loaded model.\nAny changes"
" made will not be saved. Do you want to"
" continue?"
),
parent=main_window.window,
)
answer = dialog.answer
dialog.destroy()
if not answer:
return
element_factory.flush()
with element_factory.block_events():
model = element_factory.create(UML.Package)
model.name = gettext("New model")
diagram = element_factory.create(UML.Diagram)
diagram.package = model
diagram.name = gettext("main")
self.filename = None
element_factory.model_ready()
# main_window.select_element(diagram)
# main_window.show_diagram(diagram)
@action(name="file-new-template")
def action_new_from_template(self):
"""This menu action opens the new model from template dialog."""
filters = [
{"name": gettext("Gaphor Models"), "pattern": "*.gaphor"},
{"name": gettext("All Files"), "pattern": "*"},
]
file_dialog = FileDialog(
gettext("New Gaphor Model From Template"), filters=filters
)
filename = file_dialog.selection
file_dialog.destroy()
log.debug(filename)
if filename:
self.load(filename)
self.filename = None
@action(name="file-open", shortcut="<Primary>o")
def action_open(self):
"""This menu action opens the standard model open dialog."""
filters = [
{"name": gettext("Gaphor Models"), "pattern": "*.gaphor"},
{"name": gettext("All Files"), "pattern": "*"},
]
file_dialog = FileDialog(gettext("Open Gaphor Model"), filters=filters)
filename = file_dialog.selection
file_dialog.destroy()
log.debug(filename)
if filename:
self.load(filename)
@action(name="file-open-recent")
def action_open_recent(self, filename: str):
self.load(filename)
@action(name="file-save", shortcut="<Primary>s")
def action_save(self):
"""

View File

@ -45,8 +45,8 @@ def create_hamburger_model(export_menu, tools_menu):
model = Gio.Menu.new()
part = Gio.Menu.new()
part.append(gettext("New"), "win.file-new")
part.append(gettext("New from Template"), "win.file-new-template")
part.append(gettext("New"), "app.file-new")
part.append(gettext("New from Template"), "app.file-new-template")
model.append_section(None, part)
part = Gio.Menu.new()
@ -164,7 +164,7 @@ class MainWindow(Service, ActionProvider):
button_box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
button_box.get_style_context().add_class("linked")
button_box.pack_start(button(gettext("Open"), "win.file-open"), False, False, 0)
button_box.pack_start(button(gettext("Open"), "app.file-open"), False, False, 0)
button_box.pack_start(create_recent_files_button(), False, False, 0)
button_box.show()
header.pack_start(button_box)

View File

@ -49,7 +49,7 @@ class RecentFilesMenu(Gio.Menu):
for item in recent_manager.get_items():
if APPLICATION_ID in item.get_applications():
menu_item = Gio.MenuItem.new(
item.get_uri_display().replace(HOME, "~"), "win.file-open-recent"
item.get_uri_display().replace(HOME, "~"), "app.file-open-recent"
)
filename, _host = GLib.filename_from_uri(item.get_uri())
menu_item.set_attribute_value(

View File

@ -64,6 +64,7 @@ gaphorconvert = 'gaphor.tools.gaphorconvert:main'
[tool.poetry.plugins."gaphor.appservices"]
"session" = "gaphor.services.session:Session"
"help" = "gaphor.services.helpservice:HelpService"
"app_file_manager" = "gaphor.ui.appfilemanager:AppFileManager"
[tool.poetry.plugins."gaphor.services"]
"component_registry" = "gaphor.services.componentregistry:ComponentRegistry"

View File

@ -14,6 +14,7 @@ if __name__ == "__main__":
from gaphor.services.session import Session
from gaphor.services.undomanager import UndoManager
from gaphor.ui.elementeditor import ElementEditor
from gaphor.ui.appfilemanager import AppFileManager
from gaphor.ui.filemanager import FileManager
from gaphor.ui.mainwindow import Diagrams
from gaphor.ui.mainwindow import MainWindow