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); 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++; row++;
} }
PathIterator pathIterator(directories); mSelector->setProfileContent(
findAllFilePaths(directories, mLauncherSettings.getContentListFiles(contentModelName)));
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;
} }
void Launcher::DataFilesPage::saveSettings(const QString& profile) void Launcher::DataFilesPage::saveSettings(const QString& profile)
@ -377,21 +381,20 @@ QStringList Launcher::DataFilesPage::selectedDirectoriesPaths() const
QStringList dirList; QStringList dirList;
for (int i = 0; i < ui.directoryListWidget->count(); ++i) for (int i = 0; i < ui.directoryListWidget->count(); ++i)
{ {
if (ui.directoryListWidget->item(i)->flags() & Qt::ItemIsEnabled) const QListWidgetItem* item = ui.directoryListWidget->item(i);
dirList.append(ui.directoryListWidget->item(i)->text()); if (item->flags() & Qt::ItemIsEnabled)
dirList.append(item->text());
} }
return dirList; return dirList;
} }
QStringList Launcher::DataFilesPage::selectedArchivePaths(bool all) const QStringList Launcher::DataFilesPage::selectedArchivePaths() const
{ {
QStringList archiveList; QStringList archiveList;
for (int i = 0; i < ui.archiveListWidget->count(); ++i) for (int i = 0; i < ui.archiveListWidget->count(); ++i)
{ {
const auto* item = ui.archiveListWidget->item(i); const QListWidgetItem* item = ui.archiveListWidget->item(i);
const auto archive = ui.archiveListWidget->item(i)->text(); if (item->checkState() == Qt::Checked)
if (all || item->checkState() == Qt::Checked)
archiveList.append(item->text()); archiveList.append(item->text());
} }
return archiveList; return archiveList;
@ -403,11 +406,8 @@ QStringList Launcher::DataFilesPage::selectedFilePaths() const
ContentSelectorModel::ContentFileList items = mSelector->selectedFiles(); ContentSelectorModel::ContentFileList items = mSelector->selectedFiles();
QStringList filePaths; QStringList filePaths;
for (const ContentSelectorModel::EsmFile* item : items) for (const ContentSelectorModel::EsmFile* item : items)
{ if (QFile::exists(item->filePath()))
QFile file(item->filePath());
if (file.exists())
filePaths.append(item->filePath()); filePaths.append(item->filePath());
}
return filePaths; return filePaths;
} }
@ -724,6 +724,10 @@ void Launcher::DataFilesPage::addArchivesFromDir(const QString& path)
{ {
QDir dir(path, "*.bsa"); 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()) for (const auto& fileinfo : dir.entryInfoList())
{ {
const auto absPath = fileinfo.absoluteFilePath(); const auto absPath = fileinfo.absoluteFilePath();
@ -731,9 +735,8 @@ void Launcher::DataFilesPage::addArchivesFromDir(const QString& path)
continue; continue;
const auto fileName = fileinfo.fileName(); 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); addArchive(fileName, Qt::Unchecked);
} }
} }

@ -138,60 +138,8 @@ namespace Launcher
* @return the file paths of all selected content files * @return the file paths of all selected content files
*/ */
QStringList selectedFilePaths() const; QStringList selectedFilePaths() const;
QStringList selectedArchivePaths(bool all = false) const; QStringList selectedArchivePaths() const;
QStringList selectedDirectoriesPaths() 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 #endif

@ -1,37 +1,45 @@
#include "cellnameloader.hpp" #include "cellnameloader.hpp"
#include <components/debug/debuglog.hpp>
#include <components/esm3/loadcell.hpp> #include <components/esm3/loadcell.hpp>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
QSet<QString> CellNameLoader::getCellNames(QStringList& contentPaths) QSet<QString> CellNameLoader::getCellNames(const QStringList& contentPaths)
{ {
QSet<QString> cellNames; QSet<QString> cellNames;
ESM::ESMReader esmReader; ESM::ESMReader esmReader;
// Loop through all content files // Loop through all content files
for (auto& contentPath : contentPaths) for (const QString& contentPath : contentPaths)
{ {
if (contentPath.endsWith(".omwscripts", Qt::CaseInsensitive)) if (contentPath.endsWith(".omwscripts", Qt::CaseInsensitive))
continue; continue;
esmReader.open(Files::pathFromQString(contentPath)); try
// Loop through all records
while (esmReader.hasMoreRecs())
{ {
ESM::NAME recordName = esmReader.getRecName(); esmReader.open(Files::pathFromQString(contentPath));
esmReader.getRecHeader();
if (isCellRecord(recordName)) // Loop through all records
while (esmReader.hasMoreRecs())
{ {
QString cellName = getCellName(esmReader); ESM::NAME recordName = esmReader.getRecName();
if (!cellName.isEmpty()) 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 // Stop loading content for this record and continue to the next
esmReader.skipRecord(); 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 * @param contentPaths the file paths of each content file to be examined
* @return the names of all cells * @return the names of all cells
*/ */
QSet<QString> getCellNames(QStringList& contentPaths); QSet<QString> getCellNames(const QStringList& contentPaths);
private: private:
/** /**

@ -50,9 +50,8 @@ bool Wizard::ExistingInstallationPage::validatePage()
// Or failed to be detected due to the target being a symlink // Or failed to be detected due to the target being a symlink
QString path(field(QLatin1String("installation.path")).toString()); 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; QMessageBox msgBox;
msgBox.setWindowTitle(tr("Error detecting Morrowind configuration")); msgBox.setWindowTitle(tr("Error detecting Morrowind configuration"));

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

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

@ -28,7 +28,7 @@
<widget class="QWidget" name="contentSelectorWidget" native="true"/> <widget class="QWidget" name="contentSelectorWidget" native="true"/>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="dataNoteLabel">
<property name="text"> <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> <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> </property>
@ -49,7 +49,7 @@
</widget> </widget>
</item> </item>
<item row="32" column="0" colspan="2"> <item row="32" column="0" colspan="2">
<widget class="QLabel" name="label"> <widget class="QLabel" name="directoryNoteLabel">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum"> <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -208,7 +208,7 @@
</widget> </widget>
</item> </item>
<item row="27" column="0" colspan="2"> <item row="27" column="0" colspan="2">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="archiveNoteLabel">
<property name="text"> <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> <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> </property>
@ -238,13 +238,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_2"> <widget class="QWidget" name="navigationMeshCacheTab">
<attribute name="title"> <attribute name="title">
<string>Navigation Mesh Cache</string> <string>Navigation Mesh Cache</string>
</attribute> </attribute>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="navigationMeshCacheProgressLayout">
<item> <item>
<widget class="QPushButton" name="updateNavMeshButton"> <widget class="QPushButton" name="updateNavMeshButton">
<property name="focusPolicy"> <property name="focusPolicy">
@ -294,9 +294,9 @@
</widget> </widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="navigationMeshCacheParamsLayout">
<item> <item>
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="navMeshMaxSizeLabel">
<property name="text"> <property name="text">
<string>Max size</string> <string>Max size</string>
</property> </property>

Loading…
Cancel
Save