Use GTK Composite Templates for GUI elements to clean up and simplify the code for widgets and all UI elements. This includes splitting the large “gtk.glade” file into smaller .ui files and the large “widgets.py” file into smaller .py files.
122 lines
3.4 KiB
Python
122 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
|
|
import logging
|
|
import urllib
|
|
|
|
import gi
|
|
gi.require_version('Gtk', '3.0')
|
|
from gi.repository import Gio, Gtk, Gdk, GLib
|
|
|
|
from mcg.window import Window
|
|
from mcg.infodialog import InfoDialog
|
|
|
|
|
|
|
|
|
|
class Application(Gtk.Application):
|
|
TITLE = "CoverGrid"
|
|
ID = 'de.coderkun.mcg'
|
|
DOMAIN = 'mcg'
|
|
|
|
|
|
def _get_option(shortname, longname, description):
|
|
option = GLib.OptionEntry()
|
|
option.short_name = ord(shortname)
|
|
option.long_name = longname
|
|
option.description = description
|
|
return option
|
|
|
|
|
|
def __init__(self):
|
|
Gtk.Application.__init__(self, application_id=Application.ID, flags=Gio.ApplicationFlags.FLAGS_NONE)
|
|
self._window = None
|
|
self._info_dialog = None
|
|
self._verbosity = logging.WARNING
|
|
self.add_main_option_entries([
|
|
Application._get_option("v", "verbose", "Be verbose: show info messages"),
|
|
Application._get_option("d", "debug", "Enable debugging: show debug messages")
|
|
])
|
|
self.connect('handle-local-options', self.handle_local_options)
|
|
|
|
|
|
def handle_local_options(self, widget, options):
|
|
if options.contains("debug") and options.lookup_value('debug'):
|
|
self._verbosity = logging.DEBUG
|
|
elif options.contains("verbose") and options.lookup_value('verbose'):
|
|
self._verbosity = logging.INFO
|
|
return -1
|
|
|
|
|
|
def do_startup(self):
|
|
Gtk.Application.do_startup(self)
|
|
self._setup_logging()
|
|
self._load_settings()
|
|
self._set_default_settings()
|
|
self._load_css()
|
|
self._setup_actions()
|
|
self._load_appmenu()
|
|
|
|
|
|
def do_activate(self):
|
|
Gtk.Application.do_activate(self)
|
|
if not self._window:
|
|
self._window = Window(self, Application.TITLE, self._settings)
|
|
self._window.present()
|
|
|
|
|
|
def on_menu_info(self, action, value):
|
|
if not self._info_dialog:
|
|
self._info_dialog = InfoDialog()
|
|
self._info_dialog.run()
|
|
self._info_dialog.hide()
|
|
|
|
|
|
def on_menu_quit(self, action, value):
|
|
self.quit()
|
|
|
|
|
|
def _setup_logging(self):
|
|
logging.basicConfig(
|
|
level=self._verbosity,
|
|
format="%(asctime)s %(levelname)s: %(message)s"
|
|
)
|
|
|
|
|
|
def _load_settings(self):
|
|
self._settings = Gio.Settings.new(Application.ID)
|
|
|
|
|
|
def _set_default_settings(self):
|
|
settings = Gtk.Settings.get_default()
|
|
settings.set_property('gtk-application-prefer-dark-theme', True)
|
|
|
|
|
|
def _load_css(self):
|
|
styleProvider = Gtk.CssProvider()
|
|
styleProvider.load_from_resource(self._get_resource_path('gtk.css'))
|
|
Gtk.StyleContext.add_provider_for_screen(
|
|
Gdk.Screen.get_default(),
|
|
styleProvider,
|
|
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
|
|
)
|
|
|
|
|
|
def _setup_actions(self):
|
|
action = Gio.SimpleAction.new("info", None)
|
|
action.connect('activate', self.on_menu_info)
|
|
self.add_action(action)
|
|
action = Gio.SimpleAction.new("quit", None)
|
|
action.connect('activate', self.on_menu_quit)
|
|
self.add_action(action)
|
|
|
|
|
|
def _load_appmenu(self):
|
|
builder = Gtk.Builder()
|
|
builder.set_translation_domain(Application.DOMAIN)
|
|
builder.add_from_resource(self._get_resource_path('ui/gtk.menu.ui'))
|
|
self.set_app_menu(builder.get_object('app-menu'))
|
|
|
|
|
|
def _get_resource_path(self, path):
|
|
return "/{}/{}".format(Application.ID.replace('.', '/'), path)
|