forked from 3nids/quickfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickfinder_plugin.py
187 lines (154 loc) · 6.69 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
183
184
#-----------------------------------------------------------
#
# 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 PyQt4.QtCore import Qt, QObject, QSettings, QCoreApplication, QTranslator, QUrl
from PyQt4.QtGui import QAction, QIcon, QColor, QDesktopServices, QMessageBox
from qgis.gui import QgsRubberBand
from quickfinder.core.projectfinder import ProjectFinder, nDaysAgoIsoDate
from quickfinder.core.osmfinder import OsmFinder
from quickfinder.core.geomapfishfinder import GeomapfishFinder
from quickfinder.core.mysettings import MySettings
from quickfinder.gui.configurationdialog import ConfigurationDialog
from quickfinder.gui.refreshdialog import RefreshDialog
from quickfinder.gui.finderbox import FinderBox
import resources_rc
class quickFinder(QObject):
name = u"&Quick Finder"
actions = None
toolbar = None
finders = None
loadingIcon = None
def __init__(self, iface):
QObject.__init__(self)
self.iface = iface
self.actions = {}
self.finders = {}
self.settings = MySettings()
self._initFinders()
# 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_{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
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.showSettings)
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("https://github.com/3nids/quickfinder/wiki")))
self.iface.addPluginToMenu(self.name, self.actions['help'])
self._initToolbar()
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):
"""Remove the plugin menu item and icon """
for action in self.actions.itervalues():
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 _initToolbar(self):
"""setup the plugin toolbar."""
self.toolbar = self.iface.addToolBar(self.name)
self.toolbar.setObjectName('mQuickFinderToolBar')
self.searchAction = QAction( QIcon(":/plugins/quickfinder/icons/magnifier13.svg"),
self.tr("Search"),
self.toolbar)
self.stopAction = QAction(
QIcon(":/plugins/quickfinder/icons/wrong2.svg"),
self.tr("Cancel"),
self.toolbar)
self.finderBox = FinderBox(self.finders, self.iface, self.toolbar)
self.finderBox.searchStarted.connect(self.enableSearch)
self.finderBox.searchFinished.connect(self.disableSearch)
self.finderBoxAction = self.toolbar.addWidget(self.finderBox)
self.finderBoxAction.setVisible(True)
self.searchAction.triggered.connect(self.finderBox.search)
self.toolbar.addAction(self.searchAction)
self.stopAction.setVisible(False)
self.stopAction.triggered.connect(self.finderBox.stop)
self.toolbar.addAction(self.stopAction)
self.toolbar.setVisible(True)
def _initFinders(self):
self.finders = {
'geomapfish': GeomapfishFinder(self),
'osm': OsmFinder(self),
'project': ProjectFinder(self)
}
self.refreshProject()
def showSettings(self):
if ConfigurationDialog().exec_():
self.finders['project'].reload()
self.refreshProject()
def enableSearch(self):
self.searchAction.setVisible(True)
self.stopAction.setVisible(False)
def disableSearch(self):
self.searchAction.setVisible(False)
self.stopAction.setVisible(True)
def refreshProject(self):
if not self.finders['project'].activated:
return
if not self.settings.value("refreshAuto"):
return
nDays = self.settings.value("refreshDelay")
# do not ask more ofen than 3 days
askLimit = min(3,nDays)
recentlyAsked = self.settings.value("refreshLastAsked") > nDaysAgoIsoDate(askLimit)
if recentlyAsked:
return
threshDate = nDaysAgoIsoDate(nDays)
uptodate = True
for search in self.finders['project'].searches.values():
if search.dateEvaluated <= threshDate:
uptodate = False
break
if uptodate:
return
self.settings.setValue( "refreshLastAsked", nDaysAgoIsoDate(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_()