def main():
    global view
    global settings
    global windoInfo
    global menu_controller

    cursor_pos = QCursor.pos()
    desktop = qApp.desktop()
    screen_num = desktop.screenNumber(cursor_pos)
    screen_geo = desktop.screenGeometry(screen_num)
    pixmap = qApp.primaryScreen().grabWindow(0)
    pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(), screen_geo.width(), screen_geo.height())
    pixmap.save(TMP_IMAGE_FILE)

    settings.showOSD = startFromDesktopValue
    settings.tmpImageFile = TMP_IMAGE_FILE
    menu_controller = MenuController()
    windoInfo = WindowInfo(screen_num)

    notificationsInterface.ActionInvoked.connect(_actionInvoked)
    notificationsInterface.NotificationClosed.connect(_notificationClosed)

    if fullscreenValue:
        saveScreenshot(pixmap)
    elif topWindowValue:
        wInfos = windoInfo.get_windows_info()
        if len(wInfos) > 0:
            wInfo = wInfos[0]
            pix = pixmap.copy(wInfo[0] - screen_geo.x(), wInfo[1] - screen_geo.y(), wInfo[2], wInfo[3])
            saveScreenshot(pix)
    else:
        view = Window(settings, windoInfo)
        view.setX(screen_geo.x())
        view.setY(screen_geo.y())
        view.setWidth(screen_geo.width())
        view.setHeight(screen_geo.height())
        view.visibleChanged.connect(_windowVisibleChanged)

        qml_context = view.rootContext()
        qml_context.setContextProperty("windowView", view)
        qml_context.setContextProperty("qApp", qApp)
        qml_context.setContextProperty("screenWidth", view.window_info.screen_width)
        qml_context.setContextProperty("screenHeight", view.window_info.screen_height)
        qml_context.setContextProperty("_menu_controller", menu_controller)

        view.setSource(QUrl.fromLocalFile(MAIN_QML))
        view.disable_zone()
        view.showWindow()

        menu_controller.preMenuShow.connect(view.ungrabFocus)
        menu_controller.postMenuHide.connect(view.grabFocus)
