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 _Flowcontrol_Edit_or_Insert_type="",

40 _EditFlowName="",

41 _EditFlowDT="",

42 _EditFlowControlType="",

43 _EditFlowFlowDataFile="",

44 _EditFlowIniStorage="",

45 _EditFlowMaxStorage="",

46 _EditNomalHighStorage="",

47 _EditRestrictedStorage="",

48 _EditRetiPeriodSt="",

49 _EditRetiPeriodEd="",

50 _EditFlowROType="",

51 _EditFlowAutoROMmaxOutflow_CMS="",

52 _EditFlowROConstQ="",

53 _EditFlowROConstQDuration="",

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

55 parent=None,

56 ):

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

58 self.setupUi(self)

59

60 self.datatimeFormat = _DataTimeFormat

61

62 self._AddFlowcontrol_Edit_or_Insert_type = _Flowcontrol_Edit_or_Insert_type

63 self._EditFlowName = _EditFlowName

64 self._EditFlowDT = _EditFlowDT

65 self._EditFlowControlType = _EditFlowControlType

66 self._EditFlowFlowDataFile = _EditFlowFlowDataFile

67 self._EditFlowIniStorage = _EditFlowIniStorage

68 self._EditFlowMaxStorage = _EditFlowMaxStorage

69 self._EditNomalHighStorage = _EditNomalHighStorage

70 self._EditRestrictedStorage = _EditRestrictedStorage

71 self._EditRetiPeriodSt = _EditRetiPeriodSt

72 self._EditRetiPeriodEd = _EditRetiPeriodEd

73 self._EditFlowROType = _EditFlowROType

74 self._EditFlowAutoROMmaxOutflow_CMS = _EditFlowAutoROMmaxOutflow_CMS

75 self._EditFlowROConstQ = _EditFlowROConstQ

76 self._EditFlowROConstQDuration = _EditFlowROConstQDuration

77

78 self._AddFlowcontrolFilePath = ""

79 self._AddFlowcontrolType = ""

80 self._AddFlowcontrolTimeInterval = ""

81 self._AddFlowcontrolName = ""

82 self._AddFlowcontrol_IniStorage = ""

83 self._AddFlowcontrol_MaxStorage = "0"

84 self._AddNomalHighStorage = "0"

85 self._AddRestrictedStorage = "0"

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

87 self._AddRetiPeriodSt = iniDate

88 self._AddRetiPeriodEd = iniDate

89 self._AddFlowcontrol_ROType = ""

90 self._AddFlowcontrol_AutoROMmaxOutflow_CMS = ""

91 self._AddFlowcontrol_ROConstQ = ""

92 self._AddFlowcontrol_ROConstQDuration = ""

93

94 combolist = [

95 "ReservoirOutflow",

96 "Inlet",

97 "ReservoirOperation",

98 "SinkFlow",

99 "SourceFlow",

100 ]

101 self.cmb_ControlType.addItems(combolist)

102 self.cmb_ControlType.currentIndexChanged.connect(

103 lambda: self.SelectControtype(

104 self.cmb_ControlType,

105 self.txt_TimeInterval,

106 self.btnLoadFile,

107 self.btnFileFormat,

108 self.txtFilePath,

109 )

110 )

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

112

113 self.btnLoadFile.clicked.connect(

114 lambda: self.FileSelectDialog(self.txtFilePath)

115 )

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

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

118 self.txt_outflow_cms.setEnabled(False)

119 self.txt_cms.setEnabled(False)

120 self.txt_hours.setEnabled(False)

121 self.txt_Constant_discharge.setEnabled(False)

122 self.SelectControtype(

123 self.cmb_ControlType,

124 self.txt_TimeInterval,

125 self.btnLoadFile,

126 self.btnFileFormat,

127 self.txtFilePath,

128 )

129 self.rdoAutomatic.setChecked(True)

130

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

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

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

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

135

136 if self._AddFlowcontrol_Edit_or_Insert_type != "Insert":

137 self.SetControlData()

138

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

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

141

142 def SelectControtype(

143 self, combox, txtinterval, btnLoadFile, btnFileFormat, txtFilePath

144 ):

145 crtTxt = combox.currentText()

146 if crtTxt == "ReservoirOperation":

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

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

149

150 if self.rdoAutomatic.isChecked():

151 self.txt_outflow_cms.setEnabled(True)

