Esempio n. 1
0
class Widget(QWidget):
    signalProcessEvents = pyqtSignal()
    currentLanguageChanged = pyqtSignal()

    def __init__(self):
        super(Widget, self).__init__(None)
        # Set pixmaps resource before Main Window initialized
        self._resource = os.path.join(ctx.consts.theme_dir, ctx.flags.theme,
                                      ctx.consts.pixmaps_resource_file)
        if os.path.exists(self._resource):
            resource = QResource()
            resource.registerResource(self._resource)
        else:
            raise yali.Error, _("General",
                                "Pixmaps resources file doesn't exists")

        self.ui = Ui_YaliMain()
        self.ui.setupUi(self)

        self.font = 10
        self.animation_type = None

        self.screens = None
        self.screens_content = None

        self.pds_helper = HelpWidget(self.ui.scrollAreaWidgetContents)

        # shortcut to open help
        self.help_shortcut = QShortcut(QKeySequence(Qt.Key_F1), self)

        # shortcut to open debug window
        # self.debugShortCut = QtWidgets.QShortcut(QtWidgets.QKeySequence(Qt.Key_F2),self)

        # something funny
        self.tetris_shortcut = QShortcut(QKeySequence(Qt.Key_F6), self)
        self.cursor_shortcut = QShortcut(QKeySequence(Qt.Key_F7), self)
        self.theme_shortcut = QShortcut(QKeySequence(Qt.Key_F8), self)

        # shortcut to open a console
        self.console_shortcut = QShortcut(QKeySequence(Qt.Key_F11), self)

        # set style
        self._style = os.path.join(ctx.consts.theme_dir, ctx.flags.theme,
                                   ctx.consts.style_file)
        if os.path.exists(self._style):
            self.updateStyle()
        else:
            raise yali.Error, _("General", "Style file doesn't exists")

        # set screens content
        release_file = os.path.join(ctx.consts.branding_dir,
                                    ctx.flags.branding,
                                    ctx.consts.release_file)
        if os.path.exists(release_file):
            self.screens_content = yali.util.parse_branding_screens(
                release_file)
        else:
            raise yali.Error, _("General", "Release file doesn't exists")

        # move one step at a time
        self.step_increment = 1

        # ToolButton Popup Menu
        self.menu = QMenu()
        self.shutdown = self.menu.addAction(
            QIcon(QPixmap(":/images/system-shutdown.png")),
            _("General", "Turn Off Computer"))
        self.reboot = self.menu.addAction(
            QIcon(QPixmap(":/images/system-reboot.png")),
            _("General", "Restart Computer"))
        self.restart = self.menu.addAction(
            QIcon(QPixmap(":/images/system-yali-reboot.png")),
            _("General", "Restart YALI"))
        # self.menu.setDefaultAction(self.shutdown)
        self.ui.system_menu.setMenu(self.menu)
        self.ui.system_menu.setDefaultAction(self.shutdown)

        # Main Slots
        self.help_shortcut.activated.connect(self.pds_helper.toggleHelp)
        # self.debugShortCut.activated.connect(self.toggleDebug)
        self.console_shortcut.activated.connect(self.toggleConsole)
        self.cursor_shortcut.activated.connect(self.toggleCursor)
        self.theme_shortcut.activated.connect(self.toggleTheme)
        self.tetris_shortcut.activated.connect(self.toggleTetris)
        self.ui.buttonNext.clicked.connect(self.slotNext)
        self.ui.buttonBack.clicked.connect(self.slotBack)
        self.ui.toggleHelp.clicked.connect(self.pds_helper.toggleHelp)
        if not ctx.flags.install_type == ctx.STEP_FIRST_BOOT:
            self.ui.releaseNotes.clicked.connect(self.showReleaseNotes)
        else:
            self.ui.releaseNotes.hide()
        self.menu.triggered[QAction].connect(self.slotMenu)

        self.cmb = _("General", "right")
        self.dont_ask_again = False
        self.terminal = None
        self.tetris = None

        self.ui.helpContentFrame.hide()

        self.effect = QGraphicsOpacityEffect(self)
        self.ui.mainStack.setGraphicsEffect(self.effect)
        self.effect.setOpacity(1.0)

        self.anime = QTimer(self)
        self.anime.timeout.connect(self.animate)

    def mousePressEvent(self, event):
        if event.button() == Qt.RightButton and not self.dont_ask_again:
            if self.cmb == _("General", "left"):
                ocmb = _("General", "right")
            else:
                ocmb = _("General", "left")
            reply = QuestionDialog(
                _("General", "Mouse Settings"),
                _("General", "You just clicked the <b>%s</b> mouse button.") %
                self.cmb,
                _(
                    "General",
                    "Do you want to switch to the <b>%s</b> handed configuration?"
                ) % ocmb,
                dontAsk=True)
            if reply == "yes":
                yali.sysutils.setMouse(self.cmb)
                self.cmb = ocmb
            elif reply == "dontask":
                self.dont_ask_again = True

    def updateStyle(self):
        self.setStyleSheet(file(self._style).read())
        self.font = 10

    def setFontPlus(self):
        self.increaseFontSize(1)

    def setFontMinus(self):
        self.increaseFontSize(-1)

    def increaseFontSize(self, num):
        # We have to edit style sheet to set new fonts
        # Because if you use a style sheet in your application
        # ::setFont gets useless :( http://doc.trolltech.com/4.5/qapplication.html#setFont
        old = "QWidget{font:%dpt;}" % self.font
        self.font = self.font + num
        new = "QWidget{font:%dpt;}" % self.font
        self.setStyleSheet(self.styleSheet().replace(old, new))

    def slotMenu(self, action):
        if action == self.shutdown:
            reply = QuestionDialog(
                _("General", "Warning"),
                _("General", "Are you sure you want to "
                  "shut down your computer now?"))
            if reply == "yes":
                yali.util.shutdown()
        elif action == self.reboot:
            reply = QuestionDialog(
                _("General", "Warning"),
                _("General",
                  "Are you sure you want to restart your computer now?"))
            if reply == "yes":
                yali.util.reboot()
        else:
            reply = QuestionDialog(
                _("General", "Warning"),
                _("General",
                  "Are you sure you want to restart the YALI installer now?"))
            if reply == "yes":
                os.execv("/usr/bin/yali-bin", sys.argv)

    def toggleTheme(self):
        "This easter egg will be implemented later"
        """
        if self._style == os.path.join(ctx.consts.theme_dir, "%s/style.qss" % ctx.flags.theme):
            if os.path.join(ctx.consts.theme_dir, "%s/style.glass.qss" % ctx.flags.theme):
                self._style = os.path.join(ctx.consts.theme_dir, "%s/style.glass.qss" % ctx.flags.theme)
        else:
            self._style = os.path.join(ctx.consts.theme_dir, "%s/style.qss" % ctx.flags.theme)
        self.updateStyle()
        """

    def toggleConsole(self):
        if not self.terminal:
            terminal = QTermWidget()
            terminal.setScrollBarPosition(QTermWidget.ScrollBarRight)
            terminal.setColorScheme(1)
            terminal.sendText("export TERM='xterm'\nclear\n")
            self.terminal = Dialog(_("General", "Terminal"), terminal, True,
                                   QKeySequence(Qt.Key_F11))

            self.terminal.resize(700, 500)
        self.terminal.exec_()

    def toggleTetris(self):
        self.tetris = Dialog(_("General", "Tetris"), None, True,
                             QKeySequence(Qt.Key_F6))
        _tetris = Tetris(self.tetris)
        self.tetris.addWidget(_tetris)
        self.tetris.resize(240, 500)
        _tetris.start()
        self.tetris.exec_()

    def toggleCursor(self):
        if self.cursor().shape() == QCursor(Qt.ArrowCursor).shape():
            raw = QPixmap(":/gui/pics/pardusman-icon.png")
            raw.setMask(raw.mask())
            self.setCursor(QCursor(raw, 2, 2))
        else:
            self.unsetCursor()

    # show/hide help text
    def slotToggleHelp(self):
        self.ui.helpContentFrame.setFixedHeight(self.ui.helpContent.height())
        if self.ui.helpContentFrame.isVisible():
            self.ui.helpContentFrame.hide()
        else:
            self.ui.helpContentFrame.show()
        widget = self.ui.mainStack.currentWidget()
        widget.update()

    # show/hide debug window
    def toggleDebug(self):
        if ctx.debugger.isVisible():
            ctx.debugger.hideWindow()
        else:
            ctx.debugger.showWindow()

    # returns the id of current stack
    def getCurrent(self, index):
        new_index = self.ui.mainStack.currentIndex() + index
        total_index = self.ui.mainStack.count()
        if new_index < 0: new_index = 0
        if new_index > total_index: new_index = total_index
        return new_index

    # move to id numbered step
    def setCurrent(self, index=None):
        if index:
            self.stackMove(index)

    # execute next step
    def slotNext(self, dry_run=False):
        widget = self.ui.mainStack.currentWidget()
        ret = True
        if not dry_run:
            ret = widget.execute()
        if ret:
            self.pds_helper.hideHelp()
            self.ui.toggleHelp.setChecked(False)
            self.stackMove(self.getCurrent(self.step_increment))
            self.step_increment = 1

    # execute previous step
    def slotBack(self):
        widget = self.ui.mainStack.currentWidget()
        if widget.backCheck():
            self.stackMove(self.getCurrent(self.step_increment * -1))
        self.pds_helper.hideHelp()
        self.ui.toggleHelp.setChecked(False)
        self.step_increment = 1

    # move to id numbered stack
    def stackMove(self, index):
        if not index == self.ui.mainStack.currentIndex() or index == 0:
            self.effect.setOpacity(0.0)
            self.animation_type = "fade-in"
            self.anime.start(50)
            self.ui.mainStack.setCurrentIndex(index)
            widget = self.ui.mainStack.currentWidget()
            # Hack to fix goodbye screen help content
            # BUG:#15860, #15444
            if widget.name == "goodbye":
                widget_id = "%s%s" % (widget.name, ctx.flags.install_type)
            else:
                widget_id = widget.name

            widget_icon = self.screens_content[widget_id][0]

            # if self.screens_content[widget_id][1].has_key(ctx.lang):
            if ctx.lang in self.screens_content[widget_id][1]:
                widget_title = self.screens_content[widget_id][1][ctx.lang]
            else:
                widget_title = self.screens_content[widget_id][1]["en"]

            # if self.screens_content[widget_id][2].has_key(ctx.lang):
            if ctx.lang in self.screens_content[widget_id][2]:
                widget_help = self.screens_content[widget_id][2][ctx.lang]
            else:
                widget_help = self.screens_content[widget_id][2]["en"]

            self.ui.screenName.setText(widget_title)
            self.pds_helper.ui.helpContent.setText(widget_help)
            self.pds_helper.setHelp(widget_help)
            self.ui.screenIcon.setPixmap(
                QPixmap(":/gui/pics/%s.png" % (widget_icon)))

            ctx.mainScreen.processEvents()
            widget.update()
            ctx.mainScreen.processEvents()
            widget.shown()

    def animate(self):
        if self.animation_type == "fade-in":
            if self.effect.opacity() < 1.0:
                self.effect.setOpacity(self.effect.opacity() + 0.2)
            else:
                self.anime.stop()
        if self.animation_type == "fade-out":
            if self.effect.opacity() > 0.0:
                self.effect.setOpacity(self.effect.opacity() - 0.2)
            else:
                self.anime.stop()

    def createWidgets(self, screens=[]):
        if not self.screens:
            self.screens = screens
        self.ui.mainStack.removeWidget(self.ui.page)

        for screen in screens:
            # if ctx.flags.debug:
            #     debug all screens.
            #     weave_all_object_methods(ctx.aspect, screen)

            # enable navigation buttons before shown
            weave_object_method(enableNavButtonsAspect, screen, "shown")
            # disable navigation buttons before the execute.
            weave_object_method(disableNavButtonsAspect, screen, "execute")
            try:
                self.ui.mainStack.addWidget(screen())
            except Exception, msg:
                rc = ctx.interface.messageWindow(
                    _("General", "Error"),
                    _(
                        "General", "An error occurred when attempting "
                        "to load screens:%s") % msg,
                    type="custom",
                    customIcon="error",
                    customButtons=[_("General", "Exit")])
                if not rc:
                    sys.exit(0)

        # weave_all_object_methods(ctx.aspect, self)
        self.stackMove(ctx.flags.startup)
