~suutari-olli/openlp/escape-fixes-1294111-1497637

« back to all changes in this revision

Viewing changes to openlp/core/__init__.py

  • Committer: suutari-olli
  • Date: 2016-01-07 02:53:59 UTC
  • mfrom: (2557.2.31 openlp)
  • Revision ID: suutari.olli@gmail.com-20160107025359-q2feybbwxaoihqxr
Merge to trunk on 1/7/2015.

I noticed this branch also seems to be fixing this bug:
https://bugs.launchpad.net/openlp/+bug/1531691

However, escape item still remains buggy with problems related to resuming
video and presentations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
###############################################################################
5
5
# OpenLP - Open Source Lyrics Projection                                      #
6
6
# --------------------------------------------------------------------------- #
7
 
# Copyright (c) 2008-2015 OpenLP Developers                                   #
 
7
# Copyright (c) 2008-2016 OpenLP Developers                                   #
8
8
# --------------------------------------------------------------------------- #
9
9
# This program is free software; you can redistribute it and/or modify it     #
10
10
# under the terms of the GNU General Public License as published by the Free  #
30
30
import os
31
31
import sys
32
32
import logging
33
 
from optparse import OptionParser
 
33
import argparse
34
34
from traceback import format_exception
35
35
import shutil
36
36
import time
37
 
from PyQt4 import QtCore, QtGui
 
37
from PyQt5 import QtCore, QtGui, QtWidgets
38
38
 
39
39
from openlp.core.common import Registry, OpenLPMixin, AppLocation, Settings, UiStrings, check_directory_exists, \
40
40
    is_macosx, is_win, translate
76
76
"""
77
77
 
78
78
 
79
 
class OpenLP(OpenLPMixin, QtGui.QApplication):
 
79
class OpenLP(OpenLPMixin, QtWidgets.QApplication):
80
80
    """
81
81
    The core application class. This class inherits from Qt's QApplication
82
82
    class in order to provide the core of the application.
84
84
 
85
85
    args = []
86
86
 
87
 
    def exec_(self):
 
87
    def exec(self):
88
88
        """
89
89
        Override exec method to allow the shared memory to be released on exit
90
90
        """
91
91
        self.is_event_loop_active = True
92
 
        result = QtGui.QApplication.exec_()
 
92
        result = QtWidgets.QApplication.exec()
93
93
        self.shared_memory.detach()
94
94
        return result
95
95
 
113
113
        if not has_run_wizard:
114
114
            ftw = FirstTimeForm()
115
115
            ftw.initialize(screens)
116
 
            if ftw.exec_() == QtGui.QDialog.Accepted:
 
116
            if ftw.exec() == QtWidgets.QDialog.Accepted:
117
117
                Settings().setValue('core/has run wizard', True)
118
118
            elif ftw.was_cancelled:
119
119
                QtCore.QCoreApplication.exit()
159
159
            version.start()
160
160
        self.main_window.is_display_blank()
161
161
        self.main_window.app_startup()
162
 
        return self.exec_()
 
162
        return self.exec()
163
163
 
164
164
    def is_already_running(self):
165
165
        """
167
167
        """
168
168
        self.shared_memory = QtCore.QSharedMemory('OpenLP')
169
169
        if self.shared_memory.attach():
170
 
            status = QtGui.QMessageBox.critical(None, UiStrings().Error, UiStrings().OpenLPStart,
171
 
                                                QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
172
 
                                                                                  QtGui.QMessageBox.No))
173
 
            if status == QtGui.QMessageBox.No:
 
170
            status = QtWidgets.QMessageBox.critical(None, UiStrings().Error, UiStrings().OpenLPStart,
 
171
                                                    QtWidgets.QMessageBox.StandardButtons(QtWidgets.QMessageBox.Yes |
 
172
                                                                                          QtWidgets.QMessageBox.No))
 
173
            if status == QtWidgets.QMessageBox.No:
174
174
                return True
175
175
            return False
176
176
        else:
192
192
            self.exception_form = ExceptionForm()
193
193
        self.exception_form.exception_text_edit.setPlainText(''.join(format_exception(exc_type, value, traceback)))
194
194
        self.set_normal_cursor()
195
 
        self.exception_form.exec_()
 
195
        self.exception_form.exec()
196
196
 
197
197
    def backup_on_upgrade(self, has_run_wizard):
198
198
        """
207
207
            Settings().setValue('core/application version', openlp_version)
208
208
        # If data_version is different from the current version ask if we should backup the data folder
209
209
        elif data_version != openlp_version:
210
 
            if QtGui.QMessageBox.question(None, translate('OpenLP', 'Backup'),
211
 
                                          translate('OpenLP', 'OpenLP has been upgraded, '
212
 
                                                              'do you want to create a backup of OpenLPs data folder?'),
213
 
                                          QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
214
 
                                          QtGui.QMessageBox.Yes) == QtGui.QMessageBox.Yes:
 
210
            if QtWidgets.QMessageBox.question(None, translate('OpenLP', 'Backup'),
 
211
                                              translate('OpenLP', 'OpenLP has been upgraded, do you want to create '
 
212
                                                                  'a backup of OpenLPs data folder?'),
 
213
                                              QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
 
214
                                              QtWidgets.QMessageBox.Yes) == QtWidgets.QMessageBox.Yes:
215
215
                # Create copy of data folder
216
216
                data_folder_path = AppLocation.get_data_path()
217
217
                timestamp = time.strftime("%Y%m%d-%H%M%S")
219
219
                try:
220
220
                    shutil.copytree(data_folder_path, data_folder_backup_path)
221
221
                except OSError:
222
 
                    QtGui.QMessageBox.warning(None, translate('OpenLP', 'Backup'),
223
 
                                              translate('OpenLP', 'Backup of the data folder failed!'))
 
222
                    QtWidgets.QMessageBox.warning(None, translate('OpenLP', 'Backup'),
 
223
                                                  translate('OpenLP', 'Backup of the data folder failed!'))
224
224
                    return
225
 
                QtGui.QMessageBox.information(None, translate('OpenLP', 'Backup'),
226
 
                                              translate('OpenLP', 'A backup of the data folder has been created at %s')
227
 
                                              % data_folder_backup_path)
 
225
                QtWidgets.QMessageBox.information(None, translate('OpenLP', 'Backup'),
 
226
                                                  translate('OpenLP',
 
227
                                                            'A backup of the data folder has been created at %s')
 
228
                                                  % data_folder_backup_path)
228
229
            # Update the version in the settings
229
230
            Settings().setValue('core/application version', openlp_version)
230
231
 
271
272
                    self.main_window.setWindowState(self.main_window.windowState() & ~QtCore.Qt.WindowMinimized |
272
273
                                                    QtCore.Qt.WindowActive)
273
274
                    return True
274
 
        return QtGui.QApplication.event(self, event)
 
275
        return QtWidgets.QApplication.event(self, event)
275
276
 
276
277
 
277
278
def parse_options(args):
282
283
    :return: a tuple of parsed options of type optparse.Value and a list of remaining argsZ
283
284
    """
