Commit 061e29be authored by Yan's avatar Yan
Browse files

Copy spectrum image to clipboard

* cleaned-up signalization in __main__ (reallocated some calls from
  matplotlib directly to Qt)
* separated image painting for printing and copying to separate file
parent 026f80ce
......@@ -11,13 +11,13 @@ from PyQt5 import QtPrintSupport
from rawprasslib import load_raw
from rawprasslib import rawprasslib
from prasopes.predictmz import predict as getmzpattern
from io import BytesIO
import numpy as np
import prasopes.graphtools as gt
import prasopes.config as cf
import prasopes.drltools as drl
import prasopes.filetools as ft
import prasopes.graphtools as gt
import prasopes.imagetools as imgt
import prasopes.zcetools as zce
import prasopes.drltools as drl
import prasopes.config as cf
import prasopes.tangoicons
import sys
import matplotlib
......@@ -62,43 +62,31 @@ def load_file(parent, chrom_plot, spc, d_set, ms_s, fn, chrom_s,
def print_graph(data_set, mass_spec, chrom_spec, spect, fn):
def printimage(printdevice, img):
#!TODO: copy the printer settings from TSQ
painter = QtGui.QPainter(printdevice)
painter.drawImage(QtCore.QPoint(0,0), img)
painter.end()
printfig = Figure(figsize=(10, 4), dpi=300)
canvas = FigureCanvas(printfig)
printplot = printfig.add_subplot(111)
printplot.set_xlim(spect.get_xlim())
printplot.set_ylim(spect.get_ylim())
ydata = [line.get_data()[1] for line in spect.lines]
for yline in ydata:
spect = mass_spec.copy()
spect['y'] = yline
gt.pop_plot(printplot,spect)
cache_file = BytesIO()
printfig.savefig(cache_file)
cache_file.seek(0)
image = QtGui.QImage.fromData(cache_file.read())
#TODO: substitute the QPrintPreviewDialog with QPrintPreviewWidget
printPreview = QtPrintSupport.QPrintPreviewDialog()
printer = printPreview.printer()
printer.setResolution(600)
printer.setPageSize(printer.A5)
printer.setDuplex(printer.DuplexNone)
image = imgt.paint_image(mass_spec, spect, printer)
printPreview.paintRequested.connect(lambda:
printimage(printer, image))
printPreview.exec()
def update_spectrum(condition, chromatogram, spect, ds,
ms, fn, chrom, config):
if fn[0] is not None and condition:
def update_spectrum(chromatogram, spect, ds, ms, fn, chrom, config):
if fn[0] is not None:
slims = [spect.get_xlim(), spect.get_ylim()]
ds['chrom_dat'], ds['masses'], ds['matrix'] \
= load_raw(fn[0], config.value("tmp_location"))
gt.populate(chromatogram, spect, ds, ms, chrom)
spect.set_xlim(slims[0])
spect.set_ylim(slims[1])
gt.ann_spec(spect, ms)
spect.get_figure().canvas.draw()
......@@ -128,6 +116,14 @@ def predictmz(form, chromatogram, spect, ds, ms, chrom):
spect.get_figure().canvas.draw()
def key_pressed(event, chrom, spect, ds, ms_ds, fn, chrom_ds, config):
if event.key() == QtCore.Qt.Key_F5:
update_spectrum(chrom, spect, ds, ms_ds, fn, chrom_ds, config)
if event.key() == QtCore.Qt.Key_C\
and event.modifiers().__int__() == QtCore.Qt.ControlModifier:
imgt.clip_spect_img(ms_ds, spect)
def about(parent):
"""constructs window with "about" info"""
QtWidgets.QMessageBox.information(
......@@ -168,7 +164,6 @@ def main():
mpl_canvas = FigureCanvas(graph)
mpl_canvas.setStyleSheet("background-color:transparent;")
mpl_canvas.setAutoFillBackground(False)
mpl_canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
gt.pan_factory(chromatogram)
gt.zoom_factory(chromatogram, 1.15)
......@@ -245,8 +240,8 @@ def main():
oddevenact.triggered.connect(lambda: config.setValue(
"view/oddeven", oddevenact.isChecked()))
oddevenact.triggered.connect(lambda:
update_spectrum(True, chromatogram, spectrum, ds, ms,
filename, chrom, config))
update_spectrum(chromatogram, spectrum, ds, ms, filename, chrom,
config))
filebrowseract = QtWidgets.QAction(
"&File browser", None, checkable=True,
checked=config.value("view/filebrowservisible", type=bool))
......@@ -348,17 +343,16 @@ def main():
layout = QtWidgets.QVBoxLayout(main_widget)
layout.addWidget(toolBar)
layout.addWidget(mpl_canvas)
mpl_canvas.setFocus()
mpl_canvas.mpl_connect('key_press_event', lambda event:
update_spectrum(event.key == "f5", chromatogram, spectrum, ds, ms,
filename, chrom, config))
main_window.setFocus()
main_window.dragEnterEvent = lambda event: drag_entered(event)
main_window.dropEvent = lambda event: dropped(
event, main_window, chromatogram, spectrum, ds, ms, filename,
chrom, update, config)
main_window.setAcceptDrops(True)
main_window.keyPressEvent = lambda event: key_pressed(event, chromatogram,
spectrum, ds, ms, filename, chrom, config)
if len(sys.argv) == 2:
load_file(main_window, chromatogram, spectrum, ds, ms,
......
import numpy as np
import prasopes.datatools as dt
import prasopes.config as cf
from PyQt5 import QtWidgets
from PyQt5 import QtCore
def zoom_factory(axis, base_scale, plot_data=None):
......@@ -18,8 +20,8 @@ def zoom_factory(axis, base_scale, plot_data=None):
# should not happen
scale_factor = 1
print(event.button)
if event.key == 'shift':
if QtWidgets.QApplication.keyboardModifiers() ==\
QtCore.Qt.ShiftModifier:
data = event.ydata
new_top = data + (ax.get_ylim()[1] - data) \
* scale_factor
......
#!/usr/bin/env python3
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from io import BytesIO
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtPrintSupport
from PyQt5 import QtWidgets
import prasopes.graphtools as gt
def paint_image(mass_spec, spect, painttarget=None):
if isinstance(painttarget, type(QtPrintSupport.QPrinter())):
#TODO: make it configurable from settings
xinch = 10
yinch = 4
dpi = 300
else:
#TODO: make it configurable from settings
xinch = 10
yinch = 4
dpi = 300
paintfig = Figure(figsize=(xinch, yinch), dpi=dpi)
canvas = FigureCanvas(paintfig)
printplot = paintfig.add_subplot(111)
printplot.set_xlim(spect.get_xlim())
printplot.set_ylim(spect.get_ylim())
ydata = [line.get_data()[1] for line in spect.lines]
for yline in ydata:
mspect = mass_spec.copy()
mspect['y'] = yline
gt.pop_plot(printplot,mspect)
cache_file = BytesIO()
paintfig.savefig(cache_file)
cache_file.seek(0)
image = QtGui.QImage.fromData(cache_file.read())
return image
def clip_spect_img(mass_spec, spect):
image = paint_image(mass_spec, spect)
QtWidgets.QApplication.clipboard().setImage(image)
QtWidgets.QApplication.clipboard().setPixmap(QtGui.QPixmap.fromImage(image))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment