@nicker-player
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QState>
#include <QStateMachine>
#include <QFinalState>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
//this signals may be emit by buttons click, keys events,wheel events, slider move.
void sigPageGoStart();
void sigPageGoMid();
void sigPageGoEnd();
void sigPageClose();//this signal emit when close current page,machine will finsh
private slots:
//Here I only add the button trigger emit state Transition procedure
void on_pushButton1_First_clicked();
void on_pushButton_Prev_clicked();
void on_pushButton_Next_clicked();
void on_pushButton_Last_clicked();
void slotPageFinishedState();
private:
Ui::MainWindow *ui;
QStateMachine* pageStateMachine=new QStateMachine(this);
QState* pageStartState=nullptr;
QState* pageMiddleState=nullptr;
QState* pageEndState=nullptr;
QFinalState* pageNoneState=nullptr;
void OpenPage();
void ClosePage();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//1-create state
pageStartState=new QState(pageStateMachine);
pageStartState->assignProperty(ui->pushButton1_First,"enabled",false);
pageStartState->assignProperty(ui->pushButton_Prev,"enabled",false);
pageStartState->assignProperty(ui->pushButton_Next,"enabled",true);
pageStartState->assignProperty(ui->pushButton_Last,"enabled",true);
pageMiddleState=new QState(pageStateMachine);
pageMiddleState->assignProperty(ui->pushButton1_First,"enabled",true);
pageMiddleState->assignProperty(ui->pushButton_Prev,"enabled",true);
pageMiddleState->assignProperty(ui->pushButton_Next,"enabled",true);
pageMiddleState->assignProperty(ui->pushButton_Last,"enabled",true);
pageEndState=new QState(pageStateMachine);
pageEndState->assignProperty(ui->pushButton1_First,"enabled",true);
pageEndState->assignProperty(ui->pushButton_Prev,"enabled",true);
pageEndState->assignProperty(ui->pushButton_Next,"enabled",false);
pageEndState->assignProperty(ui->pushButton_Last,"enabled",false);
pageNoneState=new QFinalState(pageStateMachine);
connect(pageStateMachine,&QStateMachine::stopped,this,&MainWindow::slotPageFinishedState);
//2-add state transition
pageStartState->addTransition(this,&MainWindow::sigPageGoMid,pageMiddleState);
pageStartState->addTransition(this,&MainWindow::sigPageGoEnd,pageEndState);
pageStartState->addTransition(this,&MainWindow::sigPageClose,pageNoneState);
pageMiddleState->addTransition(this,&MainWindow::sigPageGoStart,pageStartState);
pageMiddleState->addTransition(this,&MainWindow::sigPageGoEnd,pageEndState);
pageMiddleState->addTransition(this,&MainWindow::sigPageClose,pageNoneState);
pageEndState->addTransition(this,&MainWindow::sigPageGoMid,pageMiddleState);
pageEndState->addTransition(this,&MainWindow::sigPageGoStart,pageStartState);
pageEndState->addTransition(this,&MainWindow::sigPageClose,pageNoneState);
//3- open Page
OpenPage();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton1_First_clicked()
{
//page move to start page view
emit sigPageGoStart();
}
void MainWindow::on_pushButton_Prev_clicked()
{
//page move up
//Check page view position, send signal sigPageGoStart\sigPageGoMid\sigPageGoEnd
}
void MainWindow::on_pushButton_Next_clicked()
{
//page move down
//Check page view position, send signal sigPageGoStart\sigPageGoMid\sigPageGoEnd
}
void MainWindow::on_pushButton_Last_clicked()
{
//page move to start page view
emit sigPageGoEnd();
}
void MainWindow::slotPageFinishedState()
{
ui->pushButton1_First->setEnabled(false);
ui->pushButton_Prev->setEnabled(false);
ui->pushButton_Next->setEnabled(false);
ui->pushButton_Last->setEnabled(false);
}
void MainWindow::OpenPage()
{
//1-open page move to start page view
//2-set ui machine
pageStateMachine->setInitialState(pageStartState);
pageStateMachine->start();
}
void MainWindow::ClosePage()
{
//1-close page
//2-stop ui machine
emit sigPageClose();
}