File indexing completed on 2024-05-12 16:33:43
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2010 Carlos Licea <carlos@kdab.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 "CommentShape.h" 0021 #include "Globals.h" 0022 #include "InitialsCommentShape.h" 0023 0024 #include <KoXmlReader.h> 0025 #include <KoXmlNS.h> 0026 #include <KoXmlWriter.h> 0027 0028 #include <KoShapeLoadingContext.h> 0029 #include <KoShapeApplicationData.h> 0030 #include <KoShapeSavingContext.h> 0031 #include <KoShapeRegistry.h> 0032 #include <KoTextShapeData.h> 0033 #include <KoColorBackground.h> 0034 #include <KoShapeStroke.h> 0035 #include <KoGradientBackground.h> 0036 #include <KoApplication.h> 0037 0038 #include <kmessagebox.h> 0039 #include <klocalizedstring.h> 0040 0041 #include <QPainter> 0042 #include <QGradient> 0043 #include <QBrush> 0044 #include <QRectF> 0045 0046 #define TextShapeId "TextShapeID" 0047 0048 CommentShape::CommentShape(KoDocumentResourceManager* resourceManager) 0049 : KoShapeContainer() 0050 , m_active(false) 0051 , m_comment(nullptr) 0052 { 0053 KoShapeContainer::setSize(initialsBoxSize); 0054 0055 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value(TextShapeId); 0056 if (factory) { 0057 m_comment = factory->createDefaultShape(resourceManager); 0058 } 0059 if ( !m_comment ) { 0060 // m_comment = new KoShape; 0061 KMessageBox::error( 0, i18n("The plugin needed for displaying comments is not present."), i18n("Plugin Missing") ); 0062 } 0063 if ( dynamic_cast<KoTextShapeData*>( m_comment->userData() ) == 0 ) { 0064 KMessageBox::error( 0, i18n("The plugin needed for displaying the comment is not compatible with the current version of the comment shape."), 0065 i18n("Plugin Incompatible") ); 0066 m_comment->setUserData( new KoTextShapeData ); 0067 } 0068 0069 m_comment->setSize(commentBoxSize); 0070 m_comment->setPosition(commentBoxPoint); 0071 m_comment->setVisible(false); 0072 0073 QLinearGradient* gradient= new QLinearGradient(commentBoxPoint, QPointF(commentBoxPoint.x(), commentBoxPoint.y() + commentBoxSize.height())); 0074 gradient->setCoordinateMode(QGradient::ObjectBoundingMode); 0075 gradient->setColorAt(0.0, Qt::yellow); 0076 gradient->setColorAt(1.0, QColor(254, 201, 7)); 0077 m_comment->setBackground(new KoGradientBackground(gradient)); 0078 0079 KoShapeStroke* stroke = new KoShapeStroke; 0080 stroke->setLineBrush(QBrush(Qt::black)); 0081 stroke->setLineWidth(0.5); 0082 m_comment->setStroke(stroke); 0083 0084 addShape(m_comment); 0085 0086 m_initials = new InitialsCommentShape(); 0087 m_initials->setSize(QSizeF(20,20)); 0088 m_initials->setSelectable(false); 0089 addShape(m_initials); 0090 } 0091 0092 CommentShape::~CommentShape() 0093 { 0094 delete m_comment; 0095 delete m_initials; 0096 } 0097 0098 bool CommentShape::loadOdf(const KoXmlElement& element, KoShapeLoadingContext& context) 0099 { 0100 loadOdfAttributes(element, context, OdfPosition); 0101 0102 KoXmlElement child; 0103 forEachElement(child, element) 0104 { 0105 if(child.namespaceURI() == KoXmlNS::dc) { 0106 if(child.localName() == "creator") { 0107 m_creator = child.text(); 0108 QStringList creatorNames = m_creator.split(' '); 0109 QString initials; 0110 if(KoApplication::isLeftToRight()) { 0111 foreach(const QString& name, creatorNames) { 0112 initials += name.left(1); 0113 } 0114 } 0115 else { 0116 foreach(const QString& name, creatorNames) { 0117 initials += name.right(1); 0118 } 0119 } 0120 m_initials->setInitials(initials); 0121 } 0122 else if(child.localName() == "date") { 0123 m_date = QDate::fromString(child.text(), Qt::ISODate); 0124 } 0125 } 0126 else if(child.namespaceURI() == KoXmlNS::text && child.localName() == "p") { 0127 commentData()->document()->setHtml(child.text().replace('\n', "<br>")); 0128 } 0129 } 0130 0131 return true; 0132 } 0133 0134 void CommentShape::saveOdf(KoShapeSavingContext& context) const 0135 { 0136 KoXmlWriter& writer = context.xmlWriter(); 0137 0138 writer.startElement("officeooo:annotation"); //TODO replace with standarized element name 0139 saveOdfAttributes(context, OdfPosition); 0140 0141 writer.startElement("dc:creator"); 0142 writer.addTextSpan(m_creator); 0143 writer.endElement();//dc:creator 0144 0145 writer.startElement("dc:date"); 0146 writer.addTextSpan(m_date.toString(Qt::ISODate)); 0147 writer.endElement();//dc:date 0148 0149 writer.startElement("text:p"); 0150 writer.addTextSpan(commentData()->document()->toPlainText()); 0151 writer.endElement();//text:p 0152 0153 writer.endElement();//officeooo:annotation 0154 } 0155 0156 void CommentShape::paintComponent(QPainter& /*painter*/, const KoViewConverter& /*converter*/, KoShapePaintingContext &) 0157 { 0158 } 0159 0160 void CommentShape::setSize(const QSizeF& /*size*/) 0161 { 0162 KoShapeContainer::setSize(initialsBoxSize); 0163 } 0164 0165 void CommentShape::toogleActive() 0166 { 0167 setActive(!m_active); 0168 } 0169 0170 bool CommentShape::isActive() const 0171 { 0172 return m_active; 0173 } 0174 0175 void CommentShape::setActive(bool active) 0176 { 0177 m_active = active; 0178 if(!m_active) { 0179 KoShapeContainer::setSize(initialsBoxSize); 0180 } 0181 else { 0182 KoShapeContainer::setSize(wholeSize); 0183 } 0184 m_initials->setActive(m_active); 0185 m_comment->setVisible(m_active); 0186 update(); 0187 } 0188 0189 0190 KoTextShapeData* CommentShape::commentData() const 0191 { 0192 return qobject_cast<KoTextShapeData*>( m_comment->userData() ); 0193 }