Commit 758af18279ac045bd09cebb235e9b69cb65eefd7

Authored by Peter M. Groen
1 parent 13c49262

Building the GUI

src/calcprime.cpp
  1 +#include "calcprime.h"
  2 +
  3 +CalcPrime::CalcPrime(QObject *parent)
  4 + : QObject( parent )
  5 + , m_bCalculate( false )
  6 + , m_curValue( 0 )
  7 + , m_timer()
  8 + , oForm()
  9 +{
  10 + m_timer.setInterval( 1 );
  11 + m_timer.setSingleShot( true );
  12 +
  13 + connect( &m_timer, &QTimer::timeout, this, &CalcPrime::slotTimeOut );
  14 +
  15 + connect( &oForm, &CalcPrimeForm::signalStartButtonClicked, this, &CalcPrime::slotStartCalculation );
  16 + connect( &oForm, &CalcPrimeForm::signalStopButtonClicked, this, &CalcPrime::slotStopCalculation );
  17 +
  18 + connect( this, &CalcPrime::signalCurrentValue, &oForm, &CalcPrimeForm::slotUpdateCurrentValue );
  19 + connect( this, &CalcPrime::signalPrimeNumberFound, &oForm, &CalcPrimeForm::slotAddNewPrime );
  20 +
  21 + oForm.show();
  22 +}
  23 +
  24 +void CalcPrime::slotStartCalculation()
  25 +{
  26 + m_bCalculate = true;
  27 + slotTimeOut();
  28 +}
  29 +
  30 +void CalcPrime::slotStopCalculation()
  31 +{
  32 + m_bCalculate = false;
  33 +}
  34 +
  35 +void CalcPrime::slotTimeOut()
  36 +{
  37 + emit signalCurrentValue( m_curValue );
  38 + if( number_is_prime( m_curValue ) )
  39 + {
  40 + emit signalPrimeNumberFound( m_curValue );
  41 + }
  42 +
  43 + m_curValue++;
  44 +
  45 + if( m_bCalculate )
  46 + {
  47 + m_timer.start();
  48 + }
  49 +}
  50 +
  51 +bool CalcPrime::number_is_prime(uint64_t number)
  52 +{
  53 + uint64_t m = number / 2;
  54 +
  55 + for( uint64_t i = 2; i <= m; i++ )
  56 + {
  57 + if( number % i == 0 )
  58 + {
  59 + return false;
  60 + }
  61 + }
  62 +
  63 + return true;
  64 +}
... ...
src/calcprime.h
1 1 #pragma once
2 2  
3 3 #include "calcprime_form.h"
  4 +
4 5 #include <QObject>
  6 +#include <QTimer>
5 7  
6 8 class CalcPrime : public QObject
7 9 {
... ... @@ -10,10 +12,6 @@ class CalcPrime : public QObject
10 12 public:
11 13 explicit CalcPrime(QObject *parent = nullptr);
12 14  
13   -private:
14   - bool m_bCalculate;
15   - uint64_t m_curValue;
16   -
17 15 signals:
18 16 void signalCurrentValue( uint64_t current_value );
19 17 void signalPrimeNumberFound( uint64_t prime_number );
... ... @@ -21,4 +19,16 @@ signals:
21 19 private slots:
22 20 void slotStartCalculation();
23 21 void slotStopCalculation();
  22 + void slotTimeOut();
  23 +
  24 +private:
  25 + bool number_is_prime(uint64_t number);
  26 +
  27 +private:
  28 + bool m_bCalculate;
  29 + uint64_t m_curValue;
  30 + QTimer m_timer;
  31 +
  32 + CalcPrimeForm oForm;
  33 +
24 34 };
... ...
src/calcprime_form.cpp
... ... @@ -23,7 +23,8 @@ void CalcPrimeForm::slotUpdateCurrentValue( uint64_t value )
23 23  
24 24 void CalcPrimeForm::slotAddNewPrime( uint64_t new_prime )
25 25 {
26   - this->listPrimes->addItem(QString( std::to_string( new_prime ).c_str() ));
  26 + this->listPrimes->insertPlainText(QString( std::to_string( new_prime ).c_str() ) + "\t");
  27 + this->listPrimes->centerCursor();
27 28 }
28 29  
29 30 void CalcPrimeForm::slotStartButtonClicked()
... ...
src/calcprime_form.ui
... ... @@ -18,13 +18,33 @@
18 18 <layout class="QVBoxLayout" name="verticalLayout">
19 19 <item>
20 20 <widget class="QLCDNumber" name="currentValue">
  21 + <property name="font">
  22 + <font>
  23 + <pointsize>13</pointsize>
  24 + <bold>true</bold>
  25 + </font>
  26 + </property>
  27 + <property name="styleSheet">
  28 + <string notr="true">color: rgb(170, 255, 0);
  29 +background-color: rgb(0, 0, 127);</string>
  30 + </property>
21 31 <property name="digitCount">
22 32 <number>30</number>
23 33 </property>
24 34 </widget>
25 35 </item>
26 36 <item>
27   - <widget class="QListWidget" name="listPrimes"/>
  37 + <widget class="QPlainTextEdit" name="listPrimes">
  38 + <property name="undoRedoEnabled">
  39 + <bool>false</bool>
  40 + </property>
  41 + <property name="textInteractionFlags">
  42 + <set>Qt::NoTextInteraction</set>
  43 + </property>
  44 + <property name="centerOnScroll">
  45 + <bool>false</bool>
  46 + </property>
  47 + </widget>
28 48 </item>
29 49 <item>
30 50 <layout class="QHBoxLayout" name="horizontalLayout">
... ... @@ -36,6 +56,26 @@
36 56 </widget>
37 57 </item>
38 58 <item>
  59 + <spacer name="horizontalSpacer_2">
  60 + <property name="orientation">
  61 + <enum>Qt::Horizontal</enum>
  62 + </property>
  63 + <property name="sizeHint" stdset="0">
  64 + <size>
  65 + <width>40</width>
  66 + <height>20</height>
  67 + </size>
  68 + </property>
  69 + </spacer>
  70 + </item>
  71 + <item>
  72 + <widget class="QPushButton" name="cmdResetButton">
  73 + <property name="text">
  74 + <string>Reset</string>
  75 + </property>
  76 + </widget>
  77 + </item>
  78 + <item>
39 79 <spacer name="horizontalSpacer">
40 80 <property name="orientation">
41 81 <enum>Qt::Horizontal</enum>
... ...
src/main.cpp
1 1 #include <QApplication>
2 2  
3   -#include "calcprime_form.h"
  3 +#include "calcprime.h"
4 4  
5 5 int main( int argc, char* argv[] )
6 6 {
7 7 QApplication oApp( argc, argv );
8 8  
9   - CalcPrimeForm oForm;
10   - oForm.show();
  9 + CalcPrime ocalcPrime;
11 10  
12 11 oApp.exec();
13 12 }
... ...