Newer
Older
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
#include "MantidQtWidgets/Common/Batch/JobTreeView.h"
#include "MantidQtWidgets/Common/Batch/QtTreeCursorNavigation.h"
#include "MantidQtWidgets/Common/Batch/AssertOrThrow.h"
#include <QKeyEvent>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
namespace MantidQt {
namespace MantidWidgets {
namespace Batch {
JobTreeView::JobTreeView(QStringList const &columnHeadings, QWidget *parent)
: QTreeView(parent), m_model(this) {
setModel(&m_model);
setHeaderLabels(columnHeadings);
}
void JobTreeView::setHeaderLabels(QStringList const &columnHeadings) {
m_model.setHorizontalHeaderLabels(columnHeadings);
for (auto i = 0; i < model()->columnCount(); ++i)
resizeColumnToContents(i);
}
void JobTreeView::subscribe(IJobTreeViewSubscriber &subscriber) {
m_notifyee = &subscriber;
}
QModelIndex JobTreeView::modelIndexAt(RowLocation const &location,
int column) const {
auto parentIndex = adaptedModel().rootModelIndex();
if (location.isRoot()) {
return parentIndex;
} else {
auto &path = location.path();
for (auto it = path.cbegin(); it != path.cend() - 1; ++it)
parentIndex = model()->index(*it, column, parentIndex);
assertOrThrow(
model()->hasIndex(location.rowRelativeToParent(), 0, parentIndex),
"modelIndexAt: Location refers to an index which does not "
"exist in the model.");
return model()->index(location.rowRelativeToParent(), column, parentIndex);
}
}
RowLocation JobTreeView::rowLocationAt(QModelIndex const &index) const {
if (index.isValid()) {
auto pathComponents = RowPath();
auto currentIndex = index;
while (currentIndex.isValid()) {
pathComponents.insert(pathComponents.begin(), currentIndex.row());
currentIndex = index.parent();
}
return RowLocation(pathComponents);
} else {
return RowLocation({});
}
}
void JobTreeView::removeRowAt(RowLocation const &location) {
adaptedModel().removeRowAt(modelIndexAt(location));
}
void JobTreeView::insertChildRowOf(RowLocation const &parent, int beforeRow) {
adaptedModel().insertEmptyChildRow(modelIndexAt(parent), beforeRow);
}
void JobTreeView::insertChildRowOf(RowLocation const &parent, int beforeRow,
std::vector<std::string> const &rowText) {
adaptedModel().insertChildRow(modelIndexAt(parent), beforeRow,
adaptedModel().rowFromRowText(rowText));
}
void JobTreeView::appendChildRowOf(RowLocation const &parent) {
adaptedModel().appendEmptyChildRow(modelIndexAt(parent));
}
void JobTreeView::appendChildRowOf(RowLocation const &parent,
std::vector<std::string> const &rowText) {
adaptedModel().appendChildRow(modelIndexAt(parent),
adaptedModel().rowFromRowText(rowText));
}
QModelIndex JobTreeView::editAt(QModelIndex const &index) {
clearSelection();
setCurrentIndex(index);
edit(index);
return index;
}
QModelIndex JobTreeView::expanded(QModelIndex const &index) {
auto expandAt = index;
while (expandAt.isValid()) {
setExpanded(expandAt, true);
expandAt = model()->parent(expandAt);
}
return index;
}
QtStandardItemMutableTreeAdapter JobTreeView::adaptedModel() {
return QtStandardItemMutableTreeAdapter(m_model);
}
QtStandardItemTreeAdapter const JobTreeView::adaptedModel() const {
return QtStandardItemTreeAdapter(m_model);
}
QtTreeCursorNavigation JobTreeView::navigation() const {
return QtTreeCursorNavigation(model());
}
QModelIndex JobTreeView::findOrMakeCellBelow(QModelIndex const &index) {
if (navigation().isNotLastRowInThisNode(index)) {
return moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier);
} else {
return adaptedModel().appendEmptySiblingRow(index);
}
}
void JobTreeView::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return) {
event->accept();
if (event->modifiers() & Qt::ControlModifier)
editAt(adaptedModel().appendEmptyChildRow(currentIndex()));
else {
auto below = findOrMakeCellBelow(currentIndex());
editAt(expanded(below));
}
} else {
QTreeView::keyPressEvent(event);
}
}
QModelIndex
JobTreeView::applyNavigationResult(QtTreeCursorNavigationResult const &result) {
auto shouldMakeNewRowBelow = result.first;
if (shouldMakeNewRowBelow)
return expanded(adaptedModel().appendEmptySiblingRow(result.second));
else
return result.second;
}
QModelIndex JobTreeView::moveCursor(CursorAction cursorAction,
Qt::KeyboardModifiers modifiers) {
if (cursorAction == QAbstractItemView::MoveNext) {
return applyNavigationResult(navigation().moveCursorNext(currentIndex()));
} else if (cursorAction == QAbstractItemView::MovePrevious) {
return navigation().moveCursorPrevious(currentIndex());
} else {
return QTreeView::moveCursor(cursorAction, modifiers);
}
}
} // namespace Batch
} // namespace MantidWidgets
} // namespace MantidQt