mirror of https://github.com/OpenMW/openmw.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
479 lines
12 KiB
C++
479 lines
12 KiB
C++
11 years ago
|
#include "contentmodel.hpp"
|
||
|
#include "esmfile.hpp"
|
||
11 years ago
|
|
||
11 years ago
|
#include <QDir>
|
||
|
#include <QTextCodec>
|
||
|
#include <components/esm/esmreader.hpp>
|
||
11 years ago
|
#include <QDebug>
|
||
11 years ago
|
|
||
11 years ago
|
ContentSelectorModel::ContentModel::ContentModel(QObject *parent) :
|
||
11 years ago
|
QAbstractTableModel(parent),
|
||
|
mMimeType ("application/omwcontent"),
|
||
|
mMimeTypes (QStringList() << mMimeType),
|
||
|
mColumnCount (1),
|
||
|
mDragDropFlags (Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled),
|
||
|
mDefaultFlags (Qt::ItemIsDropEnabled | Qt::ItemIsSelectable),
|
||
|
mDropActions (Qt::CopyAction | Qt::MoveAction)
|
||
|
{
|
||
11 years ago
|
// setEncoding ("win1252");
|
||
11 years ago
|
uncheckAll();
|
||
|
}
|
||
11 years ago
|
/*
|
||
|
void ContentSelectorModel::ContentModel::setEncoding(const QString &encoding)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (encoding == QLatin1String("win1252"))
|
||
|
mCodec = QTextCodec::codecForName("windows-1252");
|
||
|
|
||
|
else if (encoding == QLatin1String("win1251"))
|
||
|
mCodec = QTextCodec::codecForName("windows-1251");
|
||
|
|
||
|
else if (encoding == QLatin1String("win1250"))
|
||
|
mCodec = QTextCodec::codecForName("windows-1250");
|
||
|
|
||
|
else
|
||
|
return; // This should never happen;
|
||
11 years ago
|
}
|
||
11 years ago
|
*/
|
||
|
int ContentSelectorModel::ContentModel::columnCount(const QModelIndex &parent) const
|
||
11 years ago
|
{
|
||
|
if (parent.isValid())
|
||
|
return 0;
|
||
|
|
||
11 years ago
|
return mColumnCount;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
int ContentSelectorModel::ContentModel::rowCount(const QModelIndex &parent) const
|
||
11 years ago
|
{
|
||
11 years ago
|
if(parent.isValid())
|
||
11 years ago
|
return 0;
|
||
|
|
||
11 years ago
|
return mFiles.size();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
const ContentSelectorModel::EsmFile *ContentSelectorModel::ContentModel::item(int row) const
|
||
11 years ago
|
{
|
||
11 years ago
|
if (row >= 0 && row < mFiles.size())
|
||
11 years ago
|
return mFiles.at(row);
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
ContentSelectorModel::EsmFile *ContentSelectorModel::ContentModel::item(int row)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (row >= 0 && row < mFiles.count())
|
||
|
return mFiles.at(row);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
11 years ago
|
const ContentSelectorModel::EsmFile *ContentSelectorModel::ContentModel::findItem(const QString &name) const
|
||
11 years ago
|
{
|
||
|
foreach (const EsmFile *file, mFiles)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (name == file->fileName())
|
||
|
return file;
|
||
11 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
QModelIndex ContentSelectorModel::ContentModel::indexFromItem(const EsmFile *item) const
|
||
11 years ago
|
{
|
||
11 years ago
|
//workaround: non-const pointer cast for calls from outside contentmodel/contentselector
|
||
|
EsmFile *non_const_file_ptr = const_cast<EsmFile *>(item);
|
||
|
|
||
11 years ago
|
if (item)
|
||
11 years ago
|
return index(mFiles.indexOf(non_const_file_ptr),0);
|
||
11 years ago
|
|
||
|
return QModelIndex();
|
||
|
}
|
||
|
|
||
11 years ago
|
Qt::ItemFlags ContentSelectorModel::ContentModel::flags(const QModelIndex &index) const
|
||
11 years ago
|
{
|
||
|
if (!index.isValid())
|
||
|
return Qt::NoItemFlags;
|
||
|
|
||
11 years ago
|
const EsmFile *file = item(index.row());
|
||
11 years ago
|
|
||
|
if (!file)
|
||
|
return Qt::NoItemFlags;
|
||
|
|
||
|
if (canBeChecked(file))
|
||
11 years ago
|
return Qt::ItemIsEnabled | mDragDropFlags | mDefaultFlags;
|
||
|
|
||
|
return mDefaultFlags;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
QVariant ContentSelectorModel::ContentModel::data(const QModelIndex &index, int role) const
|
||
11 years ago
|
{
|
||
|
if (!index.isValid())
|
||
|
return QVariant();
|
||
|
|
||
|
if (index.row() >= mFiles.size())
|
||
|
return QVariant();
|
||
|
|
||
11 years ago
|
const EsmFile *file = item(index.row());
|
||
11 years ago
|
|
||
|
if (!file)
|
||
|
return QVariant();
|
||
|
|
||
|
const int column = index.column();
|
||
|
|
||
|
switch (role)
|
||
|
{
|
||
|
case Qt::EditRole:
|
||
|
case Qt::DisplayRole:
|
||
|
{
|
||
11 years ago
|
if (column >=0 && column <=EsmFile::FileProperty_GameFile)
|
||
11 years ago
|
return file->fileProperty(static_cast<const EsmFile::FileProperty>(column));
|
||
11 years ago
|
|
||
|
return QVariant();
|
||
11 years ago
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
|
case Qt::TextAlignmentRole:
|
||
|
{
|
||
|
switch (column)
|
||
|
{
|
||
|
case 0:
|
||
|
case 1:
|
||
|
return Qt::AlignLeft + Qt::AlignVCenter;
|
||
|
case 2:
|
||
|
case 3:
|
||
|
return Qt::AlignRight + Qt::AlignVCenter;
|
||
|
default:
|
||
|
return Qt::AlignLeft + Qt::AlignVCenter;
|
||
|
}
|
||
|
return QVariant();
|
||
11 years ago
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
|
case Qt::ToolTipRole:
|
||
|
{
|
||
|
if (column != 0)
|
||
|
return QVariant();
|
||
|
|
||
11 years ago
|
return file->toolTip();
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
case Qt::CheckStateRole:
|
||
11 years ago
|
{
|
||
11 years ago
|
if (!file->isGameFile())
|
||
11 years ago
|
return isChecked(file->fileName());
|
||
|
break;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
case Qt::UserRole:
|
||
|
{
|
||
11 years ago
|
if (file->isGameFile())
|
||
|
return ContentType_GameFile;
|
||
11 years ago
|
else
|
||
11 years ago
|
if (flags(index) & Qt::ItemIsEnabled)
|
||
|
return ContentType_Addon;
|
||
|
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
case Qt::UserRole + 1:
|
||
|
return isChecked(file->fileName());
|
||
|
break;
|
||
11 years ago
|
}
|
||
11 years ago
|
return QVariant();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||
11 years ago
|
{
|
||
11 years ago
|
if(!index.isValid())
|
||
|
return false;
|
||
11 years ago
|
|
||
|
EsmFile *file = item(index.row());
|
||
11 years ago
|
QString fileName = file->fileName();
|
||
11 years ago
|
bool success = false;
|
||
11 years ago
|
|
||
11 years ago
|
switch(role)
|
||
|
{
|
||
|
case Qt::EditRole:
|
||
11 years ago
|
{
|
||
|
QStringList list = value.toStringList();
|
||
11 years ago
|
|
||
11 years ago
|
for (int i = 0; i < EsmFile::FileProperty_GameFile; i++)
|
||
11 years ago
|
file->setFileProperty(static_cast<EsmFile::FileProperty>(i), list.at(i));
|
||
11 years ago
|
|
||
11 years ago
|
for (int i = EsmFile::FileProperty_GameFile; i < list.size(); i++)
|
||
|
file->setFileProperty (EsmFile::FileProperty_GameFile, list.at(i));
|
||
11 years ago
|
|
||
11 years ago
|
emit dataChanged(index, index);
|
||
11 years ago
|
|
||
11 years ago
|
success = true;
|
||
|
}
|
||
|
break;
|
||
11 years ago
|
|
||
|
case Qt::UserRole+1:
|
||
11 years ago
|
{
|
||
|
setCheckState(fileName, value.toBool());
|
||
11 years ago
|
|
||
11 years ago
|
emit dataChanged(index, index);
|
||
11 years ago
|
|
||
11 years ago
|
foreach (EsmFile *file, mFiles)
|
||
|
{
|
||
11 years ago
|
if (file->gameFiles().contains(fileName))
|
||
11 years ago
|
{
|
||
11 years ago
|
QModelIndex idx = indexFromItem(file);
|
||
|
emit dataChanged(idx, idx);
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
success = true;
|
||
|
}
|
||
|
break;
|
||
11 years ago
|
|
||
|
case Qt::CheckStateRole:
|
||
11 years ago
|
{
|
||
|
int checkValue = value.toInt();
|
||
11 years ago
|
bool success = false;
|
||
|
bool setState = false;
|
||
11 years ago
|
if ((checkValue==Qt::Checked) && !isChecked(fileName))
|
||
11 years ago
|
{
|
||
|
setState = true;
|
||
|
success = true;
|
||
|
}
|
||
11 years ago
|
else if ((checkValue == Qt::Checked) && isChecked (fileName))
|
||
11 years ago
|
setState = true;
|
||
11 years ago
|
else if (checkValue == Qt::Unchecked)
|
||
11 years ago
|
setState = true;
|
||
|
|
||
|
if (setState)
|
||
|
{
|
||
|
setCheckState(fileName, success);
|
||
|
emit dataChanged(index, index);
|
||
|
|
||
|
}
|
||
|
else
|
||
|
return success;
|
||
11 years ago
|
|
||
11 years ago
|
|
||
|
foreach (EsmFile *file, mFiles)
|
||
|
{
|
||
|
if (file->gameFiles().contains(fileName))
|
||
|
{
|
||
|
QModelIndex idx = indexFromItem(file);
|
||
|
emit dataChanged(idx, idx);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
success = true;
|
||
|
}
|
||
|
break;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
return success;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::insertRows(int position, int rows, const QModelIndex &parent)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (parent.isValid())
|
||
|
return false;
|
||
|
|
||
11 years ago
|
beginInsertRows(parent, position, position+rows-1);
|
||
11 years ago
|
{
|
||
|
for (int row = 0; row < rows; ++row)
|
||
|
mFiles.insert(position, new EsmFile);
|
||
11 years ago
|
|
||
11 years ago
|
} endInsertRows();
|
||
11 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::removeRows(int position, int rows, const QModelIndex &parent)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (parent.isValid())
|
||
|
return false;
|
||
|
|
||
11 years ago
|
beginRemoveRows(parent, position, position+rows-1);
|
||
11 years ago
|
{
|
||
|
for (int row = 0; row < rows; ++row)
|
||
|
delete mFiles.takeAt(position);
|
||
11 years ago
|
|
||
11 years ago
|
} endRemoveRows();
|
||
11 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
11 years ago
|
Qt::DropActions ContentSelectorModel::ContentModel::supportedDropActions() const
|
||
11 years ago
|
{
|
||
11 years ago
|
return mDropActions;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
QStringList ContentSelectorModel::ContentModel::mimeTypes() const
|
||
11 years ago
|
{
|
||
11 years ago
|
return mMimeTypes;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
QMimeData *ContentSelectorModel::ContentModel::mimeData(const QModelIndexList &indexes) const
|
||
11 years ago
|
{
|
||
|
QByteArray encodedData;
|
||
|
|
||
|
foreach (const QModelIndex &index, indexes)
|
||
|
{
|
||
11 years ago
|
if (!index.isValid())
|
||
|
continue;
|
||
|
|
||
11 years ago
|
encodedData.append(item(index.row())->encodedData());
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
QMimeData *mimeData = new QMimeData();
|
||
11 years ago
|
mimeData->setData(mMimeType, encodedData);
|
||
11 years ago
|
|
||
11 years ago
|
return mimeData;
|
||
|
}
|
||
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||
11 years ago
|
{
|
||
|
if (action == Qt::IgnoreAction)
|
||
|
return true;
|
||
|
|
||
11 years ago
|
if (column > 0)
|
||
11 years ago
|
return false;
|
||
|
|
||
11 years ago
|
if (!data->hasFormat(mMimeType))
|
||
11 years ago
|
return false;
|
||
|
|
||
11 years ago
|
int beginRow = rowCount();
|
||
11 years ago
|
|
||
|
if (row != -1)
|
||
|
beginRow = row;
|
||
11 years ago
|
|
||
11 years ago
|
else if (parent.isValid())
|
||
|
beginRow = parent.row();
|
||
11 years ago
|
|
||
11 years ago
|
QByteArray encodedData = data->data(mMimeType);
|
||
11 years ago
|
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
||
|
|
||
|
while (!stream.atEnd())
|
||
|
{
|
||
11 years ago
|
|
||
|
QString value;
|
||
11 years ago
|
QStringList values;
|
||
11 years ago
|
QStringList gamefiles;
|
||
11 years ago
|
|
||
11 years ago
|
for (int i = 0; i < EsmFile::FileProperty_GameFile; ++i)
|
||
11 years ago
|
{
|
||
|
stream >> value;
|
||
|
values << value;
|
||
|
}
|
||
|
|
||
11 years ago
|
stream >> gamefiles;
|
||
11 years ago
|
|
||
|
insertRows(beginRow, 1);
|
||
|
|
||
|
QModelIndex idx = index(beginRow++, 0, QModelIndex());
|
||
11 years ago
|
setData(idx, QStringList() << values << gamefiles, Qt::EditRole);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
return true;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::canBeChecked(const EsmFile *file) const
|
||
11 years ago
|
{
|
||
|
//element can be checked if all its dependencies are
|
||
11 years ago
|
foreach (const QString &gamefile, file->gameFiles())
|
||
|
if (!isChecked(gamefile))
|
||
11 years ago
|
return false;
|
||
11 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
11 years ago
|
void ContentSelectorModel::ContentModel::addFile(EsmFile *file)
|
||
11 years ago
|
{
|
||
11 years ago
|
beginInsertRows(QModelIndex(), mFiles.count(), mFiles.count());
|
||
|
mFiles.append(file);
|
||
11 years ago
|
endInsertRows();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void ContentSelectorModel::ContentModel::addFiles(const QString &path)
|
||
11 years ago
|
{
|
||
|
QDir dir(path);
|
||
|
QStringList filters;
|
||
|
filters << "*.esp" << "*.esm" << "*.omwgame" << "*.omwaddon";
|
||
|
dir.setNameFilters(filters);
|
||
|
|
||
11 years ago
|
QTextCodec *codec = QTextCodec::codecForName("UTF8");
|
||
|
|
||
11 years ago
|
// Create a decoder for non-latin characters in esx metadata
|
||
11 years ago
|
QTextDecoder *decoder = codec->makeDecoder();
|
||
11 years ago
|
|
||
11 years ago
|
foreach (const QString &path, dir.entryList())
|
||
|
{
|
||
11 years ago
|
QFileInfo info(dir.absoluteFilePath(path));
|
||
|
EsmFile *file = new EsmFile(path);
|
||
|
|
||
|
try {
|
||
|
ESM::ESMReader fileReader;
|
||
11 years ago
|
ToUTF8::Utf8Encoder encoder(); //ToUTF8::calculateEncoding(QString(mCodec->name()).toStdString()));
|
||
|
//fileReader.setEncoder(&encoder);
|
||
11 years ago
|
fileReader.open(dir.absoluteFilePath(path).toStdString());
|
||
|
|
||
11 years ago
|
foreach (const ESM::Header::MasterData &item, fileReader.getGameFiles())
|
||
|
file->addGameFile(QString::fromStdString(item.name));
|
||
11 years ago
|
|
||
11 years ago
|
file->setAuthor (decoder->toUnicode(fileReader.getAuthor().c_str()));
|
||
|
file->setDate (info.lastModified());
|
||
11 years ago
|
file->setFormat (fileReader.getFormat());
|
||
11 years ago
|
file->setPath (info.absoluteFilePath());
|
||
11 years ago
|
file->setDescription(decoder->toUnicode(fileReader.getDesc().c_str()));
|
||
|
|
||
|
|
||
|
// Put the file in the table
|
||
|
if (findItem(path) == 0)
|
||
|
addFile(file);
|
||
|
|
||
|
} catch(std::runtime_error &e) {
|
||
|
// An error occurred while reading the .esp
|
||
|
qWarning() << "Error reading addon file: " << e.what();
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
delete decoder;
|
||
|
}
|
||
|
|
||
11 years ago
|
bool ContentSelectorModel::ContentModel::isChecked(const QString& name) const
|
||
11 years ago
|
{
|
||
11 years ago
|
if (mCheckStates.contains(name))
|
||
|
return (mCheckStates[name] == Qt::Checked);
|
||
|
|
||
|
return false;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void ContentSelectorModel::ContentModel::setCheckState(const QString &name, bool isChecked)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (name.isEmpty())
|
||
11 years ago
|
return;
|
||
|
|
||
11 years ago
|
Qt::CheckState state = Qt::Unchecked;
|
||
11 years ago
|
|
||
11 years ago
|
if (isChecked)
|
||
|
state = Qt::Checked;
|
||
11 years ago
|
|
||
11 years ago
|
mCheckStates[name] = state;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
ContentSelectorModel::ContentFileList ContentSelectorModel::ContentModel::checkedItems() const
|
||
11 years ago
|
{
|
||
|
ContentFileList list;
|
||
|
|
||
11 years ago
|
foreach (EsmFile *file, mFiles)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (isChecked(file->fileName()))
|
||
11 years ago
|
list << file;
|
||
|
}
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
|
||
11 years ago
|
void ContentSelectorModel::ContentModel::uncheckAll()
|
||
11 years ago
|
{
|
||
|
emit layoutAboutToBeChanged();
|
||
|
mCheckStates.clear();
|
||
|
emit layoutChanged();
|
||
|
}
|