Skip to content

Commit

Permalink
Demodialog now handles all demo loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro-Beirao committed May 26, 2024
1 parent 791f4e3 commit bc71bfc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 49 deletions.
35 changes: 26 additions & 9 deletions src/demodialog.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "demodialog.h"
#include <mainwindow.h>

demodialog::demodialog(QString missing_iwad, QStringList missing_files, QWidget *parent) : QDialog(parent)
demodialog::demodialog(QString footer_iwad, QStringList footer_files, QWidget *parent) : QDialog(parent)
{
QGridLayout *mainLayout = new QGridLayout;

QLabel *description;
if (missing_iwad.isEmpty() && missing_files.isEmpty())
if (footer_iwad.isEmpty() && footer_files.isEmpty())
{
description = new QLabel("No footer found on the demo.\n");
}
else
{
description = new QLabel("");
if (!missing_iwad.isEmpty()) description->setText("Demo IWAD not found.\n");
if (!missing_files.isEmpty()) description->setText(description->text() + "Demo PWADs/DEHs not found.\n");
if (!footer_iwad.isEmpty()) description->setText("Demo IWAD not found.\n");
if (!footer_files.isEmpty()) description->setText(description->text() + "Demo PWADs/DEHs not found.\n");
}
mainLayout->addWidget(description, 0, 0, 1, 3);

Expand All @@ -27,7 +27,8 @@ demodialog::demodialog(QString missing_iwad, QStringList missing_files, QWidget
iwad_comboBox->addItem(MainWindow::pMainWindow->iwad_comboBox()->itemText(i));
iwad_comboBox->setItemData(iwad_comboBox->count() - 1, MainWindow::pMainWindow->iwad_comboBox()->itemData(i, Qt::ToolTipRole), Qt::ToolTipRole);
}
iwad_comboBox->setCurrentIndex(MainWindow::pMainWindow->iwad_comboBox()->currentIndex());
qDebug() << footer_iwad;
iwad_comboBox->setCurrentIndex(iwad_comboBox->findText(removeExtension(footer_iwad).toLower()));
mainLayout->addWidget(iwad_comboBox, 1, 1, 1, 2);

QLabel *files_label = new QLabel("Files:");
Expand All @@ -36,7 +37,14 @@ demodialog::demodialog(QString missing_iwad, QStringList missing_files, QWidget
files_listWidget = new QTableWidget();
files = getFilePath_possibleFiles();

for (int i = 0; i < MainWindow::pMainWindow->wads_listWidget()->count(); i++)
{
files.append(QFileInfo(MainWindow::pMainWindow->wads_listWidget()->item(i)->data(Qt::ToolTipRole).toString()));
}
MainWindow::pMainWindow->wads_listWidget()->clear();

std::sort(files.begin(), files.end(), [](QFileInfo p1, QFileInfo p2) { return p1.baseName().toLower() < p2.baseName().toLower(); });
files.erase(std::unique(files.begin(), files.end()), files.end());

files_listWidget->setColumnCount(2);
files_listWidget->setRowCount(files.size() / 2 + 1);
Expand All @@ -62,11 +70,12 @@ demodialog::demodialog(QString missing_iwad, QStringList missing_files, QWidget
newItem->setFlags(newItem->flags() & ~Qt::ItemIsEditable);
files_listWidget->setItem(i, j, newItem);

for (int a = 0; a < MainWindow::pMainWindow->wads_listWidget()->count(); a++)
for (int a = 0; a < footer_files.count(); a++)
{
if (MainWindow::pMainWindow->wads_listWidget()->item(a)->data(Qt::ToolTipRole) == newItem->data(Qt::ToolTipRole))
if (footer_files[a].toLower() == newItem->text().toLower())
{
newItem->setSelected(true);
break;
}
}
}
Expand All @@ -79,12 +88,20 @@ demodialog::demodialog(QString missing_iwad, QStringList missing_files, QWidget
update_selected_count();
connect(files_listWidget, &QTableWidget::itemSelectionChanged, this, &demodialog::update_selected_count);

QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
mainLayout->addWidget(buttonBox, 5, 0, 1, 3);

setLayout(mainLayout);

if (iwad_comboBox->currentIndex() == -1 || files_listWidget->selectedItems().count() != footer_files.count())
{
this->open();
}
else
{
this->accept();
}
}

void demodialog::update_selected_count()
Expand Down
2 changes: 1 addition & 1 deletion src/demodialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class demodialog : public QDialog
Q_OBJECT

public:
explicit demodialog(QString missing_iwad, QStringList missing_files, QWidget *parent = nullptr);
explicit demodialog(QString footer_iwad, QStringList footer_files, QWidget *parent = nullptr);
int get_iwad_index();
QStringList get_files_list();

Expand Down
45 changes: 6 additions & 39 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,23 @@ void MainWindow::dropLmp(QString filePath)
QTextStream stream(&file);
QString buffer;

bool openDemoDialog = true;
QString missing_iwad = "";
QStringList missing_files;
QString footer_iwad = "";
QStringList footer_files;

while (stream.readLineInto(&buffer))
{
if (buffer.left(5) == "-iwad")
{
openDemoDialog = false;
ui->wads_listWidget->clear();

QStringList args = QProcess::splitCommand(buffer);

for (int i = 0; i < args.count() - 1; i++)
{
if (args[i] == "-iwad")
{
int iwad_index = ui->iwad_comboBox->findText(removeExtension(args[i + 1]).toLower());
if (iwad_index != -1)
{
ui->iwad_comboBox->setCurrentIndex(iwad_index);
}
else
{
missing_iwad = args[i + 1];
openDemoDialog = true;
}
footer_iwad = args[i + 1];
}
else if (args[i] == "-file" || args[i] == "-deh")
{
QFileInfoList files = getFilePath_possibleFiles();
for (i = i + 1; i < args.count(); i++)
{
if (args[i].size() < 2 || args[i][0] == '-')
Expand All @@ -309,22 +295,7 @@ void MainWindow::dropLmp(QString filePath)
int file_dot_pos = args[i].lastIndexOf('.');
if (file_dot_pos == -1) args[i] += ".wad";

bool found = false;
for (QFileInfo &file : files)
{
if (file.fileName().toLower() == args[i].toLower())
{
ui->wads_listWidget->addItem(file.fileName());
ui->wads_listWidget->item(ui->wads_listWidget->count() - 1)->setToolTip(file.absoluteFilePath());
found = true;
break;
}
}
if (!found)
{
missing_files.append(args[i]);
openDemoDialog = true;
}
footer_files.append(args[i]);
}
}
}
Expand All @@ -333,12 +304,8 @@ void MainWindow::dropLmp(QString filePath)

file.close();

if (openDemoDialog)
{
demodialog *demoDialogNew = new demodialog(missing_iwad, missing_files, this);
demoDialogNew->setAttribute(Qt::WA_DeleteOnClose);
demoDialogNew->open();
}
demodialog *demoDialogNew = new demodialog(footer_iwad, footer_files, this);
demoDialogNew->setAttribute(Qt::WA_DeleteOnClose);
}

void MainWindow::dropFile(QString fileName)
Expand Down

0 comments on commit bc71bfc

Please sign in to comment.