Skip to content

Commit

Permalink
Support opening databases from command-line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
DyXel committed Nov 12, 2024
1 parent 7023935 commit 0f7995d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
104 changes: 54 additions & 50 deletions src/gui/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 19 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QApplication>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QTranslator>

Expand All @@ -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);
Expand All @@ -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();
}

0 comments on commit 0f7995d

Please sign in to comment.