Skip to content

Commit

Permalink
Merge pull request #384 from jnsebgosselin/add_copy2clipboard_hydrocalc
Browse files Browse the repository at this point in the history
PR: Add copy to clipboard functionality in the "Analyze Hydrograph" toolbar
  • Loading branch information
jnsebgosselin authored Apr 23, 2021
2 parents c0154db + e16b891 commit 95236b1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
25 changes: 22 additions & 3 deletions gwhat/HydroCalc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# -----------------------------------------------------------------------------

# ---- Standard library imports
import io
from time import perf_counter
import csv
import os
Expand All @@ -17,6 +18,7 @@
# ---- Third party imports
import numpy as np
import pandas as pd
from qtpy.QtGui import QImage
from PyQt5.QtCore import Qt, QObject
from PyQt5.QtCore import pyqtSlot as QSlot
from PyQt5.QtCore import pyqtSignal as QSignal
Expand Down Expand Up @@ -242,6 +244,14 @@ def _setup_mpl_canvas(self):
def _setup_toolbar(self):
"""Setup the main toolbar of the water level calc tool."""

# Save and copy.
self.btn_copy_to_clipboard = create_toolbutton(
self, icon='copy_clipboard',
text="Copy",
tip="Put a copy of the figure on the Clipboard.",
triggered=self.copy_to_clipboard,
shortcut='Ctrl+C')

# ---- Navigate data.
self.toolbar = NavigationToolbar2QT(self.canvas, parent=self)
self.toolbar.hide()
Expand Down Expand Up @@ -350,7 +360,8 @@ def _setup_toolbar(self):

# Setup the layout.
toolbar = QToolBar()
for btn in [self.btn_home, self.btn_fit_waterlevels, self.btn_pan,
for btn in [self.btn_copy_to_clipboard, None,
self.btn_home, self.btn_fit_waterlevels, self.btn_pan,
self.btn_zoom_to_rect, None,
self.btn_wl_style, self.btn_dateFormat, None,
self.btn_show_glue, self.btn_show_weather,
Expand Down Expand Up @@ -574,6 +585,13 @@ def showEvent(self, event):
self.plot_brfperiod()
super().showEvent(event)

def copy_to_clipboard(self):
"""Put a copy of the figure on the clipboard."""
buf = io.BytesIO()
self.fig.savefig(buf, dpi=300)
QApplication.clipboard().setImage(QImage.fromData(buf.getvalue()))
buf.close()

# ---- MRC handlers
def add_mrcperiod(self, xdata):
"""
Expand Down Expand Up @@ -963,7 +981,8 @@ def setup_ax_margins(self, event=None):
fwidth = self.fig.get_figwidth()

left_margin = 1 / fwidth
right_margin = 1 / fwidth
right_margin = (
1 / fwidth if self.btn_show_weather.value() else 0.2 / fwidth)
bottom_margin = 0.75 / fheight
top_margin = 0.2 / fheight

Expand Down Expand Up @@ -1193,7 +1212,7 @@ def draw_weather(self):
time_bar, 0, ptot_bar, color=[165/255, 165/255, 165/255],
zorder=50, linestyle='None', alpha=0.65, lw=0)
self.h_etp.set_data(time_bin, etp_bin)
self.draw()
self.setup_ax_margins()

def draw_mrc(self):
"""
Expand Down
28 changes: 24 additions & 4 deletions gwhat/tests/test_hydrocalc.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# -*- coding: utf-8 -*-

# Copyright © 2014-2018 GWHAT Project Contributors
# -----------------------------------------------------------------------------
# Copyright © GWHAT Project Contributors
# https://github.com/jnsebgosselin/gwhat
#
# This file is part of GWHAT (Ground-Water Hydrograph Analysis Toolbox).
# Licensed under the terms of the GNU General Public License.
# -----------------------------------------------------------------------------

# ---- Standard Libraries Imports
import os
import os.path as osp

# ---- Third Party Libraries Imports
import pytest
from PyQt5.QtCore import Qt
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QApplication


# ---- Local Libraries Imports
from gwhat.meteo.weather_reader import WXDataFrame
Expand All @@ -22,7 +25,9 @@
from gwhat.projet.reader_projet import ProjetReader


# =============================================================================
# ---- Pytest Fixtures
# =============================================================================
DATADIR = osp.join(osp.dirname(osp.realpath(__file__)), 'data')
WXFILENAME = osp.join(DATADIR, "MARIEVILLE (7024627)_2000-2015.out")
WLFILENAME = osp.join(DATADIR, 'sample_water_level_datafile.csv')
Expand Down Expand Up @@ -64,10 +69,25 @@ def hydrocalc(datamanager, qtbot):
return hydrocalc


# ---- Test WLCalc
# =============================================================================
# ---- Tests
# =============================================================================
def test_hydrocalc_init(hydrocalc, mocker):
assert hydrocalc


def test_copy_to_clipboard(hydrocalc, qtbot):
"""
Test that puting a copy of the hydrograph figure on the clipboard is
working as expected.
"""
QApplication.clipboard().clear()
assert QApplication.clipboard().text() == ''
assert QApplication.clipboard().image().isNull()

qtbot.mouseClick(hydrocalc.btn_copy_to_clipboard, Qt.LeftButton)
assert not QApplication.clipboard().image().isNull()


if __name__ == "__main__":
pytest.main(['-x', __file__, '-v', '-rw'])

0 comments on commit 95236b1

Please sign in to comment.