]> git.mar77i.info Git - bigintmandel/commitdiff
clean up CMakeLists.txt and settingswidget
authormar77i <mar77i@protonmail.ch>
Fri, 10 May 2024 21:03:38 +0000 (23:03 +0200)
committermar77i <mar77i@protonmail.ch>
Fri, 10 May 2024 21:03:38 +0000 (23:03 +0200)
CMakeLists.txt
settingswidget.cpp
settingswidget.h

index 8558204b437bba8837d96cc3b0849d6276646c2c..9cf03b302ee17a0064fb8cf2fc8aac0181b6b333 100644 (file)
@@ -15,7 +15,7 @@ set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets Concurrent)
-find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Concurrent)
+find_package(Qt6 REQUIRED COMPONENTS Widgets Concurrent)
 
 set(PROJECT_SOURCES
         main.cpp
@@ -33,35 +33,23 @@ set(PROJECT_SOURCES
         uint64validator.h
 )
 
-if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
-    qt_add_executable(bigintmandel
-        MANUAL_FINALIZATION
-        ${PROJECT_SOURCES}
-        LICENSE
-    )
+qt_add_executable(bigintmandel
+    MANUAL_FINALIZATION
+    ${PROJECT_SOURCES}
+    LICENSE
+)
 # Define target properties for Android with Qt 6 as:
-#    set_property(TARGET bigintmandel APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
-#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
-# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
-else()
-    if(ANDROID)
-        add_library(bigintmandel SHARED
-            ${PROJECT_SOURCES}
-        )
-# Define properties for Android with Qt 5 after find_package() calls as:
-#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
-    else()
-        add_executable(bigintmandel
-            ${PROJECT_SOURCES}
-        )
-    endif()
-endif()
+#set_property(TARGET bigintmandel APPEND PROPERTY
+#             QT_ANDROID_PACKAGE_SOURCE_DIR
+#             ${CMAKE_CURRENT_SOURCE_DIR}/android)
+# For more information,
+# see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
 
 target_link_libraries(
     bigintmandel
     PRIVATE
-    Qt${QT_VERSION_MAJOR}::Widgets
-    Qt${QT_VERSION_MAJOR}::Concurrent
+    Qt6::Widgets
+    Qt6::Concurrent
     gmp
 )
 
@@ -74,7 +62,8 @@ endif()
 set_target_properties(bigintmandel PROPERTIES
     ${BUNDLE_ID_OPTION}
     MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
-    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
+    MACOSX_BUNDLE_SHORT_VERSION_STRING
+        ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
     MACOSX_BUNDLE TRUE
     WIN32_EXECUTABLE TRUE
 )
@@ -86,6 +75,4 @@ install(TARGETS bigintmandel
     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
 )
 
-if(QT_VERSION_MAJOR EQUAL 6)
-    qt_finalize_executable(bigintmandel)
-endif()
+qt_finalize_executable(bigintmandel)
index 9d0d4f2fe3179deddb7ee8368253918ea21b7471..4bb00c9e621ee7f841af620d230b4829c7471cd4 100644 (file)
@@ -6,9 +6,10 @@
  */
 
 #include <gmpxx.h>
-#include <QPushButton>
-#include <QLabel>
 #include <QIntValidator>
+#include <QLabel>
+#include <QMessageBox>
+#include <QPushButton>
 
 #include "settingswidget.h"
 #include "uint64validator.h"
@@ -21,18 +22,19 @@ SettingsWidgetFieldBase::SettingsWidgetFieldBase(
     edit->setMinimumWidth(256);
 }
 
-void SettingsWidgetFieldBase::add_to_layout(QGridLayout *grid_layout) {
+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);
-}
-
-void SettingsWidgetFieldBase::add_on_editing_finished(
-    SettingsWidget *settings_widget, const char *method_name
-) {
-    QObject::connect(edit, SIGNAL(editingFinished()), settings_widget, method_name);
+    QObject::connect(
+        edit, SIGNAL(editingFinished()), settings_widget, method_name
+    );
 }
 
 void SettingsWidgetFieldBase::reset() {
@@ -73,6 +75,18 @@ void SettingsWidgetField<mpz_class>::set_value(const mpz_class &value) {
     reset();
 }
 
+inline void SettingsWidget::setup_button(
+    QLayout *layout, QPushButton *button, const char *method_name
+) {
+    connect(
+        button,
+        SIGNAL(clicked()),
+        this,
+        method_name
+    );
+    layout->addWidget(button);
+}
+
 SettingsWidget::SettingsWidget(QWidget *parent)
 :   QDialog(parent),
     max_iter("max_iter", this),
@@ -81,45 +95,32 @@ SettingsWidget::SettingsWidget(QWidget *parent)
     center_f_x("x", this),
     center_f_y("y", this),
     one("one", this),
-    clear_image_checkbox(new QCheckBox("Clear image", this))
+    clear_image_checkbox(new QCheckBox("Clear image", this)),
+    updating(false)
 {
-    QPushButton *accept_button = new QPushButton("&Accept");
-    QPushButton *reject_button = new QPushButton("&Reject");
     QGridLayout *grid_layout = new QGridLayout(this);
     QBoxLayout *button_row = new QBoxLayout(QBoxLayout::Direction::RightToLeft);
     int i;
-    max_iter.add_to_layout(grid_layout);
-    width.add_to_layout(grid_layout);
-    width.add_on_editing_finished(this, SLOT(update_form()));
-    height.add_to_layout(grid_layout);
-    height.add_on_editing_finished(this, SLOT(update_form()));
-    center_f_x.add_to_layout(grid_layout);
-    center_f_x.add_on_editing_finished(this, SLOT(update_form()));
-    center_f_y.add_to_layout(grid_layout);
-    center_f_y.add_on_editing_finished(this, SLOT(update_form()));
-    one.add_to_layout(grid_layout);
-    one.add_on_editing_finished(this, SLOT(update_form()));
+    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()));
     connect(
         clear_image_checkbox,
-        &QCheckBox::checkStateChanged,
+        &QCheckBox::clicked,
         this,
-        &SettingsWidget::clear_image_checkbox_changed
+        &SettingsWidget::update_form
     );
     grid_layout->addWidget(clear_image_checkbox, grid_layout->rowCount(), 1);
