Preserve order of custom icons.

Closes #25
This commit is contained in:
Florian Geyer 2012-05-13 20:50:41 +02:00
parent 459cf051bf
commit a57e8f9864
7 changed files with 46 additions and 27 deletions

View File

@ -175,6 +175,11 @@ QHash<Uuid, QImage> Metadata::customIcons() const
return m_customIcons;
}
QList<Uuid> Metadata::customIconsOrder() const
{
return m_customIconsOrder;
}
bool Metadata::recycleBinEnabled() const
{
return m_recycleBinEnabled;
@ -328,6 +333,8 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
Q_ASSERT(!m_customIcons.contains(uuid));
m_customIcons.insert(uuid, icon);
m_customIconsOrder.append(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();
}
@ -337,6 +344,8 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
Q_ASSERT(m_customIcons.contains(uuid));
m_customIcons.remove(uuid);
m_customIconsOrder.removeAll(uuid);
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
Q_EMIT modified();
}

View File

@ -54,6 +54,7 @@ public:
QImage customIcon(const Uuid& uuid) const;
bool containsCustomIcon(const Uuid& uuid) const;
QHash<Uuid, QImage> customIcons() const;
QList<Uuid> customIconsOrder() const;
bool recycleBinEnabled() const;
Group* recycleBin();
const Group* recycleBin() const;
@ -134,6 +135,7 @@ private:
// bool m_autoEnableVisualHiding;
QHash<Uuid, QImage> m_customIcons;
QList<Uuid> m_customIconsOrder;
bool m_recycleBinEnabled;
QPointer<Group> m_recycleBin;

View File

@ -129,9 +129,10 @@ void KeePass2XmlWriter::writeCustomIcons()
{
m_xml.writeStartElement("CustomIcons");
QHash<Uuid, QImage> customIcons = m_meta->customIcons();
Q_FOREACH (const Uuid& uuid, customIcons.keys()) {
writeIcon(uuid, customIcons.value(uuid));
QListIterator<Uuid> i(m_meta->customIconsOrder());
while (i.hasNext()) {
Uuid uuid = i.next();
writeIcon(uuid, m_meta->customIcon(uuid));
}
m_xml.writeEndElement();

View File

@ -171,7 +171,8 @@ void EditEntryWidget::loadEntry(Entry* entry, bool create, const QString& groupN
if (database) {
m_iconsWidget->setEnabled(true);
m_customIconModel->setIcons(database->metadata()->customIcons());
m_customIconModel->setIcons(database->metadata()->customIcons(),
database->metadata()->customIconsOrder());
Uuid iconUuid = entry->iconUuid();
if (iconUuid.isNull()) {
@ -436,7 +437,8 @@ void EditEntryWidget::addCustomIcon()
QImage image(filename);
if (!image.isNull()) {
m_database->metadata()->addCustomIcon(Uuid::random(), image.scaled(16, 16));
m_customIconModel->setIcons(m_database->metadata()->customIcons());
m_customIconModel->setIcons(m_database->metadata()->customIcons(),
m_database->metadata()->customIconsOrder());
}
else {
// TODO: show error
@ -473,7 +475,8 @@ void EditEntryWidget::removeCustomIcon()
if (iconUsedCount == 0) {
m_database->metadata()->removeCustomIcon(iconUuid);
m_customIconModel->setIcons(m_database->metadata()->customIcons());
m_customIconModel->setIcons(m_database->metadata()->customIcons(),
m_database->metadata()->customIconsOrder());
}
else {
QMessageBox::information(this, tr("Can't delete icon!"),

View File

@ -54,19 +54,13 @@ CustomIconModel::CustomIconModel(QObject* parent) :
{
}
void CustomIconModel::setIcons(QHash<Uuid, QImage> icons)
void CustomIconModel::setIcons(QHash<Uuid, QImage> icons, QList<Uuid> iconsOrder)
{
beginResetModel();
m_icons = icons;
m_uuids.clear();
QHash<Uuid, QImage>::const_iterator iter;
int i = 0;
for (iter = m_icons.constBegin(); iter != m_icons.constEnd(); ++iter) {
m_uuids.insert(i, iter.key());
i++;
}
Q_ASSERT(m_uuids.count() == m_icons.size());
m_iconsOrder = iconsOrder;
Q_ASSERT(m_icons.count() == m_iconsOrder.count());
endResetModel();
}
@ -88,7 +82,8 @@ QVariant CustomIconModel::data(const QModelIndex& index, int role) const
}
if (role == Qt::DecorationRole) {
return m_icons.value(uuidFromIndex(index));
Uuid uuid = m_iconsOrder.value(index.row());
return m_icons.value(uuid);
}
return QVariant();
@ -98,16 +93,16 @@ Uuid CustomIconModel::uuidFromIndex(const QModelIndex& index) const
{
Q_ASSERT(index.isValid());
return m_uuids.value(index.row());
return m_iconsOrder.value(index.row());
}
QModelIndex CustomIconModel::indexFromUuid(const Uuid& uuid) const
{
QHash<int, Uuid>::const_iterator iter;
for (iter = m_uuids.constBegin(); iter != m_uuids.constEnd(); ++iter) {
if (iter.value() == uuid) {
return index(iter.key(), 0);
}
int idx = m_iconsOrder.indexOf(uuid);
if (idx > -1) {
return index(idx, 0);
}
else {
return QModelIndex();
}
}

View File

@ -43,13 +43,13 @@ public:
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
void setIcons(QHash<Uuid, QImage> icons);
void setIcons(QHash<Uuid, QImage> icons, QList<Uuid> iconsOrder);
Uuid uuidFromIndex(const QModelIndex& index) const;
QModelIndex indexFromUuid(const Uuid& uuid) const;
private:
QHash<Uuid, QImage> m_icons;
QHash<int, Uuid> m_uuids;
QList<Uuid> m_iconsOrder;
};
#endif // KEEPASSX_ICONMODELS_H

View File

@ -207,12 +207,21 @@ void TestEntryModel::testCustomIconModel()
QCOMPARE(model->rowCount(), 0);
QHash<Uuid, QImage> icons;
Uuid iconUuid = Uuid::random();
QList<Uuid> iconsOrder;
Uuid iconUuid(QByteArray(16, '2'));
QImage icon;
icons.insert(iconUuid, icon);
iconsOrder << iconUuid;
model->setIcons(icons);
Uuid iconUuid2(QByteArray(16, '1'));
QImage icon2;
icons.insert(iconUuid2, icon2);
iconsOrder << iconUuid2;
model->setIcons(icons, iconsOrder);
QCOMPARE(model->uuidFromIndex(model->index(0, 0)), iconUuid);
QCOMPARE(model->uuidFromIndex(model->index(1, 0)), iconUuid2);
delete modelTest;
delete model;