File indexing completed on 2024-05-12 15:42:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im>
0003  *  SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #ifndef KIRIGAMI_UNITS_H
0009 #define KIRIGAMI_UNITS_H
0010 
0011 #include <QObject>
0012 #include <memory>
0013 
0014 #include "kirigami2_export.h"
0015 
0016 class QQmlEngine;
0017 
0018 namespace Kirigami {
0019 class Units;
0020 class UnitsPrivate;
0021 
0022 /**
0023  * @class IconSizes units.h <Kirigami/Units>
0024  *
0025  * Provides access to platform-dependent icon sizing
0026  */
0027 class KIRIGAMI2_EXPORT IconSizes : public QObject
0028 {
0029     Q_OBJECT
0030 
0031     Q_PROPERTY(int sizeForLabels READ sizeForLabels NOTIFY sizeForLabelsChanged)
0032     Q_PROPERTY(int small READ small NOTIFY smallChanged)
0033     Q_PROPERTY(int smallMedium READ smallMedium NOTIFY smallMediumChanged)
0034     Q_PROPERTY(int medium READ medium NOTIFY mediumChanged)
0035     Q_PROPERTY(int large READ large NOTIFY largeChanged)
0036     Q_PROPERTY(int huge READ huge NOTIFY hugeChanged)
0037     Q_PROPERTY(int enormous READ enormous NOTIFY enormousChanged)
0038 
0039 public:
0040     IconSizes(Units *units);
0041 
0042     int sizeForLabels() const;
0043     int small() const;
0044     int smallMedium() const;
0045     int medium() const;
0046     int large() const;
0047     int huge() const;
0048     int enormous() const;
0049 
0050     Q_INVOKABLE int roundedIconSize(int size) const;
0051 
0052 private:
0053     KIRIGAMI2_NO_EXPORT float iconScaleFactor() const;
0054 
0055     Units *m_units;
0056 
0057 Q_SIGNALS:
0058     void sizeForLabelsChanged();
0059     void smallChanged();
0060     void smallMediumChanged();
0061     void mediumChanged();
0062     void largeChanged();
0063     void hugeChanged();
0064     void enormousChanged();
0065 };
0066 
0067 /**
0068  * @class Units units.h <Kirigami/Units>
0069  *
0070  * A set of values to define semantically sizes and durations.
0071  */
0072 class KIRIGAMI2_EXPORT Units : public QObject
0073 {
0074     Q_OBJECT
0075 
0076     friend class IconSizes;
0077 
0078     /**
0079      * The fundamental unit of space that should be used for sizes, expressed in pixels.
0080      * Given the screen has an accurate DPI settings, it corresponds to the height of
0081      * the font's QtGui.FontMetrics.boundingRect</a>.
0082      */
0083     Q_PROPERTY(int gridUnit READ gridUnit WRITE setGridUnit NOTIFY gridUnitChanged)
0084 
0085     /**
0086      * Provides access to platform-dependent icon sizing
0087      *
0088      * The icon sizes provided are normalized for different DPI, so icons
0089      * will scale depending on the DPI.
0090      *
0091      * * sizeForLabels (the largest icon size that fits within
0092      * <a href="https://doc.qt.io/qt-5/qml-qtquick-fontmetrics.html#height-prop">fontMetrics.height</a>)
0093      * * small
0094      * * smallMedium
0095      * * medium
0096      * * large
0097      * * huge
0098      * * enormous
0099      * 
0100      * @since KDE Frameworks 5.80
0101      * @since org.kde.kirigami 2.16
0102      */
0103     Q_PROPERTY(IconSizes *iconSizes READ iconSizes CONSTANT)
0104 
0105     /**
0106      * This property holds the amount of spacing that should be used between smaller UI elements,
0107      * such as a small icon and a label in a button.
0108      * Internally, this size depends on the size of the default font as rendered on the screen,
0109      * so it takes user-configured font size and DPI into account.
0110      */
0111     Q_PROPERTY(int smallSpacing READ smallSpacing WRITE setSmallSpacing NOTIFY smallSpacingChanged)
0112 
0113     /**
0114      * This property holds the amount of spacing that should be used between medium UI elements,
0115      * such as buttons and text fields in a toolbar.
0116      * Internally, this size depends on the size of the default font as rendered on the screen,
0117      * so it takes user-configured font size and DPI into account.
0118      */
0119     Q_PROPERTY(int mediumSpacing READ mediumSpacing WRITE setMediumSpacing NOTIFY mediumSpacingChanged)
0120 
0121     /**
0122      * This property holds the amount of spacing that should be used between bigger UI elements,
0123      * such as a large icon and a heading in a card.
0124      * Internally, this size depends on the size of the default font as rendered on the screen,
0125      * so it takes user-configured font size and DPI into account.
0126      */
0127     Q_PROPERTY(int largeSpacing READ largeSpacing WRITE setLargeSpacing NOTIFY largeSpacingChanged)
0128 
0129     /**
0130      * The ratio between physical and device-independent pixels. This value does not depend on the \
0131      * size of the configured font. If you want to take font sizes into account when scaling elements,
0132      * use theme.mSize(theme.defaultFont), units.smallSpacing and units.largeSpacing.
0133      * The devicePixelRatio follows the definition of "device independent pixel" by Microsoft.
0134      *
0135      * @deprecated since 5.86. When using Qt's high DPI scaling, all sizes are
0136      * considered to be device-independent pixels, so this will simply return 1.
0137      */
0138     Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged)
0139 
0140     /**
0141      * This property should be used for specialty animations that benefit
0142      * from being even longer than longDuration.
0143      */
0144     Q_PROPERTY(int veryLongDuration READ veryLongDuration WRITE setVeryLongDuration NOTIFY veryLongDurationChanged)
0145 
0146     /**
0147      * This property should be used for longer, screen-covering animations, for opening and
0148      * closing of dialogs and other "not too small" animations
0149      */
0150     Q_PROPERTY(int longDuration READ longDuration WRITE setLongDuration NOTIFY longDurationChanged)
0151 
0152     /**
0153      * This property should be used for short animations, such as accentuating a UI event,
0154      * hover events, etc..
0155      */
0156     Q_PROPERTY(int shortDuration READ shortDuration WRITE setShortDuration NOTIFY shortDurationChanged)
0157 
0158     /**
0159      * This property should be used for elements that should have a hint of smoothness,
0160      * but otherwise animate near instantly.
0161      */
0162     Q_PROPERTY(int veryShortDuration READ veryShortDuration WRITE setVeryShortDuration NOTIFY veryShortDurationChanged)
0163 
0164     /**
0165      * Time in milliseconds equivalent to the theoretical human moment, which can be used
0166      * to determine whether how long to wait until the user should be informed of something,
0167      * or can be used as the limit for how long something should wait before being
0168      * automatically initiated.
0169      *
0170      * Some examples:
0171      *
0172      * - When the user types text in a search field, wait no longer than this duration after
0173      *   the user completes typing before starting the search
0174      * - When loading data which would commonly arrive rapidly enough to not require interaction,
0175      *   wait this long before showing a spinner
0176      *
0177      * This might seem an arbitrary number, but given the psychological effect that three
0178      * seconds seems to be what humans consider a moment (and in the case of waiting for
0179      * something to happen, a moment is that time when you think "this is taking a bit long,
0180      * isn't it?"), the idea is to postpone for just before such a conceptual moment. The reason
0181      * for the two seconds, rather than three, is to function as a middle ground: Not long enough
0182      * that the user would think that something has taken too long, for also not so fast as to
0183      * happen too soon.
0184      *
0185      * See also
0186      * https://www.psychologytoday.com/blog/all-about-addiction/201101/tick-tock-tick-hugs-and-life-in-3-second-intervals
0187      * (the actual paper is hidden behind an academic paywall and consequently not readily
0188      * available to us, so the source will have to be the blog entry above)
0189      *
0190      * \note This should __not__ be used as an animation duration, as it is deliberately not scaled according
0191      * to the animation settings. This is specifically for determining when something has taken too long and
0192      * the user should expect some kind of feedback. See ::veryShortDuration, ::shortDuration, ::longDuration, and
0193      * ::veryLongDuration for animation duration choices.
0194      *
0195      * @since KDE Frameworks 5.81
0196      * @since org.kde.kirigami 2.16
0197      */
0198     Q_PROPERTY(int humanMoment READ humanMoment WRITE setHumanMoment NOTIFY humanMomentChanged)
0199 
0200     /**
0201      * time in ms by which the display of tooltips will be delayed.
0202      *
0203      * @see <a href="https://doc.qt.io/qt-5/qml-qtquick-controls2-tooltip.html#delay-prop">ToolTip.delay</a>
0204      */
0205     Q_PROPERTY(int toolTipDelay READ toolTipDelay WRITE setToolTipDelay NOTIFY toolTipDelayChanged)
0206 
0207 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 86)
0208     /**
0209      * How much the mouse scroll wheel scrolls, expressed in lines of text.
0210      * Note: this is strictly for classical mouse wheels, touchpads 2 figer scrolling won't be affected
0211      */
0212     Q_PROPERTY(int wheelScrollLines READ wheelScrollLines NOTIFY wheelScrollLinesChanged)
0213 #endif
0214 
0215 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 86)
0216     /**
0217      * metrics used by the default font
0218      *
0219      * @deprecated since 5.86.0, Create your own TextMetrics object if needed.
0220      * For the roundedIconSize function, use Units.iconSizes.roundedIconSize instead
0221      */
0222     Q_PROPERTY(QObject *fontMetrics READ fontMetrics CONSTANT)
0223 #endif
0224 
0225     Q_PROPERTY(int maximumInteger READ maximumInteger CONSTANT)
0226 
0227 public:
0228     explicit Units(QObject *parent = nullptr);
0229     ~Units() override;
0230 
0231     int gridUnit() const;
0232     void setGridUnit(int size);
0233 
0234     int smallSpacing() const;
0235     void setSmallSpacing(int size);
0236 
0237     int mediumSpacing() const;
0238     void setMediumSpacing(int size);
0239 
0240     int largeSpacing() const;
0241     void setLargeSpacing(int size);
0242 
0243 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 86)
0244     // TODO KF6 remove
0245     KIRIGAMI2_DEPRECATED_VERSION(5, 86, "When using Qt scaling, this would return a value of 1")
0246     qreal devicePixelRatio() const;
0247 #endif
0248 
0249     int veryLongDuration() const;
0250     void setVeryLongDuration(int duration);
0251 
0252     int longDuration() const;
0253     void setLongDuration(int duration);
0254 
0255     int shortDuration() const;
0256     void setShortDuration(int duration);
0257 
0258     int veryShortDuration() const;
0259     void setVeryShortDuration(int duration);
0260 
0261     int humanMoment() const;
0262     void setHumanMoment(int duration);
0263 
0264     int toolTipDelay() const;
0265     void setToolTipDelay(int delay);
0266 
0267 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 86)
0268     // TODO KF6 remove
0269     KIRIGAMI2_DEPRECATED_VERSION(5, 86, "Use Qt.styleHints.wheelScrollLines instead")
0270     int wheelScrollLines() const;
0271     void setWheelScrollLines(int lines);
0272 #endif
0273 
0274     IconSizes *iconSizes() const;
0275 
0276     int maximumInteger() const;
0277 
0278 Q_SIGNALS:
0279     void gridUnitChanged();
0280     void smallSpacingChanged();
0281     void mediumSpacingChanged();
0282     void largeSpacingChanged();
0283     void devicePixelRatioChanged();
0284     void veryLongDurationChanged();
0285     void longDurationChanged();
0286     void shortDurationChanged();
0287     void veryShortDurationChanged();
0288     void humanMomentChanged();
0289     void toolTipDelayChanged();
0290     void wheelScrollLinesChanged();
0291 
0292 private:
0293 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 86)
0294     QObject *fontMetrics() const;
0295 #endif
0296 
0297     std::unique_ptr<UnitsPrivate> d;
0298 };
0299 
0300 }
0301 
0302 #endif