C++ 入門指南 4.01

encryptcontroller.cpp

#include "encryptcontroller.h"

#include <QDebug>

#include <QFile>
#include <QDataStream>
#include <QClipboard>
#include <QGuiApplication>

EncryptController::EncryptController(QObject *parent) : QObject(parent)
{
}

QString EncryptController::userInput()
{
    return m_userInput;
}

void EncryptController::setUserInput(const QString &userInput)
{
    if (userInput == m_userInput)
        return;

    m_userInput = userInput;
    emit userInputChanged();
}

QString EncryptController::s2q(const string &s)
{
    return QString(QString::fromLocal8Bit(s.c_str()));
}

string EncryptController::q2s(const QString &s)
{
    return string((const char *)s.toLocal8Bit());
}

void EncryptController::setEncryptObject()
{
    encryptObject = new Encrypt();
}

QString EncryptController::getEncryptObject()
{
    return s2q(encryptObject->get_code_array());
}

void EncryptController::encodeFunction()
{
    m_encodeResult = s2q(encryptObject->ToEncode(q2s(m_userInput)));

    qInfo() << m_userInput;
    qInfo() << m_encodeResult;
}

QString EncryptController::getEncodeResult()
{
    return m_encodeResult;
}

void EncryptController::decodeFunction()
{
    m_decodeResult = s2q(encryptObject->ToDecode(q2s(m_userInput)));

    qInfo() << m_userInput;
    qInfo() << m_decodeResult;
}

QString EncryptController::getDecodeResult()
{
    return m_decodeResult;
}

void EncryptController::save()
{
    QFile file("encryptor");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    out << s2q(encryptObject->get_code_array());
}

bool EncryptController::load()
{
    QFile file("encryptor");
    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);
        QString temp;
        in >> temp;

        if (encryptObject == nullptr) {
            encryptObject = new Encrypt;
        }
        encryptObject->set_code_array(q2s(temp));

        return true;
    }
    else {
        return false;
    }
}

void EncryptController::clear()
{
    m_userInput = "";
    m_encodeResult = "";
    m_decodeResult = "";
    encryptObject = nullptr;
}

void EncryptController::copy()
{
    QClipboard *clipboard = QGuiApplication::clipboard();
    clipboard->setText(m_encodeResult);
}

/* 《程式語言教學誌》的範例程式
   http://kaiching.org/
   檔名:encryptcontroller.cpp
   功能:EncryptController 類別的標頭檔
   作者:張凱慶 */

相關教學影片

上一頁 範例程式篇 encryptcontroller.h
回 C++ 入門指南 4.01 目錄
下一頁 術語解釋
回 C++ 教材
回程式語言教材首頁