From 19a1bc40d9b2c8b775491eddaface1653d053f84 Mon Sep 17 00:00:00 2001 From: mar77i Date: Sat, 27 Apr 2024 19:05:33 +0200 Subject: [PATCH] split out menubar --- CMakeLists.txt | 2 + bigintmandelwidget.cpp | 114 ++--------------------------------------- bigintmandelwidget.h | 10 ++-- mandel.cpp | 18 +------ mandel.h | 23 ++++----- menubar.cpp | 60 ++++++++++++++++++++++ menubar.h | 34 ++++++++++++ 7 files changed, 117 insertions(+), 144 deletions(-) create mode 100644 menubar.cpp create mode 100644 menubar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 18b2d1d..4423de2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,8 @@ set(PROJECT_SOURCES mandel.h settingswidget.cpp settingswidget.h + menubar.cpp + menubar.h ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/bigintmandelwidget.cpp b/bigintmandelwidget.cpp index 328fbdb..70c8e5f 100644 --- a/bigintmandelwidget.cpp +++ b/bigintmandelwidget.cpp @@ -8,116 +8,9 @@ #include #include +#include "menubar.h" #include "bigintmandelwidget.h" -static inline QMenuBar *setup_menu_bar(BigintMandelWidget *parent) { - QMenuBar *menu_bar = new QMenuBar(parent); - QMenu *menu = new QMenu("&File", parent); - QPushButton *button = new QPushButton("Settings"); - QAction *two, *four, *eight, *sixteen; - menu_bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - QObject::connect( - menu->addAction("&Reset"), - &QAction::triggered, - parent, - &BigintMandelWidget::reset - ); - QObject::connect( - menu->addAction("&Load"), - &QAction::triggered, - parent, - &BigintMandelWidget::load_data - ); - QObject::connect( - menu->addAction("&Save"), - &QAction::triggered, - parent, - &BigintMandelWidget::save_data - ); - menu->addSeparator(); - QObject::connect( - menu->addAction("&Export"), - &QAction::triggered, - parent, - &BigintMandelWidget::export_img - ); - menu->addSeparator(); - QObject::connect( - menu->addAction("E&xit"), - &QAction::triggered, - parent, - &BigintMandelWidget::close - ); - menu_bar->addMenu(menu); - menu = new QMenu("&Zoom factor", parent); - two = menu->addAction("&2"); - four = menu->addAction("&4"); - eight = menu->addAction("&8"); - sixteen = menu->addAction("1&6"); - two->setCheckable(true); - two->setChecked(true); - four->setCheckable(true); - eight->setCheckable(true); - sixteen->setCheckable(true); - QObject::connect( - two, - &QAction::triggered, - parent, - [two, four, eight, sixteen, parent]() { - two->setChecked(true); - four->setChecked(false); - eight->setChecked(false); - sixteen->setChecked(false); - parent->set_zoom_factor(2); - } - ); - QObject::connect( - four, - &QAction::triggered, - parent, - [two, four, eight, sixteen, parent]() { - two->setChecked(false); - four->setChecked(true); - eight->setChecked(false); - sixteen->setChecked(false); - parent->set_zoom_factor(4); - } - ); - QObject::connect( - eight, - &QAction::triggered, - parent, - [two, four, eight, sixteen, parent]() { - two->setChecked(false); - four->setChecked(false); - eight->setChecked(true); - sixteen->setChecked(false); - parent->set_zoom_factor(8); - } - ); - QObject::connect( - sixteen, - &QAction::triggered, - parent, - [two, four, eight, sixteen, parent]() { - two->setChecked(false); - four->setChecked(false); - eight->setChecked(false); - sixteen->setChecked(true); - parent->set_zoom_factor(16); - } - ); - menu_bar->addMenu(menu); - QObject::connect( - button, - &QPushButton::clicked, - parent, - &BigintMandelWidget::exec_settings_widget - ); - menu_bar->setCornerWidget(button); - return menu_bar; -} - static inline void start_calculation( QFutureWatcher *fw, QVector cells ) { @@ -133,6 +26,7 @@ BigintMandelWidget::BigintMandelWidget(QWidget *parent) : QWidget(parent), fw(new QFutureWatcher(this)), settings(128, QSize(502, 334)), + menu_bar(new MenuBar(this)), scroll_area(new QScrollArea(this)), img_label(new QLabel(this)), img_dirty(true), @@ -161,7 +55,7 @@ BigintMandelWidget::BigintMandelWidget(QWidget *parent) ); start_calculation(fw, settings.get_cells()); setLayout(new QVBoxLayout()); - layout()->addWidget(setup_menu_bar(this)); + layout()->addWidget(menu_bar); layout()->addWidget(scroll_area); status_bar->setSizeGripEnabled(false); layout()->addWidget(status_bar); @@ -242,7 +136,7 @@ void BigintMandelWidget::mousePressEvent(QMouseEvent *event) { || pos.y() < 0 || pos.y() >= size.height()) return; - settings.zoom(get_ideal_size(), zoom_factor, pos); + settings.zoom(get_ideal_size(), menu_bar->get_zoom_factor(), pos); start_calculation(fw, settings.get_cells()); update_img(); } diff --git a/bigintmandelwidget.h b/bigintmandelwidget.h index cc2007f..6960c52 100644 --- a/bigintmandelwidget.h +++ b/bigintmandelwidget.h @@ -14,17 +14,20 @@ #include "mandel.h" #include "settingswidget.h" +class MenuBar; + class BigintMandelWidget : public QWidget { Q_OBJECT QFutureWatcher *fw; MandelSettings settings; + MenuBar *menu_bar; QScrollArea *scroll_area; QLabel *img_label; bool img_dirty; QStatusBar *status_bar; SettingsWidget *settings_widget; - int draw_progress, zoom_factor; + int draw_progress; inline const QSize get_ideal_size() const { return scroll_area->size().shrunkBy(scroll_area->contentsMargins()); @@ -36,11 +39,8 @@ public: void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *event); void update_img(); - inline void set_zoom_factor(int zoom_factor) { - this->zoom_factor = zoom_factor; - } -public Q_SLOT: +public Q_SLOTS: void reset(); void export_img(); void exec_settings_widget(); diff --git a/mandel.cpp b/mandel.cpp index e723f8b..94d4024 100644 --- a/mandel.cpp +++ b/mandel.cpp @@ -8,30 +8,18 @@ #include "colors.h" #include "mandel.h" -MpzPoint::MpzPoint() : x(), y() {} -MpzPoint::MpzPoint(mpz_class x, mpz_class y) : x(x), y(y) {} -MpzPoint::MpzPoint(const MpzPoint &other) : x(other.x), y(other.y) {} - -MandelResultCell::MandelResultCell() : iter(0) {} - -MandelResultCell::MandelResultCell(size_t iter, MpzPoint rpos) -: iter(iter), rpos(rpos) {} - MandelParams::MandelParams() {} - MandelParams::MandelParams( size_t max_iter, QSize size, MpzPoint center_f, mpz_class one -) -: max_iter(max_iter), +) : max_iter(max_iter), size(size), center_f(center_f), one(one), left(center_f.get_x() - size.width() / 2.), top(center_f.get_y() + size.height() / 2.) {} - MandelParams::MandelParams(const MandelParams &other) : max_iter(other.max_iter), size(other.size), @@ -93,8 +81,7 @@ static inline QImage *setup_image(QImage *img, const QSize &size) { } MandelSettings::MandelSettings(size_t max_iter, const QSize size) -: img(nullptr) -{ +: img(nullptr) { reset(max_iter, size); } @@ -165,7 +152,6 @@ QJsonObject MandelSettings::to_json() { } MandelCell::MandelCell(const MandelParams *params) : params(params) {} - MandelCell::MandelCell(const MandelCell &cell) : params(cell.params), pos(cell.pos), result(cell.result), rpos0(cell.rpos0) {} diff --git a/mandel.h b/mandel.h index 14fc2a9..03a564e 100644 --- a/mandel.h +++ b/mandel.h @@ -10,14 +10,12 @@ #include #include -class MandelCell; - class MpzPoint { mpz_class x, y; public: - MpzPoint(); - MpzPoint(mpz_class x, mpz_class y); - MpzPoint(const MpzPoint &other); + MpzPoint() : x(), y() {} + explicit MpzPoint(mpz_class x, mpz_class y) : x(x), y(y) {} + MpzPoint(const MpzPoint &other) : x(other.x), y(other.y) {} inline const mpz_class get_x() const { return x; } inline const mpz_class get_y() const { return y; } }; @@ -26,8 +24,8 @@ class MandelResultCell { size_t iter; MpzPoint rpos; public: - MandelResultCell(); - MandelResultCell(size_t iter, MpzPoint rpos); + MandelResultCell() : iter(0) {} + 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,6 +38,7 @@ private: mpz_class left, top, one; public: + // required by the MandelSettings constructor MandelParams(); MandelParams( size_t max_iter, @@ -52,18 +51,16 @@ public: inline void set_max_iter(const size_t max_iter) { this->max_iter = max_iter; } inline const QSize get_size() const { return size; } inline const MpzPoint get_center_f() const { return center_f; } - inline const mpz_class get_left() const { - return left; - } - inline const mpz_class get_top() const { - return top; - } + inline const mpz_class get_left() const { return left; } + inline const mpz_class get_top() const { return top; } inline const mpz_class get_one() const { return one; } static MandelParams from_json(const QJsonObject &json); QJsonObject to_json() const; }; +class MandelCell; + class MandelSettings { MandelParams params; QImage *img; diff --git a/menubar.cpp b/menubar.cpp new file mode 100644 index 0000000..0042659 --- /dev/null +++ b/menubar.cpp @@ -0,0 +1,60 @@ + +// menubar.cpp + +#include +#include + +#include "menubar.h" + +static inline QAction *zoom_action( + MenuBar *menu_bar, QAction *action, int data +) { + QObject::connect( + action, + &QAction::triggered, + menu_bar, + &MenuBar::change_zoom_factor + ); + action->setData(data); + action->setCheckable(true); + return action; +} + +MenuBar::MenuBar(BigintMandelWidget *parent) +: QMenuBar(parent), + file_menu(new QMenu("&File", parent)), + zoom_factor_menu(new QMenu("&Zoom factor", parent)), + zoom_factor(2) { + QKeySequence no_key; + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + addMenu(file_menu); + reset_action = file_menu->addAction( + "&Reset", no_key, parent, &BigintMandelWidget::reset + ); + load_action = file_menu->addAction( + "&Load...", no_key, parent, &BigintMandelWidget::load_data + ); + save_action = file_menu->addAction( + "&Save...", no_key, parent, &BigintMandelWidget::save_data + ); + file_menu->addSeparator(); + export_action = file_menu->addAction( + "&Export...", no_key, parent, &BigintMandelWidget::export_img + ); + file_menu->addSeparator(); + exit_action = file_menu->addAction( + "E&xit", no_key, parent, &BigintMandelWidget::close + ); + + addMenu(zoom_factor_menu); + two_action = zoom_action(this, zoom_factor_menu->addAction("&2"), 2); + four_action = zoom_action(this, zoom_factor_menu->addAction("&4"), 4); + eight_action = zoom_action(this, zoom_factor_menu->addAction("&8"), 8); + sixteen_action = zoom_action(this, zoom_factor_menu->addAction("1&6"), 16); + check_action(); +} + +void MenuBar::change_zoom_factor() { + zoom_factor = static_cast(sender())->data().toInt(); + check_action(); +} diff --git a/menubar.h b/menubar.h new file mode 100644 index 0000000..b436ed4 --- /dev/null +++ b/menubar.h @@ -0,0 +1,34 @@ +// menubar.h + +#ifndef MENUBAR_H +#define MENUBAR_H + +#include + +#include "bigintmandelwidget.h" + +class MenuBar : public QMenuBar { + Q_OBJECT + + QMenu *file_menu; + QAction *reset_action, *load_action, *save_action; + QAction *export_action, *exit_action; + QMenu *zoom_factor_menu; + QAction *two_action, *four_action, *eight_action, *sixteen_action; + int zoom_factor; + + inline void check_action() { + two_action->setChecked(zoom_factor == 2); + four_action->setChecked(zoom_factor == 4); + eight_action->setChecked(zoom_factor == 8); + sixteen_action->setChecked(zoom_factor == 16); + } + +public: + MenuBar(BigintMandelWidget *parent); + inline int get_zoom_factor() const { return zoom_factor; } +public Q_SLOTS: + void change_zoom_factor(); +}; + +#endif // MENUBAR_H -- 2.47.0