* This file is covered by the LICENSE file in the root of this project.
*/
-#include <gmpxx.h>
-#include <QIntValidator>
#include <QLabel>
-#include <QMessageBox>
+#include <QLayout>
#include <QPushButton>
+#include <QMessageBox>
#include "settingswidget.h"
-#include "uint64validator.h"
-
-SettingsWidgetFieldBase::SettingsWidgetFieldBase(
- const QString &label, QWidget *parent
-) : edit(new QLineEdit("", parent)),
- label(label)
-{
- edit->setMinimumWidth(256);
-}
-
-void SettingsWidgetFieldBase::setup(
- QGridLayout *grid_layout,
- SettingsWidget *settings_widget,
- const char *method_name
-) {
- int row_count = grid_layout->rowCount();
- grid_layout->addWidget(
- new QLabel(label, edit->parentWidget()), row_count, 0
- );
- grid_layout->addWidget(edit, row_count, 1);
- QObject::connect(
- edit, SIGNAL(editingFinished()), settings_widget, method_name
- );
-}
-
-void SettingsWidgetFieldBase::reset() {
- edit->setText(orig_value);
-}
-
-SettingsWidgetField<quint64>::SettingsWidgetField(
- const QString &label, QWidget *parent
-) : SettingsWidgetFieldBase(label, parent) {
- edit->setValidator(new UInt64Validator(edit));
-}
-
-void SettingsWidgetField<quint64>::set_value(quint64 value) {
- orig_value = QString::number(value);
- reset();
-}
-
-SettingsWidgetField<int>::SettingsWidgetField(
- const QString &label, QWidget *parent
-) : SettingsWidgetFieldBase(label, parent) {
- edit->setValidator(new QIntValidator(edit));
-}
-
-void SettingsWidgetField<int>::set_value(int value) {
- orig_value = QString::number(value);
- reset();
-}
-
-SettingsWidgetField<mpz_class>::SettingsWidgetField(
- const QString &label, QWidget *parent
-) : SettingsWidgetFieldBase(label, parent) {
- static QRegularExpression number_re("^\\d+$");
- edit->setValidator(new QRegularExpressionValidator(number_re));
-}
-
-void SettingsWidgetField<mpz_class>::set_value(const mpz_class &value) {
- orig_value = QString::QString::fromStdString(value.get_str());
- reset();
-}
-inline void SettingsWidget::setup_button(
+static inline void setup_button(
QLayout *layout, QPushButton *button, const char *method_name
) {
- connect(
+ QObject::connect(
button,
SIGNAL(clicked()),
- this,
+ button->parentWidget(),
method_name
);
layout->addWidget(button);
}
+static inline void setup_edit(
+ SettingsWidget *parent,
+ QGridLayout *layout,
+ const QString &label,
+ QLineEdit *edit,
+ int y
+) {
+ layout->addWidget(new QLabel(label, parent), y, 0);
+ edit->setMinimumWidth(256);
+ QObject::connect(
+ edit,
+ &QLineEdit::editingFinished,
+ parent,
+ &SettingsWidget::update_form
+ );
+ layout->addWidget(edit, y, 1);
+}
+
SettingsWidget::SettingsWidget(QWidget *parent)
: QDialog(parent),
- max_iter("max_iter", this),
- width("width", this),
- height("height", this),
- center_f_x("x", this),
- center_f_y("y", this),
- one("one", this),
- clear_image_checkbox(new QCheckBox("Clear image", this)),
+ max_iter(new TypedLineEdit<quint64>(this)),
+ width(new TypedLineEdit<int>(this)),
+ height(new TypedLineEdit<int>(this)),
+ center_f_x(new TypedLineEdit<mpz_class>(this)),
+ center_f_y(new TypedLineEdit<mpz_class>(this)),
+ one(new TypedLineEdit<mpz_class>(this)),
+ clear_image(new QCheckBox("Clear image", this)),
updating(false)
{
QGridLayout *grid_layout = new QGridLayout(this);
QBoxLayout *button_row = new QBoxLayout(QBoxLayout::Direction::RightToLeft);
- int i;
- max_iter.setup(grid_layout, this, SLOT(update_form()));
- width.setup(grid_layout, this, SLOT(update_form()));
- height.setup(grid_layout, this, SLOT(update_form()));
- center_f_x.setup(grid_layout, this, SLOT(update_form()));
- center_f_y.setup(grid_layout, this, SLOT(update_form()));
- one.setup(grid_layout, this, SLOT(update_form()));
+ setup_edit(this, grid_layout, "max_iter", max_iter, 0);
+ setup_edit(this, grid_layout, "width", width, 1);
+ setup_edit(this, grid_layout, "height", height, 2);
+ setup_edit(this, grid_layout, "x", center_f_x, 3);
+ setup_edit(this, grid_layout, "y", center_f_y, 4);
+ setup_edit(this, grid_layout, "one", one, 5);
+
connect(
- clear_image_checkbox,
+ clear_image,
&QCheckBox::clicked,
this,
&SettingsWidget::update_form
);
- grid_layout->addWidget(clear_image_checkbox, grid_layout->rowCount(), 1);
+ grid_layout->addWidget(clear_image, grid_layout->rowCount(), 1);
setup_button(
button_row, new QPushButton("C&alculate", this), SLOT(accept())
);
grid_layout->addLayout(button_row, grid_layout->rowCount(), 0, 1, 2);
}
-void SettingsWidget::update_fields(const MandelParams ¶ms) {
- QSize size = params.get_size();
- MpzPoint center_f = params.get_center_f();
- max_iter.set_value(params.get_max_iter());
- width.set_value(size.width());
- height.set_value(size.height());
- center_f_x.set_value(center_f.get_x());
- center_f_y.set_value(center_f.get_y());
- one.set_value(params.get_one());
- clear_image_checkbox->setChecked(false);
+void SettingsWidget::update_params(const MandelParams ¶ms) {
+ max_iter->set_value(params.get_max_iter());
+ orig = params;
+ reset_params_fields();
+ clear_image->setChecked(false);
+}
+
+void SettingsWidget::reset_params_fields() {
+ QSize size = orig.get_size();
+ MpzPoint center_f = orig.get_center_f();
+ width->set_value(size.width());
+ height->set_value(size.height());
+ center_f_x->set_value(center_f.get_x());
+ center_f_y->set_value(center_f.get_y());
+ one->set_value(orig.get_one());
+}
+
+bool SettingsWidget::are_params_fields_changed() {
+ const QSize size = orig.get_size();
+ const MpzPoint center_f = orig.get_center_f();
+ return (
+ width->get_value() != size.width()
+ || height->get_value() != size.height()
+ || center_f_x->get_value() != center_f.get_x()
+ || center_f_y->get_value() != center_f.get_y()
+ || one->get_value() != orig.get_one()
+ );
+}
+
+MandelParams SettingsWidget::get_params() const {
+ return MandelParams(
+ max_iter->get_value(),
+ QSize(width->get_value(), height->get_value()),
+ MpzPoint(center_f_x->get_value(), center_f_y->get_value()),
+ one->get_value()
+ );
}
void SettingsWidget::update_form() {
bool params_fields_changed = are_params_fields_changed();
- bool max_iter_is_less = max_iter.get_value() < max_iter.get_orig_value();
+ bool max_iter_is_less = max_iter->get_value() < orig.get_max_iter();
if (
updating
- || clear_image_checkbox->isChecked()
+ || clear_image->isChecked()
|| (!params_fields_changed && !max_iter_is_less)
)
return;
"Choosing no will undo your change."
)
) == QMessageBox::StandardButton::Yes)
- clear_image_checkbox->setChecked(true);
+ clear_image->setChecked(true);
else {
if (params_fields_changed)
reset_params_fields();
if (max_iter_is_less)
- max_iter.reset();
+ max_iter->set_value(orig.get_max_iter());
}
updating = false;
}
void SettingsWidget::reset_form() {
- max_iter.reset();
+ max_iter->set_value(orig.get_max_iter());
reset_params_fields();
- clear_image_checkbox->setChecked(false);
+ clear_image->setChecked(false);
}
#include <QCheckBox>
#include <QDialog>
-#include <QLabel>
-#include <QLayout>
#include <QLineEdit>
#include "mandel.h"
+#include "uint64validator.h"
-class SettingsWidget;
+template <typename T> class TypedLineEdit;
-class SettingsWidgetFieldBase {
-protected:
- QLineEdit *edit;
- const QString label;
- QString orig_value;
-
-public:
- SettingsWidgetFieldBase(const QString &label, QWidget *parent = nullptr);
- inline bool is_edited() const { return edit->text() != orig_value; }
- void setup(
- QGridLayout *grid_layout,
- SettingsWidget *settings_widget,
- const char *method_name
- );
- void reset();
-};
-
-template <typename T> class SettingsWidgetField;
-template <>
-class SettingsWidgetField<quint64> : public SettingsWidgetFieldBase {
+template <> class TypedLineEdit<quint64> : public QLineEdit {
public:
- explicit SettingsWidgetField(
- const QString &label, QWidget *parent = nullptr
- );
- void set_value(quint64 value);
- inline const quint64 get_value() const {
- return edit->text().toULongLong();
+ explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {
+ setValidator(new UInt64Validator(this));
}
- inline const quint64 get_orig_value() const {
- return orig_value.toULongLong();
+ inline void set_value(const quint64 &value) {
+ setText(QString::number(value));
}
+ inline quint64 get_value() const { return text().toULongLong(); }
};
-template <>
-class SettingsWidgetField<int> : public SettingsWidgetFieldBase {
+template <> class TypedLineEdit<int> : public QLineEdit {
public:
- explicit SettingsWidgetField(
- const QString &label, QWidget *parent = nullptr
- );
- void set_value(int value);
- inline const int get_value() const {
- return edit->text().toInt();
+ explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {
+ setValidator(new QIntValidator(this));
}
- inline const int get_orig_value() const {
- return orig_value.toInt();
+ inline void set_value(const int &value) {
+ setText(QString::number(value));
}
+ inline int get_value() const { return text().toInt(); }
};
-template <>
-class SettingsWidgetField<mpz_class> : public SettingsWidgetFieldBase {
+template <> class TypedLineEdit<mpz_class> : public QLineEdit {
public:
- explicit SettingsWidgetField(
- const QString &label, QWidget *parent = nullptr
- );
- void set_value(const mpz_class &value);
- inline const mpz_class get_value() const {
- return mpz_class(edit->text().toStdString());
+ explicit TypedLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {
+ QRegularExpression number_re("^-?\\d+$");
+ setValidator(new QRegularExpressionValidator(number_re, this));
}
- inline const mpz_class get_orig_value() const {
- return mpz_class(orig_value.toStdString());
+ inline void set_value(const mpz_class &value) {
+ setText(QString::fromStdString(value.get_str()));
+ }
+ inline mpz_class get_value() const {
+ return mpz_class(text().toStdString());
}
};
class SettingsWidget : public QDialog {
Q_OBJECT
- SettingsWidgetField<quint64> max_iter;
- SettingsWidgetField<int> width, height;
- SettingsWidgetField<mpz_class> center_f_x, center_f_y, one;
- QCheckBox *clear_image_checkbox;
+ MandelParams orig;
+ TypedLineEdit<quint64> *max_iter;
+ TypedLineEdit<int> *width, *height;
+ TypedLineEdit<mpz_class> *center_f_x, *center_f_y, *one;
+ QCheckBox *clear_image;
bool updating;
- inline void setup_button(
- QLayout *layout, QPushButton *button, const char *method_name
- );
public:
explicit SettingsWidget(QWidget *parent = nullptr);
- void update_fields(const MandelParams ¶ms);
- inline const quint64 get_max_iter() const { return max_iter.get_value(); }
- inline bool are_params_fields_changed() {
- return (
- width.is_edited()
- || height.is_edited()
- || center_f_x.is_edited()
- || center_f_y.is_edited()
- || one.is_edited()
- );
- }
- inline void reset_params_fields() {
- width.reset();
- height.reset();
- center_f_x.reset();
- center_f_y.reset();
- one.reset();
- }
- inline const MandelParams get_params() {
- return MandelParams(
- max_iter.get_value(),
- QSize(width.get_value(), height.get_value()),
- MpzPoint(center_f_x.get_value(), center_f_y.get_value()),
- one.get_value()
- );
+ void update_params(const MandelParams ¶ms);
+ void reset_params_fields();
+ bool are_params_fields_changed();
+ inline const quint64 get_max_iter() const {
+ return max_iter->get_value();
}
- inline bool clear_image() const {
- return clear_image_checkbox->isChecked();
+ MandelParams get_params() const;
+ inline bool is_clear_image_checked() const {
+ return clear_image->isChecked();
}
public Q_SLOTS: