1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 21:23:52 +00:00

global variable editing (no undo support yet)

This commit is contained in:
Marc Zinnschlag 2012-11-29 14:45:34 +01:00
parent 92f70635a2
commit 8a09e03d5c
7 changed files with 91 additions and 9 deletions

View file

@ -14,6 +14,18 @@ namespace CSMWorld
{ {
return record.get().mValue; return record.get().mValue;
} }
virtual void set (Record<ESXRecordT>& record, const QVariant& data)
{
ESXRecordT base = record.getBase();
base.mValue = data.toFloat();
record.setModified (base);
}
virtual bool isEditable() const
{
return true;
}
}; };
} }

View file

@ -23,6 +23,10 @@ namespace CSMWorld
virtual ~Column() {} virtual ~Column() {}
virtual QVariant get (const Record<ESXRecordT>& record) const = 0; virtual QVariant get (const Record<ESXRecordT>& record) const = 0;
virtual void set (Record<ESXRecordT>& record, const QVariant& data) = 0;
virtual bool isEditable() const = 0;
}; };
class IdCollectionBase class IdCollectionBase
@ -46,6 +50,10 @@ namespace CSMWorld
virtual std::string getTitle (int column) const = 0; virtual std::string getTitle (int column) const = 0;
virtual QVariant getData (int index, int column) const = 0; virtual QVariant getData (int index, int column) const = 0;
virtual void setData (int index, int column, const QVariant& data) = 0;
virtual bool isEditable (int column) const = 0;
}; };
///< \brief Collection of ID-based records ///< \brief Collection of ID-based records
@ -77,9 +85,13 @@ namespace CSMWorld
virtual QVariant getData (int index, int column) const; virtual QVariant getData (int index, int column) const;
virtual void setData (int index, int column, const QVariant& data);
virtual std::string getTitle (int column) const; virtual std::string getTitle (int column) const;
virtual void addColumn (Column<ESXRecordT> *column); virtual bool isEditable (int column) const;
void addColumn (Column<ESXRecordT> *column);
}; };
template<typename ESXRecordT> template<typename ESXRecordT>
@ -142,12 +154,24 @@ namespace CSMWorld
return mColumns.at (column)->get (mRecords.at (index)); return mColumns.at (column)->get (mRecords.at (index));
} }
template<typename ESXRecordT>
void IdCollection<ESXRecordT>::setData (int index, int column, const QVariant& data)
{
return mColumns.at (column)->set (mRecords.at (index), data);
}
template<typename ESXRecordT> template<typename ESXRecordT>
std::string IdCollection<ESXRecordT>::getTitle (int column) const std::string IdCollection<ESXRecordT>::getTitle (int column) const
{ {
return mColumns.at (column)->mTitle; return mColumns.at (column)->mTitle;
} }
template<typename ESXRecordT>
bool IdCollection<ESXRecordT>::isEditable (int column) const
{
return mColumns.at (column)->isEditable();
}
template<typename ESXRecordT> template<typename ESXRecordT>
void IdCollection<ESXRecordT>::addColumn (Column<ESXRecordT> *column) void IdCollection<ESXRecordT>::addColumn (Column<ESXRecordT> *column)
{ {

View file

@ -31,9 +31,18 @@ int CSMWorld::IdTable::columnCount (const QModelIndex & parent) const
QVariant CSMWorld::IdTable::data (const QModelIndex & index, int role) const QVariant CSMWorld::IdTable::data (const QModelIndex & index, int role) const
{ {
if (role!=Qt::DisplayRole) if (role!=Qt::DisplayRole && role!=Qt::EditRole)
return QVariant(); return QVariant();
if (role==Qt::EditRole)
{
if (index.column()==0)
return QVariant();
if (!mIdCollection->isEditable (index.column()-1))
return QVariant();
}
if (index.column()==0) if (index.column()==0)
return QVariant (tr (mIdCollection->getId (index.row()).c_str())); return QVariant (tr (mIdCollection->getId (index.row()).c_str()));
@ -52,4 +61,26 @@ QVariant CSMWorld::IdTable::headerData (int section, Qt::Orientation orientation
return QVariant (tr ("ID")); return QVariant (tr ("ID"));
return tr (mIdCollection->getTitle (section-1).c_str()); return tr (mIdCollection->getTitle (section-1).c_str());
}
bool CSMWorld::IdTable::setData ( const QModelIndex &index, const QVariant &value, int role)
{
if (index.column()>0 && role==Qt::EditRole)
{
mIdCollection->setData (index.row(), index.column()-1, value);
emit dataChanged (index, index);
return true;
}
return false;
}
Qt::ItemFlags CSMWorld::IdTable::flags (const QModelIndex & index) const
{
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (index.column()>0)
flags |= Qt::ItemIsEditable;
return flags;
} }

View file

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

View file

@ -8,7 +8,7 @@ namespace CSMWorld
template <typename ESXRecordT> template <typename ESXRecordT>
struct Record struct Record
{ {
enum state enum State
{ {
State_BaseOnly, // defined in base only State_BaseOnly, // defined in base only
State_Modified, // exists in base, but has been modified State_Modified, // exists in base, but has been modified
@ -18,7 +18,7 @@ namespace CSMWorld
ESXRecordT mBase; ESXRecordT mBase;
ESXRecordT mModified; ESXRecordT mModified;
state mState; State mState;
bool isDeleted() const; bool isDeleted() const;
@ -27,10 +27,11 @@ namespace CSMWorld
const ESXRecordT& get() const; const ESXRecordT& get() const;
///< Throws an exception, if the record is deleted. ///< Throws an exception, if the record is deleted.
ESXRecordT& get(); const ESXRecordT& getBase() const;
///< Throws an exception, if the record is deleted. ///< Throws an exception, if the record is deleted. Returns modified, if there is no base.
void setModified (const ESXRecordT& modified); void setModified (const ESXRecordT& modified);
///< Throws an exception, if the record is deleted.
}; };
template <typename ESXRecordT> template <typename ESXRecordT>
@ -55,21 +56,24 @@ namespace CSMWorld
} }
template <typename ESXRecordT> template <typename ESXRecordT>
ESXRecordT& Record<ESXRecordT>::get() const ESXRecordT& Record<ESXRecordT>::getBase() const
{ {
if (isDeleted()) if (isDeleted())
throw std::logic_error ("attempt to access a deleted record"); throw std::logic_error ("attempt to access a deleted record");
return mState==State_BaseOnly ? mBase : mModified; return mState==State_ModifiedOnly ? mModified : mBase;
} }
template <typename ESXRecordT> template <typename ESXRecordT>
void Record<ESXRecordT>::setModified (const ESXRecordT& modified) void Record<ESXRecordT>::setModified (const ESXRecordT& modified)
{ {
if (isDeleted())
throw std::logic_error ("attempt to modify a deleted record");
mModified = modified; mModified = modified;
if (mState!=State_ModifiedOnly) if (mState!=State_ModifiedOnly)
mState = State_Modified; mState = mBase==mModified ? State_BaseOnly : State_Modified;
} }
} }

View file

@ -44,4 +44,8 @@ void Global::save(ESMWriter &esm)
esm.writeHNT("FLTV", mValue); esm.writeHNT("FLTV", mValue);
} }
bool operator== (const Global& left, const Global& right)
{
return left.mId==right.mId && left.mValue==right.mValue && left.mType==right.mType;
}
} }

View file

@ -24,5 +24,8 @@ struct Global
void load(ESMReader &esm); void load(ESMReader &esm);
void save(ESMWriter &esm); void save(ESMWriter &esm);
}; };
bool operator== (const Global& left, const Global& right);
} }
#endif #endif