284
285
    # Set up command line options.
285
 
    usage = 'Usage: %prog [options] [qt-options]'
286
 
    parser = OptionParser(usage=usage)
287
 
    parser.add_option('-e', '--no-error-form', dest='no_error_form', action='store_true',
288
 
                      help='Disable the error notification form.')
289
 
    parser.add_option('-l', '--log-level', dest='loglevel', default='warning', metavar='LEVEL',
290
 
                      help='Set logging to LEVEL level. Valid values are "debug", "info", "warning".')
291
 
    parser.add_option('-p', '--portable', dest='portable', action='store_true',
292
 
                      help='Specify if this should be run as a portable app, off a USB flash drive (not implemented).')
293
 
    parser.add_option('-d', '--dev-version', dest='dev_version', action='store_true',
294
 
                      help='Ignore the version file and pull the version directly from Bazaar')
295
 
    parser.add_option('-s', '--style', dest='style', help='Set the Qt4 style (passed directly to Qt4).')
 
286
    parser = argparse.ArgumentParser(prog='openlp.py')
 
287
    parser.add_argument('-e', '--no-error-form', dest='no_error_form', action='store_true',
 
288
                        help='Disable the error notification form.')
 
289
    parser.add_argument('-l', '--log-level', dest='loglevel', default='warning', metavar='LEVEL',
 
290
                        help='Set logging to LEVEL level. Valid values are "debug", "info", "warning".')
 
291
    parser.add_argument('-p', '--portable', dest='portable', action='store_true',
 
292
                        help='Specify if this should be run as a portable app, '
 
293
                             'off a USB flash drive (not implemented).')
 
294
    parser.add_argument('-d', '--dev-version', dest='dev_version', action='store_true',
 
295
                        help='Ignore the version file and pull the version directly from Bazaar')
 
296
    parser.add_argument('-s', '--style', dest='style', help='Set the Qt5 style (passed directly to Qt5).')
 
297
    parser.add_argument('rargs', nargs='?', default=[])
296
298
    # Parse command line options and deal with them. Use args supplied pragmatically if possible.
297
299
    return parser.parse_args(args) if args else parser.parse_args()
298
300
 
318
320
 
319
321
    :param args: Some args
320
322
    """
321
 
    (options, args) = parse_options(args)
 
323
    args = parse_options(args)
322
324
    qt_args = []
323
 
    if options.loglevel.lower() in ['d', 'debug']:
 
325
    if args and args.loglevel.lower() in ['d', 'debug']:
324
326
        log.setLevel(logging.DEBUG)
325
 
    elif options.loglevel.lower() in ['w', 'warning']:
 
327
    elif args and args.loglevel.lower() in ['w', 'warning']:
326
328
        log.setLevel(logging.WARNING)
327
329
    else:
328
330
        log.setLevel(logging.INFO)
329
 
    if options.style:
330
 
        qt_args.extend(['-style', options.style])
 
331
    if args and args.style:
 
332
        qt_args.extend(['-style', args.style])
331
333
    # Throw the rest of the arguments at Qt, just in case.
332
 
    qt_args.extend(args)
 
334
    qt_args.extend(args.rargs)
333
335
    # Bug #1018855: Set the WM_CLASS property in X11
334
336
    if not is_win() and not is_macosx():
335
337
        qt_args.append('OpenLP')
339
341
    application = OpenLP(qt_args)
340
342
    application.setOrganizationName('OpenLP')
341
343
    application.setOrganizationDomain('openlp.org')
342
 
    if options.portable:
 
344
    application.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
 
345
    if args and args.portable:
343
346
        application.setApplicationName('OpenLPPortable')
344
347
        Settings.setDefaultFormat(Settings.IniFormat)
345
348
        # Get location OpenLPPortable.ini
371
374
    Settings().remove_obsolete_settings()
372
375
    # First time checks in settings
373
376
    if not Settings().value('core/has run wizard'):
374
 
        if not FirstTimeLanguageForm().exec_():
 
377
        if not FirstTimeLanguageForm().exec():
375
378
            # if cancel then stop processing
376
379
            sys.exit()
377
380
    # i18n Set Language
383
386
        application.installTranslator(default_translator)
384
387
    else:
385
388
        log.debug('Could not find default_translator.')
386
 
    if not options.no_error_form:
 
389
    if args and not args.no_error_form:
387
390
        sys.excepthook = application.hook_exception
388
391
    sys.exit(application.run(qt_args))