-/***************************************************************************
- * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program; if not, write to the *
- * Free Software Foundation, Inc., *
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
#include "kitemlistheaderwidget.h"
-
-#include <KAction>
-#include <KMenu>
-#include <kitemviews/kitemmodelbase.h>
+#include "kitemviews/kitemmodelbase.h"
#include <QApplication>
#include <QGraphicsSceneHoverEvent>
#include <QPainter>
#include <QStyleOptionHeader>
-#include <KDebug>
KItemListHeaderWidget::KItemListHeaderWidget(QGraphicsWidget* parent) :
QGraphicsWidget(parent),
m_automaticColumnResizing(true),
- m_model(0),
+ m_model(nullptr),
m_offset(0),
m_columns(),
m_columnWidths(),
}
if (m_model) {
- disconnect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
- this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
- disconnect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
- this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
+ disconnect(m_model, &KItemModelBase::sortRoleChanged,
+ this, &KItemListHeaderWidget::slotSortRoleChanged);
+ disconnect(m_model, &KItemModelBase::sortOrderChanged,
+ this, &KItemListHeaderWidget::slotSortOrderChanged);
}
m_model = model;
if (m_model) {
- connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
- this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
- connect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
- this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
+ connect(m_model, &KItemModelBase::sortRoleChanged,
+ this, &KItemListHeaderWidget::slotSortRoleChanged);
+ connect(m_model, &KItemModelBase::sortOrderChanged,
+ this, &KItemListHeaderWidget::slotSortOrderChanged);
}
}
{
foreach (const QByteArray& role, roles) {
if (!m_columnWidths.contains(role)) {
- m_columnWidths.remove(role);
m_preferredColumnWidths.remove(role);
}
}
void KItemListHeaderWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
- Q_UNUSED(option);
- Q_UNUSED(widget);
+ Q_UNUSED(option)
+ Q_UNUSED(widget)
if (!m_model) {
return;
++orderIndex;
}
- // Draw background without roles
- QStyleOption opt;
- opt.init(widget);
- opt.rect = QRect(x, 0, size().width() - x, size().height());
- opt.state |= QStyle::State_Horizontal;
- style()->drawControl(QStyle::CE_HeaderEmptyArea, &opt, painter);
-
if (!m_movingRole.pixmap.isNull()) {
Q_ASSERT(m_roleOperation == MoveRoleOperation);
painter->drawPixmap(m_movingRole.x, 0, m_movingRole.pixmap);
// Change the sort role and reset to the ascending order
const QByteArray previous = m_model->sortRole();
const QByteArray current = m_columns[m_pressedRoleIndex];
- m_model->setSortRole(current);
+ const bool resetSortOrder = m_model->sortOrder() == Qt::DescendingOrder;
+ m_model->setSortRole(current, !resetSortOrder);
emit sortRoleChanged(current, previous);
- if (m_model->sortOrder() == Qt::DescendingOrder) {
+ if (resetSortOrder) {
m_model->setSortOrder(Qt::AscendingOrder);
emit sortOrderChanged(Qt::AscendingOrder, Qt::DescendingOrder);
}
break;
}
+ case ResizeRoleOperation: {
+ const QByteArray pressedRole = m_columns[m_pressedRoleIndex];
+ const qreal currentWidth = m_columnWidths.value(pressedRole);
+ emit columnWidthChangeFinished(pressedRole, currentWidth);
+ break;
+ }
+
case MoveRoleOperation:
m_movingRole.pixmap = QPixmap();
m_movingRole.x = 0;
}
}
+void KItemListHeaderWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
+{
+ QGraphicsItem::mouseDoubleClickEvent(event);
+
+ const int roleIndex = roleIndexAt(event->pos());
+ if (roleIndex >= 0 && isAboveRoleGrip(event->pos(), roleIndex)) {
+ const QByteArray role = m_columns.at(roleIndex);
+
+ qreal previousWidth = columnWidth(role);
+ setColumnWidth(role, preferredColumnWidth(role));
+ qreal currentWidth = columnWidth(role);
+
+ emit columnWidthChanged(role, currentWidth, previousWidth);
+ emit columnWidthChangeFinished(role, currentWidth);
+ }
+}
+
void KItemListHeaderWidget::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
QGraphicsWidget::hoverEnterEvent(event);
void KItemListHeaderWidget::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
update();
}
void KItemListHeaderWidget::slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
{
- Q_UNUSED(current);
- Q_UNUSED(previous);
+ Q_UNUSED(current)
+ Q_UNUSED(previous)
update();
}
QWidget* widget) const
{
// The following code is based on the code from QHeaderView::paintSection().
- // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+ // SPDX-FileCopyrightText: 2011 Nokia Corporation and/or its subsidiary(-ies).
QStyleOptionHeader option;
option.section = orderIndex;
option.state = QStyle::State_None | QStyle::State_Raised | QStyle::State_Horizontal;
}
option.rect = rect.toRect();
+ bool paintBackgroundForEmptyArea = false;
+
if (m_columns.count() == 1) {
option.position = QStyleOptionHeader::OnlyOneSection;
} else if (orderIndex == 0) {
option.position = QStyleOptionHeader::Beginning;
} else if (orderIndex == m_columns.count() - 1) {
- option.position = QStyleOptionHeader::End;
+ // We are just painting the header for the last column. Check if there
+ // is some empty space to the right which needs to be filled.
+ if (rect.right() < size().width()) {
+ option.position = QStyleOptionHeader::Middle;
+ paintBackgroundForEmptyArea = true;
+ } else {
+ option.position = QStyleOptionHeader::End;
+ }
} else {
option.position = QStyleOptionHeader::Middle;
}
option.text = m_model->roleDescription(role);
style()->drawControl(QStyle::CE_Header, &option, painter, widget);
+
+ if (paintBackgroundForEmptyArea) {
+ option.state = QStyle::State_None | QStyle::State_Raised | QStyle::State_Horizontal;
+ option.section = m_columns.count();
+ option.sortIndicator = QStyleOptionHeader::None;
+
+ qreal backgroundRectX = rect.x() + rect.width();
+ QRectF backgroundRect(backgroundRectX, 0.0, size().width() - backgroundRectX, rect.height());
+ option.rect = backgroundRect.toRect();
+ option.position = QStyleOptionHeader::End;
+ option.text = QString();
+
+ style()->drawControl(QStyle::CE_Header, &option, painter, widget);
+ }
}
void KItemListHeaderWidget::updatePressedRoleIndex(const QPointF& pos)
return -1;
}
-#include "kitemlistheaderwidget.moc"