diff --git a/src/plugins/axivion/axivionoutputpane.cpp b/src/plugins/axivion/axivionoutputpane.cpp index dc6ee0da26b..b72bc0e68fc 100644 --- a/src/plugins/axivion/axivionoutputpane.cpp +++ b/src/plugins/axivion/axivionoutputpane.cpp @@ -469,15 +469,16 @@ void IssuesWidget::updateTable() QStringList columnHeaders; QStringList hiddenColumns; - QList sortableColumns; - QList columnWidths; + QList columnInfos; QList alignments; for (const Dto::ColumnInfoDto &column : m_currentTableInfo->columns) { columnHeaders << column.header.value_or(column.key); if (!column.showByDefault) hiddenColumns << column.key; - sortableColumns << column.canSort; - columnWidths << column.width; + IssueHeaderView::ColumnInfo info; + info.sortable = column.canSort; + info.width = column.width; + columnInfos.append(info); alignments << alignmentFromString(column.alignment); } m_addedFilter->setText("0"); @@ -487,8 +488,7 @@ void IssuesWidget::updateTable() m_issuesModel->clear(); m_issuesModel->setHeader(columnHeaders); m_issuesModel->setAlignments(alignments); - m_headerView->setSortableColumns(sortableColumns); - m_headerView->setColumnWidths(columnWidths); + m_headerView->setColumnInfoList(columnInfos); int counter = 0; for (const QString &header : std::as_const(columnHeaders)) m_issuesView->setColumnHidden(counter++, hiddenColumns.contains(header)); @@ -705,7 +705,7 @@ IssueListSearch IssuesWidget::searchFromUi() const QTC_ASSERT(m_currentTableInfo, return search); QTC_ASSERT((ulong)column < m_currentTableInfo->columns.size(), return search); search.sort = m_currentTableInfo->columns.at(m_headerView->currentSortColumn()).key - + (m_headerView->currentSortOrder() == SortOrder::Ascending ? " asc" : " desc"); + + (m_headerView->currentSortOrder() == Qt::AscendingOrder ? " asc" : " desc"); } return search; diff --git a/src/plugins/axivion/issueheaderview.cpp b/src/plugins/axivion/issueheaderview.cpp index 3635568a5a8..019763d548b 100644 --- a/src/plugins/axivion/issueheaderview.cpp +++ b/src/plugins/axivion/issueheaderview.cpp @@ -12,7 +12,7 @@ namespace Axivion::Internal { constexpr int ICON_SIZE = 16; -static QIcon iconForSorted(SortOrder order) +static QIcon iconForSorted(std::optional order) { const Utils::Icon UNSORTED( {{":/axivion/images/sortAsc.png", Utils::Theme::IconsDisabledColor}, @@ -30,30 +30,24 @@ static QIcon iconForSorted(SortOrder order) static const QIcon sortedAsc = SORT_ASC.icon(); static const QIcon sortedDesc = SORT_DESC.icon(); - switch (order) { - case SortOrder::None: + if (!order) return unsorted; - case SortOrder::Ascending: - return sortedAsc; - case SortOrder::Descending: - return sortedDesc; - } - return {}; + return order.value() == Qt::AscendingOrder ? sortedAsc : sortedDesc; } -void IssueHeaderView::setSortableColumns(const QList &sortable) +void IssueHeaderView::setColumnInfoList(const QList &infos) { - m_sortableColumns = sortable; + m_columnInfoList = infos; int oldIndex = m_currentSortIndex; m_currentSortIndex = -1; - m_currentSortOrder = SortOrder::None; + m_currentSortOrder.reset(); if (oldIndex != -1) headerDataChanged(Qt::Horizontal, oldIndex, oldIndex); } int IssueHeaderView::currentSortColumn() const { - return m_currentSortOrder == SortOrder::None ? -1 : m_currentSortIndex; + return m_currentSortOrder ? m_currentSortIndex : -1; } void IssueHeaderView::mousePressEvent(QMouseEvent *event) @@ -85,12 +79,12 @@ void IssueHeaderView::mouseReleaseEvent(QMouseEvent *event) const int y = position.y(); const int logical = logicalIndexAt(position.x()); if (logical == m_lastToggleLogicalPos - && logical > -1 && logical < m_sortableColumns.size()) { - if (m_sortableColumns.at(logical)) { // ignore non-sortable + && logical > -1 && logical < m_columnInfoList.size()) { + if (m_columnInfoList.at(logical).sortable) { // ignore non-sortable if (y < height() / 2) // TODO improve - onToggleSort(logical, SortOrder::Ascending); + onToggleSort(logical, Qt::AscendingOrder); else - onToggleSort(logical, SortOrder::Descending); + onToggleSort(logical, Qt::DescendingOrder); } } } @@ -105,12 +99,16 @@ void IssueHeaderView::mouseMoveEvent(QMouseEvent *event) QHeaderView::mouseMoveEvent(event); } -void IssueHeaderView::onToggleSort(int index, SortOrder order) +void IssueHeaderView::onToggleSort(int index, Qt::SortOrder order) { - if (m_currentSortIndex == index) - m_currentSortOrder = (order == m_currentSortOrder) ? SortOrder::None : order; - else + if (m_currentSortIndex == index) { + if (!m_currentSortOrder || m_currentSortOrder.value() != order) + m_currentSortOrder = order; + else + m_currentSortOrder.reset(); + } else { m_currentSortOrder = order; + } int oldIndex = m_currentSortIndex; m_currentSortIndex = index; @@ -123,11 +121,12 @@ void IssueHeaderView::onToggleSort(int index, SortOrder order) QSize IssueHeaderView::sectionSizeFromContents(int logicalIndex) const { const QSize oldSize = QHeaderView::sectionSizeFromContents(logicalIndex); - const QSize newSize = logicalIndex < m_columnWidths.size() - ? QSize(qMax(m_columnWidths.at(logicalIndex), oldSize.width()), oldSize.height()) : oldSize; + const QSize newSize = logicalIndex < m_columnInfoList.size() + ? QSize(qMax(m_columnInfoList.at(logicalIndex).width, oldSize.width()), oldSize.height()) + : oldSize; const int margin = style()->pixelMetric(QStyle::PM_HeaderGripMargin, nullptr, this); - // add icon size and margin (default resize handle margin + 1) + // add icon size and margin (default resize handle margin) return QSize{newSize.width() + ICON_SIZE + margin, qMax(newSize.height(), ICON_SIZE)}; } @@ -136,13 +135,13 @@ void IssueHeaderView::paintSection(QPainter *painter, const QRect &rect, int log painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); - if (logicalIndex < 0 || logicalIndex >= m_sortableColumns.size()) + if (logicalIndex < 0 || logicalIndex >= m_columnInfoList.size()) return; - if (!m_sortableColumns.at(logicalIndex)) + if (!m_columnInfoList.at(logicalIndex).sortable) return; const int margin = style()->pixelMetric(QStyle::PM_HeaderGripMargin, nullptr, this); - const QIcon icon = iconForSorted(logicalIndex == m_currentSortIndex ? m_currentSortOrder : SortOrder::None); + const QIcon icon = iconForSorted(logicalIndex == m_currentSortIndex ? m_currentSortOrder : std::nullopt); const int offset = qMax((rect.height() - ICON_SIZE), 0) / 2; const int left = rect.left() + rect.width() - ICON_SIZE - margin; const QRect iconRect(left, offset, ICON_SIZE, ICON_SIZE); diff --git a/src/plugins/axivion/issueheaderview.h b/src/plugins/axivion/issueheaderview.h index c1c8230440c..748f2faa11d 100644 --- a/src/plugins/axivion/issueheaderview.h +++ b/src/plugins/axivion/issueheaderview.h @@ -6,19 +6,25 @@ #include #include -namespace Axivion::Internal { +#include -enum class SortOrder { None, Ascending, Descending }; +namespace Axivion::Internal { class IssueHeaderView : public QHeaderView { Q_OBJECT public: - explicit IssueHeaderView(QWidget *parent = nullptr) : QHeaderView(Qt::Horizontal, parent) {} - void setSortableColumns(const QList &sortable); - void setColumnWidths(const QList &widths) { m_columnWidths = widths; } + struct ColumnInfo + { + int width = 0; + std::optional sortOrder = std::nullopt; + bool sortable = false; + }; - SortOrder currentSortOrder() const { return m_currentSortOrder; } + explicit IssueHeaderView(QWidget *parent = nullptr) : QHeaderView(Qt::Horizontal, parent) {} + void setColumnInfoList(const QList &infos); + + std::optional currentSortOrder() const { return m_currentSortOrder; } int currentSortColumn() const; signals: @@ -32,14 +38,13 @@ protected: void mouseMoveEvent(QMouseEvent *event) override; private: - void onToggleSort(int index, SortOrder order); + void onToggleSort(int index, Qt::SortOrder order); bool m_dragging = false; bool m_maybeToggleSort = false; int m_lastToggleLogicalPos = -1; int m_currentSortIndex = -1; - SortOrder m_currentSortOrder = SortOrder::None; - QList m_sortableColumns; - QList m_columnWidths; + std::optional m_currentSortOrder = std::nullopt; + QList m_columnInfoList; }; } // namespace Axivion::Internal