hytos / DTI_PID / DTI_PID / QDirTreeWidget.py @ 4f0ae2af
이력 | 보기 | 이력해설 | 다운로드 (9.94 KB)
1 |
try:
|
---|---|
2 |
from PyQt5.QtCore import * |
3 |
from PyQt5.QtGui import * |
4 |
from PyQt5.QtWidgets import * |
5 |
except ImportError: |
6 |
try:
|
7 |
from PyQt4.QtCore import * |
8 |
from PyQt4.QtGui import * |
9 |
except ImportError: |
10 |
raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
11 |
from AppDocData import AppDocData |
12 |
import os |
13 |
import sys |
14 |
import SymbolBase |
15 |
import symbol |
16 |
import QSymbolEditorDialog |
17 |
import QSymbolDisplayDialog |
18 | |
19 |
class QDirTreeWidget(QTreeWidget): |
20 |
#Add signal
|
21 |
singleClicked = pyqtSignal(SymbolBase.SymbolBase) |
22 |
TREE_DATA_ROLE = Qt.ToolTipRole |
23 | |
24 |
def __init__(self): |
25 |
QTreeWidget.__init__(self)
|
26 |
self.setDragEnabled(True) # enable drag |
27 |
self.initDirTreeWidget()
|
28 |
self.isDoubleClicked = False |
29 |
self.itemDoubleClicked.connect(self.itemDoubleClickEvent) |
30 |
self.itemClicked.connect(self.itemClickEvent) |
31 |
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
32 |
self.customContextMenuRequested.connect(self.openContextMenu) |
33 |
|
34 |
def openContextMenu(self, position): |
35 |
indexes = self.selectedIndexes()
|
36 |
itemPosition = self.mapTo(self, position) |
37 |
item = self.itemAt(itemPosition)
|
38 |
sym = self.getSymbolByItemName(item, 0) |
39 |
text = item.text(0)
|
40 |
if len(indexes) > 0: |
41 |
level = 0
|
42 |
index = indexes[0]
|
43 |
while index.parent().isValid():
|
44 |
index = index.parent() |
45 |
level += 1
|
46 |
if text.lower().endswith(".png"): |
47 |
menu = QMenu() |
48 |
editSymbolAction = QAction(self.tr("Edit Symbol")) |
49 |
editSymbolAction.triggered.connect(lambda: self.editSymbolActionClickEvent(item, 0)) |
50 |
menu.addAction(editSymbolAction) |
51 |
displaySymbolAction = QAction(self.tr("Display Symbol")) |
52 |
displaySymbolAction.triggered.connect(lambda: self.displaySymbolActionClickEvent(sym.getType(), text)) |
53 |
menu.addAction(displaySymbolAction) |
54 |
deleteSymbolAction = QAction(self.tr("Delete Symbol")) |
55 |
deleteSymbolAction.triggered.connect(lambda: self.deleteSymbolActionClickEvent(sym.getType(), text)) |
56 |
menu.addAction(deleteSymbolAction) |
57 |
menu.exec_(self.viewport().mapToGlobal(position))
|
58 | |
59 |
def editSymbolActionClickEvent(self, item, columNo): |
60 |
self.showSymbolEditorDialog(item, columNo)
|
61 | |
62 |
def displaySymbolActionClickEvent(self, itemType, itemName): |
63 |
project = AppDocData.instance().getCurrentProject() |
64 |
image = QImage(project.getImageFilePath()+"/"+itemType+"/"+itemName, "PNG") #itemName includes ".png" |
65 |
dialog = QSymbolDisplayDialog.QSymbolDisplayDialog(image) |
66 |
dialog.showDialog() |
67 | |
68 |
def deleteSymbolActionClickEvent(self, itemType, itemName): |
69 |
msg = QMessageBox() |
70 |
msg.setIcon(QMessageBox.Critical) |
71 |
msg.setText("선택한 심볼을 삭제하시겠습니까?\n삭제된 심볼은 복구할 수 없습니다.")
|
72 |
msg.setWindowTitle("심볼 삭제")
|
73 |
msg.setStandardButtons(QMessageBox.Ok|QMessageBox.Cancel) |
74 |
#msg.buttonClicked.connect(lambda: self.handleDeleteSymbolAction(itemName))
|
75 |
result = msg.exec_() |
76 |
self.handleDeleteSymbolAction(result, itemType, itemName)
|
77 | |
78 |
def handleDeleteSymbolAction(self, result, itemType, itemName): |
79 |
print("handle")
|
80 |
if result == QMessageBox.Ok:
|
81 |
project = AppDocData.instance().getCurrentProject() |
82 |
imagePath = project.getImageFilePath() + "/" + itemType + "/" + itemName # itemName includes ".png" |
83 |
if os.path.exists(imagePath):
|
84 |
os.remove(imagePath) |
85 | |
86 |
svgPath = AppDocData.instance().getSvgFilePath() + "/" + itemType + "/" + itemName.replace(".png", ".svg") |
87 |
if os.path.exists(svgPath):
|
88 |
os.remove(svgPath) |
89 | |
90 |
AppDocData.instance().deleteSymbol(itemName.replace(".png", "")) |
91 |
self.initDirTreeWidget()
|
92 |
else:
|
93 |
pass
|
94 | |
95 |
def initDirTreeWidget(self): |
96 |
project = AppDocData.instance().getCurrentProject() |
97 |
if project is not None: |
98 |
self.clear()
|
99 |
print("Project : " + project.getName())
|
100 |
projectPath = project.getPath().replace("\\", "/") |
101 |
self.makeChildDir()
|
102 |
self.loadSymbolInfo()
|
103 |
#self.loadDirectoryInfo(project.getImageFilePath(), self)
|
104 |
self.expandAll()
|
105 | |
106 |
'''
|
107 |
@brief Load Symbol Info and add TreeItem with DB
|
108 |
@author Jeongwoo
|
109 |
@date 18.04.20
|
110 |
'''
|
111 |
def loadSymbolInfo(self): |
112 |
'''ss'''
|
113 |
SVG_PATH_ROOT = AppDocData.instance().getCurrentProject().getSvgFilePath() |
114 |
symbolTypeList = AppDocData.instance().getSymbolTypeList() |
115 |
for symbolType in symbolTypeList: |
116 |
parent = QTreeWidgetItem(self, [symbolType])
|
117 |
symbolList = AppDocData.instance().getSymbolListByQuery('type', symbolType)
|
118 |
#symbolNameList = AppDocData.instance().getSymbolNameListByType(symbolType)
|
119 |
for symbol in symbolList: |
120 |
symbolItem = QTreeWidgetItem(parent, [symbol.getName()]) |
121 |
symbolItem.setData(0, self.TREE_DATA_ROLE, symbol) ## ADD DATA |
122 |
svgPath = SVG_PATH_ROOT + "/" + symbolType + "/" + symbol.getName() + ".svg" |
123 |
icon = QIcon(svgPath) |
124 |
symbolItem.setIcon(0, icon)
|
125 |
symbolItem.svgFilePath = svgPath # save svg file path
|
126 | |
127 | |
128 |
'''
|
129 |
@brief Make Directory
|
130 |
@author Jeongwoo
|
131 |
@date 18.04.??
|
132 |
@history 18.04.12 Jeongwoo Add output, temp Directory
|
133 |
'''
|
134 |
def makeChildDir(self): |
135 |
project = AppDocData.instance().getCurrentProject() |
136 |
dbDir = project.getDbFilePath() |
137 |
if not os.path.exists(dbDir): |
138 |
os.makedirs(dbDir) |
139 |
imgDir = project.getImageFilePath() |
140 |
if not os.path.exists(imgDir): |
141 |
os.makedirs(imgDir) |
142 |
svgDir = project.getSvgFilePath() |
143 |
if not os.path.exists(svgDir): |
144 |
os.makedirs(svgDir) |
145 |
outputDir = project.getOutputPath() |
146 |
if not os.path.exists(outputDir): |
147 |
os.makedirs(outputDir) |
148 |
tempDir = project.getTempPath() |
149 |
if not os.path.exists(tempDir): |
150 |
os.makedirs(tempDir) |
151 | |
152 |
#def loadDirectoryInfo(self, startPath, tree):
|
153 |
# for element in os.listdir(startPath):
|
154 |
# pathInfo = os.path.join(startPath, element)
|
155 |
# parentItem = QTreeWidgetItem(tree, [os.path.basename(element)])
|
156 |
# if pathInfo.endswith(".png"):
|
157 |
# icon = QIcon(pathInfo)
|
158 |
# parentItem.setIcon(0, icon)
|
159 |
# parentItem.svgFilePath = pathInfo # save svg file path
|
160 |
# if os.path.isdir(pathInfo):
|
161 |
# self.loadDirectoryInfo(pathInfo, parentItem)
|
162 | |
163 |
def showSymbolEditorDialog(self, item, columnNo): |
164 |
try:
|
165 |
sym = self.getSymbolByItemName(item, columnNo)
|
166 |
if sym is not None: |
167 |
path = sym.getPath() |
168 |
image = QImage(path, "PNG")
|
169 |
print("after image")
|
170 |
symbolEditorDialog = QSymbolEditorDialog.QSymbolEditorDialog(self, image, AppDocData.instance().getCurrentProject(), sym)
|
171 |
_ret = symbolEditorDialog.showDialog() |
172 |
self.initDirTreeWidget()
|
173 |
else:
|
174 |
QMessageBox.about(self, "알림", "심볼 데이터를 불러오는 중 에러가 발생했습니다.") |
175 |
except Exception as ex: |
176 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
177 | |
178 | |
179 |
def itemDoubleClickEvent(self, item, columnNo): |
180 |
self.isDoubleClicked = True |
181 |
itemName = item.text(columnNo) |
182 |
print(itemName + " : Double Clicked")
|
183 |
self.showSymbolEditorDialog(item, columnNo)
|
184 |
self.isDoubleClicked = False |
185 | |
186 |
def itemClickEvent(self, item, columnNo): |
187 |
if self.isDoubleClicked == False: |
188 |
print(item.text(columnNo) + " : Single Clicked")
|
189 |
sym = self.getSymbolByItemName(item, columnNo)
|
190 |
if sym is not None: |
191 |
self.singleClicked.emit(sym)
|
192 |
else:
|
193 |
QMessageBox.about(self, "알림", "심볼 데이터를 불러오는 중 에러가 발생했습니다.") |
194 | |
195 |
'''
|
196 |
@brief Get Symbol data by symbol name
|
197 |
@author Jeongwoo
|
198 |
@date 18.04.20
|
199 |
'''
|
200 |
def getSymbolByItemName(self, item, columnNo): |
201 |
tmpItem = item |
202 |
itemName = item.text(columnNo) |
203 |
#if itemName.lower().endswith(".png"):
|
204 |
#path = itemName
|
205 |
#while tmpItem.parent() is not None:
|
206 |
# path = tmpItem.parent().text(columnNo) + "/" + path
|
207 |
# tmpItem = tmpItem.parent()
|
208 |
#fullPath = AppDocData.instance().getCurrentProject().getImageFilePath() + "/" + path # path includes ".png"
|
209 |
|
210 |
#name = itemName.replace(".png", "")
|
211 |
name = itemName |
212 |
sym = AppDocData.instance().getSymbolByQuery("name", name)
|
213 |
return sym
|
214 |
#else:
|
215 |
# return None
|
216 | |
217 |
'''
|
218 |
@brief start drag
|
219 |
@author humkyung
|
220 |
@date 2018.04.17
|
221 |
@history 18.04.20 Jeongwoo Change Path in QPixmap
|
222 |
'''
|
223 |
def startDrag(self, dropAction): |
224 |
try:
|
225 |
items = self.selectedItems()
|
226 |
if items:
|
227 |
symData = items[0].data(0, self.TREE_DATA_ROLE) |
228 |
pixmap = QPixmap(AppDocData.instance().getCurrentProject().getSvgFilePath() + '/'+symData.getType()+'/' +symData.getName()+ '.svg') |
229 | |
230 |
mime = QMimeData() |
231 |
mime.setText('BALL VALVE WITH PLUG')
|
232 | |
233 |
drag = QDrag(self)
|
234 |
drag.setMimeData(mime) |
235 |
originalPoint = symData.getOriginalPoint() |
236 |
drag.setHotSpot(QPoint(int(originalPoint.split(",")[0]), int(originalPoint.split(",")[1]))) |
237 |
drag.setPixmap(pixmap) |
238 |
drag.exec(Qt.CopyAction) |
239 |
except Exception as ex: |
240 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |