summaryrefslogblamecommitdiffstats
path: root/src/sessiontreemodel.cpp
blob: 4c41d8008deef27d1178f6411eeee7353dc059fe (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"
#include "globals.h"

SessionTreeModel::SessionTreeModel(QObject *parent, bool ignoreSections)
    : QAbstractItemModel(parent), ignoreSections_(ignoreSections) {
    root_ = new SessionTreeItem(SECTION_nullptr);
}

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::ForegroundRole)
            return s->foregroundRole();
        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 Qt::NoItemFlags;
    }

    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) {
        SessionTreeItem* parentItem = nullptr;
        SectionType section = s->section();

        if (ignoreSections_) {
            section = SECTION_GENERIC;
        }
        for (int i = 0; i < root_->childCount(); ++i) {
            SessionTreeItem* item = root_->child(i);
            if (item->sectionType() == section) {
                parentItem = item;
                break;
            }
        }

        if (parentItem == nullptr) {
            parentItem = new SessionTreeItem(section, root_);
            root_->appendChild(parentItem);
        }
        parentItem->appendChild(new SessionTreeItem(s, parentItem));
    }
}

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);
        }
    }
}

static void lookDeeper(QList<Session*>& result, QList<QString>& items, SessionTreeItem* root) {
    for (int i = 0; i < root->childCount(); ++i) {
        SessionTreeItem* item = root->child(i);
        if (item->session() != nullptr && item->session()->containsKeywords(items)) {
            result.append(const_cast<Session*>(item->session()));
        }
        lookDeeper(result, items, item);
    }
}

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

    QList<QString> items;
    if (label.startsWith("\"")) {
        QString searchTerm = label;
        searchTerm.remove(0, 1);
        if (label.endsWith("\"")) {
            searchTerm.chop(1);
        }
        items.append(searchTerm);
    } else {
        items = label.split(" ", QString::SkipEmptyParts);
    }
    lookDeeper(result, items, root_);

    return result;

}

QModelIndex SessionTreeModel::getSection(const SectionType type) {
    for (int i = 0; i < root_->childCount(); ++i) {
        SessionTreeItem* item = root_->child(i);
        if (item->sectionType() == type && item->session() == nullptr) {
            return createIndex(i, 0, item);
        }
    }
    return QModelIndex();
}

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