Skip to content

Commit

Permalink
moving ResultsManager use from isogeo.py to search_form.py
Browse files Browse the repository at this point in the history
#210
+ docstring
  • Loading branch information
SimonSAMPERE committed Aug 30, 2019
1 parent ad6b435 commit bddfcc2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
39 changes: 20 additions & 19 deletions isogeo.py
Expand Up @@ -55,16 +55,13 @@
from . import resources

# UI classes
from .ui.isogeo_dockwidget import IsogeoDockWidget # main widget
from .ui.credits.dlg_credits import IsogeoCredits

# Plugin modules
from .modules import Authenticator
from .modules import ApiRequester
from .modules import MetadataDisplayer
from .modules import ResultsManager
from .modules import IsogeoPlgTools
from .modules import QuickSearchManager
from .modules import SharesParser
from .modules import SearchFormManager

Expand Down Expand Up @@ -197,16 +194,17 @@ def __init__(self, iface):
# instanciating
self.md_display = MetadataDisplayer()

self.results_mng = ResultsManager(self)
self.results_mng.cache_mng.loader()

self.approps_mng = SharesParser()
self.approps_mng.tr = self.tr

self.authenticator = Authenticator(auth_folder=plg_authdir)

self.api_requester = ApiRequester()
self.api_requester.tr = self.tr

self.form_mng = SearchFormManager(self.tr)
self.form_mng.qs_mng.url_builder = self.api_requester.build_request_url
self.form_mng.qs_mng.lang = self.lang

# connecting
self.api_requester.token_sig.connect(self.token_slot)
Expand All @@ -223,7 +221,7 @@ def __init__(self, iface):
self.showResult = False
self.showDetails = False
self.store = False
self.PostGISdict = self.results_mng.build_postgis_dict(qsettings)
self.PostGISdict = self.form_mng.results_mng.build_postgis_dict(qsettings)

self.old_text = ""
self.page_index = 1
Expand Down Expand Up @@ -303,7 +301,7 @@ def initGui(self):
def onClosePlugin(self):
"""Cleanup necessary items here when plugin dockwidget is closed."""
# save cache
self.results_mng.cache_mng.dumper()
self.form_mng.results_mng.cache_mng.dumper()
# disconnects
self.form_mng.closingPlugin.disconnect(self.onClosePlugin)

Expand Down Expand Up @@ -536,11 +534,8 @@ def search_slot(self, result: dict, tags:dict):

# Showing result : if button 'show result', 'next page' or 'previous page' pressed
if self.showResult is True:
self.form_mng.prepare_tbl_result(self.page_index, self.results_count)
self.form_mng.qs_mng.write_params('_current', search_kind="Current")
self.results_mng.show_results(result,
self.form_mng.tbl_result,
progress_bar=self.bar)
self.form_mng.fill_tbl_result(content = result, page_index = self.page_index, results_count = self.results_count)
iface.mainWindow().statusBar().removeWidget(self.bar)
self.store = True
else:
pass
Expand Down Expand Up @@ -742,6 +737,8 @@ def run(self):
# pagination
self.form_mng.btn_next.pressed.connect(partial(self.search, show = True, page_change = 1))
self.form_mng.btn_previous.pressed.connect(partial(self.search, show = True, page_change = -1))
# metadata display
self.form_mng.results_mng.md_asked.connect(self.send_details_request)

# -- Quicksearches ----------------------------------------------------

Expand All @@ -750,7 +747,7 @@ def run(self):

# # -- Settings tab - Search --------------------------------------------
# button to empty the cache of filepaths #135
self.form_mng.btn_cache_trash.pressed.connect(self.results_mng.cache_mng.cleaner)
self.form_mng.btn_cache_trash.pressed.connect(self.form_mng.results_mng.cache_mng.cleaner)

