Make EntryAttributesModel editable.

This commit is contained in:
Felix Geyer 2012-04-27 12:35:13 +02:00
parent 7c094d4723
commit 6140a688d7
2 changed files with 64 additions and 10 deletions

View File

@ -83,22 +83,72 @@ QVariant EntryAttributesModel::headerData(int section, Qt::Orientation orientati
QVariant EntryAttributesModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) {
return QVariant();
}
if (role == Qt::DisplayRole) {
QString key = m_attributes.at(index.row());
QString key = m_attributes.at(index.row());
if (index.column() == 0) {
return key;
}
else {
return m_entryAttributes->value(key);
}
if (index.column() == 0) {
return key;
}
else {
return m_entryAttributes->value(key);
}
}
bool EntryAttributesModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!index.isValid() || role != Qt::EditRole || value.type() != QVariant::String
|| value.toString().isEmpty()) {
return false;
}
return QVariant();
QString oldKey = m_attributes.at(index.row());
if (index.column() == 0) {
if (EntryAttributes::isDefaultAttribute(value.toString())) {
return false;
}
m_entryAttributes->rename(oldKey, value.toString());
}
else {
m_entryAttributes->set(oldKey, value.toString());
}
return true;
}
Qt::ItemFlags EntryAttributesModel::flags(const QModelIndex& index) const
{
if (!index.isValid()) {
return Qt::NoItemFlags;
}
else {
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
}
QModelIndex EntryAttributesModel::indexByKey(const QString& key) const
{
int row = m_attributes.indexOf(key);
if (row == -1) {
return QModelIndex();
}
else {
return index(row, 0);
}
}
QString EntryAttributesModel::keyByIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
}
else {
return m_attributes.at(index.row());
}
}
void EntryAttributesModel::attributeChange(QString key)

View File

@ -33,6 +33,10 @@ public:
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex& index) const;
QModelIndex indexByKey(const QString& key) const;
QString keyByIndex(const QModelIndex& index) const;
private Q_SLOTS:
void attributeChange(QString key);