Merge branch 'launcher_code_cleanup' into 'master'

Cleanup launcher code

See merge request OpenMW/openmw!2848
depth-refraction
psi29a 2 years ago
commit 4b5de083d8

@ -98,6 +98,27 @@ namespace Launcher
{
return Settings::Manager::getUInt64("max navmeshdb file size", "Navigator") / (1024 * 1024);
}
std::optional<QString> findFirstPath(const QStringList& directories, const QString& fileName)
{
for (const QString& directoryPath : directories)
{
const QString filePath = QDir(directoryPath).absoluteFilePath(fileName);
if (QFile::exists(filePath))
return filePath;
}
return std::nullopt;
}
QStringList findAllFilePaths(const QStringList& directories, const QStringList& fileNames)
{
QStringList result;
result.reserve(fileNames.size());
for (const QString& fileName : fileNames)
if (const auto filepath = findFirstPath(directories, fileName))
result.append(*filepath);
return result;
}
}
}
@ -305,25 +326,8 @@ void Launcher::DataFilesPage::populateFileViews(const QString& contentModelName)
row++;
}
PathIterator pathIterator(directories);
mSelector->setProfileContent(filesInProfile(contentModelName, pathIterator));
}
QStringList Launcher::DataFilesPage::filesInProfile(const QString& profileName, PathIterator& pathIterator)
{
QStringList files = mLauncherSettings.getContentListFiles(profileName);
QStringList filepaths;
for (const QString& file : files)
{
QString filepath = pathIterator.findFirstPath(file);
if (!filepath.isEmpty())
filepaths << filepath;
}
return filepaths;
mSelector->setProfileContent(
findAllFilePaths(directories, mLauncherSettings.getContentListFiles(contentModelName)));
}
void Launcher::DataFilesPage::saveSettings(const QString& profile)
@ -377,21 +381,20 @@ QStringList Launcher::DataFilesPage::selectedDirectoriesPaths() const
QStringList dirList;
for (int i = 0; i < ui.directoryListWidget->count(); ++i)
{
if (ui.directoryListWidget->item(i)->flags() & Qt::ItemIsEnabled)
dirList.append(ui.directoryListWidget->item(i)->text());
const QListWidgetItem* item = ui.directoryListWidget->item(i);
if (item->flags() & Qt::ItemIsEnabled)
dirList.append(item->text());
}
return dirList;
}
QStringList Launcher::DataFilesPage::selectedArchivePaths(bool all) const
QStringList Launcher::DataFilesPage::selectedArchivePaths() const
{
QStringList archiveList;
for (int i = 0; i < ui.archiveListWidget->count(); ++i)
{
const auto* item = ui.archiveListWidget->item(i);
const auto archive = ui.archiveListWidget->item(i)->text();
if (all || item->checkState() == Qt::Checked)
const QListWidgetItem* item = ui.archiveListWidget->item(i);
if (item->checkState() == Qt::Checked)
archiveList.append(item->text());
}
return archiveList;
@ -403,11 +406,8 @@ QStringList Launcher::DataFilesPage::selectedFilePaths() const
ContentSelectorModel::ContentFileList items = mSelector->selectedFiles();
QStringList filePaths;
for (const ContentSelectorModel::EsmFile* item : items)
{
QFile file(item->filePath());
if (file.exists())
if (QFile::exists(item->filePath()))
filePaths.append(item->filePath());
}
return filePaths;
}
@ -724,6 +724,10 @@ void Launcher::DataFilesPage::addArchivesFromDir(const QString& path)
{
QDir dir(path, "*.bsa");
std::unordered_set<QString> archives;
for (int i = 0; i < ui.archiveListWidget->count(); ++i)
archives.insert(ui.archiveListWidget->item(i)->text());
for (const auto& fileinfo : dir.entryInfoList())
{
const auto absPath = fileinfo.absoluteFilePath();
@ -731,9 +735,8 @@ void Launcher::DataFilesPage::addArchivesFromDir(const QString& path)
continue;
const auto fileName = fileinfo.fileName();
const auto currentList = selectedArchivePaths(true);
if (!currentList.contains(fileName, Qt::CaseInsensitive))
if (archives.insert(fileName).second)
addArchive(fileName, Qt::Unchecked);
}
}

@ -138,60 +138,8 @@ namespace Launcher
* @return the file paths of all selected content files
*/
QStringList selectedFilePaths() const;
QStringList selectedArchivePaths(bool all = false) const;
QStringList selectedArchivePaths() const;
QStringList selectedDirectoriesPaths() const;
class PathIterator
{
QStringList::ConstIterator mCitEnd;
QStringList::ConstIterator mCitCurrent;
QStringList::ConstIterator mCitBegin;
QString mFile;
QString mFilePath;
public:
PathIterator(const QStringList& list)
{
mCitBegin = list.constBegin();
mCitCurrent = mCitBegin;
mCitEnd = list.constEnd();
}
QString findFirstPath(const QString& file)
{
mCitCurrent = mCitBegin;
mFile = file;
return path();
}
QString findNextPath() { return path(); }
private:
QString path()
{
bool success = false;
QDir dir;
QFileInfo file;
while (!success)
{
if (mCitCurrent == mCitEnd)
break;
dir.setPath(*(mCitCurrent++));
file.setFile(dir.absoluteFilePath(mFile));
success = file.exists();
}
if (success)
return file.absoluteFilePath();
return "";
}
};
QStringList filesInProfile(const QString& profileName, PathIterator& pathIterator);
};
}
#endif

