ResultsDock.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. ***************************************************************************
  3. ResultsDock.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. import time
  22. import warnings
  23. from qgis.PyQt import uic
  24. from qgis.PyQt.QtCore import (QUrl,
  25. QFileInfo,
  26. QDir)
  27. from qgis.gui import QgsDockWidget
  28. from qgis.PyQt.QtGui import QDesktopServices
  29. from qgis.PyQt.QtWidgets import QTreeWidgetItem
  30. from processing.core.ProcessingResults import resultsList
  31. pluginPath = os.path.split(os.path.dirname(__file__))[0]
  32. with warnings.catch_warnings():
  33. warnings.filterwarnings("ignore", category=DeprecationWarning)
  34. WIDGET, BASE = uic.loadUiType(
  35. os.path.join(pluginPath, 'ui', 'resultsdockbase.ui'))
  36. class ResultsDock(QgsDockWidget, WIDGET):
  37. def __init__(self):
  38. super().__init__(None)
  39. self.setupUi(self)
  40. resultsList.resultAdded.connect(self.addResult)
  41. self.treeResults.currentItemChanged.connect(self.updateDescription)
  42. self.treeResults.itemDoubleClicked.connect(self.openResult)
  43. self.txtDescription.setOpenLinks(False)
  44. self.txtDescription.anchorClicked.connect(self.openLink)
  45. self.fillTree()
  46. def addResult(self):
  47. self.fillTree()
  48. # Automatically open the panel for users to see output
  49. self.setUserVisible(True)
  50. self.treeResults.setCurrentItem(self.treeResults.topLevelItem(0))
  51. def fillTree(self):
  52. self.treeResults.blockSignals(True)
  53. self.treeResults.clear()
  54. elements = resultsList.getResults()
  55. for element in elements:
  56. item = TreeResultItem(element)
  57. self.treeResults.insertTopLevelItem(0, item)
  58. self.treeResults.blockSignals(False)
  59. def updateDescription(self, current, previous):
  60. if isinstance(current, TreeResultItem):
  61. html = f'<b>Algorithm</b>: {current.algorithm}<br><b>File path</b>: <a href="{QUrl.fromLocalFile(current.filename).toString()}">{QDir.toNativeSeparators(current.filename)}</a>'
  62. self.txtDescription.setHtml(html)
  63. def openLink(self, url):
  64. QDesktopServices.openUrl(url)
  65. def openResult(self, item, column):
  66. QDesktopServices.openUrl(QUrl.fromLocalFile(item.filename))
  67. class TreeResultItem(QTreeWidgetItem):
  68. def __init__(self, result):
  69. QTreeWidgetItem.__init__(self)
  70. self.setIcon(0, result.icon)
  71. self.setText(0, '{} [{}]'.format(result.name, time.strftime('%I:%M:%S%p', result.timestamp)))
  72. self.algorithm = result.name
  73. self.filename = result.filename