Skip to content

Commit

Permalink
User interface
Browse files Browse the repository at this point in the history
GUI
  • Loading branch information
lostjared authored Jan 18, 2025
1 parent d908a61 commit 1835264
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bin2cpp20-gui/bin2cpp.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 5): QT += widgets

# We want to use C++20 features
CONFIG += c++20

TEMPLATE = app
TARGET = bin2cpp

SOURCES += \
main.cpp \
mainwindow.cpp \
worker.cpp

HEADERS += \
mainwindow.h \
worker.h
10 changes: 10 additions & 0 deletions bin2cpp20-gui/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
206 changes: 206 additions & 0 deletions bin2cpp20-gui/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#include "mainwindow.h"
#include "worker.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QTextEdit>
#include <QLabel>
#include <QThread>
#include <QClipboard>
#include <QGuiApplication>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_inputFileLine(nullptr),
m_variableNameLine(nullptr),
m_cExprCheck(nullptr),
m_asStringCheck(nullptr),
m_unicodeCheck(nullptr),
m_sortCheck(nullptr),
m_generateButton(nullptr),
m_copyButton(nullptr),
m_saveButton(nullptr),
m_outputText(nullptr)
{
setupUI();
setupConnections();
setWindowTitle("bin2cpp20");
resize(800, 600);
}

MainWindow::~MainWindow()
{
}

void MainWindow::setupUI()
{
QWidget *central = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(central);

{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Input File:", central);
m_inputFileLine = new QLineEdit(central);
QPushButton *browseBtn = new QPushButton("Browse...", central);

layout->addWidget(label);
layout->addWidget(m_inputFileLine);
layout->addWidget(browseBtn);
mainLayout->addLayout(layout);

connect(browseBtn, &QPushButton::clicked, this, &MainWindow::onBrowse);
}

{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Variable Name:", central);
m_variableNameLine = new QLineEdit(central);

layout->addWidget(label);
layout->addWidget(m_variableNameLine);
mainLayout->addLayout(layout);
}

{
QHBoxLayout *layout = new QHBoxLayout;

m_cExprCheck = new QCheckBox("Use constexpr", central);
m_asStringCheck = new QCheckBox("String Output", central);
m_unicodeCheck = new QCheckBox("Unicode", central);
m_sortCheck = new QCheckBox("Sort Strings", central);

layout->addWidget(m_cExprCheck);
layout->addWidget(m_asStringCheck);
layout->addWidget(m_unicodeCheck);
layout->addWidget(m_sortCheck);
mainLayout->addLayout(layout);
}

{
QHBoxLayout *layout = new QHBoxLayout;
m_generateButton = new QPushButton("Generate", central);
m_copyButton = new QPushButton("Copy Output", central);
m_saveButton = new QPushButton("Save Output", central);

layout->addWidget(m_generateButton);
layout->addWidget(m_copyButton);
layout->addWidget(m_saveButton);
mainLayout->addLayout(layout);
}

m_outputText = new QTextEdit(central);
m_outputText->setReadOnly(true);
mainLayout->addWidget(m_outputText);

setCentralWidget(central);
setGeometry(150, 150, 800, 600);
}

void MainWindow::setupConnections()
{
connect(m_generateButton, &QPushButton::clicked, this, &MainWindow::onGenerate);
connect(m_copyButton, &QPushButton::clicked, this, &MainWindow::onCopy);
connect(m_saveButton, &QPushButton::clicked, this, &MainWindow::onSave);
connect(m_cExprCheck, &QCheckBox::toggled, this, &MainWindow::onCheckCExprChanged);
}

void MainWindow::onBrowse()
{
QString filePath = QFileDialog::getOpenFileName(this, "Select Input File");
if (!filePath.isEmpty()) {
m_inputFileLine->setText(filePath);
}
}

void MainWindow::onGenerate()
{
m_outputText->clear();

QString inputFile = m_inputFileLine->text().trimmed();
QString variableName = m_variableNameLine->text().trimmed();
bool cExpr = m_cExprCheck->isChecked();
bool asString = m_asStringCheck->isChecked();
bool unicode = m_unicodeCheck->isChecked();
bool sorted = m_sortCheck->isChecked();

if (inputFile.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please select an input file.");
return;
}
if (variableName.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please provide a variable name.");
return;
}
QThread *thread = new QThread(this);
Worker *worker = new Worker(inputFile, variableName, cExpr, asString, unicode, sorted);
worker->moveToThread(thread);
connect(thread, &QThread::started, worker, &Worker::doWork);
connect(worker, &Worker::finished, this, &MainWindow::handleWorkerFinished);
connect(worker, &Worker::finished, thread, &QThread::quit);
connect(thread, &QThread::finished, worker, &Worker::deleteLater);
connect(thread, &QThread::finished, thread, &QThread::deleteLater);

// Start
thread->start();
}

void MainWindow::onCopy()
{
QString text = m_outputText->toPlainText();
if (!text.isEmpty()) {
QGuiApplication::clipboard()->setText(text);
}
}

void MainWindow::onSave()
{
QString text = m_outputText->toPlainText();
if (text.isEmpty()) {
QMessageBox::information(this, "Info", "No output to save.");
return;
}

QString fileName = QFileDialog::getSaveFileName(this, "Save Output", QString(), "*.hpp");
if (!fileName.isEmpty()) {
if (!fileName.endsWith(".hpp", Qt::CaseInsensitive)) {
fileName += ".hpp";
}
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(text.toUtf8());
file.close();
} else {
QMessageBox::warning(this, "Error", "Could not open file for writing.");
}
}
}

void MainWindow::onCheckCExprChanged()
{
if (m_cExprCheck->isChecked()) {
m_asStringCheck->setChecked(false);
m_asStringCheck->setEnabled(false);
m_unicodeCheck->setChecked(false);
m_unicodeCheck->setEnabled(false);
m_sortCheck->setChecked(false);
m_sortCheck->setEnabled(false);
} else {
m_asStringCheck->setEnabled(true);
m_unicodeCheck->setEnabled(true);
m_sortCheck->setEnabled(true);
}
}

void MainWindow::handleWorkerFinished(const QString &result, const QString &error)
{
if (!error.isEmpty()) {
QMessageBox::critical(this, "Error", error);
} else {
m_outputText->setPlainText(result);
}
}
43 changes: 43 additions & 0 deletions bin2cpp20-gui/mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QLineEdit;
class QPushButton;
class QCheckBox;
class QTextEdit;
class QLabel;

class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void onBrowse(); // Browse for input file
void onGenerate(); // Start background generation
void onCopy(); // Copy output text to clipboard
void onSave(); // Save output text to a file
void onCheckCExprChanged(); // If "constexpr" is checked/unchecked

void handleWorkerFinished(const QString &result, const QString &error);

private:
// UI elements
QLineEdit *m_inputFileLine;
QLineEdit *m_variableNameLine;
QCheckBox *m_cExprCheck;
QCheckBox *m_asStringCheck;
QCheckBox *m_unicodeCheck;
QCheckBox *m_sortCheck;
QPushButton *m_generateButton;
QPushButton *m_copyButton;
QPushButton *m_saveButton;
QTextEdit *m_outputText;
void setupUI();
void setupConnections();
};

#endif
Loading

0 comments on commit 1835264

Please sign in to comment.