forked from qt-creator/qt-creator
Axivion: Consolidate column information
Splitting the column information across multiple lists was not smart as it complicates the handling and the planned extension. No change in functionality intended yet. Change-Id: I968edf28a6a279a14dbe223bf21e007b3e42f843 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
@@ -469,15 +469,16 @@ void IssuesWidget::updateTable()
|
||||
|
||||
QStringList columnHeaders;
|
||||
QStringList hiddenColumns;
|
||||
QList<bool> sortableColumns;
|
||||
QList<int> columnWidths;
|
||||
QList<IssueHeaderView::ColumnInfo> columnInfos;
|
||||
QList<Qt::Alignment> 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;
|
||||
|
@@ -12,7 +12,7 @@ namespace Axivion::Internal {
|
||||
|
||||
constexpr int ICON_SIZE = 16;
|
||||
|
||||
static QIcon iconForSorted(SortOrder order)
|
||||
static QIcon iconForSorted(std::optional<Qt::SortOrder> 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<bool> &sortable)
|
||||
void IssueHeaderView::setColumnInfoList(const QList<ColumnInfo> &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);
|
||||
|
@@ -6,19 +6,25 @@
|
||||
#include <QHeaderView>
|
||||
#include <QList>
|
||||
|
||||
namespace Axivion::Internal {
|
||||
#include <optional>
|
||||
|
||||
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<bool> &sortable);
|
||||
void setColumnWidths(const QList<int> &widths) { m_columnWidths = widths; }
|
||||
struct ColumnInfo
|
||||
{
|
||||
int width = 0;
|
||||
std::optional<Qt::SortOrder> 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<ColumnInfo> &infos);
|
||||
|
||||
std::optional<Qt::SortOrder> 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<bool> m_sortableColumns;
|
||||
QList<int> m_columnWidths;
|
||||
std::optional<Qt::SortOrder> m_currentSortOrder = std::nullopt;
|
||||
QList<ColumnInfo> m_columnInfoList;
|
||||
};
|
||||
|
||||
} // namespace Axivion::Internal
|
||||
|
Reference in New Issue
Block a user