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