diff options
Diffstat (limited to 'src/SessionTreeModel.cpp')
-rw-r--r-- | src/SessionTreeModel.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/SessionTreeModel.cpp b/src/SessionTreeModel.cpp new file mode 100644 index 0000000..273d050 --- /dev/null +++ b/src/SessionTreeModel.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2010,2011 - RZ Uni Freiburg + * Copyright (c) 2010,2011 - OpenSLX Project + * + * This program/file is free software distributed under the GPL version 2. + * See http://gpl.openslx.org/ + * + * If you have any feedback please consult http://feedback.openslx.org/ and + * send your feedback to feedback@openslx.org + * + * General information about OpenSLX - libChooser can be found under + * http://openslx.org + * + */ + +#include <QFileInfo> +#include <QIcon> +#include <QResource> +#include <QString> + +#include "SessionTreeItem.h" +#include "SessionTreeModel.h" + + +SessionTreeModel::SessionTreeModel(QObject *parent) + : QAbstractItemModel(parent) { + root_ = new SessionTreeItem("dummy"); +} + +SessionTreeModel::~SessionTreeModel() { + delete root_; +} + +int SessionTreeModel::columnCount(const QModelIndex& /*parent*/) const { + return 1; +} + +int SessionTreeModel::rowCount(const QModelIndex &parent) const { + SessionTreeItem* parentItem; + if (parent.column() > 0) { + return 0; + } + + if (!parent.isValid()) { + parentItem = root_; + } else { + parentItem = static_cast<SessionTreeItem*>(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +QVariant SessionTreeModel::data(const QModelIndex &index, int role) const { + if (!index.isValid()) { + return QVariant(); + } + + SessionTreeItem* item = + static_cast<SessionTreeItem*>(index.internalPointer()); + + const Session* s = item->session(); + if (s) { + if (role == Qt::DisplayRole) + return s->shortDescription(); + if (role == Qt::ToolTipRole) + return s->description(); + if (role == Qt::DecorationRole) { + // TODO: use cache for icons + if (index.column() == 0) { // TODO: is this line needed? + QString icon(s->icon()); + + if (QFileInfo(icon).isAbsolute()) { + // try to load icon from file + return QIcon(icon); + } else { + // try to load icon from QResource + if (QResource(":" + icon.toLower() + ".svg").isValid()) { + return QIcon(":" + icon.toLower() + ".svg"); + } else if (QResource(":icons/" + icon.toLower()).isValid()) { + return QIcon(":icons/" + icon.toLower()); + } else if (QResource(":" + icon.toLower()).isValid()) { + return QIcon(":" + icon.toLower()); + } else { + return QIcon(":none"); + } + } + } + } + } else if (role == Qt::DisplayRole) { + // this is a section item (X Sessions / Virtual Sessions) + return item->text(); + } + + return QVariant(); +} + +Qt::ItemFlags SessionTreeModel::flags(const QModelIndex &index) const { + if (!index.isValid()) { + return 0; + } + + SessionTreeItem* item = + static_cast<SessionTreeItem*>(index.internalPointer()); + + if (item->session()) { + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; + } else { + return Qt::ItemIsEnabled; + } +} + +QVariant SessionTreeModel::headerData(int /*section*/, + Qt::Orientation /*orientation*/, + int /*role*/) const { + return QVariant(); +} + +QModelIndex SessionTreeModel::index(int row, int column, + const QModelIndex &parent) const { + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } + + SessionTreeItem *parentItem; + + if (!parent.isValid()) { + parentItem = root_; + } else { + parentItem = static_cast<SessionTreeItem*>(parent.internalPointer()); + } + + SessionTreeItem *childItem = parentItem->child(row); + if (childItem) { + return createIndex(row, column, childItem); + } else { + return QModelIndex(); + } +} + +QModelIndex SessionTreeModel::parent(const QModelIndex &index) const { + if (!index.isValid()) { + return QModelIndex(); + } + + SessionTreeItem *childItem = + static_cast<SessionTreeItem*>(index.internalPointer()); + SessionTreeItem *parentItem = childItem->parent(); + + if (parentItem == root_) { + return QModelIndex(); + } + + return createIndex(parentItem->row(), 0, parentItem); +} + +void SessionTreeModel::addItems(const QList<Session*>& sessions, + const QString& section) { + SessionTreeItem* parentItem; + + bool sectionExists = false; + + for (int i = 0; i < root_->childCount(); ++i) { + SessionTreeItem* item = root_->child(i); + if (item->text() == section) { + parentItem = item; + sectionExists = true; + break; + } + } + + if (!sectionExists) { + parentItem = new SessionTreeItem(section, root_); + root_->appendChild(parentItem); + } + + foreach (Session* s, sessions) { + parentItem->appendChild(new SessionTreeItem(s, parentItem)); + } +} |