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

0001 #ifndef oxygenscrollbar_data_h
0002 #define oxygenscrollbar_data_h
0003 
0004 //////////////////////////////////////////////////////////////////////////////
0005 // oxygenscrollbardata.h
0006 // data container for QScrollBar animations
0007 // -------------------
0008 //
0009 // SPDX-FileCopyrightText: 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0010 //
0011 // SPDX-License-Identifier: MIT
0012 //////////////////////////////////////////////////////////////////////////////
0013 
0014 #include "oxygenwidgetstatedata.h"
0015 
0016 #include <QStyle>
0017 
0018 namespace Oxygen
0019 {
0020 //* scrollbar data
0021 class ScrollBarData : public WidgetStateData
0022 {
0023     Q_OBJECT
0024     Q_PROPERTY(qreal addLineOpacity READ addLineOpacity WRITE setAddLineOpacity)
0025     Q_PROPERTY(qreal subLineOpacity READ subLineOpacity WRITE setSubLineOpacity)
0026 
0027 public:
0028     //* constructor
0029     ScrollBarData(QObject *parent, QWidget *target, int);
0030 
0031     //* event filter
0032     bool eventFilter(QObject *, QEvent *) override;
0033 
0034     using WidgetStateData::animation;
0035     using WidgetStateData::opacity;
0036 
0037     //* return animation for a given subcontrol
0038     const Animation::Pointer &animation(QStyle::SubControl) const;
0039 
0040     //* return default opacity for a given subcontrol
0041     qreal opacity(QStyle::SubControl) const;
0042 
0043     //* return default opacity for a given subcontrol
0044     bool isHovered(QStyle::SubControl control) const
0045     {
0046         switch (control) {
0047         case QStyle::SC_ScrollBarAddLine:
0048             return addLineArrowHovered();
0049         case QStyle::SC_ScrollBarSubLine:
0050             return subLineArrowHovered();
0051         default:
0052             return false;
0053         }
0054     }
0055 
0056     //* subControlRect
0057     QRect subControlRect(QStyle::SubControl control) const
0058     {
0059         switch (control) {
0060         case QStyle::SC_ScrollBarAddLine:
0061             return _addLineData._rect;
0062         case QStyle::SC_ScrollBarSubLine:
0063             return _subLineData._rect;
0064         default:
0065             return QRect();
0066         }
0067     }
0068 
0069     //* subcontrol rect
0070     void setSubControlRect(QStyle::SubControl control, const QRect &rect)
0071     {
0072         switch (control) {
0073         case QStyle::SC_ScrollBarAddLine:
0074             _addLineData._rect = rect;
0075             break;
0076 
0077         case QStyle::SC_ScrollBarSubLine:
0078             _subLineData._rect = rect;
0079             break;
0080 
0081         default:
0082             break;
0083         }
0084     }
0085 
0086     //* duration
0087     void setDuration(int duration) override
0088     {
0089         WidgetStateData::setDuration(duration);
0090         addLineAnimation().data()->setDuration(duration);
0091         subLineAnimation().data()->setDuration(duration);
0092     }
0093 
0094     //* addLine opacity
0095     void setAddLineOpacity(qreal value)
0096     {
0097         value = digitize(value);
0098         if (_addLineData._opacity == value)
0099             return;
0100         _addLineData._opacity = value;
0101         setDirty();
0102     }
0103 
0104     //* addLine opacity
0105     qreal addLineOpacity(void) const
0106     {
0107         return _addLineData._opacity;
0108     }
0109 
0110     //* subLine opacity
0111     void setSubLineOpacity(qreal value)
0112     {
0113         value = digitize(value);
0114         if (_subLineData._opacity == value)
0115             return;
0116         _subLineData._opacity = value;
0117         setDirty();
0118     }
0119 
0120     //* subLine opacity
0121     qreal subLineOpacity(void) const
0122     {
0123         return _subLineData._opacity;
0124     }
0125 
0126     //* mouse position
0127     QPoint position(void) const
0128     {
0129         return _position;
0130     }
0131 
0132 private Q_SLOTS:
0133 
0134     //* clear addLineRect
0135     void clearAddLineRect(void)
0136     {
0137         if (addLineAnimation().data()->direction() == Animation::Backward) {
0138             _addLineData._rect = QRect();
0139         }
0140     }
0141 
0142     //* clear subLineRect
0143     void clearSubLineRect(void)
0144     {
0145         if (subLineAnimation().data()->direction() == Animation::Backward) {
0146             _subLineData._rect = QRect();
0147         }
0148     }
0149 
0150 private:
0151     //* hoverMoveEvent
0152     void hoverMoveEvent(QObject *, QEvent *);
0153 
0154     //* hoverMoveEvent
0155     void hoverLeaveEvent(QObject *, QEvent *);
0156 
0157     //*@name hover flags
0158     //@{
0159 
0160     bool addLineArrowHovered(void) const
0161     {
0162         return _addLineData._hovered;
0163     }
0164 
0165     void setAddLineArrowHovered(bool value)
0166     {
0167         _addLineData._hovered = value;
0168     }
0169 
0170     bool subLineArrowHovered(void) const
0171     {
0172         return _subLineData._hovered;
0173     }
0174 
0175     void setSubLineArrowHovered(bool value)
0176     {
0177         _subLineData._hovered = value;
0178     }
0179 
0180     //@}
0181 
0182     //* update add line arrow
0183     void updateAddLineArrow(QStyle::SubControl);
0184 
0185     //* update sub line arrow
0186     void updateSubLineArrow(QStyle::SubControl);
0187 
0188     //*@name timelines
0189     //@{
0190 
0191     const Animation::Pointer &addLineAnimation(void) const
0192     {
0193         return _addLineData._animation;
0194     }
0195 
0196     const Animation::Pointer &subLineAnimation(void) const
0197     {
0198         return _subLineData._animation;
0199     }
0200 
0201     //* stores arrow data
0202     class Data
0203     {
0204     public:
0205         //* constructor
0206         Data(void)
0207             : _hovered(false)
0208             , _opacity(AnimationData::OpacityInvalid)
0209         {
0210         }
0211 
0212         //* true if hovered
0213         bool _hovered;
0214 
0215         //* animation
0216         Animation::Pointer _animation;
0217 
0218         //* opacity
0219         qreal _opacity;
0220 
0221         //* rect
0222         QRect _rect;
0223     };
0224 
0225     //* add line data (down arrow)
0226     Data _addLineData;
0227 
0228     //* subtract line data (up arrow)
0229     Data _subLineData;
0230 
0231     //* mouse position
0232     QPoint _position;
0233 };
0234 }
0235 
0236 #endif