Example #1
0
 def uninstall(self):
     index = self.ui.clockWallpaperView.currentIndex()
     if index.isValid():
         package = self.wallpaperModel.package(index.row())
         if KMessageBox.warningYesNo(self.widget, \
                 i18n('Are you sure you want to uninstall "%1"- clock wallpaper', \
                      package.metadata().name())) == KMessageBox.Yes:
             packageRoot = KStandardDirs.locateLocal("data", package.defaultPackageRoot())
             ClockPackage.uninstallPackage(package.metadata().pluginName(), packageRoot)
Example #2
0
 def installPackage(self, localPath):
     #print '### installPackage', localPath
     package = ClockPackage(self)
     packageRoot = KStandardDirs.locateLocal("data", package.defaultPackageRoot())
     if ClockPackage.installPackage(localPath, packageRoot):
         name = os.path.splitext(os.path.basename(U(localPath)))[0]
         packageDir = os.path.join(U(packageRoot), U(name))
         if self.wallpaperModel:
             package.setPath(packageDir)
             self.wallpaperModel.addClockWallpaper(package)
         return packageDir
     else:
         return ''
Example #3
0
    def init(self, config):
        #print '### init'
        self.cache.init()

        self.method = Plasma.Wallpaper.ResizeMethod(config.readEntry('resizemethod', \
                Plasma.Wallpaper.ScaledResize).toInt()[0])
        self.color = QColor(config.readEntry('wallpapercolor', QColor(56, 111, 150)))
        self.ampm = config.readEntry('ampm', False).toBool()
        self.clockPackage = ClockPackage(self,
                self.checkIfEmpty(config.readEntry('clockwallpaper', '').toString()))
        self.cache.initId(self.Current, [WallpaperCache.Manual], self.color, self.method)
        self.cache.initId(self.Next, [WallpaperCache.Stack, [self.BackgroundHour, '']], \
                          self.color, self.method)
        self.cache.initId(self.BackgroundHour, [WallpaperCache.Stack, []], self.color, self.method)
        engine = self.dataEngine('time')
        Moon.timeEngine = engine
        engine.disconnectSource('Local', self)
        engine.connectSource('Local', self, int(self.UpdateInterval * 60 * 1000), \
                             Plasma.AlignToMinute)
        self.immediateRepaint = True
