Skip to content

Commit 1835264

Browse files
authored
User interface
GUI
1 parent d908a61 commit 1835264

File tree

6 files changed

+473
-0
lines changed

6 files changed

+473
-0
lines changed

bin2cpp20-gui/bin2cpp.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
QT += core gui
2+
greaterThan(QT_MAJOR_VERSION, 5): QT += widgets
3+
4+
# We want to use C++20 features
5+
CONFIG += c++20
6+
7+
TEMPLATE = app
8+
TARGET = bin2cpp
9+
10+
SOURCES += \
11+
main.cpp \
12+
mainwindow.cpp \
13+
worker.cpp
14+
15+
HEADERS += \
16+
mainwindow.h \
17+
worker.h

bin2cpp20-gui/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <QApplication>
2+
#include "mainwindow.h"
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
w.show();
9+
return a.exec();
10+
}

bin2cpp20-gui/mainwindow.cpp

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#include "mainwindow.h"
2+
#include "worker.h"
3+
4+
#include <QHBoxLayout>
5+
#include <QVBoxLayout>
6+
#include <QFileDialog>
7+
#include <QCheckBox>
8+
#include <QLineEdit>
9+
#include <QPushButton>
10+
#include <QTextEdit>
11+
#include <QLabel>
12+
#include <QThread>
13+
#include <QClipboard>
14+
#include <QGuiApplication>
15+
#include <QMessageBox>
16+
17+
MainWindow::MainWindow(QWidget *parent)
18+
: QMainWindow(parent),
19+
m_inputFileLine(nullptr),
20+
m_variableNameLine(nullptr),
21+
m_cExprCheck(nullptr),
22+
m_asStringCheck(nullptr),
23+
m_unicodeCheck(nullptr),
24+
m_sortCheck(nullptr),
25+
m_generateButton(nullptr),
26+
m_copyButton(nullptr),
27+
m_saveButton(nullptr),
28+
m_outputText(nullptr)
29+
{
30+
setupUI();
31+
setupConnections();
32+
setWindowTitle("bin2cpp20");
33+
resize(800, 600);
34+
}
35+
36+
MainWindow::~MainWindow()
37+
{
38+
}
39+
40+
void MainWindow::setupUI()
41+
{
42+
QWidget *central = new QWidget(this);
43+
QVBoxLayout *mainLayout = new QVBoxLayout(central);
44+
45+
{
46+
QHBoxLayout *layout = new QHBoxLayout;
47+
QLabel *label = new QLabel("Input File:", central);
48+
m_inputFileLine = new QLineEdit(central);
49+
QPushButton *browseBtn = new QPushButton("Browse...", central);
50+
51+
layout->addWidget(label);
52+
layout->addWidget(m_inputFileLine);
53+
layout->addWidget(browseBtn);
54+
mainLayout->addLayout(layout);
55+
56+
connect(browseBtn, &QPushButton::clicked, this, &MainWindow::onBrowse);
57+
}
58+
59+
{
60+
QHBoxLayout *layout = new QHBoxLayout;
61+
QLabel *label = new QLabel("Variable Name:", central);
62+
m_variableNameLine = new QLineEdit(central);
63+
64+
layout->addWidget(label);
65+
layout->addWidget(m_variableNameLine);
66+
mainLayout->addLayout(layout);
67+
}
68+
69+
{
70+
QHBoxLayout *layout = new QHBoxLayout;
71+
72+
m_cExprCheck = new QCheckBox("Use constexpr", central);
73+
m_asStringCheck = new QCheckBox("String Output", central);
74+
m_unicodeCheck = new QCheckBox("Unicode", central);
75+
m_sortCheck = new QCheckBox("Sort Strings", central);
76+
77+
layout->addWidget(m_cExprCheck);
78+
layout->addWidget(m_asStringCheck);
79+
layout->addWidget(m_unicodeCheck);
80+
layout->addWidget(m_sortCheck);
81+
mainLayout->addLayout(layout);
82+
}
83+
84+
{
85+
QHBoxLayout *layout = new QHBoxLayout;
86+
m_generateButton = new QPushButton("Generate", central);
87+
m_copyButton = new QPushButton("Copy Output", central);
88+
m_saveButton = new QPushButton("Save Output", central);
89+
90+
layout->addWidget(m_generateButton);
91+
layout->addWidget(m_copyButton);
92+
layout->addWidget(m_saveButton);
93+
mainLayout->addLayout(layout);
94+
}
95+
96+
m_outputText = new QTextEdit(central);
97+
m_outputText->setReadOnly(true);
98+
mainLayout->addWidget(m_outputText);
99+
100+
setCentralWidget(central);
101+
setGeometry(150, 150, 800, 600);
102+
}
103+
104+
void MainWindow::setupConnections()
105+
{
106+
connect(m_generateButton, &QPushButton::clicked, this, &MainWindow::onGenerate);
107+
connect(m_copyButton, &QPushButton::clicked, this, &MainWindow::onCopy);
108+
connect(m_saveButton, &QPushButton::clicked, this, &MainWindow::onSave);
109+
connect(m_cExprCheck, &QCheckBox::toggled, this, &MainWindow::onCheckCExprChanged);
110+
}
111+
112+
void MainWindow::onBrowse()
113+
{
114+
QString filePath = QFileDialog::getOpenFileName(this, "Select Input File");
115+
if (!filePath.isEmpty()) {
116+
m_inputFileLine->setText(filePath);
117+
}
118+
}
119+
120+
void MainWindow::onGenerate()
121+
{
122+
m_outputText->clear();
123+
124+
QString inputFile = m_inputFileLine->text().trimmed();
125+
QString variableName = m_variableNameLine->text().trimmed();
126+
bool cExpr = m_cExprCheck->isChecked();
127+
bool asString = m_asStringCheck->isChecked();
128+
bool unicode = m_unicodeCheck->isChecked();
129+
bool sorted = m_sortCheck->isChecked();
130+
131+
if (inputFile.isEmpty()) {
132+
QMessageBox::warning(this, "Warning", "Please select an input file.");
133+
return;
134+
}
135+
if (variableName.isEmpty()) {
136+
QMessageBox::warning(this, "Warning", "Please provide a variable name.");
137+
return;
138+
}
139+
QThread *thread = new QThread(this);
140+
Worker *worker = new Worker(inputFile, variableName, cExpr, asString, unicode, sorted);
141+
worker->moveToThread(thread);
142+
connect(thread, &QThread::started, worker, &Worker::doWork);
143+
connect(worker, &Worker::finished, this, &MainWindow::handleWorkerFinished);
144+
connect(worker, &Worker::finished, thread, &QThread::quit);
145+
connect(thread, &QThread::finished, worker, &Worker::deleteLater);
146+
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
147+
148+
// Start
149+
thread->start();
150+
}
151+
152+
void MainWindow::onCopy()
153+
{
154+
QString text = m_outputText->toPlainText();
155+
if (!text.isEmpty()) {
156+
QGuiApplication::clipboard()->setText(text);
157+
}
158+
}
159+
160+
void MainWindow::onSave()
161+
{
162+
QString text = m_outputText->toPlainText();
163+
if (text.isEmpty()) {
164+
QMessageBox::information(this, "Info", "No output to save.");
165+
return;
166+
}
167+
168+
QString fileName = QFileDialog::getSaveFileName(this, "Save Output", QString(), "*.hpp");
169+
if (!fileName.isEmpty()) {
170+
if (!fileName.endsWith(".hpp", Qt::CaseInsensitive)) {
171+
fileName += ".hpp";
172+
}
173+
QFile file(fileName);
174+
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
175+
file.write(text.toUtf8());
176+
file.close();
177+
} else {
178+
QMessageBox::warning(this, "Error", "Could not open file for writing.");
179+
}
180+
}
181+
}
182+
183+
void MainWindow::onCheckCExprChanged()
184+
{
185+
if (m_cExprCheck->isChecked()) {
186+
m_asStringCheck->setChecked(false);
187+
m_asStringCheck->setEnabled(false);
188+
m_unicodeCheck->setChecked(false);
189+
m_unicodeCheck->setEnabled(false);
190+
m_sortCheck->setChecked(false);
191+
m_sortCheck->setEnabled(false);
192+
} else {
193+
m_asStringCheck->setEnabled(true);
194+
m_unicodeCheck->setEnabled(true);
195+
m_sortCheck->setEnabled(true);
196+
}
197+
}
198+
199+
void MainWindow::handleWorkerFinished(const QString &result, const QString &error)
200+
{
201+
if (!error.isEmpty()) {
202+
QMessageBox::critical(this, "Error", error);
203+
} else {
204+
m_outputText->setPlainText(result);
205+
}
206+
}

