def __init__(self, parent=None, webconf=None): super(WebconfForm, self).__init__(parent, acceptDrops=True) self._qui = Ui_WebconfForm() self._qui.setupUi(self) self._initicons() self._qui.path_edit.currentIndexChanged.connect(self._updateview) self._qui.path_edit.currentIndexChanged.connect(self._updateform) self._qui.add_button.clicked.connect(self._addpathmap) self.setwebconf(webconf or wconfig.config()) self._updateform()
class WebconfForm(QWidget): """Widget to show/edit webconf""" def __init__(self, parent=None, webconf=None): super(WebconfForm, self).__init__(parent, acceptDrops=True) self._qui = Ui_WebconfForm() self._qui.setupUi(self) self._initicons() self._qui.path_edit.currentIndexChanged.connect(self._updateview) self._qui.path_edit.currentIndexChanged.connect(self._updateform) self._qui.add_button.clicked.connect(self._addpathmap) self.setwebconf(webconf or wconfig.config()) self._updateform() def _initicons(self): def setstdicon(w, name): w.setIcon(self.style().standardIcon(name)) setstdicon(self._qui.open_button, QStyle.SP_DialogOpenButton) setstdicon(self._qui.save_button, QStyle.SP_DialogSaveButton) self._qui.add_button.setIcon(qtlib.geticon('fileadd')) self._qui.edit_button.setIcon(qtlib.geticon('edit-file')) self._qui.remove_button.setIcon(qtlib.geticon('filedelete')) def dragEnterEvent(self, event): if self._getlocalpath_from_dropevent(event): event.setDropAction(Qt.LinkAction) event.accept() def dropEvent(self, event): localpath = self._getlocalpath_from_dropevent(event) if localpath: event.setDropAction(Qt.LinkAction) event.accept() self._addpathmap(localpath=localpath) @staticmethod def _getlocalpath_from_dropevent(event): m = event.mimeData() if m.hasFormat('text/uri-list') and len(m.urls()) == 1: return unicode(m.urls()[0].toLocalFile()) def setwebconf(self, webconf): """set current webconf object""" path = hglib.tounicode(getattr(webconf, 'path', None) or '') i = self._qui.path_edit.findText(path) if i < 0: i = 0 self._qui.path_edit.insertItem(i, path, webconf) self._qui.path_edit.setCurrentIndex(i) @property def webconf(self): """current webconf object""" def curconf(w): i = w.currentIndex() _path, conf = unicode(w.itemText(i)), w.itemData(i).toPyObject() return conf return curconf(self._qui.path_edit) @property def _webconfmodel(self): """current model object of webconf""" return self._qui.repos_view.model() @pyqtSlot() def _updateview(self): m = WebconfModel(config=self.webconf, parent=self) self._qui.repos_view.setModel(m) self._qui.repos_view.selectionModel().currentChanged.connect( self._updateform) def _updateform(self): """Update availability of each widget""" self._qui.repos_view.setEnabled(hasattr(self.webconf, 'write')) self._qui.add_button.setEnabled(hasattr(self.webconf, 'write')) self._qui.edit_button.setEnabled( hasattr(self.webconf, 'write') and self._qui.repos_view.currentIndex().isValid()) self._qui.remove_button.setEnabled( hasattr(self.webconf, 'write') and self._qui.repos_view.currentIndex().isValid()) @pyqtSlot() def on_open_button_clicked(self): path = QFileDialog.getOpenFileName( self, _('Open hgweb config'), getattr(self.webconf, 'path', None) or '', _FILE_FILTER) if path: self.openwebconf(path) def openwebconf(self, path): """load the specified webconf file""" path = hglib.fromunicode(path) c = wconfig.readfile(path) c.path = os.path.abspath(path) self.setwebconf(c) @pyqtSlot() def on_save_button_clicked(self): path = QFileDialog.getSaveFileName( self, _('Save hgweb config'), getattr(self.webconf, 'path', None) or '', _FILE_FILTER) if path: self.savewebconf(path) def savewebconf(self, path): """save current webconf to the specified file""" path = hglib.fromunicode(path) wconfig.writefile(self.webconf, path) self.openwebconf(path) # reopen in case file path changed @pyqtSlot() def _addpathmap(self, path=None, localpath=None): path, localpath = _PathDialog.getaddpathmap( self, path=path, localpath=localpath, invalidpaths=self._webconfmodel.paths) if path: self._webconfmodel.addpathmap(path, localpath) @pyqtSlot() def on_edit_button_clicked(self): self.on_repos_view_doubleClicked(self._qui.repos_view.currentIndex()) @pyqtSlot(QModelIndex) def on_repos_view_doubleClicked(self, index): assert index.isValid() origpath, origlocalpath = self._webconfmodel.getpathmapat(index.row()) path, localpath = _PathDialog.geteditpathmap( self, path=origpath, localpath=origlocalpath, invalidpaths=set(self._webconfmodel.paths) - set([origpath])) if not path: return if path != origpath: # we cannot change config key without reordering self._webconfmodel.removepathmap(origpath) self._webconfmodel.addpathmap(path, localpath) else: self._webconfmodel.setpathmap(path, localpath) @pyqtSlot() def on_remove_button_clicked(self): index = self._qui.repos_view.currentIndex() assert index.isValid() path, _localpath = self._webconfmodel.getpathmapat(index.row()) self._webconfmodel.removepathmap(path)