-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydialogs.py
More file actions
302 lines (244 loc) · 9.93 KB
/
mydialogs.py
File metadata and controls
302 lines (244 loc) · 9.93 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import string
import secrets
import datetime
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import QLineEdit, QLabel, QPushButton, QPlainTextEdit, \
QDialogButtonBox, QDialog, QMessageBox, QHBoxLayout, QFrame, \
QApplication, QVBoxLayout, QGridLayout
from mytableview import HistoryTableView
from mimabox import MimaBox, HistoryBox
from passwordedit import PasswordEdit
import pymimaconst
from pymimaconst import HISTORY_COLUMNS, create_model
class MyDialog(QDialog):
def __init__(self, mimatemp_model, parent=None):
super().__init__(parent)
self.PASSWORD_LENGTH = 15
self.alphabet = string.ascii_letters + string.digits
# self.setStyleSheet(
# 'QLabel {font-size: 18px;}'
# 'QPushButton {font-size: 18px;}'
# 'QLineEdit {font-size: 18px;}'
# 'QPlainTextEdit {font-size: 18px;}')
self.setWindowFlag(Qt.WindowContextHelpButtonHint, False)
self.mimatemp_model = mimatemp_model
def _init_ui_(self):
self.vBox = QVBoxLayout()
self.setLayout(self.vBox)
grid = QGridLayout()
self.vBox.addLayout(grid)
titleLabel = QLabel('Title')
self.titleEdit = QLineEdit()
titleLabel.setBuddy(self.titleEdit)
grid.addWidget(titleLabel, 0, 0)
grid.addWidget(self.titleEdit, 0, 1)
websiteLabel = QLabel('Website')
self.websiteEdit = QLineEdit()
websiteLabel.setBuddy(self.websiteEdit)
grid.addWidget(websiteLabel, 1, 0)
grid.addWidget(self.websiteEdit, 1, 1)
usernameLabel = QLabel('Username')
self.usernameEdit = QLineEdit()
usernameLabel.setBuddy(self.usernameEdit)
grid.addWidget(usernameLabel, 2, 0)
grid.addWidget(self.usernameEdit, 2, 1)
passwordLabel = QLabel('Password')
self.passwordEdit = PasswordEdit(self)
self.passwordEdit.setFixedHeight(30)
self.passwordEdit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
passwordLabel.setBuddy(self.passwordEdit)
grid.addWidget(passwordLabel, 3, 0)
grid.addWidget(self.passwordEdit, 3, 1)
# self.passwordEdit.highlighter = PasswordHighlighter(
# self.passwordEdit.document())
self.generateButton = QPushButton('generate')
self.generateButton.setFixedHeight(30)
grid.addWidget(self.generateButton, 3, 1, Qt.AlignRight)
# font = self.generateButton.font()
# font.setPixelSize(14)
# self.generateButton.setFont(font)
self.generateButton.setStyleSheet(
'QPushButton {font-size: 14px; font-family: Sans;}')
self.generateButton.clicked.connect(self.generate)
notesLabel = QLabel('Notes')
# notesLabel.setAlignment(Qt.AlignTop)
self.notesEdit = QPlainTextEdit()
self.notesEdit.setFixedHeight(100)
notesLabel.setBuddy(self.notesEdit)
grid.addWidget(notesLabel, 4, 0, Qt.AlignTop)
grid.addWidget(self.notesEdit, 4, 1)
# self.notesEdit.highlighter = PasswordHighlighter(
# self.notesEdit.document())
# submitButton = QPushButton('Submit')
# submitButton.setAutoDefault(False)
# submitButton.clicked.connect(self.addToDatabase)
# cancelButton = QPushButton('Cancel')
# cancelButton.setAutoDefault(False)
# cancelButton.clicked.connect(self.close)
# buttonBox = QDialogButtonBox(Qt.Horizontal)
# buttonBox.addButton(cancelButton, QDialogButtonBox.ActionRole)
# cancelButton.clicked.connect(self.close)
# cancelButton = buttonBox.addButton(QDialogButtonBox.Cancel)
# buttonBox.rejected.connect(self.reject)
self.buttonBox = QDialogButtonBox(
QDialogButtonBox.Save | QDialogButtonBox.Cancel, Qt.Horizontal)
self.buttonBox.rejected.connect(self.reject)
self.buttonBox.accepted.connect(self.accept)
grid.addWidget(self.buttonBox, 5, 1, 2, 1)
self.resize(QSize(400, self.sizeHint().height()))
def generate(self):
password = self.new_password()
self.passwordEdit.setPlainText(password)
def new_password(self):
while True:
password = ''.join(secrets.choice(self.alphabet) for i in range(
self.PASSWORD_LENGTH))
if (any(c.islower() for c in password) and
any(c.isupper() for c in password) and
any(c.isdigit() for c in password)):
return password
def accept(self):
if self.titleEdit.text().strip():
self.really_accept()
else:
QMessageBox.warning(self,
'Need a title.',
'The title cannot be blank.\n'
'Please input a title.')
self.titleEdit.setFocus()
def show_not_unique_message(self):
QMessageBox.warning(
self,
'Not Unique',
'The (title, username) pair already exists.')
self.titleEdit.setFocus()
class AddDialog(MyDialog):
def __init__(self, mimatemp_model, parent=None):
super().__init__(mimatemp_model, parent)
self.setMinimumWidth(425)
self.setWindowTitle('Add')
self.box = MimaBox()
super()._init_ui_()
def really_accept(self):
self.box.title = self.titleEdit.text().strip()
self.box.username = self.usernameEdit.text().strip()
self.box.website = self.websiteEdit.text().strip()
self.box.password = self.passwordEdit.toPlainText().strip()
self.box.notes = self.notesEdit.toPlainText().strip()
if self.box.is_unique():
self.box.insert_into_database_and_temp()
self.mimatemp_model.submitAll()
QDialog.accept(self)
else:
self.show_not_unique_message()
class EditDialog(MyDialog):
def __init__(self, box, mimatemp_model, parent=None):
super().__init__(mimatemp_model, parent)
self.setMinimumWidth(500)
self.setWindowTitle('Edit')
self.box = box
super()._init_ui_()
self.resetButton = self.buttonBox.addButton(
'Reset', QDialogButtonBox.ResetRole)
self.resetButton.clicked.connect(self.update_form)
self.update_form()
self._init_table_()
if self.tableView.rowAt(0) == -1:
self.tableView.hide()
else:
self.tableView.show()
self.show_history_table()
self.setMinimumHeight(600)
def show_history_table(self):
self.vBox.addSpacing(20)
self.vBox.addWidget(self.make_hLine())
self.vBox.addWidget(QLabel('History'))
self.vBox.addSpacing(20)
self.vBox.addWidget(self.tableView)
hBox = QHBoxLayout()
self.deleteButton = QPushButton('Delete')
self.deleteButton.clicked.connect(self.delete_history)
hBox.addStretch(1)
hBox.addWidget(self.deleteButton)
self.vBox.addLayout(hBox)
def make_hLine(self):
hLine = QFrame(self)
hLine.setLineWidth(1)
hLine.setMidLineWidth(1)
hLine.setFrameStyle(QFrame.HLine | QFrame.Sunken)
return hLine
def update_form(self, box=None):
if not box:
box = self.box
self.titleEdit.setText(box.title)
self.usernameEdit.setText(box.username)
self.websiteEdit.setText(box.website)
self.passwordEdit.setPlainText(box.password)
self.notesEdit.setPlainText(box.notes)
def _init_table_(self):
self.model = create_model('historytemp')
self.model.setFilter(f"mimanonce = '{self.box.nonce}'")
self.model.setSort(HISTORY_COLUMNS['deleted'], Qt.DescendingOrder)
self.tableView = HistoryTableView(self)
self.tableView.setModel(self.model)
self.tableView.customize_columns()
def really_accept(self):
has_changed = False
old_values = [
self.box.title,
self.box.username,
self.box.website,
self.box.password,
self.box.notes
]
new_values = [
self.titleEdit.text().strip(),
self.usernameEdit.text().strip(),
self.websiteEdit.text().strip(),
self.passwordEdit.toPlainText().strip(),
self.notesEdit.toPlainText().strip()
]
for i in range(len(old_values)):
if old_values[i] != new_values[i]:
has_changed = True
break
if not has_changed:
QDialog.reject(self)
return
has_changed = False
new_box_dict = dict(
title=self.titleEdit.text().strip(),
username=self.usernameEdit.text().strip(),
website=self.websiteEdit.text().strip(),
password=self.passwordEdit.toPlainText().strip(),
notes=self.notesEdit.toPlainText().strip(),
favorite=self.box.favorite
)
new_box = MimaBox(nonce=self.box.nonce, **new_box_dict)
if new_box.is_unique_except_itself():
historyBox = HistoryBox()
historyBox.get_values_from_mimabox(self.box.nonce)
historyBox.deleted = datetime.datetime.now().isoformat(sep=' ')
historyBox.insert_into_database_and_temp()
self.model.submitAll()
new_box.update_temp()
new_box.update_database()
self.mimatemp_model.submitAll()
QDialog.accept(self)
else:
self.show_not_unique_message()
def delete_history(self):
self.COLUMNS = HISTORY_COLUMNS
self.confirm_message = "Delete the seleted record?\n" \
"(Can not recover)"
pymimaconst.delete(self)
def move_to_trash_or_delete(self, nonce):
box = HistoryBox(nonce)
box.delete_forever()
self.model.submitAll()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = AddDialog()
window.show()
sys.exit(app.exec_())