|
| 1 | +from PyQt4.QtCore import * |
| 2 | +from PyQt4.QtGui import * |
| 3 | +from PyQt4 import QtCore, QtGui, QtWebKit |
| 4 | +import os |
| 5 | +import pickle |
| 6 | + |
| 7 | +class HelpEditionDialog(QtGui.QDialog): |
| 8 | + |
| 9 | + ALG_DESC = "ALG_DESC" |
| 10 | + ALG_CREATOR = "ALG_CREATOR" |
| 11 | + ALG_HELP_CREATOR = "ALG_HELP_CREATOR" |
| 12 | + |
| 13 | + def __init__(self, alg): |
| 14 | + self.alg = alg |
| 15 | + QtGui.QDialog.__init__(self) |
| 16 | + self.setModal(True) |
| 17 | + self.descriptions = {} |
| 18 | + if self.alg.descriptionFile: |
| 19 | + helpfile = alg.descriptionFile + ".help" |
| 20 | + if os.path.exists(helpfile): |
| 21 | + f = open(helpfile, "rb") |
| 22 | + self.descriptions = pickle.load(f) |
| 23 | + self.currentName = self.ALG_DESC |
| 24 | + self.setupUi() |
| 25 | + |
| 26 | + def setupUi(self): |
| 27 | + self.resize(700, 500) |
| 28 | + self.tree = QtGui.QTreeWidget() |
| 29 | + self.tree.setHeaderHidden(True) |
| 30 | + self.tree.setMinimumWidth(300) |
| 31 | + QObject.connect(self.tree, QtCore.SIGNAL("itemClicked(QTreeWidgetItem*, int)"), self.changeItem) |
| 32 | + self.groupIcon = QtGui.QIcon() |
| 33 | + self.groupIcon.addPixmap(self.style().standardPixmap(QtGui.QStyle.SP_DirClosedIcon), |
| 34 | + QtGui.QIcon.Normal, QtGui.QIcon.Off) |
| 35 | + self.groupIcon.addPixmap(self.style().standardPixmap(QtGui.QStyle.SP_DirOpenIcon), |
| 36 | + QtGui.QIcon.Normal, QtGui.QIcon.On) |
| 37 | + self.keyIcon = QtGui.QIcon() |
| 38 | + self.keyIcon.addPixmap(self.style().standardPixmap(QtGui.QStyle.SP_FileIcon)) |
| 39 | + self.fillTree() |
| 40 | + self.setWindowTitle("Help editor") |
| 41 | + self.horizontalLayout= QtGui.QHBoxLayout() |
| 42 | + self.horizontalLayout.setSpacing(15) |
| 43 | + self.horizontalLayout.setMargin(0) |
| 44 | + self.label = QtGui.QLabel() |
| 45 | + self.label.setText("Select elements on the tree and fill their description in the text box below") |
| 46 | + self.labelName = QtGui.QLabel() |
| 47 | + self.labelName.setText("Algorithm description") |
| 48 | + self.text = QtGui.QTextEdit() |
| 49 | + self.text.setMinimumHeight(200) |
| 50 | + self.verticalLayout= QtGui.QVBoxLayout() |
| 51 | + self.verticalLayout.setSpacing(5) |
| 52 | + self.verticalLayout.setMargin(0) |
| 53 | + self.verticalLayout.addWidget(self.tree) |
| 54 | + self.verticalLayout.addSpacing(20) |
| 55 | + self.verticalLayout.addWidget(self.label) |
| 56 | + self.verticalLayout.addSpacing(20) |
| 57 | + self.verticalLayout.addWidget(self.labelName) |
| 58 | + self.verticalLayout.addWidget(self.text) |
| 59 | + self.horizontalLayout.addLayout(self.verticalLayout) |
| 60 | + self.webView = QtWebKit.QWebView() |
| 61 | + self.webView.setMinimumWidth(300) |
| 62 | + self.webView.setHtml(self.getHtml()) |
| 63 | + self.horizontalLayout.addWidget(self.webView) |
| 64 | + self.closeButton = QtGui.QPushButton() |
| 65 | + self.closeButton.setText("Cancel") |
| 66 | + self.saveButton = QtGui.QPushButton() |
| 67 | + self.saveButton.setText("OK") |
| 68 | + self.horizontalLayout2= QtGui.QHBoxLayout() |
| 69 | + self.horizontalLayout2.setSpacing(2) |
| 70 | + self.horizontalLayout2.setMargin(0) |
| 71 | + self.horizontalLayout2.addStretch(1000) |
| 72 | + self.horizontalLayout2.addWidget(self.saveButton) |
| 73 | + self.horizontalLayout2.addWidget(self.closeButton) |
| 74 | + QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.closeWindow) |
| 75 | + QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveHelp) |
| 76 | + self.verticalLayout2= QtGui.QVBoxLayout() |
| 77 | + self.verticalLayout2.setSpacing(2) |
| 78 | + self.verticalLayout2.setMargin(0) |
| 79 | + self.verticalLayout2.addLayout(self.horizontalLayout) |
| 80 | + self.verticalLayout2.addLayout(self.horizontalLayout2) |
| 81 | + self.setLayout(self.verticalLayout2) |
| 82 | + QtCore.QMetaObject.connectSlotsByName(self) |
| 83 | + self.updateHtmlView() |
| 84 | + |
| 85 | + def closeWindow(self): |
| 86 | + self.descriptions = None |
| 87 | + self.close() |
| 88 | + |
| 89 | + def saveHelp(self): |
| 90 | + self.descriptions[self.currentName] = str(self.text.toPlainText()) |
| 91 | + if self.alg.descriptionFile: |
| 92 | + f = open(self.alg.descriptionFile + ".help", "wb") |
| 93 | + pickle.dump(self.descriptions, f) |
| 94 | + f.close() |
| 95 | + self.close() |
| 96 | + |
| 97 | + def getHtml(self): |
| 98 | + s = "<h2>Algorithm description</h2>\n" |
| 99 | + s += "<p>" + self.getDescription(self.ALG_DESC) + "</p>\n" |
| 100 | + s += "<h2>Input parameters</h2>\n" |
| 101 | + for param in self.alg.parameters: |
| 102 | + s += "<h3>" + param.description + "</h3>\n" |
| 103 | + s += "<p>" + self.getDescription(param.name) + "</p>\n" |
| 104 | + s += "<h2>Outputs</h2>\n" |
| 105 | + for out in self.alg.outputs: |
| 106 | + s += "<h3>" + out.description + "</h3>\n" |
| 107 | + s += "<p>" + self.getDescription(out.name) + "</p>\n" |
| 108 | + return s |
| 109 | + |
| 110 | + def fillTree(self): |
| 111 | + item = TreeDescriptionItem("Algorithm description", self.ALG_DESC) |
| 112 | + self.tree.addTopLevelItem(item) |
| 113 | + parametersItem = TreeDescriptionItem("Input parameters", None) |
| 114 | + self.tree.addTopLevelItem(parametersItem) |
| 115 | + for param in self.alg.parameters: |
| 116 | + item = TreeDescriptionItem(param.description, param.name) |
| 117 | + parametersItem.addChild(item) |
| 118 | + outputsItem = TreeDescriptionItem("Outputs", None) |
| 119 | + self.tree.addTopLevelItem(outputsItem) |
| 120 | + for out in self.alg.outputs: |
| 121 | + item = TreeDescriptionItem(out.description, out.name) |
| 122 | + outputsItem.addChild(item) |
| 123 | + item = TreeDescriptionItem("Algorithm created by", self.ALG_CREATOR) |
| 124 | + self.tree.addTopLevelItem(item) |
| 125 | + item = TreeDescriptionItem("Algorithm help written by", self.ALG_HELP_CREATOR) |
| 126 | + self.tree.addTopLevelItem(item) |
| 127 | + |
| 128 | + def changeItem(self): |
| 129 | + item = self.tree.currentItem() |
| 130 | + if isinstance(item, TreeDescriptionItem): |
| 131 | + if self.currentName: |
| 132 | + self.descriptions[self.currentName] = str(self.text.toPlainText()) |
| 133 | + name = item.name |
| 134 | + if name: |
| 135 | + self.updateHtmlView() |
| 136 | + self.currentName = name |
| 137 | + self.labelName.setText(item.description) |
| 138 | + if name in self.descriptions: |
| 139 | + self.text.setText(self.descriptions[name]) |
| 140 | + else: |
| 141 | + self.text.setText("") |
| 142 | + |
| 143 | + def updateHtmlView(self): |
| 144 | + self.webView.setHtml(self.getHtml()) |
| 145 | + |
| 146 | + def getDescription(self, name): |
| 147 | + if name in self.descriptions : |
| 148 | + return self.descriptions[name] |
| 149 | + else: |
| 150 | + return "" |
| 151 | + |
| 152 | +class TreeDescriptionItem(QtGui.QTreeWidgetItem): |
| 153 | + def __init__(self, description, name): |
| 154 | + QTreeWidgetItem.__init__(self) |
| 155 | + self.name = name |
| 156 | + self.description = description |
| 157 | + self.setText(0, description) |
0 commit comments