# -- Settings tab - Application authentication ------------------------
# Change user -> see below for authentication form
Expand All @@ -765,11 +762,12 @@ def run(self):
self.form_mng.btn_log_dir.pressed.connect(partial(plg_tools.open_dir_file,
target=plg_logdir))
self.form_mng.btn_report.pressed.connect(partial(plg_tools.open_webpage,
link=u"https://github.com/isogeo/isogeo-plugin-qgis/issues/new?title={} - plugin v{} QGIS {} ({})&labels=bug&milestone=4".format(self.tr("TITLE ISSUE REPORTED"),
plg_tools.plugin_metadata(base_path=plg_basepath),
Qgis.QGIS_VERSION,
platform.platform())
))
link=u"https://github.com/isogeo/isogeo-plugin-qgis/issues/new?title={} - plugin v{} QGIS {} ({})&labels=bug&milestone=4".format(
self.tr("TITLE ISSUE REPORTED"),
plg_tools.plugin_metadata(base_path=plg_basepath),
Qgis.QGIS_VERSION,
platform.platform())
))
# help button
self.form_mng.btn_help.pressed.connect(
partial(plg_tools.open_webpage,
Expand Down Expand Up @@ -798,6 +796,9 @@ def run(self):
# self.form_mng.cbb_chck_kw.setMaximumSize(QSize(250, 25))
self.form_mng.txt_input.setFocus()
self.savedSearch = "first"
# load cache file
self.form_mng.results_mng.cache_mng.loader()
# launch authentication
self.user_authentication()

# #############################################################################
Expand Down
34 changes: 16 additions & 18 deletions modules/results/display.py
Expand Up @@ -9,7 +9,7 @@
from pathlib import Path
# PyQT
# from QByteArray
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtCore import QSettings, QObject, pyqtSignal
from qgis.PyQt.QtGui import QIcon, QPixmap
from qgis.PyQt.QtWidgets import (QTableWidgetItem, QComboBox, QPushButton, QLabel,
QProgressBar, QHeaderView)
Expand Down Expand Up @@ -65,31 +65,31 @@
# ##################################


class ResultsManager():
class ResultsManager(QObject):
"""Basic class that holds utilitary methods for the plugin."""

def __init__(self, isogeo_plugin):
"""Class constructor."""
self.isogeo_widget = isogeo_plugin.form_mng
self.send_details_request = isogeo_plugin.send_details_request
self.tr = isogeo_plugin.tr
md_asked = pyqtSignal(str)

def __init__(self, search_form_manager: object):
# inheritance
super().__init__()

self.form_mng = search_form_manager
self.tbl_result = self.form_mng.tbl_result
self.tr = self.form_mng.tr

self.layer_adder = LayerAdder()
self.layer_adder.tr = self.tr
self.layer_adder.tbl_result = self.tbl_result
self.add_layer = self.layer_adder.adding
self.pg_connections = self.build_postgis_dict(qsettings)

self.cache_mng = CacheManager()

def show_results(self, api_results, tbl_result=None, pg_connections=dict(), progress_bar=QProgressBar):
"""Display the results in a table ."""
def show_results(self, api_results, pg_connections=dict()):
"""Display the results in a table."""
logger.info("Results manager called. Displaying the results")
# check parameters
if not tbl_result:
tbl_result = self.isogeo_widget.tbl_result
else:
pass
self.layer_adder.tbl_result = tbl_result
tbl_result = self.tbl_result
# Get the name (and other informations) of all databases whose
# connection is set up in QGIS
if pg_connections == {}:
Expand Down Expand Up @@ -120,7 +120,7 @@ def show_results(self, api_results, tbl_result=None, pg_connections=dict(), prog
btn_md_title = QPushButton(plg_tools.format_button_title(md_title))
# Connecting the button to the full metadata popup
btn_md_title.pressed.connect(partial(
self.send_details_request, md_id=md_id))
self.md_asked.emit, md_id))
# Putting the abstract as a tooltip on this button
btn_md_title.setToolTip(i.get("abstract", "")[:300])
# Insert it in column 1
Expand Down Expand Up @@ -388,8 +388,6 @@ def show_results(self, api_results, tbl_result=None, pg_connections=dict(), prog
header.setSectionResizeMode(0, QHeaderView.Stretch)
header.setSectionResizeMode(1, QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
# Remove the "loading" bar
iface.mainWindow().statusBar().removeWidget(progress_bar)
# method ending
return None

Expand Down
56 changes: 41 additions & 15 deletions modules/search_form.py
Expand Up @@ -55,8 +55,16 @@
# ##################################

class SearchFormManager(IsogeoDockWidget):
""" Basic class to manage IsogeoDockwidget Ui module (ui/isogeo_dockwidget.py).
It performs different tasks :
- update widgets (clear, fill and set appropriate status)
- fill the results table calling ResultsManager.show_results method
- save search parameters selected by the user, wich is useful for updating
widgets or building search request's URL.
Most of its methods are called by Isogeo.search_slot method which launched after
the results of a search request has been parsed and validated.
"""
"""
# Simple signal to connect keywords special combobox with Isogeo.search method
kw_sig = pyqtSignal()

def __init__(self, trad):
Expand Down Expand Up @@ -98,6 +106,9 @@ def __init__(self, trad):
self.btn_delete_sr.pressed.connect(self.qs_mng.remove)
self.btn_default_save.pressed.connect(self.qs_mng.write_params)

# Setting result manager
self.results_mng = ResultsManager(self)

def update_cbb_keywords(self, tags_keywords:dict ={}, selected_keywords:list =[]):
"""Keywords combobox is specific because items are checkable.
See: https://github.com/isogeo/isogeo-plugin-qgis/issues/159
Expand Down Expand Up @@ -248,24 +259,24 @@ def set_ccb_index(self, params: dict, quicksearch: str = ""):

return

def prepare_tbl_result(self, page_index:int, results_count:int):
def fill_tbl_result(self, content: dict, page_index:int, results_count:int):
""" Called by Isogeo.search_slot method. It sets some widgets' statuts
in order to display results.
:param int page_index: results table's page index
:param int page_index: results table's page index.
:param int results_count: number of metadata to be displayed in the
table.
:param dict content: a dict containing the parsed content of API's reply
to a search request
"""
nb_page = plg_tools.results_pages_counter(total=results_count)
if nb_page == 1:
self.btn_next.setEnabled(False)
self.btn_previous.setEnabled(False)
elif page_index < 2:
logger.debug("*=====* desabling btn_previous")
self.btn_previous.setEnabled(False)
self.btn_next.setEnabled(True)
elif page_index == nb_page:
logger.debug("*=====* desabling btn_next")
self.btn_next.setEnabled(False)
self.btn_previous.setEnabled(True)
else :
Expand All @@ -275,10 +286,9 @@ def prepare_tbl_result(self, page_index:int, results_count:int):
self.cbb_ob.setEnabled(True)
self.cbb_od.setEnabled(True)
self.btn_show.setToolTip(self.tr("Display results"))
# self.results_mng.show_results(content,
# self.tbl_result,
# progress_bar=self.bar)
# self.write_search_params('_current', search_kind="Current")

self.results_mng.show_results(api_results = content)
self.qs_mng.write_params('_current', search_kind="Current")

def switch_widgets_on_and_off(self, mode=1):
"""Disable all the UI widgets when a request is being sent.
Expand Down Expand Up @@ -329,9 +339,16 @@ def reinit_widgets(self):
def save_params(self):
"""Save the widgets state/index.
This save the current state/index of each user input so we can put them
back to their previous state/index after they have been updated
(cleared and filled again).
This save the current state/index of each user input in a dict so we can
put them back to their previous state/index after they have been updated
(cleared and filled again). The dict is also used to build search requests
URL.
:returns: a dictionary whose keys correspond to the names of the different
user input and values correspond to the user's sélection in each of these
inputs (None if nothing selected).
:rtype: dict
"""
params = {}
# get the data of the item which index is (comboboxes current index)
Expand Down Expand Up @@ -379,11 +396,20 @@ def save_params(self):
pass
# saving params in QSettings
qsettings.setValue("isogeo/settings/georelation", params.get("operation"))
logger.debug("*=====* params : {}".format(params))
return params

def get_coords(self, filter):
"""Get the canvas coordinates in the right format and SRS (WGS84)."""
def get_coords(self, filter:str):
"""Get the extent's coordinates of a layer or canvas in the right format
and SRS (WGS84).
:param str filter: the name of the element wich we want to get extent's
coordinates.
:returns: the x and y coordinates of the canvas' Southwestern and
Northeastern vertexes.
:rtype: str
"""
if filter == 'canvas':
e = iface.mapCanvas().extent()
current_epsg = plg_tools.get_map_crs()
Expand Down

0 comments on commit bddfcc2

Please sign in to comment.