# Coverage for grm\AddFlowControl_dialog.py : 0%

## 277 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# -*- coding: utf-8 -*-

2"""

3/***************************************************************************

4 FlatDialog

5 A QGIS plugin

6 Flat

7 -------------------

8 begin : 2017-04-26

9 git sha : $Format:%H$

10 copyright : (C) 2017 by Hermesys

11 email : shpark@hermesys.co.kr

12 ***************************************************************************/

13

14/***************************************************************************

15 * *

16 * This program is free software; you can redistribute it and/or modify *

17 * it under the terms of the GNU General Public License as published by *

18 * the Free Software Foundation; either version 2 of the License, or *

19 * (at your option) any later version. *

20 * *

21 ***************************************************************************/

22"""

23

24 import os

25

26 from qgis.PyQt import QtWidgets

27 from qgis.PyQt.QtCore import Qt

28 from qgis.PyQt.QtWidgets import QFileDialog

29

30 from grm.FileFormat_dialog import FileFormat

31 from grm.lib.Regular_Expression import floatVerification, retrPdDayVerification

32 from grm.lib.Util import MsInfo

33 from grm.ui.AddFlowControl_dialog_base import Ui_FileFormatDialogBase

34

35

36 class AddFlowControl(QtWidgets.QDialog, Ui_FileFormatDialogBase):

37 def __init__(

38 self,

39 _Project_Path="",

40 _Flowcontrol_Edit_or_Insert_type="",

41 _EditFlowName="",

42 _EditFlowDT="",

43 _EditFlowControlType="",

44 _EditFlowFlowDataFile="",

45 _EditFlowIniStorage="",

46 _EditFlowMaxStorage="",

47 _EditNomalHighStorage="",

48 _EditRestrictedStorage="",

49 _EditRetiPeriodSt="",

50 _EditRetiPeriodEd="",

51 _EditFlowROType="",

52 _EditFlowAutoROMmaxOutflow_CMS="",

53 _EditFlowROConstQ="",

54 _EditFlowROConstQDuration="",

55 _DataTimeFormat=True, # grm_dialog_base.ui simulation 탭에서 set simulation starting time 체크박스 관련

56 parent=None,

57 ):

58 super(AddFlowControl, self).__init__(parent)

59 self.setupUi(self)

60

61 self.project_path = _Project_Path

62 self.datatimeFormat = _DataTimeFormat

63

64 self._AddFlowcontrol_Edit_or_Insert_type = _Flowcontrol_Edit_or_Insert_type

65 self._EditFlowName = _EditFlowName

66 self._EditFlowDT = _EditFlowDT

67 self._EditFlowControlType = _EditFlowControlType

68 self._EditFlowFlowDataFile = _EditFlowFlowDataFile

69 self._EditFlowIniStorage = _EditFlowIniStorage

70 self._EditFlowMaxStorage = _EditFlowMaxStorage

71 self._EditNomalHighStorage = _EditNomalHighStorage

72 self._EditRestrictedStorage = _EditRestrictedStorage

73 self._EditRetiPeriodSt = _EditRetiPeriodSt

74 self._EditRetiPeriodEd = _EditRetiPeriodEd

75 self._EditFlowROType = _EditFlowROType

76 self._EditFlowAutoROMmaxOutflow_CMS = _EditFlowAutoROMmaxOutflow_CMS

77 self._EditFlowROConstQ = _EditFlowROConstQ

78 self._EditFlowROConstQDuration = _EditFlowROConstQDuration

79

80 self._AddFlowcontrolFilePath = ""

81 self._AddFlowcontrolType = ""

82 self._AddFlowcontrolTimeInterval = ""

83 self._AddFlowcontrolName = ""

84 self._AddFlowcontrol_IniStorage = ""

85 self._AddFlowcontrol_MaxStorage = "0"

86 self._AddNomalHighStorage = "0"

87 self._AddRestrictedStorage = "0"

88 iniDate = "01M01D" if self.datatimeFormat else "0"

89 self._AddRetiPeriodSt = iniDate

90 self._AddRetiPeriodEd = iniDate

91 self._AddFlowcontrol_ROType = ""

92 self._AddFlowcontrol_AutoROMmaxOutflow_CMS = ""

93 self._AddFlowcontrol_ROConstQ = ""

94 self._AddFlowcontrol_ROConstQDuration = ""

95

96 combolist = [

97 "ReservoirOutflow",

98 "Inlet",

99 "ReservoirOperation",

100 "SinkFlow",

101 "SourceFlow",

102 ]

103 self.cmb_ControlType.addItems(combolist)

104 self.cmb_ControlType.currentIndexChanged.connect(

105 lambda: self.SelectControtype(

106 self.cmb_ControlType,

107 self.txt_TimeInterval,

108 self.btnLoadFile,

109 self.btnFileFormat,

110 self.txtFilePath,

111 )

112 )

