File indexing completed on 2024-05-19 05:35:21

0001 /*
0002     SPDX-FileCopyrightText: 2009, 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0003     SPDX-License-Identifier: GPL-2.0-or-later AND MIT
0004 */
0005 
0006 #include "oxygentabbardata.h"
0007 
0008 #include <QHoverEvent>
0009 
0010 namespace Oxygen
0011 {
0012 //______________________________________________
0013 TabBarData::TabBarData(QObject *parent, QWidget *target, int duration)
0014     : AnimationData(parent, target)
0015 {
0016     _current._animation = new Animation(duration, this);
0017     setupAnimation(currentIndexAnimation(), "currentOpacity");
0018     currentIndexAnimation().data()->setDirection(Animation::Forward);
0019 
0020     _previous._animation = new Animation(duration, this);
0021     setupAnimation(previousIndexAnimation(), "previousOpacity");
0022     previousIndexAnimation().data()->setDirection(Animation::Backward);
0023 }
0024 
0025 //______________________________________________
0026 Animation::Pointer TabBarData::animation(const QPoint &position) const
0027 {
0028     if (!enabled())
0029         return Animation::Pointer();
0030 
0031     const QTabBar *local(qobject_cast<const QTabBar *>(target().data()));
0032     if (!local)
0033         return Animation::Pointer();
0034 
0035     int index(local->tabAt(position));
0036     if (index < 0)
0037         return Animation::Pointer();
0038     else if (index == currentIndex())
0039         return currentIndexAnimation();
0040     else if (index == previousIndex())
0041         return previousIndexAnimation();
0042     else
0043         return Animation::Pointer();
0044 }
0045 
0046 //______________________________________________
0047 bool TabBarData::updateState(const QPoint &position, bool hovered)
0048 {
0049     if (!enabled())
0050         return false;
0051 
0052     const QTabBar *local(qobject_cast<const QTabBar *>(target().data()));
0053     if (!local)
0054         return false;
0055 
0056     int index(local->tabAt(position));
0057     if (index < 0)
0058         return false;
0059 
0060     if (hovered) {
0061         if (index != currentIndex()) {
0062             if (currentIndex() >= 0) {
0063                 setPreviousIndex(currentIndex());
0064                 setCurrentIndex(-1);
0065                 previousIndexAnimation().data()->restart();
0066             }
0067 
0068             setCurrentIndex(index);
0069             currentIndexAnimation().data()->restart();
0070             return true;
0071 
0072         } else
0073             return false;
0074 
0075     } else if (index == currentIndex()) {
0076         setPreviousIndex(currentIndex());
0077         setCurrentIndex(-1);
0078         previousIndexAnimation().data()->restart();
0079         return true;
0080 
0081     } else
0082         return false;
0083 }
0084 
0085 //______________________________________________
0086 qreal TabBarData::opacity(const QPoint &position) const
0087 {
0088     if (!enabled())
0089         return OpacityInvalid;
0090 
0091     const QTabBar *local(qobject_cast<const QTabBar *>(target().data()));
0092     if (!local)
0093         return OpacityInvalid;
0094 
0095     int index(local->tabAt(position));
0096     if (index < 0)
0097         return OpacityInvalid;
0098     else if (index == currentIndex())
0099         return currentOpacity();
0100     else if (index == previousIndex())
0101         return previousOpacity();
0102     else
0103         return OpacityInvalid;
0104 }
0105 }