forked from un-pogaz/Mass-Search-Replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
1372 lines (1122 loc) · 54.5 KB
/
config.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
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2020, un_pogaz <[email protected]>'
__docformat__ = 'restructuredtext en'
import os, time, shutil, copy
# calibre Python 3 compatibility.
from six import text_type as unicode
try:
load_translations()
except NameError:
pass # load_translations() added in calibre 1.9
try:
from PyQt5 import Qt as QtGui
from PyQt5 import QtCore
from PyQt5.Qt import (Qt, QToolButton, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit,
QFormLayout, QAction, QFileDialog, QDialog, QTableWidget,
QTableWidgetItem, QAbstractItemView, QComboBox, QCheckBox,
QGroupBox, QGridLayout, QRadioButton, QDialogButtonBox,
QPushButton, QSizePolicy)
except:
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import (Qt, QToolButton, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit,
QFormLayout, QAction, QFileDialog, QDialog, QTableWidget,
QTableWidgetItem, QAbstractItemView, QComboBox, QCheckBox,
QGroupBox, QGridLayout, QRadioButton, QDialogButtonBox,
QPushButton, QSizePolicy)
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
from functools import partial
from calibre.constants import iswindows
from calibre.utils.config import config_dir, JSONConfig
from calibre.gui2 import error_dialog, question_dialog, info_dialog, choose_files, open_local_file, FileDialog
from calibre.gui2.widgets2 import Dialog
from calibre.utils.zipfile import ZipFile
from calibre_plugins.mass_search_replace.SearchReplace import SearchReplaceDialog, KEY_OPERATION, TEMPLATE_FIELD, operation_is_active, get_default_operation, operation_ConvertError, operation_string, operation_para_list, operation_isFullValid, operation_testFullError, clean_empty_operation
from calibre_plugins.mass_search_replace.common_utils import (NoWheelComboBox, CheckableTableWidgetItem, TextIconWidgetItem, KeyboardConfigDialog, ReadOnlyTextIconWidgetItem, ReadOnlyTableWidgetItem, KeyValueComboBox,
get_icon, debug_print)
class ICON:
PLUGIN = 'images/plugin.png'
ADD_IMAGE = 'images/image_add.png'
EXPORT = 'images/export.png'
IMPORT = 'images/import.png'
WARNING = 'images/warning.png'
ALL = [
PLUGIN,
ADD_IMAGE,
EXPORT,
IMPORT,
WARNING,
]
class KEY_MENU:
MENU = 'Menu'
ACTIVE = 'Active'
IMAGE = 'Image'
TEXT = 'Text'
SUBMENU = 'SubMenu'
OPERATIONS = 'Operations'
ALL = [
ACTIVE,
TEXT,
SUBMENU,
IMAGE,
OPERATIONS,
]
QUICK = 'Quick'
UPDATE_REPORT = 'UpdateReport'
class KEY_ERROR:
ERROR = 'ErrorStrategy'
UPDATE = 'Update'
OPERATION = 'Operation'
class ERROR_UPDATE:
INTERRUPT = 'interrupt'
INTERRUPT_NAME = _('Interrupt execution')
INTERRUPT_DESC = _('Stop Mass Search/Replace and display the error normally without further action.')
RESTORE = 'restore'
RESTORE_NAME = _('Restore the library')
RESTORE_DESC = _('Stop Mass Search/Replace and restore the library to its original state.')
safely_txt = _('Updates the fields one by one. This operation can be slower than other strategies.')
SAFELY = 'safely stop'
SAFELY_NAME = _('Carefully executed (slower)')
SAFELY_DESC = safely_txt+'\n'+ _('When a error occurs, stop Mass Search/Replace and display the error normally without further action.')
DONT_STOP = 'don\'t stop'
DONT_STOP_NAME = _('Don\'t stop (slower, not recomanded)')
DONT_STOP_DESC = safely_txt+'\n'+_('Update the library, no matter how many errors are encountered. The problematics fields will not be updated.')
LIST = {
INTERRUPT: [INTERRUPT_NAME, INTERRUPT_DESC],
RESTORE: [RESTORE_NAME, RESTORE_DESC],
SAFELY: [SAFELY_NAME, SAFELY_DESC],
DONT_STOP: [DONT_STOP_NAME, DONT_STOP_DESC],
}
DEFAULT = INTERRUPT
class ERROR_OPERATION:
ABORT = 'abort'
ABORT_NAME = _('Abbort')
ABORT_DESC = _('If an invalid operation is detected, abort the changes.')
ASK = 'ask'
ASK_NAME = _('Asked')
ASK_DESC = _('If an invalid operation is detected, asked whether to continue or abort the changes.')
HIDE = 'hide'
HIDE_NAME = _('Hidden')
HIDE_DESC = _('Ignore all invalid operations.')
LIST = {
ABORT: [ABORT_NAME, ABORT_DESC],
ASK: [ASK_NAME, ASK_DESC],
HIDE: [HIDE_NAME, HIDE_DESC],
}
DEFAULT = ASK
# This is where all preferences for this plugin are stored
PREFS = JSONConfig('plugins/Mass Search-Replace')
# Set defaults
PREFS.defaults[KEY_MENU.MENU] = []
PREFS.defaults[KEY_MENU.QUICK] = []
PREFS.defaults[KEY_MENU.UPDATE_REPORT] = False
PREFS.defaults[KEY_ERROR.ERROR] = {
KEY_ERROR.OPERATION : ERROR_UPDATE.DEFAULT,
KEY_ERROR.UPDATE : ERROR_OPERATION.DEFAULT
}
OWIP = 'owip'
def get_default_menu():
menu = {}
menu[KEY_MENU.ACTIVE] = True
menu[KEY_MENU.TEXT] = ''
menu[KEY_MENU.SUBMENU] = ''
menu[KEY_MENU.IMAGE] = ''
menu[KEY_MENU.OPERATIONS] = []
return menu
class ConfigWidget(QWidget):
def __init__(self, plugin_action):
QWidget.__init__(self)
self.plugin_action = plugin_action
layout = QVBoxLayout(self)
self.setLayout(layout)
menu_list = PREFS[KEY_MENU.MENU]
heading_layout = QHBoxLayout()
layout.addLayout(heading_layout)
heading_label = QLabel(_('Select and configure the menu items to display:'), self)
heading_layout.addWidget(heading_label)
# Add a horizontal layout containing the table and the buttons next to it
table_layout = QHBoxLayout()
layout.addLayout(table_layout)
# Create a table the user can edit the menu list
self._table = MenuTableWidget(plugin_action, menu_list, self)
heading_label.setBuddy(self._table)
table_layout.addWidget(self._table)
# Add a vertical layout containing the the buttons to move up/down etc.
button_layout = QtGui.QVBoxLayout()
table_layout.addLayout(button_layout)
move_up_button = QtGui.QToolButton(self)
move_up_button.setToolTip(_('Move menu item up'))
move_up_button.setIcon(get_icon('arrow-up.png'))
button_layout.addWidget(move_up_button)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem)
add_button = QtGui.QToolButton(self)
add_button.setToolTip(_('Add menu item'))
add_button.setIcon(get_icon('plus.png'))
button_layout.addWidget(add_button)
spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem1)
copy_button = QtGui.QToolButton(self)
copy_button.setToolTip(_('Copy menu item'))
copy_button.setIcon(get_icon('edit-copy.png'))
button_layout.addWidget(copy_button)
spacerItem2 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem2)
delete_button = QtGui.QToolButton(self)
delete_button.setToolTip(_('Delete menu item'))
delete_button.setIcon(get_icon('minus.png'))
button_layout.addWidget(delete_button)
spacerItem3 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem3)
move_down_button = QtGui.QToolButton(self)
move_down_button.setToolTip(_('Move menu item down'))
move_down_button.setIcon(get_icon('arrow-down.png'))
button_layout.addWidget(move_down_button)
move_up_button.clicked.connect(self._table.move_rows_up)
move_down_button.clicked.connect(self._table.move_rows_down)
add_button.clicked.connect(self._table.add_row)
delete_button.clicked.connect(self._table.delete_rows)
copy_button.clicked.connect(self._table.copy_row)
# Define a context menu for the table widget
self.create_context_menu(self._table)
# --- Keyboard shortcuts ---
keyboard_layout = QHBoxLayout()
layout.addLayout(keyboard_layout)
keyboard_shortcuts_button = QPushButton(_('Keyboard shortcuts...'), self)
keyboard_shortcuts_button.setToolTip(_('Edit the keyboard shortcuts associated with this plugin'))
keyboard_shortcuts_button.clicked.connect(self.edit_shortcuts)
keyboard_layout.addWidget(keyboard_shortcuts_button)
keyboard_layout.insertStretch(-1)
self.updateReport = QCheckBox(_('Display a update report'), self)
if PREFS[KEY_MENU.UPDATE_REPORT]:
self.updateReport.setCheckState(Qt.Checked)
else:
self.updateReport.setCheckState(Qt.Unchecked)
keyboard_layout.addWidget(self.updateReport)
error_button = QPushButton(_('Error strategy...'), self)
error_button.setToolTip(_('Define the strategy when a error occurs during the library update'))
error_button.clicked.connect(self.edit_error_strategy)
keyboard_layout.addWidget(error_button)
def save_settings(self):
PREFS[KEY_MENU.MENU] = self._table.get_menu_list()
PREFS[KEY_MENU.UPDATE_REPORT] = self.updateReport.checkState() == Qt.Checked
#debug_print('Save settings:\n{0}\n'.format(PREFS))
def edit_shortcuts(self):
self.save_settings()
self.plugin_action.rebuild_menus()
d = KeyboardConfigDialog(self.plugin_action.gui, self.plugin_action.action_spec[0])
if d.exec_() == d.Accepted:
self.plugin_action.gui.keyboard.finalize()
def edit_error_strategy(self):
d = ErrorStrategyDialog(self.plugin_action.gui, self.plugin_action)
if d.exec_() == d.Accepted:
PREFS[KEY_ERROR.ERROR] = {
KEY_ERROR.OPERATION : d.error_operation,
KEY_ERROR.UPDATE : d.error_update
}
debug_print('Error Strategy settings: {0}\n'.format(PREFS[KEY_ERROR.ERROR]))
def create_context_menu(self, table):
table.setContextMenuPolicy(Qt.ActionsContextMenu)
act_add_image = QAction(get_icon(ICON.ADD_IMAGE), _('&Add image...'), table)
act_add_image.triggered.connect(table.display_add_new_image_dialog)
table.addAction(act_add_image)
act_open = QAction(get_icon('document_open.png'), _('&Open images folder'), table)
act_open.triggered.connect(partial(self.open_images_folder, table.resources_dir))
table.addAction(act_open)
sep2 = QAction(table)
sep2.setSeparator(True)
table.addAction(sep2)
act_import = QAction(get_icon(ICON.IMPORT), _('&Import...'), table)
act_import.triggered.connect(self.import_menus)
table.addAction(act_import)
act_export = QAction(get_icon(ICON.EXPORT), _('&Export...'), table)
act_export.triggered.connect(self.export_menus)
table.addAction(act_export)
def open_images_folder(self, path):
if not os.path.exists(path):
os.makedirs(path)
open_local_file(path)
def import_menus(self):
table = self._table
archive_path = self.pick_archive_to_import()
if not archive_path:
return
json = os.path.join(table.resources_dir, OWIP)
json_path = os.path.join(table.resources_dir, OWIP+'.json')
# Write the whole file contents into the resources\images directory
if not os.path.exists(table.resources_dir):
os.makedirs(table.resources_dir)
with ZipFile(archive_path, 'r') as zf:
contents = zf.namelist()
if os.path.basename(json_path) not in contents:
return error_dialog(self, _('Import failed'), _('This is not a valid OWIP export archive'), show=True)
for resource in contents:
fs = os.path.join(table.resources_dir,resource)
with open(fs,'wb') as f:
f.write(zf.read(resource))
try:
# Read the .JSON file to add to the menus then delete it.
import_config = JSONConfig(json)
menu_list = import_config[KEY_MENU.MENU]
# Now insert the menus into the table
table.append_menu_list(menu_list)
info_dialog(self, _('Import completed'), _('{:d} menu items imported').format(len(menu_list)),
show=True, show_copy_button=False)
except Exception as e:
return error_dialog(self, _('Import failed'), e, show=True)
finally:
if os.path.exists(json_path):
os.remove(json_path)
def pick_archive_to_import(self):
archives = choose_files(self, 'owp archive dialog', _('Select a menu file archive to import...'),
filters=[('OWIP Files', ['owip','owip.zip']),('ZIP Files', ['owip','zip'])], all_files=False, select_only_single_file=True)
if not archives:
return
f = archives[0]
return f
def export_menus(self):
table = self._table
menu_list = table.get_selected_menu()
if len(menu_list) == 0:
return error_dialog(self, _('Export failed'), _('No menu items selected to export'), show=True)
archive_path = self.pick_archive_to_export()
if not archive_path:
return
# Build our unique list of images that need to be exported
image_names = {}
for menu in menu_list:
image_name = menu[KEY_MENU.IMAGE]
if image_name and image_name not in image_names:
image_path = os.path.join(table.resources_dir, image_name)
if os.path.exists(image_path):
image_names[image_name] = image_path
# Write our menu items out to a json file
if not os.path.exists(table.resources_dir):
os.makedirs(table.resources_dir)
json = os.path.join(table.resources_dir, OWIP)
export_config = JSONConfig(json)
export_config[KEY_MENU.MENU] = menu_list
json_path = os.path.join(table.resources_dir, OWIP+'.json')
try:
# Create the zip file archive
with ZipFile(archive_path, 'w') as archive_zip:
archive_zip.write(json_path, os.path.basename(json_path))
# Add any images referred to in those menu items that are local resources
for image_name, image_path in list(image_names.items()):
archive_zip.write(image_path, os.path.basename(image_path))
info_dialog(self, _('Export completed'), _('{:d} menu items exported to\n{:s}').format(len(menu_list), archive_path),
show=True, show_copy_button=False)
except Exception as e:
return error_dialog(self, _('Export failed'), e, show=True)
finally:
if os.path.exists(json_path):
os.remove(json_path)
def pick_archive_to_export(self):
fd = FileDialog(name='owp archive dialog', title=_('Save menu archive as...'), filters=[('OWIP Files', ['owip.zip']),('ZIP Files', ['zip'])],
parent=self, add_all_files_filter=False, mode=QFileDialog.AnyFile)
fd.setParent(None)
if not fd.accepted:
return None
return fd.get_files()[0]
COMBO_IMAGE_ADD = _('Add New Image...')
def get_image_names(image_map):
image_names = sorted(image_map.keys())
# Add a blank item at the beginning of the list, and a blank then special 'Add" item at end
image_names.insert(0, '')
image_names.append('')
image_names.append(COMBO_IMAGE_ADD)
return image_names
COL_NAMES = ['', _('Name'), _('Submenu'), _('Image'), _('Operation')]
class MenuTableWidget(QTableWidget):
def __init__(self, plugin_action, menu_list=None, *args):
QTableWidget.__init__(self, *args)
self.plugin_action = plugin_action
self.gui = plugin_action.gui
self.resources_dir = os.path.join(config_dir, 'resources/images')
if iswindows:
self.resources_dir = os.path.normpath(self.resources_dir)
self.image_map = self.get_image_map()
self.setAlternatingRowColors(True)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSortingEnabled(False)
self.setMinimumSize(600, 0)
self.populate_table(menu_list)
self.cellChanged.connect(self.cell_changed)
def populate_table(self, menu_list=None):
self.clear()
self.setColumnCount(len(COL_NAMES))
self.setHorizontalHeaderLabels(COL_NAMES)
self.verticalHeader().setDefaultSectionSize(24)
if menu_list == None: menu_list = []
self.setRowCount(len(menu_list))
for row, menu in enumerate(menu_list, 0):
self.populate_table_row(row, menu)
self.selectRow(0)
def populate_table_row(self, row, menu):
self.blockSignals(True)
icon_name = menu[KEY_MENU.IMAGE]
menu_text = menu[KEY_MENU.TEXT]
self.setItem(row, 0, CheckableTableWidgetItem(menu[KEY_MENU.ACTIVE]))
self.setItem(row, 1, TextIconWidgetItem(menu_text, get_icon(icon_name)))
self.setItem(row, 2, QTableWidgetItem(menu[KEY_MENU.SUBMENU]))
if menu_text:
self.set_editable_cells_in_row(row, image=icon_name, menu=menu)
else:
# Make all the later column cells non-editable
self.set_noneditable_cells_in_row(row)
self.resizeColumnsToContents()
self.blockSignals(False)
def cell_changed(self, row, col):
self.blockSignals(True)
if col == 1 or col == 2:
menu_text = unicode(self.item(row, col).text()).strip()
self.item(row, col).setText(menu_text)
if unicode(self.item(row, 1).text()):
# Make sure that the other columns in this row are enabled if not already.
if not self.cellWidget(row, len(COL_NAMES)-1):
self.set_editable_cells_in_row(row)
self.cellWidget(row, 4).setMenu(self.convert_row_to_menu(row))
else:
# Blank menu text so treat it as a separator row
self.set_noneditable_cells_in_row(row)
self.resizeColumnsToContents()
self.blockSignals(False)
def image_combo_index_changed(self, combo, row):
if combo.currentText() == COMBO_IMAGE_ADD:
# Special item in the combo for choosing a new image to add to Calibre
self.display_add_new_image_dialog(select_in_combo=True, combo=combo)
# Regardless of new or existing item, update image on the title column
title_item = self.item(row, 1)
title_item.setIcon(combo.itemIcon(combo.currentIndex()))
# Store the current index as item data in index 0 in case user cancels dialog in future
combo.setItemData(0, combo.currentIndex())
def set_editable_cells_in_row(self, row, image='', menu=None):
image_combo = ImageComboBox(self, self.image_map, image)
image_combo.currentIndexChanged.connect(partial(self.image_combo_index_changed, image_combo, row))
self.setCellWidget(row, 3, image_combo)
if menu==None: menu = self.create_blank_row_menu()
self.setCellWidget(row, 4, SettingsButton(self.plugin_action, menu))
def set_noneditable_cells_in_row(self, row):
for col in range(3, len(COL_NAMES)):
if self.cellWidget(row, col):
self.removeCellWidget(row, col)
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsSelectable|Qt.ItemIsEnabled)
self.setItem(row, col, item)
self.item(row, 1).setIcon(get_icon())
def display_add_new_image_dialog(self, select_in_combo=False, combo=None):
add_image_dialog = ImageDialog(self, self.resources_dir, get_image_names(self.image_map))
add_image_dialog.exec_()
if add_image_dialog.result() == QDialog.Rejected:
# User cancelled the add operation or an error - set to previous value
if select_in_combo and combo:
prevIndex = combo.itemData(0)
combo.blockSignals(True)
combo.setCurrentIndex(prevIndex)
combo.blockSignals(False)
return
# User has added a new image so we need to repopulate every combo with new sorted list
self.image_map[add_image_dialog.image_name] = get_icon(add_image_dialog.image_name)
for update_row in range(self.rowCount()):
cellCombo = self.cellWidget(update_row, 3)
if cellCombo:
cellCombo.blockSignals(True)
cellCombo.populate_combo(self.image_map, cellCombo.currentText())
cellCombo.blockSignals(False)
# Now select the newly added item in this row if required
if select_in_combo and combo:
idx = combo.findText(add_image_dialog.image_name)
combo.blockSignals(True)
combo.setCurrentIndex(idx)
combo.blockSignals(False)
def add_row(self):
self.setFocus()
# We will insert a blank row below the currently selected row
row = self.currentRow() + 1
self.insertRow(row)
self.populate_table_row(row, self.create_blank_row_menu())
self.select_and_scroll_to_row(row)
def copy_row(self):
self.setFocus()
currentRow = self.currentRow()
if currentRow < 0:
return
menu = self.convert_row_to_menu(currentRow)
menu[KEY_MENU.TEXT] += ' ' + _('(copy)')
# We will insert a blank row below the currently selected row
row = self.currentRow() + 1
self.insertRow(row)
self.populate_table_row(row, menu)
self.select_and_scroll_to_row(row)
self.resizeColumnsToContents()
def delete_rows(self):
self.setFocus()
rows = self.selectionModel().selectedRows()
if len(rows) == 0:
return
message = _('Are you sure you want to delete this menu item?')
if len(rows) > 1:
message = _('Are you sure you want to delete the selected {:d} menu items?').format(len(rows))
if not question_dialog(self, _('Are you sure?'), message, show_copy_button=False):
return
first_sel_row = self.currentRow()
for selrow in reversed(rows):
self.removeRow(selrow.row())
if first_sel_row < self.rowCount():
self.select_and_scroll_to_row(first_sel_row)
elif self.rowCount() > 0:
self.select_and_scroll_to_row(first_sel_row - 1)
def move_rows_up(self):
self.setFocus()
rows = self.selectionModel().selectedRows()
if len(rows) == 0:
return
first_sel_row = rows[0].row()
if first_sel_row <= 0:
return
for selrow in rows:
self.swap_row_widgets(selrow.row() - 1, selrow.row() + 1)
scroll_to_row = first_sel_row - 1
if scroll_to_row > 0:
scroll_to_row = scroll_to_row - 1
self.scrollToItem(self.item(scroll_to_row, 0))
def move_rows_down(self):
self.setFocus()
rows = self.selectionModel().selectedRows()
if len(rows) == 0:
return
last_sel_row = rows[-1].row()
if last_sel_row == self.rowCount() - 1:
return
for selrow in reversed(rows):
self.swap_row_widgets(selrow.row() + 2, selrow.row())
scroll_to_row = last_sel_row + 1
if scroll_to_row < self.rowCount() - 1:
scroll_to_row = scroll_to_row + 1
self.scrollToItem(self.item(scroll_to_row, 0))
def swap_row_widgets(self, src_row, dest_row):
self.blockSignals(True)
self.insertRow(dest_row)
for col in range(0,3):
self.setItem(dest_row, col, self.takeItem(src_row, col))
menu_text = unicode(self.item(dest_row, 1).text()).strip()
if menu_text:
for col in range(3, len(COL_NAMES)):
if col == 3:
# Image column has a combobox we have to recreate as cannot move widget (Qt crap)
icon_name = self.cellWidget(src_row, col).currentText()
image_combo = ImageComboBox(self, self.image_map, icon_name)
image_combo.currentIndexChanged.connect(partial(self.image_combo_index_changed, image_combo, dest_row))
self.setCellWidget(dest_row, col, image_combo)
elif col == 4:
self.setCellWidget(dest_row, col, self.cellWidget(src_row, col))
else:
# This is a separator row
self.set_noneditable_cells_in_row(dest_row)
self.removeRow(src_row)
self.blockSignals(False)
def select_and_scroll_to_row(self, row):
self.selectRow(row)
self.scrollToItem(self.currentItem())
def get_image_map(self):
image_map = {}
if os.path.exists(self.resources_dir):
# Get the names of any .png images in this directory
for f in os.listdir(self.resources_dir):
if f.lower().endswith('.png'):
image_name = os.path.basename(f)
image_map[image_name] = get_icon(image_name)
return image_map
def create_blank_row_menu(self):
return get_default_menu()
def get_menu_list(self):
menu_list = []
for row in range(self.rowCount()):
menu_list.append(self.convert_row_to_menu(row))
# Remove any blank separator row items from at the start and the end
while len(menu_list) > 0 and not menu_list[-1][KEY_MENU.TEXT]:
menu_list.pop()
while len(menu_list) > 0 and not menu_list[0][KEY_MENU.TEXT]:
menu_list.pop(0)
return menu_list
def convert_row_to_menu(self, row):
menu = self.create_blank_row_menu()
menu[KEY_MENU.ACTIVE] = self.item(row, 0).checkState() == Qt.Checked
menu[KEY_MENU.TEXT] = unicode(self.item(row, 1).text()).strip()
menu[KEY_MENU.SUBMENU] = unicode(self.item(row, 2).text()).strip()
if menu[KEY_MENU.TEXT]:
menu[KEY_MENU.IMAGE] = unicode(self.cellWidget(row, 3).currentText()).strip()
menu[KEY_MENU.OPERATIONS] = self.cellWidget(row, 4).getOperationList()
return menu
def get_selected_menu(self):
menu_list = []
for row in self.selectionModel().selectedRows():
menu_list.append(self.convert_row_to_menu(row.row()))
return menu_list
def append_menu_list(self, menu_list):
for menu in reversed(menu_list):
row = self.currentRow() + 1
self.insertRow(row)
self.populate_table_row(row, menu)
class ImageComboBox(NoWheelComboBox):
def __init__(self, parent, image_map, selected_text):
NoWheelComboBox.__init__(self, parent)
self.populate_combo(image_map, selected_text)
def populate_combo(self, image_map, selected_text):
self.clear()
for i, image in enumerate(get_image_names(image_map), 0):
self.insertItem(i, image_map.get(image, image), image)
idx = self.findText(selected_text)
self.setCurrentIndex(idx)
self.setItemData(0, idx)
class SettingsButton(QToolButton):
def __init__(self, plugin_action, menu):
QToolButton.__init__(self)
self.plugin_action = plugin_action
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.clicked.connect(self._clicked)
self._menu = menu
self._initial_menu = copy.deepcopy(menu)
self.setMenu(menu)
def setMenu(self, menu):
self._menu = menu
self.updateText()
self.hasError()
def getMenu(self):
return copy.copy(self._menu)
def updateText(self):
count = len(self.getOperationList())
active = 0
for operation in self.getOperationList():
if operation_is_active(operation):
active += 1
txt = ''
if active < count:
txt = _('{:d}/{:d} operations').format(active, count)
else:
txt = _('{:d} operations').format(count)
if self.getHasChanged():
txt+='*'
self.setText(txt)
def hasError(self):
hasError = False
for operation in self.getOperationList():
if not operation_isFullValid(operation, self.plugin_action):
hasError = True
break
if hasError:
self.setIcon(get_icon(ICON.WARNING))
self.setToolTip(_('This operations list contain a error'))
else:
self.setIcon(get_icon('gear.png'))
self.setToolTip(_('Edit the operations list'))
return hasError
def getHasChanged(self):
op_lst = self.getOperationList()
initial_op_lst = self._initial_menu[KEY_MENU.OPERATIONS]
if len(op_lst) != len(initial_op_lst):
return True
for i in range(0, len(op_lst)):
if operation_is_active(op_lst[i]) != operation_is_active(initial_op_lst[i]):
return True
for key in KEY_OPERATION.ALL:
if op_lst[i][key] != initial_op_lst[i][key]:
return True
return False
def setOperationList(self, operation_list):
self._menu[KEY_MENU.OPERATIONS] = operation_list
self.setMenu(self._menu)
def getOperationList(self):
return copy.copy(self._menu[KEY_MENU.OPERATIONS])
def _clicked(self):
d = ConfigOperationListDialog(self, self.plugin_action, menu=self.getMenu())
if d.exec_() == d.Accepted:
self.setOperationList(d.operation_list)
class ImageDialog(QDialog):
def __init__(self, parent=None, resources_dir='', image_names=[]):
QDialog.__init__(self, parent)
self.resources_dir = resources_dir
self.image_names = image_names
self.setWindowTitle(_('Add New Image'))
v = QVBoxLayout(self)
group_box = QGroupBox(_('&Select image source'), self)
v.addWidget(group_box)
grid = QGridLayout()
self._radio_web = QRadioButton(_('From &web domain favicon'), self)
self._radio_web.setChecked(True)
self._web_domain_edit = QLineEdit(self)
self._radio_web.setFocusProxy(self._web_domain_edit)
grid.addWidget(self._radio_web, 0, 0)
grid.addWidget(self._web_domain_edit, 0, 1)
grid.addWidget(QLabel('e.g. www.amazon.com'), 0, 2)
self._radio_file = QRadioButton(_('From .png &file'), self)
self._input_file_edit = QLineEdit(self)
self._input_file_edit.setMinimumSize(200, 0)
self._radio_file.setFocusProxy(self._input_file_edit)
pick_button = QPushButton('...', self)
pick_button.setMaximumSize(24, 20)
pick_button.clicked.connect(self.pick_file_to_import)
grid.addWidget(self._radio_file, 1, 0)
grid.addWidget(self._input_file_edit, 1, 1)
grid.addWidget(pick_button, 1, 2)
group_box.setLayout(grid)
save_layout = QHBoxLayout()
lbl_filename = QLabel(_('&Save as filename:'), self)
lbl_filename.setMinimumSize(155, 0)
self._save_as_edit = QLineEdit('', self)
self._save_as_edit.setMinimumSize(200, 0)
lbl_filename.setBuddy(self._save_as_edit)
lbl_ext = QLabel('.png', self)
save_layout.addWidget(lbl_filename, 0, Qt.AlignLeft)
save_layout.addWidget(self._save_as_edit, 0, Qt.AlignLeft)
save_layout.addWidget(lbl_ext, 1, Qt.AlignLeft)
v.addLayout(save_layout)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.ok_clicked)
button_box.rejected.connect(self.reject)
v.addWidget(button_box)
self.resize(self.sizeHint())
self._web_domain_edit.setFocus()
self.new_image_name = None
@property
def image_name(self):
return self.new_image_name
def pick_file_to_import(self):
images = choose_files(None, _('menu icon dialog'), _('Select a .png file for the menu icon'),
filters=[('PNG Image Files', ['png'])], all_files=False, select_only_single_file=True)
if not images:
return
f = images[0]
if not f.lower().endswith('.png'):
return error_dialog(self, _('Cannot import image'), _('Source image must be a .png file.'), show=True)
self._input_file_edit.setText(f)
self._save_as_edit.setText(os.path.splitext(os.path.basename(f))[0])
def ok_clicked(self):
# Validate all the inputs
save_name = unicode(self._save_as_edit.text()).strip()
if not save_name:
return error_dialog(self, _('Cannot import image'), _('You must specify a filename to save as.'), show=True)
self.new_image_name = os.path.splitext(save_name)[0] + '.png'
if save_name.find('\\') > -1 or save_name.find('/') > -1:
return error_dialog(self, _('Cannot import image'), _('The save as filename should consist of a filename only.'), show=True)
if not os.path.exists(self.resources_dir):
os.makedirs(self.resources_dir)
dest_path = os.path.join(self.resources_dir, self.new_image_name)
if save_name in self.image_names or os.path.exists(dest_path):
if not question_dialog(self, _('Are you sure?'), _('An image with this name already exists - overwrite it?'), show_copy_button=False):
return
if self._radio_web.isChecked():
domain = unicode(self._web_domain_edit.text()).strip()
if not domain:
return error_dialog(self, _('Cannot import image'), _('You must specify a web domain url'), show=True)
url = 'http://www.google.com/s2/favicons?domain=' + domain
urlretrieve(url, dest_path)
return self.accept()
else:
source_file_path = unicode(self._input_file_edit.text()).strip()
if not source_file_path:
return error_dialog(self, _('Cannot import image'), _('You must specify a source file.'), show=True)
if not source_file_path.lower().endswith('.png'):
return error_dialog(self, _('Cannot import image'), _('Source image must be a .png file.'), show=True)
if not os.path.exists(source_file_path):
return error_dialog(self, _('Cannot import image'), _('Source image does not exist!'), show=True)
shutil.copyfile(source_file_path, dest_path)
return self.accept()
class OperationWidgetItem(QTableWidgetItem):
def __init__(self, plugin_action, operation):
QTableWidgetItem.__init__(self, '')
self.setFlags(Qt.ItemFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled ))
self.plugin_action = plugin_action
self._operation = operation
self._hasError = False
self.setOperation(operation)
def setOperation(self, operation):
if not operation: operation = get_default_operation()
self._operation = operation
checked = operation_is_active(self._operation)
if checked:
self.setCheckState(Qt.Checked)
else:
self.setCheckState(Qt.Unchecked)
self.hasError()
def getOperation(self):
self._operation[KEY_OPERATION.ACTIVE] = Qt.Checked == self.checkState()
self._operation = operation_ConvertError(self._operation)
return copy.copy(self._operation)
def hasError(self):
err = operation_testFullError(self._operation, self.plugin_action)
if err:
self.setIcon(get_icon(ICON.WARNING))
self.setToolTip(str(err))
return True
else:
self.setIcon(get_icon())
self.setToolTip('')
return False
COL_CONFIG = ['', _('Columns'), _('Template'), _('Search mode'), _('Search'), _('Replace')]
class ConfigOperationListDialog(Dialog):
def __init__(self, parent, plugin_action, menu):
self.plugin_action = plugin_action
if not menu: menu = get_default_menu()
name = menu[KEY_MENU.TEXT]
sub_menu = menu[KEY_MENU.SUBMENU]
self.operation_list = menu[KEY_MENU.OPERATIONS]
title = ''
if not name:
title = _('List of operations for a quick Search/Replaces')
else:
if sub_menu:
name = '{:s} > {:s}'.format(sub_menu, name)
else:
name = '{:s}'.format(name)
title = _('List of Search/Replace operations for {:s}').format(name)
Dialog.__init__(self, title, 'config_list_SearchReplace', parent)
def setup_ui(self):
layout = QVBoxLayout(self)
self.setLayout(layout)
heading_layout = QHBoxLayout()
layout.addLayout(heading_layout)
heading_label = QLabel(_('Select and configure the order of execution of the operations of Search/Replace operations:'), self)
heading_layout.addWidget(heading_label)
#help_label = QLabel(' ', self)
#help_label.setTextInteractionFlags(Qt.LinksAccessibleByMouse | Qt.LinksAccessibleByKeyboard)
#help_label.setAlignment(Qt.AlignRight)
#heading_layout.addWidget(help_label)
# Add a horizontal layout containing the table and the buttons next to it
table_layout = QHBoxLayout()
layout.addLayout(table_layout)
# Create a table the user can edit the operation list
self._table = OperationListTableWidget(self.plugin_action, self.operation_list, self)
heading_label.setBuddy(self._table)
table_layout.addWidget(self._table)
# Add a vertical layout containing the the buttons to move up/down etc.
button_layout = QtGui.QVBoxLayout()
table_layout.addLayout(button_layout)
move_up_button = QtGui.QToolButton(self)
move_up_button.setToolTip(_('Move operation up'))
move_up_button.setIcon(get_icon('arrow-up.png'))
button_layout.addWidget(move_up_button)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem)
add_button = QtGui.QToolButton(self)
add_button.setToolTip(_('Add operation'))
add_button.setIcon(get_icon('plus.png'))
button_layout.addWidget(add_button)
spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem1)
copy_button = QtGui.QToolButton(self)
copy_button.setToolTip(_('Copy operation'))
copy_button.setIcon(get_icon('edit-copy.png'))
button_layout.addWidget(copy_button)
spacerItem2 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem2)
delete_button = QtGui.QToolButton(self)
delete_button.setToolTip(_('Delete operation'))
delete_button.setIcon(get_icon('minus.png'))
button_layout.addWidget(delete_button)
spacerItem3 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
button_layout.addItem(spacerItem3)
move_down_button = QtGui.QToolButton(self)
move_down_button.setToolTip(_('Move operation down'))
move_down_button.setIcon(get_icon('arrow-down.png'))
button_layout.addWidget(move_down_button)