forked from utdemir/simpleshortcuts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainui.py
163 lines (108 loc) · 4.52 KB
/
mainui.py
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
from __future__ import division
from functools import partial
import sip
sip.setapi("QString", 2)
sip.setapi("QVariant", 2)
from PyQt4 import QtCore, QtGui
from settings import Settings
from settingsdialog import SettingsDialog
from utils import get_qicon
class MainUi(QtGui.QWidget):
def __init__(self):
super(QtGui.QWidget, self).__init__()
self.setWindowTitle("SimpleShortcuts")
main_layout = QtGui.QVBoxLayout()
main_layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
self.shortcut_layout = QtGui.QGridLayout()
bottom_layout = QtGui.QHBoxLayout()
settings_button = QtGui.QPushButton("Settings")
settings_button.clicked.connect(self.settings_button_clicked)
kill_button = QtGui.QPushButton("Kill Application")
kill_button.clicked.connect(partial(self.run, "xkill"))
bottom_layout.addWidget(kill_button)
bottom_layout.addStretch()
bottom_layout.addWidget(settings_button)
main_layout.addLayout(self.shortcut_layout)
main_layout.addLayout(bottom_layout)
self.setLayout(main_layout)
self.settings = Settings.read()
self.refresh_shortcuts()
def refresh_shortcuts(self):
layout = self.shortcut_layout
if not self.settings:
self.settings_button_clicked()
return
options = self.settings["options"]
column_width = options["column_width"]
column_count = options["column_count"]
row, column = layout.rowCount(), layout.columnCount()
for x in range(row):
for y in range(column):
item = layout.itemAtPosition(x, y)
if item is not None:
item.widget().hide()
layout.removeItem(item)
shortcuts = self.settings["shortcuts"]
rows = self._chunks(shortcuts, column_count)
for x, row in enumerate(rows):
for y, shortcut in enumerate(row):
button = QtGui.QPushButton()
button.setMaximumWidth(column_width)
button.setMinimumWidth(column_width)
if options["show_names"]:
name = shortcut["name"]
if options["show_hints"]:
try:
name += " (&{})".format(
options["row{}_keys".format(x+1)][y]
)
except:
pass
button.setText(name)
button.setIcon(get_qicon(shortcut["icon"]))
size = options["icon_size"]
button.setIconSize(QtCore.QSize(size, size))
button.clicked.connect(partial(self.run, shortcut["command"]))
layout.addWidget(button, x, y)
def settings_button_clicked(self):
s = SettingsDialog(self)
ret = s.exec_()
if ret == 1:
Settings.save(s.settings)
self.settings = Settings.read()
self.refresh_shortcuts()
def run(self, command):
print(command)
command = str(command)
#for reducing startup time
from shlex import split
from subprocess import Popen
from sys import exit
splitted = split(command)
try:
Popen(splitted)
except OSError as e:
box = QtGui.QMessageBox()
box.setWindowTitle("Error - SimpleShortcuts")
box.setText('Error when trying to run "{0}"'.format(command))
box.setDetailedText(e.strerror)
box.setStandardButtons(box.Close)
box.setDefaultButton(box.Close)
box.exec_()
else:
exit(0)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
exit(0)
@staticmethod
def _chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
rect = app.desktop().geometry()
g = MainUi()
g.show()
g.move(rect.center() - g.rect().center())
app.exec_()