1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-10 12:39:53 +00:00

Fix editing "ID" column within nested tables in dialogue subview.

This commit is contained in:
cc9cii 2015-04-11 11:27:48 +10:00
parent 1220369da3
commit a632b2cfeb
4 changed files with 41 additions and 18 deletions

View File

@ -38,18 +38,26 @@ QVariant CSMWorld::IdTree::data (const QModelIndex & index, int role) const
if ((role!=Qt::DisplayRole && role!=Qt::EditRole) || index.row() < 0 || index.column() < 0) if ((role!=Qt::DisplayRole && role!=Qt::EditRole) || index.row() < 0 || index.column() < 0)
return QVariant(); return QVariant();
if (role==Qt::EditRole && !idCollection()->getColumn (index.column()).isEditable())
return QVariant();
if (index.internalId() != 0) if (index.internalId() != 0)
{ {
std::pair<int, int> parentAdress(unfoldIndexAdress(index.internalId())); std::pair<int, int> parentAddress(unfoldIndexAddress(index.internalId()));
return mNestedCollection->getNestedData(parentAdress.first, if (role == Qt::EditRole &&
parentAdress.second, index.row(), index.column()); !mNestedCollection->getNestableColumn(parentAddress.second)->nestedColumn(index.column()).isEditable())
{
return QVariant();
}
return mNestedCollection->getNestedData(parentAddress.first,
parentAddress.second, index.row(), index.column());
} }
else else
{
if (role==Qt::EditRole && !idCollection()->getColumn (index.column()).isEditable())
return QVariant();
return idCollection()->getData (index.row(), index.column()); return idCollection()->getData (index.row(), index.column());
}
} }
QVariant CSMWorld::IdTree::nestedHeaderData(int section, int subSection, Qt::Orientation orientation, int role) const QVariant CSMWorld::IdTree::nestedHeaderData(int section, int subSection, Qt::Orientation orientation, int role) const
@ -80,12 +88,12 @@ bool CSMWorld::IdTree::setData (const QModelIndex &index, const QVariant &value,
{ {
if (idCollection()->getColumn(parent(index).column()).isEditable() && role==Qt::EditRole) if (idCollection()->getColumn(parent(index).column()).isEditable() && role==Qt::EditRole)
{ {
const std::pair<int, int>& parentAdress(unfoldIndexAdress(index.internalId())); const std::pair<int, int>& parentAddress(unfoldIndexAddress(index.internalId()));
mNestedCollection->setNestedData(parentAdress.first, parentAdress.second, value, index.row(), index.column()); mNestedCollection->setNestedData(parentAddress.first, parentAddress.second, value, index.row(), index.column());
emit dataChanged (CSMWorld::IdTree::index (parentAdress.first, 0), emit dataChanged (CSMWorld::IdTree::index (parentAddress.first, 0),
CSMWorld::IdTree::index (parentAdress.second, idCollection()->getColumns()-1)); CSMWorld::IdTree::index (parentAddress.second, idCollection()->getColumns()-1));
return true; return true;
} }
@ -145,7 +153,7 @@ QModelIndex CSMWorld::IdTree::index (int row, int column, const QModelIndex& par
unsigned int encodedId = 0; unsigned int encodedId = 0;
if (parent.isValid()) if (parent.isValid())
{ {
encodedId = this->foldIndexAdress(parent); encodedId = this->foldIndexAddress(parent);
} }
if (row<0 || row>=idCollection()->getSize()) if (row<0 || row>=idCollection()->getSize())
@ -157,13 +165,18 @@ QModelIndex CSMWorld::IdTree::index (int row, int column, const QModelIndex& par
return createIndex(row, column, encodedId); // store internal id return createIndex(row, column, encodedId); // store internal id
} }
QModelIndex CSMWorld::IdTree::getNestedModelIndex (const std::string& id, int column) const
{
return CSMWorld::IdTable::index(idCollection()->getIndex (id), column);
}
QModelIndex CSMWorld::IdTree::parent (const QModelIndex& index) const QModelIndex CSMWorld::IdTree::parent (const QModelIndex& index) const
{ {
if (index.internalId() == 0) // 0 is used for indexs with invalid parent (top level data) if (index.internalId() == 0) // 0 is used for indexs with invalid parent (top level data)
return QModelIndex(); return QModelIndex();
unsigned int id = index.internalId(); unsigned int id = index.internalId();
const std::pair<int, int>& adress(unfoldIndexAdress(id)); const std::pair<int, int>& adress(unfoldIndexAddress(id));
if (adress.first >= this->rowCount() || adress.second >= this->columnCount()) if (adress.first >= this->rowCount() || adress.second >= this->columnCount())
throw "Parent index is not present in the model"; throw "Parent index is not present in the model";
@ -171,14 +184,14 @@ QModelIndex CSMWorld::IdTree::parent (const QModelIndex& index) const
return createIndex(adress.first, adress.second); return createIndex(adress.first, adress.second);
} }
unsigned int CSMWorld::IdTree::foldIndexAdress (const QModelIndex& index) const unsigned int CSMWorld::IdTree::foldIndexAddress (const QModelIndex& index) const
{ {
unsigned int out = index.row() * this->columnCount(); unsigned int out = index.row() * this->columnCount();
out += index.column(); out += index.column();
return ++out; return ++out;
} }
std::pair< int, int > CSMWorld::IdTree::unfoldIndexAdress (unsigned int id) const std::pair< int, int > CSMWorld::IdTree::unfoldIndexAddress (unsigned int id) const
{ {
if (id == 0) if (id == 0)
throw "Attempt to unfold index id of the top level data cell"; throw "Attempt to unfold index id of the top level data cell";
@ -189,6 +202,8 @@ std::pair< int, int > CSMWorld::IdTree::unfoldIndexAdress (unsigned int id) cons
return std::make_pair (row, column); return std::make_pair (row, column);
} }
// FIXME: Not sure why this check is also needed?
//
// index.data().isValid() requires RefIdAdapter::getData() to return a valid QVariant for // index.data().isValid() requires RefIdAdapter::getData() to return a valid QVariant for
// nested columns (refidadapterimp.hpp) // nested columns (refidadapterimp.hpp)
// //
@ -198,7 +213,7 @@ bool CSMWorld::IdTree::hasChildren(const QModelIndex& index) const
return (index.isValid() && return (index.isValid() &&
index.internalId() == 0 && index.internalId() == 0 &&
mNestedCollection->getNestableColumn(index.column())->hasChildren() && mNestedCollection->getNestableColumn(index.column())->hasChildren() &&
index.data().isValid()); // FIXME: not sure why this check is also needed index.data().isValid());
} }
void CSMWorld::IdTree::setNestedTable(const QModelIndex& index, const CSMWorld::NestedTableWrapperBase& nestedTable) void CSMWorld::IdTree::setNestedTable(const QModelIndex& index, const CSMWorld::NestedTableWrapperBase& nestedTable)

View File

@ -34,8 +34,8 @@ namespace CSMWorld
IdTree (const IdTree&); IdTree (const IdTree&);
IdTree& operator= (const IdTree&); IdTree& operator= (const IdTree&);
unsigned int foldIndexAdress(const QModelIndex& index) const; unsigned int foldIndexAddress(const QModelIndex& index) const;
std::pair<int, int> unfoldIndexAdress(unsigned int id) const; std::pair<int, int> unfoldIndexAddress(unsigned int id) const;
public: public:
@ -61,6 +61,8 @@ namespace CSMWorld
virtual QModelIndex parent (const QModelIndex& index) const; virtual QModelIndex parent (const QModelIndex& index) const;
QModelIndex getNestedModelIndex (const std::string& id, int column) const;
QVariant nestedHeaderData(int section, int subSection, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QVariant nestedHeaderData(int section, int subSection, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
NestedTableWrapperBase* nestedTable(const QModelIndex &index) const; NestedTableWrapperBase* nestedTable(const QModelIndex &index) const;

View File

@ -50,7 +50,7 @@ QModelIndex CSMWorld::NestedTableProxyModel::mapFromSource(const QModelIndex& so
QModelIndex CSMWorld::NestedTableProxyModel::mapToSource(const QModelIndex& proxyIndex) const QModelIndex CSMWorld::NestedTableProxyModel::mapToSource(const QModelIndex& proxyIndex) const
{ {
const QModelIndex& parent = mMainModel->getModelIndex (mId, mParentColumn); const QModelIndex& parent = mMainModel->getNestedModelIndex (mId, mParentColumn);
return mMainModel->index(proxyIndex.row(), proxyIndex.column(), parent); return mMainModel->index(proxyIndex.row(), proxyIndex.column(), parent);
} }
@ -98,6 +98,10 @@ QVariant CSMWorld::NestedTableProxyModel::headerData(int section,
return mMainModel->nestedHeaderData(mParentColumn, section, orientation, role); return mMainModel->nestedHeaderData(mParentColumn, section, orientation, role);
} }
QVariant CSMWorld::NestedTableProxyModel::data(const QModelIndex& index, int role) const
{
return mMainModel->data(mapToSource(index), role);
}
bool CSMWorld::NestedTableProxyModel::setData (const QModelIndex & index, const QVariant & value, int role) bool CSMWorld::NestedTableProxyModel::setData (const QModelIndex & index, const QVariant & value, int role)
{ {

View File

@ -53,6 +53,8 @@ namespace CSMWorld
virtual QVariant headerData (int section, Qt::Orientation orientation, int role) const; virtual QVariant headerData (int section, Qt::Orientation orientation, int role) const;
virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole); virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags(const QModelIndex& index) const; virtual Qt::ItemFlags flags(const QModelIndex& index) const;