##// END OF EJS Templates
Adds width and height properties
Alexandre Leroux -
r256:7011b0fc9fba
parent child
Show More
@@ -1,144 +1,152
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>
6 #include <QDateTime>
7
7
8 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
8 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
9
9
10 namespace {
10 namespace {
11
11
12 // Column indexes
12 // Column indexes
13 const auto NAME_COLUMN = 0;
13 const auto NAME_COLUMN = 0;
14 const auto TSTART_COLUMN = 1;
14 const auto TSTART_COLUMN = 1;
15 const auto TEND_COLUMN = 2;
15 const auto TEND_COLUMN = 2;
16 const auto NB_COLUMNS = 3;
16 const auto NB_COLUMNS = 3;
17
17
18 // Column properties
18 // Column properties
19 const auto DEFAULT_HEIGHT = 25;
20 const auto DEFAULT_WIDTH = 100;
19
21
20 struct ColumnProperties {
22 struct ColumnProperties {
21 ColumnProperties(const QString &name = {}) : m_Name{name} {}
23 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
24 int height = DEFAULT_HEIGHT)
25 : m_Name{name}, m_Width{width}, m_Height{height}
26 {
27 }
22
28
23 QString m_Name;
29 QString m_Name;
30 int m_Width;
31 int m_Height;
24 };
32 };
25
33
26 const auto COLUMN_PROPERTIES
34 const auto COLUMN_PROPERTIES
27 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
35 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
28 {TSTART_COLUMN, {QObject::tr("tStart")}},
36 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
29 {TEND_COLUMN, {QObject::tr("tEnd")}}};
37 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
30
38
31 /// Format for datetimes
39 /// Format for datetimes
32 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
40 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
33
41
34 } // namespace
42 } // namespace
35
43
36 struct VariableModel::VariableModelPrivate {
44 struct VariableModel::VariableModelPrivate {
37 /// Variables created in SciQlop
45 /// Variables created in SciQlop
38 std::vector<std::shared_ptr<Variable> > m_Variables;
46 std::vector<std::shared_ptr<Variable> > m_Variables;
39 };
47 };
40
48
41 VariableModel::VariableModel(QObject *parent)
49 VariableModel::VariableModel(QObject *parent)
42 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
50 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
43 {
51 {
44 }
52 }
45
53
46 std::shared_ptr<Variable>
54 std::shared_ptr<Variable>
47 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
55 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
48 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
56 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
49 {
57 {
50 auto insertIndex = rowCount();
58 auto insertIndex = rowCount();
51 beginInsertRows({}, insertIndex, insertIndex);
59 beginInsertRows({}, insertIndex, insertIndex);
52
60
53 /// @todo For the moment, the other data of the variable is initialized with default values
61 /// @todo For the moment, the other data of the variable is initialized with default values
54 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
62 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
55 QStringLiteral("mission"), dateTime);
63 QStringLiteral("mission"), dateTime);
56 variable->setDataSeries(std::move(defaultDataSeries));
64 variable->setDataSeries(std::move(defaultDataSeries));
57
65
58 impl->m_Variables.push_back(variable);
66 impl->m_Variables.push_back(variable);
59
67
60 endInsertRows();
68 endInsertRows();
61
69
62 return variable;
70 return variable;
63 }
71 }
64
72
65 std::shared_ptr<Variable> VariableModel::variable(int index) const
73 std::shared_ptr<Variable> VariableModel::variable(int index) const
66 {
74 {
67 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
75 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
68 }
76 }
69
77
70 int VariableModel::columnCount(const QModelIndex &parent) const
78 int VariableModel::columnCount(const QModelIndex &parent) const
71 {
79 {
72 Q_UNUSED(parent);
80 Q_UNUSED(parent);
73
81
74 return NB_COLUMNS;
82 return NB_COLUMNS;
75 }
83 }
76
84
77 int VariableModel::rowCount(const QModelIndex &parent) const
85 int VariableModel::rowCount(const QModelIndex &parent) const
78 {
86 {
79 Q_UNUSED(parent);
87 Q_UNUSED(parent);
80
88
81 return impl->m_Variables.size();
89 return impl->m_Variables.size();
82 }
90 }
83
91
84 QVariant VariableModel::data(const QModelIndex &index, int role) const
92 QVariant VariableModel::data(const QModelIndex &index, int role) const
85 {
93 {
86 if (!index.isValid()) {
94 if (!index.isValid()) {
87 return QVariant{};
95 return QVariant{};
88 }
96 }
89
97
90 if (index.row() < 0 || index.row() >= rowCount()) {
98 if (index.row() < 0 || index.row() >= rowCount()) {
91 return QVariant{};
99 return QVariant{};
92 }
100 }
93
101
94 if (role == Qt::DisplayRole) {
102 if (role == Qt::DisplayRole) {
95 if (auto variable = impl->m_Variables.at(index.row()).get()) {
103 if (auto variable = impl->m_Variables.at(index.row()).get()) {
96 /// Lambda function that builds the variant to return for a time value
104 /// Lambda function that builds the variant to return for a time value
97 auto dateTimeVariant = [](double time) {
105 auto dateTimeVariant = [](double time) {
98 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
106 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
99 return dateTime.toString(DATETIME_FORMAT);
107 return dateTime.toString(DATETIME_FORMAT);
100 };
108 };
101
109
102 switch (index.column()) {
110 switch (index.column()) {
103 case NAME_COLUMN:
111 case NAME_COLUMN:
104 return variable->name();
112 return variable->name();
105 case TSTART_COLUMN:
113 case TSTART_COLUMN:
106 return dateTimeVariant(variable->dateTime().m_TStart);
114 return dateTimeVariant(variable->dateTime().m_TStart);
107 case TEND_COLUMN:
115 case TEND_COLUMN:
108 return dateTimeVariant(variable->dateTime().m_TEnd);
116 return dateTimeVariant(variable->dateTime().m_TEnd);
109 default:
117 default:
110 // No action
118 // No action
111 break;
119 break;
112 }
120 }
113
121
114 qWarning(LOG_VariableModel())
122 qWarning(LOG_VariableModel())
115 << tr("Can't get data (unknown column %1)").arg(index.column());
123 << tr("Can't get data (unknown column %1)").arg(index.column());
116 }
124 }
117 else {
125 else {
118 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
126 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
119 }
127 }
120 }
128 }
121
129
122 return QVariant{};
130 return QVariant{};
123 }
131 }
124
132
125 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
133 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
126 {
134 {
127 if (role != Qt::DisplayRole) {
135 if (role != Qt::DisplayRole) {
128 return QVariant{};
136 return QVariant{};
129 }
137 }
130
138
131 if (orientation == Qt::Horizontal) {
139 if (orientation == Qt::Horizontal) {
132 auto propertiesIt = COLUMN_PROPERTIES.find(section);
140 auto propertiesIt = COLUMN_PROPERTIES.find(section);
133 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
141 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
134 // Role is either DisplayRole or SizeHintRole
142 // Role is either DisplayRole or SizeHintRole
135 return QVariant{propertiesIt->m_Name};
143 return QVariant{propertiesIt->m_Name};
136 }
144 }
137 else {
145 else {
138 qWarning(LOG_VariableModel())
146 qWarning(LOG_VariableModel())
139 << tr("Can't get header data (unknown column %1)").arg(section);
147 << tr("Can't get header data (unknown column %1)").arg(section);
140 }
148 }
141 }
149 }
142
150
143 return QVariant{};
151 return QVariant{};
144 }
152 }
General Comments 0
You need to be logged in to leave comments. Login now