Make stopFactor accessible to QML
This commit is contained in:
		
							parent
							
								
									028f492c32
								
							
						
					
					
						commit
						c58697b126
					
				
					 3 changed files with 30 additions and 8 deletions
				
			
		| 
						 | 
					@ -119,6 +119,11 @@ ApplicationWindow {
 | 
				
			||||||
                    font.pixelSize: 14
 | 
					                    font.pixelSize: 14
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Text {
 | 
				
			||||||
 | 
					                    text: "Stop Factor: " + factorizationController.stopFactor
 | 
				
			||||||
 | 
					                    font.pixelSize: 14
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                ProgressBar {
 | 
					                ProgressBar {
 | 
				
			||||||
                    Layout.fillWidth: true
 | 
					                    Layout.fillWidth: true
 | 
				
			||||||
                    from: 0
 | 
					                    from: 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,7 @@
 | 
				
			||||||
#include <cmath>
 | 
					#include <cmath>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
FactorizationController::FactorizationController(QObject* parent) :
 | 
					FactorizationController::FactorizationController(QObject* parent) :
 | 
				
			||||||
    QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_currentFactor(2) {
 | 
					    QObject{parent}, m_isRunning(false), m_useSqrtOptimization(true), m_iterationsPerCycle(1), m_currentFactNumber(0), m_currentFactor(0), m_stopFactor(0) {
 | 
				
			||||||
    assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick));
 | 
					    assert(connect(&m_timer, &QTimer::timeout, this, &FactorizationController::onTimerTick));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,6 +70,10 @@ long long FactorizationController::currentFactor() const {
 | 
				
			||||||
    return m_currentFactor;
 | 
					    return m_currentFactor;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					long long FactorizationController::stopFactor() const {
 | 
				
			||||||
 | 
					    return m_stopFactor;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QList<long long> FactorizationController::factors() const {
 | 
					QList<long long> FactorizationController::factors() const {
 | 
				
			||||||
    return m_factors;
 | 
					    return m_factors;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -81,6 +85,12 @@ void FactorizationController::start(long long number) {
 | 
				
			||||||
    m_isRunning = true;
 | 
					    m_isRunning = true;
 | 
				
			||||||
    m_isFinished = false;
 | 
					    m_isFinished = false;
 | 
				
			||||||
    m_factors.clear();
 | 
					    m_factors.clear();
 | 
				
			||||||
 | 
					    if (m_useSqrtOptimization) {
 | 
				
			||||||
 | 
					        // we could also just compute this every time, but sqrt is pretty expensive
 | 
				
			||||||
 | 
					        m_stopFactor = static_cast<long long>(std::sqrt(m_currentFactNumber));
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        m_stopFactor = m_currentFactNumber - 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    emit isRunningChanged();
 | 
					    emit isRunningChanged();
 | 
				
			||||||
    emit isFinishedChanged();
 | 
					    emit isFinishedChanged();
 | 
				
			||||||
    emit numberChanged();
 | 
					    emit numberChanged();
 | 
				
			||||||
| 
						 | 
					@ -88,13 +98,7 @@ void FactorizationController::start(long long number) {
 | 
				
			||||||
    emit progressChanged();
 | 
					    emit progressChanged();
 | 
				
			||||||
    emit factorsChanged();
 | 
					    emit factorsChanged();
 | 
				
			||||||
    emit curFactNumberChanged();
 | 
					    emit curFactNumberChanged();
 | 
				
			||||||
 | 
					    emit stopFactorChanged();
 | 
				
			||||||
    if (m_useSqrtOptimization) {
 | 
					 | 
				
			||||||
        // we could also just compute this every time, but sqrt is pretty expensive
 | 
					 | 
				
			||||||
        m_stopFactor = static_cast<long long>(std::sqrt(m_currentFactNumber));
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        m_stopFactor = m_currentFactNumber - 1;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    m_timer.start(0);
 | 
					    m_timer.start(0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -193,6 +197,7 @@ void FactorizationController::factorize() {
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            m_stopFactor = m_currentFactNumber - 1;
 | 
					            m_stopFactor = m_currentFactNumber - 1;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        emit stopFactorChanged();
 | 
				
			||||||
        emit progressChanged();
 | 
					        emit progressChanged();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Don't increase current factor, keep dividing by it until no longer divisible
 | 
					        // Don't increase current factor, keep dividing by it until no longer divisible
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,6 +29,7 @@ class FactorizationController : public QObject {
 | 
				
			||||||
    Q_PROPERTY(long long number READ number NOTIFY numberChanged)
 | 
					    Q_PROPERTY(long long number READ number NOTIFY numberChanged)
 | 
				
			||||||
    Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged)
 | 
					    Q_PROPERTY(long long curFactNumber READ curFactNumber NOTIFY curFactNumberChanged)
 | 
				
			||||||
    Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged)
 | 
					    Q_PROPERTY(long long currentFactor READ currentFactor NOTIFY currentFactorChanged)
 | 
				
			||||||
 | 
					    Q_PROPERTY(long long stopFactor READ stopFactor NOTIFY stopFactorChanged)
 | 
				
			||||||
    Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
 | 
					    Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
 | 
				
			||||||
    Q_PROPERTY(QList<long long> factors READ factors NOTIFY factorsChanged)
 | 
					    Q_PROPERTY(QList<long long> factors READ factors NOTIFY factorsChanged)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -112,6 +113,16 @@ class FactorizationController : public QObject {
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    long long currentFactor() const;
 | 
					    long long currentFactor() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @brief Get the number at which the factorization process will stop
 | 
				
			||||||
 | 
					     * @return The current stop factor.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * The stop factor determines at which factor will the factorization process be stopped.
 | 
				
			||||||
 | 
					     * If sqrt optimizations are enabled, this will generally be the square root of the number
 | 
				
			||||||
 | 
					     * currently being factorized. If not, it will be the number being factorized - 1.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    long long stopFactor() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @brief Gets the list of discovered factors (may not be complete).
 | 
					     * @brief Gets the list of discovered factors (may not be complete).
 | 
				
			||||||
     * @return A QList of extracted prime factors.
 | 
					     * @return A QList of extracted prime factors.
 | 
				
			||||||
| 
						 | 
					@ -203,6 +214,7 @@ class FactorizationController : public QObject {
 | 
				
			||||||
    void factorsChanged();
 | 
					    void factorsChanged();
 | 
				
			||||||
    void progressChanged();
 | 
					    void progressChanged();
 | 
				
			||||||
    void curFactNumberChanged();
 | 
					    void curFactNumberChanged();
 | 
				
			||||||
 | 
					    void stopFactorChanged();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private slots:
 | 
					  private slots:
 | 
				
			||||||
    void onTimerTick();
 | 
					    void onTimerTick();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue