Logs
Consultez les logs.
OK
Liste des données
Consultez la liste des données.
OK
Loading...
Formulaire
Saisissez vos données.
Enregistrer
Annuler

Références QML

Vues
124

Vous souhaitez réaliser des applications graphiques hautes performances en C++ avec la bibliothèque graphique Qt fournissant le langage QML capable d'exploiter la puissance des processeurs graphiques GPU afin de bénéficier de l'accélération matérielle pour créer des interfaces graphiques fluides multiplateformes orientées bureau (Windows, Linux, Mac OS) ou mobile (Android, iOS).

image.png



Gestion de la calculatrice Standard en C++ (Standard.cpp)


On inclut les fichiers d'entête du projet.

...
#include "Standard.h"
#include "History.h"
#include "HistoryElement.h"
#include "Utils.h"
...

On inclut les fichiers d'entête Qt. 

...
#include <QJSEngine>
...

On définit le constructeur du projet. 

...
Standard::Standard(QObject* parent)
    : QObject{ parent }
    , m_jsEngine(std::make_unique<QJSEngine>())
{
    m_UnsavedValue  = '0';
    m_LastOperation = Value;
}
...

On traite les boutons de la calculatrice. 

...
void Standard::processButton(QString const& type, QString const& func, QString const& placeHolder)
{
    QString capitalized = type;
    capitalized[0]      = type[0].toUpper();
    WaitOperation t     = ConvertStringToEnum<WaitOperation>(capitalized);

    switch (t)
    {
    case Standard::Cmd:      executeCommand(func); break;
    case Standard::Function: addFunction(func, placeHolder); break;
    case Standard::Operator: addOperator(*func.begin()); break;
    case Standard::Value:    addEnteredValue(*func.begin()); break;
    }
}
...

On initialise l'historique de la calculatrice. 

...
void Standard::setHistory(History* history)
{
    connect(history, &History::currentItemChanged, this, &Standard::onHistoryItemClicked);
    m_History = history;
}
...

On notifie la mise à jour de la calculatrice. 

...
void Standard::update()
{
    emit lastOperationChanged();
    emit enteredValueChanged();
}
...

On traite l'opération d'égalité.

...
void Standard::getEqual()
{
    if (m_BackEndExpression.isEmpty())
        return;

    if (m_LastOperation == Function)
    {
        m_UnsavedValue = m_FinalValue;
        m_BackEndExpression.clear();
    }

    if (m_LastOperation == Cmd)
    {
        if (m_LastCommand == equal)
        {
            m_UnsavedValue = m_FinalValue;
            m_LastJoined   = m_FinalValue;
            m_BackEndExpression.clear();
        }
    }

    if (!m_BackEndExpression.cend()->isNumber())
        m_BackEndExpression += m_UnsavedValue;

    m_FinalValue = calculateProduct();
    emit finalValueChanged();

    m_LastJoined = m_BackEndExpression;

    OperationElement* el  = new OperationElement();
    el->FinalValue        = m_FinalValue;
    el->UnsavedValue      = m_UnsavedValue;
    el->LastJoined        = m_LastJoined;
    el->BackEndExpression = m_BackEndExpression;
    el->LastOperation     = m_LastOperation;

    m_History->pushElement(el);

    m_UnsavedValue.clear();
    emit lastOperationChanged();
}
...

On exécute une commande.

...
void Standard::executeCommand(QString const& cmd)
{
    Command const c = ConvertStringToEnum<Command>(cmd);

    switch (c)
    {
    case Standard::del:    deleteEnteredValue(); break;
    case Standard::clr:    clearEntered(); break;
    case Standard::clrall: clearCalculations(); break;
    case Standard::equal:  getEqual(); break;
    case Standard::cnvrt:
        if (m_UnsavedValue[0] == '0')
            return;

        if (m_UnsavedValue.isEmpty())
        {
            if (m_FinalValue.indexOf('-') != -1)
                m_FinalValue.remove(0, 1);
            else
                m_FinalValue.push_front('-');

            m_UnsavedValue = m_FinalValue;

            emit finalValueChanged();
        }
        else
        {
            if (m_UnsavedValue.indexOf('-') != -1)
                m_UnsavedValue.remove(0, 1);
            else
                m_UnsavedValue.push_front('-');

            m_FinalValue = m_UnsavedValue;

            emit enteredValueChanged();
        }

        break;
    }

    m_LastCommand   = c;
    m_LastOperation = Cmd;
}
...

On initialise une valeur saisie.

...
void Standard::setEnteredValue(QString const& value)
{
    m_UnsavedValue  = value;
    m_LastOperation = Value;

    emit enteredValueChanged();
}
...

On ajoute une valeur saisie.

...
void Standard::addEnteredValue(QChar const& value)
{
    if (!m_UnsavedValue.isEmpty())
    {
        if (m_UnsavedValue.left(2) != "0." && value.isSymbol())
        {
            return;
        }
        if (m_UnsavedValue[0] == '0' && m_UnsavedValue.size() == 1 && value.isNumber())
            m_UnsavedValue.clear();
    }
    else
    {
        if (value == '.')
            return;
    }

    if (m_LastOperation == Cmd)
        if (m_LastCommand == equal)
            m_UnsavedValue = value;

    if (m_LastOperation == Operator)
    {
        m_UnsavedValue = value;
    }
    else
    {
        m_UnsavedValue += value;
    }

    m_LastOperation = Value;
    emit enteredValueChanged();
} "
...

