File indexing completed on 2024-05-12 16:36:44

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2020 Dag Andersen <danders@get2net.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KPrPageTransition.h"
0021 
0022 #include <KoXmlReader.h>
0023 #include <KoGenStyle.h>
0024 #include <KoShapeLoadingContext.h>
0025 #include <KoOdfLoadingContext.h>
0026 #include <KoOdfStylesReader.h>
0027 #include <KoXmlNS.h>
0028 
0029 
0030 KPrPageTransition::KPrPageTransition()
0031 : m_type(Manual)
0032 , m_duration(0.0)
0033 {
0034     m_odfNames[Manual] = "manual";
0035     m_odfNames[Automatic] = "automatic";
0036     m_odfNames[SemiAutomatic] = "semi-automatic";
0037 }
0038 
0039 KPrPageTransition::Type KPrPageTransition::type() const
0040 {
0041     return m_type;
0042 }
0043 
0044 void KPrPageTransition::setType(KPrPageTransition::Type type)
0045 {
0046     m_type = type;
0047 }
0048 
0049 QString KPrPageTransition::odfName() const
0050 {
0051     return m_odfNames[m_type];
0052 }
0053 
0054 qreal KPrPageTransition::duration() const
0055 {
0056     return m_duration;
0057 }
0058 
0059 void KPrPageTransition::setDuration(qreal duration)
0060 {
0061     m_duration = duration;
0062 }
0063 
0064 int KPrPageTransition::milliseconds() const
0065 {
0066     return m_duration * 1000;
0067 }
0068 
0069 void KPrPageTransition::saveOdfAttributes(KoGenStyle &style) const
0070 {
0071     style.addProperty("presentation:transition-type", odfName());
0072     style.addProperty("presentation:duration", QString("PT%1S").arg(QString::number(m_duration)));
0073 }
0074 
0075 bool KPrPageTransition::loadOdfAttributes(const KoXmlElement &element, KoShapeLoadingContext &context)
0076 {
0077     KoOdfStylesReader& stylesReader = context.odfLoadingContext().stylesReader();
0078     const KoXmlElement * styleElement = stylesReader.findContentAutoStyle( element.attributeNS( KoXmlNS::draw, "style-name" ), "drawing-page" );
0079     if ( styleElement ) {
0080         KoXmlElement element = styleElement->namedItemNS( KoXmlNS::style, "drawing-page-properties" ).toElement();
0081         if (!element.isNull()) {
0082             if (element.hasAttributeNS(KoXmlNS::presentation, "transition-type")) {
0083                 m_type = m_odfNames.key(element.attributeNS(KoXmlNS::presentation, "transition-type"));
0084             }
0085             if (element.hasAttributeNS(KoXmlNS::presentation, "duration")) {
0086                 // NOTE: This is kept very simple as duration is specified as 'PTnS' (by LO and us)
0087                 // In general it can conform to the pattern: 'PnYndMnDTnHnMnS'
0088                 // However, as only seconds are actually used, reduces to 'PTnS'
0089                 // E.g. 'PT3.5S' for tree and a half seconds
0090                 m_duration = element.attributeNS(KoXmlNS::presentation, "duration").remove("PT").remove('S').toDouble();
0091             }
0092         }
0093     }
0094     return true;
0095 }
0096 
0097 QDebug operator<<(QDebug dbg, const KPrPageTransition &t)
0098 {
0099     return dbg.noquote().nospace() << "KPrPageTransition[" << t.odfName() << ',' << t.duration() << ']';
0100 }