2012-05-09 04:46:22 -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 "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;
|
|
|
|
}
|
2012-05-09 04:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-13 14:50:41 -04:00
|
|
|
void CustomIconModel::setIcons(QHash<Uuid, QImage> icons, QList<Uuid> iconsOrder)
|
2012-05-09 04:46:22 -04:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
m_icons = icons;
|
2012-05-13 14:50:41 -04:00
|
|
|
m_iconsOrder = iconsOrder;
|
|
|
|
Q_ASSERT(m_icons.count() == m_iconsOrder.count());
|
2012-05-09 04:46:22 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-05-09 04:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant CustomIconModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == Qt::DecorationRole) {
|
2012-05-13 15:27:56 -04:00
|
|
|
Uuid uuid = uuidFromIndex(index);
|
2012-05-13 14:50:41 -04:00
|
|
|
return m_icons.value(uuid);
|
2012-05-09 04:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Uuid CustomIconModel::uuidFromIndex(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(index.isValid());
|
|
|
|
|
2012-05-13 14:50:41 -04:00
|
|
|
return m_iconsOrder.value(index.row());
|
2012-05-09 04:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex CustomIconModel::indexFromUuid(const Uuid& uuid) const
|
|
|
|
{
|
2012-05-13 14:50:41 -04:00
|
|
|
int idx = m_iconsOrder.indexOf(uuid);
|
|
|
|
if (idx > -1) {
|
|
|
|
return index(idx, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QModelIndex();
|
2012-05-09 04:46:22 -04:00
|
|
|
}
|
|
|
|
}
|