-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabber.py
233 lines (179 loc) · 7.63 KB
/
tabber.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import sys,os,signal
from PySide6 import QtCore, QtWidgets, QtGui
import wmctrl
CMD_LIST = [
("xterm", "xterm"),
("rdp01", "xfreerdp /u:zxt /p:t /v:127.0.0.1 /port:33891 -wallpaper +clipboard +compression /bpp:24 /dynamic-resolution"),
("vnc-157", "vncviewer -passwd <(vncpasswd -f <<<'yourpassword') 192.168.0.157:5900"),
]
class TabberSettingWidget(QtWidgets.QWidget):
'''
The first MAIN tab
'''
def __init__(self, tabwidget):
super().__init__()
self.layout = QtWidgets.QVBoxLayout()
self.tabwidget = tabwidget
self.tab_type = 'tabber'
# ToolBar
self.tool_bar = QtWidgets.QToolBar()
self.tool_bar_name_label = QtWidgets.QLabel("name:")
self.tool_bar_name_edit = QtWidgets.QLineEdit()
self.tool_bar_cmd_label = QtWidgets.QLabel(" cmd:")
self.tool_bar_cmd_edit = QtWidgets.QLineEdit()
self.tool_bar_run_btn = QtWidgets.QPushButton("run")
self.tool_bar.addWidget(self.tool_bar_name_label)
self.tool_bar.addWidget(self.tool_bar_name_edit)
self.tool_bar.addWidget(self.tool_bar_cmd_label)
self.tool_bar.addWidget(self.tool_bar_cmd_edit)
self.tool_bar.addWidget(self.tool_bar_run_btn)
self.tool_bar_name_edit.setMaximumWidth(100)
self.tool_bar_run_btn.clicked.connect(self.tool_bar_conn_btn_click)
self.layout.addWidget(self.tool_bar)
########
# Table cmd List
self.table_cmds = QtWidgets.QTableWidget()
self.table_cmds.setRowCount(len(CMD_LIST))
self.table_cmds.setColumnCount(2)
self.table_cmds.setHorizontalHeaderLabels(["name", "cmd"])
header = self.table_cmds.horizontalHeader()
header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
self.table_cmds.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
for i, (name,cmd) in enumerate(CMD_LIST):
item_name = QtWidgets.QTableWidgetItem(name)
item_cmd = QtWidgets.QTableWidgetItem(cmd)
self.table_cmds.setItem(i, 0, item_name)
self.table_cmds.setItem(i, 1, item_cmd)
self.table_cmds.cellDoubleClicked.connect(self.table_cmds_cell_double_click)
self.layout.addWidget(self.table_cmds)
########
# All Win ToolBar
self.tool_bar_all_wins = QtWidgets.QToolBar()
self.tool_bar_all_wins_name_label = QtWidgets.QLabel("All Windows ")
self.tool_bar_all_wins_fresh_btn = QtWidgets.QPushButton("fresh")
self.tool_bar_all_wins.addWidget(self.tool_bar_all_wins_name_label)
self.tool_bar_all_wins.addWidget(self.tool_bar_all_wins_fresh_btn)
self.tool_bar_all_wins_fresh_btn.clicked.connect(self.tool_bar_fresh_btn_click)
self.layout.addWidget(self.tool_bar_all_wins)
########
# All Wins List
self.table_all_wins = QtWidgets.QTableWidget()
self.table_all_wins.setColumnCount(3)
self.table_all_wins.setHorizontalHeaderLabels(["pid", "wid", "name"])
header = self.table_all_wins.horizontalHeader()
header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
self.table_all_wins.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
self.tool_bar_fresh_btn_click()
self.table_all_wins.cellDoubleClicked.connect(self.table_all_wins_cell_double_click)
self.layout.addWidget(self.table_all_wins)
########
self.setLayout(self.layout)
@QtCore.Slot()
def tool_bar_fresh_btn_click(self):
ws = wmctrl.Window.list()
self.table_all_wins.setRowCount(len(ws))
for i, w in enumerate(ws):
item_name = QtWidgets.QTableWidgetItem(w.wm_name)
item_wid = QtWidgets.QTableWidgetItem(w.id)
item_pid = QtWidgets.QTableWidgetItem(str(w.pid))
self.table_all_wins.setItem(i, 0, item_pid)
self.table_all_wins.setItem(i, 1, item_wid)
self.table_all_wins.setItem(i, 2, item_name)
@QtCore.Slot()
def table_cmds_cell_double_click(self, row, col):
name = self.table_cmds.item(row,0).text()
cmd = self.table_cmds.item(row,1).text()
self.tool_bar_name_edit.setText(name)
self.tool_bar_cmd_edit.setText(cmd)
self.tool_bar_conn_btn_click()
@QtCore.Slot()
def table_all_wins_cell_double_click(self, row, col):
pid = int(self.table_all_wins.item(row,0).text())
wid = int(self.table_all_wins.item(row,1).text(),16)
name = self.table_all_wins.item(row,2).text()
self.tabwidget.new_tab(pid,wid,name)
self.tool_bar_fresh_btn_click()
@QtCore.Slot()
def tool_bar_conn_btn_click(self):
#wid = int(self.tool_bar_edit.text(), 16)
#self.tabwidget.new_tab(0, wid)
name = self.tool_bar_name_edit.text()
cmd = self.tool_bar_cmd_edit.text().split()
proc = QtCore.QProcess()
proc.start(cmd[0], cmd[1:])
proc.waitForStarted(1000)
pid = proc.processId()
import time
wins = []
for _ in range(5):
wins = list(filter(lambda w: w.pid == pid, wmctrl.Window.list()))
if len(wins) == 0:
time.sleep(1)
if len(wins) == 0:
return
wid = int(wins[0].id, 16)
self.tabwidget.new_tab(pid, wid, name, proc)
def tabber_close(self):
pass
class TabberWinWidget(QtWidgets.QWidget):
'''
Window container tab
'''
def __init__(self, pid:int, wid:int, name:str, proc=None):
super().__init__()
self.tab_type = 'win'
self.pid = pid
self.wid = wid
self.name = name
self.proc = proc
self.win = QtGui.QWindow.fromWinId(wid)
self.old_flags = self.win.flags()
self.layout = QtWidgets.QVBoxLayout()
self.win_container = self.createWindowContainer(self.win, self)
self.layout.addWidget(self.win_container)
def resizeEvent(self, event) -> None:
win = QtGui.QWindow.fromWinId(self.wid)
win.resize(self.size())
return super().resizeEvent(event)
def tabber_close(self):
try:
os.kill(self.pid, signal.SIGKILL)
except:
pass
class TabberTabWidget(QtWidgets.QTabWidget):
'''
TabWidget
'''
def __init__(self):
super().__init__()
self.setTabsClosable(True)
self.setMovable(True)
self.setting_tab = TabberSettingWidget(self)
self.addTab(self.setting_tab, "MAIN")
self.tabCloseRequested.connect(self.close_tab)
def new_tab(self, pid:int, wid:int, name:str, proc=None):
wintab = TabberWinWidget(pid, wid, name, proc)
win = QtGui.QWindow.fromWinId(wid)
self.addTab(wintab, name)
@QtCore.Slot()
def close_tab(self, idx):
widget = self.widget(idx)
if widget.tab_type == 'win':
ok = QtWidgets.QMessageBox.question(self, "Close", "close this window?",
QtWidgets.QMessageBox.Ok,
QtWidgets.QMessageBox.Cancel)
if ok == QtWidgets.QMessageBox.Ok:
widget.tabber_close()
self.removeTab(idx)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("tabber")
self.tab_widget = TabberTabWidget()
self.setCentralWidget(self.tab_widget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.resize(800, 600)
window.show()
sys.exit(app.exec())