X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/fdab593c01964ff4e5bf6c7cc093b802d0a1bbf7..9c7e196bddd692032245c8f068495f318434169d:/src/kitemviews/kitemlistwidget.cpp diff --git a/src/kitemviews/kitemlistwidget.cpp b/src/kitemviews/kitemlistwidget.cpp index b812bcf9d..c3c54a06c 100644 --- a/src/kitemviews/kitemlistwidget.cpp +++ b/src/kitemviews/kitemlistwidget.cpp @@ -1,8 +1,7 @@ /*************************************************************************** * Copyright (C) 2011 by Peter Penz * * * - * Based on the Itemviews NG project from Trolltech Labs: * - * http://qt.gitorious.org/qt-labs/itemviews-ng * + * Based on the Itemviews NG project from Trolltech Labs * * * * 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 * @@ -22,20 +21,25 @@ #include "kitemlistwidget.h" -#include "kitemlistselectiontoggle_p.h" #include "kitemlistview.h" -#include "kitemmodelbase.h" +#include "private/kitemlistselectiontoggle.h" -#include - -#include #include #include #include #include -KItemListWidget::KItemListWidget(QGraphicsItem* parent) : - QGraphicsWidget(parent, 0), +KItemListWidgetInformant::KItemListWidgetInformant() +{ +} + +KItemListWidgetInformant::~KItemListWidgetInformant() +{ +} + +KItemListWidget::KItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) : + QGraphicsWidget(parent, nullptr), + m_informant(informant), m_index(-1), m_selected(false), m_current(false), @@ -48,9 +52,10 @@ KItemListWidget::KItemListWidget(QGraphicsItem* parent) : m_styleOption(), m_siblingsInfo(), m_hoverOpacity(0), - m_hoverCache(0), - m_hoverAnimation(0), - m_selectionToggle(0) + m_hoverCache(nullptr), + m_hoverAnimation(nullptr), + m_selectionToggle(nullptr), + m_editedRole() { } @@ -63,7 +68,7 @@ void KItemListWidget::setIndex(int index) { if (m_index != index) { delete m_selectionToggle; - m_selectionToggle = 0; + m_selectionToggle = nullptr; if (m_hoverAnimation) { m_hoverAnimation->stop(); @@ -103,7 +108,7 @@ QHash KItemListWidget::data() const void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { - Q_UNUSED(option); + Q_UNUSED(option) if (m_alternateBackground) { const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase); @@ -111,7 +116,7 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o painter->fillRect(backgroundRect, backgroundColor); } - if (m_selected) { + if (m_selected && m_editedRole.isEmpty()) { const QStyle::State activeState(isActiveWindow() ? QStyle::State_Active : 0); drawItemStyleOption(painter, widget, activeState | QStyle::State_Enabled | @@ -119,9 +124,9 @@ void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* o QStyle::State_Item); } - if (isCurrent()) { + if (m_current && m_editedRole.isEmpty()) { QStyleOptionFocusRect focusRectOption; - focusRectOption.initFrom(widget); + initStyleOption(&focusRectOption); focusRectOption.rect = textFocusRect().toRect(); focusRectOption.state = QStyle::State_Enabled | QStyle::State_Item | QStyle::State_KeyboardFocusChange; if (m_selected) { @@ -170,8 +175,8 @@ QList KItemListWidget::visibleRoles() const void KItemListWidget::setColumnWidth(const QByteArray& role, qreal width) { - if (m_columnWidths.value(role) != width) { - const qreal previousWidth = width; + const qreal previousWidth = m_columnWidths.value(role); + if (previousWidth != width) { m_columnWidths.insert(role, width); columnWidthChanged(role, width, previousWidth); update(); @@ -185,10 +190,13 @@ qreal KItemListWidget::columnWidth(const QByteArray& role) const void KItemListWidget::setStyleOption(const KItemListStyleOption& option) { + if (m_styleOption == option) { + return; + } + const KItemListStyleOption previous = m_styleOption; clearHoverCache(); m_styleOption = option; - styleOptionChanged(option, previous); update(); } @@ -239,9 +247,9 @@ void KItemListWidget::setHovered(bool hovered) if (!m_hoverAnimation) { m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this); - const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 200; + const int duration = style()->styleHint(QStyle::SH_Widget_Animate) ? 200 : 1; m_hoverAnimation->setDuration(duration); - connect(m_hoverAnimation, SIGNAL(finished()), this, SLOT(slotHoverAnimationFinished())); + connect(m_hoverAnimation, &QPropertyAnimation::finished, this, &KItemListWidget::slotHoverAnimationFinished); } m_hoverAnimation->stop(); @@ -268,6 +276,13 @@ bool KItemListWidget::isHovered() const return m_hovered; } +void KItemListWidget::setHoverPosition(const QPointF& pos) +{ + if (m_selectionToggle) { + m_selectionToggle->setHovered(selectionToggleRect().contains(pos)); + } +} + void KItemListWidget::setAlternateBackground(bool enable) { if (m_alternateBackground != enable) { @@ -308,6 +323,20 @@ QBitArray KItemListWidget::siblingsInformation() const return m_siblingsInfo; } +void KItemListWidget::setEditedRole(const QByteArray& role) +{ + if (m_editedRole != role) { + const QByteArray previous = m_editedRole; + m_editedRole = role; + editedRoleChanged(role, previous); + } +} + +QByteArray KItemListWidget::editedRole() const +{ + return m_editedRole; +} + bool KItemListWidget::contains(const QPointF& point) const { if (!QGraphicsWidget::contains(point)) { @@ -335,66 +364,104 @@ QRectF KItemListWidget::expansionToggleRect() const return QRectF(); } +QPixmap KItemListWidget::createDragPixmap(const QStyleOptionGraphicsItem* option, + QWidget* widget) +{ + QPixmap pixmap(size().toSize() * widget->devicePixelRatio()); + pixmap.setDevicePixelRatio(widget->devicePixelRatio()); + pixmap.fill(Qt::transparent); + + QPainter painter(&pixmap); + + const bool oldAlternateBackground = m_alternateBackground; + const bool wasSelected = m_selected; + const bool wasHovered = m_hovered; + + setAlternateBackground(false); + setSelected(false); + setHovered(false); + + paint(&painter, option, widget); + + setAlternateBackground(oldAlternateBackground); + setSelected(wasSelected); + setHovered(wasHovered); + + return pixmap; +} + void KItemListWidget::dataChanged(const QHash& current, const QSet& roles) { - Q_UNUSED(current); - Q_UNUSED(roles); + Q_UNUSED(current) + Q_UNUSED(roles) } void KItemListWidget::visibleRolesChanged(const QList& current, const QList& previous) { - Q_UNUSED(current); - Q_UNUSED(previous); + Q_UNUSED(current) + Q_UNUSED(previous) } void KItemListWidget::columnWidthChanged(const QByteArray& role, qreal current, qreal previous) { - Q_UNUSED(role); - Q_UNUSED(current); - Q_UNUSED(previous); + Q_UNUSED(role) + Q_UNUSED(current) + Q_UNUSED(previous) } void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous) { - Q_UNUSED(current); - Q_UNUSED(previous); + Q_UNUSED(current) + Q_UNUSED(previous) } void KItemListWidget::currentChanged(bool current) { - Q_UNUSED(current); + Q_UNUSED(current) } void KItemListWidget::selectedChanged(bool selected) { - Q_UNUSED(selected); + Q_UNUSED(selected) } void KItemListWidget::hoveredChanged(bool hovered) { - Q_UNUSED(hovered); + Q_UNUSED(hovered) } void KItemListWidget::alternateBackgroundChanged(bool enabled) { - Q_UNUSED(enabled); + Q_UNUSED(enabled) } void KItemListWidget::siblingsInformationChanged(const QBitArray& current, const QBitArray& previous) { - Q_UNUSED(current); - Q_UNUSED(previous); + Q_UNUSED(current) + Q_UNUSED(previous) +} + +void KItemListWidget::editedRoleChanged(const QByteArray& current, const QByteArray& previous) +{ + Q_UNUSED(current) + Q_UNUSED(previous) } void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event) { QGraphicsWidget::resizeEvent(event); clearHoverCache(); + + if (m_selectionToggle) { + const QRectF& toggleRect = selectionToggleRect(); + m_selectionToggle->setPos(toggleRect.topLeft()); + m_selectionToggle->resize(toggleRect.size()); + } } qreal KItemListWidget::hoverOpacity() const @@ -404,9 +471,9 @@ qreal KItemListWidget::hoverOpacity() const void KItemListWidget::slotHoverAnimationFinished() { - if (!m_hovered) { - delete m_selectionToggle; - m_selectionToggle = 0; + if (!m_hovered && m_selectionToggle) { + m_selectionToggle->deleteLater(); + m_selectionToggle = nullptr; } } @@ -434,7 +501,7 @@ void KItemListWidget::setHoverOpacity(qreal opacity) if (m_hoverOpacity <= 0.0) { delete m_hoverCache; - m_hoverCache = 0; + m_hoverCache = nullptr; } update(); @@ -443,20 +510,17 @@ void KItemListWidget::setHoverOpacity(qreal opacity) void KItemListWidget::clearHoverCache() { delete m_hoverCache; - m_hoverCache = 0; + m_hoverCache = nullptr; } void KItemListWidget::drawItemStyleOption(QPainter* painter, QWidget* widget, QStyle::State styleState) { - const QRect textBounds = textRect().toRect(); - - QStyleOptionViewItemV4 viewItemOption; - viewItemOption.initFrom(widget); + QStyleOptionViewItem viewItemOption; + initStyleOption(&viewItemOption); viewItemOption.state = styleState; - viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne; + viewItemOption.viewItemPosition = QStyleOptionViewItem::OnlyOne; viewItemOption.showDecorationSelected = true; - viewItemOption.rect = textBounds; - widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); + viewItemOption.rect = selectionRect().toRect(); + style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget); } -#include "kitemlistwidget.moc"