summaryrefslogblamecommitdiffstats
path: root/src/sessiontreemodel.cpp
blob: aaf47b93deeb5fdc4ae9be342b389091cc0ed771 (plain) (tree)
1
2
3
4
5
6
7
8
9





                             
                  

                            
                     
                               











































                                                                             
                                                                    
                                 




































































                                                                             
                                                                  
                                    
                                                          

     
 

                                                           

 
                                                        

                                                        

                                                 

                 



                                                                     
 















                                                                                     




                                                                                     

                 
 
                      
 
 



                                     
 
#include "sessiontreemodel.h"

#include <QFileInfo>
#include <QIcon>
#include <QResource>
#include <QString>
#include <QtDebug>

#include "sessiontreeitem.h"
#include "vsession.h"
#include "sessionsiconholder.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) {
            if (index.column() == 0) { // TODO: is this line needed?
            	return s->icon();
            }
        }
    } 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) {
    foreach (Session* s, sessions) {
        root_->appendChild(new SessionTreeItem(s, root_));
    }
}

void SessionTreeModel::addLabelItem(const QString& label) {
    root_->appendChild(new SessionTreeItem(label, root_));
}

void SessionTreeModel::removeItem(const QString& name) {
	for (int i = 0; i < root_->childCount(); ++i) {
		SessionTreeItem* item = root_->child(i);
		if (item->text() == name) {
			root_->removeChild(item);
		}
	}
}

QList<Session*> SessionTreeModel::lookForItem(const QString& label) {
	QList<Session*> result;

	if (label.startsWith("\"")) {
		QString searchTerm = label;
		searchTerm.remove(0, 1);
		if (label.endsWith("\"")) {
			searchTerm.chop(1);
		}
		QList<QString> items;
		items.append(searchTerm);
		for (int i = 0; i < root_->childCount(); ++i) {
			SessionTreeItem* item = root_->child(i);
			if (item->session()->containsKeywords(items)) {
				result.append(const_cast<Session*>(item->session()));
			}
		}
	} else {
		QList<QString> items = label.split(" ", QString::SkipEmptyParts);
		for (int i = 0; i < root_->childCount(); ++i) {
			SessionTreeItem* item = root_->child(i);
			if (item->session()->containsKeywords(items)) {
				result.append(const_cast<Session*>(item->session()));
			}
		}
	}

	return result;

}

void SessionTreeModel::updateView() {
	emit layoutChanged();
}