diff --git a/examples/studiMain.cpp b/examples/studiMain.cpp index 89b5b2a030f69fd006c08d2872635db64fe1d28a..d529ed0c4f1e1cb4243bd64f1f0fc1a92bb0e71d 100644 --- a/examples/studiMain.cpp +++ b/examples/studiMain.cpp @@ -133,7 +133,8 @@ void bubblesort() bool did_change = true; cute::resetPlot(0); cute::setPlotScale(0, cute::linear, cute::linear); - cute::plotValues(data); + if (cute::verbosity() >= cute::result) + cute::plotValues(data); cute::setLegendVisible(0, false); cute::setPlotVisible(1, false); cute::setPlotVisible(2, true); @@ -151,13 +152,16 @@ void bubblesort() did_change = false; for (size_t i = 0; i < data.size() - 1; ++i) { - cute::timerPause(); - cute::highlightValue(data[i]); - cute::highlightValue(data[i + 1]); - cute::delay(100); - cute::undo(0); - cute::undo(0); - cute::timerResume(); + if (cute::verbosity() >= cute::all) + { + cute::timerPause(); + cute::highlightValue(data[i]); + cute::highlightValue(data[i + 1]); + cute::delay(100); + cute::undo(0); + cute::undo(0); + cute::timerResume(); + } if (data[i] > data[i + 1]) { float temp = data[i]; @@ -165,22 +169,33 @@ void bubblesort() data[i + 1] = temp; did_change = true; ++swaps; - cute::timerPause(); - cute::undo(0); - cute::plotValues(data); - cute::highlightValue(data[i], 0, cute::Colors::yellow); - cute::highlightValue(data[i + 1], 0, cute::Colors::yellow); - cute::delay(300); - cute::undo(0); - cute::undo(0); - cute::logMessage(fvec2str(data)); - cute::setStatusMessage(std::to_string(swaps)); - cute::delay(200); - cute::timerResume(); + if (cute::verbosity() >= cute::partialResult) + { + cute::timerPause(); + cute::undo(0); + cute::plotValues(data); + if (cute::verbosity() >= cute::verbose) + { + cute::highlightValue(data[i], 0, cute::Colors::yellow); + cute::highlightValue(data[i + 1], 0, cute::Colors::yellow); + cute::delay(300); + cute::undo(0); + cute::undo(0); + cute::logMessage(fvec2str(data)); + cute::setStatusMessage(std::to_string(swaps)); + } + cute::delay(200); + cute::timerResume(); + } } } } cute::timerStop(); + if(cute::verbosity() == cute::result) + { + cute::undo(0); + cute::plotValues(data); + } if (cute::ok()) { diff --git a/include/cute.h b/include/cute.h index f812ebecd988b44091b2d82ceb334a6befd701c8..b3660de565895a5f879a9edaaab24921c467e8b7 100644 --- a/include/cute.h +++ b/include/cute.h @@ -15,6 +15,14 @@ namespace cute float x; float y; }; + enum Verbosity + { + silent, + result, + partialResult, + verbose, + all + }; enum Scale { linear, @@ -114,6 +122,9 @@ namespace cute int DYNAMIC run(void (*setup)()); bool DYNAMIC ok(); + Verbosity DYNAMIC verbosity(); + void DYNAMIC setVerbosity(Verbosity level); + void DYNAMIC timerStart(); void DYNAMIC timerPause(); void DYNAMIC timerResume(); diff --git a/src/configPanel.cpp b/src/configPanel.cpp index b55d798be63aa8ceb5a0667f9bad597e326e1eac..4e89f697843b625f357de73279a2b18d104898fb 100644 --- a/src/configPanel.cpp +++ b/src/configPanel.cpp @@ -23,11 +23,14 @@ ConfigPanel::ConfigPanel(QWidget* parent) : QDockWidget{parent} algoBox = new QComboBox(); f->layout()->addWidget(algoBox); - debugBox = new QComboBox(); - f->layout()->addWidget(debugBox); - debugBox->addItem("All"); - debugBox->addItem("Result"); - debugBox->addItem("None"); + verbosityBox = new QComboBox(); + f->layout()->addWidget(verbosityBox); + verbosityBox->addItem("Silent"); + verbosityBox->addItem("Result"); + verbosityBox->addItem("Partial Result"); + verbosityBox->addItem("Verbose"); + verbosityBox->addItem("All"); + verbosityBox->setCurrentIndex(4); runButton = new QPushButton("Start"); f->layout()->addWidget(runButton); diff --git a/src/configPanel.h b/src/configPanel.h index a2df0b6673b5a4d42a5e25b1df4a16f04fc6c232..50a54cd81079c0c8c00dad00938c7ae8fdbc6eda 100644 --- a/src/configPanel.h +++ b/src/configPanel.h @@ -21,7 +21,7 @@ public: QLineEdit* fileLine; QComboBox* algoBox; QSpinBox* problemSizeBox; - QComboBox* debugBox; + QComboBox* verbosityBox; QStackedWidget* stackedWidget; QWidget* fileWidget; QWidget* randomWidget; diff --git a/src/control.cpp b/src/control.cpp index 96ce7baef3af02c61c722a4b562de789be1599ff..1e4cb8e26bd790ba98d6cfdfce48e0a0d9759fc7 100644 --- a/src/control.cpp +++ b/src/control.cpp @@ -51,6 +51,11 @@ void Control::makeConnections() connect(w->configPanel->problemSizeBox, &QSpinBox::textChanged, this, &Control::onProblemSettingChanged); connect(w, &MainWindow::saveProblem, this, &Control::saveProblem); connect(w->cm, &ChartMenu::s_setPlotScale, this, &Control::setPlotScale); + connect(cc, &CuteControl::s_verbosityChanged, w, &MainWindow::changeVerbosity); + connect(w->configPanel->verbosityBox, + QOverload<int>::of(&QComboBox::currentIndexChanged), + cc, + &CuteControl::changeVerbosity); } int Control::run(void (*userSetup)()) @@ -194,6 +199,7 @@ void Control::startThread(void (*func)()) connect(thread, &AlgoThread::finished, thread, &QObject::deleteLater); connect(thread, &AlgoThread::finished, this, &Control::threadEnded); CuteControl::get().setProblem(getProblem()); + CuteControl::get().setVerbosity(w->configPanel->verbosityBox->currentIndex()); CuteControl::get().moveToWorkerThread(thread); w->configPanel->setRunning(true); threadRunning = true; @@ -514,7 +520,6 @@ void Control::resetPlot(int plot) void Control::undo(int plot) { using QtCharts::QAbstractSeries; - QList<QAbstractSeries*> series = w->charts[plot]->series(); if (plotHistory[plot].size() > 0) { auto lastEvent = plotHistory[plot].back(); @@ -523,7 +528,8 @@ void Control::undo(int plot) QAbstractSeries* series = std::get<QAbstractSeries*>(lastEvent); w->charts[plot]->removeSeries(series); plotHistory[plot].pop_back(); - std::remove(onTopSeries[plot].begin(), onTopSeries[plot].end(), series); + onTopSeries[plot].erase(std::remove(onTopSeries[plot].begin(), onTopSeries[plot].end(), series), + onTopSeries[plot].end()); series->deleteLater(); } else if (std::holds_alternative<QGraphicsItem*>(lastEvent)) diff --git a/src/cute.cpp b/src/cute.cpp index 51d1311b63e795e36c762f03601a79ecff83d3c4..f11bc1428ce1dbbb7c301e72c9e3ffd341a5fbcf 100644 --- a/src/cute.cpp +++ b/src/cute.cpp @@ -40,7 +40,7 @@ void cute::timerPause() } void cute::timerResume() { - if (timer::state == timer::paused) + if (timer::state == timer::paused || timer::state == timer::off) { timer::state = timer::running; timer::start = std::chrono::high_resolution_clock().now(); @@ -261,7 +261,30 @@ int cute::run(void (*setup)()) bool cute::ok() { timerPauseInternal(); - bool ok = !CuteControl::get().endRequested(); + static int i = 0; + if (verbosity() == cute::silent && ++i % 5000 != 0) + return true; + else + { + bool ok = !CuteControl::get().endRequested(); + CuteControl::get().updateVerbosity(); + timerResumeInternal(); + return ok; + } +} + + +cute::Verbosity cute::verbosity() +{ + timerPauseInternal(); + Verbosity level = CuteControl::get().getVerbosity(); + timerResumeInternal(); + return level; +} + +void cute::setVerbosity(Verbosity level) +{ + timerPauseInternal(); + CuteControl::get().setVerbosity(level); timerResumeInternal(); - return ok; } diff --git a/src/cuteControl.h b/src/cuteControl.h index 0893c21de12118ee4ecd2badaf21f317d6d057c1..75040141b1e4a240ea75e078f2400aeee6f47784 100644 --- a/src/cuteControl.h +++ b/src/cuteControl.h @@ -21,6 +21,8 @@ private: bool runningInThread; std::vector<cute::Point> problem2D; std::vector<float> problem1D; + cute::Verbosity verbosityLevel = cute::verbose; + cute::Verbosity nextVerbosityLevel = cute::verbose; signals: void s_setProgress(int n); @@ -41,6 +43,7 @@ signals: void s_highlightValue(float value, int plot, QColor c); void s_resetPlot(int plot); void s_undo(int plot); + void s_verbosityChanged(int level); public: static CuteControl& getInstance() @@ -231,8 +234,18 @@ public: } void setAxisVisible(int plot, bool x, bool y) { emit s_setAxisVisible(plot, x, y); } + cute::Verbosity getVerbosity() { return verbosityLevel; } + void setVerbosity(int level) + { + verbosityLevel = (cute::Verbosity)level; + nextVerbosityLevel = (cute::Verbosity)level; + emit s_verbosityChanged((int)level); + } + void updateVerbosity() { verbosityLevel = nextVerbosityLevel; } + public slots: void end() { endThread = true; } + void changeVerbosity(int level) { nextVerbosityLevel = (cute::Verbosity)level; } public: }; diff --git a/src/mainWindow.cpp b/src/mainWindow.cpp index 1ae16d14d8808bf54d0aa0a9ea685fbffe1d8c72..1de9468d80bba1ce1dcd695ad2132b7dcfcce528 100644 --- a/src/mainWindow.cpp +++ b/src/mainWindow.cpp @@ -179,3 +179,8 @@ void MainWindow::chartContextMenu(const QPoint& pos) { cm->popup(mapToParent(pos)); } + +void MainWindow::changeVerbosity(int level) +{ + configPanel->verbosityBox->setCurrentIndex(level); +} \ No newline at end of file diff --git a/src/mainWindow.h b/src/mainWindow.h index 3b00654434776dc4a8b05abd976ef3481b9f1a1c..43ffb80f7cdaf17581c68f152ad0bd7457e401a9 100644 --- a/src/mainWindow.h +++ b/src/mainWindow.h @@ -47,6 +47,7 @@ public slots: void switchToFile(); void switchToRandom(); void changeChartTheme(int themeNumber); + void changeVerbosity(int level); signals: void saveProblem();