ProjectFlask/FlaskMigrateExtension/MigrateSummaryDialog.py

changeset 39
120f30d7b949
parent 36
548dea93941c
child 60
02243723ac17
equal deleted inserted replaced
38:f5055c1e4e07 39:120f30d7b949
8 """ 8 """
9 9
10 from PyQt5.QtCore import pyqtSlot, Qt, QProcess, QEventLoop, QTimer 10 from PyQt5.QtCore import pyqtSlot, Qt, QProcess, QEventLoop, QTimer
11 from PyQt5.QtGui import QGuiApplication 11 from PyQt5.QtGui import QGuiApplication
12 from PyQt5.QtWidgets import ( 12 from PyQt5.QtWidgets import (
13 QDialog, QDialogButtonBox, QAbstractButton, QTreeWidgetItem 13 QDialog, QDialogButtonBox, QAbstractButton, QTreeWidgetItem,
14 QAbstractItemView
14 ) 15 )
15 16
16 from E5Gui import E5MessageBox 17 from E5Gui import E5MessageBox
17 18
18 from .Ui_MigrateSummaryDialog import Ui_MigrateSummaryDialog 19 from .Ui_MigrateSummaryDialog import Ui_MigrateSummaryDialog
43 self.__refreshButton.clicked.connect(self.showSummary) 44 self.__refreshButton.clicked.connect(self.showSummary)
44 45
45 self.__project = project 46 self.__project = project
46 self.__migrateProject = migrateProject 47 self.__migrateProject = migrateProject
47 self.__migrations = migrations 48 self.__migrations = migrations
49
48 self.__process = None 50 self.__process = None
51 self.__currentItemIndex = 1000000
52 self.__currentRevision = ""
49 53
50 def showSummary(self): 54 def showSummary(self):
51 """ 55 """
52 Public method to show the migrations summary. 56 Public method to show the migrations summary.
53 """ 57 """
79 if ok: 83 if ok:
80 ok = self.__process.waitForFinished(10000) 84 ok = self.__process.waitForFinished(10000)
81 if ok: 85 if ok:
82 out = str(self.__process.readAllStandardOutput(), "utf-8") 86 out = str(self.__process.readAllStandardOutput(), "utf-8")
83 self.__processOutput(out) 87 self.__processOutput(out)
88 self.__selectItem(self.__currentRevision)
84 else: 89 else:
85 E5MessageBox.critical( 90 E5MessageBox.critical(
86 None, 91 None,
87 self.tr("Migrations Summary"), 92 self.tr("Migrations Summary"),
88 self.tr("""The Flask process did not finish within""" 93 self.tr("""The Flask process did not finish within"""
109 114
110 @param output output of the flask process 115 @param output output of the flask process
111 @type str 116 @type str
112 """ 117 """
113 self.summaryWidget.clear() 118 self.summaryWidget.clear()
114 self.upgradeButton.setEnabled(False) 119 self.upDownButton.setEnabled(False)
115 self.downgradeButton.setEnabled(False) 120 self.__currentItemIndex = 1000000
121 self.__currentRevision = ""
116 122
117 lines = output.splitlines() 123 lines = output.splitlines()
118 for line in lines: 124 for line in lines:
119 isCurrent = False 125 isCurrent = False
120 oldRev, rest = line.split("->") 126 oldRev, rest = line.split("->")
140 if isCurrent: 146 if isCurrent:
141 font = itm.font(0) 147 font = itm.font(0)
142 font.setBold(True) 148 font.setBold(True)
143 for column in range(self.summaryWidget.columnCount()): 149 for column in range(self.summaryWidget.columnCount()):
144 itm.setFont(column, font) 150 itm.setFont(column, font)
151
152 self.__currentItemIndex = (
153 self.summaryWidget.indexOfTopLevelItem(itm)
154 )
155 self.__currentRevision = newRev.strip()
145 156
146 @pyqtSlot() 157 @pyqtSlot()
147 def on_summaryWidget_itemSelectionChanged(self): 158 def on_summaryWidget_itemSelectionChanged(self):
148 """ 159 """
149 Private slot to handle the selection of an entry. 160 Private slot to handle the selection of an entry.
150 """ 161 """
151 enable = bool(self.summaryWidget.selectedItems()) 162 items = self.summaryWidget.selectedItems()
152 self.upgradeButton.setEnabled(enable) 163 if items:
153 self.downgradeButton.setEnabled(enable) 164 index = self.summaryWidget.indexOfTopLevelItem(items[0])
165 if index < self.__currentItemIndex:
166 self.upDownButton.setText(self.tr("Upgrade"))
167 elif index > self.__currentItemIndex:
168 self.upDownButton.setText(self.tr("Downgrade"))
169 self.upDownButton.setEnabled(index != self.__currentItemIndex)
170 else:
171 self.upDownButton.setEnabled(False)
154 172
155 @pyqtSlot() 173 @pyqtSlot()
156 def on_upgradeButton_clicked(self): 174 def on_upDownButton_clicked(self):
157 """ 175 """
158 Private slot to upgrade to the selected revision. 176 Private slot to upgrade/downgrade to the selected revision.
159 """ 177 """
160 itm = self.summaryWidget.selectedItems()[0] 178 itm = self.summaryWidget.selectedItems()[0]
161 rev = itm.text(1) 179 rev = itm.text(1)
162 self.__migrateProject.upgradeDatabase(revision=rev) 180 if self.upDownButton.text() == self.tr("Upgrade"):
163 self.showSummary() 181 self.__migrateProject.upgradeDatabase(revision=rev)
164 182 else:
165 @pyqtSlot() 183 self.__migrateProject.downgradeDatabase(revision=rev)
166 def on_downgradeButton_clicked(self):
167 """
168 Private slot to downgrade to the selected revision.
169 """
170 itm = self.summaryWidget.selectedItems()[0]
171 rev = itm.text(1)
172 self.__migrateProject.downgradeDatabase(revision=rev)
173 self.showSummary() 184 self.showSummary()
174 185
175 @pyqtSlot(QAbstractButton) 186 @pyqtSlot(QAbstractButton)
176 def on_buttonBox_clicked(self, button): 187 def on_buttonBox_clicked(self, button):
177 """ 188 """
195 self.__process.terminate() 206 self.__process.terminate()
196 QTimer.singleShot(2000, self.__process.kill) 207 QTimer.singleShot(2000, self.__process.kill)
197 self.__process.waitForFinished(3000) 208 self.__process.waitForFinished(3000)
198 209
199 self.__process = None 210 self.__process = None
211
212 def __selectItem(self, revision):
213 """
214 Private method to select an item given its revision.
215
216 @param revision revision of the item to select
217 @type str
218 """
219 if revision:
220 items = self.summaryWidget.findItems(
221 revision, Qt.MatchExactly, 1)
222 if items:
223 # select the first item
224 items[0].setSelected(True)
225 self.summaryWidget.scrollToItem(
226 items[0], QAbstractItemView.PositionAtCenter)

eric ide

mercurial