From 2637ad389e085769bf193b93480e2b1db2147382 Mon Sep 17 00:00:00 2001 From: AsgerPetersen Date: Wed, 3 Feb 2016 14:41:57 +0100 Subject: [PATCH] Warn user and avoid crash when trying to add qlr which does not exist Fixes #18 --- src/QlrBrowser/core/qlrmanager.py | 8 ++++++-- src/QlrBrowser/i18n/da.qm | Bin 783 -> 1026 bytes src/QlrBrowser/i18n/da.ts | 12 +++++++++++- src/QlrBrowser/qlrbrowser.py | 2 +- src/QlrBrowser/ui/dockwidget.py | 22 +++++++++++++++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/QlrBrowser/core/qlrmanager.py b/src/QlrBrowser/core/qlrmanager.py index d10fa14..4fb8a49 100644 --- a/src/QlrBrowser/core/qlrmanager.py +++ b/src/QlrBrowser/core/qlrmanager.py @@ -125,8 +125,10 @@ def browser_itemclicked(self, fileinfo, newState): QCoreApplication.processEvents() # Load qlr QgsLayerDefinition.loadLayerDefinition(path, self.layer_insertion_point()) - # This is backwards, but qlr is always loaded at the bottom of the TOC - self._move_qlr_to_top(path) + # Lets see if we catched the loaded layer. If not - it could be because the qlrfile was moved + if path in self.fileSystemItemToLegendNode: + # This is backwards, but qlr is always loaded at the bottom of the TOC + self._move_qlr_to_top(path) # Remove message self.iface.messageBar().popWidget(msgItem) finally: @@ -139,6 +141,8 @@ def layer_insertion_point(self): def _move_qlr_to_top(self, qlrpath): """Moves the layers added to the TOC from the given QLR file. For now always to the top of the TOC""" + if not qlrpath in self.fileSystemItemToLegendNode: + return # Only supports moving layer to the top of the TOC. for nodeinfo in self.fileSystemItemToLegendNode[qlrpath]: node = self._getlayerTreeNode(nodeinfo) diff --git a/src/QlrBrowser/i18n/da.qm b/src/QlrBrowser/i18n/da.qm index 9d4fdfa78879bade528896e8025b6bd9a53cf46e..2c8afced0f0be847141c2240ffdf6a13b8216385 100644 GIT binary patch delta 286 zcmeBYYvPz7BALLTdsC7D1i&=2DWkmHVFm`KOA~eF^qG1&-`#Bj@v7Kk!1Fotpa zf%uFsd3G}v1I0yn7r8wG^4T&O7??XI?_si>xcD3k&@hFG4<+l<7*ZLs7;-p3rn3RD zU|>#>f>Tj`d2woyf@@Jxei1uRl*=VQIXgTvB|Wu-kr~MMVQ>NJ%VSVrC}T)u$YDrl zC;{>n7>a>(CXh~L$Of`A!TeN)A|SsAD4Gcr%?7K>VR#1Aoz9SI29ktZt`U-vs!*Jo rlbT$TnxasUSdyWTlAl_vke6SgkXn&hT%wSeSDBk%l)5>AaXljdM~Ou^ delta 79 zcmZqT=x3WCA{oG-dsC7D1i&;?5Tm@@VFm`q_Y-yHzOSlZa%j;g=rom0O}kRqW}N^ diff --git a/src/QlrBrowser/i18n/da.ts b/src/QlrBrowser/i18n/da.ts index 4fee962..68de59b 100644 --- a/src/QlrBrowser/i18n/da.ts +++ b/src/QlrBrowser/i18n/da.ts @@ -29,10 +29,20 @@ DockWidget - + Filter Filtrér + + + Qlr Browser Error + Qlr Browser fejl + + + + The selected path does not exist anymore + Den valgte sti eksisterer ikke længere + QlrBrowser diff --git a/src/QlrBrowser/qlrbrowser.py b/src/QlrBrowser/qlrbrowser.py index a1c6f02..2f93fcd 100644 --- a/src/QlrBrowser/qlrbrowser.py +++ b/src/QlrBrowser/qlrbrowser.py @@ -188,7 +188,7 @@ def initGui(self): # removed on close (see self.onClosePlugin method) if self.dockwidget is None: # Create the dockwidget (after translation) and keep reference - self.dockwidget = DockWidget( self.iface.mainWindow()) + self.dockwidget = DockWidget( self.iface) self.dockwidget.addRootPath(self.settings.value("baseDirectory")) self.qlrmanager = QlrManager(self.iface, self.dockwidget) diff --git a/src/QlrBrowser/ui/dockwidget.py b/src/QlrBrowser/ui/dockwidget.py index d52542f..e32b55e 100644 --- a/src/QlrBrowser/ui/dockwidget.py +++ b/src/QlrBrowser/ui/dockwidget.py @@ -24,6 +24,7 @@ import os from PyQt4 import QtGui, uic from PyQt4.QtCore import QFileInfo, QDir, pyqtSignal, pyqtSlot, Qt, QTimer +from qgis._gui import QgsMessageBar from ..core.filesystemmodel import FileSystemModel FORM_CLASS, _ = uic.loadUiType(os.path.join( @@ -37,8 +38,9 @@ class DockWidget(QtGui.QDockWidget, FORM_CLASS): itemStateChanged = pyqtSignal(object, bool) - def __init__(self, parent=None): + def __init__(self, iface=None): """Constructor.""" + parent = None if iface is None else iface.mainWindow() super(DockWidget, self).__init__(parent) # Set up the user interface from Designer. # After setupUI you can access any designer object by doing @@ -47,6 +49,8 @@ def __init__(self, parent=None): # #widgets-and-dialogs-with-auto-connect self.setupUi(self) + self.iface = iface + # UI self.filterLineEdit.setPlaceholderText( self.trUtf8(u'Filter')) self.treeWidget.setColumnCount(1) @@ -112,6 +116,12 @@ def _treeitem_doubleclicked(self, item, column): def _treeitem_changed(self, item, column): checked = item.checkState(column) == Qt.Checked path = item.fullpath + if checked: + # Dont try to turn on a non-existing qlr + if not self._checkFileItemExists(path): + # Path no longer exists. Reload filesystem + self.reloadFileSystemInfo() + return self.setPathCheckState(path, checked) self.itemStateChanged.emit(item.fileitem, checked) @@ -180,6 +190,16 @@ def _createWidgetItem(self, fileitem): num_checked_sub_paths = self.getNumCheckedSubPaths(fileitem.fullpath) return TreeWidgetItem(fileitem, checked, num_checked_sub_paths) + def _checkFileItemExists(self, path): + if os.path.exists(path): + return True + else: + self.iface.messageBar().pushMessage( + self.trUtf8("Qlr Browser Error"), + self.trUtf8("The selected path does not exist anymore"), + level=QgsMessageBar.CRITICAL) + return False + # # Events #