Beispiel #1
0
class trayApp(QSystemTrayIcon):
    def __init__(self, parent=None):
        super(trayApp, self).__init__(parent)
        self.loadSettings()
        self.setIcon(QIcon("icon.png"))
        self.createSysTrayMenu()
        self.setGlobalHotkeys()
        self.imgurClient = Imgur("", "")
        self.clipboard = QApplication.clipboard()

    def createSysTrayMenu(self):
        self.sysTrayMenu = QMenu()
        self.setContextMenu(self.sysTrayMenu)
        self.createSysTrayActions()

    def createSysTrayActions(self):
        self.sysTrayMenuRegionAction = self.createAction("&Capture region", self.createDrawSurface, self.hotkey)
        self.sysTrayMenuHotkeyAction = self.createAction("&Change hotkey...", self.createKeySequenceDialog)
        self.sysTrayMenuUploadAction = self.createAction("&Upload to imgur", checkable=True)
        self.sysTrayMenuUploadAction.setChecked(self.upload)
        self.sysTrayMenuSaveAction = self.createAction("&Save locally", checkable=True)
        self.sysTrayMenuSaveAction.setChecked(self.save)
        self.sysTrayMenuExitAction = self.createAction("&Exit", self.quit)

        self.addActions(self.sysTrayMenu, (self.sysTrayMenuRegionAction,
                                           None,
                                           self.sysTrayMenuHotkeyAction,
                                           self.sysTrayMenuUploadAction,
                                           self.sysTrayMenuSaveAction,
                                           self.sysTrayMenuExitAction))

    def createAction(self, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False):
        action = QAction(text, self)
        if slot is not None:
            action.triggered.connect(slot)
        if shortcut is not None:
            action.setShortcut(shortcut)
        if icon is not None:
            action.setIcon(QIcon(":/{}.png".format(icon)))
        if tip is not None:
            action.setToolTip(tip)
            action.setStatusTip(tip)
        if checkable:
            action.setCheckable(True)
        return action

    def addActions(self, target, actions):
        for action in actions:
            if action is None:
                target.addSeparator()
            else:
                target.addAction(action)

    def createKeySequenceDialog(self):
        self.keySequenceDialog = KeySequenceDialog(self.hotkey)
        self.keySequenceDialog.accepted.connect(self.changeHotkey)
        self.keySequenceDialog.show()

    def changeHotkey(self):
        text = self.keySequenceDialog.keySequenceLineEdit.text()
        if text:
            self.hotkey = text
            self.sysTrayMenuRegionAction.setShortcut(self.hotkey)
            self.sysTrayMenuRegionGlobalHotkey.setShortcut(QKeySequence(self.hotkey))

    def loadSettings(self):
        settings = QSettings("ninjapic", "settings")
        self.upload = settings.value("upload").toBool()
        self.save = settings.value("save").toBool()
        self.hotkey = settings.value("hotkey").toString()
        if not self.hotkey:
            self.hotkey = "Alt+C"

    def saveSettings(self):
        settings = QSettings("ninjapic", "settings")
        settings.setValue("upload", self.sysTrayMenuUploadAction.isChecked())
        settings.setValue("save", self.sysTrayMenuSaveAction.isChecked())
        settings.setValue("hotkey", self.hotkey)

    def createDrawSurface(self):
        self.surface = Surface()
        self.surface.imageReady.connect(self.storeImage)
        self.surface.show()

    def setGlobalHotkeys(self):
        self.sysTrayMenuRegionGlobalHotkey = QxtGlobalShortcut()
        self.sysTrayMenuRegionGlobalHotkey.setShortcut(QKeySequence(self.hotkey))
        self.sysTrayMenuRegionGlobalHotkey.activated.connect(self.createDrawSurface)

    def storeImage(self):
        if not self.surface.image.save("screenshot.png", "PNG", -1):
            QMessageBox.warning(None, "Image Error", "The image couldn't be saved.")
            return
        if self.sysTrayMenuUploadAction.isChecked():
            try:
                uploaded_image = self.imgurClient.upload_image("screenshot.png")
                self.clipboard.setText(uploaded_image.link)
                webbrowser.open(uploaded_image.link)
            except HTTPError, e:
                self.surface.dispose()
                QMessageBox.warning(None, "Imgur Error", unicode(e))
        if not self.sysTrayMenuSaveAction.isChecked():
            os.remove("screenshot.png")
Beispiel #2
0
class Matrix:
    def __init__(self, width, height, channels, brightness=1):
        self.running = False
        self.width = width
        self.height = height
        self.channels = channels
        self.tbuf = TranslatedFrameBuffer()

        self.surface = Surface(width, height, channels, brightness)

        self.jobs = JobRunner()

        weatherFetcher = WeatherFetcher()
        newsApi = NewsApi()

        self.jobs.add(weatherFetcher.do)
        self.jobs.add(newsApi.do)

        self.apps = [
            App(News(newsApi), 0, 0),
            App(Weather(weatherFetcher), 0, 10),
            App(Clock(), 0, 20),
        ]

        self.rotator = Rotator(self.apps, 2 * second, 120 * second)
        self.timer = Timer(self.loop, 60)

    def start(self):
        print('starting...')
        self.jobs.start()
        self.timer.start()

    def stop(self):
        self.timer.stop()
        self.surface.dispose()

    def update(self, now: int, elapsed: int):
        self.rotator.update(now, elapsed)

        for app in self.apps:
            app.update(now, elapsed)

    def draw(self):
        fbuf = self.surface.begin()

        try:
            fbuf.clear()

            self.tbuf.fbuf = fbuf

            for app in self.apps:
                if -7 < app.x and app.x < 8:
                    self.tbuf.tx = app.x
                    self.tbuf.ty = app.y
                    app.draw(self.tbuf)
        except:
            print("Unexpected error:", sys.exc_info()[0])
        finally:
            self.surface.commit()

    def loop(self, now, elapsed):
        self.update(now, elapsed)
        self.draw()
        # print('loop')
        time.sleep(0.001)