keepassxc/src/gui/IconModels.cpp

114 lines
2.7 KiB
C++
Raw Normal View History

/*
* 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 "IconModels.h"
#include "core/DatabaseIcons.h"
DefaultIconModel::DefaultIconModel(QObject* parent) :
QAbstractListModel(parent)
{
}
int DefaultIconModel::rowCount(const QModelIndex& parent) const
{
2012-05-10 07:59:36 -04:00
if (!parent.isValid()) {
return databaseIcons()->iconCount();
}
else {
return 0;
}
}
QVariant DefaultIconModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
Q_ASSERT(index.row() < databaseIcons()->iconCount());
if (role == Qt::DecorationRole) {
return databaseIcons()->iconPixmap(index.row());
}
return QVariant();
}
CustomIconModel::CustomIconModel(QObject* parent) :
QAbstractListModel(parent)
{
}
void CustomIconModel::setIcons(QHash<Uuid, QImage> icons)
{
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());
endResetModel();
}
int CustomIconModel::rowCount(const QModelIndex& parent) const
{
2012-05-10 07:59:36 -04:00
if (!parent.isValid()) {
return m_icons.size();
}
else {
return 0;
}
}
QVariant CustomIconModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role == Qt::DecorationRole) {
return m_icons.value(uuidFromIndex(index));
}
return QVariant();
}
Uuid CustomIconModel::uuidFromIndex(const QModelIndex& index) const
{
Q_ASSERT(index.isValid());
return m_uuids.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) {
2012-05-10 07:59:36 -04:00
return index(iter.key(), 0);
}
}
return QModelIndex();
}