113 self.preControlType = self.cmb_ControlType.currentText()

114

115 self.btnLoadFile.clicked.connect(

116 lambda: self.FileSelectDialog(self.txtFilePath)

117 )

118 self.btnFileFormat.clicked.connect(self.ViewFileFormat)

119 self.rdoAutomatic.clicked.connect(self.ClickRdoAutomatic)

120 self.txt_outflow_cms.setEnabled(False)

121 self.txt_cms.setEnabled(False)

122 self.txt_hours.setEnabled(False)

123 self.txt_Constant_discharge.setEnabled(False)

124 self.SelectControtype(

125 self.cmb_ControlType,

126 self.txt_TimeInterval,

127 self.btnLoadFile,

128 self.btnFileFormat,

129 self.txtFilePath,

130 )

131 self.rdoAutomatic.setChecked(True)

132

133 self.rdoRigid.clicked.connect(self.ClickRdoRigid)

134 self.rdoUsingConstant.clicked.connect(self.ClickRdoUsingConstant)

135 self.btnCancel.clicked.connect(lambda: self.Close_Form(False, "cancel"))

136 self.btnOK.clicked.connect(self.Ok_click)

137

138 if self._AddFlowcontrol_Edit_or_Insert_type != "Insert":

139 self.SetControlData()

140

141 self.setWindowFlags(Qt.Window | Qt.WindowTitleHint | Qt.CustomizeWindowHint)

142 self.groupBox_5.setStyleSheet("QGroupBox{padding-top:15px;margin-top:-15px;}")

143

144 def SelectControtype(

145 self, combox, txtinterval, btnLoadFile, btnFileFormat, txtFilePath

146 ):

147 crtTxt = combox.currentText()

148 if crtTxt == "ReservoirOperation":

149 self.groupBox_4.setEnabled(False) # set flow data

150 self.groupBox_3.setEnabled(True) # reservoir operation

151 self.groupBox_2.setEnabled(True)

152 self.txt_TimeInterval.setEnabled(False)

153

154 if self.rdoAutomatic.isChecked():

155 self.txt_outflow_cms.setEnabled(True)

156 self.txt_outflow_cms.setText(self._EditFlowAutoROMmaxOutflow_CMS)

157 self.txt_cms.setEnabled(False)

158 self.txt_Constant_discharge.setEnabled(False)

159 self.txt_hours.setEnabled(False)

160 elif self.rdoRigid.isChecked():

161 self.txt_cms.setText(self._EditFlowROConstQ)

162 self.ClickRdoRigid()

163 elif self.rdoUsingConstant.isChecked():

164 self.rdoUsingConstant.setChecked(True)

165 self.txt_Constant_discharge.setText(self._EditFlowROConstQ)

166 self.txt_hours.setText(self._EditFlowROConstQDuration)

167 self.ClickRdoUsingConstant()

168 elif crtTxt in ["SinkFlow", "SourceFlow", "Inlet"]:

169 self.groupBox_4.setEnabled(True)

170 self.groupBox_3.setEnabled(False)

171 self.groupBox_2.setEnabled(False)

172 self.txt_TimeInterval.setEnabled(True)

173 else:

174 self.groupBox_4.setEnabled(True)

175 self.groupBox_3.setEnabled(False)

176 self.groupBox_2.setEnabled(True)

177 self.txt_TimeInterval.setEnabled(True)

178

179 if (

180 self.preControlType == "ReservoirOperation"

181 or crtTxt == "ReservoirOperation"

182 ):

183 self.txtFilePath.setText("")

184

185 def FileSelectDialog(self, txtpath):

186 txtpath.clear()

187 dir = os.path.dirname(os.path.realpath(self.project_path))

188 self.filename = QFileDialog.getOpenFileName(self, "select file ", dir, "*.txt")[

189 0

190 ]

191 txtpath.setText(self.filename)

192

193 def ClickRdoAutomatic(self):

194 self.txt_outflow_cms.setEnabled(True)

195 self.txt_cms.setEnabled(False)

196 self.txt_hours.setEnabled(False)

197 self.txt_Constant_discharge.setEnabled(False)

198

199 def ClickRdoRigid(self):

200 self.txt_outflow_cms.setEnabled(False)

201 self.txt_cms.setEnabled(True)

202 self.txt_hours.setEnabled(False)

203 self.txt_Constant_discharge.setEnabled(False)

204

205 def ClickRdoUsingConstant(self):

206 self.txt_outflow_cms.setEnabled(False)

207 self.txt_cms.setEnabled(False)

208 self.txt_hours.setEnabled(True)

209 self.txt_Constant_discharge.setEnabled(True)

210

211 def ViewFileFormat(self):

