Connect instance list to model.
This commit is contained in:
parent
b84dfddd1b
commit
65faabeed4
@ -6,8 +6,33 @@ InstanceModel::InstanceModel ( const InstanceList& instances, QObject *parent )
|
|||||||
: QAbstractListModel ( parent ), m_instances ( &instances )
|
: QAbstractListModel ( parent ), m_instances ( &instances )
|
||||||
{
|
{
|
||||||
cachedIcon = QIcon(":/icons/multimc/scalable/apps/multimc.svg");
|
cachedIcon = QIcon(":/icons/multimc/scalable/apps/multimc.svg");
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this doesn't trigger yet
|
||||||
|
void InstanceModel::onInstanceChanged ( int index )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void InstanceModel::onInvalidated()
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
currentInstancesNumber = m_instances->count();
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int InstanceModel::rowCount ( const QModelIndex& parent ) const
|
int InstanceModel::rowCount ( const QModelIndex& parent ) const
|
||||||
{
|
{
|
||||||
Q_UNUSED ( parent );
|
Q_UNUSED ( parent );
|
||||||
@ -17,7 +42,7 @@ int InstanceModel::rowCount ( const QModelIndex& parent ) const
|
|||||||
QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const
|
QModelIndex InstanceModel::index ( int row, int column, const QModelIndex& parent ) const
|
||||||
{
|
{
|
||||||
Q_UNUSED ( parent );
|
Q_UNUSED ( parent );
|
||||||
if ( row < 0 || row >= m_instances->count() )
|
if ( row < 0 || row >= currentInstancesNumber )
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
return createIndex ( row, column, ( void* ) m_instances->at ( row ).data() );
|
return createIndex ( row, column, ( void* ) m_instances->at ( row ).data() );
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,15 @@ public:
|
|||||||
QVariant data ( const QModelIndex& index, int role ) const;
|
QVariant data ( const QModelIndex& index, int role ) const;
|
||||||
Qt::ItemFlags flags ( const QModelIndex& index ) const;
|
Qt::ItemFlags flags ( const QModelIndex& index ) const;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void onInstanceAdded(int index);
|
||||||
|
void onInstanceChanged(int index);
|
||||||
|
void onInvalidated();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const InstanceList* m_instances;
|
const InstanceList* m_instances;
|
||||||
QIcon cachedIcon;
|
QIcon cachedIcon;
|
||||||
|
int currentInstancesNumber;
|
||||||
};
|
};
|
||||||
|
|
||||||
class InstanceProxyModel : public KCategorizedSortFilterProxyModel
|
class InstanceProxyModel : public KCategorizedSortFilterProxyModel
|
||||||
|
@ -64,8 +64,6 @@ MainWindow::MainWindow ( QWidget *parent ) :
|
|||||||
{
|
{
|
||||||
ui->setupUi ( this );
|
ui->setupUi ( this );
|
||||||
// Create the widget
|
// Create the widget
|
||||||
instList.loadList();
|
|
||||||
|
|
||||||
view = new KCategorizedView ( ui->centralWidget );
|
view = new KCategorizedView ( ui->centralWidget );
|
||||||
drawer = new KCategoryDrawer ( view );
|
drawer = new KCategoryDrawer ( view );
|
||||||
|
|
||||||
@ -100,7 +98,9 @@ MainWindow::MainWindow ( QWidget *parent ) :
|
|||||||
view->setModel ( proxymodel );
|
view->setModel ( proxymodel );
|
||||||
connect(view, SIGNAL(doubleClicked(const QModelIndex &)),
|
connect(view, SIGNAL(doubleClicked(const QModelIndex &)),
|
||||||
this, SLOT(instanceActivated(const QModelIndex &)));
|
this, SLOT(instanceActivated(const QModelIndex &)));
|
||||||
|
|
||||||
|
// Load the instances.
|
||||||
|
instList.loadList();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
|
@ -17,16 +17,14 @@
|
|||||||
#define INSTANCELIST_H
|
#define INSTANCELIST_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
|
||||||
#include "siglist.h"
|
#include "instance.h"
|
||||||
|
|
||||||
#include "libmmc_config.h"
|
#include "libmmc_config.h"
|
||||||
|
|
||||||
class Instance;
|
class Instance;
|
||||||
|
|
||||||
class LIBMULTIMC_EXPORT InstanceList : public QObject, public SigList< QSharedPointer<Instance> >
|
class LIBMULTIMC_EXPORT InstanceList : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@ -46,14 +44,43 @@ public:
|
|||||||
QString instDir() const { return m_instDir; }
|
QString instDir() const { return m_instDir; }
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Loads the instance list.
|
* \brief Loads the instance list. Triggers notifications.
|
||||||
*/
|
*/
|
||||||
InstListError loadList();
|
InstListError loadList();
|
||||||
|
|
||||||
DEFINE_SIGLIST_SIGNALS(QSharedPointer<Instance>);
|
/*!
|
||||||
SETUP_SIGLIST_SIGNALS(QSharedPointer<Instance>);
|
* \brief Get the instance at index
|
||||||
|
*/
|
||||||
|
InstancePtr at(int i) const
|
||||||
|
{
|
||||||
|
return m_instances.at(i);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Get the count of loaded instances
|
||||||
|
*/
|
||||||
|
int count() const
|
||||||
|
{
|
||||||
|
return m_instances.count();
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Clear all instances. Triggers notifications.
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
/// Add an instance. Triggers notifications, returns the new index
|
||||||
|
int add(InstancePtr t);
|
||||||
|
|
||||||
|
/// Get an instance by ID
|
||||||
|
InstancePtr getInstanceById (QString id);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void instanceAdded(int index);
|
||||||
|
void instanceChanged(int index);
|
||||||
|
void invalidated();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QString m_instDir;
|
QString m_instDir;
|
||||||
|
QList< InstancePtr > m_instances;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INSTANCELIST_H
|
#endif // INSTANCELIST_H
|
||||||
|
@ -15,11 +15,10 @@
|
|||||||
|
|
||||||
#include "include/instancelist.h"
|
#include "include/instancelist.h"
|
||||||
|
|
||||||
#include "siglist_impl.h"
|
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
#include "include/instance.h"
|
#include "include/instance.h"
|
||||||
#include "include/instanceloader.h"
|
#include "include/instanceloader.h"
|
||||||
@ -37,7 +36,7 @@ InstanceList::InstListError InstanceList::loadList()
|
|||||||
{
|
{
|
||||||
QDir dir(m_instDir);
|
QDir dir(m_instDir);
|
||||||
QDirIterator iter(dir);
|
QDirIterator iter(dir);
|
||||||
|
m_instances.clear();
|
||||||
while (iter.hasNext())
|
while (iter.hasNext())
|
||||||
{
|
{
|
||||||
QString subDir = iter.next();
|
QString subDir = iter.next();
|
||||||
@ -78,10 +77,41 @@ InstanceList::InstListError InstanceList::loadList()
|
|||||||
|
|
||||||
qDebug(QString("Loaded instance %1").arg(inst->name()).toUtf8());
|
qDebug(QString("Loaded instance %1").arg(inst->name()).toUtf8());
|
||||||
inst->setParent(this);
|
inst->setParent(this);
|
||||||
append(QSharedPointer<Instance>(inst));
|
m_instances.append(inst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
emit invalidated();
|
||||||
return NoError;
|
return NoError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clear all instances. Triggers notifications.
|
||||||
|
void InstanceList::clear()
|
||||||
|
{
|
||||||
|
m_instances.clear();
|
||||||
|
emit invalidated();
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Add an instance. Triggers notifications, returns the new index
|
||||||
|
int InstanceList::add(InstancePtr t)
|
||||||
|
{
|
||||||
|
m_instances.append(t);
|
||||||
|
emit instanceAdded(count() - 1);
|
||||||
|
return count() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
InstancePtr InstanceList::getInstanceById(QString instId)
|
||||||
|
{
|
||||||
|
QListIterator<InstancePtr> iter(m_instances);
|
||||||
|
InstancePtr inst;
|
||||||
|
while(iter.hasNext())
|
||||||
|
{
|
||||||
|
inst = iter.next();
|
||||||
|
if (inst->id() == instId)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (inst->id() != instId)
|
||||||
|
return InstancePtr();
|
||||||
|
else
|
||||||
|
return iter.peekPrevious();
|
||||||
|
}
|
@ -31,9 +31,6 @@ include/pathutils.h
|
|||||||
include/osutils.h
|
include/osutils.h
|
||||||
include/userutils.h
|
include/userutils.h
|
||||||
include/cmdutils.h
|
include/cmdutils.h
|
||||||
|
|
||||||
include/siglist.h
|
|
||||||
include/siglist_impl.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
SET(LIBUTIL_SOURCES
|
SET(LIBUTIL_SOURCES
|
||||||
|
19
main.cpp
19
main.cpp
@ -57,23 +57,6 @@ public:
|
|||||||
this->instId = instId;
|
this->instId = instId;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
InstancePtr findInstance(QString instId)
|
|
||||||
{
|
|
||||||
QListIterator<InstancePtr> iter(instances);
|
|
||||||
InstancePtr inst;
|
|
||||||
while(iter.hasNext())
|
|
||||||
{
|
|
||||||
inst = iter.next();
|
|
||||||
if (inst->id() == instId)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (inst->id() != instId)
|
|
||||||
return InstancePtr();
|
|
||||||
else
|
|
||||||
return iter.peekPrevious();
|
|
||||||
}
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onTerminated()
|
void onTerminated()
|
||||||
{
|
{
|
||||||
@ -117,7 +100,7 @@ public:
|
|||||||
instances.loadList();
|
instances.loadList();
|
||||||
|
|
||||||
std::cout << "Launching Instance '" << qPrintable(instId) << "'" << std::endl;
|
std::cout << "Launching Instance '" << qPrintable(instId) << "'" << std::endl;
|
||||||
instance = findInstance(instId);
|
instance = instances.getInstanceById(instId);
|
||||||
if (instance.isNull())
|
if (instance.isNull())
|
||||||
{
|
{
|
||||||
std::cout << "Could not find instance requested. note that you have to specify the ID, not the NAME" << std::endl;
|
std::cout << "Could not find instance requested. note that you have to specify the ID, not the NAME" << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user