##// END OF EJS Templates
Merge branch 'feature/DatetimeColumns' into develop
Alexandre Leroux -
r263:8ab9ca5324b3 merge
parent child
Show More
@@ -1,125 +1,155
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableModel.h>
2 #include <Variable/VariableModel.h>
3
3
4 #include <Data/IDataSeries.h>
4 #include <Data/IDataSeries.h>
5
5
6 #include <QDateTime>
7 #include <QSize>
8
6 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
9 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
7
10
8 namespace {
11 namespace {
9
12
10 // Column indexes
13 // Column indexes
11 const auto NAME_COLUMN = 0;
14 const auto NAME_COLUMN = 0;
12 const auto UNIT_COLUMN = 1;
15 const auto TSTART_COLUMN = 1;
13 const auto MISSION_COLUMN = 2;
16 const auto TEND_COLUMN = 2;
14 const auto NB_COLUMNS = 3;
17 const auto NB_COLUMNS = 3;
15
18
19 // Column properties
20 const auto DEFAULT_HEIGHT = 25;
21 const auto DEFAULT_WIDTH = 100;
22
23 struct ColumnProperties {
24 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
25 int height = DEFAULT_HEIGHT)
26 : m_Name{name}, m_Width{width}, m_Height{height}
27 {
28 }
29
30 QString m_Name;
31 int m_Width;
32 int m_Height;
33 };
34
35 const auto COLUMN_PROPERTIES
36 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
37 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
38 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
39
40 /// Format for datetimes
41 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42
16 } // namespace
43 } // namespace
17
44
18 struct VariableModel::VariableModelPrivate {
45 struct VariableModel::VariableModelPrivate {
19 /// Variables created in SciQlop
46 /// Variables created in SciQlop
20 std::vector<std::shared_ptr<Variable> > m_Variables;
47 std::vector<std::shared_ptr<Variable> > m_Variables;
21 };
48 };
22
49
23 VariableModel::VariableModel(QObject *parent)
50 VariableModel::VariableModel(QObject *parent)
24 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
51 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
25 {
52 {
26 }
53 }
27
54
28 std::shared_ptr<Variable>
55 std::shared_ptr<Variable>
29 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
56 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
30 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
57 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
31 {
58 {
32 auto insertIndex = rowCount();
59 auto insertIndex = rowCount();
33 beginInsertRows({}, insertIndex, insertIndex);
60 beginInsertRows({}, insertIndex, insertIndex);
34
61
35 /// @todo For the moment, the other data of the variable is initialized with default values
62 /// @todo For the moment, the other data of the variable is initialized with default values
36 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
63 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
37 QStringLiteral("mission"), dateTime);
64 QStringLiteral("mission"), dateTime);
38 variable->setDataSeries(std::move(defaultDataSeries));
65 variable->setDataSeries(std::move(defaultDataSeries));
39
66
40 impl->m_Variables.push_back(variable);
67 impl->m_Variables.push_back(variable);
41
68
42 endInsertRows();
69 endInsertRows();
43
70
44 return variable;
71 return variable;
45 }
72 }
46
73
47 std::shared_ptr<Variable> VariableModel::variable(int index) const
74 std::shared_ptr<Variable> VariableModel::variable(int index) const
48 {
75 {
49 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
76 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
50 }
77 }
51
78
52 int VariableModel::columnCount(const QModelIndex &parent) const
79 int VariableModel::columnCount(const QModelIndex &parent) const
53 {
80 {
54 Q_UNUSED(parent);
81 Q_UNUSED(parent);
55
82
56 return NB_COLUMNS;
83 return NB_COLUMNS;
57 }
84 }
58
85
59 int VariableModel::rowCount(const QModelIndex &parent) const
86 int VariableModel::rowCount(const QModelIndex &parent) const
60 {
87 {
61 Q_UNUSED(parent);
88 Q_UNUSED(parent);
62
89
63 return impl->m_Variables.size();
90 return impl->m_Variables.size();
64 }
91 }
65
92
66 QVariant VariableModel::data(const QModelIndex &index, int role) const
93 QVariant VariableModel::data(const QModelIndex &index, int role) const
67 {
94 {
68 if (!index.isValid()) {
95 if (!index.isValid()) {
69 return QVariant{};
96 return QVariant{};
70 }
97 }
71
98
72 if (index.row() < 0 || index.row() >= rowCount()) {
99 if (index.row() < 0 || index.row() >= rowCount()) {
73 return QVariant{};
100 return QVariant{};
74 }
101 }
75
102
76 if (role == Qt::DisplayRole) {
103 if (role == Qt::DisplayRole) {
77 if (auto variable = impl->m_Variables.at(index.row()).get()) {
104 if (auto variable = impl->m_Variables.at(index.row()).get()) {
105 /// Lambda function that builds the variant to return for a time value
106 auto dateTimeVariant = [](double time) {
107 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
108 return dateTime.toString(DATETIME_FORMAT);
109 };
110
78 switch (index.column()) {
111 switch (index.column()) {
79 case NAME_COLUMN:
112 case NAME_COLUMN:
80 return variable->name();
113 return variable->name();
81 case UNIT_COLUMN:
114 case TSTART_COLUMN:
82 return variable->unit();
115 return dateTimeVariant(variable->dateTime().m_TStart);
83 case MISSION_COLUMN:
116 case TEND_COLUMN:
84 return variable->mission();
117 return dateTimeVariant(variable->dateTime().m_TEnd);
85 default:
118 default:
86 // No action
119 // No action
87 break;
120 break;
88 }
121 }
89
122
90 qWarning(LOG_VariableModel())
123 qWarning(LOG_VariableModel())
91 << tr("Can't get data (unknown column %1)").arg(index.column());
124 << tr("Can't get data (unknown column %1)").arg(index.column());
92 }
125 }
93 else {
126 else {
94 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
127 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
95 }
128 }
96 }
129 }
97
130
98 return QVariant{};
131 return QVariant{};
99 }
132 }
100
133
101 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
134 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
102 {
135 {
103 if (role != Qt::DisplayRole) {
136 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
104 return QVariant{};
137 return QVariant{};
105 }
138 }
106
139
107 if (orientation == Qt::Horizontal) {
140 if (orientation == Qt::Horizontal) {
108 switch (section) {
141 auto propertiesIt = COLUMN_PROPERTIES.find(section);
109 case NAME_COLUMN:
142 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
110 return tr("Name");
143 // Role is either DisplayRole or SizeHintRole
111 case UNIT_COLUMN:
144 return (role == Qt::DisplayRole)
112 return tr("Unit");
145 ? QVariant{propertiesIt->m_Name}
113 case MISSION_COLUMN:
146 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
114 return tr("Mission");
147 }
115 default:
148 else {
116 // No action
149 qWarning(LOG_VariableModel())
117 break;
150 << tr("Can't get header data (unknown column %1)").arg(section);
118 }
151 }
119
120 qWarning(LOG_VariableModel())
121 << tr("Can't get header data (unknown column %1)").arg(section);
122 }
152 }
123
153
124 return QVariant{};
154 return QVariant{};
125 }
155 }
@@ -1,57 +1,65
1 #include <Variable/VariableController.h>
1 #include <Variable/VariableController.h>
2 #include <Variable/VariableInspectorWidget.h>
2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableModel.h>
3 #include <Variable/VariableModel.h>
4
4
5 #include <ui_VariableInspectorWidget.h>
5 #include <ui_VariableInspectorWidget.h>
6
6
7 #include <QSortFilterProxyModel>
7 #include <QSortFilterProxyModel>
8
8
9 #include <SqpApplication.h>
9 #include <SqpApplication.h>
10
10
11 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
11 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
12
12
13 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
13 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
14 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
14 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
15 {
15 {
16 ui->setupUi(this);
16 ui->setupUi(this);
17
17
18 // Sets model for table
18 // Sets model for table
19 auto sortFilterModel = new QSortFilterProxyModel{this};
19 auto sortFilterModel = new QSortFilterProxyModel{this};
20 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
20 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
21
21
22 ui->tableView->setModel(sortFilterModel);
22 ui->tableView->setModel(sortFilterModel);
23
23
24 // Fixes column sizes
25 auto model = ui->tableView->model();
26 const auto count = model->columnCount();
27 for (auto i = 0; i < count; ++i) {
28 ui->tableView->setColumnWidth(
29 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
30 }
31
24 // Connection to show a menu when right clicking on the tree
32 // Connection to show a menu when right clicking on the tree
25 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
33 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
26 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
34 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
27 &VariableInspectorWidget::onTableMenuRequested);
35 &VariableInspectorWidget::onTableMenuRequested);
28 }
36 }
29
37
30 VariableInspectorWidget::~VariableInspectorWidget()
38 VariableInspectorWidget::~VariableInspectorWidget()
31 {
39 {
32 delete ui;
40 delete ui;
33 }
41 }
34
42
35 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
43 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
36 {
44 {
37 auto selectedIndex = ui->tableView->indexAt(pos);
45 auto selectedIndex = ui->tableView->indexAt(pos);
38 if (selectedIndex.isValid()) {
46 if (selectedIndex.isValid()) {
39 // Gets the model to retrieve the underlying selected variable
47 // Gets the model to retrieve the underlying selected variable
40 auto model = sqpApp->variableController().variableModel();
48 auto model = sqpApp->variableController().variableModel();
41 if (auto selectedVariable = model->variable(selectedIndex.row())) {
49 if (auto selectedVariable = model->variable(selectedIndex.row())) {
42 QMenu tableMenu{};
50 QMenu tableMenu{};
43
51
44 // Emit a signal so that potential receivers can populate the menu before displaying it
52 // Emit a signal so that potential receivers can populate the menu before displaying it
45 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariable);
53 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariable);
46
54
47 if (!tableMenu.isEmpty()) {
55 if (!tableMenu.isEmpty()) {
48 tableMenu.exec(mapToGlobal(pos));
56 tableMenu.exec(mapToGlobal(pos));
49 }
57 }
50 }
58 }
51 }
59 }
52 else {
60 else {
53 qCCritical(LOG_VariableInspectorWidget())
61 qCCritical(LOG_VariableInspectorWidget())
54 << tr("Can't display menu : invalid index (%1;%2)")
62 << tr("Can't display menu : invalid index (%1;%2)")
55 .arg(selectedIndex.row(), selectedIndex.column());
63 .arg(selectedIndex.row(), selectedIndex.column());
56 }
64 }
57 }
65 }
General Comments 0
You need to be logged in to leave comments. Login now