1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-31 22:45:33 +00:00

Merge plugins and masters list view in openDialog. Simplify datafilesmodel.

This commit is contained in:
Michal Sciubidlo 2013-02-10 14:02:06 +01:00
parent 347a734364
commit e4ed397b2d
4 changed files with 28 additions and 167 deletions

View file

@ -48,8 +48,7 @@ DataFilesList::DataFilesList(Files::ConfigurationManager &cfg, QWidget *parent)
: QWidget(parent) : QWidget(parent)
, mCfgMgr(cfg) , mCfgMgr(cfg)
{ {
// Models // Model
mMastersModel = new DataFilesModel(this);
mPluginsModel = new DataFilesModel(this); mPluginsModel = new DataFilesModel(this);
mPluginsProxyModel = new QSortFilterProxyModel(); mPluginsProxyModel = new QSortFilterProxyModel();
@ -78,29 +77,6 @@ DataFilesList::DataFilesList(Files::ConfigurationManager &cfg, QWidget *parent)
QCheckBox checkBox; QCheckBox checkBox;
unsigned int height = checkBox.sizeHint().height() + 4; unsigned int height = checkBox.sizeHint().height() + 4;
mMastersTable = new QTableView(this);
mMastersTable->setModel(mMastersModel);
mMastersTable->setObjectName("MastersTable");
mMastersTable->setSelectionBehavior(QAbstractItemView::SelectRows);
mMastersTable->setSelectionMode(QAbstractItemView::SingleSelection);
mMastersTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
mMastersTable->setAlternatingRowColors(true);
mMastersTable->horizontalHeader()->setStretchLastSection(true);
mMastersTable->horizontalHeader()->hide();
// Set the row height to the size of the checkboxes
mMastersTable->verticalHeader()->setDefaultSectionSize(height);
mMastersTable->verticalHeader()->setResizeMode(QHeaderView::Fixed);
mMastersTable->verticalHeader()->hide();
mMastersTable->setColumnHidden(1, true);
mMastersTable->setColumnHidden(2, true);
mMastersTable->setColumnHidden(3, true);
mMastersTable->setColumnHidden(4, true);
mMastersTable->setColumnHidden(5, true);
mMastersTable->setColumnHidden(6, true);
mMastersTable->setColumnHidden(7, true);
mMastersTable->setColumnHidden(8, true);
mPluginsTable = new QTableView(this); mPluginsTable = new QTableView(this);
mPluginsTable->setModel(mPluginsProxyModel); mPluginsTable->setModel(mPluginsProxyModel);
mPluginsTable->setObjectName("PluginsTable"); mPluginsTable->setObjectName("PluginsTable");
@ -124,26 +100,14 @@ DataFilesList::DataFilesList(Files::ConfigurationManager &cfg, QWidget *parent)
mPluginsTable->setColumnHidden(7, true); mPluginsTable->setColumnHidden(7, true);
mPluginsTable->setColumnHidden(8, true); mPluginsTable->setColumnHidden(8, true);
// Add both tables to a splitter
QSplitter *splitter = new QSplitter(this);
splitter->setOrientation(Qt::Horizontal);
splitter->addWidget(mMastersTable);
splitter->addWidget(mPluginsTable);
// Adjust the default widget widths inside the splitter
QList<int> sizeList;
sizeList << 175 << 200;
splitter->setSizes(sizeList);
QVBoxLayout *pageLayout = new QVBoxLayout(this); QVBoxLayout *pageLayout = new QVBoxLayout(this);
pageLayout->addWidget(filterToolBar); pageLayout->addWidget(filterToolBar);
pageLayout->addWidget(splitter); pageLayout->addWidget(mPluginsTable);
connect(mPluginsTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex))); connect(mPluginsTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex)));
connect(mMastersTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex)));
connect(mPluginsModel, SIGNAL(checkedItemsChanged(QStringList,QStringList)), mPluginsModel, SLOT(slotcheckedItemsChanged(QStringList,QStringList)));
connect(mMastersModel, SIGNAL(checkedItemsChanged(QStringList,QStringList)), mPluginsModel, SLOT(slotcheckedItemsChanged(QStringList,QStringList)));
connect(filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged(QString))); connect(filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged(QString)));
@ -178,7 +142,6 @@ bool DataFilesList::setupDataFiles(Files::PathContainer dataDirs, const QString
{ {
// Set the charset for reading the esm/esp files // Set the charset for reading the esm/esp files
if (!encoding.isEmpty() && encoding != QLatin1String("win1252")) { if (!encoding.isEmpty() && encoding != QLatin1String("win1252")) {
mMastersModel->setEncoding(encoding);
mPluginsModel->setEncoding(encoding); mPluginsModel->setEncoding(encoding);
} }
@ -186,11 +149,9 @@ bool DataFilesList::setupDataFiles(Files::PathContainer dataDirs, const QString
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) { for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) {
QString path = QString::fromStdString(it->string()); QString path = QString::fromStdString(it->string());
path.remove(QChar('\"')); path.remove(QChar('\"'));
mMastersModel->addMasters(path); mPluginsModel->addFiles(path);
mPluginsModel->addPlugins(path);
} }
mMastersModel->sort(0);
mPluginsModel->sort(0); mPluginsModel->sort(0);
// mMastersTable->sortByColumn(3, Qt::AscendingOrder); // mMastersTable->sortByColumn(3, Qt::AscendingOrder);
// mPluginsTable->sortByColumn(3, Qt::AscendingOrder); // mPluginsTable->sortByColumn(3, Qt::AscendingOrder);
@ -200,11 +161,6 @@ bool DataFilesList::setupDataFiles(Files::PathContainer dataDirs, const QString
void DataFilesList::selectedFiles(std::vector<boost::filesystem::path>& paths) void DataFilesList::selectedFiles(std::vector<boost::filesystem::path>& paths)
{ {
QStringList masterPaths = mMastersModel->checkedItemsPaths();
foreach (const QString &path, masterPaths)
{
paths.push_back(path.toStdString());
}
QStringList pluginPaths = mPluginsModel->checkedItemsPaths(); QStringList pluginPaths = mPluginsModel->checkedItemsPaths();
foreach (const QString &path, pluginPaths) foreach (const QString &path, pluginPaths)
{ {
@ -281,19 +237,12 @@ void DataFilesList::setCheckState(QModelIndex index)
: mPluginsModel->setCheckState(sourceIndex, Qt::Checked); : mPluginsModel->setCheckState(sourceIndex, Qt::Checked);
} }
if (object->objectName() == QLatin1String("MastersTable")) {
(mMastersModel->checkState(index) == Qt::Checked)
? mMastersModel->setCheckState(index, Qt::Unchecked)
: mMastersModel->setCheckState(index, Qt::Checked);
}
return; return;
} }
void DataFilesList::uncheckAll() void DataFilesList::uncheckAll()
{ {
mMastersModel->uncheckAll();
mPluginsModel->uncheckAll(); mPluginsModel->uncheckAll();
} }
@ -338,14 +287,9 @@ void DataFilesList::setCheckState(const QString& element, Qt::CheckState state)
{ {
mPluginsModel->setCheckState(mPluginsModel->indexFromItem(file), Qt::Checked); mPluginsModel->setCheckState(mPluginsModel->indexFromItem(file), Qt::Checked);
} }
else
{
file = mMastersModel->findItem(element);
mMastersModel->setCheckState(mMastersModel->indexFromItem(file), Qt::Checked);
}
} }
QStringList DataFilesList::checkedFiles() QStringList DataFilesList::checkedFiles()
{ {
return mMastersModel->checkedItems() + mPluginsModel->checkedItems(); return mPluginsModel->checkedItems();
} }

View file

@ -49,12 +49,10 @@ public slots:
void refresh(); void refresh();
private: private:
DataFilesModel *mMastersModel;
DataFilesModel *mPluginsModel; DataFilesModel *mPluginsModel;
QSortFilterProxyModel *mPluginsProxyModel; QSortFilterProxyModel *mPluginsProxyModel;
QTableView *mMastersTable;
QTableView *mPluginsTable; QTableView *mPluginsTable;
QMenu *mContextMenu; QMenu *mContextMenu;

View file

@ -159,7 +159,7 @@ Qt::ItemFlags DataFilesModel::flags(const QModelIndex &index) const
if (!file) if (!file)
return Qt::NoItemFlags; return Qt::NoItemFlags;
if (mAvailableFiles.contains(file->fileName())) { if (canBeChecked(file)) {
if (index.column() == 0) { if (index.column() == 0) {
return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
} else { } else {
@ -212,7 +212,6 @@ bool DataFilesModel::setData(const QModelIndex &index, const QVariant &value, in
QString name = item(index.row())->fileName(); QString name = item(index.row())->fileName();
mCheckStates[name] = static_cast<Qt::CheckState>(value.toInt()); mCheckStates[name] = static_cast<Qt::CheckState>(value.toInt());
emit checkedItemsChanged(checkedItems(), uncheckedItems());
emit layoutChanged(); emit layoutChanged();
return true; return true;
} }
@ -263,73 +262,12 @@ void DataFilesModel::addFile(EsmFile *file)
emit endInsertRows(); emit endInsertRows();
} }
void DataFilesModel::addMasters(const QString &path) void DataFilesModel::addFiles(const QString &path)
{ {
QDir dir(path); QDir dir(path);
dir.setNameFilters(QStringList(QLatin1String("*.esp"))); QStringList filters;
filters << "*.esp" << "*.esm";
// Read the dependencies from the plugins dir.setNameFilters(filters);
foreach (const QString &path, dir.entryList()) {
try {
ESM::ESMReader fileReader;
ToUTF8::Utf8Encoder encoder (ToUTF8::calculateEncoding(mEncoding.toStdString()));
fileReader.setEncoder(&encoder);
fileReader.open(dir.absoluteFilePath(path).toStdString());
ESM::ESMReader::MasterList mlist = fileReader.getMasters();
for (unsigned int i = 0; i < mlist.size(); ++i) {
QString master = QString::fromStdString(mlist[i].name);
// Add the plugin to the internal dependency map
mDependencies[master].append(path);
// Don't add esps
if (master.endsWith(".esp", Qt::CaseInsensitive))
continue;
QFileInfo info(dir.absoluteFilePath(master));
EsmFile *file = new EsmFile(master);
file->setDates(info.lastModified(), info.lastRead());
file->setPath(info.absoluteFilePath());
// Add the master to the table
if (findItem(master) == 0)
addFile(file);
}
} catch(std::runtime_error &e) {
// An error occurred while reading the .esp
qWarning() << "Error reading esp: " << e.what();
continue;
}
}
// See if the masters actually exist in the filesystem
dir.setNameFilters(QStringList(QLatin1String("*.esm")));
foreach (const QString &path, dir.entryList()) {
QFileInfo info(dir.absoluteFilePath(path));
if (findItem(path) == 0) {
EsmFile *file = new EsmFile(path);
file->setDates(info.lastModified(), info.lastRead());
addFile(file);
}
// Make the master selectable
mAvailableFiles.append(path);
}
}
void DataFilesModel::addPlugins(const QString &path)
{
QDir dir(path);
dir.setNameFilters(QStringList(QLatin1String("*.esp")));
foreach (const QString &path, dir.entryList()) { foreach (const QString &path, dir.entryList()) {
QFileInfo info(dir.absoluteFilePath(path)); QFileInfo info(dir.absoluteFilePath(path));
@ -347,9 +285,6 @@ void DataFilesModel::addPlugins(const QString &path)
for (unsigned int i = 0; i < mlist.size(); ++i) { for (unsigned int i = 0; i < mlist.size(); ++i) {
QString master = QString::fromStdString(mlist[i].name); QString master = QString::fromStdString(mlist[i].name);
masters.append(master); masters.append(master);
// Add the plugin to the internal dependency map
mDependencies[master].append(path);
} }
file->setAuthor(QString::fromStdString(fileReader.getAuthor())); file->setAuthor(QString::fromStdString(fileReader.getAuthor()));
@ -421,7 +356,7 @@ QStringList DataFilesModel::checkedItems()
QString name = file->fileName(); QString name = file->fileName();
// Only add the items that are in the checked list and available // Only add the items that are in the checked list and available
if (mCheckStates[name] == Qt::Checked && mAvailableFiles.contains(name)) if (mCheckStates[name] == Qt::Checked && canBeChecked(file))
list << name; list << name;
} }
@ -439,8 +374,8 @@ QStringList DataFilesModel::checkedItemsPaths()
for (it = mFiles.constBegin(); it != itEnd; ++it) { for (it = mFiles.constBegin(); it != itEnd; ++it) {
EsmFile *file = item(i); EsmFile *file = item(i);
++i; ++i;
if (mCheckStates[file->fileName()] == Qt::Checked && mAvailableFiles.contains(file->fileName())) if (mCheckStates[file->fileName()] == Qt::Checked && canBeChecked(file))
list << file->path(); list << file->path();
} }
@ -475,24 +410,17 @@ QStringList DataFilesModel::uncheckedItems()
return list; return list;
} }
void DataFilesModel::slotcheckedItemsChanged(const QStringList &checkedItems, const QStringList &unCheckedItems) bool DataFilesModel::canBeChecked(EsmFile *file) const
{ {
emit layoutAboutToBeChanged(); //element can be checked if all its dependencies are
bool canBeChecked = true;
QStringList list; foreach (const QString &master, file->masters())
{
foreach (const QString &file, checkedItems) { if (!mCheckStates.contains(master) || mCheckStates[master] != Qt::Checked)
list << mDependencies[file]; {
} canBeChecked = false;
break;
foreach (const QString &file, unCheckedItems) {
foreach (const QString &remove, mDependencies[file]) {
list.removeAll(remove);
} }
} }
return canBeChecked;
mAvailableFiles.clear();
mAvailableFiles.append(list);
emit layoutChanged();
} }

View file

@ -34,10 +34,7 @@ public:
void setEncoding(const QString &encoding); void setEncoding(const QString &encoding);
void addFile(EsmFile *file); void addFiles(const QString &path);
void addMasters(const QString &path);
void addPlugins(const QString &path);
void uncheckAll(); void uncheckAll();
@ -51,18 +48,12 @@ public:
QModelIndex indexFromItem(EsmFile *item) const; QModelIndex indexFromItem(EsmFile *item) const;
EsmFile* findItem(const QString &name); EsmFile* findItem(const QString &name);
EsmFile* item(int row) const; EsmFile* item(int row) const;
signals:
void checkedItemsChanged(const QStringList checkedItems, const QStringList unCheckedItems);
public slots:
void slotcheckedItemsChanged(const QStringList &checkedItems, const QStringList &unCheckedItems);
private: private:
bool canBeChecked(EsmFile *file) const;
void addFile(EsmFile *file);
QList<EsmFile *> mFiles; QList<EsmFile *> mFiles;
QStringList mAvailableFiles;
QHash<QString, QStringList> mDependencies;
QHash<QString, Qt::CheckState> mCheckStates; QHash<QString, Qt::CheckState> mCheckStates;
QString mEncoding; QString mEncoding;