This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
quickfinder_plugin.py
185 lines (161 loc) · 7.18 KB
/
quickfinder_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#-----------------------------------------------------------
#
# QGIS Quick Finder Plugin
# Copyright (C) 2013 Denis Rouzaud
#
#-----------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
import os.path
from PyQt5.QtCore import Qt, QObject, QSettings, QCoreApplication, QTranslator, QUrl, pyqtSlot
from PyQt5.QtWidgets import QAction, QMessageBox
from PyQt5.QtGui import QIcon, QColor, QDesktopServices
from qgis.gui import QgsRubberBand, QgsMessageBar
from .core.project_finder import ProjectFinder, n_days_ago_iso_date
from .core.osm_finder import OsmFinder
from .core.geomapfish_finder import GeomapfishFinder
from .core.my_settings import MySettings
from .gui.configuration_dialog import ConfigurationDialog
from .gui.refresh_dialog import RefreshDialog
from .gui.finder_box import FinderBox
from .resources_rc import *
class QuickFinder(QObject):
name = u"&Quick Finder"
actions = None
toolbar = None
finders = {}
loadingIcon = None
def __init__(self, iface):
QObject.__init__(self)
self.iface = iface
self.actions = {}
self.finders = {}
self.settings = MySettings()
self.rubber = None
self._init_finders()
self.iface.projectRead.connect(self._reload_finders)
self.iface.newProjectCreated.connect(self._reload_finders)
# translation environment
self.plugin_dir = os.path.dirname(__file__)
locale = QSettings().value("locale/userLocale")[0:2]
localePath = os.path.join(self.plugin_dir, 'i18n', 'quickfinder_{0}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
QCoreApplication.installTranslator(self.translator)
def initGui(self):
self.actions['showSettings'] = QAction(
QIcon(":/plugins/quickfinder/icons/settings.svg"),
self.tr(u"&Settings"),
self.iface.mainWindow())
self.actions['showSettings'].triggered.connect(self.show_settings)
self.iface.addPluginToMenu(self.name, self.actions['showSettings'])
self.actions['help'] = QAction(
QIcon(":/plugins/quickfinder/icons/help.svg"),
self.tr("Help"),
self.iface.mainWindow())
self.actions['help'].triggered.connect(
lambda: QDesktopServices().openUrl(
QUrl("http://3nids.github.io/quickfinder")))
self.iface.addPluginToMenu(self.name, self.actions['help'])
self._init_toolbar()
self.rubber = QgsRubberBand(self.iface.mapCanvas())
self.rubber.setColor(QColor(255, 255, 50, 200))
self.rubber.setIcon(self.rubber.ICON_CIRCLE)
self.rubber.setIconSize(15)
self.rubber.setWidth(4)
self.rubber.setBrushStyle(Qt.NoBrush)
def unload(self):
""" Unload plugin """
for key in self.finders.keys():
self.finders[key].close()
for action in self.actions.values():
self.iface.removePluginMenu(self.name, action)
if self.toolbar:
del self.toolbar
if self.rubber:
self.iface.mapCanvas().scene().removeItem(self.rubber)
del self.rubber
def _init_toolbar(self):
"""setup the plugin toolbar."""
self.toolbar = self.iface.addToolBar(self.name)
self.toolbar.setObjectName('mQuickFinderToolBar')
self.search_action = QAction(QIcon(":/plugins/quickfinder/icons/magnifier13.svg"), self.tr("Search"), self.toolbar)
self.stop_action = QAction(QIcon(":/plugins/quickfinder/icons/wrong2.svg"), self.tr("Cancel"), self.toolbar)
self.finder_box = FinderBox(self.finders, self.iface, self.toolbar)
self.finder_box.search_started.connect(self.search_started)
self.finder_box.search_finished.connect(self.search_finished)
self.finder_box_action = self.toolbar.addWidget(self.finder_box)
self.finder_box_action.setVisible(True)
self.search_action.triggered.connect(self.finder_box.search)
self.toolbar.addAction(self.search_action)
self.stop_action.setVisible(False)
self.stop_action.triggered.connect(self.finder_box.stop)
self.toolbar.addAction(self.stop_action)
self.toolbar.setVisible(True)
def _init_finders(self):
self.finders['geomapfish'] = GeomapfishFinder(self)
self.finders['osm'] = OsmFinder(self)
self.finders['project'] = ProjectFinder(self)
for key in self.finders.keys():
self.finders[key].message.connect(self.display_message)
self.refresh_project()
def _reload_finders(self):
for key in self.finders.keys():
self.finders[key].close()
self.finders[key].reload()
self.refresh_project()
@pyqtSlot(str, QgsMessageBar.MessageLevel)
def display_message(self, message, level):
self.iface.messageBar().pushMessage("QuickFinder", message, level)
def show_settings(self):
if ConfigurationDialog().exec_():
self._reload_finders()
def search_started(self):
self.search_action.setVisible(False)
self.stop_action.setVisible(True)
def search_finished(self):
self.search_action.setVisible(True)
self.stop_action.setVisible(False)
def refresh_project(self):
if not self.finders['project'].activated:
return
if not self.settings.value("refreshAuto"):
return
n_days = self.settings.value("refreshDelay")
# do not ask more ofen than 3 days
ask_limit = min(3, n_days)
recently_asked = self.settings.value("refreshLastAsked") >= n_days_ago_iso_date(ask_limit)
if recently_asked:
return
thresh_date = n_days_ago_iso_date(n_days)
uptodate = True
for search in self.finders['project'].searches.values():
if search.dateEvaluated <= thresh_date:
uptodate = False
break
if uptodate:
return
self.settings.setValue("refreshLastAsked", n_days_ago_iso_date(0))
ret = QMessageBox(QMessageBox.Warning,
"Quick Finder",
QCoreApplication.translate("Auto Refresh", "Some searches are outdated. Do you want to refresh them ?"),
QMessageBox.Cancel | QMessageBox.Yes).exec_()
if ret == QMessageBox.Yes:
RefreshDialog(self.finders['project']).exec_()