From 56ef872dfec365456e818af57f8fad9800ba1ba2 Mon Sep 17 00:00:00 2001 From: mar77i Date: Sat, 27 Apr 2024 20:12:18 +0200 Subject: [PATCH] break out draw_progress to mandellabel --- CMakeLists.txt | 2 + bigintmandelwidget.cpp | 93 +++++++++++++++--------------------------- bigintmandelwidget.h | 15 +++---- mandel.h | 9 ++-- mandellabel.cpp | 26 ++++++++++++ mandellabel.h | 28 +++++++++++++ menubar.h | 3 +- settingswidget.h | 2 +- 8 files changed, 106 insertions(+), 72 deletions(-) create mode 100644 mandellabel.cpp create mode 100644 mandellabel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4423de2..7d4929c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,8 @@ set(PROJECT_SOURCES settingswidget.h menubar.cpp menubar.h + mandellabel.cpp + mandellabel.h ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/bigintmandelwidget.cpp b/bigintmandelwidget.cpp index 70c8e5f..97dfa52 100644 --- a/bigintmandelwidget.cpp +++ b/bigintmandelwidget.cpp @@ -8,8 +8,9 @@ #include #include -#include "menubar.h" #include "bigintmandelwidget.h" +#include "mandellabel.h" +#include "menubar.h" static inline void start_calculation( QFutureWatcher *fw, QVector cells @@ -28,13 +29,11 @@ BigintMandelWidget::BigintMandelWidget(QWidget *parent) settings(128, QSize(502, 334)), menu_bar(new MenuBar(this)), scroll_area(new QScrollArea(this)), - img_label(new QLabel(this)), - img_dirty(true), + mandel_label(new MandelLabel(this)), status_bar(new QStatusBar(this)), - settings_widget(new SettingsWidget(this)), - draw_progress(-1) + settings_widget(new SettingsWidget(this)) { - scroll_area->setWidget(img_label); + scroll_area->setWidget(mandel_label); connect( settings_widget, &QDialog::accepted, @@ -59,7 +58,7 @@ BigintMandelWidget::BigintMandelWidget(QWidget *parent) layout()->addWidget(scroll_area); status_bar->setSizeGripEnabled(false); layout()->addWidget(status_bar); - img_label->resize(settings.get_params().get_size()); + mandel_label->resize(settings.get_params().get_size()); } BigintMandelWidget::~BigintMandelWidget() { @@ -69,65 +68,41 @@ BigintMandelWidget::~BigintMandelWidget() { void BigintMandelWidget::finished_cell(int num) { int y = num / settings.get_params().get_size().width(); - if (y > draw_progress) - draw_progress = y; + mandel_label->set_draw_progress(y); settings.finished_cell(num, fw->resultAt(num)); - update_img(); + update(); } void BigintMandelWidget::finished() { - draw_progress = -1; + mandel_label->set_draw_progress(-1); settings_widget->set_finished(true); - update_img(); -} - -static inline QPixmap enhance_pixmap(QPixmap pixmap, int draw_progress) { - QPainter qp; - if (draw_progress > -1) { - qp.begin(&pixmap); - qp.setPen(Qt::GlobalColor::gray); - qp.drawLine(0, draw_progress, pixmap.width() - 1, draw_progress); - qp.end(); - } - return pixmap; -} - -static inline void calculating_status( - QStatusBar *status_bar, int &prev_num_threads -) { - QTextStream ss(new QString()); - int num_threads = QThreadPool::globalInstance()->activeThreadCount(); - if (prev_num_threads == num_threads) - return; - ss << "Calculating with " << num_threads << " threads ..."; - status_bar->showMessage(ss.readAll()); - prev_num_threads = num_threads; -} - -static inline void finished_status(QStatusBar *status_bar) { - status_bar->showMessage("Click the rendering to zoom."); + update(); } -void BigintMandelWidget::paintEvent(QPaintEvent *event) { +void BigintMandelWidget::update_status_bar() { + QTextStream ss; static int prev_num_threads = -1; - if (!img_dirty) - return; - img_label->setPixmap(enhance_pixmap(settings.get_pixmap(), draw_progress)); - img_label->resize(img_label->pixmap().size()); - img_dirty = false; + int num_threads; if (fw->isFinished()) - finished_status(status_bar); - else { - calculating_status(status_bar, prev_num_threads); - prev_num_threads = -1; - } + num_threads = -1; + else + num_threads = QThreadPool::globalInstance()->activeThreadCount(); + if (num_threads == prev_num_threads) + return; + ss.setString(new QString()); + if (num_threads != -1) + ss << "Calculating with " << num_threads << " threads ..."; + else + ss << "Click the rendering to zoom."; + status_bar->showMessage(*ss.string()); + prev_num_threads = num_threads; } void BigintMandelWidget::mousePressEvent(QMouseEvent *event) { QSize size(settings.get_params().get_size()); QPoint pos(event->pos()); QWidget *w; - for (w = img_label; w != this; w = w->parentWidget()) + for (w = mandel_label; w != this; w = w->parentWidget()) pos = w->mapFromParent(pos); if (event->button() != Qt::MouseButton::LeftButton || !fw->isFinished() @@ -138,11 +113,6 @@ void BigintMandelWidget::mousePressEvent(QMouseEvent *event) { return; settings.zoom(get_ideal_size(), menu_bar->get_zoom_factor(), pos); start_calculation(fw, settings.get_cells()); - update_img(); -} - -void BigintMandelWidget::update_img() { - img_dirty = true; update(); } @@ -196,13 +166,13 @@ void BigintMandelWidget::reset() { fw->waitForFinished(); settings.reset(128, get_ideal_size()); start_calculation(fw, settings.get_cells()); - update_img(); + update(); } void BigintMandelWidget::settings_widget_accepted() { settings.set_max_iter(settings_widget->get_max_iter().toULongLong()); start_calculation(fw, settings.get_cells()); - update_img(); + update(); } void BigintMandelWidget::load_data() { @@ -224,7 +194,7 @@ void BigintMandelWidget::load_data() { if (file_name.endsWith(".qcompress")) qba = qUncompress(qba); settings.from_json(QJsonDocument::fromJson(qba).object()); - update_img(); + update(); } void BigintMandelWidget::save_data() { @@ -251,3 +221,8 @@ void BigintMandelWidget::save_data() { file.write(qba); file.close(); } + +void BigintMandelWidget::update() { + update_status_bar(); + QWidget::update(); +} diff --git a/bigintmandelwidget.h b/bigintmandelwidget.h index 6960c52..2101cb6 100644 --- a/bigintmandelwidget.h +++ b/bigintmandelwidget.h @@ -5,7 +5,6 @@ #define BIGINTMANDELWIDGET_H #include -#include #include #include #include @@ -15,6 +14,7 @@ #include "settingswidget.h" class MenuBar; +class MandelLabel; class BigintMandelWidget : public QWidget { Q_OBJECT @@ -23,22 +23,22 @@ class BigintMandelWidget : public QWidget { MandelSettings settings; MenuBar *menu_bar; QScrollArea *scroll_area; - QLabel *img_label; - bool img_dirty; + MandelLabel *mandel_label; QStatusBar *status_bar; SettingsWidget *settings_widget; - int draw_progress; + void update_status_bar(); inline const QSize get_ideal_size() const { return scroll_area->size().shrunkBy(scroll_area->contentsMargins()); } public: - BigintMandelWidget(QWidget *parent = nullptr); + explicit BigintMandelWidget(QWidget *parent = nullptr); ~BigintMandelWidget(); - void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *event); - void update_img(); + inline const MandelSettings *get_settings() const { + return &settings; + } public Q_SLOTS: void reset(); @@ -49,6 +49,7 @@ public Q_SLOTS: void finished(); void load_data(); void save_data(); + void update(); }; #endif // BIGINTMANDELWIDGET_H diff --git a/mandel.h b/mandel.h index 03a564e..3d31b7d 100644 --- a/mandel.h +++ b/mandel.h @@ -25,7 +25,8 @@ class MandelResultCell { MpzPoint rpos; public: MandelResultCell() : iter(0) {} - MandelResultCell(size_t iter, MpzPoint rpos) : iter(iter), rpos(rpos) {} + explicit MandelResultCell(size_t iter, MpzPoint rpos) + : iter(iter), rpos(rpos) {} inline const size_t get_iter() const { return iter; } inline const MpzPoint get_rpos() const { return rpos; } }; @@ -40,7 +41,7 @@ private: public: // required by the MandelSettings constructor MandelParams(); - MandelParams( + explicit MandelParams( size_t max_iter, QSize size, MpzPoint center_f, @@ -67,7 +68,7 @@ class MandelSettings { QVector cells; public: - MandelSettings(size_t max_iter, QSize size); + explicit MandelSettings(size_t max_iter, QSize size); inline QPixmap get_pixmap() const { return QPixmap::fromImage(*img); } inline const QVector get_cells() const { return cells; } inline const MandelParams &get_params() const { return params; } @@ -91,7 +92,7 @@ class MandelCell { public: MandelCell(const MandelParams *params); - MandelCell( + explicit MandelCell( const MandelParams *params, const QPoint pos, const MpzPoint rpos0, diff --git a/mandellabel.cpp b/mandellabel.cpp new file mode 100644 index 0000000..397b166 --- /dev/null +++ b/mandellabel.cpp @@ -0,0 +1,26 @@ + +// mandellabel.cpp + +#include + +#include "mandellabel.h" + +MandelLabel::MandelLabel(BigintMandelWidget *parent) +: QLabel(parent), + settings(parent->get_settings()), + draw_progress(-1) {} + +void MandelLabel::paintEvent(QPaintEvent *event) { + QPixmap pixmap; + QPainter qp; + pixmap = settings->get_pixmap(); + if (draw_progress != -1) { + qp.begin(&pixmap); + qp.setPen(Qt::GlobalColor::gray); + qp.drawLine(0, draw_progress, pixmap.width() - 1, draw_progress); + qp.end(); + } + resize(pixmap.size()); + setPixmap(pixmap); + QLabel::paintEvent(event); +} diff --git a/mandellabel.h b/mandellabel.h new file mode 100644 index 0000000..4d5fccd --- /dev/null +++ b/mandellabel.h @@ -0,0 +1,28 @@ + +// mandellabel.h + +#ifndef MANDELLABEL_H +#define MANDELLABEL_H + +#include + +#include "bigintmandelwidget.h" +#include "mandel.h" + +class MandelLabel : public QLabel { + const MandelSettings *settings; + int draw_progress; + +public: + explicit MandelLabel(BigintMandelWidget *parent = nullptr); + void paintEvent(QPaintEvent *event); + inline int get_draw_progress() { return draw_progress; } + inline void set_draw_progress(int draw_progress) { + if (draw_progress == this->draw_progress) + return; + if (draw_progress == -1 || draw_progress > this->draw_progress) + this->draw_progress = draw_progress; + } +}; + +#endif // MANDELLABEL_H diff --git a/menubar.h b/menubar.h index b436ed4..d251e61 100644 --- a/menubar.h +++ b/menubar.h @@ -1,3 +1,4 @@ + // menubar.h #ifndef MENUBAR_H @@ -25,7 +26,7 @@ class MenuBar : public QMenuBar { } public: - MenuBar(BigintMandelWidget *parent); + explicit MenuBar(BigintMandelWidget *parent); inline int get_zoom_factor() const { return zoom_factor; } public Q_SLOTS: void change_zoom_factor(); diff --git a/settingswidget.h b/settingswidget.h index 3e7470d..0fab8c1 100644 --- a/settingswidget.h +++ b/settingswidget.h @@ -17,7 +17,7 @@ class SettingsWidget : public QDialog { bool is_finished; public: - SettingsWidget(QWidget *parent = nullptr); + explicit SettingsWidget(QWidget *parent = nullptr); void update_fields(const MandelParams ¶ms, const bool is_finished); inline const QString get_max_iter() const { return max_iter->text(); }; -- 2.47.0