Skip to content

Commit

Permalink
Warn user and avoid crash when trying to add qlr which does not exist
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
AsgerPetersen committed Feb 3, 2016
1 parent 1fa6a54 commit 2637ad3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/QlrBrowser/core/qlrmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
Binary file modified src/QlrBrowser/i18n/da.qm
Binary file not shown.
12 changes: 11 additions & 1 deletion src/QlrBrowser/i18n/da.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
<context>
<name>DockWidget</name>
<message>
<location filename="dockwidget.py" line="51"/>
<location filename="dockwidget.py" line="55"/>
<source>Filter</source>
<translation>Filtrér</translation>
</message>
<message>
<location filename="dockwidget.py" line="197"/>
<source>Qlr Browser Error</source>
<translation>Qlr Browser fejl</translation>
</message>
<message>
<location filename="dockwidget.py" line="197"/>
<source>The selected path does not exist anymore</source>
<translation>Den valgte sti eksisterer ikke længere</translation>
</message>
</context>
<context>
<name>QlrBrowser</name>
Expand Down
2 changes: 1 addition & 1 deletion src/QlrBrowser/qlrbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 21 additions & 1 deletion src/QlrBrowser/ui/dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
#
Expand Down

0 comments on commit 2637ad3

Please sign in to comment.