File indexing completed on 2024-12-01 04:37:02

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "selectedmessagebackgroundanimation.h"
0008 #include "model/messagesmodel.h"
0009 
0010 #include "colorsandmessageviewstyle.h"
0011 #include "ruqolawidgets_debug.h"
0012 #include <QPropertyAnimation>
0013 
0014 SelectedMessageBackgroundAnimation::SelectedMessageBackgroundAnimation(MessagesModel *model, QObject *parent)
0015     : QObject{parent}
0016     , mModel(model)
0017 {
0018     connect(this, &SelectedMessageBackgroundAnimation::backgroundColorChanged, this, &SelectedMessageBackgroundAnimation::slotBackgroundColorChanged);
0019 }
0020 
0021 SelectedMessageBackgroundAnimation::~SelectedMessageBackgroundAnimation() = default;
0022 
0023 QColor SelectedMessageBackgroundAnimation::backgroundColor() const
0024 {
0025     return m_backgroundColor;
0026 }
0027 
0028 void SelectedMessageBackgroundAnimation::slotBackgroundColorChanged()
0029 {
0030     if (mModel && mModelIndex.isValid()) {
0031         mModel->setData(mModelIndex, m_backgroundColor, MessagesModel::GoToMessageBackgroundColor);
0032     }
0033 }
0034 
0035 QPersistentModelIndex SelectedMessageBackgroundAnimation::modelIndex() const
0036 {
0037     return mModelIndex;
0038 }
0039 
0040 void SelectedMessageBackgroundAnimation::setModelIndex(const QPersistentModelIndex &newModelIndex)
0041 {
0042     mModelIndex = newModelIndex;
0043 }
0044 
0045 void SelectedMessageBackgroundAnimation::setBackgroundColor(const QColor &newBackgroundColor)
0046 {
0047     if (m_backgroundColor == newBackgroundColor)
0048         return;
0049     m_backgroundColor = newBackgroundColor;
0050     qCDebug(RUQOLAWIDGETS_LOG) << " Background color changed " << newBackgroundColor;
0051     Q_EMIT backgroundColorChanged();
0052 }
0053 
0054 void SelectedMessageBackgroundAnimation::start()
0055 {
0056     auto animation = new QPropertyAnimation(this, "backgroundColor", this);
0057     animation->setDuration(2000);
0058     const auto color = ColorsAndMessageViewStyle::self().schemeView().foreground(KColorScheme::NeutralText).color();
0059     animation->setStartValue(color);
0060     animation->setEndValue(QColor(Qt::transparent));
0061     animation->setEasingCurve(QEasingCurve::InOutQuad);
0062     animation->start();
0063     connect(animation, &QPropertyAnimation::finished, this, &SelectedMessageBackgroundAnimation::deleteLater);
0064 }
0065 
0066 MessagesModel *SelectedMessageBackgroundAnimation::messageModel() const
0067 {
0068     return mModel;
0069 }
0070 
0071 #include "moc_selectedmessagebackgroundanimation.cpp"