Esempio n. 2
0
class NumPadWidget(object):
    def __init__(self, nums, parent):
        super().__init__()
        self.nums = nums
        self.parent = parent

        self.tile_events = events.TileInterfaceEvent()
        # Init main widget for master-controlling NumPad
        self.widget = QWidget()
        self.widget.setObjectName('NumPad')
        # Create blur effect for widgets
        self.blur_top_lbl = QGraphicsBlurEffect()
        self.blur_top_lbl.setBlurRadius(0.0)

        self.blur_bottom_lbl = QGraphicsBlurEffect()
        self.blur_bottom_lbl.setBlurRadius(0.0)

        self.opacity_effect = QGraphicsOpacityEffect()
        self.opacity_effect.setOpacity(0.0)

        # Create animation classes
        self.blur_anim = QVariantAnimation()
        self.blur_anim.setEasingCurve(QEasingCurve.InOutQuad)
        self.blur_anim.valueChanged.connect(self.play_animation)

        self.opacity_anim = QPropertyAnimation(self.opacity_effect, b'opacity')
        self.opacity_anim.setEasingCurve(QEasingCurve.InOutQuad)

        elements_hbl = QVBoxLayout()

        # Top label of NumPad
        self.top_lbl = QLabel()
        self.top_lbl.setGraphicsEffect(self.blur_top_lbl)
        self.top_lbl.setText(str(nums))
        self.top_lbl.setObjectName('NumPad')
        self.top_lbl.setAlignment(Qt.AlignCenter)

        # Bottom label of NumPad
        self.bottom_lbl = QLabel()
        self.bottom_lbl.setGraphicsEffect(self.blur_bottom_lbl)
        self.bottom_lbl.setObjectName('NumPad')
        self.bottom_lbl.setAlignment(Qt.AlignCenter)

        elements_hbl.addWidget(self.top_lbl, 50)
        elements_hbl.addWidget(self.bottom_lbl, 50)

        self.widget.setLayout(elements_hbl)

        self.create_master_numpad()

        # Events and signals widget
        self.widget.enterEvent = lambda event: self.enter_event()
        self.widget.leaveEvent = lambda event: self.leave_event()

    def create_master_numpad(self):
        self.overlay_widget = QWidget(self.widget)
        self.overlay_widget.setObjectName("OverlayNumPadWidget")

        sound_pbtn_widget = QWidget(self.overlay_widget)
        # sound_pbtn_widget.setAttribute(Qt.WA_TranslucentBackground)

        # NumPad widgets, layouts and items
        sound_name_lbl = QLabel(self.overlay_widget)
        sound_name_lbl.setObjectName('NumPadName')
        sound_name_lbl.setAlignment(Qt.AlignCenter)
        sound_name_lbl.setText('Name of Sound')

        numpad_spacer = QSpacerItem(20, 40, QSizePolicy.Minimum,
                                    QSizePolicy.Expanding)

        settings_numpad_pbtn = QPushButton(self.overlay_widget)
        settings_numpad_pbtn.setObjectName('NumPad')
        settings_numpad_pbtn.setText('Settings')

        add_numpad_pbtn = QPushButton(self.overlay_widget)
        add_numpad_pbtn.setObjectName('NumPad')
        add_numpad_pbtn.setText('Add Sound')

        remove_numpad_pbtn = QPushButton(self.overlay_widget)
        remove_numpad_pbtn.setObjectName('NumPad')
        remove_numpad_pbtn.setText('Remove Sound')

        # Master sound buttons
        play_sound_pbtn = QPushButton()
        play_sound_pbtn.setObjectName('PlayBtns')
        play_sound_pbtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        play_sound_pbtn.setIcon(
            QIcon('./interface-stylesheets/interface-element/play.gif'))
        play_sound_pbtn.setAccessibleName('NumPadPushButton_%d' % self.nums)

        begin_sound_pbtn = QPushButton()
        begin_sound_pbtn.setObjectName('PlayBtns')
        begin_sound_pbtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        begin_sound_pbtn.setIcon(
            QIcon('./interface-stylesheets/interface-element/end.gif'))

        end_sound_pbtn = QPushButton()
        end_sound_pbtn.setObjectName('PlayBtns')
        end_sound_pbtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        end_sound_pbtn.setIcon(
            QIcon('./interface-stylesheets/interface-element/begin.gif'))

        overlay_pbtn_hbl = QVBoxLayout()
        overlay_pbtn_hbl.setContentsMargins(0, 0, 0, 0)
        overlay_pbtn_hbl.setSpacing(0)

        # Create and set layouts and add widgets
        play_sound_hbl = QHBoxLayout()
        play_sound_hbl.setContentsMargins(0, 0, 0, 0)

        play_sound_hbl.addWidget(begin_sound_pbtn)
        play_sound_hbl.addWidget(play_sound_pbtn)
        play_sound_hbl.addWidget(end_sound_pbtn)

        overlay_pbtn_hbl.addWidget(sound_name_lbl)
        overlay_pbtn_hbl.addItem(numpad_spacer)
        overlay_pbtn_hbl.addWidget(sound_pbtn_widget)
        overlay_pbtn_hbl.addWidget(add_numpad_pbtn)
        overlay_pbtn_hbl.addWidget(remove_numpad_pbtn)
        overlay_pbtn_hbl.addWidget(settings_numpad_pbtn)

        sound_pbtn_widget.setLayout(play_sound_hbl)
        self.overlay_widget.setLayout(overlay_pbtn_hbl)

        # Custom parameters
        self.overlay_widget.setGraphicsEffect(self.opacity_effect)

        # Events and signals widget
        self.widget.resizeEvent = lambda event: self.resize_event()
        play_sound_pbtn.clicked.connect(
            lambda: self.tile_events.switch_play_pbtn(play_sound_pbtn))
        settings_numpad_pbtn.mousePressEvent = lambda event: self.tile_events.press_settings_buttonEvent(
            self.parent)

    def set_text_lbl_icons(self, bottom_text_lbl):

        if type(bottom_text_lbl) is str:
            self.bottom_lbl.setText(bottom_text_lbl)
        elif bottom_text_lbl is not None:
            pixmap_lbl = QPixmap(
                './interface-stylesheets/interface-element/%d_numpad_night.png'
                % bottom_text_lbl)
            pixmap_lbl.scaled(32, 32, Qt.KeepAspectRatio,
                              Qt.FastTransformation)
            self.bottom_lbl.setPixmap(pixmap_lbl)

    # If mouse pointer on the widget then play animate widget buttons and blur master widget
    def enter_event(self):
        self.blur_anim.setDirection(QPropertyAnimation.Forward)
        self.blur_anim.setDuration(500)
        self.blur_anim.setStartValue(self.blur_top_lbl.blurRadius())
        self.blur_anim.setEndValue(8.0)
        self.blur_anim.start()

        self.opacity_anim.setDirection(QPropertyAnimation.Forward)
        self.opacity_anim.setDuration(500)
        self.opacity_anim.setStartValue(self.opacity_effect.opacity())
        self.opacity_anim.setEndValue(0.6)
        self.opacity_anim.start()

    # If mouse pointer out the widget then play reverse animate widget buttons and unblur master widget
    def leave_event(self):
        self.blur_anim.setDirection(QPropertyAnimation.Backward)
        self.blur_anim.setDuration(500)
        self.blur_anim.setStartValue(0.0)
        self.blur_anim.setEndValue(self.blur_top_lbl.blurRadius())
        self.blur_anim.start()

        self.opacity_anim.setDirection(QPropertyAnimation.Backward)
        self.opacity_anim.setDuration(500)
        self.opacity_anim.setStartValue(0.0)
        self.opacity_anim.setEndValue(self.opacity_effect.opacity())
        self.opacity_anim.start()

    def play_animation(self):
        self.blur_top_lbl.setBlurRadius(self.blur_anim.currentValue())
        self.blur_bottom_lbl.setBlurRadius(self.blur_anim.currentValue())

    def resize_event(self):
        self.overlay_widget.setGeometry(0, 0,
                                        self.widget.geometry().width(),
                                        self.widget.geometry().height())

    def return_top_lbl(self):
        return self.top_lbl

    def return_bottom_lbl(self):
        return self.bottom_lbl

    def return_widget(self):
        return self.widget