]> git.mar77i.info Git - bigintmandel/commitdiff
split out menubar
authormar77i <mar77i@protonmail.ch>
Sat, 27 Apr 2024 17:05:33 +0000 (19:05 +0200)
committermar77i <mar77i@protonmail.ch>
Sat, 27 Apr 2024 17:05:33 +0000 (19:05 +0200)
CMakeLists.txt
bigintmandelwidget.cpp
bigintmandelwidget.h
mandel.cpp
mandel.h
menubar.cpp [new file with mode: 0644]
menubar.h [new file with mode: 0644]

index 18b2d1dc7edf3312c3f6a6efc99aa83f9b90d48e..4423de261ffce57c3544a1ae249257bb2f534ea6 100644 (file)
@@ -20,6 +20,8 @@ set(PROJECT_SOURCES
         mandel.h
         settingswidget.cpp
         settingswidget.h
+        menubar.cpp
+        menubar.h
 )
 
 if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
index 328fbdbc0a901053e8d0ee264f9141b085da1df0..70c8e5fd49e04df0d3670bf48860a393b2992ee1 100644 (file)
@@ -8,116 +8,9 @@
 #include <QPushButton>
 #include <QtConcurrent/QtConcurrent>
 
+#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<MandelResultCell> *fw, QVector<MandelCell> cells
 ) {
@@ -133,6 +26,7 @@ BigintMandelWidget::BigintMandelWidget(QWidget *parent)
 :   QWidget(parent),
     fw(new QFutureWatcher<MandelResultCell>(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();
 }
index cc2007f11dd83debcfc4e127724388f6f53888b9..6960c5240249d6d9f44da75ea12136348e7eef3f 100644 (file)
 #include "mandel.h"
 #include "settingswidget.h"
 
+class MenuBar;
+
 class BigintMandelWidget : public QWidget {
     Q_OBJECT
 
     QFutureWatcher<MandelResultCell> *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();
index e723f8b03fe13889eaba1254b4cd62aee4c95b73..94d4024a9a01cbe0ab097388dff35351ddea2058 100644 (file)
@@ -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) {}
index 14fc2a97e75581da2e741da328e80cd9c1705ec3..03a564e4d0c98fb3ed5396cdf4ec98cc796233df 100644 (file)
--- a/mandel.h
+++ b/mandel.h
 #include <QVector>
 #include <gmpxx.h>
 
-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 (file)
index 0000000..0042659
--- /dev/null
@@ -0,0 +1,60 @@
+
+// menubar.cpp
+
+#include <QPushButton>
+#include <qwidget.h>
+
+#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<QAction*>(sender())->data().toInt();
+    check_action();
+}
diff --git a/menubar.h b/menubar.h
new file mode 100644 (file)
index 0000000..b436ed4
--- /dev/null
+++ b/menubar.h
@@ -0,0 +1,34 @@
+// menubar.h
+
+#ifndef MENUBAR_H
+#define MENUBAR_H
+
+#include <QMenuBar>
+
+#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