From 0f7995de0e76c2bcaa55d99cf2509c0c82cbb930 Mon Sep 17 00:00:00 2001 From: Dylam De La Torre Date: Tue, 12 Nov 2024 14:25:10 +0100 Subject: [PATCH] Support opening databases from command-line arguments --- src/gui/main_window.cpp | 104 +++++++++++++++++++++------------------- src/gui/main_window.hpp | 2 + src/main.cpp | 23 +++++++-- 3 files changed, 75 insertions(+), 54 deletions(-) diff --git a/src/gui/main_window.cpp b/src/gui/main_window.cpp index f2f24d9..5d18b8d 100644 --- a/src/gui/main_window.cpp +++ b/src/gui/main_window.cpp @@ -155,6 +155,58 @@ MainWindow::~MainWindow() QApplication::removeTranslator(currentTranslator.get()); } +void MainWindow::openDatabaseWithFile(QString const& file) +{ + auto does_db_have_correct_format = [&](QSqlDatabase& db) + { + auto verify_table = [&](const auto& query_and_result) + { + auto q = buildQuery(db, query_and_result[0]); + execQuery(q); + auto record = q.record(); + auto name_index = record.indexOf("name"); + auto pk_index = record.indexOf("pk"); + if(name_index == -1 || pk_index == -1) + return false; + QStringList columns; + while(q.next()) + columns << q.value(name_index).toString() + + q.value(pk_index).toString(); + columns.sort(Qt::CaseInsensitive); + return columns.join(',').compare(query_and_result[1], + Qt::CaseInsensitive) == 0; + }; + return verify_table(SQL_DATAS_TABLE_FIELDS) && + verify_table(SQL_TEXTS_TABLE_FIELDS); + }; + if(file.isEmpty()) + return; + if(QSqlDatabase::contains(file)) + { + ui->dbEditorTabsWidget->setCurrentWidget(&widgetFromConnection(file)); + return; + } + auto db = QSqlDatabase::addDatabase(SQL_DB_DRIVER, file); + db.setDatabaseName(file); + if(!db.open()) + { + QMessageBox::critical(this, tr("Error Opening Database"), + db.lastError().text()); + QSqlDatabase::removeDatabase(file); + return; + } + if(!does_db_have_correct_format(db)) + { + QMessageBox::critical( + this, tr("Error Opening Database"), + tr("Selected file is not a proper YGOPRO database.")); + db.close(); + QSqlDatabase::removeDatabase(file); + return; + } + addTab(file); +} + void MainWindow::changeEvent(QEvent* event) { switch(event->type()) @@ -234,57 +286,9 @@ void MainWindow::closeTabDatabase(int index) void MainWindow::openDatabase() { - auto does_db_have_correct_format = [&](QSqlDatabase& db) - { - auto verify_table = [&](const auto& query_and_result) - { - auto q = buildQuery(db, query_and_result[0]); - execQuery(q); - auto record = q.record(); - auto name_index = record.indexOf("name"); - auto pk_index = record.indexOf("pk"); - if(name_index == -1 || pk_index == -1) - return false; - QStringList columns; - while(q.next()) - columns << q.value(name_index).toString() + - q.value(pk_index).toString(); - columns.sort(Qt::CaseInsensitive); - return columns.join(',').compare(query_and_result[1], - Qt::CaseInsensitive) == 0; - }; - return verify_table(SQL_DATAS_TABLE_FIELDS) && - verify_table(SQL_TEXTS_TABLE_FIELDS); - }; - QString const file = QFileDialog::getOpenFileName( + openDatabaseWithFile(QFileDialog::getOpenFileName( this, tr("Select Database"), ".", - tr("YGOPro Database (*.cdb *.db *.sqlite)")); - if(file.isEmpty()) - return; - if(QSqlDatabase::contains(file)) - { - ui->dbEditorTabsWidget->setCurrentWidget(&widgetFromConnection(file)); - return; - } - auto db = QSqlDatabase::addDatabase(SQL_DB_DRIVER, file); - db.setDatabaseName(file); - if(!db.open()) - { - QMessageBox::critical(this, tr("Error Opening Database"), - db.lastError().text()); - QSqlDatabase::removeDatabase(file); - return; - } - if(!does_db_have_correct_format(db)) - { - QMessageBox::critical( - this, tr("Error Opening Database"), - tr("Selected file is not a proper YGOPRO database.")); - db.close(); - QSqlDatabase::removeDatabase(file); - return; - } - addTab(file); + tr("YGOPro Database (*.cdb *.db *.sqlite)"))); } void MainWindow::showClipboardDatabase() diff --git a/src/gui/main_window.hpp b/src/gui/main_window.hpp index 42684ca..10c7fbf 100644 --- a/src/gui/main_window.hpp +++ b/src/gui/main_window.hpp @@ -23,6 +23,8 @@ class MainWindow final : public QMainWindow explicit MainWindow(QWidget* parent = nullptr); ~MainWindow() override; + void openDatabaseWithFile(QString const& file); + protected: void changeEvent(QEvent* event) override; void closeEvent(QCloseEvent* event) override; diff --git a/src/main.cpp b/src/main.cpp index ae98fde..5fdac4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -11,11 +12,23 @@ int main(int argc, char* argv[]) QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); #endif - QApplication a(argc, argv); + QApplication app(argc, argv); + QCoreApplication::setApplicationName("datacorn"); + QCoreApplication::setApplicationVersion("0.9"); + + QCommandLineParser parser; + // NOTE: We recognize the CLA on all platforms, but only do something + // on Windows. Later we can see if this is useful in other OSs. + QCommandLineOption systemThemeOption( + QStringList() << "st" << "system-theme", + QCoreApplication::translate("main", "Use system's theme")); + parser.addOption(systemThemeOption); + + parser.process(app); #ifdef Q_OS_WIN - if(!a.arguments().contains("--system-theme")) + if(!parser.isSet(systemThemeOption)) { - a.setStyle("fusion"); + app.setStyle("fusion"); QPalette p; p.setColor(QPalette::Window, QColor(52, 52, 52)); p.setColor(QPalette::WindowText, Qt::white); @@ -30,10 +43,12 @@ int main(int argc, char* argv[]) p.setColor(QPalette::Link, QColor(220, 206, 128)); p.setColor(QPalette::Highlight, QColor(220, 206, 128)); p.setColor(QPalette::HighlightedText, Qt::black); - a.setPalette(p); + app.setPalette(p); } #endif // Q_OS_WIN MainWindow w; + for(auto const& arg : parser.positionalArguments()) + w.openDatabaseWithFile(arg); w.show(); return QApplication::exec(); }