@@ -1,108 +1,112 | |||
|
1 | 1 | #ifndef SCIQLOP_SQPITERATOR_H |
|
2 | 2 | #define SCIQLOP_SQPITERATOR_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | |
|
6 | 6 | /** |
|
7 | 7 | * @brief The SqpIterator class represents an iterator used in SciQlop. It defines all operators |
|
8 | 8 | * needed for a standard forward iterator |
|
9 | 9 | * @tparam T the type of object handled in iterator |
|
10 | 10 | * @sa http://www.cplusplus.com/reference/iterator/ |
|
11 | 11 | */ |
|
12 | 12 | template <typename T> |
|
13 | 13 | class SCIQLOP_CORE_EXPORT SqpIterator { |
|
14 | 14 | public: |
|
15 | 15 | using iterator_category = std::random_access_iterator_tag; |
|
16 | 16 | using value_type = const T; |
|
17 | 17 | using difference_type = std::ptrdiff_t; |
|
18 | 18 | using pointer = value_type *; |
|
19 | 19 | using reference = value_type &; |
|
20 | 20 | |
|
21 | 21 | explicit SqpIterator(T value) : m_CurrentValue{std::move(value)} {} |
|
22 | 22 | |
|
23 | 23 | virtual ~SqpIterator() noexcept = default; |
|
24 | 24 | SqpIterator(const SqpIterator &) = default; |
|
25 |
SqpIterator &operator=(SqpIterator other) |
|
|
25 | SqpIterator &operator=(SqpIterator other) | |
|
26 | { | |
|
27 | swap(m_CurrentValue, other.m_CurrentValue); | |
|
28 | return *this; | |
|
29 | } | |
|
26 | 30 | |
|
27 | 31 | SqpIterator &operator++() |
|
28 | 32 | { |
|
29 | 33 | m_CurrentValue.next(); |
|
30 | 34 | return *this; |
|
31 | 35 | } |
|
32 | 36 | |
|
33 | 37 | SqpIterator &operator--() |
|
34 | 38 | { |
|
35 | 39 | m_CurrentValue.prev(); |
|
36 | 40 | return *this; |
|
37 | 41 | } |
|
38 | 42 | |
|
39 | 43 | SqpIterator operator++(int)const |
|
40 | 44 | { |
|
41 | 45 | auto result = *this; |
|
42 | 46 | this->operator++(); |
|
43 | 47 | return result; |
|
44 | 48 | } |
|
45 | 49 | SqpIterator operator--(int)const |
|
46 | 50 | { |
|
47 | 51 | auto result = *this; |
|
48 | 52 | this->operator--(); |
|
49 | 53 | return result; |
|
50 | 54 | } |
|
51 | 55 | |
|
52 | 56 | SqpIterator &operator+=(int offset) |
|
53 | 57 | { |
|
54 | 58 | if (offset >= 0) { |
|
55 | 59 | m_CurrentValue.next(offset); |
|
56 | 60 | } |
|
57 | 61 | else { |
|
58 | 62 | while (offset++) { |
|
59 | 63 | m_CurrentValue.prev(); |
|
60 | 64 | } |
|
61 | 65 | } |
|
62 | 66 | |
|
63 | 67 | return *this; |
|
64 | 68 | } |
|
65 | 69 | SqpIterator &operator-=(int offset) { return *this += -offset; } |
|
66 | 70 | |
|
67 | 71 | SqpIterator operator+(int offset) const |
|
68 | 72 | { |
|
69 | 73 | auto result = *this; |
|
70 | 74 | result += offset; |
|
71 | 75 | return result; |
|
72 | 76 | } |
|
73 | 77 | SqpIterator operator-(int offset) const |
|
74 | 78 | { |
|
75 | 79 | auto result = *this; |
|
76 | 80 | result -= offset; |
|
77 | 81 | return result; |
|
78 | 82 | } |
|
79 | 83 | |
|
80 | 84 | int operator-(const SqpIterator &other) const |
|
81 | 85 | { |
|
82 | 86 | return m_CurrentValue.distance(other.m_CurrentValue); |
|
83 | 87 | } |
|
84 | 88 | |
|
85 | 89 | const T *operator->() const { return &m_CurrentValue; } |
|
86 | 90 | const T &operator*() const { return m_CurrentValue; } |
|
87 | 91 | T *operator->() { return &m_CurrentValue; } |
|
88 | 92 | T &operator*() { return m_CurrentValue; } |
|
89 | 93 | T &operator[](int offset) const { return m_CurrentValue.advance(offset); } |
|
90 | 94 | |
|
91 | 95 | bool operator==(const SqpIterator &other) const |
|
92 | 96 | { |
|
93 | 97 | return m_CurrentValue.equals(other.m_CurrentValue); |
|
94 | 98 | } |
|
95 | 99 | bool operator!=(const SqpIterator &other) const { return !(*this == other); } |
|
96 | 100 | bool operator>(const SqpIterator &other) const { return other.m_CurrentValue.lowerThan(*this); } |
|
97 | 101 | bool operator<(const SqpIterator &other) const |
|
98 | 102 | { |
|
99 | 103 | return m_CurrentValue.lowerThan(other.m_CurrentValue); |
|
100 | 104 | } |
|
101 | 105 | bool operator>=(const SqpIterator &other) const { return !(*this < other); } |
|
102 | 106 | bool operator<=(const SqpIterator &other) const { return !(*this > other); } |
|
103 | 107 | |
|
104 | 108 | private: |
|
105 | 109 | T m_CurrentValue; |
|
106 | 110 | }; |
|
107 | 111 | |
|
108 | 112 | #endif // SCIQLOP_SQPITERATOR_H |
@@ -1,84 +1,83 | |||
|
1 | 1 | #include "Data/DataSeriesIterator.h" |
|
2 | 2 | |
|
3 | 3 | DataSeriesIteratorValue::DataSeriesIteratorValue( |
|
4 | 4 | std::unique_ptr<DataSeriesIteratorValue::Impl> impl) |
|
5 | 5 | : m_Impl{std::move(impl)} |
|
6 | 6 | { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | 9 | DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other) |
|
10 | 10 | : m_Impl{other.m_Impl->clone()} |
|
11 | 11 | { |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other) |
|
15 | 15 | { |
|
16 | 16 | m_Impl->swap(*other.m_Impl); |
|
17 | 17 | return *this; |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | int DataSeriesIteratorValue::distance(const DataSeriesIteratorValue &other) const |
|
21 | 21 | { |
|
22 | auto dist = m_Impl->distance(*other.m_Impl); | |
|
23 | 22 | return m_Impl->distance(*other.m_Impl); |
|
24 | 23 | } |
|
25 | 24 | |
|
26 | 25 | bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const |
|
27 | 26 | { |
|
28 | 27 | return m_Impl->equals(*other.m_Impl); |
|
29 | 28 | } |
|
30 | 29 | |
|
31 | 30 | bool DataSeriesIteratorValue::lowerThan(const DataSeriesIteratorValue &other) const |
|
32 | 31 | { |
|
33 | 32 | return m_Impl->lowerThan(*other.m_Impl); |
|
34 | 33 | } |
|
35 | 34 | |
|
36 | 35 | DataSeriesIteratorValue DataSeriesIteratorValue::advance(int offset) const |
|
37 | 36 | { |
|
38 | 37 | return DataSeriesIteratorValue{m_Impl->advance(offset)}; |
|
39 | 38 | } |
|
40 | 39 | |
|
41 | 40 | void DataSeriesIteratorValue::next(int offset) |
|
42 | 41 | { |
|
43 | 42 | m_Impl->next(offset); |
|
44 | 43 | } |
|
45 | 44 | |
|
46 | 45 | void DataSeriesIteratorValue::prev() |
|
47 | 46 | { |
|
48 | 47 | m_Impl->prev(); |
|
49 | 48 | } |
|
50 | 49 | |
|
51 | 50 | double DataSeriesIteratorValue::x() const |
|
52 | 51 | { |
|
53 | 52 | return m_Impl->x(); |
|
54 | 53 | } |
|
55 | 54 | |
|
56 | 55 | double DataSeriesIteratorValue::value() const |
|
57 | 56 | { |
|
58 | 57 | return m_Impl->value(); |
|
59 | 58 | } |
|
60 | 59 | |
|
61 | 60 | double DataSeriesIteratorValue::value(int componentIndex) const |
|
62 | 61 | { |
|
63 | 62 | return m_Impl->value(componentIndex); |
|
64 | 63 | } |
|
65 | 64 | |
|
66 | 65 | double DataSeriesIteratorValue::minValue() const |
|
67 | 66 | { |
|
68 | 67 | return m_Impl->minValue(); |
|
69 | 68 | } |
|
70 | 69 | |
|
71 | 70 | double DataSeriesIteratorValue::maxValue() const |
|
72 | 71 | { |
|
73 | 72 | return m_Impl->maxValue(); |
|
74 | 73 | } |
|
75 | 74 | |
|
76 | 75 | QVector<double> DataSeriesIteratorValue::values() const |
|
77 | 76 | { |
|
78 | 77 | return m_Impl->values(); |
|
79 | 78 | } |
|
80 | 79 | |
|
81 | 80 | DataSeriesIteratorValue::Impl *DataSeriesIteratorValue::impl() |
|
82 | 81 | { |
|
83 | 82 | return m_Impl.get(); |
|
84 | 83 | } |
@@ -1,228 +1,232 | |||
|
1 | 1 | #include <Variable/RenameVariableDialog.h> |
|
2 | 2 | #include <Variable/Variable.h> |
|
3 | 3 | #include <Variable/VariableController.h> |
|
4 | 4 | #include <Variable/VariableInspectorWidget.h> |
|
5 | 5 | #include <Variable/VariableMenuHeaderWidget.h> |
|
6 | 6 | #include <Variable/VariableModel.h> |
|
7 | 7 | |
|
8 | 8 | #include <ui_VariableInspectorWidget.h> |
|
9 | 9 | |
|
10 | 10 | #include <QMouseEvent> |
|
11 | 11 | #include <QSortFilterProxyModel> |
|
12 | 12 | #include <QStyledItemDelegate> |
|
13 | 13 | #include <QWidgetAction> |
|
14 | 14 | |
|
15 | 15 | #include <SqpApplication.h> |
|
16 | 16 | |
|
17 | 17 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | class QProgressBarItemDelegate : public QStyledItemDelegate { |
|
21 | 21 | |
|
22 | 22 | public: |
|
23 | 23 | QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {} |
|
24 | 24 | |
|
25 | 25 | void paint(QPainter *painter, const QStyleOptionViewItem &option, |
|
26 | 26 | const QModelIndex &index) const |
|
27 | 27 | { |
|
28 | 28 | auto data = index.data(Qt::DisplayRole); |
|
29 | 29 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
30 | 30 | if (data.isValid() && progressData.isValid()) { |
|
31 | 31 | auto name = data.value<QString>(); |
|
32 | 32 | auto progress = progressData.value<double>(); |
|
33 | 33 | if (progress > 0) { |
|
34 | 34 | auto cancelButtonWidth = 20; |
|
35 | 35 | auto progressBarOption = QStyleOptionProgressBar{}; |
|
36 | 36 | auto progressRect = option.rect; |
|
37 | 37 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
38 | 38 | progressBarOption.rect = progressRect; |
|
39 | 39 | progressBarOption.minimum = 0; |
|
40 | 40 | progressBarOption.maximum = 100; |
|
41 | 41 | progressBarOption.progress = progress; |
|
42 | 42 | progressBarOption.text |
|
43 | 43 | = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%"); |
|
44 | 44 | progressBarOption.textVisible = true; |
|
45 | 45 | progressBarOption.textAlignment = Qt::AlignCenter; |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, |
|
49 | 49 | painter); |
|
50 | 50 | |
|
51 | 51 | // Cancel button |
|
52 | 52 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
53 | 53 | option.rect.height()); |
|
54 | 54 | auto buttonOption = QStyleOptionButton{}; |
|
55 | 55 | buttonOption.rect = buttonRect; |
|
56 | 56 | buttonOption.text = "X"; |
|
57 | 57 | |
|
58 | 58 | QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); |
|
59 | 59 | } |
|
60 | 60 | else { |
|
61 | 61 | QStyledItemDelegate::paint(painter, option, index); |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | else { |
|
65 | 65 | QStyledItemDelegate::paint(painter, option, index); |
|
66 | 66 | } |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, |
|
70 | 70 | const QModelIndex &index) |
|
71 | 71 | { |
|
72 | 72 | if (event->type() == QEvent::MouseButtonRelease) { |
|
73 | 73 | auto data = index.data(Qt::DisplayRole); |
|
74 | 74 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
75 | 75 | if (data.isValid() && progressData.isValid()) { |
|
76 | 76 | auto cancelButtonWidth = 20; |
|
77 | 77 | auto progressRect = option.rect; |
|
78 | 78 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
79 | 79 | // Cancel button |
|
80 | 80 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
81 | 81 | option.rect.height()); |
|
82 | 82 | |
|
83 | 83 | auto e = (QMouseEvent *)event; |
|
84 | 84 | auto clickX = e->x(); |
|
85 | 85 | auto clickY = e->y(); |
|
86 | 86 | |
|
87 | 87 | auto x = buttonRect.left(); // the X coordinate |
|
88 | 88 | auto y = buttonRect.top(); // the Y coordinate |
|
89 | 89 | auto w = buttonRect.width(); // button width |
|
90 | 90 | auto h = buttonRect.height(); // button height |
|
91 | 91 | |
|
92 | 92 | if (clickX > x && clickX < x + w) { |
|
93 | 93 | if (clickY > y && clickY < y + h) { |
|
94 | 94 | auto variableModel = sqpApp->variableController().variableModel(); |
|
95 | 95 | variableModel->abortProgress(index); |
|
96 | 96 | } |
|
97 | return true; | |
|
97 | 98 | } |
|
98 | 99 | else { |
|
99 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
100 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
100 | 101 | } |
|
101 | 102 | } |
|
102 | 103 | else { |
|
103 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
104 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
104 | 105 | } |
|
105 | 106 | } |
|
106 | 107 | else { |
|
107 | QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
108 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
108 | 109 | } |
|
110 | ||
|
111 | ||
|
112 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |
|
109 | 113 | } |
|
110 | 114 | }; |
|
111 | 115 | |
|
112 | 116 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) |
|
113 | 117 | : QWidget{parent}, |
|
114 | 118 | ui{new Ui::VariableInspectorWidget}, |
|
115 | 119 | m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}} |
|
116 | 120 | { |
|
117 | 121 | ui->setupUi(this); |
|
118 | 122 | |
|
119 | 123 | // Sets model for table |
|
120 | 124 | // auto sortFilterModel = new QSortFilterProxyModel{this}; |
|
121 | 125 | // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); |
|
122 | 126 | |
|
123 | 127 | auto variableModel = sqpApp->variableController().variableModel(); |
|
124 | 128 | ui->tableView->setModel(variableModel); |
|
125 | 129 | |
|
126 | 130 | // Adds extra signal/slot between view and model, so the view can be updated instantly when |
|
127 | 131 | // there is a change of data in the model |
|
128 | 132 | connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, |
|
129 | 133 | SLOT(refresh())); |
|
130 | 134 | |
|
131 | 135 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); |
|
132 | 136 | ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate); |
|
133 | 137 | |
|
134 | 138 | // Fixes column sizes |
|
135 | 139 | auto model = ui->tableView->model(); |
|
136 | 140 | const auto count = model->columnCount(); |
|
137 | 141 | for (auto i = 0; i < count; ++i) { |
|
138 | 142 | ui->tableView->setColumnWidth( |
|
139 | 143 | i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width()); |
|
140 | 144 | } |
|
141 | 145 | |
|
142 | 146 | // Sets selection options |
|
143 | 147 | ui->tableView->setSelectionBehavior(QTableView::SelectRows); |
|
144 | 148 | ui->tableView->setSelectionMode(QTableView::ExtendedSelection); |
|
145 | 149 | |
|
146 | 150 | // Connection to show a menu when right clicking on the tree |
|
147 | 151 | ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); |
|
148 | 152 | connect(ui->tableView, &QTableView::customContextMenuRequested, this, |
|
149 | 153 | &VariableInspectorWidget::onTableMenuRequested); |
|
150 | 154 | } |
|
151 | 155 | |
|
152 | 156 | VariableInspectorWidget::~VariableInspectorWidget() |
|
153 | 157 | { |
|
154 | 158 | delete ui; |
|
155 | 159 | } |
|
156 | 160 | |
|
157 | 161 | void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept |
|
158 | 162 | { |
|
159 | 163 | auto selectedRows = ui->tableView->selectionModel()->selectedRows(); |
|
160 | 164 | |
|
161 | 165 | // Gets the model to retrieve the underlying selected variables |
|
162 | 166 | auto model = sqpApp->variableController().variableModel(); |
|
163 | 167 | auto selectedVariables = QVector<std::shared_ptr<Variable> >{}; |
|
164 | 168 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
165 | 169 | if (auto selectedVariable = model->variable(selectedRow.row())) { |
|
166 | 170 | selectedVariables.push_back(selectedVariable); |
|
167 | 171 | } |
|
168 | 172 | } |
|
169 | 173 | |
|
170 | 174 | QMenu tableMenu{}; |
|
171 | 175 | |
|
172 | 176 | // Emits a signal so that potential receivers can populate the menu before displaying it |
|
173 | 177 | emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); |
|
174 | 178 | |
|
175 | 179 | // Adds menu-specific actions |
|
176 | 180 | if (!selectedVariables.isEmpty()) { |
|
177 | 181 | tableMenu.addSeparator(); |
|
178 | 182 | |
|
179 | 183 | // 'Rename' and 'Duplicate' actions (only if one variable selected) |
|
180 | 184 | if (selectedVariables.size() == 1) { |
|
181 | 185 | auto selectedVariable = selectedVariables.front(); |
|
182 | 186 | |
|
183 | 187 | auto duplicateFun = [&selectedVariable]() { |
|
184 | 188 | sqpApp->variableController().cloneVariable(selectedVariable); |
|
185 | 189 | }; |
|
186 | 190 | |
|
187 | 191 | tableMenu.addAction(tr("Duplicate"), duplicateFun); |
|
188 | 192 | |
|
189 | 193 | auto renameFun = [&selectedVariable, &model, this]() { |
|
190 | 194 | // Generates forbidden names (names associated to existing variables) |
|
191 | 195 | auto allVariables = model->variables(); |
|
192 | 196 | auto forbiddenNames = QVector<QString>(allVariables.size()); |
|
193 | 197 | std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(), |
|
194 | 198 | [](const auto &variable) { return variable->name(); }); |
|
195 | 199 | |
|
196 | 200 | RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this}; |
|
197 | 201 | if (dialog.exec() == QDialog::Accepted) { |
|
198 | 202 | selectedVariable->setName(dialog.name()); |
|
199 | 203 | } |
|
200 | 204 | }; |
|
201 | 205 | |
|
202 | 206 | tableMenu.addAction(tr("Rename..."), renameFun); |
|
203 | 207 | } |
|
204 | 208 | |
|
205 | 209 | // 'Delete' action |
|
206 | 210 | auto deleteFun = [&selectedVariables]() { |
|
207 | 211 | sqpApp->variableController().deleteVariables(selectedVariables); |
|
208 | 212 | }; |
|
209 | 213 | |
|
210 | 214 | tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); |
|
211 | 215 | } |
|
212 | 216 | |
|
213 | 217 | if (!tableMenu.isEmpty()) { |
|
214 | 218 | // Generates menu header (inserted before first action) |
|
215 | 219 | auto firstAction = tableMenu.actions().first(); |
|
216 | 220 | auto headerAction = new QWidgetAction{&tableMenu}; |
|
217 | 221 | headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu}); |
|
218 | 222 | tableMenu.insertAction(firstAction, headerAction); |
|
219 | 223 | |
|
220 | 224 | // Displays menu |
|
221 | 225 | tableMenu.exec(QCursor::pos()); |
|
222 | 226 | } |
|
223 | 227 | } |
|
224 | 228 | |
|
225 | 229 | void VariableInspectorWidget::refresh() noexcept |
|
226 | 230 | { |
|
227 | 231 | ui->tableView->viewport()->update(); |
|
228 | 232 | } |
General Comments 0
You need to be logged in to leave comments.
Login now