~macaco-dev/macaco/trunk

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE

import sys
import os
import gtk

# Check if we are working in the source tree or from the installed 
# package and mangle the python path accordingly
if os.path.dirname(sys.argv[0]) != ".":
    if sys.argv[0][0] == "/":
        fullPath = os.path.dirname(sys.argv[0])
    else:
        fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0])
else:
    fullPath = os.getcwd()
sys.path.insert(0, os.path.dirname(fullPath))

from harmony import AboutHarmonyDialog, PreferencesHarmonyDialog
from harmony.harmonyconfig import getdatapath

class HarmonyWindow(gtk.Window):
    __gtype_name__ = "HarmonyWindow"

    def __init__(self):
        """__init__ - This function is typically not called directly.
        Creation a HarmonyWindow requires redeading the associated ui
        file and parsing the ui definition extrenally,
        and then calling HarmonyWindow.finish_initializing().

        Use the convenience function NewHarmonyWindow to create
        HarmonyWindow object.

        """
        pass

    def finish_initializing(self, builder):
        """finish_initalizing should be called after parsing the ui definition
        and creating a HarmonyWindow object with it in order to finish
        initializing the start of the new HarmonyWindow instance.

        """
        #get a reference to the builder and set up the signals
        self.builder = builder
        self.builder.connect_signals(self)

        #uncomment the following code to read in preferences at start up
        #dlg = PreferencesHarmonyDialog.NewPreferencesHarmonyDialog()
        #self.preferences = dlg.get_preferences()

        #code for other initialization actions should be added here

    def about(self, widget, data=None):
        """about - display the about box for harmony """
        about = AboutHarmonyDialog.NewAboutHarmonyDialog()
        response = about.run()
        about.destroy()

    def preferences(self, widget, data=None):
        """preferences - display the preferences window for harmony """
        prefs = PreferencesHarmonyDialog.NewPreferencesHarmonyDialog()
        response = prefs.run()
        if response == gtk.RESPONSE_OK:
            #make any updates based on changed preferences here
            pass
        prefs.destroy()

    def quit(self, widget, data=None):
        """quit - signal handler for closing the HarmonyWindow"""
        self.destroy()

    def on_destroy(self, widget, data=None):
        """on_destroy - called when the HarmonyWindow is close. """
        #clean up code for saving application state should be added here

        gtk.main_quit()

def NewHarmonyWindow():
    """NewHarmonyWindow - returns a fully instantiated
    HarmonyWindow object. Use this function rather than
    creating a HarmonyWindow directly.
    """

    #look for the ui file that describes the ui
    ui_filename = os.path.join(getdatapath(), 'ui', 'HarmonyWindow.ui')
    if not os.path.exists(ui_filename):
        ui_filename = None

    builder = gtk.Builder()
    builder.add_from_file(ui_filename)
    window = builder.get_object("harmony_window")
    window.finish_initializing(builder)
    return window

if __name__ == "__main__":
    #support for command line options
    import logging, optparse
    parser = optparse.OptionParser(version="%prog %ver")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages")
    (options, args) = parser.parse_args()

    #set the logging level to show debug messages
    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
        logging.debug('logging enabled')

    #run the application
    window = NewHarmonyWindow()
    window.show()
    gtk.main()