@ -1,37 +1,45 @@
#include "cellnameloader.hpp"
#include <components/debug/debuglog.hpp>
#include <components/esm3/loadcell.hpp>
#include <components/files/qtconversion.hpp>
QSet<QString> CellNameLoader::getCellNames(QStringList& contentPaths)
QSet<QString> CellNameLoader::getCellNames(const QStringList& contentPaths)
{
QSet<QString> cellNames;
ESM::ESMReader esmReader;
// Loop through all content files
for (auto& contentPath : contentPaths)
for (const QString& contentPath : contentPaths)
{
if (contentPath.endsWith(".omwscripts", Qt::CaseInsensitive))
continue;
esmReader.open(Files::pathFromQString(contentPath));
// Loop through all records
while (esmReader.hasMoreRecs())
try
{
ESM::NAME recordName = esmReader.getRecName();
esmReader.getRecHeader();
esmReader.open(Files::pathFromQString(contentPath));
if (isCellRecord(recordName))
// Loop through all records
while (esmReader.hasMoreRecs())
{
QString cellName = getCellName(esmReader);
if (!cellName.isEmpty())
ESM::NAME recordName = esmReader.getRecName();
esmReader.getRecHeader();
if (isCellRecord(recordName))
{
cellNames.insert(cellName);
QString cellName = getCellName(esmReader);
if (!cellName.isEmpty())
{
cellNames.insert(cellName);
}
}
}
// Stop loading content for this record and continue to the next
esmReader.skipRecord();
// Stop loading content for this record and continue to the next
esmReader.skipRecord();
}
}
catch (const std::exception& e)
{
Log(Debug::Error) << "Failed to get cell names from " << contentPath.toStdString() << ": " << e.what();
}
}

@ -25,7 +25,7 @@ public:
* @param contentPaths the file paths of each content file to be examined
* @return the names of all cells
*/
QSet<QString> getCellNames(QStringList& contentPaths);
QSet<QString> getCellNames(const QStringList& contentPaths);
private:
/**

@ -50,9 +50,8 @@ bool Wizard::ExistingInstallationPage::validatePage()
// Or failed to be detected due to the target being a symlink
QString path(field(QLatin1String("installation.path")).toString());
QFile file(mWizard->mInstallations[path].iniPath);
if (!file.exists())
if (!QFile::exists(mWizard->mInstallations[path].iniPath))
{
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Error detecting Morrowind configuration"));

@ -580,10 +580,10 @@ void ContentSelectorModel::ContentModel::sortFiles()
bool ContentSelectorModel::ContentModel::isChecked(const QString& filepath) const
{
if (mCheckStates.contains(filepath))
return (mCheckStates[filepath] == Qt::Checked);
return false;
const auto it = mCheckStates.find(filepath);
if (it == mCheckStates.end())
return false;
return it.value() == Qt::Checked;
}
bool ContentSelectorModel::ContentModel::isEnabled(const QModelIndex& index) const
@ -593,10 +593,10 @@ bool ContentSelectorModel::ContentModel::isEnabled(const QModelIndex& index) con
bool ContentSelectorModel::ContentModel::isNew(const QString& filepath) const
{
if (mNewFiles.contains(filepath))
return mNewFiles[filepath];
return false;
const auto it = mNewFiles.find(filepath);
if (it == mNewFiles.end())
return false;
return it.value();
}
void ContentSelectorModel::ContentModel::setNew(const QString& filepath, bool isNew)

@ -45,19 +45,19 @@ namespace ContentSelectorModel
void setGameFiles(const QStringList& gameFiles);
void setDescription(const QString& description);
inline void addGameFile(const QString& name) { mGameFiles.append(name); }
void addGameFile(const QString& name) { mGameFiles.append(name); }
QVariant fileProperty(const FileProperty prop) const;
inline QString fileName() const { return mFileName; }
inline QString author() const { return mAuthor; }
inline QDateTime modified() const { return mModified; }
QString fileName() const { return mFileName; }
QString author() const { return mAuthor; }
QDateTime modified() const { return mModified; }
ESM::FormatVersion formatVersion() const { return mVersion; }
inline QString filePath() const { return mPath; }
QString filePath() const { return mPath; }
/// @note Contains file names, not paths.
inline const QStringList& gameFiles() const { return mGameFiles; }
inline QString description() const { return mDescription; }
inline QString toolTip() const
const QStringList& gameFiles() const { return mGameFiles; }
QString description() const { return mDescription; }
QString toolTip() const
{
return sToolTip.arg(mAuthor)
.arg(mVersion)

@ -28,7 +28,7 @@
<widget class="QWidget" name="contentSelectorWidget" native="true"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<widget class="QLabel" name="dataNoteLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;note: content files that are not part of current Content List are &lt;/span&gt;&lt;span style=&quot; font-style:italic; background-color:#00ff00;&quot;&gt;highlighted&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
@ -49,7 +49,7 @@
</widget>
</item>
<item row="32" column="0" colspan="2">
<widget class="QLabel" name="label">
<widget class="QLabel" name="directoryNoteLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch>
@ -208,7 +208,7 @@
</widget>
</item>
<item row="27" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="archiveNoteLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-style:italic;&quot;&gt;note: archives that are not part of current Content List are &lt;/span&gt;&lt;span style=&quot; font-style:italic; background-color:#00ff00;&quot;&gt;highlighted&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
@ -238,13 +238,13 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<widget class="QWidget" name="navigationMeshCacheTab">
<attribute name="title">
<string>Navigation Mesh Cache</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QHBoxLayout" name="navigationMeshCacheProgressLayout">
<item>
<widget class="QPushButton" name="updateNavMeshButton">
<property name="focusPolicy">
@ -294,9 +294,9 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout" name="navigationMeshCacheParamsLayout">
<item>
<widget class="QLabel" name="label_4">
<widget class="QLabel" name="navMeshMaxSizeLabel">
<property name="text">
<string>Max size</string>
</property>

Loading…
Cancel
Save