Repository navigation
Expand file tree
/
Copy pathSettingsDialog.py
More file actions
153 lines (131 loc) · 6.1 KB
/
Copy pathSettingsDialog.py
File metadata and controls
153 lines (131 loc) · 6.1 KB
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
# MPV-VJ2 Copyright 2016 paulguy <paulguy119@gmail.com>
#
# This file is part of MPV-VJ2.
#
# MPV-VJ2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MPV-VJ2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MPV-VJ2. If not, see <http://www.gnu.org/licenses/>.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import PromptDialog
class SettingsDialog(Gtk.Dialog):
def closeAlert(self, dialog, response):
if response != Gtk.ResponseType.DELETE_EVENT:
dialog.destroy()
def editKeyResponse(self, dialog, response, key):
if response == Gtk.ResponseType.OK:
if dialog.get_text() in self.options:
alertBox = Gtk.MessageDialog(parent=dialog,
flags=Gtk.DialogFlags.MODAL,
type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
message_format="Key already exists.")
alertBox.connect('response', self.closeAlert)
alertBox.show_all()
else:
newKey = dialog.get_text()
value = self.options[key]
del self.options[key]
self.options[newKey] = value
for row in self.listStore:
if row[0] == key:
row[0] = newKey
break
if response != Gtk.ResponseType.DELETE_EVENT:
dialog.destroy()
def editValueResponse(self, dialog, response, key):
value = dialog.get_text()
if response == Gtk.ResponseType.OK:
self.options[key] = value
for row in self.listStore:
if row[0] == key:
row[1] = value
break
if response != Gtk.ResponseType.DELETE_EVENT:
dialog.destroy()
def editEntry(self, listView, row, column):
key = self.listStore[row.get_indices()[0]][0]
if self.add == True and column.get_title() == "Key":
editDialog = PromptDialog.PromptDialog(self, "Edit Key", key)
editDialog.connect('response', self.editKeyResponse, key)
editDialog.show_all()
else:
editDialog = PromptDialog.PromptDialog(self, "Edit Value", self.options[key])
editDialog.connect('response', self.editValueResponse, key)
editDialog.show_all()
def addKeyResponse(self, dialog, response):
key = dialog.get_text()
if response == Gtk.ResponseType.OK:
self.options[key] = ""
i = self.listStore.get_iter_first()
self.listStore.insert_before(i, (key, ""))
if response != Gtk.ResponseType.DELETE_EVENT:
dialog.destroy()
def addKey(self, button):
editDialog = PromptDialog.PromptDialog(self, "Add Key")
editDialog.connect('response', self.addKeyResponse)
editDialog.show_all()
def delKey(self, button):
row, column = self.listView.get_cursor()
key = self.listStore[row.get_indices()[0]][0]
del self.options[key]
del self.listStore[row.get_indices()[0]]
def __init__(self, lastWindow, options, add=False):
Gtk.Dialog.__init__(self, title="Settings")
self.add = add
self.set_modal(True)
self.set_transient_for(lastWindow)
if type(options) != dict:
raise TypeError
if type(add) != bool:
raise TypeError
self.options = options
self.listStore = Gtk.ListStore(str, str)
for key in self.options.keys():
if type(key) != str or type(self.options[key]) != str:
raise TypeError
self.listStore.append((key, self.options[key]))
self.listView = Gtk.TreeView.new_with_model(self.listStore)
self.keyColumn = Gtk.TreeViewColumn("Key", Gtk.CellRendererText(), text=0)
self.keyColumn.set_resizable(True)
self.keyColumn.set_sizing(Gtk.TreeViewColumnSizing.GROW_ONLY)
self.listView.append_column(self.keyColumn)
self.valueColumn = Gtk.TreeViewColumn("Value", Gtk.CellRendererText(), text=1)
self.valueColumn.set_resizable(True)
self.valueColumn.set_sizing(Gtk.TreeViewColumnSizing.GROW_ONLY)
self.listView.append_column(self.valueColumn)
self.listView.set_search_column(0)
self.listView.set_enable_search(True)
self.listView.connect('row-activated', self.editEntry)
if self.add:
self.buttonBox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=1)
self.addBtn = Gtk.Button()
self.addBtn.add(Gtk.Image.new_from_icon_name('list-add', Gtk.IconSize.BUTTON))
self.addBtn.connect('clicked', self.addKey)
self.delBtn = Gtk.Button()
self.delBtn.add(Gtk.Image.new_from_icon_name('list-remove', Gtk.IconSize.BUTTON))
self.delBtn.connect('clicked', self.delKey)
self.buttonBox.pack_start(self.addBtn, False, False, 0)
self.buttonBox.pack_start(self.delBtn, False, False, 0)
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
scroll = Gtk.ScrolledWindow()
scroll.add(self.listView)
self.box.pack_start(scroll, True, True, 0)
self.box.pack_start(self.buttonBox, False, False, 0)
self.get_content_area().pack_start(self.box, True, True, 0)
else:
scroll = Gtk.ScrolledWindow()
scroll.add(self.listView)
self.get_content_area().pack_start(scroll, True, True, 0)
self.add_button("Close", Gtk.ResponseType.CLOSE)
self.resize(200, 200)