2012-04-14 09:41:57 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EntryAttributesModel.h"
|
|
|
|
|
|
|
|
#include "core/Entry.h"
|
|
|
|
#include "core/Tools.h"
|
|
|
|
|
|
|
|
EntryAttributesModel::EntryAttributesModel(QObject* parent)
|
2012-04-28 10:50:17 -04:00
|
|
|
: QAbstractListModel(parent)
|
2012-04-14 09:41:57 -04:00
|
|
|
, m_entryAttributes(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::setEntryAttributes(EntryAttributes* entryAttributes)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
if (m_entryAttributes) {
|
|
|
|
m_entryAttributes->disconnect(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_entryAttributes = entryAttributes;
|
|
|
|
|
|
|
|
if (m_entryAttributes) {
|
|
|
|
updateAttributes();
|
|
|
|
connect(m_entryAttributes, SIGNAL(customKeyModified(QString)), SLOT(attributeChange(QString)));
|
|
|
|
connect(m_entryAttributes, SIGNAL(aboutToBeAdded(QString)), SLOT(attributeAboutToAdd(QString)));
|
|
|
|
connect(m_entryAttributes, SIGNAL(added(QString)), SLOT(attributeAdd()));
|
|
|
|
connect(m_entryAttributes, SIGNAL(aboutToBeRemoved(QString)), SLOT(attributeAboutToRemove(QString)));
|
|
|
|
connect(m_entryAttributes, SIGNAL(removed(QString)), SLOT(attributeRemove()));
|
|
|
|
connect(m_entryAttributes, SIGNAL(aboutToBeReset()), SLOT(aboutToReset()));
|
|
|
|
connect(m_entryAttributes, SIGNAL(reset()), SLOT(reset()));
|
|
|
|
}
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int EntryAttributesModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (!m_entryAttributes || parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_attributes.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int EntryAttributesModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
2012-04-28 10:50:17 -04:00
|
|
|
return 1;
|
2012-04-14 09:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant EntryAttributesModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2012-04-28 10:50:17 -04:00
|
|
|
if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole) && (section == 0)) {
|
|
|
|
return tr("Name");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
2012-04-14 09:41:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant EntryAttributesModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
2012-04-27 06:35:13 -04:00
|
|
|
if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) {
|
2012-04-14 09:41:57 -04:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2012-04-28 10:50:17 -04:00
|
|
|
return m_attributes.at(index.row());
|
2012-04-27 06:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString oldKey = m_attributes.at(index.row());
|
2012-04-28 10:50:17 -04:00
|
|
|
QString newKey = value.toString();
|
2012-04-27 06:35:13 -04:00
|
|
|
|
2012-04-28 10:50:17 -04:00
|
|
|
if (EntryAttributes::isDefaultAttribute(newKey)
|
|
|
|
|| m_entryAttributes->keys().contains(newKey)) {
|
|
|
|
return false;
|
2012-04-14 09:41:57 -04:00
|
|
|
}
|
2012-04-28 10:50:17 -04:00
|
|
|
m_entryAttributes->rename(oldKey, newKey);
|
2012-04-14 09:41:57 -04:00
|
|
|
|
2012-04-27 06:35:13 -04:00
|
|
|
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());
|
|
|
|
}
|
2012-04-14 09:41:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::attributeChange(QString key)
|
|
|
|
{
|
|
|
|
int row = m_attributes.indexOf(key);
|
|
|
|
Q_ASSERT(row != -1);
|
|
|
|
Q_EMIT dataChanged(index(row, 0), index(row, columnCount()-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::attributeAboutToAdd(QString key)
|
|
|
|
{
|
|
|
|
QList<QString> rows = m_attributes;
|
|
|
|
rows.append(key);
|
|
|
|
qSort(rows);
|
|
|
|
int row = rows.indexOf(key);
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::attributeAdd()
|
|
|
|
{
|
|
|
|
updateAttributes();
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::attributeAboutToRemove(QString key)
|
|
|
|
{
|
|
|
|
int row = m_attributes.indexOf(key);
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::attributeRemove()
|
|
|
|
{
|
|
|
|
updateAttributes();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::aboutToReset()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::reset()
|
|
|
|
{
|
2012-04-28 10:50:51 -04:00
|
|
|
updateAttributes();
|
2012-04-14 09:41:57 -04:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntryAttributesModel::updateAttributes()
|
|
|
|
{
|
|
|
|
m_attributes.clear();
|
|
|
|
|
|
|
|
Q_FOREACH (const QString& key, m_entryAttributes->keys()) {
|
|
|
|
if (!EntryAttributes::isDefaultAttribute(key)) {
|
|
|
|
m_attributes.append(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|