From 8ab9ca5324b3fb657e002714dfb41a9eff0f0fb9 2017-06-29 09:10:29 From: Alexandre Leroux Date: 2017-06-29 09:10:29 Subject: [PATCH] Merge branch 'feature/DatetimeColumns' into develop --- diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index 044b2a9..e92a27a 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -3,16 +3,43 @@ #include +#include +#include + Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") namespace { // Column indexes const auto NAME_COLUMN = 0; -const auto UNIT_COLUMN = 1; -const auto MISSION_COLUMN = 2; +const auto TSTART_COLUMN = 1; +const auto TEND_COLUMN = 2; const auto NB_COLUMNS = 3; +// Column properties +const auto DEFAULT_HEIGHT = 25; +const auto DEFAULT_WIDTH = 100; + +struct ColumnProperties { + ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, + int height = DEFAULT_HEIGHT) + : m_Name{name}, m_Width{width}, m_Height{height} + { + } + + QString m_Name; + int m_Width; + int m_Height; +}; + +const auto COLUMN_PROPERTIES + = QHash{{NAME_COLUMN, {QObject::tr("Name")}}, + {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, + {TEND_COLUMN, {QObject::tr("tEnd"), 180}}}; + +/// Format for datetimes +const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); + } // namespace struct VariableModel::VariableModelPrivate { @@ -75,13 +102,19 @@ QVariant VariableModel::data(const QModelIndex &index, int role) const if (role == Qt::DisplayRole) { if (auto variable = impl->m_Variables.at(index.row()).get()) { + /// Lambda function that builds the variant to return for a time value + auto dateTimeVariant = [](double time) { + auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); + return dateTime.toString(DATETIME_FORMAT); + }; + switch (index.column()) { case NAME_COLUMN: return variable->name(); - case UNIT_COLUMN: - return variable->unit(); - case MISSION_COLUMN: - return variable->mission(); + case TSTART_COLUMN: + return dateTimeVariant(variable->dateTime().m_TStart); + case TEND_COLUMN: + return dateTimeVariant(variable->dateTime().m_TEnd); default: // No action break; @@ -100,25 +133,22 @@ QVariant VariableModel::data(const QModelIndex &index, int role) const QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (role != Qt::DisplayRole) { + if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { return QVariant{}; } if (orientation == Qt::Horizontal) { - switch (section) { - case NAME_COLUMN: - return tr("Name"); - case UNIT_COLUMN: - return tr("Unit"); - case MISSION_COLUMN: - return tr("Mission"); - default: - // No action - break; + auto propertiesIt = COLUMN_PROPERTIES.find(section); + if (propertiesIt != COLUMN_PROPERTIES.cend()) { + // Role is either DisplayRole or SizeHintRole + return (role == Qt::DisplayRole) + ? QVariant{propertiesIt->m_Name} + : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; + } + else { + qWarning(LOG_VariableModel()) + << tr("Can't get header data (unknown column %1)").arg(section); } - - qWarning(LOG_VariableModel()) - << tr("Can't get header data (unknown column %1)").arg(section); } return QVariant{}; diff --git a/gui/src/Variable/VariableInspectorWidget.cpp b/gui/src/Variable/VariableInspectorWidget.cpp index a31c7d0..b83cd4d 100644 --- a/gui/src/Variable/VariableInspectorWidget.cpp +++ b/gui/src/Variable/VariableInspectorWidget.cpp @@ -21,6 +21,14 @@ VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) ui->tableView->setModel(sortFilterModel); + // Fixes column sizes + auto model = ui->tableView->model(); + const auto count = model->columnCount(); + for (auto i = 0; i < count; ++i) { + ui->tableView->setColumnWidth( + i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width()); + } + // Connection to show a menu when right clicking on the tree ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableView, &QTableView::customContextMenuRequested, this,