hytos / DTI_PID / DTI_PID / ProjectDialog.py @ d66325ef
이력 | 보기 | 이력해설 | 다운로드 (7.93 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
|
3 |
# Form implementation generated from reading ui file 'ProjectDialog.ui'
|
4 |
#
|
5 |
# Created by: PyQt5 UI code generator 5.6
|
6 |
#
|
7 |
# WARNING! All changes made in this file will be lost!
|
8 |
|
9 |
from PyQt5 import QtCore, QtGui, QtWidgets |
10 |
from PyQt5.QtWidgets import * |
11 |
import os |
12 |
from Project import Project |
13 |
from AppDocData import AppDocData |
14 |
import Project_UI |
15 |
|
16 |
|
17 |
class Ui_Dialog(QDialog): |
18 |
def __init__(self, parent=None): |
19 |
QDialog.__init__(self, parent)
|
20 |
_translate = QtCore.QCoreApplication.translate |
21 |
|
22 |
self.selectedProject = None |
23 |
self.ui = Project_UI.Ui_ProjectDialog()
|
24 |
self.ui.setupUi(self) |
25 |
self.initComboBox()
|
26 |
self.ui.comboBox.currentIndexChanged.connect(self.changeProject) |
27 |
self.ui.radioButtonSQLite.clicked.connect(self.on_sqlite_selected) |
28 |
self.ui.radioButtonMSSQL.clicked.connect(self.on_mssql_selected) |
29 |
self.ui.lineEditPassword.setEchoMode(QLineEdit.Password)
|
30 |
self.ui.pushButtonTestConnection.clicked.connect(self.on_test_connection_clicked) |
31 |
self.changeProject() # force fill project's properties |
32 |
self.ui.toolButton.clicked.connect(self.addProjectClick) |
33 |
self.ui.toolButtonDelete.clicked.connect(self.deleteProjectClick) |
34 |
self.setWindowTitle(_translate('Project Dialog', 'Project')) |
35 |
|
36 |
def initComboBox(self): |
37 |
from AppDocData import AppDocData |
38 |
|
39 |
self.ui.comboBox.clear()
|
40 |
projectList = AppDocData.instance().getProjectList() |
41 |
# ComboBox setting
|
42 |
for project in projectList: |
43 |
self.ui.comboBox.addItem(project.getName(), project)
|
44 |
|
45 |
if projectList: self.ui.comboBox.setCurrentIndex(0) |
46 |
|
47 |
self.ui.comboBoxProjectUnit.clear()
|
48 |
self.ui.comboBoxProjectUnit.addItem('Metric') |
49 |
self.ui.comboBoxProjectUnit.addItem('Imperial') |
50 |
self.ui.comboBoxProjectUnit.setCurrentIndex(0) |
51 |
|
52 |
def showDialog(self): |
53 |
self.setWindowFlags(
|
54 |
self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint & ~QtCore.Qt.WindowContextHelpButtonHint)
|
55 |
self.exec_()
|
56 |
|
57 |
index = self.ui.comboBox.currentIndex()
|
58 |
if self.selectedProject is None: |
59 |
return None |
60 |
else:
|
61 |
return self.ui.comboBox.itemData(index) |
62 |
|
63 |
def accept(self): |
64 |
""" accept project """
|
65 |
index = self.ui.comboBox.currentIndex()
|
66 |
project = self.ui.comboBox.itemData(index)
|
67 |
prj_desc = self.ui.lineEditProjectDesc.text()
|
68 |
prj_unit = self.ui.comboBoxProjectUnit.currentText()
|
69 |
if project is not None: |
70 |
project.desc = prj_desc |
71 |
project.prj_unit = prj_unit |
72 |
project.database.db_type = 'SQLite' if self.ui.radioButtonSQLite.isChecked() else 'MSSQL' |
73 |
if project.database.db_type == 'MSSQL': |
74 |
project.database.host = self.ui.lineEditServer.text()
|
75 |
project.database.user = self.ui.lineEditUser.text()
|
76 |
project.database.password = self.ui.lineEditPassword.text()
|
77 |
project.database.file_path = project.getName() |
78 |
else:
|
79 |
project.database.host = None
|
80 |
project.database.user = None
|
81 |
project.database.password = None
|
82 |
project.database.file_path = os.path.join(project.getDbFilePath(), Project.DATABASE) |
83 |
|
84 |
AppDocData.instance().updateProjectUpdatedDate(project) |
85 |
self.selectedProject = True |
86 |
|
87 |
QDialog.accept(self)
|
88 |
|
89 |
def reject(self): |
90 |
self.selectedProject = None |
91 |
QDialog.reject(self)
|
92 |
|
93 |
def addProjectClick(self): |
94 |
_translate = QtCore.QCoreApplication.translate |
95 |
|
96 |
options = QFileDialog.Options() |
97 |
options |= QFileDialog.DontUseNativeDialog |
98 |
options |= QFileDialog.ShowDirsOnly |
99 |
selected_dir = QFileDialog.getExistingDirectory(None, _translate('Project Dialog', "Select Project Path"), |
100 |
os.getcwd(), options=options) |
101 |
if selected_dir:
|
102 |
if selected_dir and not any(c.isspace() for c in selected_dir): |
103 |
prj_unit = self.ui.comboBoxProjectUnit.currentText()
|
104 |
self.insertProjectInfo(dir=selected_dir, desc=self.ui.lineEditProjectDesc.text(), prj_unit=prj_unit) |
105 |
else:
|
106 |
QMessageBox.warning(self, self.tr('Message'), self.tr('Folder name should not contains space')) |
107 |
|
108 |
def deleteProjectClick(self): |
109 |
"""remove selected project"""
|
110 |
self.selectedProject = self.ui.comboBox.currentData() |
111 |
if self.selectedProject: |
112 |
msg = QMessageBox() |
113 |
msg.setIcon(QMessageBox.Critical) |
114 |
msg.setText(self.tr('Are you sure you want to delete project "{}" ?\nData can not be restored! '.format( |
115 |
self.selectedProject.name)))
|
116 |
msg.setWindowTitle(self.tr('Delete Project')) |
117 |
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) |
118 |
if QMessageBox.Ok == msg.exec_():
|
119 |
AppDocData.instance().removeProjectInfo(self.selectedProject)
|
120 |
self.initComboBox()
|
121 |
|
122 |
'''
|
123 |
@brief display project desc,unit and database information when change project
|
124 |
@author humkyung
|
125 |
@date 2019.04.09
|
126 |
'''
|
127 |
|
128 |
def changeProject(self): |
129 |
index = self.ui.comboBox.currentIndex()
|
130 |
project = self.ui.comboBox.itemData(index)
|
131 |
if project:
|
132 |
self.ui.lineEditProjectDesc.setText(project.desc)
|
133 |
self.ui.comboBoxProjectUnit.setCurrentText(project.prj_unit)
|
134 |
|
135 |
self.ui.radioButtonSQLite.setChecked(project.database.db_type == 'SQLite') |
136 |
self.ui.radioButtonMSSQL.setChecked(project.database.db_type == 'MSSQL') |
137 |
self.ui.lineEditServer.setEnabled(project.database.db_type == 'MSSQL') |
138 |
self.ui.lineEditUser.setEnabled(project.database.db_type == 'MSSQL') |
139 |
self.ui.lineEditPassword.setEnabled(project.database.db_type == 'MSSQL') |
140 |
self.ui.pushButtonTestConnection.setEnabled(project.database.db_type == 'MSSQL') |
141 |
if project.database.db_type == 'MSSQL': |
142 |
self.ui.lineEditServer.setText(project.database.host)
|
143 |
self.ui.lineEditUser.setText(project.database.user)
|
144 |
self.ui.lineEditPassword.setText(project.database.password)
|
145 |
|
146 |
def on_sqlite_selected(self): |
147 |
""" call when user select sqlite """
|
148 |
|
149 |
self.ui.lineEditServer.setEnabled(False) |
150 |
self.ui.lineEditUser.setEnabled(False) |
151 |
self.ui.lineEditPassword.setEnabled(False) |
152 |
self.ui.pushButtonTestConnection.setEnabled(False) |
153 |
|
154 |
def on_mssql_selected(self): |
155 |
""" called when user select mssql """
|
156 |
|
157 |
self.ui.lineEditServer.setEnabled(True) |
158 |
self.ui.lineEditUser.setEnabled(True) |
159 |
self.ui.lineEditPassword.setEnabled(True) |
160 |
self.ui.pushButtonTestConnection.setEnabled(True) |
161 |
|
162 |
def on_test_connection_clicked(self): |
163 |
from AppDatabase import AppDatabase |
164 |
|
165 |
index = self.ui.comboBox.currentIndex()
|
166 |
project = self.ui.comboBox.itemData(index)
|
167 |
database = AppDatabase('MSSQL', self.ui.lineEditServer.text(), self.ui.lineEditUser.text(), |
168 |
self.ui.lineEditPassword.text(), db_path=project.getName())
|
169 |
try:
|
170 |
conn = database.connect() |
171 |
with conn:
|
172 |
QMessageBox.information(self, self.tr('Information'), self.tr('Test connection is success')) |
173 |
except Exception as ex: |
174 |
mb = QMessageBox() |
175 |
mb.setIcon(QMessageBox.Critical) |
176 |
mb.setWindowTitle(self.tr('Error')) |
177 |
mb.setText('{}'.format(ex))
|
178 |
mb.exec_() |
179 |
|
180 |
def insertProjectInfo(self, desc, prj_unit, dir): |
181 |
AppDocData.instance().insertProjectInfo(dir=dir, desc=desc, prj_unit=prj_unit)
|
182 |
self.initComboBox()
|
183 |
|
184 |
# if __name__ == "__init__":
|
185 |
# dialog = QtWidgets.QDialog()
|
186 |
# ui = Ui_Dialog()
|
187 |
# ui.setupUi(Dialog)
|
188 |
# Dialog.show()
|
189 |
|
190 |
# if __name__ == "__main__":
|
191 |
# import sys
|
192 |
# app = QtWidgets.QApplication(sys.argv)
|
193 |
# Dialog = QtWidgets.QDialog()
|
194 |
# Dialog.show()
|
195 |
# sys.exit(app.exec_())
|