summaryrefslogtreecommitdiffstats
path: root/gui/instancemodel.cpp
blob: 3cbb0fb9305f8c5ae53af19913fa166179066519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "instancemodel.h"
#include <BaseInstance.h>
#include <IconListModel.h>
#include <QIcon>
//#include "iconcache.h"

InstanceModel::InstanceModel ( const InstanceList& instances, QObject *parent )
	: QAbstractListModel ( parent ), m_instances ( &instances )
{
	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()));
}

void InstanceModel::onInstanceAdded ( int index )
{
	beginInsertRows(QModelIndex(), index, index);
	currentInstancesNumber ++;
	endInsertRows();
}

void InstanceModel::onInstanceChanged ( int index )
{
	QModelIndex mx = InstanceModel::index(index);
	dataChanged(mx,mx);
}

void InstanceModel::onInvalidated()
{
	beginResetModel();
	currentInstancesNumber = m_instances->count();
	endResetModel();
}


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 );
	if ( row < 0 || row >= currentInstancesNumber )
		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();
	}
	BaseInstance *pdata = static_cast<BaseInstance*> ( index.internalPointer() );
	switch ( role )
	{
	case InstancePointerRole:
	{
		QVariant v = qVariantFromValue((void *) pdata);
		return v;
	}
	case Qt::DisplayRole:
	{
		return pdata->name();
	}
	case Qt::ToolTipRole:
	{
		return pdata->instanceRoot();
	}
	case Qt::DecorationRole:
	{
		IconList * ic = IconList::instance();
		// FIXME: replace with an icon cache/renderer
		/*
		QString path = ":/icons/instances/";
		path += pdata->iconKey();
		QIcon icon(path);
		*/
		QString key = pdata->iconKey();
		return ic->getIcon(key);
		//else return QIcon(":/icons/multimc/scalable/apps/multimc.svg");
	}
	// for now.
	case KCategorizedSortFilterProxyModel::CategorySortRole:
	case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
	{
		return pdata->group();
	}
	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
{
	BaseInstance *pdataLeft = static_cast<BaseInstance*> ( left.internalPointer() );
	BaseInstance *pdataRight = static_cast<BaseInstance*> ( right.internalPointer() );
	//kDebug() << *pdataLeft << *pdataRight;
	return QString::localeAwareCompare(pdataLeft->name(), pdataRight->name()) < 0;
	//return pdataLeft->name() < pdataRight->name();
}

#include "instancemodel.moc"