sneedmc/gui/instancemodel.cpp

126 lines
3.1 KiB
C++
Raw Normal View History

2013-03-11 21:19:17 +00:00
#include "instancemodel.h"
#include <instance.h>
#include <QIcon>
2013-03-20 06:59:35 +00:00
#include "iconcache.h"
2013-03-11 21:19:17 +00:00
InstanceModel::InstanceModel ( const InstanceList& instances, QObject *parent )
: QAbstractListModel ( parent ), m_instances ( &instances )
{
2013-03-18 22:00:46 +00:00
currentInstancesNumber = m_instances->count();
connect(m_instances,SIGNAL(instanceAdded(int)),this,SLOT(onInstanceAdded(int)));
connect(m_instances,SIGNAL(instanceChanged(int)),this,SLOT(onInstanceChanged(int)));
connect(m_instances,SIGNAL(invalidated()),this,SLOT(onInvalidated()));
2013-03-11 21:19:17 +00:00
}
2013-03-18 22:00:46 +00:00
void InstanceModel::onInstanceAdded ( int index )
{
beginInsertRows(QModelIndex(), index, index);
currentInstancesNumber ++;
endInsertRows();
}
void InstanceModel::onInstanceChanged ( int index )
{
QModelIndex mx = InstanceModel::index(index);
dataChanged(mx,mx);
2013-03-18 22:00:46 +00:00
}
void InstanceModel::onInvalidated()
{
beginResetModel();
currentInstancesNumber = m_instances->count();
endResetModel();
}
2013-03-11 21:19:17 +00:00
int InstanceModel::rowCount ( const QModelIndex& parent ) const
{
Q_UNUSED ( parent );
return m_instances->count();
}
QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const
{
Q_UNUSED ( parent );
2013-03-18 22:00:46 +00:00
if ( row < 0 || row >= currentInstancesNumber )
2013-03-11 21:19:17 +00:00
return QModelIndex();
return createIndex ( row, column, ( void* ) m_instances->at ( row ).data() );
}
QVariant InstanceModel::data ( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
Instance *pdata = static_cast<Instance*> ( index.internalPointer() );
switch ( role )
{
case InstancePointerRole:
{
QVariant v = qVariantFromValue((void *) pdata);
return v;
}
case Qt::DisplayRole:
{
return pdata->name();
}
case Qt::ToolTipRole:
{
return pdata->rootDir();
}
case Qt::DecorationRole:
{
2013-03-20 06:59:35 +00:00
IconCache * ic = IconCache::instance();
// FIXME: replace with an icon cache/renderer
2013-03-20 06:59:35 +00:00
/*
QString path = ":/icons/instances/";
path += pdata->iconKey();
QIcon icon(path);
2013-03-20 06:59:35 +00:00
*/
QString key = pdata->iconKey();
return ic->getIcon(key);
//else return QIcon(":/icons/multimc/scalable/apps/multimc.svg");
2013-03-11 21:19:17 +00:00
}
// for now.
case KCategorizedSortFilterProxyModel::CategorySortRole:
case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
{
return pdata->group();
2013-03-11 21:19:17 +00:00
}
default:
break;
}
return QVariant();
}
Qt::ItemFlags InstanceModel::flags ( const QModelIndex& index ) const
{
Qt::ItemFlags f;
if ( index.isValid() )
{
f |= ( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
}
return f;
}
InstanceProxyModel::InstanceProxyModel ( QObject *parent )
: KCategorizedSortFilterProxyModel ( parent )
{
// disable since by default we are globally sorting by date:
setCategorizedModel(true);
}
bool InstanceProxyModel::subSortLessThan (
const QModelIndex& left, const QModelIndex& right ) const
{
Instance *pdataLeft = static_cast<Instance*> ( left.internalPointer() );
Instance *pdataRight = static_cast<Instance*> ( right.internalPointer() );
//kDebug() << *pdataLeft << *pdataRight;
return QString::localeAwareCompare(pdataLeft->name(), pdataRight->name()) < 0;
//return pdataLeft->name() < pdataRight->name();
}
#include "instancemodel.moc"