|
| 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 | +} |
0 commit comments