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