File indexing completed on 2024-05-19 05:28:48

0001 /*
0002  * SPDX-FileCopyrightText: 2009, 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later OR MIT
0005  */
0006 
0007 #include "breezeheaderviewdata.h"
0008 
0009 #include <QHoverEvent>
0010 #include <QTextStream>
0011 
0012 namespace Breeze
0013 {
0014 //______________________________________________
0015 HeaderViewData::HeaderViewData(QObject *parent, QWidget *target, int duration)
0016     : AnimationData(parent, target)
0017 {
0018     _current._animation = new Animation(duration, this);
0019     setupAnimation(currentIndexAnimation(), "currentOpacity");
0020     currentIndexAnimation().data()->setDirection(Animation::Forward);
0021 
0022     _previous._animation = new Animation(duration, this);
0023     setupAnimation(previousIndexAnimation(), "previousOpacity");
0024     previousIndexAnimation().data()->setDirection(Animation::Backward);
0025 }
0026 
0027 //______________________________________________
0028 bool HeaderViewData::updateState(const QPoint &position, bool hovered)
0029 {
0030     if (!enabled()) {
0031         return false;
0032     }
0033 
0034     const QHeaderView *local(qobject_cast<const QHeaderView *>(target().data()));
0035     if (!local) {
0036         return false;
0037     }
0038 
0039     const int index(local->logicalIndexAt(position));
0040     if (index < 0) {
0041         return false;
0042     }
0043 
0044     if (hovered) {
0045         if (index != currentIndex()) {
0046             if (currentIndex() >= 0) {
0047                 setPreviousIndex(currentIndex());
0048                 setCurrentIndex(-1);
0049                 previousIndexAnimation().data()->restart();
0050             }
0051 
0052             setCurrentIndex(index);
0053             currentIndexAnimation().data()->restart();
0054 
0055             return true;
0056 
0057         } else {
0058             return false;
0059         }
0060 
0061     } else if (index == currentIndex()) {
0062         setPreviousIndex(currentIndex());
0063         setCurrentIndex(-1);
0064         previousIndexAnimation().data()->restart();
0065         return true;
0066 
0067     } else {
0068         return false;
0069     }
0070 }
0071 
0072 //______________________________________________
0073 Animation::Pointer HeaderViewData::animation(const QPoint &position) const
0074 {
0075     if (!enabled()) {
0076         return Animation::Pointer();
0077     }
0078 
0079     const QHeaderView *local(qobject_cast<const QHeaderView *>(target().data()));
0080     if (!local) {
0081         return Animation::Pointer();
0082     }
0083 
0084     int index(local->logicalIndexAt(position));
0085     if (index < 0) {
0086         return Animation::Pointer();
0087     } else if (index == currentIndex()) {
0088         return currentIndexAnimation();
0089     } else if (index == previousIndex()) {
0090         return previousIndexAnimation();
0091     } else {
0092         return Animation::Pointer();
0093     }
0094 }
0095 
0096 //______________________________________________
0097 qreal HeaderViewData::opacity(const QPoint &position) const
0098 {
0099     if (!enabled()) {
0100         return OpacityInvalid;
0101     }
0102 
0103     const QHeaderView *local(qobject_cast<const QHeaderView *>(target().data()));
0104     if (!local) {
0105         return OpacityInvalid;
0106     }
0107 
0108     int index(local->logicalIndexAt(position));
0109     if (index < 0) {
0110         return OpacityInvalid;
0111     } else if (index == currentIndex()) {
0112         return currentOpacity();
0113     } else if (index == previousIndex()) {
0114         return previousOpacity();
0115     } else {
0116         return OpacityInvalid;
0117     }
0118 }
0119 
0120 //__________________________________________________________
0121 void HeaderViewData::setDirty() const
0122 {
0123     QHeaderView *header = qobject_cast<QHeaderView *>(target().data());
0124     if (!header) {
0125         return;
0126     }
0127 
0128     // get first and last index, sorted
0129     const int lastIndex(qMax(previousIndex(), currentIndex()));
0130     if (lastIndex < 0) {
0131         return;
0132     }
0133 
0134     int firstIndex(qMin(previousIndex(), currentIndex()));
0135     if (firstIndex < 0) {
0136         firstIndex = lastIndex;
0137     }
0138 
0139     // find relevant rectangle to be updated, in viewport coordinate
0140     QWidget *viewport(header->viewport());
0141     const int left = header->sectionViewportPosition(firstIndex);
0142     const int right = header->sectionViewportPosition(lastIndex) + header->sectionSize(lastIndex);
0143 
0144     // trigger update
0145     if (header->orientation() == Qt::Horizontal) {
0146         viewport->update(left, 0, right - left, header->height());
0147     } else {
0148         viewport->update(0, left, header->width(), right - left);
0149     }
0150 }
0151 
0152 }