nitroplasm/nitroplasm.py

179 lines
5 KiB
Python
Raw Normal View History

2021-03-20 05:15:04 +00:00
#!/usr/bin/python3
# nitroplasm: a lightweight desktop setter for KDE Plasma
# by Derek Stevens <drkste@zoho.com>
# written for use with the custom root menu by MatMoul:
# https://github.com/MatMoul/plasma-containmentactions-customdesktopmenu
# original script by pashazz:
# https://github.com/pashazz/ksetwallpaper
# license: GPLv3
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import \
QApplication, \
QWidget, \
QComboBox, \
QVBoxLayout, \
QHBoxLayout, \
QPushButton, \
QFileDialog , \
QLabel, \
QSizePolicy
import sys
from PyQt5.QtGui import QPixmap
import subprocess
import dbus
import argparse
class Window(QWidget):
def __init__(self):
super().__init__()
self.title = "Nitroplasm"
self.top = 100
self.left = 200
self.width = 500
self.height = 400
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon(None))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setFixedSize(self.width, self.height)
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.setWindowFlags(QtCore.Qt.Dialog)
vbox = QVBoxLayout()
hbox = QHBoxLayout()
self.image = None
self.label = QLabel(self.image)
self.label.setScaledContents(False)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.layout = 2
self.monitor = -1
self.select_button = QPushButton("Select Image")
self.select_button.clicked.connect(self.get_image)
layouts = {
"Stretch": 0,
"Scale": 1,
"Zoom": 2,
"Tile": 3,
"Vertical Tile": 4,
"Horizontal Tile": 5,
"Center": 6
}
self.layout_selector = QComboBox()
for l in layouts:
self.layout_selector.addItem(l, layouts[l])
self.layout_selector.currentIndexChanged.connect(self.set_layout)
self.layout_selector.setCurrentIndex(2)
self.monitor_selector = QComboBox()
self.monitor_selector.addItem("All Monitors", -1)
nDisplays = self.get_displays()
print(nDisplays)
if nDisplays > 1:
for i in range(0, nDisplays):
self.monitor_selector.addItem("Monitor" + str(i+1), i)
self.monitor_selector.currentIndexChanged.connect(self.set_monitor)
self.monitor_selector.setCurrentIndex(0)
self.apply_button = QPushButton("Apply Image")
self.apply_button.setEnabled(False)
self.apply_button.clicked.connect(self.apply_image)
vbox.addWidget(self.label)
hbox.addWidget(self.select_button)
hbox.addWidget(self.layout_selector)
hbox.addWidget(self.monitor_selector)
hbox.addWidget(self.apply_button)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
def set_layout(self, index):
self.layout = self.layout_selector.itemData(index)
def get_displays(self):
try:
output = [l for l in subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()]
return len([l.split()[0] for l in output if " connected " in l])
except:
2021-03-20 05:15:04 +00:00
return 1
2021-03-20 05:15:04 +00:00
def set_monitor(self, index):
self.monitor = self.monitor_selector.itemData(index)
def get_image(self):
fname = QFileDialog.getOpenFileName(
self,
'Select Image File',
'./',
'Image files (*.png *.jpg *.gif *.jpeg)')
if fname[0]:
self.image = fname[0]
self.apply_button.setEnabled(True)
pixmap = QPixmap(self.image).scaled(
self.label.width(),
self.label.height(),
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation)
#self.label.setMaximumSize(self.label.width(), self.label.height())
self.label.setPixmap(pixmap)
def set_wallpaper(self, filepath, plugin = 'org.kde.image', layout = 2, desk = -1):
if desk < 0:
jscript = """
var allDesktops = desktops();
for (i=0;i<allDesktops.length;i++) {
d = allDesktops[i];
d.wallpaperPlugin = "%s";
d.currentConfigGroup = Array("Wallpaper", "%s", "General");
d.writeConfig("Image", "file://%s")
d.writeConfig("FillMode", "%s")
}
"""
else:
jscript = """
var d = desktops()[%s];
d.wallpaperPlugin = "%s";
d.currentConfigGroup = Array("Wallpaper", "%s", "General");
d.writeConfig("Image", "file://%s")
d.writeConfig("FillMode", "%s")
"""
bus = dbus.SessionBus()
plasma = dbus.Interface(
bus.get_object(
'org.kde.plasmashell',
'/PlasmaShell'),
dbus_interface='org.kde.PlasmaShell')
if desk < 0:
plasma.evaluateScript(jscript % (plugin, plugin, filepath, layout))
else:
plasma.evaluateScript(jscript % (desk, plugin, plugin, filepath, layout))
def apply_image(self):
self.set_wallpaper(self.image, 'org.kde.image', self.layout, self.monitor)
if __name__ == '__main__':
if len(sys.argv) == 1:
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
else:
parser = argparse.ArgumentParser(description='KDE Wallpaper setter')