@@ -0,0 +1,31 | |||
|
1 | #ifndef SCIQLOP_RENAMEVARIABLEDIALOG_H | |
|
2 | #define SCIQLOP_RENAMEVARIABLEDIALOG_H | |
|
3 | ||
|
4 | #include <QDialog> | |
|
5 | ||
|
6 | namespace Ui { | |
|
7 | class RenameVariableDialog; | |
|
8 | } // Ui | |
|
9 | ||
|
10 | /** | |
|
11 | * @brief The RenameVariableDialog class represents the dialog to rename a variable | |
|
12 | */ | |
|
13 | class RenameVariableDialog : public QDialog { | |
|
14 | public: | |
|
15 | explicit RenameVariableDialog(const QString &defaultName, | |
|
16 | const QVector<QString> &forbiddenNames, | |
|
17 | QWidget *parent = nullptr); | |
|
18 | virtual ~RenameVariableDialog() noexcept; | |
|
19 | ||
|
20 | QString name() const noexcept; | |
|
21 | ||
|
22 | public slots: | |
|
23 | void accept() override; | |
|
24 | ||
|
25 | private: | |
|
26 | Ui::RenameVariableDialog *ui; | |
|
27 | QString m_DefaultName; | |
|
28 | QVector<QString> m_ForbiddenNames; | |
|
29 | }; | |
|
30 | ||
|
31 | #endif // SCIQLOP_RENAMEVARIABLEDIALOG_H |
@@ -0,0 +1,63 | |||
|
1 | #include "Variable/RenameVariableDialog.h" | |
|
2 | ||
|
3 | #include <ui_RenameVariableDialog.h> | |
|
4 | ||
|
5 | RenameVariableDialog::RenameVariableDialog(const QString &defaultName, | |
|
6 | const QVector<QString> &forbiddenNames, QWidget *parent) | |
|
7 | : QDialog{parent}, | |
|
8 | ui{new Ui::RenameVariableDialog}, | |
|
9 | m_DefaultName{defaultName}, | |
|
10 | m_ForbiddenNames{forbiddenNames} | |
|
11 | { | |
|
12 | ui->setupUi(this); | |
|
13 | ||
|
14 | connect(ui->nameLineEdit, &QLineEdit::textChanged, [this]() { ui->errorLabel->hide(); }); | |
|
15 | ||
|
16 | ui->nameLineEdit->setText(defaultName); | |
|
17 | ui->nameLineEdit->selectAll(); | |
|
18 | ui->nameLineEdit->setFocus(); | |
|
19 | } | |
|
20 | ||
|
21 | RenameVariableDialog::~RenameVariableDialog() noexcept | |
|
22 | { | |
|
23 | delete ui; | |
|
24 | } | |
|
25 | ||
|
26 | QString RenameVariableDialog::name() const noexcept | |
|
27 | { | |
|
28 | return ui->nameLineEdit->text(); | |
|
29 | } | |
|
30 | ||
|
31 | void RenameVariableDialog::accept() | |
|
32 | { | |
|
33 | auto invalidateInput = [this](const auto &error) { | |
|
34 | ui->nameLineEdit->selectAll(); | |
|
35 | ui->nameLineEdit->setFocus(); | |
|
36 | ui->errorLabel->setText(error); | |
|
37 | ui->errorLabel->show(); | |
|
38 | }; | |
|
39 | ||
|
40 | // Empty name | |
|
41 | auto name = ui->nameLineEdit->text(); | |
|
42 | if (name.isEmpty()) { | |
|
43 | invalidateInput(tr("A variable name must be specified")); | |
|
44 | return; | |
|
45 | } | |
|
46 | ||
|
47 | // Same name when opening dialog | |
|
48 | if (name.compare(m_DefaultName, Qt::CaseInsensitive) == 0) { | |
|
49 | reject(); | |
|
50 | return; | |
|
51 | } | |
|
52 | ||
|
53 | // Forbidden name | |
|
54 | auto isForbidden | |
|
55 | = [&name](const auto &it) { return name.compare(it, Qt::CaseInsensitive) == 0; }; | |
|
56 | if (std::any_of(m_ForbiddenNames.cbegin(), m_ForbiddenNames.cend(), isForbidden)) { | |
|
57 | invalidateInput(tr("'%1' is already used").arg(name)); | |
|
58 | return; | |
|
59 | } | |
|
60 | ||
|
61 | // Valid name | |
|
62 | QDialog::accept(); | |
|
63 | } |
@@ -0,0 +1,90 | |||
|
1 | <?xml version="1.0" encoding="UTF-8"?> | |
|
2 | <ui version="4.0"> | |
|
3 | <class>RenameVariableDialog</class> | |
|
4 | <widget class="QDialog" name="RenameVariableDialog"> | |
|
5 | <property name="geometry"> | |
|
6 | <rect> | |
|
7 | <x>0</x> | |
|
8 | <y>0</y> | |
|
9 | <width>379</width> | |
|
10 | <height>109</height> | |
|
11 | </rect> | |
|
12 | </property> | |
|
13 | <property name="windowTitle"> | |
|
14 | <string>Rename variable</string> | |
|
15 | </property> | |
|
16 | <layout class="QGridLayout" name="gridLayout"> | |
|
17 | <item row="2" column="0"> | |
|
18 | <spacer name="verticalSpacer"> | |
|
19 | <property name="orientation"> | |
|
20 | <enum>Qt::Vertical</enum> | |
|
21 | </property> | |
|
22 | <property name="sizeHint" stdset="0"> | |
|
23 | <size> | |
|
24 | <width>20</width> | |
|
25 | <height>40</height> | |
|
26 | </size> | |
|
27 | </property> | |
|
28 | </spacer> | |
|
29 | </item> | |
|
30 | <item row="3" column="0"> | |
|
31 | <widget class="QDialogButtonBox" name="buttonBox"> | |
|
32 | <property name="orientation"> | |
|
33 | <enum>Qt::Horizontal</enum> | |
|
34 | </property> | |
|
35 | <property name="standardButtons"> | |
|
36 | <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> | |
|
37 | </property> | |
|
38 | </widget> | |
|
39 | </item> | |
|
40 | <item row="0" column="0"> | |
|
41 | <widget class="QLineEdit" name="nameLineEdit"/> | |
|
42 | </item> | |
|
43 | <item row="1" column="0"> | |
|
44 | <widget class="QLabel" name="errorLabel"> | |
|
45 | <property name="styleSheet"> | |
|
46 | <string notr="true">color: rgb(255, 0, 0);</string> | |
|
47 | </property> | |
|
48 | <property name="text"> | |
|
49 | <string>Error label</string> | |
|
50 | </property> | |
|
51 | </widget> | |
|
52 | </item> | |
|
53 | </layout> | |
|
54 | </widget> | |
|
55 | <resources/> | |
|
56 | <connections> | |
|
57 | <connection> | |
|
58 | <sender>buttonBox</sender> | |
|
59 | <signal>accepted()</signal> | |
|
60 | <receiver>RenameVariableDialog</receiver> | |
|
61 | <slot>accept()</slot> | |
|
62 | <hints> | |
|
63 | <hint type="sourcelabel"> | |
|
64 | <x>248</x> | |
|
65 | <y>254</y> | |
|
66 | </hint> | |
|
67 | <hint type="destinationlabel"> | |
|
68 | <x>157</x> | |
|
69 | <y>274</y> | |
|
70 | </hint> | |
|
71 | </hints> | |
|
72 | </connection> | |
|
73 | <connection> | |
|
74 | <sender>buttonBox</sender> | |
|
75 | <signal>rejected()</signal> | |
|
76 | <receiver>RenameVariableDialog</receiver> | |
|
77 | <slot>reject()</slot> | |
|
78 | <hints> | |
|
79 | <hint type="sourcelabel"> | |
|
80 | <x>316</x> | |
|
81 | <y>260</y> | |
|
82 | </hint> | |
|
83 | <hint type="destinationlabel"> | |
|
84 | <x>286</x> | |
|
85 | <y>274</y> | |
|
86 | </hint> | |
|
87 | </hints> | |
|
88 | </connection> | |
|
89 | </connections> | |
|
90 | </ui> |
@@ -1,74 +1,75 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLE_H |
|
2 | 2 | #define SCIQLOP_VARIABLE_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | |
|
6 | 6 | #include <Data/DataSeriesIterator.h> |
|
7 | 7 | #include <Data/SqpRange.h> |
|
8 | 8 | |
|
9 | 9 | #include <QLoggingCategory> |
|
10 | 10 | #include <QObject> |
|
11 | 11 | |
|
12 | 12 | #include <Common/MetaTypes.h> |
|
13 | 13 | #include <Common/spimpl.h> |
|
14 | 14 | |
|
15 | 15 | Q_DECLARE_LOGGING_CATEGORY(LOG_Variable) |
|
16 | 16 | |
|
17 | 17 | class IDataSeries; |
|
18 | 18 | class QString; |
|
19 | 19 | |
|
20 | 20 | /** |
|
21 | 21 | * @brief The Variable class represents a variable in SciQlop. |
|
22 | 22 | */ |
|
23 | 23 | class SCIQLOP_CORE_EXPORT Variable : public QObject { |
|
24 | 24 | |
|
25 | 25 | Q_OBJECT |
|
26 | 26 | |
|
27 | 27 | public: |
|
28 | 28 | explicit Variable(const QString &name, const SqpRange &dateTime, |
|
29 | 29 | const QVariantHash &metadata = {}); |
|
30 | 30 | |
|
31 | 31 | QString name() const noexcept; |
|
32 | void setName(const QString &name) noexcept; | |
|
32 | 33 | SqpRange range() const noexcept; |
|
33 | 34 | void setRange(const SqpRange &range) noexcept; |
|
34 | 35 | SqpRange cacheRange() const noexcept; |
|
35 | 36 | void setCacheRange(const SqpRange &cacheRange) noexcept; |
|
36 | 37 | |
|
37 | 38 | /// Returns the real range of the variable, i.e. the min and max x-axis values of the data |
|
38 | 39 | /// series between the range of the variable. The real range is updated each time the variable |
|
39 | 40 | /// range or the data series changed |
|
40 | 41 | /// @return the real range, invalid range if the data series is null or empty |
|
41 | 42 | /// @sa setDataSeries() |
|
42 | 43 | /// @sa setRange() |
|
43 | 44 | SqpRange realRange() const noexcept; |
|
44 | 45 | |
|
45 | 46 | /// @return the data of the variable, nullptr if there is no data |
|
46 | 47 | std::shared_ptr<IDataSeries> dataSeries() const noexcept; |
|
47 | 48 | |
|
48 | 49 | QVariantHash metadata() const noexcept; |
|
49 | 50 | |
|
50 | 51 | bool contains(const SqpRange &range) const noexcept; |
|
51 | 52 | bool intersect(const SqpRange &range) const noexcept; |
|
52 | 53 | bool isInside(const SqpRange &range) const noexcept; |
|
53 | 54 | |
|
54 | 55 | bool cacheContains(const SqpRange &range) const noexcept; |
|
55 | 56 | bool cacheIntersect(const SqpRange &range) const noexcept; |
|
56 | 57 | bool cacheIsInside(const SqpRange &range) const noexcept; |
|
57 | 58 | |
|
58 | 59 | QVector<SqpRange> provideNotInCacheRangeList(const SqpRange &range) const noexcept; |
|
59 | 60 | QVector<SqpRange> provideInCacheRangeList(const SqpRange &range) const noexcept; |
|
60 | 61 | void mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept; |
|
61 | 62 | |
|
62 | 63 | signals: |
|
63 | 64 | void updated(); |
|
64 | 65 | |
|
65 | 66 | private: |
|
66 | 67 | class VariablePrivate; |
|
67 | 68 | spimpl::unique_impl_ptr<VariablePrivate> impl; |
|
68 | 69 | }; |
|
69 | 70 | |
|
70 | 71 | // Required for using shared_ptr in signals/slots |
|
71 | 72 | SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr<Variable>) |
|
72 | 73 | SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_VECTOR_REGISTRY, QVector<std::shared_ptr<Variable> >) |
|
73 | 74 | |
|
74 | 75 | #endif // SCIQLOP_VARIABLE_H |
@@ -1,80 +1,81 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLEMODEL_H |
|
2 | 2 | #define SCIQLOP_VARIABLEMODEL_H |
|
3 | 3 | |
|
4 | 4 | #include "CoreGlobal.h" |
|
5 | 5 | |
|
6 | 6 | #include <Data/SqpRange.h> |
|
7 | 7 | |
|
8 | 8 | #include <QAbstractTableModel> |
|
9 | 9 | #include <QLoggingCategory> |
|
10 | 10 | |
|
11 | 11 | #include <Common/MetaTypes.h> |
|
12 | 12 | #include <Common/spimpl.h> |
|
13 | 13 | |
|
14 | 14 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) |
|
15 | 15 | |
|
16 | 16 | enum VariableRoles { ProgressRole = Qt::UserRole }; |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | class IDataSeries; |
|
20 | 20 | class Variable; |
|
21 | 21 | |
|
22 | 22 | /** |
|
23 | 23 | * @brief The VariableModel class aims to hold the variables that have been created in SciQlop |
|
24 | 24 | */ |
|
25 | 25 | class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel { |
|
26 | 26 | Q_OBJECT |
|
27 | 27 | public: |
|
28 | 28 | explicit VariableModel(QObject *parent = nullptr); |
|
29 | 29 | |
|
30 | 30 | /** |
|
31 | 31 | * Creates a new variable in the model |
|
32 | 32 | * @param name the name of the new variable |
|
33 | 33 | * @param dateTime the dateTime of the new variable |
|
34 | 34 | * @param metadata the metadata associated to the new variable |
|
35 | 35 | * @return the pointer to the new variable |
|
36 | 36 | */ |
|
37 | 37 | std::shared_ptr<Variable> createVariable(const QString &name, const SqpRange &dateTime, |
|
38 | 38 | const QVariantHash &metadata) noexcept; |
|
39 | 39 | |
|
40 | 40 | /** |
|
41 | 41 | * Deletes a variable from the model, if it exists |
|
42 | 42 | * @param variable the variable to delete |
|
43 | 43 | */ |
|
44 | 44 | void deleteVariable(std::shared_ptr<Variable> variable) noexcept; |
|
45 | 45 | |
|
46 | 46 | |
|
47 | 47 | std::shared_ptr<Variable> variable(int index) const; |
|
48 | std::vector<std::shared_ptr<Variable> > variables() const; | |
|
48 | 49 | |
|
49 | 50 | void setDataProgress(std::shared_ptr<Variable> variable, double progress); |
|
50 | 51 | |
|
51 | 52 | |
|
52 | 53 | // /////////////////////////// // |
|
53 | 54 | // QAbstractTableModel methods // |
|
54 | 55 | // /////////////////////////// // |
|
55 | 56 | |
|
56 | 57 | virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
57 | 58 | virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
58 | 59 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
|
59 | 60 | virtual QVariant headerData(int section, Qt::Orientation orientation, |
|
60 | 61 | int role = Qt::DisplayRole) const override; |
|
61 | 62 | |
|
62 | 63 | |
|
63 | 64 | void abortProgress(const QModelIndex &index); |
|
64 | 65 | |
|
65 | 66 | signals: |
|
66 | 67 | void abortProgessRequested(std::shared_ptr<Variable> variable); |
|
67 | 68 | |
|
68 | 69 | private: |
|
69 | 70 | class VariableModelPrivate; |
|
70 | 71 | spimpl::unique_impl_ptr<VariableModelPrivate> impl; |
|
71 | 72 | |
|
72 | 73 | private slots: |
|
73 | 74 | /// Slot called when data of a variable has been updated |
|
74 | 75 | void onVariableUpdated() noexcept; |
|
75 | 76 | }; |
|
76 | 77 | |
|
77 | 78 | // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal |
|
78 | 79 | SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>) |
|
79 | 80 | |
|
80 | 81 | #endif // SCIQLOP_VARIABLEMODEL_H |
@@ -1,270 +1,277 | |||
|
1 | 1 | #include "Variable/Variable.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/IDataSeries.h> |
|
4 | 4 | #include <Data/SqpRange.h> |
|
5 | 5 | |
|
6 | 6 | #include <QMutex> |
|
7 | 7 | #include <QReadWriteLock> |
|
8 | 8 | #include <QThread> |
|
9 | 9 | |
|
10 | 10 | Q_LOGGING_CATEGORY(LOG_Variable, "Variable") |
|
11 | 11 | |
|
12 | 12 | struct Variable::VariablePrivate { |
|
13 | 13 | explicit VariablePrivate(const QString &name, const SqpRange &dateTime, |
|
14 | 14 | const QVariantHash &metadata) |
|
15 | 15 | : m_Name{name}, |
|
16 | 16 | m_Range{dateTime}, |
|
17 | 17 | m_Metadata{metadata}, |
|
18 | 18 | m_DataSeries{nullptr}, |
|
19 | 19 | m_RealRange{INVALID_RANGE} |
|
20 | 20 | { |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | void lockRead() { m_Lock.lockForRead(); } |
|
24 | 24 | void lockWrite() { m_Lock.lockForWrite(); } |
|
25 | 25 | void unlock() { m_Lock.unlock(); } |
|
26 | 26 | |
|
27 | 27 | void purgeDataSeries() |
|
28 | 28 | { |
|
29 | 29 | if (m_DataSeries) { |
|
30 | 30 | m_DataSeries->purge(m_CacheRange.m_TStart, m_CacheRange.m_TEnd); |
|
31 | 31 | } |
|
32 | 32 | updateRealRange(); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | /// Updates real range according to current variable range and data series |
|
36 | 36 | void updateRealRange() |
|
37 | 37 | { |
|
38 | 38 | if (m_DataSeries) { |
|
39 | 39 | m_DataSeries->lockRead(); |
|
40 | 40 | auto end = m_DataSeries->cend(); |
|
41 | 41 | auto minXAxisIt = m_DataSeries->minXAxisData(m_Range.m_TStart); |
|
42 | 42 | auto maxXAxisIt = m_DataSeries->maxXAxisData(m_Range.m_TEnd); |
|
43 | 43 | |
|
44 | 44 | m_RealRange = (minXAxisIt != end && maxXAxisIt != end) |
|
45 | 45 | ? SqpRange{minXAxisIt->x(), maxXAxisIt->x()} |
|
46 | 46 | : INVALID_RANGE; |
|
47 | 47 | m_DataSeries->unlock(); |
|
48 | 48 | } |
|
49 | 49 | else { |
|
50 | 50 | m_RealRange = INVALID_RANGE; |
|
51 | 51 | } |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | QString m_Name; |
|
55 | 55 | |
|
56 | 56 | SqpRange m_Range; |
|
57 | 57 | SqpRange m_CacheRange; |
|
58 | 58 | QVariantHash m_Metadata; |
|
59 | 59 | std::shared_ptr<IDataSeries> m_DataSeries; |
|
60 | 60 | SqpRange m_RealRange; |
|
61 | 61 | |
|
62 | 62 | QReadWriteLock m_Lock; |
|
63 | 63 | }; |
|
64 | 64 | |
|
65 | 65 | Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata) |
|
66 | 66 | : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)} |
|
67 | 67 | { |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | QString Variable::name() const noexcept |
|
71 | 71 | { |
|
72 | 72 | impl->lockRead(); |
|
73 | 73 | auto name = impl->m_Name; |
|
74 | 74 | impl->unlock(); |
|
75 | 75 | return name; |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | void Variable::setName(const QString &name) noexcept | |
|
79 | { | |
|
80 | impl->lockWrite(); | |
|
81 | impl->m_Name = name; | |
|
82 | impl->unlock(); | |
|
83 | } | |
|
84 | ||
|
78 | 85 | SqpRange Variable::range() const noexcept |
|
79 | 86 | { |
|
80 | 87 | impl->lockRead(); |
|
81 | 88 | auto range = impl->m_Range; |
|
82 | 89 | impl->unlock(); |
|
83 | 90 | return range; |
|
84 | 91 | } |
|
85 | 92 | |
|
86 | 93 | void Variable::setRange(const SqpRange &range) noexcept |
|
87 | 94 | { |
|
88 | 95 | impl->lockWrite(); |
|
89 | 96 | impl->m_Range = range; |
|
90 | 97 | impl->updateRealRange(); |
|
91 | 98 | impl->unlock(); |
|
92 | 99 | } |
|
93 | 100 | |
|
94 | 101 | SqpRange Variable::cacheRange() const noexcept |
|
95 | 102 | { |
|
96 | 103 | impl->lockRead(); |
|
97 | 104 | auto cacheRange = impl->m_CacheRange; |
|
98 | 105 | impl->unlock(); |
|
99 | 106 | return cacheRange; |
|
100 | 107 | } |
|
101 | 108 | |
|
102 | 109 | void Variable::setCacheRange(const SqpRange &cacheRange) noexcept |
|
103 | 110 | { |
|
104 | 111 | impl->lockWrite(); |
|
105 | 112 | if (cacheRange != impl->m_CacheRange) { |
|
106 | 113 | impl->m_CacheRange = cacheRange; |
|
107 | 114 | impl->purgeDataSeries(); |
|
108 | 115 | } |
|
109 | 116 | impl->unlock(); |
|
110 | 117 | } |
|
111 | 118 | |
|
112 | 119 | SqpRange Variable::realRange() const noexcept |
|
113 | 120 | { |
|
114 | 121 | return impl->m_RealRange; |
|
115 | 122 | } |
|
116 | 123 | |
|
117 | 124 | void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept |
|
118 | 125 | { |
|
119 | 126 | qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries" |
|
120 | 127 | << QThread::currentThread()->objectName(); |
|
121 | 128 | if (!dataSeries) { |
|
122 | 129 | /// @todo ALX : log |
|
123 | 130 | return; |
|
124 | 131 | } |
|
125 | 132 | |
|
126 | 133 | // Add or merge the data |
|
127 | 134 | impl->lockWrite(); |
|
128 | 135 | if (!impl->m_DataSeries) { |
|
129 | 136 | impl->m_DataSeries = dataSeries->clone(); |
|
130 | 137 | } |
|
131 | 138 | else { |
|
132 | 139 | impl->m_DataSeries->merge(dataSeries.get()); |
|
133 | 140 | } |
|
134 | 141 | impl->purgeDataSeries(); |
|
135 | 142 | impl->unlock(); |
|
136 | 143 | } |
|
137 | 144 | |
|
138 | 145 | std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept |
|
139 | 146 | { |
|
140 | 147 | impl->lockRead(); |
|
141 | 148 | auto dataSeries = impl->m_DataSeries; |
|
142 | 149 | impl->unlock(); |
|
143 | 150 | |
|
144 | 151 | return dataSeries; |
|
145 | 152 | } |
|
146 | 153 | |
|
147 | 154 | QVariantHash Variable::metadata() const noexcept |
|
148 | 155 | { |
|
149 | 156 | impl->lockRead(); |
|
150 | 157 | auto metadata = impl->m_Metadata; |
|
151 | 158 | impl->unlock(); |
|
152 | 159 | return metadata; |
|
153 | 160 | } |
|
154 | 161 | |
|
155 | 162 | bool Variable::contains(const SqpRange &range) const noexcept |
|
156 | 163 | { |
|
157 | 164 | impl->lockRead(); |
|
158 | 165 | auto res = impl->m_Range.contains(range); |
|
159 | 166 | impl->unlock(); |
|
160 | 167 | return res; |
|
161 | 168 | } |
|
162 | 169 | |
|
163 | 170 | bool Variable::intersect(const SqpRange &range) const noexcept |
|
164 | 171 | { |
|
165 | 172 | |
|
166 | 173 | impl->lockRead(); |
|
167 | 174 | auto res = impl->m_Range.intersect(range); |
|
168 | 175 | impl->unlock(); |
|
169 | 176 | return res; |
|
170 | 177 | } |
|
171 | 178 | |
|
172 | 179 | bool Variable::isInside(const SqpRange &range) const noexcept |
|
173 | 180 | { |
|
174 | 181 | impl->lockRead(); |
|
175 | 182 | auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd}); |
|
176 | 183 | impl->unlock(); |
|
177 | 184 | return res; |
|
178 | 185 | } |
|
179 | 186 | |
|
180 | 187 | bool Variable::cacheContains(const SqpRange &range) const noexcept |
|
181 | 188 | { |
|
182 | 189 | impl->lockRead(); |
|
183 | 190 | auto res = impl->m_CacheRange.contains(range); |
|
184 | 191 | impl->unlock(); |
|
185 | 192 | return res; |
|
186 | 193 | } |
|
187 | 194 | |
|
188 | 195 | bool Variable::cacheIntersect(const SqpRange &range) const noexcept |
|
189 | 196 | { |
|
190 | 197 | impl->lockRead(); |
|
191 | 198 | auto res = impl->m_CacheRange.intersect(range); |
|
192 | 199 | impl->unlock(); |
|
193 | 200 | return res; |
|
194 | 201 | } |
|
195 | 202 | |
|
196 | 203 | bool Variable::cacheIsInside(const SqpRange &range) const noexcept |
|
197 | 204 | { |
|
198 | 205 | impl->lockRead(); |
|
199 | 206 | auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd}); |
|
200 | 207 | impl->unlock(); |
|
201 | 208 | return res; |
|
202 | 209 | } |
|
203 | 210 | |
|
204 | 211 | |
|
205 | 212 | QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept |
|
206 | 213 | { |
|
207 | 214 | // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange |
|
208 | 215 | |
|
209 | 216 | auto notInCache = QVector<SqpRange>{}; |
|
210 | 217 | |
|
211 | 218 | if (!this->cacheContains(range)) { |
|
212 | 219 | if (range.m_TEnd <= impl->m_CacheRange.m_TStart |
|
213 | 220 | || range.m_TStart >= impl->m_CacheRange.m_TEnd) { |
|
214 | 221 | notInCache << range; |
|
215 | 222 | } |
|
216 | 223 | else if (range.m_TStart < impl->m_CacheRange.m_TStart |
|
217 | 224 | && range.m_TEnd <= impl->m_CacheRange.m_TEnd) { |
|
218 | 225 | notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}; |
|
219 | 226 | } |
|
220 | 227 | else if (range.m_TStart < impl->m_CacheRange.m_TStart |
|
221 | 228 | && range.m_TEnd > impl->m_CacheRange.m_TEnd) { |
|
222 | 229 | notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart} |
|
223 | 230 | << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; |
|
224 | 231 | } |
|
225 | 232 | else if (range.m_TStart < impl->m_CacheRange.m_TEnd) { |
|
226 | 233 | notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd}; |
|
227 | 234 | } |
|
228 | 235 | else { |
|
229 | 236 | qCCritical(LOG_Variable()) << tr("Detection of unknown case.") |
|
230 | 237 | << QThread::currentThread(); |
|
231 | 238 | } |
|
232 | 239 | } |
|
233 | 240 | |
|
234 | 241 | return notInCache; |
|
235 | 242 | } |
|
236 | 243 | |
|
237 | 244 | QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &range) const noexcept |
|
238 | 245 | { |
|
239 | 246 | // This code assume that cach in contigue. Can return 0 or 1 SqpRange |
|
240 | 247 | |
|
241 | 248 | auto inCache = QVector<SqpRange>{}; |
|
242 | 249 | |
|
243 | 250 | |
|
244 | 251 | if (this->intersect(range)) { |
|
245 | 252 | if (range.m_TStart <= impl->m_CacheRange.m_TStart |
|
246 | 253 | && range.m_TEnd >= impl->m_CacheRange.m_TStart |
|
247 | 254 | && range.m_TEnd < impl->m_CacheRange.m_TEnd) { |
|
248 | 255 | inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd}; |
|
249 | 256 | } |
|
250 | 257 | |
|
251 | 258 | else if (range.m_TStart >= impl->m_CacheRange.m_TStart |
|
252 | 259 | && range.m_TEnd <= impl->m_CacheRange.m_TEnd) { |
|
253 | 260 | inCache << range; |
|
254 | 261 | } |
|
255 | 262 | else if (range.m_TStart > impl->m_CacheRange.m_TStart |
|
256 | 263 | && range.m_TEnd > impl->m_CacheRange.m_TEnd) { |
|
257 | 264 | inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd}; |
|
258 | 265 | } |
|
259 | 266 | else if (range.m_TStart <= impl->m_CacheRange.m_TStart |
|
260 | 267 | && range.m_TEnd >= impl->m_CacheRange.m_TEnd) { |
|
261 | 268 | inCache << impl->m_CacheRange; |
|
262 | 269 | } |
|
263 | 270 | else { |
|
264 | 271 | qCCritical(LOG_Variable()) << tr("Detection of unknown case.") |
|
265 | 272 | << QThread::currentThread(); |
|
266 | 273 | } |
|
267 | 274 | } |
|
268 | 275 | |
|
269 | 276 | return inCache; |
|
270 | 277 | } |
@@ -1,260 +1,265 | |||
|
1 | 1 | #include <Variable/Variable.h> |
|
2 | 2 | #include <Variable/VariableModel.h> |
|
3 | 3 | |
|
4 | 4 | #include <Common/DateUtils.h> |
|
5 | 5 | |
|
6 | 6 | #include <Data/IDataSeries.h> |
|
7 | 7 | |
|
8 | 8 | #include <QSize> |
|
9 | 9 | #include <unordered_map> |
|
10 | 10 | |
|
11 | 11 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
12 | 12 | |
|
13 | 13 | namespace { |
|
14 | 14 | |
|
15 | 15 | // Column indexes |
|
16 | 16 | const auto NAME_COLUMN = 0; |
|
17 | 17 | const auto TSTART_COLUMN = 1; |
|
18 | 18 | const auto TEND_COLUMN = 2; |
|
19 | 19 | const auto UNIT_COLUMN = 3; |
|
20 | 20 | const auto MISSION_COLUMN = 4; |
|
21 | 21 | const auto PLUGIN_COLUMN = 5; |
|
22 | 22 | const auto NB_COLUMNS = 6; |
|
23 | 23 | |
|
24 | 24 | // Column properties |
|
25 | 25 | const auto DEFAULT_HEIGHT = 25; |
|
26 | 26 | const auto DEFAULT_WIDTH = 100; |
|
27 | 27 | |
|
28 | 28 | struct ColumnProperties { |
|
29 | 29 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, |
|
30 | 30 | int height = DEFAULT_HEIGHT) |
|
31 | 31 | : m_Name{name}, m_Width{width}, m_Height{height} |
|
32 | 32 | { |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | QString m_Name; |
|
36 | 36 | int m_Width; |
|
37 | 37 | int m_Height; |
|
38 | 38 | }; |
|
39 | 39 | |
|
40 | 40 | const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{ |
|
41 | 41 | {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, |
|
42 | 42 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}}, |
|
43 | 43 | {MISSION_COLUMN, {QObject::tr("Mission")}}, {PLUGIN_COLUMN, {QObject::tr("Plugin")}}}; |
|
44 | 44 | |
|
45 | 45 | /// Format for datetimes |
|
46 | 46 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | } // namespace |
|
50 | 50 | |
|
51 | 51 | struct VariableModel::VariableModelPrivate { |
|
52 | 52 | /// Variables created in SciQlop |
|
53 | 53 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
54 | 54 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; |
|
55 | 55 | |
|
56 | 56 | /// Return the row index of the variable. -1 if it's not found |
|
57 | 57 | int indexOfVariable(Variable *variable) const noexcept; |
|
58 | 58 | }; |
|
59 | 59 | |
|
60 | 60 | VariableModel::VariableModel(QObject *parent) |
|
61 | 61 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
62 | 62 | { |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, |
|
66 | 66 | const SqpRange &dateTime, |
|
67 | 67 | const QVariantHash &metadata) noexcept |
|
68 | 68 | { |
|
69 | 69 | auto insertIndex = rowCount(); |
|
70 | 70 | beginInsertRows({}, insertIndex, insertIndex); |
|
71 | 71 | |
|
72 | 72 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); |
|
73 | 73 | |
|
74 | 74 | impl->m_Variables.push_back(variable); |
|
75 | 75 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); |
|
76 | 76 | |
|
77 | 77 | endInsertRows(); |
|
78 | 78 | |
|
79 | 79 | return variable; |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
83 | 83 | { |
|
84 | 84 | if (!variable) { |
|
85 | 85 | qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; |
|
86 | 86 | return; |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | // Finds variable in the model |
|
90 | 90 | auto begin = impl->m_Variables.cbegin(); |
|
91 | 91 | auto end = impl->m_Variables.cend(); |
|
92 | 92 | auto it = std::find(begin, end, variable); |
|
93 | 93 | if (it != end) { |
|
94 | 94 | auto removeIndex = std::distance(begin, it); |
|
95 | 95 | |
|
96 | 96 | // Deletes variable |
|
97 | 97 | beginRemoveRows({}, removeIndex, removeIndex); |
|
98 | 98 | impl->m_Variables.erase(it); |
|
99 | 99 | endRemoveRows(); |
|
100 | 100 | } |
|
101 | 101 | else { |
|
102 | 102 | qCritical(LOG_VariableModel()) |
|
103 | 103 | << tr("Can't delete variable %1 from the model: the variable is not in the model") |
|
104 | 104 | .arg(variable->name()); |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | // Removes variable from progress map |
|
108 | 108 | impl->m_VariableToProgress.erase(variable); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | |
|
112 | 112 | std::shared_ptr<Variable> VariableModel::variable(int index) const |
|
113 | 113 | { |
|
114 | 114 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | std::vector<std::shared_ptr<Variable> > VariableModel::variables() const | |
|
118 | { | |
|
119 | return impl->m_Variables; | |
|
120 | } | |
|
121 | ||
|
117 | 122 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) |
|
118 | 123 | { |
|
119 | 124 | if (progress > 0.0) { |
|
120 | 125 | impl->m_VariableToProgress[variable] = progress; |
|
121 | 126 | } |
|
122 | 127 | else { |
|
123 | 128 | impl->m_VariableToProgress.erase(variable); |
|
124 | 129 | } |
|
125 | 130 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); |
|
126 | 131 | |
|
127 | 132 | emit dataChanged(modelIndex, modelIndex); |
|
128 | 133 | } |
|
129 | 134 | |
|
130 | 135 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
131 | 136 | { |
|
132 | 137 | Q_UNUSED(parent); |
|
133 | 138 | |
|
134 | 139 | return NB_COLUMNS; |
|
135 | 140 | } |
|
136 | 141 | |
|
137 | 142 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
138 | 143 | { |
|
139 | 144 | Q_UNUSED(parent); |
|
140 | 145 | |
|
141 | 146 | return impl->m_Variables.size(); |
|
142 | 147 | } |
|
143 | 148 | |
|
144 | 149 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
145 | 150 | { |
|
146 | 151 | if (!index.isValid()) { |
|
147 | 152 | return QVariant{}; |
|
148 | 153 | } |
|
149 | 154 | |
|
150 | 155 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
151 | 156 | return QVariant{}; |
|
152 | 157 | } |
|
153 | 158 | |
|
154 | 159 | if (role == Qt::DisplayRole) { |
|
155 | 160 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
156 | 161 | switch (index.column()) { |
|
157 | 162 | case NAME_COLUMN: |
|
158 | 163 | return variable->name(); |
|
159 | 164 | case TSTART_COLUMN: { |
|
160 | 165 | auto range = variable->realRange(); |
|
161 | 166 | return range != INVALID_RANGE |
|
162 | 167 | ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT) |
|
163 | 168 | : QVariant{}; |
|
164 | 169 | } |
|
165 | 170 | case TEND_COLUMN: { |
|
166 | 171 | auto range = variable->realRange(); |
|
167 | 172 | return range != INVALID_RANGE |
|
168 | 173 | ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT) |
|
169 | 174 | : QVariant{}; |
|
170 | 175 | } |
|
171 | 176 | case UNIT_COLUMN: |
|
172 | 177 | return variable->metadata().value(QStringLiteral("units")); |
|
173 | 178 | case MISSION_COLUMN: |
|
174 | 179 | return variable->metadata().value(QStringLiteral("mission")); |
|
175 | 180 | case PLUGIN_COLUMN: |
|
176 | 181 | return variable->metadata().value(QStringLiteral("plugin")); |
|
177 | 182 | default: |
|
178 | 183 | // No action |
|
179 | 184 | break; |
|
180 | 185 | } |
|
181 | 186 | |
|
182 | 187 | qWarning(LOG_VariableModel()) |
|
183 | 188 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
184 | 189 | } |
|
185 | 190 | else { |
|
186 | 191 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
187 | 192 | } |
|
188 | 193 | } |
|
189 | 194 | else if (role == VariableRoles::ProgressRole) { |
|
190 | 195 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
191 | 196 | |
|
192 | 197 | auto it = impl->m_VariableToProgress.find(variable); |
|
193 | 198 | if (it != impl->m_VariableToProgress.cend()) { |
|
194 | 199 | return it->second; |
|
195 | 200 | } |
|
196 | 201 | } |
|
197 | 202 | } |
|
198 | 203 | |
|
199 | 204 | return QVariant{}; |
|
200 | 205 | } |
|
201 | 206 | |
|
202 | 207 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
203 | 208 | { |
|
204 | 209 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { |
|
205 | 210 | return QVariant{}; |
|
206 | 211 | } |
|
207 | 212 | |
|
208 | 213 | if (orientation == Qt::Horizontal) { |
|
209 | 214 | auto propertiesIt = COLUMN_PROPERTIES.find(section); |
|
210 | 215 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { |
|
211 | 216 | // Role is either DisplayRole or SizeHintRole |
|
212 | 217 | return (role == Qt::DisplayRole) |
|
213 | 218 | ? QVariant{propertiesIt->m_Name} |
|
214 | 219 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; |
|
215 | 220 | } |
|
216 | 221 | else { |
|
217 | 222 | qWarning(LOG_VariableModel()) |
|
218 | 223 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
219 | 224 | } |
|
220 | 225 | } |
|
221 | 226 | |
|
222 | 227 | return QVariant{}; |
|
223 | 228 | } |
|
224 | 229 | |
|
225 | 230 | void VariableModel::abortProgress(const QModelIndex &index) |
|
226 | 231 | { |
|
227 | 232 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
228 | 233 | emit abortProgessRequested(variable); |
|
229 | 234 | } |
|
230 | 235 | } |
|
231 | 236 | |
|
232 | 237 | void VariableModel::onVariableUpdated() noexcept |
|
233 | 238 | { |
|
234 | 239 | // Finds variable that has been updated in the model |
|
235 | 240 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { |
|
236 | 241 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); |
|
237 | 242 | |
|
238 | 243 | if (updatedVariableIndex > -1) { |
|
239 | 244 | emit dataChanged(createIndex(updatedVariableIndex, 0), |
|
240 | 245 | createIndex(updatedVariableIndex, columnCount() - 1)); |
|
241 | 246 | } |
|
242 | 247 | } |
|
243 | 248 | } |
|
244 | 249 | |
|
245 | 250 | int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept |
|
246 | 251 | { |
|
247 | 252 | auto begin = std::cbegin(m_Variables); |
|
248 | 253 | auto end = std::cend(m_Variables); |
|
249 | 254 | auto it |
|
250 | 255 | = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); |
|
251 | 256 | |
|
252 | 257 | if (it != end) { |
|
253 | 258 | // Gets the index of the variable in the model: we assume here that views have the same |
|
254 | 259 | // order as the model |
|
255 | 260 | return std::distance(begin, it); |
|
256 | 261 | } |
|
257 | 262 | else { |
|
258 | 263 | return -1; |
|
259 | 264 | } |
|
260 | 265 | } |
@@ -1,199 +1,222 | |||
|
1 | #include <Variable/RenameVariableDialog.h> | |
|
2 | #include <Variable/Variable.h> | |
|
1 | 3 | #include <Variable/VariableController.h> |
|
2 | 4 | #include <Variable/VariableInspectorWidget.h> |
|
3 | 5 | #include <Variable/VariableMenuHeaderWidget.h> |
|
4 | 6 | #include <Variable/VariableModel.h> |
|
5 | 7 | |
|
6 | 8 | #include <ui_VariableInspectorWidget.h> |
|
7 | 9 | |
|
8 | 10 | #include <QMouseEvent> |
|
9 | 11 | #include <QSortFilterProxyModel> |
|
10 | 12 | #include <QStyledItemDelegate> |
|
11 | 13 | #include <QWidgetAction> |
|
12 | 14 | |
|
13 | 15 | #include <SqpApplication.h> |
|
14 | 16 | |
|
15 | 17 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") |
|
16 | 18 | |
|
17 | 19 | |
|
18 | 20 | class QProgressBarItemDelegate : public QStyledItemDelegate { |
|
19 | 21 | |
|
20 | 22 | public: |
|
21 | 23 | QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {} |
|
22 | 24 | |
|
23 | 25 | void paint(QPainter *painter, const QStyleOptionViewItem &option, |
|
24 | 26 | const QModelIndex &index) const |
|
25 | 27 | { |
|
26 | 28 | auto data = index.data(Qt::DisplayRole); |
|
27 | 29 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
28 | 30 | if (data.isValid() && progressData.isValid()) { |
|
29 | 31 | auto name = data.value<QString>(); |
|
30 | 32 | auto progress = progressData.value<double>(); |
|
31 | 33 | if (progress > 0) { |
|
32 | 34 | auto cancelButtonWidth = 20; |
|
33 | 35 | auto progressBarOption = QStyleOptionProgressBar{}; |
|
34 | 36 | auto progressRect = option.rect; |
|
35 | 37 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
36 | 38 | progressBarOption.rect = progressRect; |
|
37 | 39 | progressBarOption.minimum = 0; |
|
38 | 40 | progressBarOption.maximum = 100; |
|
39 | 41 | progressBarOption.progress = progress; |
|
40 | 42 | progressBarOption.text |
|
41 | 43 | = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%"); |
|
42 | 44 | progressBarOption.textVisible = true; |
|
43 | 45 | progressBarOption.textAlignment = Qt::AlignCenter; |
|
44 | 46 | |
|
45 | 47 | |
|
46 | 48 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, |
|
47 | 49 | painter); |
|
48 | 50 | |
|
49 | 51 | // Cancel button |
|
50 | 52 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
51 | 53 | option.rect.height()); |
|
52 | 54 | auto buttonOption = QStyleOptionButton{}; |
|
53 | 55 | buttonOption.rect = buttonRect; |
|
54 | 56 | buttonOption.text = "X"; |
|
55 | 57 | |
|
56 | 58 | QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); |
|
57 | 59 | } |
|
58 | 60 | else { |
|
59 | 61 | QStyledItemDelegate::paint(painter, option, index); |
|
60 | 62 | } |
|
61 | 63 | } |
|
62 | 64 | else { |
|
63 | 65 | QStyledItemDelegate::paint(painter, option, index); |
|
64 | 66 | } |
|
65 | 67 | } |
|
66 | 68 | |
|
67 | 69 | bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, |
|
68 | 70 | const QModelIndex &index) |
|
69 | 71 | { |
|
70 | 72 | if (event->type() == QEvent::MouseButtonRelease) { |
|
71 | 73 | auto data = index.data(Qt::DisplayRole); |
|
72 | 74 | auto progressData = index.data(VariableRoles::ProgressRole); |
|
73 | 75 | if (data.isValid() && progressData.isValid()) { |
|
74 | 76 | auto cancelButtonWidth = 20; |
|
75 | 77 | auto progressRect = option.rect; |
|
76 | 78 | progressRect.setWidth(progressRect.width() - cancelButtonWidth); |
|
77 | 79 | // Cancel button |
|
78 | 80 | auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, |
|
79 | 81 | option.rect.height()); |
|
80 | 82 | |
|
81 | 83 | auto e = (QMouseEvent *)event; |
|
82 | 84 | auto clickX = e->x(); |
|
83 | 85 | auto clickY = e->y(); |
|
84 | 86 | |
|
85 | 87 | auto x = buttonRect.left(); // the X coordinate |
|
86 | 88 | auto y = buttonRect.top(); // the Y coordinate |
|
87 | 89 | auto w = buttonRect.width(); // button width |
|
88 | 90 | auto h = buttonRect.height(); // button height |
|
89 | 91 | |
|
90 | 92 | if (clickX > x && clickX < x + w) { |
|
91 | 93 | if (clickY > y && clickY < y + h) { |
|
92 | 94 | auto variableModel = sqpApp->variableController().variableModel(); |
|
93 | 95 | variableModel->abortProgress(index); |
|
94 | 96 | } |
|
95 | 97 | } |
|
96 | 98 | else { |
|
97 | 99 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
98 | 100 | } |
|
99 | 101 | } |
|
100 | 102 | else { |
|
101 | 103 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
102 | 104 | } |
|
103 | 105 | } |
|
104 | 106 | else { |
|
105 | 107 | QStyledItemDelegate::editorEvent(event, model, option, index); |
|
106 | 108 | } |
|
107 | 109 | } |
|
108 | 110 | }; |
|
109 | 111 | |
|
110 | 112 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) |
|
111 | 113 | : QWidget{parent}, |
|
112 | 114 | ui{new Ui::VariableInspectorWidget}, |
|
113 | 115 | m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}} |
|
114 | 116 | { |
|
115 | 117 | ui->setupUi(this); |
|
116 | 118 | |
|
117 | 119 | // Sets model for table |
|
118 | 120 | // auto sortFilterModel = new QSortFilterProxyModel{this}; |
|
119 | 121 | // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); |
|
120 | 122 | |
|
121 | 123 | auto variableModel = sqpApp->variableController().variableModel(); |
|
122 | 124 | ui->tableView->setModel(variableModel); |
|
123 | 125 | |
|
124 | 126 | // Adds extra signal/slot between view and model, so the view can be updated instantly when |
|
125 | 127 | // there is a change of data in the model |
|
126 | 128 | connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, |
|
127 | 129 | SLOT(refresh())); |
|
128 | 130 | |
|
129 | 131 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); |
|
130 | 132 | ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate); |
|
131 | 133 | |
|
132 | 134 | // Fixes column sizes |
|
133 | 135 | auto model = ui->tableView->model(); |
|
134 | 136 | const auto count = model->columnCount(); |
|
135 | 137 | for (auto i = 0; i < count; ++i) { |
|
136 | 138 | ui->tableView->setColumnWidth( |
|
137 | 139 | i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width()); |
|
138 | 140 | } |
|
139 | 141 | |
|
140 | 142 | // Sets selection options |
|
141 | 143 | ui->tableView->setSelectionBehavior(QTableView::SelectRows); |
|
142 | 144 | ui->tableView->setSelectionMode(QTableView::ExtendedSelection); |
|
143 | 145 | |
|
144 | 146 | // Connection to show a menu when right clicking on the tree |
|
145 | 147 | ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); |
|
146 | 148 | connect(ui->tableView, &QTableView::customContextMenuRequested, this, |
|
147 | 149 | &VariableInspectorWidget::onTableMenuRequested); |
|
148 | 150 | } |
|
149 | 151 | |
|
150 | 152 | VariableInspectorWidget::~VariableInspectorWidget() |
|
151 | 153 | { |
|
152 | 154 | delete ui; |
|
153 | 155 | } |
|
154 | 156 | |
|
155 | 157 | void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept |
|
156 | 158 | { |
|
157 | 159 | auto selectedRows = ui->tableView->selectionModel()->selectedRows(); |
|
158 | 160 | |
|
159 | 161 | // Gets the model to retrieve the underlying selected variables |
|
160 | 162 | auto model = sqpApp->variableController().variableModel(); |
|
161 | 163 | auto selectedVariables = QVector<std::shared_ptr<Variable> >{}; |
|
162 | 164 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
163 | 165 | if (auto selectedVariable = model->variable(selectedRow.row())) { |
|
164 | 166 | selectedVariables.push_back(selectedVariable); |
|
165 | 167 | } |
|
166 | 168 | } |
|
167 | 169 | |
|
168 | 170 | QMenu tableMenu{}; |
|
169 | 171 | |
|
170 | 172 | // Emits a signal so that potential receivers can populate the menu before displaying it |
|
171 | 173 | emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); |
|
172 | 174 | |
|
173 | 175 | // Adds menu-specific actions |
|
174 | 176 | if (!selectedVariables.isEmpty()) { |
|
177 | tableMenu.addSeparator(); | |
|
178 | ||
|
179 | // 'Rename' action (only if one variable selected) | |
|
180 | if (selectedVariables.size() == 1) { | |
|
181 | auto selectedVariable = selectedVariables.front(); | |
|
182 | ||
|
183 | auto renameFun = [&selectedVariable, &model, this]() { | |
|
184 | // Generates forbidden names (names associated to existing variables) | |
|
185 | auto allVariables = model->variables(); | |
|
186 | auto forbiddenNames = QVector<QString>(allVariables.size()); | |
|
187 | std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(), | |
|
188 | [](const auto &variable) { return variable->name(); }); | |
|
189 | ||
|
190 | RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this}; | |
|
191 | if (dialog.exec() == QDialog::Accepted) { | |
|
192 | selectedVariable->setName(dialog.name()); | |
|
193 | } | |
|
194 | }; | |
|
195 | ||
|
196 | tableMenu.addAction(tr("Rename..."), renameFun); | |
|
197 | } | |
|
198 | ||
|
175 | 199 | // 'Delete' action |
|
176 | 200 | auto deleteFun = [&selectedVariables]() { |
|
177 | 201 | sqpApp->variableController().deleteVariables(selectedVariables); |
|
178 | 202 | }; |
|
179 | 203 | |
|
180 | tableMenu.addSeparator(); | |
|
181 | 204 | tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); |
|
182 | 205 | } |
|
183 | 206 | |
|
184 | 207 | if (!tableMenu.isEmpty()) { |
|
185 | 208 | // Generates menu header (inserted before first action) |
|
186 | 209 | auto firstAction = tableMenu.actions().first(); |
|
187 | 210 | auto headerAction = new QWidgetAction{&tableMenu}; |
|
188 | 211 | headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu}); |
|
189 | 212 | tableMenu.insertAction(firstAction, headerAction); |
|
190 | 213 | |
|
191 | 214 | // Displays menu |
|
192 | 215 | tableMenu.exec(QCursor::pos()); |
|
193 | 216 | } |
|
194 | 217 | } |
|
195 | 218 | |
|
196 | 219 | void VariableInspectorWidget::refresh() noexcept |
|
197 | 220 | { |
|
198 | 221 | ui->tableView->viewport()->update(); |
|
199 | 222 | } |
General Comments 0
You need to be logged in to leave comments.
Login now