From da2e9b24ace34faba4413d805a79f7861901ef7a 2017-12-13 11:15:53 From: Thibaud Rabillard Date: 2017-12-13 11:15:53 Subject: [PATCH] TableModel for events --- diff --git a/extern/CatalogueAPI.cmake b/extern/CatalogueAPI.cmake index 7bacfa1..c18078c 100644 --- a/extern/CatalogueAPI.cmake +++ b/extern/CatalogueAPI.cmake @@ -40,7 +40,7 @@ ExternalProject_Add( GIT_REPOSITORY https://perrinel@hephaistos.lpp.polytechnique.fr/rhodecode/GIT_REPOSITORIES/LPP/Users/mperrinel/CatalogueAPI GIT_TAG develop - UPDATE_COMMAND ${GIT_EXECUTABLE} pull + UPDATE_COMMAND ${GIT_EXECUTABLE} pull origin develop PATCH_COMMAND "" SOURCE_DIR "${CATALOGUEAPI_SOURCES_PATH}" diff --git a/gui/include/Catalogue/CatalogueEventsTableModel.h b/gui/include/Catalogue/CatalogueEventsTableModel.h new file mode 100644 index 0000000..d452826 --- /dev/null +++ b/gui/include/Catalogue/CatalogueEventsTableModel.h @@ -0,0 +1,33 @@ +#ifndef SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H +#define SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H + +#include +#include + +#include + +class CatalogueEventsTableModel : public QAbstractTableModel { +public: + CatalogueEventsTableModel(QObject *parent = nullptr); + + void setEvents(const QVector &events); + DBEvent getEvent(int row) const; + + + // Model + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + + +private: + class CatalogueEventsTableModelPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_CATALOGUEEVENTSTABLEMODEL_H diff --git a/gui/meson.build b/gui/meson.build index 0f3997b..f7bf8af 100644 --- a/gui/meson.build +++ b/gui/meson.build @@ -107,7 +107,8 @@ gui_sources = [ 'src/Catalogue/CatalogueEventsWidget.cpp', 'src/Catalogue/CatalogueSideBarWidget.cpp', 'src/Catalogue/CatalogueInspectorWidget.cpp', - 'src/Catalogue/CatalogueTreeWidgetItem.cpp' + 'src/Catalogue/CatalogueTreeWidgetItem.cpp', + 'src/Catalogue/CatalogueEventsTableModel.cpp' ] gui_inc = include_directories(['include']) diff --git a/gui/src/Catalogue/CatalogueEventsTableModel.cpp b/gui/src/Catalogue/CatalogueEventsTableModel.cpp new file mode 100644 index 0000000..f69213d --- /dev/null +++ b/gui/src/Catalogue/CatalogueEventsTableModel.cpp @@ -0,0 +1,119 @@ +#include "Catalogue/CatalogueEventsTableModel.h" + +#include +#include +#include + +struct CatalogueEventsTableModel::CatalogueEventsTableModelPrivate { + QVector m_Events; + + enum class Column { Event, TStart, TEnd, Tags, Product, NbColumn }; + QStringList columnNames() + { + return QStringList{tr("Event"), tr("TStart"), tr("TEnd"), tr("Tags"), tr("Product")}; + } + + QVariant eventData(int col, const DBEvent &event) const + { + switch (static_cast(col)) { + case Column::Event: + return event.getName(); + case Column::TStart: + return DateUtils::dateTime(event.getTStart()); + case Column::TEnd: + return DateUtils::dateTime(event.getTEnd()); + case Column::Product: + return event.getProduct(); + case Column::Tags: { + QString tagList; + auto tags = const_cast(&event)->getTags(); + for (auto tag : tags) { + tagList += tag.getName(); + tagList += ' '; + } + + return tagList; + } + default: + break; + } + + Q_ASSERT(false); + return QStringLiteral("Unknown Data"); + } +}; + +CatalogueEventsTableModel::CatalogueEventsTableModel(QObject *parent) + : QAbstractTableModel(parent), + impl{spimpl::make_unique_impl()} +{ +} + +void CatalogueEventsTableModel::setEvents(const QVector &events) +{ + beginResetModel(); + impl->m_Events = events; + endResetModel(); +} + +DBEvent CatalogueEventsTableModel::getEvent(int row) const +{ + return impl->m_Events.value(row); +} + +int CatalogueEventsTableModel::rowCount(const QModelIndex &parent) const +{ + int r = impl->m_Events.count(); + return r; +} + +int CatalogueEventsTableModel::columnCount(const QModelIndex &parent) const +{ + int c = static_cast(CatalogueEventsTableModelPrivate::Column::NbColumn); + return c; +} + +Qt::ItemFlags CatalogueEventsTableModel::flags(const QModelIndex &index) const +{ + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + +QVariant CatalogueEventsTableModel::data(const QModelIndex &index, int role) const +{ + if (index.isValid()) { + auto event = impl->m_Events.value(index.row()); + + switch (role) { + case Qt::DisplayRole: + return impl->eventData(index.column(), event); + break; + } + } + + return QVariant{}; +} + +QVariant CatalogueEventsTableModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + return impl->columnNames().value(section); + } + + return QVariant(); +} + +void CatalogueEventsTableModel::sort(int column, Qt::SortOrder order) +{ + std::sort(impl->m_Events.begin(), impl->m_Events.end(), + [this, column, order](auto e1, auto e2) { + auto data1 = impl->eventData(column, e1); + auto data2 = impl->eventData(column, e2); + + auto result = data1.toString() < data2.toString(); + + return order == Qt::AscendingOrder ? result : !result; + }); + + emit dataChanged(QModelIndex(), QModelIndex()); +} diff --git a/gui/src/Catalogue/CatalogueEventsWidget.cpp b/gui/src/Catalogue/CatalogueEventsWidget.cpp index 5463fd5..64c2109 100644 --- a/gui/src/Catalogue/CatalogueEventsWidget.cpp +++ b/gui/src/Catalogue/CatalogueEventsWidget.cpp @@ -2,6 +2,7 @@ #include "ui_CatalogueEventsWidget.h" #include +#include #include #include #include @@ -11,12 +12,8 @@ const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss"); struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { - void addEventItem(const QStringList &data, QTableWidget *tableWidget); - enum class Column { Event, TStart, TEnd, Tags, Product, NbColumn }; - QStringList columnNames() { return QStringList{"Event", "TStart", "TEnd", "Tags", "Product"}; } - - QVector m_Events; + CatalogueEventsTableModel *m_Model = nullptr; }; @@ -27,6 +24,11 @@ CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) { ui->setupUi(this); + impl->m_Model = new CatalogueEventsTableModel(this); + ui->tableView->setModel(impl->m_Model); + + ui->tableView->setSortingEnabled(true); + connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { if (checked) { ui->btnChart->setChecked(false); @@ -39,33 +41,28 @@ CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) } }); - connect(ui->tableWidget, &QTableWidget::cellClicked, [this](auto row, auto column) { - auto event = impl->m_Events.value(row); + connect(ui->tableView, &QTableView::clicked, [this](auto index) { + auto event = impl->m_Model->getEvent(index.row()); emit this->eventSelected(event); }); - connect(ui->tableWidget, &QTableWidget::currentItemChanged, + connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentChanged, [this](auto current, auto previous) { - if (current && current->row() >= 0) { - auto event = impl->m_Events.value(current->row()); + if (current.isValid() && current.row() >= 0) { + auto event = impl->m_Model->getEvent(current.row()); emit this->eventSelected(event); } }); - connect(ui->tableWidget, &QTableWidget::itemSelectionChanged, [this]() { - auto selection = ui->tableWidget->selectedRanges(); - auto isNotMultiSelection - = selection.isEmpty() || (selection.count() == 1 && selection.first().rowCount() == 1); + connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { + auto isNotMultiSelection = ui->tableView->selectionModel()->selectedRows().count() <= 1; ui->btnChart->setEnabled(isNotMultiSelection); ui->btnTime->setEnabled(isNotMultiSelection); }); - Q_ASSERT(impl->columnNames().count() == (int)CatalogueEventsWidgetPrivate::Column::NbColumn); - ui->tableWidget->setColumnCount((int)CatalogueEventsWidgetPrivate::Column::NbColumn); - ui->tableWidget->setHorizontalHeaderLabels(impl->columnNames()); - ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); - ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); - ui->tableWidget->horizontalHeader()->setSortIndicatorShown(true); + ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + ui->tableView->horizontalHeader()->setSortIndicatorShown(true); } CatalogueEventsWidget::~CatalogueEventsWidget() @@ -75,41 +72,15 @@ CatalogueEventsWidget::~CatalogueEventsWidget() void CatalogueEventsWidget::populateWithCatalogue(const DBCatalogue &catalogue) { - ui->tableWidget->clearContents(); - ui->tableWidget->setRowCount(0); - auto &dao = sqpApp->catalogueController().getDao(); auto events = dao.getCatalogueEvents(catalogue); + QVector eventVector; for (auto event : events) { - impl->m_Events << event; - - auto tags = event.getTags(); - QString tagList; - for (auto tag : tags) { - tagList += tag.getName(); - tagList += ' '; - } - - impl->addEventItem({event.getName(), - DateUtils::dateTime(event.getTStart()).toString(DATETIME_FORMAT), - DateUtils::dateTime(event.getTEnd()).toString(DATETIME_FORMAT), tagList, - event.getProduct()}, - ui->tableWidget); + eventVector << event; } -} -void CatalogueEventsWidget::CatalogueEventsWidgetPrivate::addEventItem(const QStringList &data, - QTableWidget *tableWidget) -{ - tableWidget->setSortingEnabled(false); - auto row = tableWidget->rowCount(); - tableWidget->setRowCount(row + 1); - - for (auto i = 0; i < (int)Column::NbColumn; ++i) { - auto item = new QTableWidgetItem(data.value(i)); - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); - tableWidget->setItem(row, i, item); - } - tableWidget->setSortingEnabled(true); + ui->tableView->setSortingEnabled(false); + impl->m_Model->setEvents(eventVector); + ui->tableView->setSortingEnabled(true); } diff --git a/gui/ui/Catalogue/CatalogueEventsWidget.ui b/gui/ui/Catalogue/CatalogueEventsWidget.ui index 171698e..7a08a15 100644 --- a/gui/ui/Catalogue/CatalogueEventsWidget.ui +++ b/gui/ui/Catalogue/CatalogueEventsWidget.ui @@ -34,7 +34,7 @@ + - + :/icones/add.png:/icones/add.png @@ -48,7 +48,7 @@ - - + :/icones/remove.png:/icones/remove.png @@ -69,7 +69,7 @@ T - + :/icones/time.png:/icones/time.png @@ -86,7 +86,7 @@ G - + :/icones/chart.png:/icones/chart.png @@ -114,26 +114,35 @@ - - - true - + QAbstractItemView::SelectRows + + true + false + + true + false 25 + + false + - + + + +