source: orange/Orange/OrangeCanvas/main.py @ 11147:5ddec5f677c1

Revision 11147:5ddec5f677c1, 6.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixed rejected open/browse actions in welcome dialog.

Line 
1"""
2Orange Canvas main entry point
3
4"""
5
6import os
7import sys
8import logging
9import optparse
10import cPickle
11
12import pkg_resources
13
14from PyQt4.QtGui import QFont, QColor
15from PyQt4.QtCore import QRect, QSettings
16
17from Orange import OrangeCanvas
18from Orange.OrangeCanvas.application.application import CanvasApplication
19from Orange.OrangeCanvas.application.canvasmain import CanvasMainWindow
20
21from Orange.OrangeCanvas.gui.splashscreen import SplashScreen, QPixmap
22from Orange.OrangeCanvas.config import open_config, cache_dir
23from Orange.OrangeCanvas import config
24
25from Orange.OrangeCanvas.registry import qt
26from Orange.OrangeCanvas.registry import WidgetRegistry, set_global_registry
27from Orange.OrangeCanvas.registry import cache
28
29log = logging.getLogger(__name__)
30
31
32def qt_logging_handle(msg_type, message):
33    print msg_type, message
34
35
36def main(argv=None):
37    if argv is None:
38        argv = sys.argv[1:]
39
40    usage = "usage: %prog [options] [scheme_file]"
41    parser = optparse.OptionParser(usage=usage)
42
43    parser.add_option("--no-discovery",
44                      action="store_true",
45                      help="Don't run widget discovery "
46                           "(use full cache instead)")
47
48    parser.add_option("--force-discovery",
49                      action="store_true",
50                      help="Force full widget discovery "
51                           "(invalidate cache)")
52    parser.add_option("--no-welcome",
53                      action="store_true",
54                      help="Don't show welcome dialog.")
55    parser.add_option("--no-splash",
56                      action="store_true",
57                      help="Don't show splash screen.")
58    parser.add_option("-l", "--log-level",
59                      help="Logging level (0, 1, 2, 3)",
60                      type="int", default=1)
61    parser.add_option("--style",
62                      help="QStyle to use",
63                      type="str", default=None)
64    parser.add_option("--stylesheet",
65                      help="Application level CSS style sheet to use",
66                      type="str", default="orange.qss")
67    parser.add_option("--qt",
68                      help="Additional arguments for QApplication",
69                      type="str", default=None)
70
71    (options, args) = parser.parse_args(argv)
72
73    open_config()
74
75    levels = [logging.CRITICAL,
76              logging.WARN,
77              logging.INFO,
78              logging.DEBUG]
79
80    logging.basicConfig(level=levels[options.log_level])
81
82    log.info("Starting 'Orange Canvas' application.")
83
84    qt_argv = ["orange-canvas"]
85
86    if options.style is not None:
87        qt_argv += ["-style", options.style]
88
89    if options.qt is not None:
90        qt_argv += options.qt.split()
91
92    config.init()
93    settings = QSettings()
94
95    log.debug("Starting CanvasApplicaiton with argv = %r.", qt_argv)
96    app = CanvasApplication(qt_argv)
97
98    stylesheet = options.stylesheet
99    stylesheet_string = None
100
101    if stylesheet != "none":
102        if os.path.isfile(stylesheet):
103            stylesheet_string = open(stylesheet, "rb").read()
104        else:
105            if not os.path.splitext(stylesheet)[1]:
106                # no extension
107                stylesheet = os.path.extsep.join([stylesheet, "qss"])
108
109            pkg_name = OrangeCanvas.__name__
110            resource = os.path.join("styles", stylesheet)
111            if pkg_resources.resource_exists(pkg_name, resource):
112                stylesheet_string = pkg_resources.resource_string(
113                                        pkg_name, resource)
114            else:
115                log.info("%r style sheet not found.", stylesheet)
116
117    if stylesheet_string is not None:
118        app.setStyleSheet(stylesheet_string)
119
120    canvas_window = CanvasMainWindow()
121    canvas_window.resize(1024, 650)
122
123    if not options.force_discovery:
124        reg_cache = cache.registry_cache()
125    else:
126        reg_cache = None
127
128    widget_discovery = qt.QtWidgetDiscovery(cached_descriptions=reg_cache)
129
130    widget_registry = qt.QtWidgetRegistry()
131
132    widget_discovery.found_category.connect(
133        widget_registry.register_category
134    )
135    widget_discovery.found_widget.connect(
136        widget_registry.register_widget
137    )
138
139    want_splash = \
140        settings.value("startup/show-splash-screen", True).toPyObject()
141
142    if want_splash:
143        pm = QPixmap(pkg_resources.resource_filename(
144                        __name__, "icons/orange-splash-screen.png")
145                     )
146        # Text rectangle in which to fit the message.
147        rect = QRect(88, 193, 200, 20)
148        splash_screen = SplashScreen(pixmap=pm, textRect=rect)
149        splash_screen.setFont(QFont("Helvetica", 12))
150        color = QColor("#FFD39F")
151
152        def show_message(message):
153            splash_screen.showMessage(message, color=color)
154
155        widget_discovery.discovery_start.connect(splash_screen.show)
156        widget_discovery.discovery_process.connect(show_message)
157        widget_discovery.discovery_finished.connect(splash_screen.hide)
158
159    log.info("Running widget discovery process.")
160
161    cache_filename = os.path.join(cache_dir(), "widget-registry.pck")
162    if options.no_discovery:
163        widget_registry = cPickle.load(open(cache_filename, "rb"))
164        widget_registry = qt.QtWidgetRegistry(widget_registry)
165    else:
166        widget_discovery.run()
167        # Store cached descriptions
168        cache.save_registry_cache(widget_discovery.cached_descriptions)
169        cPickle.dump(WidgetRegistry(widget_registry),
170                     open(cache_filename, "wb"))
171    set_global_registry(widget_registry)
172    canvas_window.set_widget_registry(widget_registry)
173    canvas_window.show()
174
175    want_welcome = \
176        settings.value("welcomedialog/show-at-startup", True).toBool() \
177        and not options.no_welcome
178
179    canvas_window.raise_()
180
181    if want_welcome and not args:
182        canvas_window.welcome_dialog()
183
184    elif args:
185        log.info("Loading a scheme from the command line argument %r",
186                 args[0])
187        canvas_window.load_scheme(args[0])
188
189    log.info("Entering main event loop.")
190    try:
191        status = app.exec_()
192    except BaseException:
193        log.error("Error in main event loop.", exc_info=True)
194
195    canvas_window.deleteLater()
196    app.processEvents()
197    app.flush()
198    del canvas_window
199    del app
200    return status
201
202
203if __name__ == "__main__":
204    sys.exit(main())
Note: See TracBrowser for help on using the repository browser.