152 self.txt_outflow_cms.setText(self._EditFlowAutoROMmaxOutflow_CMS)

153 self.txt_cms.setEnabled(False)

154 self.txt_Constant_discharge.setEnabled(False)

155 self.txt_hours.setEnabled(False)

156 elif self.rdoRigid.isChecked():

157 self.txt_cms.setText(self._EditFlowROConstQ)

158 self.ClickRdoRigid()

159 elif self.rdoUsingConstant.isChecked():

160 self.rdoUsingConstant.setChecked(True)

161 self.txt_Constant_discharge.setText(self._EditFlowROConstQ)

162 self.txt_hours.setText(self._EditFlowROConstQDuration)

163 self.ClickRdoUsingConstant()

164 else:

165 self.groupBox_4.setEnabled(True)

166 self.groupBox_3.setEnabled(False)

167

168 if (

169 self.preControlType == "ReservoirOperation"

170 or crtTxt == "ReservoirOperation"

171 ):

172 self.txtFilePath.setText("")

173

174 def FileSelectDialog(self, txtpath):

175 txtpath.clear()

176 dir = os.path.dirname(os.path.realpath(__file__))

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

178 0

179 ]

180 txtpath.setText(self.filename)

181

182 def ClickRdoAutomatic(self):

183 self.txt_outflow_cms.setEnabled(True)

184 self.txt_cms.setEnabled(False)

185 self.txt_hours.setEnabled(False)

186 self.txt_Constant_discharge.setEnabled(False)

187

188 def ClickRdoRigid(self):

189 self.txt_outflow_cms.setEnabled(False)

190 self.txt_cms.setEnabled(True)

191 self.txt_hours.setEnabled(False)

192 self.txt_Constant_discharge.setEnabled(False)

193

194 def ClickRdoUsingConstant(self):

195 self.txt_outflow_cms.setEnabled(False)

196 self.txt_cms.setEnabled(False)

197 self.txt_hours.setEnabled(True)

198 self.txt_Constant_discharge.setEnabled(True)

199

200 def ViewFileFormat(self):

201 results = FileFormat()

202 results.exec_()

203

204 def Close_Form(self, OkCancel, insert_edit):

205 if OkCancel:

206 self._AddFlowcontrol_Edit_or_Insert_type = insert_edit

207 self.close()

208 else:

209 self._AddFlowcontrol_Edit_or_Insert_type = "cancel"

210 self.close()

211

212 def keyPressEvent(self, e):

213 if e.key() == 16777216:

214 pass

215

216 def Ok_click(self):

217 if self._AddFlowcontrol_Edit_or_Insert_type == "Insert":

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

219 MsInfo(" Input name, please")

220 self.txt_Name.setFocus()

221 return

222 else:

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

224

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

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

227 MsInfo(" Please enter a time interval")

228 self.txt_TimeInterval.setFocus()

229 return

230 else:

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

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

233 else:

234 MsInfo(" Please enter a time interval")

235 self.txt_TimeInterval.setFocus()

236 return

237

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

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

240 MsInfo("Please enter only numbers")

241 self.txtFilePath.setFocus()

242 return

243 else:

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

245 else:

246 # self._AddFlowcontrolFilePath = "ResurvoirOperation"

247 self._AddFlowcontrolFilePath = ""

248

249 rst = self.confirmFlowValue()

250 if not rst:

251 return

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

253

254 self._AddFlowcontrol_IniStorage = inistorg

255 self._AddFlowcontrol_MaxStorage = maxstorg

256 self._AddNomalHighStorage = nomalstorg

257 self._AddRestrictedStorage = retrstorg

258 self._AddRetiPeriodSt = retrStrSt

259 self._AddRetiPeriodEd = retrStrEd

260

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

262 if self.rdoAutomatic.isChecked():

263 self._AddFlowcontrol_ROType = "AutoROM"

264 self._AddFlowcontrol_AutoROMmaxOutflow_CMS = (

265 self.txt_outflow_cms.text()

266 )

267

268 elif self.rdoRigid.isChecked():

269 self._AddFlowcontrol_ROType = "RigidROM"

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

271 self._AddFlowcontrol_ROConstQDuration = ""

272

273 elif self.rdoUsingConstant.isChecked():

274 self._AddFlowcontrol_ROType = "ConstantQ"

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

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

