Commit 73e56a5f authored by Yan's avatar Yan

Rewritten image from figure generator to class

* Created ImagePainter class, to be implemented instead of various
  functions across Prasopes
parent 22051abf
......@@ -6,12 +6,14 @@ from PyQt5 import QtGui
from PyQt5 import QtPrintSupport
from rawprasslib import load_raw
from prasopes.predictmz import predict as getmzpattern
from pathlib import Path
try:
from rawautoparams import load_params
import rawautoparams
autoparams = True
except ImportError:
autoparams = False
import copy
import rawprasslib
import numpy as np
import prasopes.config as cf
......@@ -142,8 +144,9 @@ def print_graph(augCanvas):
printer = printPreview.printer()
printer.setPageSize(printer.A5)
printer.setDuplex(printer.DuplexNone)
image = imgt.paint_image(augCanvas.ms, augCanvas.spectplot,
augCanvas.filename, printer)
imggen = imgt.ImagePainter("msspec", printer)
imggen.popfig = lambda: popfig_override(imggen, augCanvas)
image = imggen.paint()
printPreview.paintRequested.connect(lambda:
printimage(printer, image))
printPreview.exec()
......@@ -226,6 +229,30 @@ def oddeven_changed(augCanvas, config, oddevenact):
update_spectrum(augCanvas, config)
def popfig_override(self, augCanvas):
self.plot.set_xlim(augCanvas.spectplot.get_xlim())
self.plot.set_ylim(augCanvas.spectplot.get_ylim())
data = [line.get_data() for line in augCanvas.spectplot.lines]
texts = copy.copy(augCanvas.ms)
if len(augCanvas.ms['headers']) != 0:
legend = augCanvas.spectplot.get_legend().get_texts()
[gt.pop_plot(*line, self.plot, texts, i, legend[i].get_text(),
not (cf.settings().value(
self.conftype.format("onlymanann"), type=bool)))
for i, line in enumerate(data)]
legend = 1
if legend == True:
self.plot.legend(loc=2)
else:
[gt.pop_plot(*line, self.plot, texts, i)
for i, line in enumerate(data)]
xtics = int(cf.settings().value(self.conftype.format("xtics")))
self.plot.locator_params(nbins=xtics, axis='x')
if augCanvas.filename not in (None, [None]):
self.plot.set_title(Path(
*Path(augCanvas.filename).resolve().parts[-2:]), loc="right")
def key_pressed(event, augCanvas, config, update):
if event.key() == QtCore.Qt.Key_F5:
update_spectrum(augCanvas, config)
......@@ -236,8 +263,9 @@ def key_pressed(event, augCanvas, config, update):
if augCanvas.paramstable.underMouse():
dt.clip_tablestr(augCanvas)
else:
imgt.clip_spect_img(augCanvas.ms, augCanvas.spectplot,
augCanvas.filename)
painter = imgt.ImagePainter("msspec")
painter.popfig = lambda: popfig_override(painter, augCanvas)
painter.clip()
if event.modifiers().__int__() == QtCore.Qt.ControlModifier + \
QtCore.Qt.ShiftModifier:
dt.clip_spectstr(augCanvas)
......
......@@ -12,15 +12,15 @@ def settings():
("view/acqparvisible", True),
("view/intensities", False),
("view/oddeven", False),
("print/xinch", 10),
("print/yinch", 4),
("print/dpi", 300),
("print/xtics", 5),
("imggen/xinch", 10),
("imggen/yinch", 4),
("imggen/dpi", 300),
("imggen/xtics", 5),
("imggen/onlymanann", False),
("print/msspec_xinch", 10),
("print/msspec_yinch", 4),
("print/msspec_dpi", 300),
("print/msspec_xtics", 5),
("imggen/msspec_xinch", 10),
("imggen/msspec_yinch", 4),
("imggen/msspec_dpi", 300),
("imggen/msspec_xtics", 5),
("imggen/msspec_onlymanann", False),
("reactivity/index", 0),
("recents", "")}
[settings.setValue(*i)
......@@ -107,15 +107,15 @@ def dial(parent):
imggentab = QtWidgets.QWidget()
imggenlayout = QtWidgets.QVBoxLayout(imggentab)
imggenlayout.addLayout(posvarconf(
"Figure width (inch)", "imggen/xinch", config, "nonint"))
"Figure width (inch)", "imggen/msspec_xinch", config, "nonint"))
imggenlayout.addLayout(posvarconf(
"Figure height (inch)", "imggen/yinch", config, "nonint"))
"Figure height (inch)", "imggen/msspec_yinch", config, "nonint"))
imggenlayout.addLayout(posvarconf(
"Figure dpi", "imggen/dpi", config))
"Figure dpi", "imggen/msspec_dpi", config))
imggenlayout.addLayout(posvarconf(
"Figure x axis major ticks count", "imggen/xtics", config))
"Figure x axis major ticks count", "imggen/msspec_xtics", config))
imggenlayout.addWidget(checkboxconf(
"Manual annotation only", "imggen/onlymanann", config))
"Manual annotation only", "imggen/msspec_onlymanann", config))
tabs.addTab(imggentab, "Image clip/export")
close_button = QtWidgets.QPushButton("Close")
......
......@@ -6,51 +6,43 @@ from io import BytesIO
from PyQt5 import QtGui
from PyQt5 import QtPrintSupport
from PyQt5 import QtWidgets
from pathlib import Path
import copy
import prasopes.graphtools as gt
import prasopes.config as cf
class ImagePainter:
"""base class for painting image into cache"""
def __init__(self, configname, painttarget=None):
self.painttartget = painttarget
self.confname = configname
self.conftype = ("print/{}_{{}}" if isinstance(
painttarget, type(QtPrintSupport.QPrinter()))
else "imggen/{}_{{}}").format(self.confname)
self.plot = None
def paint_image(mass_spec, spect, fn=None, painttarget=None):
"""generates QImage from mass spectrum"""
configtype = "print/{}" if isinstance(
painttarget, type(QtPrintSupport.QPrinter())) else "imggen/{}"
xinch, yinch = [float(cf.settings().value(configtype.format(i), type=str)\
.replace(",", ".")) for i in ("xinch", "yinch")]
dpi, xtics = [int(cf.settings().value(configtype.format(i))) for i
in ("dpi", "xtics")]
paintfig = Figure(figsize=(xinch, yinch), dpi=dpi, constrained_layout=True)
FigureCanvas(paintfig)
printplot = paintfig.add_subplot(111)
printplot.set_xlim(spect.get_xlim())
printplot.set_ylim(spect.get_ylim())
data = [line.get_data() for line in spect.lines]
texts = copy.copy(mass_spec)
if len(mass_spec['headers']) != 0:
legend = spect.get_legend().get_texts()
[gt.pop_plot(*line, printplot, texts, i, legend[i].get_text(),
not (cf.settings().value(
configtype.format("onlymanann"), type=bool)))
for i, line in enumerate(data)]
legend = 1
if legend == True:
printplot.legend(loc=2)
else:
[gt.pop_plot(*line, printplot, texts, i)
for i, line in enumerate(data)]
printplot.locator_params(nbins=xtics, axis='x')
if fn not in (None, [None]):
printplot.set_title(Path(*Path(fn).resolve().parts[-2:]),
loc="right")
cache_file = BytesIO()
paintfig.savefig(cache_file)
cache_file.seek(0)
image = QtGui.QImage.fromData(cache_file.read())
return image
def updatefig(self):
"""generates a figure with desired parameters"""
xinch, yinch = [float(
cf.settings().value(self.conftype.format(i), type=str).replace(
",", ".")) for i in ("xinch", "yinch")]
dpi = int(cf.settings().value(self.conftype.format("dpi")))
fig = Figure(figsize=(xinch, yinch), dpi=dpi, constrained_layout=True)
FigureCanvas(fig)
self.plot = fig.add_subplot(111)
def popfig(self):
"""to be overriden specifically"""
raise NotImplementedError
def clip_spect_img(mass_spec, spect, fn):
image = paint_image(mass_spec, spect, fn=fn)
QtWidgets.QApplication.clipboard().clear()
[QtWidgets.QApplication.clipboard().setImage(image, i) for i in range(2)]
def paint(self):
self.updatefig()
self.popfig()
cache_file = BytesIO()
self.plot.figure.savefig(cache_file)
cache_file.seek(0)
image = QtGui.QImage.fromData(cache_file.read())
return image
def clip(self):
img = self.paint()
QtWidgets.QApplication.clipboard().clear()
[QtWidgets.QApplication.clipboard().setImage(img, i) for i in range(2)]
Markdown is supported
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