Example #2
0
class AppContext(QObject):
    """Every AppContext instance keeps an environment which is different
       from other instances, acting like a container.
    """

    needSound = pyqtSignal()
    needOSD = pyqtSignal(QRect)
    finished = pyqtSignal()

    def __init__(self, argValues):
        super(AppContext, self).__init__()
        self.argValues = argValues
        self.settings = None
        self.windowInfo = None
        self.window = None
        self.pixmap = None

        self._notificationId = None
        self._fileSaveLocation = None
        self.callHelpManual = False

        self._waitNotificationTimer = QTimer()
        self._waitNotificationTimer.setInterval(10 * 1000)
        self._waitNotificationTimer.setSingleShot(True)
        self._waitNotificationTimer.timeout.connect(self.finished)

    def _notify(self, *args, **kwargs):
        noNotificationValue = self.argValues["noNotification"]
        if noNotificationValue:
            self.finished.emit()
        else:
            self._waitNotificationTimer.start()
            time.sleep(1)
            return notificationsInterface.notify(_("Deepin Screenshot"),
                                                 *args, **kwargs)

    def _actionInvoked(self, notificationId, actionId):
        self._waitNotificationTimer.stop()
        if self._notificationId == notificationId:

            if actionId == ACTION_ID_OPEN:
                fileManager = FileManagerInterface()
                if fileManager.isValid():
                    fileManager.showItems([self._fileSaveLocation])
                # TODO(hualet): remove this workaround after dde-file-manager's
                # done supporting org.freedesktop.FileManager1 interface.
                elif os.path.exists("/usr/bin/dde-file-manager"):
                    encode = lambda x : QUrl.fromLocalFile(x).toString()
                    argDict = {
                        "directory" : encode(dirname(self._fileSaveLocation)),
                        "item" : encode(self._fileSaveLocation)
                    }
                    arg = "{directory}?selectUrl={item}".format(**argDict)
                    subprocess.Popen(["dde-file-manager", arg])
            elif actionId == ACTION_ID_MANUAL:
                subprocess.Popen(["dman", "deepin-screenshot"])
            self.window.windowClosing.emit()
            self.window.close()
            self.finished.emit()

    def _notificationClosed(self, notificationId, reason):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            self.finished.emit()

    def _windowVisibleChanged(self, visible):
        if visible:
            self.sender().disable_zone()
            self.sender().grabFocus()

            controlCenterInterface.hideImmediately()
        else:
            self.sender().enable_zone()
            self.sender().ungrabFocus()

            if self.settings.showOSD:
                area = QRect(QPoint(self.window.x(), self.window.y()),
                             QSize(self.window.width(), self.window.height()))
                self.needOSD.emit(area)

    # this function just handles the situation that this context's
    # finished by the user interaction.
    def _windowClosing(self):
        if self.settings.showOSD:
            area = QRect(QPoint(self.window.x(), self.window.y()),
                         QSize(self.window.width(), self.window.height()))
            self.needOSD.emit(area)
        self.finished.emit()

    def isActive(self):
        return self.window.isVisible()

    def copyPixmap(self, pixmap):
        _temp = "%s.png" % tempfile.mktemp()
        pixmap.save(_temp)
        subprocess.call([GTK_CLIP, _temp])

        if not self.callHelpManual:
            self._notificationId = self._notify(
            _("Picture has been saved to clipboard"))

    def savePixmap(self, pixmap, fileName):
        pixmap.save(fileName)

        self._fileSaveLocation = fileName

    def saveScreenshot(self, pixmap):
        self.needSound.emit()

        savePathValue = self.argValues["savePath"]
        timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        fileName = "%s%s.png" % (_("DeepinScreenshot"), timeStamp)
        save_op = self.settings.getOption("save", "save_op")
        save_op_index = int(save_op)

        absSavePath = ""
        copyToClipborad = False
        if savePathValue != "":
            pic_dir = os.path.dirname(savePathValue)
            pic_name = os.path.basename(savePathValue)
            if pic_name == "":
                pic_name = fileName;
            else:
                pic_name_stuffix = os.path.splitext(pic_name)[1]
                if not validFormat(pic_name_stuffix):
                    pic_name = pic_name + ".png"
                    savePathValue = pic_dir + "/" + pic_name
            savePathValue = os.path.abspath(savePathValue)
        else:
            if save_op_index == 0: #saveId == "save_to_desktop":
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.DesktopLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 1: #saveId == "auto_save" :
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 2: #saveId == "save_to_dir":
                lastSavePath = self.settings.getOption("save", "folder")
                absSavePath = QFileDialog.getSaveFileName(None, _("Save"),
                    os.path.join(lastSavePath, fileName))[0]
                if absSavePath != "":
                    pic_dir = os.path.dirname(absSavePath)
                    pic_name = os.path.basename(absSavePath)
                    if pic_name == "":
                        pic_name = fileName;
                    else:
                        pic_name_stuffix = os.path.splitext(pic_name)[1]
                        if not validFormat(pic_name_stuffix):
                            pic_name = pic_name + ".png"
                            absSavePath = pic_dir + "/" + pic_name
                self.settings.setOption("save", "folder",
                    os.path.dirname(absSavePath) or lastSavePath)
            elif save_op_index == 4: #saveId == "auto_save_ClipBoard":
                copyToClipborad = True
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            else: copyToClipborad = True
        if savePathValue:
            self.savePixmap(pixmap, savePathValue)
            self._notificationId = self._notify(
                    _("Picture has been saved to %s") % savePathValue,
                    [ACTION_ID_OPEN, _("View")])
            if self.callHelpManual:
                self._notificationId = self._notify(
                        _(" View Manual, the picture is automatically saved."),
                        [ACTION_ID_MANUAL, _("View")])
            else:
                self.finished.emit()
        elif absSavePath or copyToClipborad:
            if copyToClipborad:
                self.copyPixmap(pixmap)
            if absSavePath:
                copyToClipborad = False
                self.savePixmap(pixmap, absSavePath)
            if self.callHelpManual:
                self._notificationId = self._notify(
                        _(" View Manual, the picture is automatically saved."),
                        [ACTION_ID_MANUAL, _("View")])

            if absSavePath:
                self._notificationId = self._notify(
                        _("Picture has been saved to %s") % absSavePath,
                        [ACTION_ID_OPEN, _("View")])
        else:
            self.finished.emit()

    def helpManual(self):
        self.callHelpManual = True
        self.window.ungrabFocus()
        self.window.hide()
        self.saveScreenshot(self.pixmap)

    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]
        savePathValue = self.argValues["savePath"]
        noNotificationValue = self.argValues["noNotification"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        if not noNotificationValue:
            notificationsInterface.ActionInvoked.connect(
                self._actionInvoked)
            notificationsInterface.NotificationClosed.connect(
                self._notificationClosed)

        self.pixmap = pixmap
        self.window = Window(ref(self)())

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfo = self.windowInfo.get_active_window_info()
            pix = pixmap.copy(wInfo[0], wInfo[1], wInfo[2], wInfo[3])
            self.saveScreenshot(pix)
        else:
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty("screenWidth",
                self.window.window_info.screen_width)
            qml_context.setContextProperty("screenHeight",
                self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()
            rootObject = self.window.rootObject()
            rootObject.helpView.connect(self.helpManual)
            rootObject.setProperty("saveSpecifiedPath", savePathValue)

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)
Example #3
0
    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]
        savePathValue = self.argValues["savePath"]
        noNotificationValue = self.argValues["noNotification"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        if not noNotificationValue:
            notificationsInterface.ActionInvoked.connect(
                self._actionInvoked)
            notificationsInterface.NotificationClosed.connect(
                self._notificationClosed)

        self.pixmap = pixmap
        self.window = Window(ref(self)())

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfo = self.windowInfo.get_active_window_info()
            pix = pixmap.copy(wInfo[0], wInfo[1], wInfo[2], wInfo[3])
            self.saveScreenshot(pix)
        else:
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty("screenWidth",
                self.window.window_info.screen_width)
            qml_context.setContextProperty("screenHeight",
                self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()
            rootObject = self.window.rootObject()
            rootObject.helpView.connect(self.helpManual)
            rootObject.setProperty("saveSpecifiedPath", savePathValue)

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)
Example #4
0
class AppContext(QObject):
    """Every AppContext instance keeps an environment which is different
       from other instances, acting like a container.
    """

    needSound = pyqtSignal()
    needOSD = pyqtSignal(QRect)
    finished = pyqtSignal()

    def __init__(self, argValues):
        super(AppContext, self).__init__()
        self.argValues = argValues
        self.settings = None
        self.windowInfo = None
        self.window = None

        self._notificationId = None
        self._fileSaveLocation = None

        self._waitNotificationTimer = QTimer()
        self._waitNotificationTimer.setInterval(10 * 1000)
        self._waitNotificationTimer.setSingleShot(True)
        self._waitNotificationTimer.timeout.connect(self.finished)

    def _notify(self, *args, **kwargs):
        self._waitNotificationTimer.start()
        notify = partial(notificationsInterface.notify, _("Deepin Screenshot"))
        return notify(*args, **kwargs)

    def _actionInvoked(self, notificationId, actionId):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            if actionId == ACTION_ID_OPEN:
                subprocess.call(
                    ["xdg-open",
                     os.path.dirname(self._fileSaveLocation)])
            self.finished.emit()

    def _notificationClosed(self, notificationId, reason):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            self.finished.emit()

    def _windowVisibleChanged(self, visible):
        if visible:
            self.sender().disable_zone()
            self.sender().grabFocus()

            controlCenterInterface.hideImmediately()
        else:
            self.sender().enable_zone()
            self.sender().ungrabFocus()

            if self.settings.showOSD:
                area = QRect(QPoint(self.window.x(), self.window.y()),
                             QSize(self.window.width(), self.window.height()))
                self.needOSD.emit(area)

    # this function just handles the situation that this context's
    # finished by the user interaction.
    def _windowClosing(self):
        if self.settings.showOSD:
            area = QRect(QPoint(self.window.x(), self.window.y()),
                         QSize(self.window.width(), self.window.height()))
            self.needOSD.emit(area)
        self.finished.emit()

    def isActive(self):
        return self.window.isVisible()

    def copyPixmap(self, pixmap):
        _temp = "%s.png" % tempfile.mktemp()
        pixmap.save(_temp)
        subprocess.call([GTK_CLIP, _temp])

        self._notificationId = self._notify(
            _("Picture has been saved to clipboard"))

    def savePixmap(self, pixmap, fileName):
        pixmap.save(fileName)

        self._fileSaveLocation = fileName
        self._notificationId = self._notify(
            _("Picture has been saved to %s") % fileName,
            [ACTION_ID_OPEN, _("View")])

    def saveScreenshot(self, pixmap):
        self.needSound.emit()

        savePathValue = self.argValues["savePath"]
        timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        fileName = "%s%s.png" % (_("DeepinScreenshot"), timeStamp)

        save_op = self.settings.getOption("save", "save_op")
        save_op_index = int(save_op)

        absSavePath = ""
        copyToClipborad = False
        if savePathValue and os.path.exists(os.path.dirname(absSavePath)):
            absSavePath = os.path.abspath(savePathValue)
        else:
            if save_op_index == 0:  #saveId == "save_to_desktop":
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.DesktopLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 1:  #saveId == "auto_save" :
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 2:  #saveId == "save_to_dir":
                lastSavePath = self.settings.getOption("save", "folder")
                absSavePath = QFileDialog.getSaveFileName(
                    None, _("Save"), os.path.join(lastSavePath, fileName))[0]
                self.settings.setOption(
                    "save", "folder",
                    os.path.dirname(absSavePath) or lastSavePath)
            elif save_op_index == 4:  #saveId == "auto_save_ClipBoard":
                copyToClipborad = True
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            else:
                copyToClipborad = True

        if absSavePath or copyToClipborad:
            if copyToClipborad: self.copyPixmap(pixmap)
            if absSavePath: self.savePixmap(pixmap, absSavePath)
        else:
            self.finished.emit()

    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        notificationsInterface.ActionInvoked.connect(self._actionInvoked)
        notificationsInterface.NotificationClosed.connect(
            self._notificationClosed)

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfos = self.windowInfo.get_windows_info()
            if len(wInfos) > 0:
                wInfo = wInfos[0]
                pix = pixmap.copy(wInfo[0] - screen_geo.x(),
                                  wInfo[1] - screen_geo.y(), wInfo[2],
                                  wInfo[3])
                self.saveScreenshot(pix)
        else:
            self.window = Window(ref(self)())
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty(
                "screenWidth", self.window.window_info.screen_width)
            qml_context.setContextProperty(
                "screenHeight", self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                                           self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                                           self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                                           self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                                           self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)
Example #5
0
    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        notificationsInterface.ActionInvoked.connect(self._actionInvoked)
        notificationsInterface.NotificationClosed.connect(
            self._notificationClosed)

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfos = self.windowInfo.get_windows_info()
            if len(wInfos) > 0:
                wInfo = wInfos[0]
                pix = pixmap.copy(wInfo[0] - screen_geo.x(),
                                  wInfo[1] - screen_geo.y(), wInfo[2],
                                  wInfo[3])
                self.saveScreenshot(pix)
        else:
            self.window = Window(ref(self)())
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty(
                "screenWidth", self.window.window_info.screen_width)
            qml_context.setContextProperty(
                "screenHeight", self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                                           self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                                           self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                                           self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                                           self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)
class AppContext(QObject):
    """Every AppContext instance keeps an environment which is different
       from other instances, acting like a container.
    """

    needSound = pyqtSignal()
    needOSD = pyqtSignal(QRect)
    finished = pyqtSignal()

    def __init__(self, argValues):
        super(AppContext, self).__init__()
        self.argValues = argValues
        self.settings = None
        self.windowInfo = None
        self.window = None

        self._notificationId = None
        self._fileSaveLocation = None

        self._waitNotificationTimer = QTimer()
        self._waitNotificationTimer.setInterval(10 * 1000)
        self._waitNotificationTimer.setSingleShot(True)
        self._waitNotificationTimer.timeout.connect(self.finished)

    def _notify(self, *args, **kwargs):
        self._waitNotificationTimer.start()
        notify = partial(notificationsInterface.notify, _("Deepin Screenshot"))
        return notify(*args, **kwargs)

    def _actionInvoked(self, notificationId, actionId):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            if actionId == ACTION_ID_OPEN:
                subprocess.call(["xdg-open",
                    os.path.dirname(self._fileSaveLocation)])
            self.finished.emit()

    def _notificationClosed(self, notificationId, reason):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            self.finished.emit()

    def _windowVisibleChanged(self, visible):
        if visible:
            self.sender().disable_zone()
            self.sender().grabFocus()

            controlCenterInterface.hideImmediately()
        else:
            self.sender().enable_zone()
            self.sender().ungrabFocus()

            if self.settings.showOSD:
                area = QRect(QPoint(self.window.x(), self.window.y()),
                             QSize(self.window.width(), self.window.height()))
                self.needOSD.emit(area)

    # this function just handles the situation that this context's
    # finished by the user interaction.
    def _windowClosing(self):
        if self.settings.showOSD:
            area = QRect(QPoint(self.window.x(), self.window.y()),
                         QSize(self.window.width(), self.window.height()))
            self.needOSD.emit(area)
        self.finished.emit()

    def isActive(self):
        return self.window.isVisible()

    def copyPixmap(self, pixmap):
        _temp = "%s.png" % tempfile.mktemp()
        pixmap.save(_temp)
        subprocess.call([GTK_CLIP, _temp])

        self._notificationId = self._notify(
            _("Picture has been saved to clipboard"))

    def savePixmap(self, pixmap, fileName):
        pixmap.save(fileName)

        self._fileSaveLocation = fileName
        self._notificationId = self._notify(
            _("Picture has been saved to %s") % fileName,
            [ACTION_ID_OPEN, _("View")])

    def saveScreenshot(self, pixmap):
        self.needSound.emit()

        savePathValue = self.argValues["savePath"]
        timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        fileName = "%s%s.png" % (_("DeepinScreenshot"), timeStamp)

        save_op = self.settings.getOption("save", "save_op")
        save_op_index = int(save_op)

        absSavePath = ""
        copyToClipborad = False
        if savePathValue and os.path.exists(os.path.dirname(absSavePath)):
            absSavePath = os.path.abspath(savePathValue)
        else:
            if save_op_index == 0: #saveId == "save_to_desktop":
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.DesktopLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 1: #saveId == "auto_save" :
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 2: #saveId == "save_to_dir":
                lastSavePath = self.settings.getOption("save", "folder")
                absSavePath = QFileDialog.getSaveFileName(None, _("Save"),
                    os.path.join(lastSavePath, fileName))[0]
                self.settings.setOption("save", "folder",
                    os.path.dirname(absSavePath) or lastSavePath)
            elif save_op_index == 4: #saveId == "auto_save_ClipBoard":
                copyToClipborad = True
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            else: copyToClipborad = True

        if absSavePath or copyToClipborad:
            if copyToClipborad: self.copyPixmap(pixmap)
            if absSavePath: self.savePixmap(pixmap, absSavePath)
        else:
            self.finished.emit()

    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        notificationsInterface.ActionInvoked.connect(
            self._actionInvoked)
        notificationsInterface.NotificationClosed.connect(
            self._notificationClosed)

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfos = self.windowInfo.get_windows_info()
            if len(wInfos) > 0:
                wInfo = wInfos[0]
                pix = pixmap.copy(wInfo[0] - screen_geo.x(),
                                  wInfo[1] - screen_geo.y(),
                                  wInfo[2], wInfo[3])
                self.saveScreenshot(pix)
        else:
            self.window = Window(ref(self)())
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty("screenWidth",
                self.window.window_info.screen_width)
            qml_context.setContextProperty("screenHeight",
                self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)
Example #7
0
class AppContext(QObject):
    """Every AppContext instance keeps an environment which is different
       from other instances, acting like a container.
    """

    needSound = pyqtSignal()
    needOSD = pyqtSignal(QRect)
    finished = pyqtSignal()

    def __init__(self, argValues):
        super(AppContext, self).__init__()
        self.argValues = argValues
        self.settings = None
        self.windowInfo = None
        self.window = None
        self.pixmap = None

        self._notificationId = None
        self._fileSaveLocation = None
        self.callHelpManual = False

        self._waitNotificationTimer = QTimer()
        self._waitNotificationTimer.setInterval(10 * 1000)
        self._waitNotificationTimer.setSingleShot(True)
        self._waitNotificationTimer.timeout.connect(self.finished)

    def _notify(self, *args, **kwargs):
        noNotificationValue = self.argValues["noNotification"]
        if noNotificationValue:
            self.finished.emit()
        else:
            self._waitNotificationTimer.start()
            time.sleep(1)
            return notificationsInterface.notify(_("Deepin Screenshot"), *args,
                                                 **kwargs)

    def _actionInvoked(self, notificationId, actionId):
        self._waitNotificationTimer.stop()
        if self._notificationId == notificationId:

            if actionId == ACTION_ID_OPEN:
                fileManager = FileManagerInterface()
                if fileManager.isValid():
                    fileManager.showItems([self._fileSaveLocation])
                # TODO(hualet): remove this workaround after dde-file-manager's
                # done supporting org.freedesktop.FileManager1 interface.
                elif os.path.exists("/usr/bin/dde-file-manager"):
                    encode = lambda x: QUrl.fromLocalFile(x).toString()
                    argDict = {
                        "directory": encode(dirname(self._fileSaveLocation)),
                        "item": encode(self._fileSaveLocation)
                    }
                    arg = "{directory}?selectUrl={item}".format(**argDict)
                    subprocess.Popen(["dde-file-manager", arg])
            elif actionId == ACTION_ID_MANUAL:
                subprocess.Popen(["dman", "deepin-screenshot"])
            self.window.windowClosing.emit()
            self.window.close()
            self.finished.emit()

    def _notificationClosed(self, notificationId, reason):
        self._waitNotificationTimer.stop()

        if self._notificationId == notificationId:
            self.finished.emit()

    def _windowVisibleChanged(self, visible):
        if visible:
            self.sender().disable_zone()
            self.sender().grabFocus()

            controlCenterInterface.hideImmediately()
        else:
            self.sender().enable_zone()
            self.sender().ungrabFocus()

            if self.settings.showOSD:
                area = QRect(QPoint(self.window.x(), self.window.y()),
                             QSize(self.window.width(), self.window.height()))
                self.needOSD.emit(area)

    # this function just handles the situation that this context's
    # finished by the user interaction.
    def _windowClosing(self):
        if self.settings.showOSD:
            area = QRect(QPoint(self.window.x(), self.window.y()),
                         QSize(self.window.width(), self.window.height()))
            self.needOSD.emit(area)
        self.finished.emit()

    def isActive(self):
        return self.window.isVisible()

    def copyPixmap(self, pixmap):
        _temp = "%s.png" % tempfile.mktemp()
        pixmap.save(_temp)
        subprocess.call([GTK_CLIP, _temp])

        if not self.callHelpManual:
            self._notificationId = self._notify(
                _("Picture has been saved to clipboard"))

    def savePixmap(self, pixmap, fileName):
        pixmap.save(fileName)

        self._fileSaveLocation = fileName

    def saveScreenshot(self, pixmap):
        self.needSound.emit()

        savePathValue = self.argValues["savePath"]
        timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        fileName = "%s%s.png" % (_("DeepinScreenshot"), timeStamp)
        save_op = self.settings.getOption("save", "save_op")
        save_op_index = int(save_op)

        absSavePath = ""
        copyToClipborad = False
        if savePathValue != "":
            pic_dir = os.path.dirname(savePathValue)
            pic_name = os.path.basename(savePathValue)
            if pic_name == "":
                pic_name = fileName
            else:
                pic_name_stuffix = os.path.splitext(pic_name)[1]
                if not validFormat(pic_name_stuffix):
                    pic_name = pic_name + ".png"
                    savePathValue = pic_dir + "/" + pic_name
            savePathValue = os.path.abspath(savePathValue)
        else:
            if save_op_index == 0:  #saveId == "save_to_desktop":
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.DesktopLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 1:  #saveId == "auto_save" :
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            elif save_op_index == 2:  #saveId == "save_to_dir":
                lastSavePath = self.settings.getOption("save", "folder")
                absSavePath = QFileDialog.getSaveFileName(
                    None, _("Save"), os.path.join(lastSavePath, fileName))[0]
                if absSavePath != "":
                    pic_dir = os.path.dirname(absSavePath)
                    pic_name = os.path.basename(absSavePath)
                    if pic_name == "":
                        pic_name = fileName
                    else:
                        pic_name_stuffix = os.path.splitext(pic_name)[1]
                        if not validFormat(pic_name_stuffix):
                            pic_name = pic_name + ".png"
                            absSavePath = pic_dir + "/" + pic_name
                self.settings.setOption(
                    "save", "folder",
                    os.path.dirname(absSavePath) or lastSavePath)
            elif save_op_index == 4:  #saveId == "auto_save_ClipBoard":
                copyToClipborad = True
                saveDir = QStandardPaths.writableLocation(
                    QStandardPaths.PicturesLocation)
                absSavePath = os.path.join(saveDir, fileName)
            else:
                copyToClipborad = True
        if savePathValue:
            self.savePixmap(pixmap, savePathValue)
            self._notificationId = self._notify(
                _("Picture has been saved to %s") % savePathValue,
                [ACTION_ID_OPEN, _("View")])
            if self.callHelpManual:
                self._notificationId = self._notify(
                    _(" View Manual, the picture is automatically saved."),
                    [ACTION_ID_MANUAL, _("View")])
            else:
                self.finished.emit()
        elif absSavePath or copyToClipborad:
            if copyToClipborad:
                self.copyPixmap(pixmap)
            if absSavePath:
                copyToClipborad = False
                self.savePixmap(pixmap, absSavePath)
            if self.callHelpManual:
                self._notificationId = self._notify(
                    _(" View Manual, the picture is automatically saved."),
                    [ACTION_ID_MANUAL, _("View")])

            if absSavePath:
                self._notificationId = self._notify(
                    _("Picture has been saved to %s") % absSavePath,
                    [ACTION_ID_OPEN, _("View")])
        else:
            self.finished.emit()

    def helpManual(self):
        self.callHelpManual = True
        self.window.ungrabFocus()
        self.window.hide()
        self.saveScreenshot(self.pixmap)

    def main(self):
        fullscreenValue = self.argValues["fullscreen"]
        topWindowValue = self.argValues["topWindow"]
        startFromDesktopValue = self.argValues["startFromDesktop"]
        savePathValue = self.argValues["savePath"]
        noNotificationValue = self.argValues["noNotification"]

        cursor_pos = QCursor.pos()
        desktop = qApp.desktop()
        screen_num = desktop.screenNumber(cursor_pos)
        screen_geo = desktop.screenGeometry(screen_num)
        pixmap = qApp.primaryScreen().grabWindow(0)
        pixmap = pixmap.copy(screen_geo.x(), screen_geo.y(),
                             screen_geo.width(), screen_geo.height())
        pixmap.save(self.settings.tmpImageFile)

        show_osd = self.settings.getOption("showOSD", "show")
        if show_osd == True or show_osd == "true":
            self.settings.showOSD = startFromDesktopValue
            if self.settings.showOSD:
                self.settings.setOption("showOSD", "show", QVariant(False))
        else:
            self.settings.showOSD = False
        self.menu_controller = MenuController()
        self.windowInfo = WindowInfo(screen_num)

        if not noNotificationValue:
            notificationsInterface.ActionInvoked.connect(self._actionInvoked)
            notificationsInterface.NotificationClosed.connect(
                self._notificationClosed)

        self.pixmap = pixmap
        self.window = Window(ref(self)())

        if fullscreenValue:
            self.saveScreenshot(pixmap)
        elif topWindowValue:
            wInfo = self.windowInfo.get_active_window_info()
            pix = pixmap.copy(wInfo[0], wInfo[1], wInfo[2], wInfo[3])
            self.saveScreenshot(pix)
        else:
            self.window.setX(screen_geo.x())
            self.window.setY(screen_geo.y())
            self.window.setWidth(screen_geo.width())
            self.window.setHeight(screen_geo.height())
            self.window.windowClosing.connect(self._windowClosing)
            self.window.visibleChanged.connect(self._windowVisibleChanged)

            # NOTE: make sure that all the objects that are set as context
            # property are always referenced by others through the lifetime
            # of this application, otherwise it'll cause problems.
            qml_context = self.window.rootContext()
            qml_context.setContextProperty("windowView", self.window)
            qml_context.setContextProperty("qApp", qApp)
            qml_context.setContextProperty(
                "screenWidth", self.window.window_info.screen_width)
            qml_context.setContextProperty(
                "screenHeight", self.window.window_info.screen_height)
            qml_context.setContextProperty("tmpImageFile",
                                           self.settings.tmpImageFile)
            qml_context.setContextProperty("blurImageFile",
                                           self.settings.tmpBlurFile)
            qml_context.setContextProperty("mosaicImageFile",
                                           self.settings.tmpMosaiceFile)
            qml_context.setContextProperty("_menu_controller",
                                           self.menu_controller)

            self.window.setSource(QUrl.fromLocalFile(MAIN_QML))
            self.window.showWindow()
            rootObject = self.window.rootObject()
            rootObject.helpView.connect(self.helpManual)
            rootObject.setProperty("saveSpecifiedPath", savePathValue)

            self.menu_controller.preMenuShow.connect(self.window.ungrabFocus)
            self.menu_controller.postMenuHide.connect(self.window.grabFocus)