added command-line option to tests that allows to modify a graph with a list of functors

This commit is contained in:
jkriege2 2022-09-26 02:07:07 +02:00
parent 465583b48d
commit d82e20fd33
2 changed files with 123 additions and 65 deletions

View File

@ -16,9 +16,12 @@ JKQTPExampleApplication::JKQTPExampleApplication(int &argc, char **argv, bool _w
saveScreenshotPlot(false), saveScreenshotPlot(false),
saveSmallScreenshotPlot(false), saveSmallScreenshotPlot(false),
scaleDownFromHighDPI(false), scaleDownFromHighDPI(false),
iterateFunctorSteps(false),
iterateFunctorStepsSupressInitial(false),
screenshotBasename("screenshot") screenshotBasename("screenshot")
{ {
screenshotDir=QDir::current(); screenshotDir=QDir::current();
//for (int i=0; i<argc; i++) std::cout<<" argv["<<i<<"]="<<argv[i]<<"\n";
} }
JKQTPExampleApplication::~JKQTPExampleApplication() JKQTPExampleApplication::~JKQTPExampleApplication()
@ -26,6 +29,11 @@ JKQTPExampleApplication::~JKQTPExampleApplication()
} }
void JKQTPExampleApplication::addExportStepFunctor(const std::function<void ()> &f)
{
functors<<f;
}
void JKQTPExampleApplication::readCmdLine() { void JKQTPExampleApplication::readCmdLine() {
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QString("JKQTPlotter example program '%1'").arg(applicationName())); parser.setApplicationDescription(QString("JKQTPlotter example program '%1'").arg(applicationName()));
@ -47,6 +55,10 @@ void JKQTPExampleApplication::readCmdLine() {
parser.addOption(scaleDownFromHighDPIOption); parser.addOption(scaleDownFromHighDPIOption);
QCommandLineOption disablehighdpiOption(QStringList()<<"disablehighdpi", "idisable high-dpi support."); QCommandLineOption disablehighdpiOption(QStringList()<<"disablehighdpi", "idisable high-dpi support.");
parser.addOption(disablehighdpiOption); parser.addOption(disablehighdpiOption);
QCommandLineOption iterateFunctorStepsOption(QStringList()<<"iteratefunctorsteps", "iterate over steps defined by addExportStepFunctor() in code.");
parser.addOption(iterateFunctorStepsOption);
QCommandLineOption iterateFunctorStepsSupressInitialOption(QStringList()<<"iteratefunctorsteps_suppressinitial", "iterate over steps defined by addExportStepFunctor() in code.");
parser.addOption(iterateFunctorStepsSupressInitialOption);
parser.process(*this); parser.process(*this);
@ -57,6 +69,8 @@ void JKQTPExampleApplication::readCmdLine() {
saveScreenshotPlot = parser.isSet(screenshotPlotOption); saveScreenshotPlot = parser.isSet(screenshotPlotOption);
saveSmallScreenshotPlot = parser.isSet(smallscreenshotPlotOption); saveSmallScreenshotPlot = parser.isSet(smallscreenshotPlotOption);
scaleDownFromHighDPI = parser.isSet(scaleDownFromHighDPIOption); scaleDownFromHighDPI = parser.isSet(scaleDownFromHighDPIOption);
iterateFunctorSteps = parser.isSet(iterateFunctorStepsOption);
iterateFunctorStepsSupressInitial = parser.isSet(iterateFunctorStepsSupressInitialOption);
} }
QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor) QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor)
@ -87,26 +101,109 @@ QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor
int JKQTPExampleApplication::exec() int JKQTPExampleApplication::exec()
{ {
readCmdLine(); readCmdLine();
if (saveScreenshot||saveSmallScreenshot||saveScreenshotPlot||saveSmallScreenshotPlot) {
QList<JKQTPlotter*> plotterList; QList<JKQTPlotter*> plotterList;
auto checkPlottersResizeDone=[plotterList]() { auto checkPlottersResizeDone=[plotterList]() {
if (plotterList.isEmpty()) return true; if (plotterList.isEmpty()) return true;
for (JKQTPlotter* p: plotterList) { for (JKQTPlotter* p: plotterList) {
if (p->isResizeTimerRunning()) return false; if (p->isResizeTimerRunning()) return false;
}
return true;
};
for (QWidget* w: allWidgets()) {
JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w);
if (plot) plotterList<<plot;
} }
QWidgetList widgets=topLevelWidgets(); return true;
std::sort(widgets.begin(), widgets.end(), [](const QWidget* a, const QWidget* b) { };
if (a && b) return a->windowTitle().toLower()<b->windowTitle().toLower(); for (QWidget* w: allWidgets()) {
if (a) return true; JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w);
if (b) return false; if (plot) plotterList<<plot;
return a<b; }
}); QWidgetList widgets=topLevelWidgets();
std::sort(widgets.begin(), widgets.end(), [](const QWidget* a, const QWidget* b) {
if (a && b) return a->windowTitle().toLower()<b->windowTitle().toLower();
if (a) return true;
if (b) return false;
return a<b;
});
auto saveWidget=[&](QWidget* w, int iVisible) {
JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w);
QString bn=screenshotBasename.value(iVisible, screenshotBasename.value(0));
if (iVisible>0 && screenshotBasename.value(iVisible, "")=="") {
bn+=QString("_win%1").arg(iVisible, 2, 10, QLatin1Char('0'));
}
if (w) {
QPixmap pix_win=w->grab();
/*QPixmap pix;
if (screenshotIncludeWindowTitle) {
pix=w->screen()->grabWindow(0, w->frameGeometry().x(), w->frameGeometry().y(), w->frameGeometry().width(), w->frameGeometry().height());
} else {
pix=pix_win;
}*/
if (saveScreenshot || (saveScreenshotPlot&&!plot)) {
if (scaleDownFromHighDPI && pix_win.devicePixelRatio()>1.0) {
pix_win.scaled((QSizeF(pix_win.size())/pix_win.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(bn+".png"));
} else {
pix_win.save(screenshotDir.absoluteFilePath(bn+".png"));
}
}
if (saveSmallScreenshot || (saveSmallScreenshotPlot&&!plot)) {
QPixmap img=pix_win.scaledToWidth(150, Qt::SmoothTransformation);
img.save(screenshotDir.absoluteFilePath(bn+"_small.png"));
}
}
if (plot) {
QString bnp=bn+"_plot";
QImage gr=plot->grabPixelImage();
if (saveScreenshotPlot) {
QString fn=bn+".png";
if (saveScreenshot) fn=bnp+".png";
if (scaleDownFromHighDPI && gr.devicePixelRatio()>1.0) {
gr.scaled((QSizeF(gr.size())/gr.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(fn));
} else {
gr.save(screenshotDir.absoluteFilePath(fn));
}
}
if (saveSmallScreenshotPlot) {
QString fn=bn+"_small.png";
if (saveSmallScreenshot) fn=bnp+"_small.png";
QImage img=gr.scaledToWidth(150, Qt::SmoothTransformation);
img.save(screenshotDir.absoluteFilePath(fn));
}
}
};
if (iterateFunctorSteps) {
QVector<std::function<void(void)>> localfunctors=functors;
if (!iterateFunctorStepsSupressInitial) localfunctors.prepend([](){});
int iVisible=0;
//std::cout<<localfunctors.size()<<" functors\n";
for (auto& f: localfunctors) {
//std::cout<<"iVisible="<<iVisible<<" f="<<std::boolalpha<<static_cast<bool>(f)<<"\n";
if (f) f();
//JKQTPlotter::setGlobalResizeDelay(10);
QElapsedTimer timer;
timer.start();
while(timer.elapsed()<JKQTPlotter::getGlobalResizeDelay()*3/2 || !readyForScreenshot || !checkPlottersResizeDone()) {
QApplication::processEvents();
QApplication::processEvents();
}
timer.start();
while(timer.elapsed()<50) {
QApplication::processEvents();
QApplication::processEvents();
}
for (int i=0; i<widgets.size(); i++) {
QWidget* w=widgets[i];
if (w->isVisible()) {
saveWidget(w, iVisible);
}
}
iVisible++;
}
return 0;
} else if (saveScreenshot||saveSmallScreenshot||saveScreenshotPlot||saveSmallScreenshotPlot) {
//std::cout<<"non-functor-mode\n";
//JKQTPlotter::setGlobalResizeDelay(10); //JKQTPlotter::setGlobalResizeDelay(10);
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
@ -123,53 +220,10 @@ int JKQTPExampleApplication::exec()
for (int i=0; i<widgets.size(); i++) { for (int i=0; i<widgets.size(); i++) {
QWidget* w=widgets[i]; QWidget* w=widgets[i];
if (w->isVisible()) { if (w->isVisible()) {
JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w); saveWidget(w, iVisible);
QString bn=screenshotBasename.value(iVisible, screenshotBasename.value(0));
if (iVisible>0 && screenshotBasename.value(iVisible, "")=="") {
bn+=QString("_win%1").arg(iVisible, 2, 10, QLatin1Char('0'));
}
if (w) {
QPixmap pix_win=w->grab();
/*QPixmap pix;
if (screenshotIncludeWindowTitle) {
pix=w->screen()->grabWindow(0, w->frameGeometry().x(), w->frameGeometry().y(), w->frameGeometry().width(), w->frameGeometry().height());
} else {
pix=pix_win;
}*/
if (saveScreenshot || (saveScreenshotPlot&&!plot)) {
if (scaleDownFromHighDPI && pix_win.devicePixelRatio()>1.0) {
pix_win.scaled((QSizeF(pix_win.size())/pix_win.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(bn+".png"));
} else {
pix_win.save(screenshotDir.absoluteFilePath(bn+".png"));
}
}
if (saveSmallScreenshot || (saveSmallScreenshotPlot&&!plot)) {
QPixmap img=pix_win.scaledToWidth(150, Qt::SmoothTransformation);
img.save(screenshotDir.absoluteFilePath(bn+"_small.png"));
}
}
if (plot) {
QString bnp=bn+"_plot";
QImage gr=plot->grabPixelImage();
if (saveScreenshotPlot) {
QString fn=bn+".png";
if (saveScreenshot) fn=bnp+".png";
if (scaleDownFromHighDPI && gr.devicePixelRatio()>1.0) {
gr.scaled((QSizeF(gr.size())/gr.devicePixelRatio()).toSize()).save(screenshotDir.absoluteFilePath(fn));
} else {
gr.save(screenshotDir.absoluteFilePath(fn));
}
}
if (saveSmallScreenshotPlot) {
QString fn=bn+"_small.png";
if (saveSmallScreenshot) fn=bnp+"_small.png";
QImage img=gr.scaledToWidth(150, Qt::SmoothTransformation);
img.save(screenshotDir.absoluteFilePath(fn));
}
}
iVisible++; iVisible++;
} }
} }
return 0; return 0;
} else { } else {

View File

@ -3,6 +3,7 @@
#include "jkqtplotter/jkqtplotter.h" #include "jkqtplotter/jkqtplotter.h"
#include "jkqtpappsettingcontroller.h" #include "jkqtpappsettingcontroller.h"
#include <QDir> #include <QDir>
#include <functional>
@ -12,7 +13,7 @@ public:
JKQTPExampleApplication(int &argc, char **argv, bool waitForScreenshotReady=false); JKQTPExampleApplication(int &argc, char **argv, bool waitForScreenshotReady=false);
virtual ~JKQTPExampleApplication(); virtual ~JKQTPExampleApplication();
void addExportStepFunctor(const std::function<void(void)>& f);
int exec(); int exec();
public slots: public slots:
void notifyReadyForScreenshot(); void notifyReadyForScreenshot();
@ -25,7 +26,10 @@ protected:
bool saveScreenshotPlot; bool saveScreenshotPlot;
bool saveSmallScreenshotPlot; bool saveSmallScreenshotPlot;
bool scaleDownFromHighDPI; bool scaleDownFromHighDPI;
bool iterateFunctorSteps;
bool iterateFunctorStepsSupressInitial;
QStringList screenshotBasename; QStringList screenshotBasename;
QVector<std::function<void(void)>> functors;
void readCmdLine(); void readCmdLine();
QRect getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor = Qt::white); QRect getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor = Qt::white);
}; };