added context menu that allows to change graph visibility

This commit is contained in:
jkriege2 2018-08-19 18:17:18 +02:00
parent b0d8bea140
commit 7af0c7b6d1
5 changed files with 43 additions and 7 deletions

View File

@ -48,6 +48,22 @@ JKQTPgraph::JKQTPgraph(JKQtPlotter *parent):
setParent(parent->get_plotter());
}
QImage JKQTPgraph::generateKeyMarker(QSize size)
{
QImage img(size.width(),size.height(),QImage::Format_ARGB32);
if (parent) img.fill(parent->get_keyBackgroundColor());
{
JKQTPEnhancedPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
QRectF rect(0,0,size.width(),size.height());
drawKeyMarker(painter, rect);
}
return img;
}
QPointF JKQTPgraph::transform(const QPointF& x) {
if (xAxis&&yAxis) return QPointF(xAxis->x2p(x.x()), yAxis->x2p(x.y()));
return QPointF();

View File

@ -79,6 +79,8 @@ class LIB_EXPORT JKQTPgraph: public QObject {
virtual void draw(JKQTPEnhancedPainter& painter)=0;
/** \brief plots a key marker inside the specified rectangle \a rect */
virtual void drawKeyMarker(JKQTPEnhancedPainter& painter, QRectF& rect)=0;
/** \brief returns an image with a key marker inside */
QImage generateKeyMarker(QSize size=QSize(16,16));
/** \brief get the maximum and minimum x-value of the graph
*

View File

@ -473,6 +473,20 @@ void JKQtPlotter::initContextMenu()
contextMenu->addAction(plotter->get_actZoomAll());
contextMenu->addAction(plotter->get_actZoomIn());
contextMenu->addAction(plotter->get_actZoomOut());
contextMenu->addSeparator();
QMenu* menVisibleGroup=new QMenu(tr("Graph Visibility"), contextMenu);
for (size_t i=0; i<get_plotter()->getGraphCount(); i++) {
QString tit=get_plotter()->getGraph(i)->get_title();
if (tit.isEmpty()) tit=tr("Graph %1").arg(static_cast<int>(i));
QAction* act=new QAction(tit, menVisibleGroup);
act->setCheckable(true);
act->setChecked(get_plotter()->getGraph(i)->get_visible());
act->setIcon(QIcon(QPixmap::fromImage(get_plotter()->getGraph(i)->generateKeyMarker(QSize(16,16)))));
act->setData(static_cast<int>(i));
connect(act, SIGNAL(toggled(bool)), this, SLOT(reactGraphVisible(bool)));
menVisibleGroup->addAction(act);
}
contextMenu->addMenu(menVisibleGroup);
if (actions().size()>0) {
contextMenu->addSeparator();
@ -782,6 +796,14 @@ void JKQtPlotter::intBeforePlotScalingRecalculate() {
emit beforePlotScalingRecalculate();
}
void JKQtPlotter::reactGraphVisible(bool visible)
{
QAction* act=dynamic_cast<QAction*>(sender());
if (act) {
get_plotter()->setGraphVisible(act->data().toInt(), visible);
}
}
void JKQtPlotter::set_zoomByMouseRectangle(bool zomByrectangle) {
if (zomByrectangle) mouseActionMode=JKQtPlotter::ZoomRectangle;
else mouseActionMode=JKQtPlotter::NoMouseAction;

View File

@ -638,6 +638,8 @@ class LIB_EXPORT JKQtPlotter: public QWidget {
/** \brief emitted before the plot scaling has been recalculated */
void intBeforePlotScalingRecalculate();
/** \brief called from a menu entry that encodes the graph ID */
void reactGraphVisible(bool visible);
};

View File

@ -54,13 +54,7 @@ QVariant JKQTPPlotsModel::data(const QModelIndex &index, int role) const
if (index.row()<m_plotter->getGraphCount()) return m_plotter->getGraph(index.row())->get_visible()?Qt::Checked:Qt::Unchecked;
} else if (role == Qt::DecorationRole) {
if (index.row()<m_plotter->getGraphCount()) {
QImage img(16,16,QImage::Format_ARGB32);
{
JKQTPEnhancedPainter painter(&img);
QRectF rect(0,0,16,16);
m_plotter->getGraph(index.row())->drawKeyMarker(painter, rect);
}
return img;
return m_plotter->getGraph(index.row())->generateKeyMarker(QSize(16,16));
}
}
return QVariant();