From 7f1526d3135c24d8c7f9f216796fe6cd10e4d752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl=20Restori?= Date: Wed, 20 Aug 2014 20:10:25 +0200 Subject: [PATCH] New UI --- QBrainfuck.pro | 2 + qbrainfuck.cpp | 337 +++++++++++++++++++++++++---- qbrainfuck.h | 35 ++- qbrainfuck.ui | 576 ++++++++++++++++++++++++++++--------------------- 4 files changed, 661 insertions(+), 289 deletions(-) diff --git a/QBrainfuck.pro b/QBrainfuck.pro index c706a38..75250c4 100644 --- a/QBrainfuck.pro +++ b/QBrainfuck.pro @@ -20,3 +20,5 @@ HEADERS += qbrainfuck.h \ decoderthread.h FORMS += qbrainfuck.ui + +LIBS += -lqrencode diff --git a/qbrainfuck.cpp b/qbrainfuck.cpp index 59e8fce..63db6d6 100644 --- a/qbrainfuck.cpp +++ b/qbrainfuck.cpp @@ -17,6 +17,8 @@ QBrainfuck::~QBrainfuck() delete ui; } +/********** Text part **********/ + void QBrainfuck::on_encodeButton_clicked() { QByteArray text = ui->textTextEdit->document()->toPlainText().toLatin1(); @@ -111,8 +113,58 @@ void QBrainfuck::on_encodeButton_clicked() } ui->programTextEdit->setPlainText(program); + + ui->tabWidget->setCurrentIndex(1); } +void QBrainfuck::on_textLoadButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + + if (dialog.exec()) { + QFile loadFile(dialog.selectedFiles().first()); + + if (!loadFile.open(QIODevice::ReadOnly)) { + return; + } + + ui->textTextEdit->document()->setPlainText(loadFile.readAll()); + + loadFile.close(); + } +} + +void QBrainfuck::on_textSaveButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + QFile saveFile(dialog.selectedFiles().first()); + + if (!saveFile.open(QIODevice::WriteOnly)) { + return; + } + + saveFile.write(ui->textTextEdit->document()->toPlainText().toUtf8()); + saveFile.close(); + } +} + +void QBrainfuck::on_textTextEdit_textChanged() +{ + ui->textLabel->setText("Text length: " + QString().setNum(ui->textTextEdit->document()->toPlainText().length())); +} + +/********** Program part **********/ + void QBrainfuck::on_decodeButton_clicked() { ui->textTextEdit->setPlainText(""); @@ -121,8 +173,58 @@ void QBrainfuck::on_decodeButton_clicked() QByteArray input = ui->inputLineEdit->text().toLatin1(); decoder->decode(program, input); + + ui->tabWidget->setCurrentIndex(0); } +void QBrainfuck::on_programLoadButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Bf Files (*.bf)"); + dialog.setDefaultSuffix("bf"); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + + if (dialog.exec()) { + QFile loadFile(dialog.selectedFiles().first()); + + if (!loadFile.open(QIODevice::ReadOnly)) { + return; + } + + ui->programTextEdit->document()->setPlainText(loadFile.readAll()); + + loadFile.close(); + } +} + +void QBrainfuck::on_programSaveButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Bf Files (*.bf)"); + dialog.setDefaultSuffix("bf"); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + QFile saveFile(dialog.selectedFiles().first()); + + if (!saveFile.open(QIODevice::WriteOnly)) { + return; + } + + saveFile.write(ui->programTextEdit->document()->toPlainText().toUtf8()); + saveFile.close(); + } +} + +void QBrainfuck::on_programTextEdit_textChanged() +{ + ui->programLabel->setText("Program length: " + QString().setNum(ui->programTextEdit->document()->toPlainText().count(QRegExp(">|<|\\+|-|\\.|,|\\[|\\]")))); +} + +/********** Motif part **********/ + void QBrainfuck::on_motifButton_clicked() { QString motif = ui->motifTextEdit->document()->toPlainText(); @@ -155,16 +257,48 @@ void QBrainfuck::on_motifButton_clicked() } ui->resultTextEdit->setPlainText(motif); + ui->tabWidget->setCurrentIndex(3); } -void QBrainfuck::on_textTextEdit_textChanged() +void QBrainfuck::on_motifLoadButton_clicked() { - ui->textLabel->setText("Text length: " + QString().setNum(ui->textTextEdit->document()->toPlainText().length())); + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + + if (dialog.exec()) { + QFile loadFile(dialog.selectedFiles().first()); + + if (!loadFile.open(QIODevice::ReadOnly)) { + return; + } + + ui->motifTextEdit->document()->setPlainText(loadFile.readAll()); + + loadFile.close(); + } } -void QBrainfuck::on_programTextEdit_textChanged() +void QBrainfuck::on_motifSaveButton_clicked() { - ui->programLabel->setText("Program length: " + QString().setNum(ui->programTextEdit->document()->toPlainText().count(QRegExp(">|<|\\+|-|\\.|,|\\[|\\]")))); + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + QFile saveFile(dialog.selectedFiles().first()); + + if (!saveFile.open(QIODevice::WriteOnly)) { + return; + } + + saveFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8()); + saveFile.close(); + } } void QBrainfuck::on_motifTextEdit_textChanged() @@ -177,47 +311,48 @@ void QBrainfuck::on_regexLineEdit_textChanged(const QString &arg1) on_motifTextEdit_textChanged(); } -void QBrainfuck::on_saveButton_clicked() +/********** Result part **********/ + +void QBrainfuck::on_qrcodeButton_clicked() { - QFileDialog dialog(this); - dialog.setFileMode(QFileDialog::AnyFile); - dialog.setNameFilter("Json Files (*.json);;Txt Files (*.txt)"); - dialog.setDefaultSuffix("json"); - dialog.setAcceptMode(QFileDialog::AcceptSave); + char* text = new char[ui->resultTextEdit->document()->toPlainText().toLatin1().size() + 1]; + strcpy(text, ui->resultTextEdit->document()->toPlainText().toLatin1().data()); - if (dialog.exec()) { - QFile saveFile(dialog.selectedFiles().first()); + QRcode *qrCode=QRcode_encodeString(text, 0, QR_ECLEVEL_L, QR_MODE_8, 1); + if(qrCode) { + int width = qrCode->width; - if (!saveFile.open(QIODevice::WriteOnly)) { - return; + QImage image((width + 4)*8, (width + 4)*8, QImage::Format_RGB32);; + QPainter painter(&image); + + QLinearGradient linearGrad(QPointF(2*8, 2*8), QPointF((width + 2)*8, (width + 2)*8)); + linearGrad.setColorAt(0, Qt::black); + linearGrad.setColorAt(1, Qt::black); + + painter.fillRect(0, 0, (width + 4)*8, (width + 4)*8, Qt::white); + painter.fillRect(2*8, 2*8, width*8, width*8, linearGrad); + + for (int i = 0; i < width*width; ++i) { + if (!(qrCode->data[i] &0x01)) { + int col = i % width; + int row = (i - col) / width; + + painter.fillRect((col + 2)*8, (row + 2)*8, 8, 8, Qt::white); + } } - if (dialog.selectedNameFilter() == "Json Files (*.json)") { - QJsonObject jsonObject; - - jsonObject["text"] = ui->textTextEdit->document()->toPlainText(); - jsonObject["program"] = ui->programTextEdit->document()->toPlainText(); - jsonObject["motif"] = ui->motifTextEdit->document()->toPlainText(); - jsonObject["result"] = ui->resultTextEdit->document()->toPlainText(); - - QJsonDocument jsonDoc(jsonObject); - saveFile.write(jsonDoc.toJson()); - } else { - saveFile.write(ui->textTextEdit->document()->toPlainText().toUtf8() + "\n\n"); - saveFile.write(ui->programTextEdit->document()->toPlainText().toUtf8() + "\n\n"); - saveFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8() + "\n\n"); - saveFile.write(ui->resultTextEdit->document()->toPlainText().toUtf8() + "\n\n"); - } + ui->qrcodeLabel->setPixmap(QPixmap::fromImage(image)); + ui->tabWidget->setCurrentIndex(4); } } -void QBrainfuck::on_loadButton_clicked() +void QBrainfuck::on_resultLoadButton_clicked() { QFileDialog dialog(this); dialog.setFileMode(QFileDialog::AnyFile); - dialog.setNameFilter("Json Files (*.json)"); - dialog.setDefaultSuffix("json"); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); dialog.setAcceptMode(QFileDialog::AcceptOpen); if (dialog.exec()) { @@ -227,18 +362,54 @@ void QBrainfuck::on_loadButton_clicked() return; } - QByteArray saveData = loadFile.readAll(); + ui->resultTextEdit->document()->setPlainText(loadFile.readAll()); - QJsonDocument jsonDoc(QJsonDocument::fromJson(saveData)); - QJsonObject jsonObject = jsonDoc.object(); - - ui->textTextEdit->setPlainText(jsonObject["text"].toString()); - ui->programTextEdit->setPlainText(jsonObject["program"].toString()); - ui->motifTextEdit->setPlainText(jsonObject["motif"].toString()); - ui->resultTextEdit->setPlainText(jsonObject["result"].toString()); + loadFile.close(); } } +void QBrainfuck::on_resultSaveButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("Txt Files (*.txt)"); + dialog.setDefaultSuffix("txt"); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + QFile saveFile(dialog.selectedFiles().first()); + + if (!saveFile.open(QIODevice::WriteOnly)) { + return; + } + + saveFile.write(ui->resultTextEdit->document()->toPlainText().toUtf8()); + saveFile.close(); + } +} + +void QBrainfuck::on_resultTextEdit_textChanged() +{ + ui->resultLabel->setText("Result length: " + QString().setNum(ui->resultTextEdit->document()->toPlainText().length())); +} + +/********** QRCode part **********/ + +void QBrainfuck::on_qrcodeSaveButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setNameFilter("PNG Files (*.png)"); + dialog.setDefaultSuffix("png"); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + ui->qrcodeLabel->pixmap()->toImage().save(dialog.selectedFiles().first(), "PNG"); + } +} + +/*******************************/ + void QBrainfuck::setText(QString text) { ui->textTextEdit->setPlainText(text); } @@ -246,3 +417,87 @@ void QBrainfuck::setText(QString text) { void QBrainfuck::errorMessage(QString message) { QMessageBox::critical(this, "QBrainfuck error", "Error: " + message); } + +void QBrainfuck::on_saveAllButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::Directory); + dialog.setAcceptMode(QFileDialog::AcceptSave); + + if (dialog.exec()) { + QString saveDir(dialog.selectedFiles().first()); + + QFile textFile(saveDir + "/text.txt"); + if (!textFile.open(QIODevice::WriteOnly)) { + return; + } + textFile.write(ui->textTextEdit->document()->toPlainText().toUtf8()); + textFile.close(); + + QFile programFile(saveDir + "/program.bf"); + if (!programFile.open(QIODevice::WriteOnly)) { + return; + } + programFile.write(ui->programTextEdit->document()->toPlainText().toUtf8()); + programFile.close(); + + QFile motifFile(saveDir + "/motif.txt"); + if (!motifFile.open(QIODevice::WriteOnly)) { + return; + } + motifFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8()); + motifFile.close(); + + QFile resultFile(saveDir + "/result.txt"); + if (!resultFile.open(QIODevice::WriteOnly)) { + return; + } + resultFile.write(ui->resultTextEdit->document()->toPlainText().toUtf8()); + resultFile.close(); + + ui->qrcodeLabel->pixmap()->toImage().save(saveDir + "/qrcode.png", "PNG"); + } +} + +void QBrainfuck::on_loadAllButton_clicked() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::Directory); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + + if (dialog.exec()) { + QString saveDir(dialog.selectedFiles().first()); + + QFile textFile(saveDir + "/text.txt"); + if (!textFile.open(QIODevice::ReadOnly)) { + return; + } + ui->textTextEdit->document()->setPlainText(textFile.readAll()); + textFile.close(); + + QFile programFile(saveDir + "/program.bf"); + if (!programFile.open(QIODevice::ReadOnly)) { + return; + } + ui->programTextEdit->document()->setPlainText(programFile.readAll()); + programFile.close(); + + QFile motifFile(saveDir + "/motif.txt"); + if (!motifFile.open(QIODevice::ReadOnly)) { + return; + } + ui->motifTextEdit->document()->setPlainText(motifFile.readAll()); + motifFile.close(); + + QFile resultFile(saveDir + "/result.txt"); + if (!resultFile.open(QIODevice::ReadOnly)) { + return; + } + ui->resultTextEdit->document()->setPlainText(resultFile.readAll()); + resultFile.close(); + + QImage image; + image.load(saveDir + "/qrcode.png", "PNG"); + ui->qrcodeLabel->setPixmap(QPixmap::fromImage(image)); + } +} diff --git a/qbrainfuck.h b/qbrainfuck.h index ec2af0b..0e2b8e7 100644 --- a/qbrainfuck.h +++ b/qbrainfuck.h @@ -4,9 +4,8 @@ #include #include #include -#include -#include -#include +#include +#include #include "decoderthread.h" namespace Ui { @@ -36,14 +35,36 @@ private slots: void on_regexLineEdit_textChanged(const QString &arg1); - void on_saveButton_clicked(); - - void on_loadButton_clicked(); - void setText(QString text); void errorMessage(QString message); + void on_textLoadButton_clicked(); + + void on_textSaveButton_clicked(); + + void on_programLoadButton_clicked(); + + void on_programSaveButton_clicked(); + + void on_motifLoadButton_clicked(); + + void on_motifSaveButton_clicked(); + + void on_resultSaveButton_clicked(); + + void on_resultLoadButton_clicked(); + + void on_qrcodeButton_clicked(); + + void on_resultTextEdit_textChanged(); + + void on_qrcodeSaveButton_clicked(); + + void on_saveAllButton_clicked(); + + void on_loadAllButton_clicked(); + private: Ui::QBrainfuck *ui; DecoderThread *decoder; diff --git a/qbrainfuck.ui b/qbrainfuck.ui index 694676e..239380c 100644 --- a/qbrainfuck.ui +++ b/qbrainfuck.ui @@ -6,7 +6,7 @@ 0 0 - 1194 + 1189 634 @@ -14,270 +14,364 @@ QBrainfuck - - + + 0 - + - Main + Plain Text - + + + + + Save to file ... + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 1 + + + 255 + + + 10 + + + + + + + Max signs (+/-) + + + + + + + Decades size + + + - - - Brainfuck program + + + 1 + + + 255 + + + 10 - - - - - - Monospace - - - - QPlainTextEdit::NoWrap - - - - - - - Program length: 0 - - - - - - - Decode - - - - - - - - Motif + + + + Load from file ... - - - - - - Monospace - - - - QPlainTextEdit::NoWrap - - - - - - - Motif length: 0 - - - - - - - Motif - - - - - - - - Result - - - - - - - Monospace - - - - QPlainTextEdit::NoWrap - - - - - - - Save - - - - - - - Load - - - - - - - - - - Plain text - - - - - - - Monospace - - - - QPlainTextEdit::NoWrap - - - - - - - Text length: 0 - - - - - - - Encode - - - - - - - - - - Options - - - - - - QFormLayout::ExpandingFieldsGrow - - - - - Input - - - - - - - - - - # - - - - - - - Motif regex - - - - - - - - - QFormLayout::ExpandingFieldsGrow - - - - - Max signs (+/-) - - - - - - - 1 - - - 255 - - - 10 - - - - - - - Decades size - - - - - - - 1 - - - 255 - - - 10 - - - - - - - - - - textGroupBox - programGroupBox - motifGoupBox - resultGroupBox - optionsGroupBox - - - - Debug - - - - + + Monospace - QTextEdit::NoWrap + QPlainTextEdit::NoWrap + + + + + + + Encode + + + Brainfuck program + + + + + + + + + Load from file ... + + + + + + + Input + + + + + + + Decode + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Save to file ... + + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + + Motif + + + + + + # + + + + + + + Motif + + + + + + + Motif regex + + + + + + + Load from file... + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Save to file ... + + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + + Result + + + + + + Save to file ... + + + + + + + Load from file ... + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + QRCode + + + + + + + + QRCode + + + + + + Save to file ... + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + QRCode + + + + + + + + + + + Text length: 0 + + + + + + + Motif length: 0 + + + + + + + Program length: 0 + + + + + + + Result length: 0 + + + + + + + Save All + + + + + + + Load All +