File indexing completed on 2024-10-06 07:36:07

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "mediasizehelper.h"
0005 
0006 #include "neochatconfig.h"
0007 
0008 MediaSizeHelper::MediaSizeHelper(QObject *parent)
0009     : QObject(parent)
0010 {
0011 }
0012 
0013 qreal MediaSizeHelper::contentMaxWidth() const
0014 {
0015     return m_contentMaxWidth;
0016 }
0017 
0018 void MediaSizeHelper::setContentMaxWidth(qreal contentMaxWidth)
0019 {
0020     if (contentMaxWidth < 0.0 || qFuzzyCompare(contentMaxWidth, 0.0)) {
0021         m_contentMaxWidth = -1.0;
0022         Q_EMIT contentMaxWidthChanged();
0023         Q_EMIT currentSizeChanged();
0024         return;
0025     }
0026     if (qFuzzyCompare(contentMaxWidth, m_contentMaxWidth)) {
0027         return;
0028     }
0029     m_contentMaxWidth = contentMaxWidth;
0030     Q_EMIT contentMaxWidthChanged();
0031     Q_EMIT currentSizeChanged();
0032 }
0033 
0034 qreal MediaSizeHelper::contentMaxHeight() const
0035 {
0036     return m_contentMaxHeight;
0037 }
0038 
0039 void MediaSizeHelper::setContentMaxHeight(qreal contentMaxHeight)
0040 {
0041     if (contentMaxHeight < 0.0 || qFuzzyCompare(contentMaxHeight, 0.0)) {
0042         m_contentMaxHeight = -1.0;
0043         Q_EMIT contentMaxHeightChanged();
0044         Q_EMIT currentSizeChanged();
0045         return;
0046     }
0047     if (qFuzzyCompare(contentMaxHeight, m_contentMaxHeight)) {
0048         return;
0049     }
0050     m_contentMaxHeight = contentMaxHeight;
0051     Q_EMIT contentMaxHeightChanged();
0052     Q_EMIT currentSizeChanged();
0053 }
0054 
0055 qreal MediaSizeHelper::mediaWidth() const
0056 {
0057     return m_mediaWidth;
0058 }
0059 
0060 void MediaSizeHelper::setMediaWidth(qreal mediaWidth)
0061 {
0062     if (mediaWidth < 0.0 || qFuzzyCompare(mediaWidth, 0.0)) {
0063         m_mediaWidth = -1.0;
0064         Q_EMIT mediaWidthChanged();
0065         Q_EMIT currentSizeChanged();
0066         return;
0067     }
0068     if (qFuzzyCompare(mediaWidth, m_mediaWidth)) {
0069         return;
0070     }
0071     m_mediaWidth = mediaWidth;
0072     Q_EMIT mediaWidthChanged();
0073     Q_EMIT currentSizeChanged();
0074 }
0075 
0076 qreal MediaSizeHelper::mediaHeight() const
0077 {
0078     return m_mediaHeight;
0079 }
0080 
0081 void MediaSizeHelper::setMediaHeight(qreal mediaHeight)
0082 {
0083     if (mediaHeight < 0.0 || qFuzzyCompare(mediaHeight, 0.0)) {
0084         m_mediaHeight = -1.0;
0085         Q_EMIT mediaHeightChanged();
0086         Q_EMIT currentSizeChanged();
0087         return;
0088     }
0089     if (qFuzzyCompare(mediaHeight, m_mediaHeight)) {
0090         return;
0091     }
0092     m_mediaHeight = mediaHeight;
0093     Q_EMIT mediaHeightChanged();
0094     Q_EMIT currentSizeChanged();
0095 }
0096 
0097 qreal MediaSizeHelper::resolvedMediaWidth() const
0098 {
0099     if (m_mediaWidth > 0.0) {
0100         return m_mediaWidth;
0101     }
0102     return widthLimit();
0103 }
0104 
0105 qreal MediaSizeHelper::resolvedMediaHeight() const
0106 {
0107     if (m_mediaHeight > 0.0) {
0108         return m_mediaHeight;
0109     }
0110     return widthLimit() / 16.0 * 9.0;
0111 }
0112 
0113 qreal MediaSizeHelper::aspectRatio() const
0114 {
0115     return resolvedMediaWidth() / resolvedMediaHeight();
0116 }
0117 
0118 bool MediaSizeHelper::limitWidth() const
0119 {
0120     // If actual data isn't available we'll be using a placeholder that is width
0121     // limited so return true.
0122     if (m_mediaWidth < 0.0 || m_mediaHeight < 0.0) {
0123         return true;
0124     }
0125     return m_mediaWidth >= m_mediaHeight;
0126 }
0127 
0128 qreal MediaSizeHelper::widthLimit() const
0129 {
0130     if (m_contentMaxWidth < 0.0) {
0131         return NeoChatConfig::self()->mediaMaxWidth();
0132     }
0133     return std::min(m_contentMaxWidth, qreal(NeoChatConfig::self()->mediaMaxWidth()));
0134 }
0135 
0136 qreal MediaSizeHelper::heightLimit() const
0137 {
0138     if (m_contentMaxHeight < 0.0) {
0139         return NeoChatConfig::self()->mediaMaxHeight();
0140     }
0141     return std::min(m_contentMaxHeight, qreal(NeoChatConfig::self()->mediaMaxHeight()));
0142 }
0143 
0144 QSize MediaSizeHelper::currentSize() const
0145 {
0146     if (limitWidth()) {
0147         qreal width = std::min(widthLimit(), resolvedMediaWidth());
0148         qreal height = width / aspectRatio();
0149         if (height > heightLimit()) {
0150             return QSize(qRound(heightLimit() * aspectRatio()), qRound(heightLimit()));
0151         }
0152         return QSize(qRound(width), qRound(height));
0153     } else {
0154         qreal height = std::min(heightLimit(), resolvedMediaHeight());
0155         qreal width = height * aspectRatio();
0156         if (width > widthLimit()) {
0157             return QSize(qRound(widthLimit()), qRound(widthLimit() / aspectRatio()));
0158         }
0159         return QSize(qRound(width), qRound(height));
0160     }
0161 }
0162 
0163 #include "moc_mediasizehelper.cpp"