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