212 results = FileFormat()

213 results.exec_()

214

215 def Close_Form(self, OkCancel, insert_edit):

216 if OkCancel:

217 self._AddFlowcontrol_Edit_or_Insert_type = insert_edit

218 self.close()

219 else:

220 self._AddFlowcontrol_Edit_or_Insert_type = "cancel"

221 self.close()

222

223 def keyPressEvent(self, e):

224 if e.key() == 16777216:

225 pass

226

227 def Ok_click(self):

228 if self._AddFlowcontrol_Edit_or_Insert_type == "Insert":

229 if self.txt_Name.text().strip() == "":

230 MsInfo(" Input name, please")

231 self.txt_Name.setFocus()

232 return

233 else:

234 self._AddFlowcontrolName = self.txt_Name.text()

235

236 self._AddFlowcontrolType = self.cmb_ControlType.currentText()

237 if self.txt_TimeInterval.text().strip() == "":

238 MsInfo(" Please enter a time interval")

239 self.txt_TimeInterval.setFocus()

240 return

241 else:

242 if self.txt_TimeInterval.text().isdigit():

243 self._AddFlowcontrolTimeInterval = self.txt_TimeInterval.text()

244 else:

245 MsInfo(" Please enter a time interval")

246 self.txt_TimeInterval.setFocus()

247 return

248

249 if self.cmb_ControlType.currentText() != "ReservoirOperation":

250 if self.txtFilePath.text() == "":

251 MsInfo("Please enter only numbers")

252 self.txtFilePath.setFocus()

253 return

254 else:

255 self._AddFlowcontrolFilePath = self.txtFilePath.text()

256 else:

257 # self._AddFlowcontrolFilePath = "ResurvoirOperation"

258 self._AddFlowcontrolFilePath = ""

259

260 rst = self.confirmFlowValue()

261 if not rst:

262 return

263 retrStrSt, retrStrEd, inistorg, maxstorg, nomalstorg, retrstorg = rst

264

265 self._AddFlowcontrol_IniStorage = inistorg

266 self._AddFlowcontrol_MaxStorage = maxstorg

267 self._AddNomalHighStorage = nomalstorg

268 self._AddRestrictedStorage = retrstorg

269 self._AddRetiPeriodSt = retrStrSt

270 self._AddRetiPeriodEd = retrStrEd

271

272 if self.cmb_ControlType.currentText() == "ReservoirOperation":

273 if self.rdoAutomatic.isChecked():

274 self._AddFlowcontrol_ROType = "AutoROM"

275 self._AddFlowcontrol_AutoROMmaxOutflow_CMS = (

276 self.txt_outflow_cms.text()

277 )

278

279 elif self.rdoRigid.isChecked():

280 self._AddFlowcontrol_ROType = "RigidROM"

281 self._AddFlowcontrol_ROConstQ = self.txt_cms.text()

282 self._AddFlowcontrol_ROConstQDuration = ""

283

284 elif self.rdoUsingConstant.isChecked():

285 self._AddFlowcontrol_ROType = "ConstantQ"

286 self._AddFlowcontrol_ROConstQ = self.txt_Constant_discharge.text()

287 self._AddFlowcontrol_ROConstQDuration = self.txt_hours.text()

288 else:

289 self._AddFlowcontrol_ROType = ""

290 self._AddFlowcontrol_ROConstQ = ""

291 self._AddFlowcontrol_ROConstQDuration = ""

292

293 self._Flowcontrolgrid_flag_Insert = True

294 self.Close_Form(True, self._AddFlowcontrol_Edit_or_Insert_type)

295

296 else:

297 if self.txt_Name.text() == "":

298 MsInfo(" Input name, please")

299 self.txt_Name.setFocus()

300 return

301 else:

302 self._EditFlowName = self.txt_Name.text()

303

304 self._EditFlowControlType = self.cmb_ControlType.currentText()

305

306 if self.txt_TimeInterval.text() == "":

307 MsInfo(" Please enter a time interval")

308 self.txt_TimeInterval.setFocus()

309 return

310 else:

311 self._EditFlowDT = self.txt_TimeInterval.text()

312

313 if self.cmb_ControlType.currentText() != "ReservoirOperation":

314 if self.txtFilePath.text() == "":

315 MsInfo(" Please set file path")

316 self.txtFilePath.setFocus()

317 return

318 else:

319 self._EditFlowFlowDataFile = self.txtFilePath.text()

320 else:

321 # self._EditFlowFlowDataFile = "ResurvoirOperation"

322 self._EditFlowFlowDataFile = ""

323

324 rst = self.confirmFlowValue()

325 if not rst:

326 return

327 retrStrSt, retrStrEd, inistorg, maxstorg, nomalstorg, retrstorg = rst

328

