hytos / DTI_PID / DTI_PID / QDirTreeWidget.py @ bcdd2a76
이력 | 보기 | 이력해설 | 다운로드 (6.21 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 SymbolBase |
14 |
import symbol |
15 |
import QSymbolEditorDialog |
16 |
import QSymbolDisplayDialog |
17 |
|
18 |
class QDirTreeWidget(QTreeWidget): |
19 |
#Add signal
|
20 |
singleClicked = pyqtSignal(SymbolBase.SymbolBase) |
21 |
|
22 |
def __init__(self): |
23 |
QTreeWidget.__init__(self)
|
24 |
self.initDirTreeWidget()
|
25 |
self.isDoubleClicked = False |
26 |
self.itemDoubleClicked.connect(self.itemDoubleClickEvent) |
27 |
self.itemClicked.connect(self.itemClickEvent) |
28 |
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
29 |
self.customContextMenuRequested.connect(self.openContextMenu) |
30 |
|
31 |
def openContextMenu(self, position): |
32 |
indexes = self.selectedIndexes()
|
33 |
itemPosition = self.mapTo(self, position) |
34 |
item = self.itemAt(itemPosition)
|
35 |
text = item.text(0)
|
36 |
if len(indexes) > 0: |
37 |
level = 0
|
38 |
index = indexes[0]
|
39 |
while index.parent().isValid():
|
40 |
index = index.parent() |
41 |
level += 1
|
42 |
if text.lower().endswith(".png"): |
43 |
menu = QMenu() |
44 |
editSymbolAction = QAction(self.tr("Edit Symbol")) |
45 |
editSymbolAction.triggered.connect(lambda: self.editSymbolActionClickEvent(item, 0)) |
46 |
menu.addAction(editSymbolAction) |
47 |
displaySymbolAction = QAction(self.tr("Display Symbol")) |
48 |
displaySymbolAction.triggered.connect(lambda: self.displaySymbolActionClickEvent(text)) |
49 |
menu.addAction(displaySymbolAction) |
50 |
deleteSymbolAction = QAction(self.tr("Delete Symbol")) |
51 |
deleteSymbolAction.triggered.connect(lambda: self.deleteSymbolActionClickEvent(text)) |
52 |
menu.addAction(deleteSymbolAction) |
53 |
menu.exec_(self.viewport().mapToGlobal(position))
|
54 |
|
55 |
def editSymbolActionClickEvent(self, item, columNo): |
56 |
self.showSymbolEditorDialog(item, columNo)
|
57 |
|
58 |
def displaySymbolActionClickEvent(self, itemName): |
59 |
project = AppDocData.instance().getCurrentProject() |
60 |
image = QImage(project.getPath()+"/image/"+itemName, "PNG") |
61 |
dialog = QSymbolDisplayDialog.QSymbolDisplayDialog(image) |
62 |
dialog.showDialog() |
63 |
|
64 |
def deleteSymbolActionClickEvent(self, itemName): |
65 |
msg = QMessageBox() |
66 |
msg.setIcon(QMessageBox.Critical) |
67 |
msg.setText("선택한 심볼을 삭제하시겠습니까?\n삭제된 심볼은 복구할 수 없습니다.")
|
68 |
msg.setWindowTitle("심볼 삭제")
|
69 |
msg.setStandardButtons(QMessageBox.Ok|QMessageBox.Cancel) |
70 |
#msg.buttonClicked.connect(lambda: self.handleDeleteSymbolAction(itemName))
|
71 |
result = msg.exec_() |
72 |
self.handleDeleteSymbolAction(result, itemName)
|
73 |
|
74 |
def handleDeleteSymbolAction(self, result, itemName): |
75 |
print("handle")
|
76 |
if result == QMessageBox.Ok:
|
77 |
project = AppDocData.instance().getCurrentProject() |
78 |
imagePath = project.getPath() + "/image/" + itemName
|
79 |
if os.path.exists(imagePath):
|
80 |
os.remove(imagePath) |
81 |
|
82 |
svgPath = project.getPath() + "/svg/" + itemName.replace(".png", ".svg") |
83 |
if os.path.exists(svgPath):
|
84 |
os.remove(svgPath) |
85 |
|
86 |
AppDocData.instance().deleteSymbol(imagePath) |
87 |
self.initDirTreeWidget()
|
88 |
else:
|
89 |
pass
|
90 |
|
91 |
def initDirTreeWidget(self): |
92 |
project = AppDocData.instance().getCurrentProject() |
93 |
if project is not None: |
94 |
self.clear()
|
95 |
print("Project : " + project.getName())
|
96 |
projectPath = project.getPath().replace("\\", "/") |
97 |
self.makeChildDir(projectPath)
|
98 |
self.loadDirectoryInfo(projectPath+"/image", self) |
99 |
|
100 |
def makeChildDir(self, startPath): |
101 |
dbDir = startPath+"/db"
|
102 |
if not os.path.exists(dbDir): |
103 |
os.makedirs(dbDir) |
104 |
imgDir = startPath+"/image"
|
105 |
if not os.path.exists(imgDir): |
106 |
os.makedirs(imgDir) |
107 |
svgDir = startPath+"/svg"
|
108 |
if not os.path.exists(svgDir): |
109 |
os.makedirs(svgDir) |
110 |
|
111 |
def loadDirectoryInfo(self, startPath, tree): |
112 |
for element in os.listdir(startPath): |
113 |
pathInfo = startPath + "/" + element
|
114 |
parentItem = QTreeWidgetItem(tree, [os.path.basename(element)]) |
115 |
if os.path.isdir(pathInfo):
|
116 |
self.loadDirectoryInfo(pathInfo, parentItem)
|
117 |
|
118 |
def showSymbolEditorDialog(self, item, columnNo): |
119 |
sym = self.getSymbolByItemName(item, columnNo)
|
120 |
if sym is not None: |
121 |
image = QImage(sym.getPath(), "PNG")
|
122 |
print("after image")
|
123 |
symbolEditorDialog = QSymbolEditorDialog.QSymbolEditorDialog(self, image, AppDocData.instance().getCurrentProject(), sym)
|
124 |
_ret = symbolEditorDialog.showDialog() |
125 |
self.initDirTreeWidget()
|
126 |
|
127 |
def itemDoubleClickEvent(self, item, columnNo): |
128 |
self.isDoubleClicked = True |
129 |
itemName = item.text(columnNo) |
130 |
print(itemName + " : Double Clicked")
|
131 |
self.showSymbolEditorDialog(item, columnNo)
|
132 |
self.isDoubleClicked = False |
133 |
|
134 |
def itemClickEvent(self, item, columnNo): |
135 |
if self.isDoubleClicked == False: |
136 |
print(item.text(columnNo) + " : Single Clicked")
|
137 |
sym = self.getSymbolByItemName(item, columnNo)
|
138 |
if sym is not None: |
139 |
self.singleClicked.emit(sym)
|
140 |
else:
|
141 |
QMessageBox.about(self.ui.buttonBox, "알림", "심볼 데이터를 불러오는 중 에러가 발생했습니다.") |
142 |
|
143 |
def getSymbolByItemName(self, item, columnNo): |
144 |
tmpItem = item |
145 |
itemName = item.text(columnNo) |
146 |
if itemName.lower().endswith(".png"): |
147 |
path = itemName |
148 |
while tmpItem.parent() is not None: |
149 |
path = tmpItem.parent().text(columnNo) + "/" + path
|
150 |
tmpItem = tmpItem.parent() |
151 |
fullPath = AppDocData.instance().getCurrentProject().getPath() + "/image/" + path
|
152 |
|
153 |
sym = AppDocData.instance().getSymbolByQuery("path", fullPath)
|
154 |
return sym
|
155 |
else:
|
156 |
return None |