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),
saveSmallScreenshotPlot(false),
scaleDownFromHighDPI(false),
iterateFunctorSteps(false),
iterateFunctorStepsSupressInitial(false),
screenshotBasename("screenshot")
{
screenshotDir=QDir::current();
//for (int i=0; i<argc; i++) std::cout<<" argv["<<i<<"]="<<argv[i]<<"\n";
}
JKQTPExampleApplication::~JKQTPExampleApplication()
@ -26,6 +29,11 @@ JKQTPExampleApplication::~JKQTPExampleApplication()
}
void JKQTPExampleApplication::addExportStepFunctor(const std::function<void ()> &f)
{
functors<<f;
}
void JKQTPExampleApplication::readCmdLine() {
QCommandLineParser parser;
parser.setApplicationDescription(QString("JKQTPlotter example program '%1'").arg(applicationName()));
@ -47,6 +55,10 @@ void JKQTPExampleApplication::readCmdLine() {
parser.addOption(scaleDownFromHighDPIOption);
QCommandLineOption disablehighdpiOption(QStringList()<<"disablehighdpi", "idisable high-dpi support.");
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);
@ -57,6 +69,8 @@ void JKQTPExampleApplication::readCmdLine() {
saveScreenshotPlot = parser.isSet(screenshotPlotOption);
saveSmallScreenshotPlot = parser.isSet(smallscreenshotPlotOption);
scaleDownFromHighDPI = parser.isSet(scaleDownFromHighDPIOption);
iterateFunctorSteps = parser.isSet(iterateFunctorStepsOption);
iterateFunctorStepsSupressInitial = parser.isSet(iterateFunctorStepsSupressInitialOption);
}
QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor &exclusionColor)
@ -87,26 +101,109 @@ QRect JKQTPExampleApplication::getBoundsWithoutColor(QImage qImage, const QColor
int JKQTPExampleApplication::exec()
{
readCmdLine();
if (saveScreenshot||saveSmallScreenshot||saveScreenshotPlot||saveSmallScreenshotPlot) {
QList<JKQTPlotter*> plotterList;
auto checkPlottersResizeDone=[plotterList]() {
if (plotterList.isEmpty()) return true;
for (JKQTPlotter* p: plotterList) {
if (p->isResizeTimerRunning()) return false;
}
return true;
};
for (QWidget* w: allWidgets()) {
JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w);
if (plot) plotterList<<plot;
QList<JKQTPlotter*> plotterList;
auto checkPlottersResizeDone=[plotterList]() {
if (plotterList.isEmpty()) return true;
for (JKQTPlotter* p: plotterList) {
if (p->isResizeTimerRunning()) return false;
}
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;
});
return true;
};
for (QWidget* w: allWidgets()) {
JKQTPlotter* plot=dynamic_cast<JKQTPlotter*>(w);
if (plot) plotterList<<plot;
}
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);
QElapsedTimer timer;
timer.start();
@ -123,53 +220,10 @@ int JKQTPExampleApplication::exec()
for (int i=0; i<widgets.size(); i++) {
QWidget* w=widgets[i];
if (w->isVisible()) {
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));
}
}
saveWidget(w, iVisible);
iVisible++;
}
}
return 0;
} else {

View File

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