File indexing completed on 2024-05-12 16:34:57

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2013 Mojtaba Shahi Senobari <mojtaba.shahi3000@gmail.com>
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 "AnnotationTextShape.h"
0021 
0022 #include "TextShapeDebug.h"
0023 
0024 #include <KoInlineTextObjectManager.h>
0025 #include <KoTextRangeManager.h>
0026 #include <KoShapeLoadingContext.h>
0027 #include <KoShapePaintingContext.h>
0028 #include <KoShapeSavingContext.h>
0029 #include <KoViewConverter.h>
0030 #include <KoXmlWriter.h>
0031 #include <KoXmlReader.h>
0032 #include <KoXmlNS.h>
0033 #include <KoTextLoader.h>
0034 #include <KoColorBackground.h>
0035 
0036 #include <QFont>
0037 #include <QPainter>
0038 #include <QPen>
0039 #include <QTextLayout>
0040 
0041 AnnotationTextShape::AnnotationTextShape(KoInlineTextObjectManager *inlineTextObjectManager,
0042                                          KoTextRangeManager *textRangeManager)
0043     : TextShape(inlineTextObjectManager, textRangeManager)
0044     , m_creator()
0045     , m_date()
0046     , m_dateString()
0047 {
0048     setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(Qt::yellow)));
0049     setAllowedInteraction(KoShape::ResizeAllowed, false);
0050     setAllowedInteraction(KoShape::MoveAllowed, false);
0051     setAllowedInteraction(KoShape::ShearingAllowed, false);
0052     setAllowedInteraction(KoShape::RotationAllowed, false);
0053 }
0054 
0055 AnnotationTextShape::~AnnotationTextShape()
0056 {
0057 }
0058 
0059 void AnnotationTextShape::setAnnotationTextData(KoTextShapeData *textShapeData)
0060 {
0061     m_textShapeData = textShapeData;
0062     m_textShapeData->setTopPadding(HeaderSpace);
0063     m_textShapeData->setLeftPadding(qreal(4.0)); // Make it a little nicer
0064     m_textShapeData->setRightPadding(qreal(4.0));
0065     m_textShapeData->setBottomPadding(qreal(4.0));
0066     m_textShapeData->setResizeMethod(KoTextShapeData::AutoGrowHeight);
0067 }
0068 
0069 void AnnotationTextShape::paintComponent(QPainter &painter, const KoViewConverter &converter,
0070                                          KoShapePaintingContext &paintcontext)
0071 {
0072     if (paintcontext.showAnnotations) {
0073         TextShape::paintComponent(painter, converter, paintcontext);
0074         QRectF clipRect = outlineRect();
0075 
0076         // Paint creator and of creation of the annotation.
0077         QPen peninfo (Qt::darkYellow);
0078         QFont serifFont("Times", HeaderFontSize, QFont::Bold);
0079         painter.setPen(peninfo);
0080         painter.setFont(serifFont);
0081 
0082         QDate date = QDate::fromString(m_date, Qt::ISODate);
0083         QString info = "  " + m_creator + "\n  " + date.toString(Qt::LocalDate);
0084         painter.drawText(clipRect, Qt::AlignTop, info);
0085     }
0086 }
0087 
0088 bool AnnotationTextShape::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0089 {
0090     //debugTextShape << "****** Start Load odf ******";
0091 
0092     KoTextLoader textLoader(context);
0093     QTextCursor cursor(textShapeData()->document());
0094 
0095     const QString localName(element.localName());
0096 
0097     if (localName == "annotation") {
0098 
0099         // FIXME: Load more attributes here
0100 
0101         // Load the metadata (author, date) and contents here.
0102         KoXmlElement el;
0103         forEachElement(el, element) {
0104             if (el.localName() == "creator" && el.namespaceURI() == KoXmlNS::dc) {
0105                 m_creator = el.text();
0106                 if (m_creator.isEmpty()) {
0107                     m_creator = "Unknown";
0108                 }
0109             }
0110             else if (el.localName() == "date" && el.namespaceURI() == KoXmlNS::dc) {
0111                 m_date = el.text();
0112             }
0113             else if (el.localName() == "datestring" && el.namespaceURI() == KoXmlNS::meta) {
0114                 m_dateString = el.text();
0115             }
0116         }
0117         textLoader.loadBody(element, cursor);
0118         //debugTextShape << "****** End Load ******";
0119     }
0120     else {
0121         // something pretty weird going on...
0122         return false;
0123     }
0124     return true;
0125 }
0126 
0127 void AnnotationTextShape::saveOdf(KoShapeSavingContext &context) const
0128 {
0129     //debugTextShape << " ****** Start saving annotation shape **********";
0130     KoXmlWriter *writer = &context.xmlWriter();
0131 
0132     writer->startElement("dc:creator", false);
0133     writer->addTextNode(m_creator);
0134     writer->endElement(); // dc:creator
0135     writer->startElement("dc:date", false);
0136     writer->addTextNode(m_date);
0137     writer->endElement(); // dc:date
0138 
0139     if (!m_dateString.isEmpty()) {
0140         writer->startElement("meta:date-string", false);
0141         writer->addTextNode(m_dateString);
0142         writer->endElement(); // meta:date-string
0143     }
0144 
0145     m_textShapeData->saveOdf(context, 0, 0, -1);
0146 }
0147 
0148 void AnnotationTextShape::setCreator(const QString &creator)
0149 {
0150     m_creator = creator;
0151 }
0152 
0153 QString AnnotationTextShape::creator() const
0154 {
0155     return m_creator;
0156 }
0157 
0158 void AnnotationTextShape::setDate(const QString &date)
0159 {
0160     m_date = date;
0161 }
0162 
0163 QString AnnotationTextShape::date() const
0164 {
0165     return m_date;
0166 }
0167 
0168 void AnnotationTextShape::setDateString(const QString &dateString)
0169 {
0170     m_dateString = dateString;
0171 }
0172 
0173 QString AnnotationTextShape::dateString() const
0174 {
0175     return m_dateString;
0176 }
0177 
0178 const qreal AnnotationTextShape::HeaderSpace = 25.0; // The space needed for the annotation header.
0179 const qreal AnnotationTextShape::HeaderFontSize = 6.0;