1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-24 11:23:50 +00:00
openmw-tes3mp/apps/launcher/datafilesdialog.cpp

409 lines
14 KiB
C++
Raw Normal View History

2011-03-28 23:36:26 +00:00
#include <QtGui>
#include <components/esm/esm_reader.hpp>
#include "datafilesdialog.h"
2011-03-30 12:05:27 +00:00
//#include "datafilesmodel.h"
//#include "datafilesitem.h"
2011-03-28 23:36:26 +00:00
using namespace ESM;
DataFilesDialog::DataFilesDialog()
{
QSplitter *splitter = new QSplitter(this);
tree = new QTreeView(this);
mastertable = new QTableView(this);
plugintable = new QTableView(this);
splitter->setOrientation(Qt::Vertical);
splitter->addWidget(tree);
splitter->addWidget(mastertable);
splitter->addWidget(plugintable);
// Adjust the widget heights inside the splitter
QList<int> sizelist;
sizelist << 100 << 200 << 400;
splitter->setSizes(sizelist);
2011-03-28 23:36:26 +00:00
QVBoxLayout *dialogLayout = new QVBoxLayout(this);
dialogLayout->addWidget(splitter);
//dialogLayout->addWidget(plugintable);
2011-03-28 23:36:26 +00:00
datafilesmodel = new QStandardItemModel();
mastersmodel = new QStandardItemModel();
2011-03-28 23:36:26 +00:00
QDir datafilesdir("data/");
if (!datafilesdir.exists())
qWarning("Cannot find the plugin directory");
2011-03-28 23:36:26 +00:00
QStringList acceptedfiles = (QStringList() << "*.esp");
QStringList files;
datafilesdir.setNameFilters(acceptedfiles);
files = datafilesdir.entryList();
2011-03-28 23:36:26 +00:00
//foreach (const QString &currentfile, datafiles) {
for (int i=0; i<files.count(); ++i)
{
ESMReader datafile;
QString currentfile = files.at(i);
QStringList masters;
QString path = QString("data/").append(currentfile);
datafile.open(path.toStdString());
ESMReader::MasterList mlist = datafile.getMasters();
for (unsigned int i = 0; i < mlist.size(); ++i) {// Add each master file
masters.append(QString::fromStdString(mlist[i].name));
}
masters.sort(); // Sort the masters alphabetically
// Add the masters to mastersmodel
foreach (const QString &currentmaster, masters) {
QStandardItem *item = new QStandardItem(currentmaster);
QList<QStandardItem*> foundmasters = mastersmodel->findItems(currentmaster);
if (foundmasters.isEmpty()) {
// Current master is not found in the master, add it
mastersmodel->appendRow(item);
}
}
// Add the masters to datafilesmodel
QStandardItem *item = new QStandardItem(masters.join(","));
QStandardItem *child = new QStandardItem(currentfile);
QList<QStandardItem*> masteritems = datafilesmodel->findItems(masters.join(","));
if (masteritems.isEmpty()) {
item->appendRow(child);
datafilesmodel->appendRow(item);
} else {
foreach (QStandardItem *currentitem, masteritems) {
currentitem->appendRow(child);
}
}
//if (foundmasters.isEmpty()) {
//datafilesmodel->appendRow(item);
//}
}
2011-03-28 23:36:26 +00:00
/*for( int r=0; r<5; ++r ) {
QStandardItem *item = new QStandardItem( QString("Morrowind.esm").arg(r));
2011-03-28 23:36:26 +00:00
/ *for( int i=0; i<3; i++ ) {
QStandardItem *child = new QStandardItem( QString("Master %0 Item %1").arg(r).arg(i));
//child->setEditable( false );
item->appendRow( child );
}* /
2011-03-28 23:36:26 +00:00
mastersmodel->setItem(r, 0, item);
}*/
2011-03-28 23:36:26 +00:00
pluginsmodel = new QStandardItemModel(0, 1);
masterselectmodel = new QItemSelectionModel(mastersmodel);
tree->setModel(datafilesmodel);
tree->header()->hide();
mastertable->setModel(mastersmodel);
mastertable->setSelectionModel(masterselectmodel);
mastertable->setSelectionBehavior(QAbstractItemView::SelectRows);
mastertable->setSelectionMode(QAbstractItemView::MultiSelection);
mastertable->setEditTriggers(QAbstractItemView::NoEditTriggers);
mastertable->horizontalHeader()->setStretchLastSection(true);
mastertable->horizontalHeader()->hide();
plugintable->setModel(pluginsmodel);
plugintable->setSelectionBehavior(QAbstractItemView::SelectRows);
plugintable->setSelectionMode(QAbstractItemView::MultiSelection);
plugintable->setEditTriggers(QAbstractItemView::NoEditTriggers);
plugintable->horizontalHeader()->setStretchLastSection(true);
plugintable->horizontalHeader()->hide();
connect(masterselectmodel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(masterSelectionChanged(const QItemSelection&, const QItemSelection&)));
// Adjust the dialog width
setMinimumWidth(500);
2011-03-28 23:36:26 +00:00
}
void DataFilesDialog::appendPlugins(const QModelIndex &masterindex)
2011-03-28 23:36:26 +00:00
{
// Find the plugins in the datafilesmodel and append them to the pluginsmodel
if (!masterindex.isValid())
2011-03-28 23:36:26 +00:00
return;
for (int r=0; r<datafilesmodel->rowCount(masterindex); ++r ) {
QModelIndex childindex = masterindex.child(r, 0);
if (childindex.isValid()) {
// Now we see if the pluginsmodel already contains this plugin
QList<QStandardItem *> itemlist = pluginsmodel->findItems(QVariant(datafilesmodel->data(childindex)).toString());
if (itemlist.isEmpty())
{
// Plugin not yet in the pluginsmodel, add it
QStandardItem *item = new QStandardItem(QVariant(datafilesmodel->data(childindex)).toString());
pluginsmodel->appendRow(item);
}
}
2011-03-28 23:36:26 +00:00
}
2011-03-28 23:36:26 +00:00
}
void DataFilesDialog::removePlugins(const QModelIndex &masterindex)
2011-03-28 23:36:26 +00:00
{
2011-03-30 12:05:27 +00:00
if (!masterindex.isValid())
return;
for (int r=0; r<datafilesmodel->rowCount(masterindex); ++r) {
QModelIndex childindex = masterindex.child(r, 0);
QList<QStandardItem *> itemlist = pluginsmodel->findItems(QVariant(childindex.data()).toString());
if (!itemlist.isEmpty()) {
foreach (const QStandardItem *currentitem, itemlist) {
qDebug() << "Remove plugin:" << currentitem->data(Qt::DisplayRole).toString();
pluginsmodel->removeRow(currentitem->row());
}
}
2011-03-30 12:05:27 +00:00
}
}
void DataFilesDialog::masterSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
if (masterselectmodel->hasSelection()) { // Not exactly necessary to check
const QModelIndexList selectedindexes = masterselectmodel->selectedIndexes();
2011-03-30 12:05:27 +00:00
QStringList masters;
QString masterstr;
// Create a QStringList containing all the masters
foreach (const QModelIndex &currentindex, selectedindexes) {
masters.append(currentindex.data().toString());
}
masters.sort();
masterstr = masters.join(","); // Make a comma-separated QString
qDebug() << "Masters" << masterstr;
// Iterate over all masters in the datafilesmodel to see if they are selected
for (int r=0; r<datafilesmodel->rowCount(); ++r) {
QModelIndex currentindex = datafilesmodel->index(r, 0);
QString master = currentindex.data().toString();
if (currentindex.isValid()) {
// See if the current master is in the string with selected masters
if (masterstr.contains(master))
{
// Append the plugins from the current master to pluginsmodel
appendPlugins(currentindex);
}
}
}
}
2011-03-30 12:05:27 +00:00
// See what plugins to remove
QModelIndexList deselectedindexes = deselected.indexes();
if (!deselectedindexes.isEmpty()) {
foreach (const QModelIndex &currentindex, deselectedindexes) {
QString master = currentindex.data().toString();
master.prepend("*");
master.append("*");
QList<QStandardItem *> itemlist = datafilesmodel->findItems(master, Qt::MatchWildcard);
if (itemlist.isEmpty())
qDebug() << "Empty as shit";
foreach (const QStandardItem *currentitem, itemlist) {
QModelIndex index = currentitem->index();
qDebug() << "Master to remove plugins of:" << index.data().toString();
removePlugins(index);
}
}
}
2011-03-28 23:36:26 +00:00
}
void DataFilesDialog::readConfig()
{
/* QString filename;
QString path = "data/"; // TODO: Should be global
QFile file("openmw.cfg"); // Specify filepath later
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "error open";
close(); // File cannot be opened or created TODO: throw error
}
QTextStream in(&file);
QStringList datafiles;
// Add each data file read from the config file to a QStringList
while (!in.atEnd()) {
QString line = in.readLine();
if (line.contains("master")) {
filename = line.remove("master=");
filename.prepend(path);
datafiles << filename << "\n";
} else if (line.contains("plugin")) {
filename = line.remove("plugin=");
filename.prepend(path);
datafiles << filename << "\n";
}
}
2011-03-28 23:36:26 +00:00
file.close();
// Check if the files are in the model, set to checked if found
foreach(const QString &currentfile, datafiles) {
QModelIndex index = dataFilesModel->index(currentfile, 0);
2011-03-28 23:36:26 +00:00
if (index.isValid()) {
// File is found in model, set it to checked
dataFilesModel->setData(index, Qt::Checked, Qt::CheckStateRole);
}
}*/
2011-03-28 23:36:26 +00:00
}
void DataFilesDialog::writeConfig()
{
/* Custom write method: We cannot use QSettings because it does not accept spaces
// QList<QPersistentModelIndex> checkeditems = dataFilesModel->getCheckedItems().toList();
QStringList checkeditems = dataFilesModel->getCheckedItems();
//QString sectionname = "[Game Files]";
QString filename;
QFileInfo datafile;
// Sort the items so that master files end up on top
foreach (const QString &currentitem, checkeditems) {
if(currentitem.endsWith(QString(".esm"), Qt::CaseInsensitive)) {
checkeditems.move(checkeditems.indexOf(currentitem), 0);
}
}
QFile file("openmw.cfg"); // Specify filepath later
2011-03-28 23:36:26 +00:00
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
close(); // File cannot be opened or created TODO: throw error
2011-03-28 23:36:26 +00:00
//QTextStream in(&file);
QTextStream in(&file);
//QString buffer;
QByteArray buffer;
// Remove all previous master/plugin entries from config
2011-03-28 23:36:26 +00:00
while (!in.atEnd()) {
QString line = in.readLine();
//if (!line.contains("GameFile") && line != "[Game Files]") {
if (!line.contains("master") && !line.contains("plugin")) {
buffer += line += "\n";
2011-03-28 23:36:26 +00:00
}
}
file.close();
// Now we write back the other config entries
2011-03-28 23:36:26 +00:00
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
close(); // File cannot be opened or created TODO: throw error
2011-03-28 23:36:26 +00:00
file.write(buffer);
QTextStream out(&file);
// Write the section name to the config file before we write out the data files
//out << sectionname << endl;
2011-03-28 23:36:26 +00:00
// Write the list of game files to the config
for (int i = 0; i < checkeditems.size(); ++i) {
//filename = dataFilesModel->fileName(checkeditems.at(i));
filename = checkeditems.at(i);
datafile = QFileInfo(filename);
if (datafile.exists()) {
if (filename.endsWith(QString(".esm"), Qt::CaseInsensitive)) {
out << "master=" << datafile.fileName() << endl;
} else if (filename.endsWith(QString(".esp"), Qt::CaseInsensitive)) {
out << "plugin=" << datafile.fileName() << endl;
}
}
2011-03-28 23:36:26 +00:00
}
file.close();
close(); // Exit dialog*/
2011-03-28 23:36:26 +00:00
}
void DataFilesDialog::restoreDefaults()
{
/* // Uncheck all checked items
2011-03-28 23:36:26 +00:00
dataFilesModel->checkedItems.clear();
QModelIndexList indexlist; // Make a list of default master files
2011-03-29 18:15:15 +00:00
indexlist.append(dataFilesModel->index("data/Morrowind.esm", 0));
indexlist.append(dataFilesModel->index("data/Tribunal.esm", 0));
indexlist.append(dataFilesModel->index("data/Bloodmoon.esm", 0));
2011-03-28 23:36:26 +00:00
foreach (const QModelIndex &index, indexlist) {
if (index.isValid()) {
// Master file found, check it
dataFilesModel->setData(index, Qt::Checked, Qt::CheckStateRole);
}
}
dataFilesModel->submit(); // Force refresh of view*/
2011-03-28 23:36:26 +00:00
}
/*void DataFilesDialog::setFilter()
2011-03-28 23:36:26 +00:00
{
/ *
2011-03-28 23:36:26 +00:00
QStringList filefilter = (QStringList() << "*.esm" << "*.esp");
QStringList currentfilefilter;
QString esmfilter = lineFilter->text();
QString espfilter = lineFilter->text();
if (lineFilter->text().isEmpty()) {
dataFilesModel->setNameFilters(filefilter);
// sortModel->setFilterRegExp(QRegExp("*.esp", Qt::CaseInsensitive,
// QRegExp::FixedString));
// sortModel->setFilterKeyColumn(0);
return;
}
esmfilter.prepend("*");
esmfilter.append("*.esm");
espfilter.prepend("*");
espfilter.append("*.esp");
currentfilefilter << esmfilter << espfilter;
// sortModel->setFilterRegExp(QRegExp(espfilter, Qt::CaseInsensitive,
// QRegExp::FixedString));
// sortModel->setFilterKeyColumn(0);
dataFilesModel->setNameFilters(currentfilefilter);
// readConfig();
// dataFilesModel->submit();
// dataFilesModel->setData();
* /
2011-03-28 23:36:26 +00:00
}
*/