Warning, file /office/calligra/libs/odf/KoPageLayout.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
0003    Copyright 2002, 2003 David Faure <faure@kde.org>
0004    Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
0005    Copyright 2007, 2010 Thomas Zander <zander@kde.org>
0006    Copyright 2009 Inge Wallin <inge@lysator.liu.se>
0007 
0008    This library is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU Library General Public
0010    License as published by the Free Software Foundation; either
0011    version 2 of the License, or (at your option) any later version.
0012 
0013    This library is distributed in the hope that it will be useful,
0014    but WITHOUT ANY WARRANTY; without even the implied warranty of
0015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016    Library General Public License for more details.
0017 
0018    You should have received a copy of the GNU Library General Public License
0019    along with this library; see the file COPYING.LIB.  If not, write to
0020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021  * Boston, MA 02110-1301, USA.
0022 */
0023 
0024 #include "KoPageLayout.h"
0025 
0026 #include <OdfDebug.h>
0027 
0028 #include "KoXmlNS.h"
0029 #include "KoUnit.h"
0030 #include "KoXmlReader.h"
0031 
0032 KoGenStyle KoPageLayout::saveOdf() const
0033 {
0034     KoGenStyle style(KoGenStyle::PageLayoutStyle);
0035 
0036     // Save page dimension.
0037     style.addPropertyPt("fo:page-width", width);
0038     style.addPropertyPt("fo:page-height", height);
0039 
0040     // Save margins. If all margins are the same, only one value needs to be saved.
0041     if (leftMargin == topMargin && leftMargin == rightMargin && leftMargin == bottomMargin) {
0042         style.addPropertyPt("fo:margin", leftMargin);
0043     }
0044     else {
0045         style.addPropertyPt("fo:margin-left", leftMargin);
0046         style.addPropertyPt("fo:margin-right", rightMargin);
0047         style.addPropertyPt("fo:margin-top", topMargin);
0048         style.addPropertyPt("fo:margin-bottom", bottomMargin);
0049     }
0050 
0051     // Save padding. If all paddings are the same, only one value needs to be saved.
0052     if (leftPadding == topPadding && leftPadding == rightPadding && leftPadding == bottomPadding) {
0053         style.addPropertyPt("fo:padding", leftPadding);
0054     }
0055     else {
0056         style.addPropertyPt("fo:padding-left", leftPadding);
0057         style.addPropertyPt("fo:padding-right", rightPadding);
0058         style.addPropertyPt("fo:padding-top", topPadding);
0059         style.addPropertyPt("fo:padding-bottom", bottomPadding);
0060     }
0061 
0062     // If there are any page borders, add them to the style.
0063     border.saveOdf(style);
0064 
0065     style.addProperty("style:print-orientation",
0066                       (orientation == KoPageFormat::Landscape
0067                        ? "landscape" : "portrait"));
0068     return style;
0069 }
0070 
0071 void KoPageLayout::loadOdf(const KoXmlElement &style)
0072 {
0073     KoXmlElement  properties(KoXml::namedItemNS(style, KoXmlNS::style,
0074                                                 "page-layout-properties"));
0075 
0076     if (!properties.isNull()) {
0077         KoPageLayout standard;
0078 
0079         // Page dimension -- width / height
0080         width = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "page-width"),
0081                                    standard.width);
0082         height = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "page-height"),
0083                                     standard.height);
0084 
0085         // Page orientation
0086         if (properties.attributeNS(KoXmlNS::style, "print-orientation", QString()) == "portrait")
0087             orientation = KoPageFormat::Portrait;
0088         else
0089             orientation = KoPageFormat::Landscape;
0090 
0091         // Margins.  Check if there is one "margin" attribute and use it for all
0092         // margins if there is.  Otherwise load the individual margins.
0093         if (properties.hasAttributeNS(KoXmlNS::fo, "margin")) {
0094             leftMargin  = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "margin"));
0095             topMargin = leftMargin;
0096             rightMargin = leftMargin;
0097             bottomMargin = leftMargin;
0098         } else {
0099             /*
0100                 If one of the individual margins is specified then the default for the others is zero.
0101                 Otherwise all of them are set to 20mm.
0102             */
0103             qreal defaultValue = 0;
0104             if (!(properties.hasAttributeNS(KoXmlNS::fo, "margin-left")
0105                     || properties.hasAttributeNS(KoXmlNS::fo, "margin-top")
0106                     || properties.hasAttributeNS(KoXmlNS::fo, "margin-right")
0107                     || properties.hasAttributeNS(KoXmlNS::fo, "margin-bottom")))
0108                 defaultValue = MM_TO_POINT(20.0); // no margin specified at all, lets make it 20mm
0109 
0110             leftMargin   = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "margin-left"), defaultValue);
0111             topMargin    = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "margin-top"), defaultValue);
0112             rightMargin  = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "margin-right"), defaultValue);
0113             bottomMargin = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "margin-bottom"), defaultValue);
0114         }
0115 
0116         // Padding.  Same reasoning as for margins
0117         if (properties.hasAttributeNS(KoXmlNS::fo, "padding")) {
0118             leftPadding  = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "padding"));
0119             topPadding = leftPadding;
0120             rightPadding = leftPadding;
0121             bottomPadding = leftPadding;
0122         }
0123         else {
0124             leftPadding   = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "padding-left"));
0125             topPadding    = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "padding-top"));
0126             rightPadding  = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "padding-right"));
0127             bottomPadding = KoUnit::parseValue(properties.attributeNS(KoXmlNS::fo, "padding-bottom"));
0128         }
0129 
0130         // Parse border properties if there are any.
0131         border.loadOdf(properties);
0132 
0133         // guessFormat takes millimeters
0134         if (orientation == KoPageFormat::Landscape)
0135             format = KoPageFormat::guessFormat(POINT_TO_MM(height), POINT_TO_MM(width));
0136         else
0137             format = KoPageFormat::guessFormat(POINT_TO_MM(width), POINT_TO_MM(height));
0138     }
0139 }
0140 
0141 bool KoPageLayout::operator==(const KoPageLayout &l) const
0142 {
0143     return qFuzzyCompare(width,l.width)
0144         && qFuzzyCompare(height,l.height)
0145         && qFuzzyCompare(leftMargin,l.leftMargin)
0146         && qFuzzyCompare(rightMargin,l.rightMargin)
0147         && qFuzzyCompare(topMargin,l.topMargin)
0148         && qFuzzyCompare(bottomMargin,l.bottomMargin)
0149         && qFuzzyCompare(pageEdge,l.pageEdge)
0150         && qFuzzyCompare(bindingSide,l.bindingSide)
0151         && border == l.border;
0152 }
0153 
0154 bool KoPageLayout::operator!=(const KoPageLayout& l) const
0155 {
0156     return !((*this) == l);
0157 }
0158 
0159 KoPageLayout::KoPageLayout()
0160   : format(KoPageFormat::defaultFormat())
0161   , orientation(KoPageFormat::Portrait)
0162   , width(MM_TO_POINT(KoPageFormat::width(format, orientation)))
0163   , height(MM_TO_POINT(KoPageFormat::height(format, orientation)))
0164   , leftMargin(MM_TO_POINT(20.0))
0165   , rightMargin(MM_TO_POINT(20.0))
0166   , topMargin(MM_TO_POINT(20.0))
0167   , bottomMargin(MM_TO_POINT(20.0))
0168   , pageEdge(-1)
0169   , bindingSide(-1)
0170   , leftPadding(0)
0171   , rightPadding(0)
0172   , topPadding(0)
0173   , bottomPadding(0)
0174   , border()
0175 {
0176 }