Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Improvements to the BRF tool gui #325

Merged
merged 8 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions gwhat/HydroCalc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, datamanager, parent=None):
SaveFileMixin.__init__(self)

self._navig_and_select_tools = []
self._last_toggled_navig_and_select_tool = None
self._wldset = None

self.dmngr = datamanager
Expand All @@ -76,11 +77,8 @@ def __init__(self, datamanager, parent=None):
# Setup BRF calculation tool.
self.brf_eval_widget = BRFManager(parent=self)
self.brf_eval_widget.sig_brfperiod_changed.connect(self.plot_brfperiod)
self.brf_eval_widget.btn_seldata.sig_value_changed.connect(
lambda: self.toggle_brfperiod_selection(
self.brf_eval_widget.btn_seldata.value())
)
self.register_navig_and_select_tool(self.brf_eval_widget.btn_seldata)
self.brf_eval_widget.sig_select_brfperiod_requested.connect(
self.toggle_brfperiod_selection)

self.__figbckground = None
self.__addPeakVisible = True
Expand Down Expand Up @@ -556,7 +554,6 @@ def close(self):
super().close()

# ---- MRC handlers

def btn_show_mrc_isclicked(self):
"""Handle when the button to draw of hide the mrc is clicked."""
if self.btn_show_mrc.value() is False:
Expand Down Expand Up @@ -643,13 +640,13 @@ def btn_mrc2rechg_isClicked(self):
mrc2rechg(self.time, self.water_lvl, A, B,
self.SOILPROFIL.zlayer, self.SOILPROFIL.Sy)

# ---- BRF handlers
# ---- BRF selection
def plot_brfperiod(self):
"""
Plot on the graph the vertical lines that are used to define the period
over which the BRF is evaluated.
"""
if (self.brf_eval_widget.btn_seldata.value() is False and
if (not self.brf_eval_widget.is_brfperiod_selection_toggled() and
self.brf_eval_widget.isVisible()):
brfperiod = self.brf_eval_widget.get_brfperiod()
else:
Expand All @@ -667,18 +664,38 @@ def toggle_brfperiod_selection(self, value):
the graph.
"""
if self.wldset is None:
self.brf_eval_widget.btn_seldata.setValue(False, silent=True)
self.brf_eval_widget.toggle_brfperiod_selection(False)
if value is True:
self.emit_warning("Please import a valid water "
"level dataset first.")
self.emit_warning(
"Please import a valid water level dataset first.")
return

self.brf_eval_widget.btn_seldata.setValue(value, silent=True)
if value is True:
self.toggle_navig_and_select_tools(
self.brf_eval_widget.btn_seldata)
self._last_toggled_navig_and_select_tool = None
for tool in self._navig_and_select_tools:
if tool.value() is True:
self._last_toggled_navig_and_select_tool = tool
tool.setValue(False)
break
else:
self.brf_eval_widget.toggle_brfperiod_selection(False)
if self._last_toggled_navig_and_select_tool is not None:
self._last_toggled_navig_and_select_tool.setValue(True)
self.plot_brfperiod()

def on_brf_select(self):
"""
Handle when a period has been selected for the BRF calculation.
"""
if all(self._selected_brfperiod):
brfperiod = [None, None]
for i in range(2):
x = self._selected_brfperiod[i] - (
self.dt4xls2mpl * self.dformat)
brfperiod[i] = self.time[np.argmin(np.abs(x - self.time))]
self.brf_eval_widget.set_brfperiod(brfperiod)
self.toggle_brfperiod_selection(False)

# ---- Peaks handlers
def find_peak(self):

Expand Down Expand Up @@ -748,6 +765,10 @@ def toggle_navig_and_select_tools(self, keep_toggled=None):
if tool not in keep_toggled:
tool.setValue(False)

# Reset BRF selection.
self.brf_eval_widget.toggle_brfperiod_selection(False)
self.plot_brfperiod()

@property
def zoom_is_active(self):
"""Return whether the zooming to rectangle tool is active or not."""
Expand Down Expand Up @@ -1311,7 +1332,7 @@ def is_all_btn_raised(self):
"""
return(self.btn_delpeak.autoRaise() and
self.btn_addpeak.autoRaise() and
self.brf_eval_widget.btn_seldata.autoRaise())
not self.brf_eval_widget.is_brfperiod_selection_toggled())

def on_fig_leave(self, event):
"""Handle when the mouse cursor leaves the graph."""
Expand All @@ -1326,19 +1347,6 @@ def on_axes_leave(self, event):
"""Handle when the mouse cursor leaves an axe."""
self.toolbar.set_cursor(1)

def on_brf_select(self):
"""
Handle when a period has been selected for the BRF calculation.
"""
if all(self._selected_brfperiod):
brfperiod = [None, None]
for i in range(2):
x = self._selected_brfperiod[i] - (
self.dt4xls2mpl * self.dformat)
brfperiod[i] = self.time[np.argmin(np.abs(x - self.time))]
self.brf_eval_widget.set_brfperiod(brfperiod)
self.toggle_brfperiod_selection(False)

