-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheditor.py
More file actions
857 lines (657 loc) · 31.9 KB
/
Copy patheditor.py
File metadata and controls
857 lines (657 loc) · 31.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
import traceback
from PyQt5.QtCore import QPoint, QDateTime
from PyQt5.QtWidgets import QLineEdit, QAbstractItemView, QFileDialog, QCheckBox, QWidget, QLabel, QTextEdit, \
QTabWidget, QComboBox, QGridLayout, QRadioButton, QListWidget, QListWidgetItem, QInputDialog, QToolTip, \
QCalendarWidget, QDateTimeEdit, QSplitter
from core import *
from Utility.ui_utility import *
from Utility.DateTimePicker import DateTimePicker
# ---------------------------------------------- class HistoryRecordEditor ---------------------------------------------
class HistoryRecordEditor(QWidget):
"""
Update 20250205: For avoiding multiple record list.
HistoryRecordEditor only record editing history record and the source it belongs to.
The full history record list MUST get from class History.
"""
class Agent:
def __init__(self):
pass
def on_apply(self):
pass
def on_cancel(self):
pass
def __init__(self, history: History, parent: QWidget):
super(HistoryRecordEditor, self).__init__(parent)
self.__history = history
self.__source = ''
self.__current_record = None
self.__operation_agents = []
self.__tab_main = QTabWidget()
self.__current_depot = 'default'
# self.__combo_depot = QComboBox()
self.__combo_records = QComboBox()
self.__label_uuid = QLabel()
self.__label_source = QLabel()
self.__line_time = ShadowLineEdit()
self.__line_location = QLineEdit()
self.__line_people = QLineEdit()
self.__line_organization = QLineEdit()
self.__line_default_tags = QLineEdit()
self.__button_save_and_new = QPushButton('Save and New', self)
self.__button_calendar = QPushButton('Calendar', self)
self.__button_auto_time = QPushButton('Auto Detect')
self.__button_auto_location = QPushButton('Auto Detect')
self.__button_auto_people = QPushButton('Auto Detect')
self.__button_auto_organization = QPushButton('Auto Detect')
self.__radio_time = QRadioButton('Time')
self.__radio_location = QRadioButton('Location')
self.__radio_people = QRadioButton('Participant')
self.__radio_organization = QRadioButton('Organization')
self.__radio_record = QRadioButton('Event')
self.__check_time = QCheckBox('Lock')
self.__check_location = QCheckBox('Lock')
self.__check_people = QCheckBox('Lock')
self.__check_organization = QCheckBox('Lock')
self.__check_default_tags = QCheckBox('Lock')
self.__line_title = QLineEdit()
self.__text_brief = QTextEdit()
self.__text_record = QTextEdit()
self.__table_tags = EasyQTableWidget()
self.__button_new = QPushButton('New Event')
self.__button_open_file = QPushButton('Open File')
self.__button_new_file = QPushButton('New File')
self.__button_del = QPushButton('Del Event')
self.__button_apply = QPushButton('Apply')
self.__button_cancel = QPushButton('Cancel')
self.init_ui()
self.config_ui()
self.create_new_file()
def init_ui(self):
root_layout = QVBoxLayout()
line = QHBoxLayout()
# line.addWidget(QLabel('Event Source'), 0)
line.addWidget(self.__label_source, 1)
line.addWidget(self.__button_open_file, 0)
line.addWidget(self.__button_new_file, 0)
root_layout.addLayout(line)
line = QHBoxLayout()
line.addWidget(self.__combo_records, 1)
line.addWidget(self.__button_new, 0)
line.addWidget(self.__button_del, 0)
root_layout.addLayout(line)
root_layout.addWidget(self.__tab_main)
root_layout.addLayout(horizon_layout([self.__button_apply, self.__button_cancel]))
self.setLayout(root_layout)
record_page_layout = create_new_tab(self.__tab_main, 'Event Editor')
property_layout = QGridLayout()
row = 0
property_layout.addWidget(QLabel('Event ID'), row, 0)
property_layout.addWidget(self.__label_uuid, row, 1)
property_layout.addWidget(self.__button_save_and_new, row, 2)
row += 1
# property_layout.addWidget(QLabel('Event Time'), 1, 0)
property_layout.addWidget(self.__radio_time, row, 0)
layout = QHBoxLayout()
layout.addWidget(self.__line_time)
layout.addWidget(self.__button_calendar)
property_layout.addLayout(layout, row, 1)
property_layout.addWidget(self.__button_auto_time, row, 2)
property_layout.addWidget(self.__check_time, row, 3)
row += 1
# property_layout.addWidget(QLabel('Event Location'), 2, 0)
property_layout.addWidget(self.__radio_location, row, 0)
property_layout.addWidget(self.__line_location, row, 1)
property_layout.addWidget(self.__button_auto_location, row, 2)
property_layout.addWidget(self.__check_location, row, 3)
row += 1
# property_layout.addWidget(QLabel('Event Participant'), 3, 0)
property_layout.addWidget(self.__radio_people, row, 0)
property_layout.addWidget(self.__line_people, row, 1)
property_layout.addWidget(self.__button_auto_people, row, 2)
property_layout.addWidget(self.__check_people, row, 3)
row += 1
# property_layout.addWidget(QLabel('Event Organization'), 4, 0)
property_layout.addWidget(self.__radio_organization, row, 0)
property_layout.addWidget(self.__line_organization, row, 1)
property_layout.addWidget(self.__button_auto_organization, row, 2)
property_layout.addWidget(self.__check_organization, row, 3)
# row += 1
# property_layout.addWidget(QLabel('Event Tags'), row, 0)
# property_layout.addWidget(self.__line_default_tags, row, 1, 1, 2)
# property_layout.addWidget(self.__check_default_tags, row, 3)
row += 1
self.__radio_record.setChecked(True)
property_layout.addWidget(self.__radio_record, row, 0)
record_page_layout.addLayout(property_layout)
group, layout = create_v_group_box('')
record_page_layout.addWidget(group)
layout.addWidget(QLabel('Event Title'))
layout.addWidget(self.__line_title)
layout.addWidget(QLabel('Event Brief'))
layout.addWidget(self.__text_brief, 2)
layout.addWidget(QLabel('Event Description'))
layout.addWidget(self.__text_record, 5)
layout = create_new_tab(self.__tab_main, 'Label Tag Editor')
layout.addWidget(self.__table_tags)
def config_ui(self):
self.__text_brief.setAcceptRichText(False)
self.__text_record.setAcceptRichText(False)
self.__line_time.textChanged.connect(self.on_line_time_changed)
self.__button_save_and_new.clicked.connect(self.on_button_save_and_new)
self.__button_calendar.clicked.connect(self.on_button_pick_date_time)
self.__button_auto_time.clicked.connect(self.on_button_auto_time)
self.__button_auto_location.clicked.connect(self.on_button_auto_location)
self.__button_auto_people.clicked.connect(self.on_button_auto_people)
self.__button_auto_organization.clicked.connect(self.on_button_auto_organization)
self.__button_new.clicked.connect(self.on_button_new)
self.__button_open_file.clicked.connect(self.on_button_open_file)
self.__button_new_file.clicked.connect(self.on_button_new_file)
self.__button_del.clicked.connect(self.on_button_del)
self.__button_apply.clicked.connect(self.on_button_apply)
self.__button_cancel.clicked.connect(self.on_button_cancel)
self.__combo_records.currentIndexChanged.connect(self.on_combo_records)
def update_combo_records(self):
self.__combo_records.clear()
records = self.__history.get_record_by_source(self.__source)
sorted_records = History.sort_records(records)
if self.__current_record:
# Check whether current record is in the list. If not, it should be a new-created record.
matching_records = list(filter(lambda r: r.uuid() == self.__current_record.uuid(), sorted_records))
if not matching_records:
sorted_records.append(self.__current_record)
# If not specify current_record, it will select the 1st item by default.
index = -1 if self.__current_record else 0
for i in range(0, len(sorted_records)):
record = sorted_records[i]
self.__combo_records.addItem('[' + HistoryTime.format_tick(record.since()) + '] ' +
record.uuid())
self.__combo_records.setItemData(i, record.uuid())
if record == self.__current_record:
index = i
if index == 0:
# When adding the first item, the index will change from -1 to 0, which will invoke on_combo_records().
# But the item data is not prepared at this time.
# However, if we setCurrentIndex(0) here, because the index has no change.
# So the on_combo_records() will not be invoked.
# Manual call this function for workaround.
self.on_combo_records()
elif index > 0:
self.__combo_records.setCurrentIndex(index)
else:
print('Cannot find the current record in combobox - empty record list.')
# else:
# # Record is not in history. Should be a file just loaded.
# if self.__combo_records.count() > 0:
# # self.__combo_records.setCurrentIndex(0)
# self.on_combo_records()
# print('Cannot find the current record in combobox - use index 0.')
# else:
# print('Cannot find the current record in combobox - empty record list.')
# ---------------------------------------------------- Features ----------------------------------------------------
def add_agent(self, agent):
self.__operation_agents.append(agent)
def edit_source(self, source: str, edit_record_uuid: str = '') -> bool:
"""
Edit or create a record in a source.
source - The source the contains the edit record.
If source is empty or None, editor will ask to create a new source when saving record editing.
edit_record_uuid - The uuid of a record that to be selected (edited).
If the specified uuid cannot be found in this source, editor will select the first record.
If current_uuid is None or '', that means create a new record.
"""
if source and source not in self.__history.get_source_list():
self.__history.load_source(source)
self.__set_current_source(source)
self.__current_record = None
if edit_record_uuid:
records = self.__history.get_record_by_source(source)
matching_records = list(filter(lambda record: record.uuid() == edit_record_uuid, records))
if matching_records:
self.__current_record = matching_records[0]
else:
self.__current_record = None
else:
self.__current_record = HistoryRecord()
self.update_combo_records()
# self.record_to_ui(self.__current_record)
return True
def get_source(self) -> str:
return self.__source
def get_current_record(self) -> HistoryRecord:
return self.__current_record
def set_current_depot(self, depot: str):
self.__current_depot = depot
print('| Editor current depot: ' + depot)
def __set_current_source(self, source: str):
self.__source = source
print('Editor current source: ' + source)
# ---------------------------------------------------- UI Event ----------------------------------------------------
def keyPressEvent(self, event):
if event.key() == Qt.Key_S and event.modifiers() == Qt.ControlModifier:
self.on_button_apply()
def on_line_time_changed(self):
raw_time_str = self.__line_time.text()
history_ticks = HistoryTime.time_text_to_ticks(raw_time_str)
history_time_strs = [HistoryTime.format_tick(t, True) for t in history_ticks]
# Show the standard date time format for reference and double check
self.__line_time.set_shadow_text(', '.join(history_time_strs))
NOT_SUPPORT_TEMPLATE = 'In feature, we will use NLP or LLM to recognize the %s information from main text'
def on_button_save_and_new(self):
self.on_button_apply()
self.on_button_new()
def on_button_pick_date_time(self):
raw_time_str = self.__line_time.text()
history_ticks = HistoryTime.time_text_to_ticks(raw_time_str)
original_time = HistoryTime.tick_to_datetime(history_ticks[0]) if len(history_ticks) > 0 else None
dt, ok = DateTimePicker.pickDateTime(original_time)
if ok:
time_str = HistoryTime.format_datetime(dt)
self.__line_time.setText(time_str)
def on_button_auto_time(self):
QMessageBox.information(self, 'Not implemented', HistoryRecordEditor.NOT_SUPPORT_TEMPLATE % 'time')
def on_button_auto_location(self):
QMessageBox.information(self, 'Not implemented', HistoryRecordEditor.NOT_SUPPORT_TEMPLATE % 'location')
def on_button_auto_people(self):
QMessageBox.information(self, 'Not implemented', HistoryRecordEditor.NOT_SUPPORT_TEMPLATE % 'people')
def on_button_auto_organization(self):
QMessageBox.information(self, 'Not implemented', HistoryRecordEditor.NOT_SUPPORT_TEMPLATE % 'organization')
def on_button_new(self):
self.create_new_record()
def on_button_open_file(self):
depot_root = HistoryRecordLoader.get_local_depot_root()
fname, _ = QFileDialog.getOpenFileName(self,
'Select History Files',
depot_root,
'History Files (*.his)')
if fname:
self.edit_source(fname, 'xxx')
# Open single file. Current depot set empty.
self.set_current_depot('')
def on_button_new_file(self):
self.create_new_file()
def on_button_del(self):
if self.__current_record is not None:
self.__history.remove_record(self.__current_record.uuid())
self.__current_record = None
self.update_combo_records()
# Save the source
for agent in self.__operation_agents:
agent.on_apply()
def on_button_apply(self):
if not self.__source_valid():
# Should create a new file.
file_choose, _ = QFileDialog.getSaveFileName(self, 'New History File',
HistoryRecordLoader.get_local_depot_root(),
'History Files (*.his)')
if file_choose:
self.__source = file_choose
else:
return
new_record = HistoryRecord()
if self.__current_record is not None:
new_record.copy_uuid_from(self.__current_record)
new_record.set_source(self.__source)
if not self.ui_to_record(new_record):
print('Update data from UI FAIL.')
return
self.__history.upsert_records(self.__source, new_record)
self.__current_record = new_record
for agent in self.__operation_agents:
agent.on_apply()
self.update_combo_records()
def on_button_cancel(self):
for agent in self.__operation_agents:
agent.on_cancel()
def on_combo_records(self):
sel_index = self.__combo_records.currentIndex()
# print(f'ComboBox select - {sel_index}')
if sel_index < 0:
# Invalid selection. Will happen when, like clear() the combo box.
return
_uuid = self.__combo_records.currentData()
if _uuid is None:
# When we add first item. The current index changed (-1 -> 0).
# The function will be invoked, But the item data has not been added yet.
return
if self.__current_record and _uuid == self.__current_record.uuid():
# No change. Or it's the new created record.
self.record_to_ui(self.__current_record)
return
# Real selection changed. Get record from history.
record = self.__history.get_record_by_uuid(_uuid)
if record is None:
print(f'Cannot find record for uuid: {_uuid}')
return
self.__current_record = record
self.record_to_ui(record)
# --------------------------------------------------- Operation ----------------------------------------------------
def clear_ui(self):
lock_time = self.__check_time.isChecked()
lock_location = self.__check_location.isChecked()
lock_people = self.__check_people.isChecked()
lock_organization = self.__check_organization.isChecked()
lock_default_tags = self.__check_default_tags.isChecked()
self.__label_uuid.setText('')
self.__label_source.setText('')
if not lock_time:
self.__line_time.setText('')
if not lock_location:
self.__line_location.setText('')
if not lock_people:
self.__line_people.setText('')
if not lock_organization:
self.__line_organization.setText('')
if not lock_default_tags:
self.__line_default_tags.setText('')
self.__line_title.clear()
self.__text_brief.clear()
self.__text_record.clear()
restore_text_editor(self.__text_brief)
restore_text_editor(self.__text_record)
def ui_to_record(self, record: HistoryRecord) -> bool:
input_time = self.__line_time.text()
input_location = self.__line_location.text()
input_people = self.__line_people.text()
input_organization = self.__line_organization.text()
input_default_tags = self.__line_default_tags.text()
input_title = self.__line_title.text()
input_brief = self.__text_brief.toPlainText()
input_event = self.__text_record.toPlainText()
focus_time = self.__radio_time.isChecked()
focus_location = self.__radio_location.isChecked()
focus_poeple = self.__radio_people.isChecked()
focus_organization = self.__radio_organization.isChecked()
focus_record = self.__radio_record.isChecked()
focus_label = ''
input_valid = False
if not input_time.strip():
QMessageBox.information(None, 'Input check', 'Time field is required.', QMessageBox.Ok)
return False
if focus_time:
focus_label = 'time'
if focus_location:
focus_label = 'location'
input_valid = (len(input_location.strip()) != 0)
if focus_poeple:
focus_label = 'people'
input_valid = (len(input_people.strip()) != 0)
if focus_organization:
focus_label = 'organization'
input_valid = (len(input_organization.strip()) != 0)
if focus_record:
focus_label = 'event'
input_valid = (len(input_title.strip()) != 0 or
len(input_brief.strip()) != 0 or
len(input_event.strip()) != 0)
if not input_valid:
tips = f"The focus label you select is: {focus_label}.\n\nBut you didn't put content in it."
QMessageBox.information(None, 'Input check', tips, QMessageBox.Ok)
return False
record.set_label_tags('time', input_time.split(','))
record.set_label_tags('location', input_location.split(','))
record.set_label_tags('people', input_people.split(','))
record.set_label_tags('organization', input_organization.split(','))
record.set_label_tags('tags', input_default_tags.split(','))
record.set_label_tags('title', input_title)
record.set_label_tags('brief', input_brief)
record.set_label_tags('event', input_event)
record.set_focus_label(focus_label)
return True
def record_to_ui(self, record: HistoryRecord or str):
self.clear_ui()
self.__label_uuid.setText(LabelTagParser.tags_to_text(record.uuid()))
self.__display_source()
self.__line_time.setText(LabelTagParser.tags_to_text(record.time()))
self.__line_location.setText(LabelTagParser.tags_to_text(record.get_tags('location')))
self.__line_people.setText(LabelTagParser.tags_to_text(record.get_tags('people')))
self.__line_organization.setText(LabelTagParser.tags_to_text(record.get_tags('organization')))
self.__line_default_tags.setText(LabelTagParser.tags_to_text(record.get_tags('tags')))
self.__line_title.setText(LabelTagParser.tags_to_text(record.title()))
self.__text_brief.setText(LabelTagParser.tags_to_text(record.brief()))
self.__text_record.setText(LabelTagParser.tags_to_text(record.event()))
def create_new_record(self):
if self.__current_record is not None:
# TODO:
pass
self.__new_record()
self.clear_ui()
self.update_combo_records()
self.__label_uuid.setText(LabelTagParser.tags_to_text(self.__current_record.uuid()))
self.__display_source()
def create_new_file(self):
self.__new_file()
self.create_new_record()
# self.clear_ui()
# self.update_combo_records()
# self.__label_uuid.setText(LabelTagParser.tags_to_text(self.__current_record.uuid()))
# self.__display_source()
# self.__source = path.join(self.__current_depot, str(self.__current_record.uuid()) + '.his')
# self.__records.clear()
# self.create_new_record()
# def save_records(self):
# result = History.Loader().to_local_depot(self.__records, 'China', self.__source)
# tips = 'Save Successful.' if result else 'Save Fail.'
# tips += '\nSave File: ' + self.__source
# QMessageBox.information(None, 'Save', tips, QMessageBox.Ok)
# ------------------------------------------------------------------------------
def __new_file(self):
self.__set_current_source('')
self.__new_record()
def __new_record(self):
self.__current_record = HistoryRecord()
# self.__records.append(self.__current_record)
# if self.__source is None or self.__source == '':
# self.__source = path.join(self.__current_depot, str(self.__current_record.uuid()) + '.his')
# self.__set_current_source(self.__source)
# self.__current_record.set_source(self.__source)
def __source_valid(self):
return self.__source and self.__source != HistoryRecordLoader.INVALID_SOURCE
def __display_source(self):
text = self.__source if self.__source_valid() else 'No Source - Will ask for a source when saving record.'
self.__label_source.setText(text)
# --------------------------------------------- class HistoryRecordBrowser ---------------------------------------------
class HistoryRecordBrowser(QWidget):
class Agent:
def __init__(self):
pass
def on_select_depot(self, depot: str):
pass
def on_select_record(self, record: str):
pass
def __init__(self, parent: QWidget):
super(HistoryRecordBrowser, self).__init__(parent)
self.__ignore_combo = False
self.__ignore_list_change = False
self.__current_file = ''
self.__current_depot = 'default'
self.__operation_agents = []
self.__combo_depot = QComboBox()
self.__list_record = QListWidget()
self.__button_rename = QPushButton('Rename')
self.init_ui()
self.config_ui()
def init_ui(self):
root_layout = QVBoxLayout()
self.setLayout(root_layout)
root_layout.addWidget(self.__combo_depot, 0)
root_layout.addWidget(self.__list_record, 10)
root_layout.addWidget(self.__button_rename, 0)
def config_ui(self):
self.setMinimumWidth(200)
self.__update_combo_depot()
self.__combo_depot.currentIndexChanged.connect(self.on_combo_depot_changed)
# self.__list_record.selectionModel().selectionChanged.connect(self.on_list_record_changed)
self.__list_record.selectionModel().currentRowChanged.connect(self.on_list_record_changed)
self.__button_rename.clicked.connect(self.on_button_rename)
def add_agent(self, agent):
self.__operation_agents.append(agent)
def refresh(self):
self.__update_list_record(self.__current_depot)
def get_current_depot(self):
return self.__current_depot
def on_combo_depot_changed(self):
if self.__ignore_combo:
return
depot = self.__combo_depot.currentText()
self.__update_list_record(depot)
self.__current_depot = depot
for agent in self.__operation_agents:
agent.on_select_depot(depot)
def on_list_record_changed(self):
if self.__ignore_list_change:
return
index = self.__list_record.currentRow()
item = self.__list_record.currentItem()
print(f'Record list, current row: {index}')
if not item:
return
# items = self.__list_record.selectedItems()
# if not items:
# return
# item = items[0]
record_path = item.data(QtCore.Qt.UserRole)
if record_path == self.__current_file:
print('Record selection has no changed. Ignore...')
return
self.__current_file = record_path
for agent in self.__operation_agents:
agent.on_select_record(record_path)
def on_button_rename(self):
if self.__current_file is None or self.__current_file == '':
return
text, ok = QInputDialog.getText(self, 'Rename', 'New Name')
if not ok:
return
if not text.endswith('.his'):
text += '.his'
new_path = os.path.join(os.path.dirname(self.__current_file), text)
tip = 'Rename from \n"' + self.__current_file + '" \nto "' + new_path + '" '
try:
os.rename(self.__current_file, new_path)
QMessageBox.information(self, 'Rename', tip + 'Successful.', QMessageBox.Ok)
self.refresh()
except Exception as e:
QMessageBox.information(self, 'Rename', tip + 'Failed.', QMessageBox.Ok)
finally:
pass
def __update_combo_depot(self):
depots = HistoryRecordBrowser.enumerate_local_depot()
if len(depots) == 0:
return
self.__ignore_combo = True
for depot in depots:
self.__combo_depot.addItem(os.path.basename(depot), depot)
self.__ignore_combo = False
self.__combo_depot.setCurrentIndex(0)
self.on_combo_depot_changed()
def __update_list_record(self, depot: str):
record_dir_file = HistoryRecordBrowser.enumerate_depot_record(depot)
self.__ignore_list_change = True
current_item = None
self.__list_record.clear()
for record_path, record_file in record_dir_file:
source = path.join(record_path, record_file)
item = QListWidgetItem()
item.setText(record_file)
item.setData(QtCore.Qt.UserRole, source)
self.__list_record.addItem(item)
if source == self.__current_file:
current_item = item
if current_item is not None:
self.__list_record.setCurrentItem(current_item)
self.__ignore_list_change = False
@staticmethod
def enumerate_local_depot() -> list:
depot_root = HistoryRecordLoader.get_local_depot_root()
items = os.listdir(depot_root)
return [item for item in items if path.isdir(path.join(depot_root, item))]
@staticmethod
def enumerate_depot_record(depot: str) -> list:
record_dir_file = []
depot_path = HistoryRecordLoader.join_local_depot_path(depot)
for parent, dirnames, filenames in os.walk(depot_path):
for filename in filenames:
if filename.endswith('.his'):
record_dir_file.append((parent, filename))
return record_dir_file
# --------------------------------------------- class HistoryEditorDialog ----------------------------------------------
class HistoryEditorDialog(QDialog):
"""
Includes browser and editor. If we're editing specify record, the browser will hide.
"""
def __init__(self,
history: History,
editor_agent: HistoryRecordEditor.Agent = None,
browser_agent: HistoryRecordBrowser.Agent = None):
super(HistoryEditorDialog, self).__init__()
self.__history = history
self.__current_depot = 'default'
self.history_editor = HistoryRecordEditor(history, self)
self.history_editor.add_agent(editor_agent if editor_agent is not None else self)
self.history_browser = HistoryRecordBrowser(self)
self.history_browser.add_agent(browser_agent if browser_agent is not None else self)
splitter = QSplitter(Qt.Horizontal)
splitter.addWidget(self.history_browser)
splitter.addWidget(self.history_editor)
splitter.setStretchFactor(0, 3) # history_browser
splitter.setStretchFactor(1, 7) # history_editor
layout = QHBoxLayout()
layout.addWidget(splitter)
self.setLayout(layout)
self.setWindowFlags(int(self.windowFlags()) |
Qt.WindowMinMaxButtonsHint |
QtCore.Qt.WindowSystemMenuHint)
self.__current_depot = self.history_browser.get_current_depot()
self.history_editor.set_current_depot(self.__current_depot)
# self.setMinimumSize(1200, 800)
self.setWindowTitle('History Record Editor')
resize_widget_to_screen_percentage(self, 80)
# def showEvent(self, event):
# self.setFocus()
# event.accept()
def show_browser(self, show: bool = True):
self.get_history_browser().setVisible(show)
def get_history_editor(self) -> HistoryRecordEditor:
return self.history_editor
def get_history_browser(self) -> HistoryRecordBrowser:
return self.history_browser
# ------------------------------- HistoryRecordEditor.Agent -------------------------------
def on_apply(self):
source = self.history_editor.get_source()
result = self.__history.save_source(source)
tips = f'Save to {source} ' + ('successful.' if result == HistoryRecordLoader.E_SUCCESS else f'fail: {result}')
QMessageBox.information(self, 'Save Result', tips, QMessageBox.Ok)
self.history_browser.refresh()
def on_cancel(self):
self.close()
# ------------------------------ HistoryRecordBrowser.Agent ------------------------------
def on_select_depot(self, depot: str):
self.__current_depot = depot
self.history_editor.set_current_depot(depot)
def on_select_record(self, record: str):
self.history_editor.edit_source(record, 'xxx')
# ------------------------------------------------ File Entry : main() -------------------------------------------------
def main():
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_EnableHighDpiScaling)
history = History()
HistoryEditorDialog(history).exec()
# ----------------------------------------------------------------------------------------------------------------------
def exception_hook(type, value, tback):
# log the exception here
print('Exception hook triggered.')
print(type)
print(value)
print(tback)
# then call the default handler
sys.__excepthook__(type, value, tback)
if __name__ == "__main__":
sys.excepthook = exception_hook
try:
main()
except Exception as e:
print('Error =>', e)
print('Error =>', traceback.format_exc())
exit()
finally:
pass