summaryrefslogtreecommitdiffstats
path: root/src/sessiontreemodel.cpp
blob: 4c41d8008deef27d1178f6411eeee7353dc059fe (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#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();
}