summaryrefslogtreecommitdiff
path: root/nitroplasm.py
blob: aed2ce1ff43fc39a8097f4205efb92c938886484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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
from Xlib import display
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):
    if 'RANDR' in display.Display().list_extensions():
      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:
        return 1
    else:
      return 1

  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')