2013-03-11 21:19:17 +00:00
|
|
|
#include "instancemodel.h"
|
2013-08-17 12:40:51 +01:00
|
|
|
#include <logic/BaseInstance.h>
|
|
|
|
#include <logic/IconListModel.h>
|
2013-03-11 21:19:17 +00:00
|
|
|
#include <QIcon>
|
2013-08-10 17:34:08 +01: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 )
|
|
|
|
{
|
2013-03-19 05:24:34 +00:00
|
|
|
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();
|
|
|
|
}
|
2013-07-28 23:59:35 +01:00
|
|
|
BaseInstance *pdata = static_cast<BaseInstance*> ( index.internalPointer() );
|
2013-03-11 21:19:17 +00:00
|
|
|
switch ( role )
|
|
|
|
{
|
|
|
|
case InstancePointerRole:
|
|
|
|
{
|
|
|
|
QVariant v = qVariantFromValue((void *) pdata);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
return pdata->name();
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
2013-08-11 23:39:19 +01:00
|
|
|
return pdata->instanceRoot();
|
2013-03-11 21:19:17 +00:00
|
|
|
}
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
{
|
2013-08-10 17:34:08 +01:00
|
|
|
IconList * ic = IconList::instance();
|
2013-03-19 05:24:34 +00:00
|
|
|
// FIXME: replace with an icon cache/renderer
|
2013-03-20 06:59:35 +00:00
|
|
|
/*
|
2013-03-19 05:24:34 +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);
|
2013-03-19 05:24:34 +00:00
|
|
|
//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:
|
|
|
|
{
|
2013-03-18 22:35:54 +00:00
|
|
|
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
|
|
|
|
{
|
2013-07-28 23:59:35 +01:00
|
|
|
BaseInstance *pdataLeft = static_cast<BaseInstance*> ( left.internalPointer() );
|
|
|
|
BaseInstance *pdataRight = static_cast<BaseInstance*> ( right.internalPointer() );
|
2013-03-11 21:19:17 +00:00
|
|
|
//kDebug() << *pdataLeft << *pdataRight;
|
|
|
|
return QString::localeAwareCompare(pdataLeft->name(), pdataRight->name()) < 0;
|
|
|
|
//return pdataLeft->name() < pdataRight->name();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "instancemodel.moc"
|
|
|
|
|