Example #4
0
class Clock(Wallpaper):
    UpdateInterval = 1.0 # minutes
    Current, Next, BackgroundHour = range(3)

    def __init__(self, parent, args = None):
        Wallpaper.__init__(self, parent)
        self.usersWallpapers = None
        self.wallpaperModel = None
        self.newStuffDialog = None
        self.fileDialog = None
        self.widget = None
        self.cache = WallpaperCache(self)
        self.immediateRepaint = False
        self.connect(self.cache, SIGNAL('renderingsCompleted()'), self.renderingsCompleted)

    def init(self, config):
        #print '### init'
        self.cache.init()

        self.method = Plasma.Wallpaper.ResizeMethod(config.readEntry('resizemethod', \
                Plasma.Wallpaper.ScaledResize).toInt()[0])
        self.color = QColor(config.readEntry('wallpapercolor', QColor(56, 111, 150)))
        self.ampm = config.readEntry('ampm', False).toBool()
        self.clockPackage = ClockPackage(self,
                self.checkIfEmpty(config.readEntry('clockwallpaper', '').toString()))
        self.cache.initId(self.Current, [WallpaperCache.Manual], self.color, self.method)
        self.cache.initId(self.Next, [WallpaperCache.Stack, [self.BackgroundHour, '']], \
                          self.color, self.method)
        self.cache.initId(self.BackgroundHour, [WallpaperCache.Stack, []], self.color, self.method)
        engine = self.dataEngine('time')
        Moon.timeEngine = engine
        engine.disconnectSource('Local', self)
        engine.connectSource('Local', self, int(self.UpdateInterval * 60 * 1000), \
                             Plasma.AlignToMinute)
        self.immediateRepaint = True

    def save(self, config):
        #print '### save'
        # For some reason QStrings must be converted to python strings before writing?
        config.writeEntry('resizemethod', self.method)
        config.writeEntry('wallpapercolor', self.color)
        config.writeEntry('ampm', self.ampm)
        config.writeEntry('clockwallpaper', self.clockPackage.path())

    @pyqtSignature('dataUpdated(const QString&, const Plasma::DataEngine::Data&)')
    def dataUpdated(self, sourceName, data):
        #print '### dataUpdated'
        if self.cache.image(self.Next) != None:
            self.cache.setImage(self.Current, self.cache.image(self.Next))
            self.update(self.boundingRect())

        now = QDateTime(data[QString('Date')], data[QString('Time')])
        next = now.addSecs(60)
        self.updateImages(next)

    def updateImages(self, next):
        path = self.clockPackage.path()
        if self.clockPackage.ampmEnabled() and self.ampm:
            h = ((next.time().hour() - 1) % 12) + 1
        elif self.clockPackage.hourImages() == 60:
            h = next.time().hour() % 12
            h = (h * 5) + (next.time().minute() / 12)
        else:
            h = next.time().hour()
        files = [path + 'bg.jpg',
                 path + 'zodiac%s.png' % Zodiac(next.date()),
                 path + 'moonphase%s.png' % Moon(next.date()),
                 path + 'month%d.png' % next.date().month(),
                 path + 'weekday%d.png' % next.date().dayOfWeek(),
                 path + 'day%d.png' % next.date().day(),
                 path + 'hour%d.png' % h]
        if self.clockPackage.ampmEnabled() and self.ampm:
            files.append(path + '%s.png' % next.time().toString('ap'))
        self.cache.setOperationParam(self.BackgroundHour, WallpaperCache.Images, files)

        self.cache.setOperationParam(self.Next, WallpaperCache.Images, \
                [self.BackgroundHour, path + 'minute%d.png' % next.time().minute()])

    def checkIfEmpty(self, wallpaper):
        #print '### checkIfEmpty'
        if wallpaper.isEmpty():
            paths = KGlobal.dirs().findDirs('data', 'plasma/clockwallpapers')
            dir = QDir()
            dir.setFilter(QDir.AllDirs | QDir.Hidden | QDir.Readable)
            for path in paths:
                dir.setPath(path)
                dirs = dir.entryInfoList()
                for cwp in dirs:
                    if (cwp.fileName() == '.') or (cwp.fileName() == '..'):
                        continue
                    wallpaper = cwp.canonicalFilePath()
                    break
        return U(wallpaper)

    def checkGeometry(self):
        #print '### checkGeometry'
        if self.cache.checkGeometry():
            self.immediateRepaint = True
            if self.wallpaperModel:
                self.wallpaperModel.setWallpaperSize(self.cache.size())

    def renderingsCompleted(self):
        #print '### renderingsCompleted', self.immediateRepaint
        # Free images
        if self.immediateRepaint:
            self.immediateRepaint = False
            self.cache.setImage(self.Current, self.cache.image(self.Next))
            self.update(self.boundingRect())

    def paint(self, painter, exposedRect):
        #print '### paint'
        self.checkGeometry()

        # get pixmap
        pixmap = self.cache.pixmap(self.Current)

        # paint
        if pixmap:
            if painter.worldMatrix() == QMatrix():
                # draw the background untransformed when possible; (saves lots of per-pixel-math)
                painter.resetTransform()

            # blit the background (saves all the per-pixel-products that blending does)
            painter.setCompositionMode(QPainter.CompositionMode_Source)

            # for pixmaps we draw only the exposed part (untransformed since the
            # bitmapBackground already has the size of the viewport)
            painter.drawPixmap(exposedRect, pixmap,
                               exposedRect.translated(-self.boundingRect().topLeft()))
        else:
            painter.fillRect(exposedRect, self.color)

    def installPackage(self, localPath):
        #print '### installPackage', localPath
        package = ClockPackage(self)
        packageRoot = KStandardDirs.locateLocal("data", package.defaultPackageRoot())
        if ClockPackage.installPackage(localPath, packageRoot):
            name = os.path.splitext(os.path.basename(U(localPath)))[0]
            packageDir = os.path.join(U(packageRoot), U(name))
            if self.wallpaperModel:
                package.setPath(packageDir)
                self.wallpaperModel.addClockWallpaper(package)
            return packageDir
        else:
            return ''

    # Url dropped
    #----------------------------------------------------------------------------------------------

    def urlDropped(self, url):
        #print '### urlDropped', url
        if url.isLocalFile():
            self.installDropped(url.toLocalFile())
        else:
            self.tmpFile = KTemporaryFile()
            if self.tmpFile.open():
                job = KIO.file_copy(url, KUrl(self.tmpFile.fileName()), -1, \
                                    KIO.JobFlags(KIO.Overwrite))
                self.connect(job, SIGNAL('result(KJob*)'), self.wallpaperRetrieved)

    def wallpaperRetrieved(self, job):
        self.installDropped(job.destUrl().toLocalFile())

    def installDropped(self, path):
        path = self.installPackage(path)
        self.tmpFile = None
        if len(path) > 0:
            self.clockPackage.setPath(path)
            self.immediateRepaint = True
            self.updateImages(QDateTime.currentDateTime())

    # Configuration dialog
    #----------------------------------------------------------------------------------------------

    def createConfigurationInterface(self, parent):
        self.widget = QWidget(parent)
        self.connect(self.widget, SIGNAL('destroyed(QObject*)'), self.configWidgetDestroyed)
        self.ui = uic.loadUi(U(self.package().filePath('ui', 'config.ui')), self.widget)

        self.ui.positioningCombo.setCurrentIndex(self.method)
        self.connect(self.ui.positioningCombo, SIGNAL('currentIndexChanged(int)'), \
                     self.resizeChanged)

        self.wallpaperModel = WallpaperClockModel(self)
        delegate = BackgroundDelegate(self.cache.ratio(), self)

        self.ui.colorButton.setColor(self.color)
        self.connect(self.ui.colorButton, SIGNAL('changed(const QColor&)'), \
                     self.colorChanged)

        self.ui.ampmCheck.setChecked(Qt.Checked if self.ampm else Qt.Unchecked)
        self.connect(self.ui.ampmCheck, SIGNAL('stateChanged(int)'), self.ampmChanged)

        self.ui.clockWallpaperView.setModel(self.wallpaperModel)
        self.ui.clockWallpaperView.setItemDelegate(delegate)
        self.connect(self.ui.clockWallpaperView.selectionModel(),
                     SIGNAL('currentChanged(const QModelIndex&, const QModelIndex&)'),
                     self.wallpaperChanged)
        index = self.wallpaperModel.indexOf(self.clockPackage.path())
        if index.isValid():
            self.ui.clockWallpaperView.setCurrentIndex(index)

        self.ui.openButton.setIcon(KIcon('document-open'));
        self.connect(self.ui.openButton, SIGNAL('clicked()'), self.showFileDialog)

        self.ui.getNewButton.setIcon(KIcon('get-hot-new-stuff'));
        self.connect(self.ui.getNewButton, SIGNAL('clicked()'), self.getNewWallpaper)

        self.ui.uninstallButton.setIcon(KIcon('edit-delete'));
        self.connect(self.ui.uninstallButton, SIGNAL('clicked()'), self.uninstall)
        return self.widget

    def configWidgetDestroyed(self):
        self.widget = None
        self.wallpaperModel = None
        self.ui = None

    def resizeChanged(self, index):
        self.settingsChanged(True)
        self.method = index
        self.immediateRepaint = True
        self.cache.setMethod(WallpaperCache.All, self.method)

    def colorChanged(self, color):
        self.settingsChanged(True)
        self.color = color
        self.immediateRepaint = True
        self.cache.setColor(WallpaperCache.All, self.color)

    def ampmChanged(self, state):
        self.settingsChanged(True)
        self.ampm = (state == Qt.Checked)
        self.immediateRepaint = True
        self.updateImages(QDateTime.currentDateTime())

    def wallpaperChanged(self, index):
        self.settingsChanged(True)
        package = self.wallpaperModel.package(index.row())
        self.ui.ampmCheck.setEnabled(package.ampmEnabled())
        self.clockPackage.setPath(package.path())
        self.immediateRepaint = True
        self.updateImages(QDateTime.currentDateTime())

    # Uninstall

    def uninstall(self):
        index = self.ui.clockWallpaperView.currentIndex()
        if index.isValid():
            package = self.wallpaperModel.package(index.row())
            if KMessageBox.warningYesNo(self.widget, \
                    i18n('Are you sure you want to uninstall "%1"- clock wallpaper', \
                         package.metadata().name())) == KMessageBox.Yes:
                packageRoot = KStandardDirs.locateLocal("data", package.defaultPackageRoot())
                ClockPackage.uninstallPackage(package.metadata().pluginName(), packageRoot)

    # New wallpaper from website

    def getNewWallpaper(self):
        self.clockPackage.createNewWidgetBrowser(self.widget)
        self.connect(self.clockPackage, SIGNAL('newWidgetBrowserFinished()'), \
                     self.newStuffFinished, Qt.UniqueConnection)

    def newStuffFinished(self):
        path = ''
        index = self.ui.clockWallpaperView.currentIndex()
        if index.isValid():
            package = self.wallpaperModel.package(index.row())
            path = package.path()
        self.wallpaperModel.findAllBackgrounds()
        index = self.wallpaperModel.indexOf(path)
        if index.isValid():
            self.ui.clockWallpaperView.setCurrentIndex(index)

    # Open file

    def showFileDialog(self):
        if not self.fileDialog:
            self.fileDialog = KFileDialog(KUrl(), '*.wcz *.zip', self.widget)
            self.fileDialog.setOperationMode(KFileDialog.Opening)
            self.fileDialog.setInlinePreviewShown(True)
            self.fileDialog.setCaption(i18n('Select Clock Wallpaper File'))
            self.fileDialog.setModal(False)
            self.connect(self.fileDialog, SIGNAL('okClicked()'), self.wallpaperBrowseCompleted)
            self.connect(self.fileDialog, SIGNAL('destroyed(QObject*)'), self.fileDialogFinished)

        self.fileDialog.show()
        self.fileDialog.raise_()
        self.fileDialog.activateWindow()

    def fileDialogFinished(self):
        self.fileDialog = None

    def wallpaperBrowseCompleted(self):
        name = self.fileDialog.selectedFile()
        self.installPackage(name)