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