~mhall119/+junk/quickly-gtk

« back to all changes in this revision

Viewing changes to quickly_gtk_lib/Window.py

  • Committer: Michael Hall
  • Date: 2012-06-14 23:31:46 UTC
  • Revision ID: mhall119@ubuntu.com-20120614233146-0zj6ezr476nuy2pc
Initial project creation with Quickly!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# This file is in the public domain
 
4
### END LICENSE
 
5
 
 
6
from gi.repository import Gio, Gtk # pylint: disable=E0611
 
7
import logging
 
8
logger = logging.getLogger('quickly_gtk_lib')
 
9
 
 
10
from . helpers import get_builder, show_uri, get_help_uri
 
11
 
 
12
# This class is meant to be subclassed by QuicklyGtkWindow.  It provides
 
13
# common functions and some boilerplate.
 
14
class Window(Gtk.Window):
 
15
    __gtype_name__ = "Window"
 
16
 
 
17
    # To construct a new instance of this method, the following notable 
 
18
    # methods are called in this order:
 
19
    # __new__(cls)
 
20
    # __init__(self)
 
21
    # finish_initializing(self, builder)
 
22
    # __init__(self)
 
23
    #
 
24
    # For this reason, it's recommended you leave __init__ empty and put
 
25
    # your initialization code in finish_initializing
 
26
    
 
27
    def __new__(cls):
 
28
        """Special static method that's automatically called by Python when 
 
29
        constructing a new instance of this class.
 
30
        
 
31
        Returns a fully instantiated BaseQuicklyGtkWindow object.
 
32
        """
 
33
        builder = get_builder('QuicklyGtkWindow')
 
34
        new_object = builder.get_object("quickly_gtk_window")
 
35
        new_object.finish_initializing(builder)
 
36
        return new_object
 
37
 
 
38
    def finish_initializing(self, builder):
 
39
        """Called while initializing this instance in __new__
 
40
 
 
41
        finish_initializing should be called after parsing the UI definition
 
42
        and creating a QuicklyGtkWindow object with it in order to finish
 
43
        initializing the start of the new QuicklyGtkWindow instance.
 
44
        """
 
45
        # Get a reference to the builder and set up the signals.
 
46
        self.builder = builder
 
47
        self.ui = builder.get_ui(self, True)
 
48
        self.PreferencesDialog = None # class
 
49
        self.preferences_dialog = None # instance
 
50
        self.AboutDialog = None # class
 
51
 
 
52
        self.settings = Gio.Settings("net.launchpad.quickly-gtk")
 
53
        self.settings.connect('changed', self.on_preferences_changed)
 
54
 
 
55
        # Optional Launchpad integration
 
56
        # This shouldn't crash if not found as it is simply used for bug reporting.
 
57
        # See https://wiki.ubuntu.com/UbuntuDevelopment/Internationalisation/Coding
 
58
        # for more information about Launchpad integration.
 
59
        try:
 
60
            from gi.repository import LaunchpadIntegration # pylint: disable=E0611
 
61
            LaunchpadIntegration.add_items(self.ui.helpMenu, 1, True, True)
 
62
            LaunchpadIntegration.set_sourcepackagename('quickly-gtk')
 
63
        except ImportError:
 
64
            pass
 
65
 
 
66
        # Optional application indicator support
 
67
        # Run 'quickly add indicator' to get started.
 
68
        # More information:
 
69
        #  http://owaislone.org/quickly-add-indicator/
 
70
        #  https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators
 
71
        try:
 
72
            from quickly_gtk import indicator
 
73
            # self is passed so methods of this class can be called from indicator.py
 
74
            # Comment this next line out to disable appindicator
 
75
            self.indicator = indicator.new_application_indicator(self)
 
76
        except ImportError:
 
77
            pass
 
78
 
 
79
    def on_mnu_contents_activate(self, widget, data=None):
 
80
        show_uri(self, "ghelp:%s" % get_help_uri())
 
81
 
 
82
    def on_mnu_about_activate(self, widget, data=None):
 
83
        """Display the about box for quickly-gtk."""
 
84
        if self.AboutDialog is not None:
 
85
            about = self.AboutDialog() # pylint: disable=E1102
 
86
            response = about.run()
 
87
            about.destroy()
 
88
 
 
89
    def on_mnu_preferences_activate(self, widget, data=None):
 
90
        """Display the preferences window for quickly-gtk."""
 
91
 
 
92
        """ From the PyGTK Reference manual
 
93
           Say for example the preferences dialog is currently open,
 
94
           and the user chooses Preferences from the menu a second time;
 
95
           use the present() method to move the already-open dialog
 
96
           where the user can see it."""
 
97
        if self.preferences_dialog is not None:
 
98
            logger.debug('show existing preferences_dialog')
 
99
            self.preferences_dialog.present()
 
100
        elif self.PreferencesDialog is not None:
 
101
            logger.debug('create new preferences_dialog')
 
102
            self.preferences_dialog = self.PreferencesDialog() # pylint: disable=E1102
 
103
            self.preferences_dialog.connect('destroy', self.on_preferences_dialog_destroyed)
 
104
            self.preferences_dialog.show()
 
105
        # destroy command moved into dialog to allow for a help button
 
106
 
 
107
    def on_mnu_close_activate(self, widget, data=None):
 
108
        """Signal handler for closing the QuicklyGtkWindow."""
 
109
        self.destroy()
 
110
 
 
111
    def on_destroy(self, widget, data=None):
 
112
        """Called when the QuicklyGtkWindow is closed."""
 
113
        # Clean up code for saving application state should be added here.
 
114
        Gtk.main_quit()
 
115
 
 
116
    def on_preferences_changed(self, settings, key, data=None):
 
117
        logger.debug('preference changed: %s = %s' % (key, str(settings.get_value(key))))
 
118
 
 
119
    def on_preferences_dialog_destroyed(self, widget, data=None):
 
120
        '''only affects gui
 
121
        
 
122
        logically there is no difference between the user closing,
 
123
        minimising or ignoring the preferences dialog'''
 
124
        logger.debug('on_preferences_dialog_destroyed')
 
125
        # to determine whether to create or present preferences_dialog
 
126
        self.preferences_dialog = None
 
127