bin2cpp20-gui/mainwindow.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
6+
class QLineEdit;
7+
class QPushButton;
8+
class QCheckBox;
9+
class QTextEdit;
10+
class QLabel;
11+
12+
class MainWindow : public QMainWindow {
13+
Q_OBJECT
14+
public:
15+
explicit MainWindow(QWidget *parent = nullptr);
16+
~MainWindow();
17+
18+
private slots:
19+
void onBrowse(); // Browse for input file
20+
void onGenerate(); // Start background generation
21+
void onCopy(); // Copy output text to clipboard
22+
void onSave(); // Save output text to a file
23+
void onCheckCExprChanged(); // If "constexpr" is checked/unchecked
24+
25+
void handleWorkerFinished(const QString &result, const QString &error);
26+
27+
private:
28+
// UI elements
29+
QLineEdit *m_inputFileLine;
30+
QLineEdit *m_variableNameLine;
31+
QCheckBox *m_cExprCheck;
32+
QCheckBox *m_asStringCheck;
33+
QCheckBox *m_unicodeCheck;
34+
QCheckBox *m_sortCheck;
35+
QPushButton *m_generateButton;
36+
QPushButton *m_copyButton;
37+
QPushButton *m_saveButton;
38+
QTextEdit *m_outputText;
39+
void setupUI();
40+
void setupConnections();
41+
};
42+
43+
#endif

0 commit comments

Comments
 (0)