def on_rect_select(self):
"""
Handle when a rectangular area to select water level data has been
Expand Down Expand Up @@ -1398,7 +1406,7 @@ def on_mouse_move(self, event):

if self.rect_select_is_active and self.__mouse_btn_is_pressed:
self._draw_rect_selection(x, y)
if (self.brf_eval_widget.btn_seldata.value() and
if (self.brf_eval_widget.is_brfperiod_selection_toggled() and
self.__mouse_btn_is_pressed):
self._draw_brf_selection(x)

Expand Down Expand Up @@ -1451,7 +1459,7 @@ def onrelease(self, event):
self._rect_selection[1] = (event.xdata, event.ydata)
self._rect_selector.set_visible(False)
self.on_rect_select()
if self.brf_eval_widget.btn_seldata.value() is True:
if self.brf_eval_widget.is_brfperiod_selection_toggled():
self._selected_brfperiod[1] = event.xdata
self._brf_selector.set_visible(False)
self.on_brf_select()
Expand Down Expand Up @@ -1530,7 +1538,7 @@ def onclick(self, event):

self.__addPeakVisible = False
self.draw()
elif self.brf_eval_widget.btn_seldata.value() is True:
elif self.brf_eval_widget.is_brfperiod_selection_toggled():
self._selected_brfperiod[0] = event.xdata
elif self.rect_select_is_active:
self._rect_selection[0] = (event.xdata, event.ydata)
Expand Down
84 changes: 53 additions & 31 deletions gwhat/brf_mod/kgs_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from PyQt5.QtWidgets import (
QLabel, QDateTimeEdit, QCheckBox, QPushButton, QApplication, QSpinBox,
QAbstractSpinBox, QGridLayout, QDoubleSpinBox, QFrame, QWidget,
QMessageBox, QFileDialog, QComboBox, QLayout, QDialog)
QMessageBox, QFileDialog, QComboBox, QLayout, QDialog,
QGroupBox)

from xlrd.xldate import xldate_from_datetime_tuple, xldate_as_datetime
import numpy as np
Expand All @@ -33,11 +34,11 @@
# ---- Local imports
import gwhat.common.widgets as myqt
from gwhat.widgets.layout import VSep, HSep
from gwhat.widgets.buttons import LangToolButton, OnOffToolButton
from gwhat.widgets.buttons import LangToolButton
from gwhat.common import StyleDB
from gwhat.config.main import CONF
from gwhat.utils import icons
from gwhat.utils.icons import QToolButtonNormal, QToolButtonSmall
from gwhat.utils.icons import QToolButtonNormal, get_icon
from gwhat.utils.dates import qdatetime_from_xldate
from gwhat import brf_mod as bm
from gwhat.brf_mod import __install_dir__
Expand Down Expand Up @@ -123,11 +124,12 @@ def install_kgsbrf(self):

class BRFManager(myqt.QFrameLayout):
sig_brfperiod_changed = QSignal(list)
sig_select_brfperiod_requested = QSignal(bool)

def __init__(self, wldset=None, parent=None):
super(BRFManager, self).__init__(parent)
self._brfperiod_selection_toggled = False

# Setup the BRF results viewer.
self.viewer = BRFViewer(wldset, parent)
self.viewer.btn_language.set_language(
CONF.get('brf', 'graphs_labels_language'))
Expand Down Expand Up @@ -166,14 +168,14 @@ def __initGUI__(self):
CONF.get('brf', 'detrend_waterlevels'))

# Setup options layout.
options_layout = QGridLayout()
options_grpbox = QGroupBox()
options_layout = QGridLayout(options_grpbox)
options_layout.addWidget(QLabel('Nbr of BP lags :'), 0, 0)
options_layout.addWidget(self.baro_spinbox, 0, 2)
options_layout.addWidget(self.earthtides_cbox, 1, 0)
options_layout.addWidget(self.earthtides_spinbox, 1, 2)
options_layout.addWidget(self.detrend_waterlevels_cbox, 2, 0, 1, 3)
options_layout.setColumnStretch(1, 100)
options_layout.setContentsMargins(0, 0, 0, 0)

# Setup BRF date range widgets.
self.date_start_edit = QDateTimeEdit()
Expand All @@ -192,44 +194,49 @@ def __initGUI__(self):
self.date_end_edit.dateChanged.connect(
lambda: self.wldset.save_brfperiod(self.get_brfperiod()))

self.btn_seldata = OnOffToolButton('select_range', size='small')
self.btn_seldata.setToolTip("Select a BRF calculation period with "
"the mouse cursor on the graph.")
self._select_brfperiod_btn = QPushButton('Select')
self._select_brfperiod_btn.setIcon(get_icon('select_range'))
self._select_brfperiod_btn.setToolTip(
"Select a BRF calculation period.")
self._select_brfperiod_btn.setCheckable(True)
self._select_brfperiod_btn.setFocusPolicy(Qt.NoFocus)
self._select_brfperiod_btn.toggled.connect(
self.toggle_brfperiod_selection)

# Setup BRF date range layout.
daterange_layout = QGridLayout()
daterange_grpbox = QGroupBox()
daterange_layout = QGridLayout(daterange_grpbox)
daterange_layout.addWidget(QLabel('BRF Start :'), 0, 0)
daterange_layout.addWidget(self.date_start_edit, 0, 2)
daterange_layout.addWidget(QLabel('BRF End :'), 1, 0)
daterange_layout.addWidget(self.date_end_edit, 1, 2)
daterange_layout.addWidget(self._select_brfperiod_btn, 2, 0, 1, 3)
daterange_layout.setColumnStretch(1, 100)
daterange_layout.setContentsMargins(0, 0, 0, 0)

seldata_layout = QGridLayout()
seldata_layout.addWidget(self.btn_seldata, 0, 0)
seldata_layout.setRowStretch(1, 100)
seldata_layout.setContentsMargins(0, 0, 0, 0)

# Setup the toolbar.
btn_comp = QPushButton('Compute BRF')
btn_comp.clicked.connect(self.calc_brf)
btn_comp.setToolTip(
"Compute the barometric response function (BRF) of the well.")
btn_comp.setIcon(get_icon('play_start'))
btn_comp.setFocusPolicy(Qt.NoFocus)
btn_comp.clicked.connect(self.calc_brf)

self.btn_show = QToolButtonSmall(icons.get_icon('search'))
self.btn_show.clicked.connect(self.viewer.show)
self._show_brf_results_btn = QPushButton('Show BRF')
self._show_brf_results_btn.setIcon(get_icon('search'))
self._show_brf_results_btn.setToolTip(
"Show the BRF previously calculated for the well.")
self._show_brf_results_btn.setFocusPolicy(Qt.NoFocus)
self._show_brf_results_btn.clicked.connect(self.viewer.show)

# Setup the main Layout.
self.addLayout(daterange_layout, 0, 0)
self.addLayout(seldata_layout, 0, 1)
self.setRowMinimumHeight(1, 15)
self.addLayout(options_layout, 2, 0)
self.setRowMinimumHeight(3, 15)
self.setRowStretch(3, 100)
self.addWidget(btn_comp, 4, 0)
self.addWidget(self.btn_show, 4, 1)
self.setColumnStretch(0, 100)

# ---- Install Panel
self.addWidget(daterange_grpbox, 0, 0)
self.addWidget(options_grpbox, 1, 0)
self.setRowMinimumHeight(2, 15)
self.setRowStretch(2, 100)
self.addWidget(self._show_brf_results_btn, 4, 0)
self.addWidget(btn_comp, 5, 0)

# Setup the install KGS_BRF panel
if not KGSBRFInstaller().kgsbrf_is_installed():
self.__install_kgs_brf_installer()

Expand Down Expand Up @@ -309,7 +316,7 @@ def set_wldset(self, wldset):
"""Set the namespace for the wldset in the widget."""
self.wldset = wldset
self.viewer.set_wldset(wldset)
self.btn_seldata.setAutoRaise(True)
self.toggle_brfperiod_selection(False)
self.setEnabled(wldset is not None)
if wldset is not None:
xldates = self.wldset.xldates
Expand All @@ -331,6 +338,21 @@ def set_daterange(self, daterange):
widget.setMaximumDateTime(qdatetime_from_xldate(daterange[1]))
widget.blockSignals(False)

def toggle_brfperiod_selection(self, toggle):
"""
Toggle the brf selection on or off in the gui.
"""
self._brfperiod_selection_toggled = toggle
if toggle is True:
self.sig_select_brfperiod_requested.emit(toggle)
self._select_brfperiod_btn.blockSignals(True)
self._select_brfperiod_btn.setChecked(toggle)
self._select_brfperiod_btn.blockSignals(False)

def is_brfperiod_selection_toggled(self):
"""Return whether the BRF period selection is toggled."""
return self._brfperiod_selection_toggled

# ---- KGS BRF installer
def __install_kgs_brf_installer(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions gwhat/brf_mod/tests/test_brf_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_save_brf_figure(brfmanager, wldataset, mocker, qtbot,
brfmanager.show()
brfmanager.set_wldset(wldataset)

qtbot.mouseClick(brfmanager.btn_show, Qt.LeftButton)
qtbot.mouseClick(brfmanager._show_brf_results_btn, Qt.LeftButton)
qtbot.waitExposed(brfmanager.viewer)

# Save the figure in the file system.
Expand All @@ -166,7 +166,7 @@ def test_graph_panel(brfmanager, wldataset, mocker, qtbot):

graph_opt_panel = brfmanager.viewer.graph_opt_panel

qtbot.mouseClick(brfmanager.btn_show, Qt.LeftButton)
qtbot.mouseClick(brfmanager._show_brf_results_btn, Qt.LeftButton)
qtbot.waitExposed(brfmanager.viewer)

# Toggle on the panel and assert it is shown correctly.
Expand Down
Binary file removed gwhat/ressources/icons_png/search.png
Binary file not shown.
Loading