diff --git a/qml/Main.qml b/qml/Main.qml index a7c2a70..5213209 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -93,10 +93,20 @@ ApplicationWindow { } } - CheckBox { - text: "Stop at sqrt(n)" - checked: true - onCheckedChanged: factorizationController.useSqrtOptimization = checked + RowLayout { + Layout.fillWidth: true + + CheckBox { + text: "Stop at sqrt(n)" + checked: factorizationController.useSqrtOptimization + onCheckedChanged: factorizationController.useSqrtOptimization = checked + } + + CheckBox { + text: "Pause when factor found" + checked: factorizationController.pauseOnFound + onCheckedChanged: factorizationController.pauseOnFound = checked + } } } } diff --git a/src/FactorizationController.cpp b/src/FactorizationController.cpp index dc66497..de4e1b8 100644 --- a/src/FactorizationController.cpp +++ b/src/FactorizationController.cpp @@ -3,7 +3,8 @@ #include FactorizationController::FactorizationController(QObject* parent) : - QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_originalNumber(0), m_currentFactor(0), m_stopFactor(0) { + QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_pauseOnFound(false), m_iterationsPerCycle(1), m_currentFactNumber(0), m_originalNumber(0), + m_currentFactor(0), m_stopFactor(0) { assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick)); } @@ -58,6 +59,18 @@ void FactorizationController::setUseSqrtOptimization(bool value) { reset(); } +bool FactorizationController::pauseOnFound() const { + return m_pauseOnFound; +} + +void FactorizationController::setPauseOnFound(bool value) { + if (value == m_pauseOnFound) + return; + + m_pauseOnFound = value; + emit pauseOnFoundChanged(); +} + int FactorizationController::iterationsPerCycle() const { return m_iterationsPerCycle; } @@ -208,6 +221,9 @@ void FactorizationController::factorize() { emit stopFactorChanged(); emit progressChanged(); + if (m_pauseOnFound) + stop(); + // Don't increase current factor, keep dividing by it until no longer divisible } else { m_currentFactor++; diff --git a/src/FactorizationController.h b/src/FactorizationController.h index 4a86c6f..dd41f38 100644 --- a/src/FactorizationController.h +++ b/src/FactorizationController.h @@ -26,6 +26,7 @@ class FactorizationController : public QObject { Q_PROPERTY(bool isPaused READ isPaused NOTIFY isPausedChanged) Q_PROPERTY(int iterationsPerCycle READ iterationsPerCycle WRITE setIterationsPerCycle NOTIFY iterationsPerCycleChanged) Q_PROPERTY(bool useSqrtOptimization READ useSqrtOptimization WRITE setUseSqrtOptimization NOTIFY useSqrtOptimizationChanged) + Q_PROPERTY(bool pauseOnFound READ pauseOnFound WRITE setPauseOnFound NOTIFY pauseOnFoundChanged) Q_PROPERTY(long long number READ number NOTIFY numberChanged) Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged) Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged) @@ -88,6 +89,12 @@ class FactorizationController : public QObject { */ bool useSqrtOptimization() const; + /** + * @brief Checks whether the computation should be paused once a new factor is found. + * @return True if the computation will get paused on new factor being found, otherwise False. + */ + bool pauseOnFound() const; + /** * @brief Gets the original number being factorized. * @return The number being factorized. @@ -165,6 +172,13 @@ class FactorizationController : public QObject { */ void setUseSqrtOptimization(bool value); + /** + * @brief Enable or disable auto-pausing on new factor being found. + * + * This value can be changed even as a computation is ongoing. + */ + void setPauseOnFound(bool value); + // endregion // region: Functions @@ -208,6 +222,7 @@ class FactorizationController : public QObject { void isFinishedChanged(); void isPausedChanged(); void useSqrtOptimizationChanged(); + void pauseOnFoundChanged(); void iterationsPerCycleChanged(); void numberChanged(); void currentFactorChanged(); @@ -224,6 +239,7 @@ class FactorizationController : public QObject { bool m_isFinished; ///< Indicates whether the factorization process is done. bool m_isPaused; ///< Indicates whether the factorization process is paused (can be resumed). bool m_useSqrtOptimization; ///< Indicates whether to use the sqrt optimization + bool m_pauseOnFound; ///< Indicates whether the computation should be paused when a new factor is found. int m_iterationsPerCycle; ///< The number of iterations to perform per cycle. long long m_currentFactNumber; ///< The number currently being factorized. long long m_originalNumber; ///< The original input number.