|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select a list of patch files. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from E5Gui import E5FileDialog |
|
14 |
|
15 from .Ui_GitPatchFilesDialog import Ui_GitPatchFilesDialog |
|
16 |
|
17 import UI.PixmapCache |
|
18 import Utilities |
|
19 |
|
20 |
|
21 class GitPatchFilesDialog(QDialog, Ui_GitPatchFilesDialog): |
|
22 """ |
|
23 Class implementing a dialog to select a list of patch files. |
|
24 """ |
|
25 def __init__(self, rootDir, patchCheckData, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param rootDir root of the directory tree (string) |
|
30 @param patchCheckData tuple of data as returned by the |
|
31 getData() method |
|
32 @param parent reference to the parent widget (QWidget) |
|
33 """ |
|
34 super().__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 self.__rootDir = rootDir |
|
38 if patchCheckData is not None: |
|
39 self.patchFilesList.addItems(patchCheckData[0]) |
|
40 self.stripSpinBox.setValue(patchCheckData[1]) |
|
41 self.eofCheckBox.setChecked(patchCheckData[2]) |
|
42 self.lineCountsCheckBox.setChecked(patchCheckData[3]) |
|
43 |
|
44 self.addButton.setIcon(UI.PixmapCache.getIcon("plus")) |
|
45 self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus")) |
|
46 self.upButton.setIcon(UI.PixmapCache.getIcon("1uparrow")) |
|
47 self.downButton.setIcon(UI.PixmapCache.getIcon("1downarrow")) |
|
48 |
|
49 self.__okButton = self.buttonBox.button( |
|
50 QDialogButtonBox.StandardButton.Ok) |
|
51 self.__okButton.setEnabled(len(self.__getPatchFilesList()) > 0) |
|
52 |
|
53 self.deleteButton.setEnabled(False) |
|
54 self.upButton.setEnabled(False) |
|
55 self.downButton.setEnabled(False) |
|
56 |
|
57 @pyqtSlot() |
|
58 def on_patchFilesList_itemSelectionChanged(self): |
|
59 """ |
|
60 Private slot to enable button states depending on selection. |
|
61 """ |
|
62 selectedItems = self.patchFilesList.selectedItems() |
|
63 count = len(selectedItems) |
|
64 isFirst = ( |
|
65 count == 1 and |
|
66 self.patchFilesList.row(selectedItems[0]) == 0 |
|
67 ) |
|
68 isLast = ( |
|
69 count == 1 and |
|
70 self.patchFilesList.row(selectedItems[0]) == |
|
71 self.patchFilesList.count() - 1 |
|
72 ) |
|
73 self.deleteButton.setEnabled(count > 0) |
|
74 self.upButton.setEnabled(count == 1 and not isFirst) |
|
75 self.downButton.setEnabled(count == 1 and not isLast) |
|
76 |
|
77 @pyqtSlot() |
|
78 def on_addButton_clicked(self): |
|
79 """ |
|
80 Private slot to add patch files to the list. |
|
81 """ |
|
82 patchFiles = E5FileDialog.getOpenFileNames( |
|
83 self, |
|
84 self.tr("Patch Files"), |
|
85 self.__rootDir, |
|
86 self.tr("Patch Files (*.diff *.patch);;All Files (*)")) |
|
87 if patchFiles: |
|
88 currentPatchFiles = self.__getPatchFilesList() |
|
89 for patchFile in patchFiles: |
|
90 patchFile = Utilities.toNativeSeparators(patchFile) |
|
91 if patchFile not in currentPatchFiles: |
|
92 self.patchFilesList.addItem(patchFile) |
|
93 |
|
94 self.__okButton.setEnabled(len(self.__getPatchFilesList()) > 0) |
|
95 self.on_patchFilesList_itemSelectionChanged() |
|
96 |
|
97 @pyqtSlot() |
|
98 def on_deleteButton_clicked(self): |
|
99 """ |
|
100 Private slot to delete the selected patch files. |
|
101 """ |
|
102 for itm in self.patchFilesList.selectedItems(): |
|
103 row = self.patchFilesList.row(itm) |
|
104 self.patchFilesList.takeItem(row) |
|
105 del itm |
|
106 |
|
107 self.__okButton.setEnabled(len(self.__getPatchFilesList()) > 0) |
|
108 self.on_patchFilesList_itemSelectionChanged() |
|
109 |
|
110 @pyqtSlot() |
|
111 def on_upButton_clicked(self): |
|
112 """ |
|
113 Private slot to move an entry up in the list. |
|
114 """ |
|
115 row = self.patchFilesList.row(self.patchFilesList.selectedItems()[0]) |
|
116 itm = self.patchFilesList.takeItem(row) |
|
117 self.patchFilesList.insertItem(row - 1, itm) |
|
118 itm.setSelected(True) |
|
119 |
|
120 @pyqtSlot() |
|
121 def on_downButton_clicked(self): |
|
122 """ |
|
123 Private slot to move an entry down in the list. |
|
124 """ |
|
125 row = self.patchFilesList.row(self.patchFilesList.selectedItems()[0]) |
|
126 itm = self.patchFilesList.takeItem(row) |
|
127 self.patchFilesList.insertItem(row + 1, itm) |
|
128 itm.setSelected(True) |
|
129 |
|
130 def __getPatchFilesList(self): |
|
131 """ |
|
132 Private method to get the list of patch files. |
|
133 |
|
134 @return list of patch files (list of string) |
|
135 """ |
|
136 patchFiles = [] |
|
137 for row in range(self.patchFilesList.count()): |
|
138 itm = self.patchFilesList.item(row) |
|
139 patchFiles.append(itm.text()) |
|
140 |
|
141 return patchFiles |
|
142 |
|
143 def getData(self): |
|
144 """ |
|
145 Public slot to get the entered data. |
|
146 |
|
147 @return tuple of list of patch files, strip count, flag indicating |
|
148 that the patch has inaccurate end-of-file marker and a flag |
|
149 indicating to not trust the line count information |
|
150 (list of string, integer, boolean, boolean) |
|
151 """ |
|
152 return (self.__getPatchFilesList(), self.stripSpinBox.value(), |
|
153 self.eofCheckBox.isChecked(), |
|
154 self.lineCountsCheckBox.isChecked()) |