-    connect(
-        accept_button,
-        &QPushButton::clicked,
-        this,
-        &SettingsWidget::accept
+    setup_button(
+        button_row, new QPushButton("C&alculate", this), SLOT(accept())
     );
-    button_row->addWidget(accept_button);
-    connect(
-        reject_button,
-        &QPushButton::clicked,
-        this,
-        &SettingsWidget::reject
+    setup_button(
+        button_row, new QPushButton("&Reset", this), SLOT(reset_form())
     );
-    button_row->addWidget(reject_button);
+    setup_button(button_row, new QPushButton("&Cancel", this), SLOT(reject()));
     grid_layout->addLayout(button_row, grid_layout->rowCount(), 0, 1, 2);
 }
 
@@ -136,24 +137,35 @@ void SettingsWidget::update_fields(const MandelParams &params) {
 }
 
 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();
     if (
-        width.is_edited()
-        || height.is_edited()
-        || center_f_x.is_edited()
-        || center_f_y.is_edited()
-        || one.is_edited()
+        updating
+        || clear_image_checkbox->isChecked()
+        || (!params_fields_changed && !max_iter_is_less)
     )
-        // todo: add a messagebox that we do this
+        return;
+    updating = true;
+    if (QMessageBox::question(
+        this,
+        "Bigintmandel Settings",
+        (
+            "Do you want to clear and recalculate the image?\n"
+            "Choosing no will undo your change."
+        )
+    ) == QMessageBox::StandardButton::Yes)
         clear_image_checkbox->setChecked(true);
+    else {
+        if (params_fields_changed)
+            reset_params_fields();
+        if (max_iter_is_less)
+            max_iter.reset();
+    }
+    updating = false;
 }
 
-void SettingsWidget::clear_image_checkbox_changed(Qt::CheckState state) {
-    if (state == Qt::Unchecked) {
-        // todo: add a messagebox if we should do this
-        width.reset();
-        height.reset();
-        center_f_x.reset();
-        center_f_y.reset();
-        one.reset();
-    }
+void SettingsWidget::reset_form() {
+    max_iter.reset();
+    reset_params_fields();
+    clear_image_checkbox->setChecked(false);
 }
index bf96d31bc94779cea6bcae8c713ba3741de5c8cf..67892306f300cfae183197e09132f943fa08761a 100644 (file)
@@ -27,9 +27,10 @@ protected:
 public:
     SettingsWidgetFieldBase(const QString &label, QWidget *parent = nullptr);
     inline bool is_edited() const { return edit->text() != orig_value; }
-    void add_to_layout(QGridLayout *grid_layout);
-    void add_on_editing_finished(
-        SettingsWidget *settings_widget, const char *method_name
+    void setup(
+        QGridLayout *grid_layout,
+        SettingsWidget *settings_widget,
+        const char *method_name
     );
     void reset();
 };
@@ -45,6 +46,9 @@ public:
     inline const quint64 get_value() const {
         return edit->text().toULongLong();
     }
+    inline const quint64 get_orig_value() const {
+        return orig_value.toULongLong();
+    }
 };
 
 template <>
@@ -57,6 +61,9 @@ public:
     inline const int get_value() const {
         return edit->text().toInt();
     }
+    inline const int get_orig_value() const {
+        return orig_value.toInt();
+    }
 };
 
 template <>
@@ -69,6 +76,9 @@ public:
     inline const mpz_class get_value() const {
         return mpz_class(edit->text().toStdString());
     }
+    inline const mpz_class get_orig_value() const {
+        return mpz_class(orig_value.toStdString());
+    }
 };
 
 class SettingsWidget : public QDialog {
@@ -78,11 +88,31 @@ class SettingsWidget : public QDialog {
     SettingsWidgetField<int> width, height;
     SettingsWidgetField<mpz_class> center_f_x, center_f_y, one;
     QCheckBox *clear_image_checkbox;
+    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 &params);
     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(),
@@ -97,7 +127,7 @@ public:
 
 public Q_SLOTS:
     void update_form();
-    void clear_image_checkbox_changed(Qt::CheckState state);
+    void reset_form();
 };
 
 #endif // SETTINGSWIDGET_H