277 else:

278 self._AddFlowcontrol_ROType = ""

279 self._AddFlowcontrol_ROConstQ = ""

280 self._AddFlowcontrol_ROConstQDuration = ""

281

282 self._Flowcontrolgrid_flag_Insert = True

283 self.Close_Form(True, self._AddFlowcontrol_Edit_or_Insert_type)

284

285 else:

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

287 MsInfo(" Input name, please")

288 self.txt_Name.setFocus()

289 return

290 else:

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

292

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

294

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

296 MsInfo(" Please enter a time interval")

297 self.txt_TimeInterval.setFocus()

298 return

299 else:

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

301

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

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

304 MsInfo(" Please set file path")

305 self.txtFilePath.setFocus()

306 return

307 else:

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

309 else:

310 # self._EditFlowFlowDataFile = "ResurvoirOperation"

311 self._EditFlowFlowDataFile = ""

312

313 rst = self.confirmFlowValue()

314 if not rst:

315 return

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

317

318 self._EditFlowIniStorage = inistorg

319 self._EditFlowMaxStorage = maxstorg

320 self._EditNomalHighStorage = nomalstorg

321 self._EditRestrictedStorage = retrstorg

322 self._EditRetiPeriodSt = retrStrSt

323 self._EditRetiPeriodEd = retrStrEd

324

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

326 if self.rdoAutomatic.isChecked():

327 self._EditFlowROType = "AutoROM"

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

329

330 elif self.rdoRigid.isChecked():

331 self._EditFlowROType = "RigidROM"

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

333 self._EditFlowROConstQDuration = ""

334

335 elif self.rdoUsingConstant.isChecked():

336 self._EditFlowROType = "ConstantQ"

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

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

339 else:

340 self._EditFlowROType = ""

341 self._EditFlowAutoROMmaxOutflow_CMS = ""

342 self._EditFlowROConstQ = ""

343 self._EditFlowROConstQDuration = ""

344

345 self.Close_Form(True, self._AddFlowcontrol_Edit_or_Insert_type)

346

347 def confirmFlowValue(self):

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

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

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

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

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

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

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

355

356 for sto in l[2:]:

357 if sto and not floatVerification(sto):

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

359 return False

360 if (

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

362 and (self.datatimeFormat)

363 and (

364 not retrPdDayVerification(retrStrSt)

365 or not retrPdDayVerification(retrStrEd)

366 )

367 ):

368 MsInfo(

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

370 )

371 return False

372 elif (

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

374 and (not self.datatimeFormat)

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

376 ): # 숫자형식

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

378 return False

379

380 for i, j in enumerate(l):

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

382 l[i] = "01M01D"

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

384 l[i] = "0"

385

386 return l

387

388 def SetControlData(self):

389 self.txt_Name.setText(self._EditFlowName)

390 controltype = self._EditFlowControlType

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

392 if index >= 0:

393 self.cmb_ControlType.setCurrentIndex(index)

394 self.txt_TimeInterval.setText(self._EditFlowDT)

395 self.txtFilePath.setText(self._EditFlowFlowDataFile)

396 self.txtIniStorage.setText(self._EditFlowIniStorage)

397 self.txtMaxStorage.setText(self._EditFlowMaxStorage)

398 self.txtNomalHighStorage.setText(self._EditNomalHighStorage)

399 self.txtRestrictedStorage.setText(self._EditRestrictedStorage)

400 self.txtRetiPeriodSt.setText(self._EditRetiPeriodSt)

401 self.txtRetiPeriodEd.setText(self._EditRetiPeriodEd)

402

403 if self._EditFlowROType == "AutoROM":

404 self.rdoAutomatic.setChecked(True)

405 self.txt_outflow_cms.setText(self._EditFlowAutoROMmaxOutflow_CMS)

406 self.ClickRdoAutomatic()

407 elif self._EditFlowROType == "RigidROM":

408 self.rdoRigid.setChecked(True)

409 self.txt_cms.setText(self._EditFlowROConstQ)

410 self.ClickRdoRigid()

411 elif self._EditFlowROType == "ConstantQ":

412 self.rdoUsingConstant.setChecked(True)

413 self.txt_Constant_discharge.setText(self._EditFlowROConstQ)

414 self.txt_hours.setText(self._EditFlowROConstQDuration)

415 self.ClickRdoUsingConstant()