Example #1
0
def _run():
    # Directory which holds additional scripts
    basedir = os.path.join(os.path.dirname(__file__), 'scripts')

    groupId = screen.groupId.decode()

    # Setup for file picker dialog
    buttonOkay = make_button('OK')
    buttonCancel = make_button('Cancel')
    buttonYes = make_button('Yes')
    buttonNo = make_button('No')
    buttonCont = make_button('Run another script', icon=Dialog.ICON_OPEN_LINK)
    buttonExit = make_button('Exit', icon=Dialog.ICON_DELETE)

    # Setup additional dialogs
    dialogRun = Dialog(42, Dialog.TYPE_ALERT, buttons=[buttonCancel, buttonOkay], groupId=groupId)
    dialogFail = Dialog(42, Dialog.TYPE_ALERT, buttons=[buttonOkay], groupId=groupId)

    # Main loop to allow multiple scripts to be run
    run = True
    while run:
        # Get list of scripts
        files = []
        dirlist = os.listdir(basedir)
        for fname in dirlist:
            if fname.endswith('.py') and not fname.startswith('.') and not fname.startswith('_'):
                files.append(fname)
        files.sort()
        dialogPicker = Dialog(42, Dialog.TYPE_POPUP, buttons=[buttonCancel, buttonOkay], items=files, groupId=groupId, bkgAlpha=0.25)

        # Prompt for script to run
        results = dialogPicker.show_for_response(titleText='Select a script', messageText='Enter a file to run')
        if results['selectedIndex'] == 0:
            break
        if not results['selectedIndices']:
            print('no selection made')
            continue

        fname = files[results['selectedIndices'][0]]
        script = os.path.join(basedir, fname)

        # Check if file exists
        if os.path.isfile(script):
            print('--------------- running', fname)
            namespace = {}
            try:
                exec(open(script).read(), namespace)
                namespace['run']()
            finally:
                # flush print output buffer
                sys.stdout.flush()
        else:
            dialogFail.show_for_response(titleText='Error', messageText='File not found')
Example #2
0
    def make_dialog(self, settings):
        if self.dlg:
            self.cancel_dialog()

        dtype = settings.pop('dtype')
        qDebug('dialog of type {}'.format(dtype))
        if 'buttons' not in settings:
            settings['buttons'] = [{Dialog.BUTTON_LABEL: Dialog.LABEL_OK}]
        self.dlg = Dialog(self.get_dialog_id(), dtype, **settings)
Example #3
0
class App(Application):

    def __init__(self):
        super(App, self).__init__()
        self.dlg = None


    def get_dialog_id(self):
        self.dlg_id = getattr(self, 'dlg_id', 0) + 1
        return self.dlg_id


    @Slot('QVariant')
    def do_dialog(self, params):
        qDebug('do_dialog {}'.format(params))

        # :param position: one of "topCenter", "middleCenter", or "bottomCenter"
        #     (default is "middleCenter")
        # :param size: The size of the dialog box. One of "small", "medium",
        #     "large", "tall" or "full" (default is "medium")

        settings = {}
        settings['dtype'] = getattr(Dialog, 'TYPE_{}'.format(params.get('type', 'alert').upper()))

        settings['bkgAlpha'] = 0.5
        try:
            settings['buttons'] = params['buttons']
        except KeyError:
            pass

        if not params.get('modal'):
            # dialogs need to have this in order not to be system-modal
            from bbpy.app import bpsThread
            qDebug('sweet! windowGroup is {}'.format(bpsThread.windowGroup))
            settings['groupId'] = bpsThread.windowGroup

        settings['position'] = params.get('position', 'middleCenter')
        settings['size'] = params.get('size', 'medium')
        try:
            settings['filter'] = params['filter']
        except KeyError:
            pass

        settings['titleText'] = params.get('title', 'No Title')
        # :param titleHtmlText: title string to display in html (this attribute
        #     trumps ``titleText``)

        try:
            settings['messageText'] = params['messageText']
        except KeyError:
            pass

        # files = ['testing', 'this']
        # buttonOkay = make_button('OK')
        # buttonCancel = make_button('Cancel')
        # dialogPicker = Dialog(42, Dialog.TYPE_POPUP,
        #     buttons=[buttonCancel, buttonOkay], groupId=groupId, items=files, bkgAlpha=0.25)
        # dialogPicker.show_for_response(titleText='Select a script',
        #     messageText='Enter a file to run')

        # qDebug('showed the dialog {}'.format(dialogPicker))

        self.make_dialog(settings)
        self.show_dialog()


    def make_dialog(self, settings):
        if self.dlg:
            self.cancel_dialog()

        dtype = settings.pop('dtype')
        qDebug('dialog of type {}'.format(dtype))
        if 'buttons' not in settings:
            settings['buttons'] = [{Dialog.BUTTON_LABEL: Dialog.LABEL_OK}]
        self.dlg = Dialog(self.get_dialog_id(), dtype, **settings)


    def show_dialog(self):
        self.dlg.show_for_response()


    def cancel_dialog(self):
        self.dlg.cancel()
        self.dlg = None


    @Slot()
    def onLoaded(self):
        pass


    def run(self):
        v = self.create_view()
        v.statusChanged.connect(self.onLoaded)

        rc = v.engine().rootContext()
        rc.setContextProperty('engine', self)

        p = os.path.join(os.path.dirname(__file__), 'main.qml')
        v.setSource(p)
        v.showFullScreen()

        self.root = v.rootObject()

        super(App, self).run()