Ejemplo n.º 1
0
    def __init__(self, xmlFile=None, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.options = {
            'showAttributeTable' : False,
            'showComments' : True,
            }
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        if not self.options['showAttributeTable']:
            self.ui.attributeTable.hide()
        self.statusLabel = QtGui.QLabel()
        self.statusBar().addWidget(self.statusLabel)
        self.modified = False
        if debug: print 'connect ...'
        self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'),
                     self.openFile)
        self.connect(self.ui.actionSave, QtCore.SIGNAL('triggered()'),
                     self.saveFile)
        self.connect(self.ui.actionSave_As, QtCore.SIGNAL('triggered()'),
                     self.saveFileAs)
        self.connect(self.ui.actionQuit, QtCore.SIGNAL('triggered()'),
                     self.close)
        self.connect(self.ui.actionDuplicate, QtCore.SIGNAL('triggered()'),
                     self.duplicateSelected)
        self.connect(self.ui.actionCut, QtCore.SIGNAL('triggered()'),
                     self.cutSelected)
        self.connect(self.ui.actionCopy, QtCore.SIGNAL('triggered()'),
                     self.copySelected)
        self.connect(self.ui.actionPaste, QtCore.SIGNAL('triggered()'),
                     self.paste)
        self.connect(self.ui.actionDelete, QtCore.SIGNAL('triggered()'),
                     self.removeSelected)

        self.connect(self.ui.treeView, QtCore.SIGNAL('clicked(QModelIndex)'),
                     self.updateAttributeInfo)
        self.connect(self.ui.attributeTable,
                     QtCore.SIGNAL('cellChanged(int,int)'),
                     self.updateAttribute)
        self.connect(self.ui.actionShow_attributes,
                     QtCore.SIGNAL('triggered()'),
                     self.showAttributeTable)
        self.connect(self.ui.actionShow_comments,
                     QtCore.SIGNAL('triggered()'),
                     self.showComments)
        self.setWindowTitle(self.getCommandName())
        if debug: print 'load XML ...'
        if xmlFile is not None:
            self.loadXML(xmlFile)
        if debug: print 'XML loaded !'
        self.tmpNode = None #Used for copy/cut/paste

        self.ui.actionShow_comments.setChecked(self.options['showComments'])
        if debug: print 'create item delegate ...'
        delegate = XMLEditorDelegate(self)
        if debug: print 'setItemDelegate ...'
        self.ui.treeView.setItemDelegate(delegate)
Ejemplo n.º 2
0
class DomViewer(QtGui.QMainWindow):

    def __init__(self, xmlFile=None, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.options = {
            'showAttributeTable' : False,
            'showComments' : True,
            }
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        if not self.options['showAttributeTable']:
            self.ui.attributeTable.hide()
        self.statusLabel = QtGui.QLabel()
        self.statusBar().addWidget(self.statusLabel)
        self.modified = False
        if debug: print 'connect ...'
        self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'),
                     self.openFile)
        self.connect(self.ui.actionSave, QtCore.SIGNAL('triggered()'),
                     self.saveFile)
        self.connect(self.ui.actionSave_As, QtCore.SIGNAL('triggered()'),
                     self.saveFileAs)
        self.connect(self.ui.actionQuit, QtCore.SIGNAL('triggered()'),
                     self.close)
        self.connect(self.ui.actionDuplicate, QtCore.SIGNAL('triggered()'),
                     self.duplicateSelected)
        self.connect(self.ui.actionCut, QtCore.SIGNAL('triggered()'),
                     self.cutSelected)
        self.connect(self.ui.actionCopy, QtCore.SIGNAL('triggered()'),
                     self.copySelected)
        self.connect(self.ui.actionPaste, QtCore.SIGNAL('triggered()'),
                     self.paste)
        self.connect(self.ui.actionDelete, QtCore.SIGNAL('triggered()'),
                     self.removeSelected)

        self.connect(self.ui.treeView, QtCore.SIGNAL('clicked(QModelIndex)'),
                     self.updateAttributeInfo)
        self.connect(self.ui.attributeTable,
                     QtCore.SIGNAL('cellChanged(int,int)'),
                     self.updateAttribute)
        self.connect(self.ui.actionShow_attributes,
                     QtCore.SIGNAL('triggered()'),
                     self.showAttributeTable)
        self.connect(self.ui.actionShow_comments,
                     QtCore.SIGNAL('triggered()'),
                     self.showComments)
        self.setWindowTitle(self.getCommandName())
        if debug: print 'load XML ...'
        if xmlFile is not None:
            self.loadXML(xmlFile)
        if debug: print 'XML loaded !'
        self.tmpNode = None #Used for copy/cut/paste

        self.ui.actionShow_comments.setChecked(self.options['showComments'])
        if debug: print 'create item delegate ...'
        delegate = XMLEditorDelegate(self)
        if debug: print 'setItemDelegate ...'
        self.ui.treeView.setItemDelegate(delegate)

    def openFile(self):
        fileName = str(QFileDialog.getOpenFileName(self, 'Open File ...', './',
                                                   self.tr('XML (*.xml *.html)'),
                                                   ))
        self.loadXML(fileName)

    def loadXML(self, fn):
        try:
            f = open(fn)
            content = f.read()
            f.close()
        except Exception, e:
            QtGui.QMessageBox.critical(self,self.getCommandName(),
                                       "Opening of " + fn + " failed!\n" \
                                           "Error was :\n" + e.message)
            return False
        if debug: print 'Creating DOM doc ...'
        self.domDoc = QtXml.QDomDocument() # Maybe use SAX here ?
        if debug: print 'DOM doc created!'
        (loadSuccess, errMsg, errLine, errCol) = self.domDoc.setContent(content)
        if not loadSuccess:
            msg = "XML parse in " + fn + " failed!\n" \
                "Error at line %d, column %d:\n" %(errLine, errCol) \
                + errMsg
            QtGui.QMessageBox.critical(self,self.getCommandName(),msg)
            return False
        self.fileName = fn
        if debug: print 'Creating DomModel ...'
        self.domModel = DomModel(self.domDoc, self)
        if debug: print 'DomModel created!'
        self.domModel.showComments(self.options['showComments'])
        if debug: print 'Setting model',
        self.ui.treeView.setModel(self.domModel)
        if debug: print 'Done !'
        self.ui.treeView.setColumnHidden(1, True)
        if debug: print 'Expanding ...'
        self.ui.treeView.expandAll()
        if debug: print 'Resize ...'
        self.ui.treeView.resizeColumnToContents(0)
        if debug: print 'Connect ...'
        self.connect(self.domModel,
                     QtCore.SIGNAL('dataChanged(QModelIndex, QModelIndex)'),
                     self.setModifiedState)
        self.setFileState(modified=False)
        return True