"""
Companion manager for QDF/IDF menu and actions.
Extracted from PyDraw.WolfMapViewer.
"""
from __future__ import annotations
import logging
import os
from typing import TYPE_CHECKING
import wx
from ._action_kind import ActionKind
from .PyTranslate import _
if TYPE_CHECKING:
from .PyDraw import WolfMapViewer
[docs]
class QdfidfManager:
"""Manages the QDF/IDF menu and associated actions."""
def __init__(self, viewer: "WolfMapViewer") -> None:
[docs]
def _on_download(self, event: wx.Event) -> None:
self._handle_download_data()
[docs]
def _on_load(self, event: wx.Event) -> None:
self._handle_load_data()
[docs]
def _on_pick_municipality(self, event: wx.Event) -> None:
v = self._viewer
if v.active_qdfidf is None:
logging.warning(_('No active QDF/IDF -- Please load data first'))
return
v.start_action(ActionKind.PICK_MUNICIPALITY, _('Pick a municipality from the QDF/IDF data'))
[docs]
def _on_show_tables(self, event: wx.Event) -> None:
v = self._viewer
if v.active_qdfidf is None:
logging.warning(_('No active QDF/IDF -- Please load data first'))
return
pgbar = wx.ProgressDialog(_('Loading QDF/IDF data'), _('Loading data...'), maximum=100, parent=v, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE)
pgbar.Pulse(_('Loading QDF/IDF data...'))
v.active_qdfidf.show_plot = False
v.active_qdfidf.show_table = True
logging.info(_('Show tables for QDF/IDF data'))
v.Refresh()
pgbar.Update(100, _('QDF/IDF data loaded'))
pgbar.Destroy()
v.treelist.SetFocus()
[docs]
def _on_show_plots(self, event: wx.Event) -> None:
v = self._viewer
if v.active_qdfidf is None:
logging.warning(_('No active QDF/IDF -- Please load data first'))
return
pgbar = wx.ProgressDialog(_('Loading QDF/IDF data'), _('Loading data...'), maximum=100, parent=v, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE)
pgbar.Pulse(_('Loading QDF/IDF data...'))
v.active_qdfidf.show_table = False
v.active_qdfidf.show_plot = True
logging.info(_('Show tables for QDF/IDF data'))
v.Refresh()
pgbar.Update(100, _('QDF/IDF data loaded'))
pgbar.Destroy()
v.treelist.SetFocus()
[docs]
def _on_scale_images(self, event: wx.Event) -> None:
v = self._viewer
if v.active_qdfidf is None:
logging.warning(_('No active QDF/IDF -- Please load data first'))
return
scalefactor = wx.GetTextFromUser(_('Enter the scale factor (default is 1.0)'), _('Scale factor'), '1.0', v)
if scalefactor == '':
scalefactor = '1.0'
try:
scalefactor = float(scalefactor)
except ValueError:
logging.error(_('Invalid scale factor: {}').format(scalefactor))
wx.MessageBox(_('Invalid scale factor: {}').format(scalefactor), _('Error'), wx.OK | wx.ICON_ERROR)
return
v.active_qdfidf.scale_images(scalefactor)
v.Refresh()
[docs]
def _handle_load_data(self) -> None:
from .irm_qdf import QDF_Hydrology_Draw
v = self._viewer
dlg_dir = wx.DirDialog(v, _('Choose directory with QDF/IDF files'), style=wx.DD_DEFAULT_STYLE)
if dlg_dir.ShowModal() == wx.ID_CANCEL:
dlg_dir.Destroy()
return
dirpath = dlg_dir.GetPath()
dlg_dir.Destroy()
pgbar = wx.ProgressDialog(_('Loading QDF/IDF data'), _('Loading data...'), maximum=100, parent=v, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE)
pgbar.Pulse(_('Loading QDF/IDF data... (estimated time 10 min.)'))
v.active_qdfidf = QDF_Hydrology_Draw(dirpath, idx='QDF/IDF', mapviewer=v)
pgbar.Update(100, _('QDF/IDF data loaded'))
pgbar.Destroy()
v.treelist.SetFocus()
[docs]
def _handle_download_data(self) -> None:
from .irm_qdf import QDF_Hydrology_Draw
v = self._viewer
dlg_dir = wx.DirDialog(v, _('Choose an empty directory to store QDF/IDF files'), style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg_dir.ShowModal() == wx.ID_CANCEL:
dlg_dir.Destroy()
return
dirpath = dlg_dir.GetPath()
dlg_dir.Destroy()
if os.listdir(dirpath):
logging.error(_('The directory {} is not empty. Please choose an empty directory.').format(dirpath))
wx.MessageBox(_('The directory {} is not empty. Please choose an empty directory.').format(dirpath), _('Error'), wx.OK | wx.ICON_ERROR)
return
pgbar = wx.ProgressDialog(_('Downloading QDF/IDF data'), _('Downloading data...'), maximum=100, parent=v, style=wx.PD_APP_MODAL | wx.PD_AUTO_HIDE)
pgbar.Pulse(_('Downloading QDF/IDF data...'))
try:
v.active_qdfidf = QDF_Hydrology_Draw(dirpath, idx='QDF/IDF', mapviewer=v)
except Exception as ex:
logging.error(_('Error downloading QDF/IDF data: {}').format(ex))
wx.MessageBox(_('Error downloading QDF/IDF data: {}').format(ex), _('Error'), wx.OK | wx.ICON_ERROR)
pgbar.Update(100, _('QDF/IDF data loaded and processed'))
pgbar.Destroy()
v.treelist.SetFocus()