hytos / DTI_PID / DTI_PID / QDirTreeWidget.py @ fbedb942
이력 | 보기 | 이력해설 | 다운로드 (10.7 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 |
'''
|
35 |
@brief Show Context Menu
|
36 |
@author Jeongwoo
|
37 |
@date 18.04.??
|
38 |
@history 18.04.23 Jeongwoo Symbol object Null Check when show context menu
|
39 |
'''
|
40 |
def openContextMenu(self, position): |
41 |
indexes = self.selectedIndexes()
|
42 |
itemPosition = self.mapTo(self, position) |
43 |
item = self.itemAt(itemPosition)
|
44 |
sym = self.getSymbolByItemName(item, 0) |
45 |
text = item.text(0)
|
46 |
if len(indexes) > 0: |
47 |
level = 0
|
48 |
index = indexes[0]
|
49 |
while index.parent().isValid():
|
50 |
index = index.parent() |
51 |
level += 1
|
52 |
if sym is not None: |
53 |
menu = QMenu() |
54 |
editSymbolAction = QAction(self.tr("Edit Symbol")) |
55 |
editSymbolAction.triggered.connect(lambda: self.editSymbolActionClickEvent(item, 0)) |
56 |
menu.addAction(editSymbolAction) |
57 |
displaySymbolAction = QAction(self.tr("Display Symbol")) |
58 |
displaySymbolAction.triggered.connect(lambda: self.displaySymbolActionClickEvent(sym.getType(), text)) |
59 |
menu.addAction(displaySymbolAction) |
60 |
deleteSymbolAction = QAction(self.tr("Delete Symbol")) |
61 |
deleteSymbolAction.triggered.connect(lambda: self.deleteSymbolActionClickEvent(sym.getType(), text)) |
62 |
menu.addAction(deleteSymbolAction) |
63 |
menu.exec_(self.viewport().mapToGlobal(position))
|
64 | |
65 |
def editSymbolActionClickEvent(self, item, columNo): |
66 |
self.showSymbolEditorDialog(item, columNo)
|
67 | |
68 |
def displaySymbolActionClickEvent(self, itemType, itemName): |
69 |
project = AppDocData.instance().getCurrentProject() |
70 |
image = QImage(project.getImageFilePath()+"/"+itemType+"/"+itemName, "PNG") #itemName includes ".png" |
71 |
dialog = QSymbolDisplayDialog.QSymbolDisplayDialog(image) |
72 |
dialog.showDialog() |
73 | |
74 |
def deleteSymbolActionClickEvent(self, itemType, itemName): |
75 |
msg = QMessageBox() |
76 |
msg.setIcon(QMessageBox.Critical) |
77 |
msg.setText("선택한 심볼을 삭제하시겠습니까?\n삭제된 심볼은 복구할 수 없습니다.")
|
78 |
msg.setWindowTitle("심볼 삭제")
|
79 |
msg.setStandardButtons(QMessageBox.Ok|QMessageBox.Cancel) |
80 |
#msg.buttonClicked.connect(lambda: self.handleDeleteSymbolAction(itemName))
|
81 |
result = msg.exec_() |
82 |
self.handleDeleteSymbolAction(result, itemType, itemName)
|
83 | |
84 |
'''
|
85 |
@history 2018.05.03 Jeongwoo Modify file path with ".png" and ".svg"
|
86 |
Use project object when making svgPath
|
87 |
'''
|
88 |
def handleDeleteSymbolAction(self, result, itemType, itemName): |
89 |
print("handle")
|
90 |
if result == QMessageBox.Ok:
|
91 |
project = AppDocData.instance().getCurrentProject() |
92 |
imagePath = project.getImageFilePath() + "/" + itemType + "/" + itemName + ".png" # itemName DOESN'T includes ".png" |
93 |
if os.path.exists(imagePath):
|
94 |
os.remove(imagePath) |
95 | |
96 |
svgPath = project.getSvgFilePath() + "/" + itemType + "/" + itemName + ".svg" |
97 |
if os.path.exists(svgPath):
|
98 |
os.remove(svgPath) |
99 | |
100 |
AppDocData.instance().deleteSymbol(itemName) |
101 |
self.initDirTreeWidget()
|
102 |
else:
|
103 |
pass
|
104 | |
105 |
'''
|
106 |
@history 2018.05.02 Jeongwoo Change return value of QSymbolEditorDialog (Single variable → Tuple)
|
107 |
'''
|
108 |
def initDirTreeWidget(self): |
109 |
project = AppDocData.instance().getCurrentProject() |
110 |
if project is not None: |
111 |
self.clear()
|
112 |
print("Project : " + project.getName())
|
113 |
projectPath = project.getPath().replace("\\", "/") |
114 |
self.makeChildDir()
|
115 |
self.loadSymbolInfo()
|
116 |
#self.loadDirectoryInfo(project.getImageFilePath(), self)
|
117 |
self.expandAll()
|
118 | |
119 |
'''
|
120 |
@brief Load Symbol Info and add TreeItem with DB
|
121 |
@author Jeongwoo
|
122 |
@date 18.04.20
|
123 |
@history 2018.05.03 Jeongwoo Get Svg File Path by SymbolBase.getSvgFileFullPath()
|
124 |
'''
|
125 |
def loadSymbolInfo(self): |
126 |
symbolTypeList = AppDocData.instance().getSymbolTypeList() |
127 |
for symbolType in symbolTypeList: |
128 |
parent = QTreeWidgetItem(self, [symbolType])
|
129 |
symbolList = AppDocData.instance().getSymbolListByQuery('type', symbolType)
|
130 |
#symbolNameList = AppDocData.instance().getSymbolNameListByType(symbolType)
|
131 |
for symbol in symbolList: |
132 |
symbolItem = QTreeWidgetItem(parent, [symbol.getName()]) |
133 |
symbolItem.setData(0, self.TREE_DATA_ROLE, symbol) ## ADD DATA |
134 |
svgPath = symbol.getSvgFileFullPath() |
135 |
icon = QIcon(svgPath) |
136 |
symbolItem.setIcon(0, icon)
|
137 |
symbolItem.svgFilePath = svgPath # save svg file path
|
138 | |
139 | |
140 |
'''
|
141 |
@brief Make Directory
|
142 |
@author Jeongwoo
|
143 |
@date 18.04.??
|
144 |
@history 18.04.12 Jeongwoo Add output, temp Directory
|
145 |
'''
|
146 |
def makeChildDir(self): |
147 |
project = AppDocData.instance().getCurrentProject() |
148 |
dbDir = project.getDbFilePath() |
149 |
if not os.path.exists(dbDir): |
150 |
os.makedirs(dbDir) |
151 |
imgDir = project.getImageFilePath() |
152 |
if not os.path.exists(imgDir): |
153 |
os.makedirs(imgDir) |
154 |
svgDir = project.getSvgFilePath() |
155 |
if not os.path.exists(svgDir): |
156 |
os.makedirs(svgDir) |
157 |
outputDir = project.getOutputPath() |
158 |
if not os.path.exists(outputDir): |
159 |
os.makedirs(outputDir) |
160 |
tempDir = project.getTempPath() |
161 |
if not os.path.exists(tempDir): |
162 |
os.makedirs(tempDir) |
163 | |
164 |
#def loadDirectoryInfo(self, startPath, tree):
|
165 |
# for element in os.listdir(startPath):
|
166 |
# pathInfo = os.path.join(startPath, element)
|
167 |
# parentItem = QTreeWidgetItem(tree, [os.path.basename(element)])
|
168 |
# if pathInfo.endswith(".png"):
|
169 |
# icon = QIcon(pathInfo)
|
170 |
# parentItem.setIcon(0, icon)
|
171 |
# parentItem.svgFilePath = pathInfo # save svg file path
|
172 |
# if os.path.isdir(pathInfo):
|
173 |
# self.loadDirectoryInfo(pathInfo, parentItem)
|
174 | |
175 |
def showSymbolEditorDialog(self, item, columnNo): |
176 |
try:
|
177 |
sym = self.getSymbolByItemName(item, columnNo)
|
178 |
if sym is not None: |
179 |
path = sym.getPath() |
180 |
image = QImage(path, "PNG")
|
181 |
print("after image")
|
182 |
symbolEditorDialog = QSymbolEditorDialog.QSymbolEditorDialog(self, image, AppDocData.instance().getCurrentProject(), sym)
|
183 |
(isAccepted, isImmediateInsert, offsetX, offsetY, newSym) = symbolEditorDialog.showDialog() |
184 |
self.initDirTreeWidget()
|
185 |
else:
|
186 |
QMessageBox.about(self, "알림", "심볼 데이터를 불러오는 중 에러가 발생했습니다.") |
187 |
except Exception as ex: |
188 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
189 | |
190 | |
191 |
def itemDoubleClickEvent(self, item, columnNo): |
192 |
self.isDoubleClicked = True |
193 |
sym = self.getSymbolByItemName(item, columnNo)
|
194 |
itemName = item.text(columnNo) |
195 |
print(itemName + " : Double Clicked")
|
196 |
if sym is not None: |
197 |
self.showSymbolEditorDialog(item, columnNo)
|
198 |
self.isDoubleClicked = False |
199 | |
200 |
'''
|
201 |
@history 18.04.24 Jeongwoo Add case of symbol is None type
|
202 |
Make Dummy Symbol (symId = -1, name = None, type = None)
|
203 |
'''
|
204 |
def itemClickEvent(self, item, columnNo): |
205 |
if self.isDoubleClicked == False: |
206 |
print(item.text(columnNo) + " : Single Clicked")
|
207 |
sym = self.getSymbolByItemName(item, columnNo)
|
208 |
if sym is not None: |
209 |
self.singleClicked.emit(sym)
|
210 |
else:
|
211 |
self.singleClicked.emit(SymbolBase.SymbolBase(-1, None, None)) # Empty SymbolBase |
212 | |
213 |
'''
|
214 |
@brief Get Symbol data by symbol name
|
215 |
@author Jeongwoo
|
216 |
@date 18.04.20
|
217 |
'''
|
218 |
def getSymbolByItemName(self, item, columnNo): |
219 |
tmpItem = item |
220 |
itemName = item.text(columnNo) |
221 |
#if itemName.lower().endswith(".png"):
|
222 |
#path = itemName
|
223 |
#while tmpItem.parent() is not None:
|
224 |
# path = tmpItem.parent().text(columnNo) + "/" + path
|
225 |
# tmpItem = tmpItem.parent()
|
226 |
#fullPath = AppDocData.instance().getCurrentProject().getImageFilePath() + "/" + path # path includes ".png"
|
227 |
|
228 |
#name = itemName.replace(".png", "")
|
229 |
name = itemName |
230 |
sym = AppDocData.instance().getSymbolByQuery("name", name)
|
231 |
return sym
|
232 |
#else:
|
233 |
# return None
|
234 | |
235 |
'''
|
236 |
@brief start drag
|
237 |
@author humkyung
|
238 |
@date 2018.04.17
|
239 |
@history 18.04.20 Jeongwoo Change Path in QPixmap
|
240 |
18.06.21 Jeongwoo Casting string to float and int
|
241 |
'''
|
242 |
def startDrag(self, dropAction): |
243 |
try:
|
244 |
items = self.selectedItems()
|
245 |
if items:
|
246 |
symData = items[0].data(0, self.TREE_DATA_ROLE) |
247 |
pixmap = QPixmap(items[0].svgFilePath)
|
248 | |
249 |
mime = QMimeData() |
250 |
mime.setText(symData.getName()) |
251 | |
252 |
drag = QDrag(self)
|
253 |
drag.setMimeData(mime) |
254 |
originalPoint = symData.getOriginalPoint() |
255 |
drag.setHotSpot(QPoint(int(float(originalPoint.split(",")[0])), int(float(originalPoint.split(",")[1])))) |
256 |
drag.setPixmap(pixmap) |
257 |
drag.exec(Qt.CopyAction) |
258 |
except Exception as ex: |
259 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |