File: Window.py

package info (click to toggle)
mugshot 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,016 kB
  • sloc: python: 1,881; xml: 36; makefile: 7
file content (100 lines) | stat: -rw-r--r-- 4,007 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#   Mugshot - Lightweight user configuration utility
#   Copyright (C) 2013-2020 Sean Davis <sean@bluesabre.org>
#
#   This program is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranties of
#   MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
#   PURPOSE.  See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import logging

from gi.repository import Gio, Gtk  # pylint: disable=E0611

from . helpers import get_builder, show_uri

logger = logging.getLogger('mugshot_lib')


class Window(Gtk.Window):

    """This class is meant to be subclassed by MugshotWindow. It provides
    common functions and some boilerplate."""
    __gtype_name__ = "Window"

    # To construct a new instance of this method, the following notable
    # methods are called in this order:
    # __new__(cls)
    # __init__(self)
    # finish_initializing(self, builder)
    # __init__(self)
    #
    # For this reason, it's recommended you leave __init__ empty and put
    # your initialization code in finish_initializing

    def __new__(cls):
        """Special static method that's automatically called by Python when
        constructing a new instance of this class.

        Returns a fully instantiated BaseMugshotWindow object.
        """
        builder = get_builder('MugshotWindow')
        new_object = builder.get_object("mugshot_window")
        new_object.finish_initializing(builder)
        return new_object

    def finish_initializing(self, builder):
        """Called while initializing this instance in __new__

        finish_initializing should be called after parsing the UI definition
        and creating a MugshotWindow object with it in order to finish
        initializing the start of the new MugshotWindow instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self, True)
        self.CameraDialog = None  # class
        self.camera_dialog = None  # instance

        self.settings = Gio.Settings.new("org.bluesabre.mugshot")
        self.settings.connect('changed', self.on_preferences_changed)

        self.tmpfile = None

    def on_help_activate(self, widget, data=None):
        """Show the Help documentation when Help is clicked."""
        show_uri(self, "https://github.com/bluesabre/mugshot/wiki")

    def on_menu_camera_activate(self, widget, data=None):
        """Display the camera window for mugshot."""
        if self.camera_dialog is not None:
            logger.debug('show existing camera_dialog')
            self.camera_dialog.show()
        elif self.CameraDialog is not None:
            logger.debug('create new camera_dialog')
            self.camera_dialog = self.CameraDialog()  # pylint: disable=E1102
            self.camera_dialog.connect(
                'apply', self.on_camera_dialog_apply)  # pylint: disable=E1101
            self.camera_dialog.show()

    def on_destroy(self, widget, data=None):
        """Called when the MugshotWindow is closed."""
        # Clean up code for saving application state should be added here.
        if self.tmpfile and os.path.isfile(self.tmpfile.name):
            os.remove(self.tmpfile.name)
        Gtk.main_quit()

    def on_preferences_changed(self, settings, key, data=None):
        """Log preference updates."""
        logger.debug('preference changed: %s = %s' %
                     (key, str(settings.get_value(key))))