On ajoute un opérateur.

...
void Standard::addOperator(QChar const& op)
{
    if (m_LastOperation == Cmd)
    {
        if (m_LastCommand == equal)
        {
            m_UnsavedValue = m_FinalValue;
            m_BackEndExpression.clear();
        }
    }

    if (m_LastOperation == Operator)
    {
        m_BackEndExpression.replace(m_BackEndExpression.last(1), op);
    }
    else if (m_LastOperation == Function)
    {
        m_UnsavedValue      = m_FinalValue;
        m_BackEndExpression = m_FinalValue + op;
    }
    else
    {
        m_BackEndExpression += m_UnsavedValue;
        m_BackEndExpression += op;
    }

    m_LastJoined = m_BackEndExpression;

    emit lastOperationChanged();

    m_LastOperation = Operator;
}
...

On ajoute une fonction.

...
void Standard::addFunction(QString const& func, QString const& placeHolder)
{
    if (m_LastOperation == Function)
    {
        m_BackEndExpression = func.arg(m_FinalValue);
        m_LastJoined        = concatPHFunctionWithValue(placeHolder, m_FinalValue.toDouble());
        m_FinalValue        = calculateProduct();
        m_UnsavedValue      = m_FinalValue;

        m_LastOperation = Function;

        OperationElement* el  = new OperationElement();
        el->FinalValue        = m_FinalValue;
        el->UnsavedValue      = m_UnsavedValue;
        el->LastJoined        = m_LastJoined;
        el->BackEndExpression = m_BackEndExpression;
        el->LastOperation     = m_LastOperation;

        m_History->pushElement(el);

        emit lastOperationChanged();
        emit finalValueChanged();

        return;
    }
    else
    {
        if (m_LastOperation != Value && m_LastOperation != Cmd)
        {
            if (m_LastCommand != equal)
                return;
            return;
        }
    }

    if (m_LastOperation == Cmd)
    {
        if (m_LastCommand == equal)
        {
            m_UnsavedValue = m_FinalValue;
        }
    }

    m_BackEndExpression = func.arg(m_UnsavedValue);
    m_FinalValue        = calculateProduct();

    m_LastJoined    = concatPHFunctionWithValue(placeHolder, m_UnsavedValue.toDouble());
    m_LastOperation = Function;

    OperationElement* el  = new OperationElement();
    el->FinalValue        = m_FinalValue;
    el->UnsavedValue      = m_UnsavedValue;
    el->LastJoined        = m_LastJoined;
    el->BackEndExpression = m_BackEndExpression;
    el->LastOperation     = m_LastOperation;

    m_History->pushElement(el);

    emit lastOperationChanged();
    emit finalValueChanged();
}
...

On supprime une valeur saisie.

...
void Standard::deleteEnteredValue()
{
    if (m_UnsavedValue.isEmpty())
    {
        m_UnsavedValue = '0';
        return;
    }

    if (m_UnsavedValue.size() == 2 && m_UnsavedValue[0] == '-')
        m_UnsavedValue = '0';
    else
        m_UnsavedValue.remove(m_UnsavedValue.size() - 1, 1);

    emit enteredValueChanged();
}
...

On supprime une saisie.

...
void Standard::clearEntered()
{
    m_UnsavedValue = '0';
    emit enteredValueChanged();
}
...

On supprime les calculs.

...
void Standard::clearCalculations()
{
    clearEntered();
    m_FinalValue.clear();
    m_BackEndExpression.clear();

    m_LastJoined.clear();
    m_LastOperation = Value;

    emit lastOperationChanged();
    emit enteredValueChanged();
}
...

On réagit à l'appui sur un élément de l'historique.

...
void Standard::onHistoryItemClicked(HistoryElement* item)
{
    OperationElement* el = static_cast<OperationElement*>(item);

    m_FinalValue        = el->FinalValue;
    m_LastJoined        = el->LastJoined;
    m_LastOperation     = el->LastOperation;
    m_BackEndExpression = el->BackEndExpression;

    emit lastOperationChanged();
    emit finalValueChanged();
}
...

On concatène le texte de remplissage avec la valeur.

...
QString Standard::concatPHFunctionWithValue(QString const& placeHolder, double value) const
{
    QString temp = placeHolder;

    if (placeHolder.indexOf('x') != -1)
        temp.replace('x', QString::number(value), Qt::CaseSensitive);
    else
    {
        if (temp.size() == 1)
            temp = QString::number(value) + placeHolder;
        else
            temp = placeHolder + QString::number(value);
    }

    return temp;
}
...

On calcule le produit.

...
QString Standard::calculateProduct()
{
    QJSValue val = m_jsEngine->evaluate(m_BackEndExpression);
    return val.toString();
}
...

On récupère le résultat final de l'opération.

...
QString const& Standard::getFinalValue() const
{
    return m_FinalValue;
}
...

On récupère la dernière opération.

...
QString const& Standard::getLastOperation() const
{
    return m_LastJoined;
}
...

On récupère le texte de remplissage.

...
QString OperationElement::placeHolderText() const
{
    return LastJoined + '=' + FinalValue;
}
...