pollymc/gui/instancemodel.cpp

126 lines
3.1 KiB
C++
Raw Normal View History

2013-03-12 02:49:17 +05:30
#include "instancemodel.h"
#include <instance.h>
#include <QIcon>
2013-03-20 12:29:35 +05:30
#include "iconcache.h"
2013-03-12 02:49:17 +05:30
InstanceModel::InstanceModel ( const InstanceList& instances, QObject *parent )
: QAbstractListModel ( parent ), m_instances ( &instances )
{
2013-03-19 03:30:46 +05:30
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-12 02:49:17 +05:30
}
2013-03-19 03:30:46 +05:30
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-19 03:30:46 +05:30
}
void InstanceModel::onInvalidated()
{
beginResetModel();
currentInstancesNumber = m_instances->count();
endResetModel();
}
2013-03-12 02:49:17 +05:30
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-19 03:30:46 +05:30
if ( row < 0 || row >= currentInstancesNumber )
2013-03-12 02:49:17 +05:30
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 12:29:35 +05:30
IconCache * ic = IconCache::instance();
// FIXME: replace with an icon cache/renderer
2013-03-20 12:29:35 +05:30
/*
QString path = ":/icons/instances/";
path += pdata->iconKey();
QIcon icon(path);
2013-03-20 12:29:35 +05:30
*/
QString key = pdata->iconKey();
return ic->getIcon(key);
//else return QIcon(":/icons/multimc/scalable/apps/multimc.svg");
2013-03-12 02:49:17 +05:30
}
// for now.
case KCategorizedSortFilterProxyModel::CategorySortRole:
case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
{
return pdata->group();
2013-03-12 02:49:17 +05:30
}
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"