/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include "StdAfx.h" #include "ColumnGroupHeaderView.h" #include "ColumnGroupProxyModel.h" #include #include #include #include #include ColumnGroupHeaderView::ColumnGroupHeaderView(QWidget* parent) : QHeaderView(Qt::Horizontal, parent) , m_groupModel(nullptr) , m_showGroups(false) { setSectionsMovable(true); setStretchLastSection(true); setSortIndicatorShown(false); } void ColumnGroupHeaderView::setModel(QAbstractItemModel* model) { QHeaderView::setModel(model); m_groupModel = qobject_cast(model); if (m_groupModel) { connect(m_groupModel, SIGNAL(SortChanged()), this, SLOT(update())); } } QSize ColumnGroupHeaderView::sizeHint() const { QSize s = QHeaderView::sizeHint(); if (m_showGroups) { s.setHeight(s.height() + GroupViewHeight()); } return s; } bool ColumnGroupHeaderView::IsGroupsShown() const { return m_showGroups; } bool ColumnGroupHeaderView::event(QEvent* event) { switch (event->type()) { case QEvent::Paint: { if (!m_showGroups || !m_groupModel) { return true; } QPainter painter(this); painter.fillRect(rect(), QColor(145, 145, 145)); int xOffset = 10; int yOffset = 10; auto groups = m_groupModel->Groups(); m_groups.clear(); foreach(int column, groups) { const int width = sectionSize(column); const QRect r(xOffset, yOffset, width == 0 ? defaultSectionSize() : width, QHeaderView::sizeHint().height()); xOffset += r.width() + 10; yOffset += 10; if (column != groups.last()) { painter.setPen(Qt::black); painter.drawLine(r.bottomRight() + QPoint(-3, 0), r.bottomRight() + QPoint(-3, 3)); painter.drawLine(r.bottomRight() + QPoint(-3, 3), r.bottomRight() + QPoint(10, 3)); } m_groups.push_back({ r, column }); paintSection(&painter, r, column); } break; } case QEvent::MouseButtonRelease: { auto mouseEvent = static_cast(event); if (m_showGroups && m_groupModel) { foreach(const Group &group, m_groups) { if (group.rect.contains(mouseEvent->pos())) { auto sortOrder = m_groupModel->SortOrder(group.col); m_groupModel->sort(group.col, (sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder); } } } break; } default: return QHeaderView::event(event); break; } return true; } void ColumnGroupHeaderView::ShowGroups(bool showGroups) { m_showGroups = showGroups; Q_EMIT geometriesChanged(); } void ColumnGroupHeaderView::updateGeometries() { setViewportMargins(0, m_showGroups ? GroupViewHeight() : 0, 0, 0); QHeaderView::updateGeometries(); } int ColumnGroupHeaderView::GroupViewHeight() const { if (!m_groupModel) { return 0; } int groupCount = m_groupModel->Groups().size(); return QHeaderView::sizeHint().height() + qMax(0, groupCount - 1) * 10 + 20; } #include