|
| 1 | +#include "main_window.hpp" |
| 2 | + |
| 3 | +#include <QDateTime> |
| 4 | +// #include <QDesktopServices> // openUrl |
| 5 | +#include <QFileDialog> |
| 6 | +#include <QMessageBox> |
| 7 | +#include <QSqlDatabase> |
| 8 | +#include <QSqlError> |
| 9 | +#include <QSqlQuery> |
| 10 | +#include <QTranslator> |
| 11 | + |
| 12 | +#include "ui_main_window.h" |
| 13 | + |
| 14 | +namespace |
| 15 | +{ |
| 16 | + |
| 17 | +QString const SQL_DB_DRIVER("QSQLITE"); |
| 18 | + |
| 19 | +QString const SQL_QUERY_DATA(R"( |
| 20 | +SELECT id,alias,setcode,type,atk,def,level,race,attribute,ot,category |
| 21 | +FROM datas WHERE datas.id = ?; |
| 22 | +)"); |
| 23 | + |
| 24 | +QString const SQL_QUERY_TEXT(R"( |
| 25 | +SELECT name,desc,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,str15,str16 |
| 26 | +FROM texts WHERE texts.id = ?; |
| 27 | +)"); |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +// public |
| 32 | + |
| 33 | +MainWindow::MainWindow(QWidget* parent) : |
| 34 | + QMainWindow(parent), |
| 35 | + spanishTranslator(std::make_unique<QTranslator>()), |
| 36 | + ui(std::make_unique<Ui::MainWindow>()) |
| 37 | +{ |
| 38 | + spanishTranslator->load(":/es"); |
| 39 | + QApplication::instance()->installTranslator(spanishTranslator.get()); |
| 40 | + ui->setupUi(this); |
| 41 | + // TODO: Create custom Validator that works with unsigned integers... |
| 42 | +// ui->passLineEdit->setValidator(new QIntValidator(0, 4294967295, this)); |
| 43 | +// ui->aliasLineEdit->setValidator(new QIntValidator(0, 4294967295, this)); |
| 44 | +// connect(ui->actionNewDatabase, &QAction::triggered, this, &MainWindow::newDatabase); |
| 45 | + connect(ui->actionOpenDatabase, &QAction::triggered, this, &MainWindow::openDatabase); |
| 46 | + connect(ui->actionCloseDatabase, &QAction::triggered, this, &MainWindow::closeDatabase); |
| 47 | + connect(ui->actionSpanish, &QAction::triggered, this, &MainWindow::toSpanish); |
| 48 | + connect(ui->actionEnglish, &QAction::triggered, this, &MainWindow::toEnglish); |
| 49 | +} |
| 50 | + |
| 51 | +MainWindow::~MainWindow() |
| 52 | +{ |
| 53 | + QApplication::instance()->removeTranslator(spanishTranslator.get()); |
| 54 | +} |
| 55 | + |
| 56 | +void MainWindow::changeEvent(QEvent *event) |
| 57 | +{ |
| 58 | + if (event->type() == QEvent::LanguageChange) |
| 59 | + ui->retranslateUi(this); |
| 60 | + else |
| 61 | + QWidget::changeEvent(event); |
| 62 | +} |
| 63 | + |
| 64 | +// private slots |
| 65 | + |
| 66 | +void MainWindow::openDatabase() |
| 67 | +{ |
| 68 | + auto does_db_have_correct_format = [&](QSqlDatabase& db) |
| 69 | + { |
| 70 | + return true; // TODO |
| 71 | + }; |
| 72 | + if(!checkAndAskToCloseDb()) |
| 73 | + return; |
| 74 | + QString const file = QFileDialog::getOpenFileName |
| 75 | + ( |
| 76 | + this, |
| 77 | + tr("Select Database"), |
| 78 | + ".", |
| 79 | + tr("YGOPro Database (*.cdb *.db *.sqlite)") |
| 80 | + ); |
| 81 | + if(file.isEmpty()) |
| 82 | + return; |
| 83 | + auto db = QSqlDatabase::addDatabase(SQL_DB_DRIVER); |
| 84 | + db.setDatabaseName(file); |
| 85 | + if(!db.open()) |
| 86 | + { |
| 87 | + QMessageBox::critical(this, tr("Error Opening Database"), db.lastError().text()); |
| 88 | + closeDatabase(); |
| 89 | + return; |
| 90 | + } |
| 91 | + if(!does_db_have_correct_format(db)) |
| 92 | + { |
| 93 | + QMessageBox::critical(this, tr("Error Opening Database"), tr("Selected file is not a proper YGOPRO database.")); |
| 94 | + closeDatabase(); |
| 95 | + return; |
| 96 | + } |
| 97 | + updateUiWithCode(5043010); // TODO: Maybe load the first card on the db? |
| 98 | +} |
| 99 | + |
| 100 | +void MainWindow::closeDatabase() |
| 101 | +{ |
| 102 | + auto db = QSqlDatabase::database(); |
| 103 | + if(db.isValid()) |
| 104 | + { |
| 105 | + db.close(); |
| 106 | + QSqlDatabase::removeDatabase(db.connectionName()); |
| 107 | + } |
| 108 | + updateUiWithCode(0U); |
| 109 | +} |
| 110 | + |
| 111 | +void MainWindow::toSpanish() |
| 112 | +{ |
| 113 | + ui->actionSpanish->setEnabled(false); |
| 114 | + ui->actionEnglish->setEnabled(true); |
| 115 | + ui->actionEnglish->setChecked(false); |
| 116 | + QApplication::instance()->installTranslator(spanishTranslator.get()); |
| 117 | +} |
| 118 | + |
| 119 | +void MainWindow::toEnglish() |
| 120 | +{ |
| 121 | + ui->actionEnglish->setEnabled(false); |
| 122 | + ui->actionSpanish->setEnabled(true); |
| 123 | + ui->actionSpanish->setChecked(false); |
| 124 | + QApplication::instance()->removeTranslator(spanishTranslator.get()); |
| 125 | +} |
| 126 | + |
| 127 | +// private |
| 128 | + |
| 129 | +bool MainWindow::checkAndAskToCloseDb() |
| 130 | +{ |
| 131 | + if(!QSqlDatabase::database().isValid()) |
| 132 | + return true; |
| 133 | + if(QMessageBox::question(this, tr("Close Opened Database?"), tr("Do you wish to close the currently opened database?")) == QMessageBox::Yes) |
| 134 | + { |
| 135 | + closeDatabase(); |
| 136 | + return true; |
| 137 | + } |
| 138 | + return false; |
| 139 | +} |
| 140 | + |
| 141 | +void MainWindow::updateUiWithCode(quint32 code) |
| 142 | +{ |
| 143 | + // Clean the UI first |
| 144 | + if(code == 0U) // If code is 0 then we don't fill in the info |
| 145 | + return; |
| 146 | + // Query the data |
| 147 | + auto db = QSqlDatabase::database(); |
| 148 | + Q_ASSERT(db.isValid()); |
| 149 | + QSqlQuery q1(db); |
| 150 | + q1.prepare(SQL_QUERY_DATA); |
| 151 | + q1.bindValue(0, code); |
| 152 | + bool const q1result = q1.exec() && q1.first(); |
| 153 | + Q_ASSERT(q1result); |
| 154 | + QSqlQuery q2(db); |
| 155 | + q2.prepare(SQL_QUERY_TEXT); |
| 156 | + q2.bindValue(0, code); |
| 157 | + bool const q2result = q2.exec() && q2.first(); |
| 158 | + Q_ASSERT(q2result); |
| 159 | + // Proceed to populate the fields with the new data |
| 160 | + ui->passLineEdit->setText(q1.value(0).toString()); |
| 161 | + ui->aliasLineEdit->setText(q1.value(1).toString()); |
| 162 | + // TODO: 2 - setcodes / archetypes |
| 163 | + constexpr quint32 TYPE_LINK = 0x4000000; |
| 164 | + quint32 const type = q1.value(3).toUInt(); |
| 165 | + // TODO: 3 - type - checkboxes |
| 166 | + constexpr qint32 QMARK_ATK_DEF = -2; |
| 167 | + qint32 const atk = q1.value(4).toInt(); |
| 168 | + ui->atkQmCheckBox->setChecked(atk == QMARK_ATK_DEF); |
| 169 | + ui->atkSpinBox->setEnabled(atk != QMARK_ATK_DEF); |
| 170 | + ui->atkSpinBox->setValue(std::max(atk, 0)); |
| 171 | + if(qint32 const def = q1.value(5).toInt(); (type & TYPE_LINK) == 0U) |
| 172 | + { |
| 173 | + ui->defQmCheckBox->setChecked(def == QMARK_ATK_DEF); |
| 174 | + ui->defSpinBox->setEnabled(def != QMARK_ATK_DEF); |
| 175 | + ui->defSpinBox->setValue(std::max(def, 0)); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + ui->markerBottomLeftButton->setChecked((def & 0x1) != 0U); |
| 180 | + ui->markerBottomButton->setChecked((def & 0x2) != 0U); |
| 181 | + ui->markerBottomRightButton->setChecked((def & 0x4) != 0U); |
| 182 | + ui->markerLeftButton->setChecked((def & 0x8) != 0U); |
| 183 | + ui->markerRightButton->setChecked((def & 0x20) != 0U); |
| 184 | + ui->markerTopLeftButton->setChecked((def & 0x40) != 0U); |
| 185 | + ui->markerTopButton->setChecked((def & 0x80) != 0U); |
| 186 | + ui->markerTopRightButton->setChecked((def & 0x100) != 0U); |
| 187 | + } |
| 188 | + quint32 const dbLevel = q1.value(6).toUInt(); |
| 189 | + ui->levelSpinBox->setValue(dbLevel & 0x800000FF); |
| 190 | + ui->lScaleSpinBox->setValue((dbLevel >> 24U) & 0xFF); |
| 191 | + ui->rScaleSpinBox->setValue((dbLevel >> 16U) & 0xFF); |
| 192 | + // TODO: 7 - race - checkboxes |
| 193 | + // TODO: 8 - attribute - checkboxes |
| 194 | + // TODO: 9 - ot/scope - checkboxes |
| 195 | + // TODO: 10 - category - checkboxes |
| 196 | + |
| 197 | + ui->nameLineEdit->setText(q2.value(0).toString()); |
| 198 | + ui->descPlainTextEdit->setPlainText(q2.value(1).toString()); |
| 199 | + int const count = ui->stringsTableWidget->rowCount(); |
| 200 | + for(int i = 0; i < count; ++i) |
| 201 | + { |
| 202 | + auto& item = *ui->stringsTableWidget->item(i, 0); |
| 203 | + item.setText(q2.value(2 + i).toString()); |
| 204 | + } |
| 205 | +} |
| 206 | + |
0 commit comments