File indexing completed on 2024-04-28 04:32:45

0001 /*
0002     SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "rotationjob_p.h"
0008 
0009 #include <QTransform>
0010 
0011 using namespace Okular;
0012 
0013 RotationJob::RotationJob(const QImage &image, Rotation oldRotation, Rotation newRotation, DocumentObserver *observer)
0014     : ThreadWeaver::QObjectDecorator(new RotationJobInternal(image, oldRotation, newRotation))
0015     , mObserver(observer)
0016     , m_pd(nullptr)
0017     , mRect(NormalizedRect())
0018     , mIsPartialUpdate(false)
0019 {
0020 }
0021 
0022 void RotationJob::setPage(PagePrivate *pd)
0023 {
0024     m_pd = pd;
0025 }
0026 
0027 void RotationJob::setRect(const NormalizedRect &rect)
0028 {
0029     mRect = rect;
0030 }
0031 
0032 void RotationJob::setIsPartialUpdate(bool partialUpdate)
0033 {
0034     mIsPartialUpdate = partialUpdate;
0035 }
0036 
0037 DocumentObserver *RotationJob::observer() const
0038 {
0039     return mObserver;
0040 }
0041 
0042 PagePrivate *RotationJob::page() const
0043 {
0044     return m_pd;
0045 }
0046 
0047 NormalizedRect RotationJob::rect() const
0048 {
0049     return mRect;
0050 }
0051 
0052 bool RotationJob::isPartialUpdate() const
0053 {
0054     return mIsPartialUpdate;
0055 }
0056 
0057 QTransform RotationJob::rotationMatrix(Rotation from, Rotation to)
0058 {
0059     QTransform matrix;
0060 
0061     if (from == Rotation0) {
0062         if (to == Rotation90) {
0063             matrix.rotate(90);
0064         } else if (to == Rotation180) {
0065             matrix.rotate(180);
0066         } else if (to == Rotation270) {
0067             matrix.rotate(270);
0068         }
0069     } else if (from == Rotation90) {
0070         if (to == Rotation180) {
0071             matrix.rotate(90);
0072         } else if (to == Rotation270) {
0073             matrix.rotate(180);
0074         } else if (to == Rotation0) {
0075             matrix.rotate(270);
0076         }
0077     } else if (from == Rotation180) {
0078         if (to == Rotation270) {
0079             matrix.rotate(90);
0080         } else if (to == Rotation0) {
0081             matrix.rotate(180);
0082         } else if (to == Rotation90) {
0083             matrix.rotate(270);
0084         }
0085     } else if (from == Rotation270) {
0086         if (to == Rotation0) {
0087             matrix.rotate(90);
0088         } else if (to == Rotation90) {
0089             matrix.rotate(180);
0090         } else if (to == Rotation180) {
0091             matrix.rotate(270);
0092         }
0093     }
0094 
0095     return matrix;
0096 }
0097 
0098 RotationJobInternal::RotationJobInternal(const QImage &image, Rotation oldRotation, Rotation newRotation)
0099     : mImage(image)
0100     , mOldRotation(oldRotation)
0101     , mNewRotation(newRotation)
0102 {
0103 }
0104 
0105 QImage RotationJobInternal::image() const
0106 {
0107     return mRotatedImage;
0108 }
0109 
0110 Rotation RotationJobInternal::rotation() const
0111 {
0112     return mNewRotation;
0113 }
0114 
0115 void RotationJobInternal::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
0116 {
0117     Q_UNUSED(self);
0118     Q_UNUSED(thread);
0119 
0120     if (mOldRotation == mNewRotation) {
0121         mRotatedImage = mImage;
0122         return;
0123     }
0124 
0125     const QTransform matrix = RotationJob::rotationMatrix(mOldRotation, mNewRotation);
0126 
0127     mRotatedImage = mImage.transformed(matrix);
0128 }
0129 
0130 #include "moc_rotationjob_p.cpp"