diff --git a/launcher/InstanceList.cpp b/launcher/InstanceList.cpp index 8eb43d5da..27b2da7cf 100644 --- a/launcher/InstanceList.cpp +++ b/launcher/InstanceList.cpp @@ -607,11 +607,11 @@ void InstanceList::providerUpdated() } } -QList InstanceList::getAllInstances() +QList InstanceList::getAllInstances() const { QList instanceList; - for (auto& inst : instances) { + for (const auto& inst : instances) { instanceList.append(inst.get()); } diff --git a/launcher/InstanceList.h b/launcher/InstanceList.h index 76ac01bc3..c0265c486 100644 --- a/launcher/InstanceList.h +++ b/launcher/InstanceList.h @@ -103,7 +103,7 @@ class InstanceList : public QAbstractListModel { InstListError loadList(); void saveNow(); - QList getAllInstances(); + QList getAllInstances() const; /* O(n) */ BaseInstance* getInstanceById(QString id) const; /* O(n) */ diff --git a/launcher/minecraft/MultiWorldList.cpp b/launcher/minecraft/MultiWorldList.cpp index 12b8529cb..280f84215 100644 --- a/launcher/minecraft/MultiWorldList.cpp +++ b/launcher/minecraft/MultiWorldList.cpp @@ -77,7 +77,7 @@ void MultiWorldList::startWatching() m_isWatching = true; - for (QDir dir : m_dirs) { + for (const QDir& dir : m_dirs) { if (m_watcher->addPath(dir.absolutePath())) { qDebug() << "Started watching" << dir.absolutePath(); } else { @@ -157,7 +157,7 @@ QList MultiWorldList::instDirPaths() const { QList dirList; - for (BaseInstance* instance : allInstances) { + for (BaseInstance const* instance : allInstances) { dirList.append(QFileInfo(instance->instanceRoot()).absoluteFilePath()); } @@ -223,7 +223,7 @@ QVariant MultiWorldList::data(const QModelIndex& index, int role) const QLocale locale; - auto& instanceWorld = m_worlds[row]; + const auto& instanceWorld = m_worlds[row]; switch (role) { case Qt::DisplayRole: switch (column) { diff --git a/launcher/minecraft/MultiWorldList.h b/launcher/minecraft/MultiWorldList.h index be9103ba3..dc0829349 100644 --- a/launcher/minecraft/MultiWorldList.h +++ b/launcher/minecraft/MultiWorldList.h @@ -73,7 +73,7 @@ class MultiWorldList : public QAbstractListModel { /// process data from drop action virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent); /// what drag actions do we support? - int64_t calculateWorldSize(const QFileInfo& file); + static int64_t calculateWorldSize(const QFileInfo& file); virtual Qt::DropActions supportedDragActions() const; /// what drop actions do we support? diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 5d4c00f38..b80b197f2 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -338,19 +338,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi connect(this, &MainWindow::selectInstance, view, &InstanceView::selectInstance); } - // Create the all worlds widget + + // All worlds toggle { - QList allInstances = APPLICATION->instances()->getAllInstances(); - - allWorlds = new MultiWorldList(allInstances); - allWorlds->update(); - allWorldsPage = new MultiWorldListPage(allWorlds); - - ui->horizontalLayout->addWidget(allWorldsPage); - - allWorldsPage->setVisible(false); - ui->instanceToolBar->setVisibilityState(QByteArray::fromBase64(instanceToolbarSetting->get().toString().toUtf8())); - connect(ui->actionAllWorlds, &QAction::toggled, this, &MainWindow::onAllWorldsToggled); connect(allWorldsPage, &MultiWorldListPage::worldJoined, this, &MainWindow::worldJoined); } @@ -878,14 +868,13 @@ void MainWindow::worldJoined(BaseInstance* instance) void MainWindow::toggleAllWorldsScreen(bool toggled) { if (toggled) { - QList allInstances = APPLICATION->instances()->getAllInstances(); + QList const allInstances = APPLICATION->instances()->getAllInstances(); - allWorlds = new MultiWorldList(allInstances); //make this be unique pointer or whatever instead of awkward replace and delete iy - allWorlds->update(); - auto newAllWorldsPage = new MultiWorldListPage(allWorlds); - ui->horizontalLayout->replaceWidget(allWorldsPage, newAllWorldsPage); - delete allWorldsPage; - allWorldsPage = newAllWorldsPage; + allWorldsList = new MultiWorldList(allInstances); + allWorldsList->update(); + + allWorldsPage = new MultiWorldListPage(allWorldsList); + ui->horizontalLayout->addWidget(allWorldsPage); view->setVisible(false); m_oldInstanceToolbarSetting = ui->instanceToolBar->isVisible(); @@ -893,16 +882,27 @@ void MainWindow::toggleAllWorldsScreen(bool toggled) allWorldsPage->setVisible(true); statusBar()->setVisible(false); - allWorlds->startWatching(); + allWorldsList->startWatching(); connect(allWorldsPage, &MultiWorldListPage::worldJoined, this, &MainWindow::worldJoined); } else { - allWorldsPage->setVisible(false); - view->setVisible(true); - ui->instanceToolBar->setVisible(m_oldInstanceToolbarSetting); - statusBar()->setVisible(APPLICATION->settings()->get("StatusBarVisible").toBool()); + if (allWorldsList == nullptr || allWorldsPage == nullptr) { + view->setVisible(true); + ui->instanceToolBar->setVisible(m_oldInstanceToolbarSetting); + statusBar()->setVisible(APPLICATION->settings()->get("StatusBarVisible").toBool()); + } else { + allWorldsPage->setVisible(false); + view->setVisible(true); + ui->instanceToolBar->setVisible(m_oldInstanceToolbarSetting); + statusBar()->setVisible(APPLICATION->settings()->get("StatusBarVisible").toBool()); - allWorlds->stopWatching(); + allWorldsList->stopWatching(); + + delete allWorldsList; + allWorldsList = nullptr; + delete allWorldsPage; + allWorldsPage = nullptr; + } } } diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 493d9c8e2..601a2e5f0 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -255,7 +255,7 @@ class MainWindow : public QMainWindow { LabeledToolButton* renameButton = nullptr; QToolButton* helpMenuButton = nullptr; KonamiCode* secretEventFilter = nullptr; - MultiWorldList* allWorlds = nullptr; + MultiWorldList* allWorldsList = nullptr; std::shared_ptr instanceToolbarSetting = nullptr; bool m_oldInstanceToolbarSetting; diff --git a/launcher/ui/MultiWorldListPage.cpp b/launcher/ui/MultiWorldListPage.cpp index 7a08d046c..4e597fb60 100644 --- a/launcher/ui/MultiWorldListPage.cpp +++ b/launcher/ui/MultiWorldListPage.cpp @@ -466,7 +466,7 @@ void MultiWorldListPage::on_actionCopy_triggered() // TODO: Make this a separate dialog class Q_DECLARE_METATYPE(BaseInstance*); -MinecraftInstance* MultiWorldListPage::selectInstance(const QString& message, BaseInstance* preselectedInstance) +MinecraftInstance* MultiWorldListPage::selectInstance(const QString& message, const BaseInstance* preselectedInstance) { auto *dialog = new QDialog(this); dialog->setWindowTitle(tr("Select Instance")); @@ -475,13 +475,13 @@ MinecraftInstance* MultiWorldListPage::selectInstance(const QString& message, Ba static_cast(std::max(0.75 * window()->height(), 400.0))); dialog->restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get("SelectInstanceGeometry").toByteArray())); - auto layout = new QVBoxLayout(dialog); + auto *layout = new QVBoxLayout(dialog); layout->addWidget(new QLabel(message)); - auto instanceList = new QListWidget(dialog); + auto *instanceList = new QListWidget(dialog); - for (auto instance : m_worlds->getInstances()) { + for (auto *instance : m_worlds->getInstances()) { auto *item = new QListWidgetItem(instanceList); item->setText(instance->name()); item->setIcon(APPLICATION->icons()->getIcon(instance->iconKey())); @@ -495,7 +495,7 @@ MinecraftInstance* MultiWorldListPage::selectInstance(const QString& message, Ba layout->addWidget(instanceList); - auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject); connect(buttonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept); layout->addWidget(buttonBox); @@ -559,7 +559,7 @@ void MultiWorldListPage::on_actionRefresh_triggered() void MultiWorldListPage::worldDoubleClicked(const QModelIndex& index) { - auto proxy = (QSortFilterProxyModel*)ui->worldTreeView->model(); + auto *proxy = static_cast(ui->worldTreeView->model()); join(proxy->mapToSource(index), LaunchMode::Normal); } diff --git a/launcher/ui/MultiWorldListPage.h b/launcher/ui/MultiWorldListPage.h index b2ef3e080..4e3e4b466 100644 --- a/launcher/ui/MultiWorldListPage.h +++ b/launcher/ui/MultiWorldListPage.h @@ -79,7 +79,7 @@ class MultiWorldListPage : public QMainWindow, public BasePage { bool worldSafetyNagQuestion(const QString& actionType); void mceditError(); void join(const QModelIndex& index, LaunchMode launchMode); - MinecraftInstance* selectInstance(const QString& message, BaseInstance* instance = nullptr); + MinecraftInstance* selectInstance(const QString& message, const BaseInstance* preselectedInstance = nullptr); private: Ui::MultiWorldListPage* ui; diff --git a/launcher/ui/instanceview/InstanceView.cpp b/launcher/ui/instanceview/InstanceView.cpp index 1619443e0..90fe7606f 100644 --- a/launcher/ui/instanceview/InstanceView.cpp +++ b/launcher/ui/instanceview/InstanceView.cpp @@ -207,13 +207,13 @@ void InstanceView::updateGeometries() viewport()->update(); } -void InstanceView::selectInstance(BaseInstance* instance) +void InstanceView::selectInstance(const BaseInstance* instance) const { QModelIndex index; for (int row = 0; row < model()->rowCount(); row++) { - for (int j = 0; j < model()->columnCount(); j++) { - auto testIndex = model()->index(row, j); + for (int col = 0; col < model()->columnCount(); col++) { + auto testIndex = model()->index(row, col); if (testIndex.data(InstanceList::InstanceIDRole).toString() == instance->id()) { index = testIndex; } diff --git a/launcher/ui/instanceview/InstanceView.h b/launcher/ui/instanceview/InstanceView.h index db5d01dd8..3fca0c4fa 100644 --- a/launcher/ui/instanceview/InstanceView.h +++ b/launcher/ui/instanceview/InstanceView.h @@ -84,7 +84,7 @@ class InstanceView : public QAbstractItemView { public slots: virtual void updateGeometries() override; - void selectInstance(BaseInstance* instance); + void selectInstance(const BaseInstance* instance) const; protected slots: virtual void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QList& roles) override;