329 self._EditFlowIniStorage = inistorg

330 self._EditFlowMaxStorage = maxstorg

331 self._EditNomalHighStorage = nomalstorg

332 self._EditRestrictedStorage = retrstorg

333 self._EditRetiPeriodSt = retrStrSt

334 self._EditRetiPeriodEd = retrStrEd

335

336 if self.cmb_ControlType.currentText() == "ReservoirOperation":

337 if self.rdoAutomatic.isChecked():

338 self._EditFlowROType = "AutoROM"

339 self._EditFlowAutoROMmaxOutflow_CMS = self.txt_outflow_cms.text()

340

341 elif self.rdoRigid.isChecked():

342 self._EditFlowROType = "RigidROM"

343 self._EditFlowROConstQ = self.txt_cms.text()

344 self._EditFlowROConstQDuration = ""

345

346 elif self.rdoUsingConstant.isChecked():

347 self._EditFlowROType = "ConstantQ"

348 self._EditFlowROConstQ = self.txt_Constant_discharge.text()

349 self._EditFlowROConstQDuration = self.txt_hours.text()

350 else:

351 self._EditFlowROType = ""

352 self._EditFlowAutoROMmaxOutflow_CMS = ""

353 self._EditFlowROConstQ = ""

354 self._EditFlowROConstQDuration = ""

355

356 self.Close_Form(True, self._AddFlowcontrol_Edit_or_Insert_type)

357

358 def confirmFlowValue(self):

359 retrStrSt = self.txtRetiPeriodSt.text().strip().upper()

360 retrStrEd = self.txtRetiPeriodEd.text().strip().upper()

361 inistorg = self.txtIniStorage.text().strip()

362 maxstorg = self.txtMaxStorage.text().strip()

363 nomalstorg = self.txtNomalHighStorage.text().strip()

364 retrstorg = self.txtRestrictedStorage.text().strip()

365 l = [retrStrSt, retrStrEd, inistorg, maxstorg, nomalstorg, retrstorg]

366

367 for sto in l[2:]:

368 if sto and not floatVerification(sto):

369 MsInfo("Enter a number in Storage.")

370 return False

371 if (

372 (l[:2] != [""] * 2)

373 and (self.datatimeFormat)

374 and (

375 not retrPdDayVerification(retrStrSt)

376 or not retrPdDayVerification(retrStrEd)

377 )

378 ):

379 MsInfo(

380 " Enter the Restricted period in the following format. \n ex) 01M01D"

381 )

382 return False

383 elif (

384 (l[:2] != [""] * 2)

385 and (not self.datatimeFormat)

386 and (not floatVerification(retrStrSt) or not floatVerification(retrStrEd))

387 ): # 숫자형식

388 MsInfo(" Enter a number in Restricted period. ")

389 return False

390

391 for i, j in enumerate(l):

392 if self.datatimeFormat and i < 2 and not j:

393 l[i] = "01M01D"

394 elif (not self.datatimeFormat and i < 2 and not j) or i > 2 and not j:

395 l[i] = "0"

396

397 return l

398

399 def SetControlData(self):

400 self.txt_Name.setText(self._EditFlowName)

401 controltype = self._EditFlowControlType

402 index = self.cmb_ControlType.findText(str(controltype), Qt.MatchFixedString)

403 if index >= 0:

404 self.cmb_ControlType.setCurrentIndex(index)

405 self.txt_TimeInterval.setText(self._EditFlowDT)

406 self.txtFilePath.setText(self._EditFlowFlowDataFile)

407 self.txtIniStorage.setText(self._EditFlowIniStorage)

408 self.txtMaxStorage.setText(self._EditFlowMaxStorage)

409 self.txtNomalHighStorage.setText(self._EditNomalHighStorage)

410 self.txtRestrictedStorage.setText(self._EditRestrictedStorage)

411 self.txtRetiPeriodSt.setText(self._EditRetiPeriodSt)

412 self.txtRetiPeriodEd.setText(self._EditRetiPeriodEd)

413

414 if self._EditFlowROType == "AutoROM":

415 self.rdoAutomatic.setChecked(True)

416 self.txt_outflow_cms.setText(self._EditFlowAutoROMmaxOutflow_CMS)

417 self.ClickRdoAutomatic()

418 elif self._EditFlowROType == "RigidROM":

419 self.rdoRigid.setChecked(True)

420 self.txt_cms.setText(self._EditFlowROConstQ)

421 self.ClickRdoRigid()

422 elif self._EditFlowROType == "ConstantQ":

423 self.rdoUsingConstant.setChecked(True)

424 self.txt_Constant_discharge.setText(self._EditFlowROConstQ)

425 self.txt_hours.setText(self._EditFlowROConstQDuration)

426 self.ClickRdoUsingConstant()