mandel.h
settingswidget.cpp
settingswidget.h
+ menubar.cpp
+ menubar.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
#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
) {
: 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),
);
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);
|| 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();
}
#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());
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();
#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),
}
MandelSettings::MandelSettings(size_t max_iter, const QSize size)
-: img(nullptr)
-{
+: img(nullptr) {
reset(max_iter, size);
}
}
MandelCell::MandelCell(const MandelParams *params) : params(params) {}
-
MandelCell::MandelCell(const MandelCell &cell)
: params(cell.params), pos(cell.pos), result(cell.result),
rpos0(cell.rpos0) {}
#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; }
};
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; }
};
mpz_class left, top, one;
public:
+ // required by the MandelSettings constructor
MandelParams();
MandelParams(
size_t max_iter,
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;
--- /dev/null
+
+// 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();
+}
--- /dev/null
+// 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