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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
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 "KPrPlaceholderStrategy.h"
0021 
0022 #include "KPrPlaceholderPictureStrategy.h"
0023 #include "KPrPlaceholderTextStrategy.h"
0024 #include "StageDebug.h"
0025 
0026 #include <QPainter>
0027 #include <QPen>
0028 #include <QTextOption>
0029 
0030 #include <klocalizedstring.h>
0031 
0032 #include <KoShape.h>
0033 #include <KoShapeFactoryBase.h>
0034 #include <KoShapeRegistry.h>
0035 #include <KoShapeSavingContext.h>
0036 #include <KoShapeLoadingContext.h>
0037 #include <KoXmlWriter.h>
0038 
0039 
0040 static const class PlaceholderData {
0041     public:
0042     const char * m_presentationClass;
0043     const char * m_shapeId;
0044     const char * m_xmlElement;
0045     const char * m_text;
0046 } placeholderData[] = {
0047     { "title", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add a title" ) },
0048     { "outline", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add an outline" ) },
0049     { "subtitle", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add a text" ) },
0050     { "text", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add a text" ) },
0051     { "notes", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add notes" ) },
0052     /*
0053     { "date-time", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add data/time" ) },
0054     { "footer", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add footer" ) },
0055     { "header", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add header" ) },
0056     { "page-number", "TextShapeID", "<draw:text-box/>", I18N_NOOP( "Double click to add page number" ) },
0057     */
0058     { "graphic", "PictureShape", "<draw:image xlink:href=\"\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>", 
0059                                        I18N_NOOP( "Double click to add a picture" ) },
0060     { "chart", "ChartShape", "<draw:object xlink:href=\"\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>",
0061                                        I18N_NOOP( "Double click to add a chart" ) },
0062     { "object", "ChartShape", "<draw:object xlink:href=\"\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>",
0063                                        I18N_NOOP( "Double click to add an object" ) }
0064 };
0065 
0066 static QMap<QString, const PlaceholderData *> s_placeholderMap;
0067 
0068 void fillPlaceholderMap()
0069 {
0070     const unsigned int numPlaceholderData = sizeof( placeholderData ) / sizeof( *placeholderData );
0071     for ( unsigned int i = 0; i < numPlaceholderData; ++i ) {
0072         s_placeholderMap.insert( placeholderData[i].m_presentationClass, &placeholderData[i] );
0073     }
0074 }
0075 
0076 KPrPlaceholderStrategy * KPrPlaceholderStrategy::create( const QString & presentationClass )
0077 {
0078     if ( s_placeholderMap.isEmpty() ) {
0079         fillPlaceholderMap();
0080     }
0081 
0082     KPrPlaceholderStrategy * strategy = 0;
0083     if ( presentationClass == "graphic" ) {
0084         strategy = new KPrPlaceholderPictureStrategy();
0085     }
0086     // TODO make nice
0087     else if ( presentationClass == "outline" || presentationClass == "title" || presentationClass == "subtitle" ) {
0088         strategy = new KPrPlaceholderTextStrategy( presentationClass );
0089     }
0090     else {
0091         if ( s_placeholderMap.contains( presentationClass ) ) {
0092             strategy = new KPrPlaceholderStrategy( presentationClass );
0093         }
0094         else {
0095             warnStage << "Unsupported placeholder strategy:" << presentationClass;
0096         }
0097     }
0098     return strategy;
0099 }
0100 
0101 bool KPrPlaceholderStrategy::supported( const QString & presentationClass )
0102 {
0103     if ( s_placeholderMap.isEmpty() ) {
0104         fillPlaceholderMap();
0105     }
0106 
0107     return s_placeholderMap.contains( presentationClass );
0108 }
0109 
0110 KPrPlaceholderStrategy::KPrPlaceholderStrategy( const QString & presentationClass )
0111 : m_placeholderData( s_placeholderMap[presentationClass] )
0112 {
0113 }
0114 
0115 KPrPlaceholderStrategy::~KPrPlaceholderStrategy()
0116 {
0117 }
0118 
0119 KoShape *KPrPlaceholderStrategy::createShape(KoDocumentResourceManager *rm)
0120 {
0121     KoShape * shape = 0;
0122     KoShapeFactoryBase * factory = KoShapeRegistry::instance()->value( m_placeholderData->m_shapeId );
0123     Q_ASSERT( factory );
0124     if ( factory ) {
0125         shape = factory->createDefaultShape(rm);
0126     } else {
0127         warnStage << "no factory found for placeholder";
0128     }
0129     return shape;
0130 }
0131 
0132 void KPrPlaceholderStrategy::paint( QPainter & painter, const KoViewConverter &converter, const QRectF & rect, KoShapePaintingContext &/*paintcontext*/)
0133 {
0134     KoShape::applyConversion( painter, converter );
0135     QPen penText( Qt::black );
0136     painter.setPen( penText );
0137     //painter.setFont()
0138     QTextOption options( Qt::AlignCenter );
0139     options.setWrapMode( QTextOption::WordWrap );
0140     painter.drawText( rect, text(), options );
0141 
0142     QPen pen(Qt::gray, 0);
0143     //pen.setStyle( Qt::DashLine ); // endless loop
0144     //pen.setStyle( Qt::DotLine ); // endless loop
0145     //pen.setStyle( Qt::DashDotLine ); // endless loop
0146     painter.setPen( pen );
0147     painter.drawRect( rect );
0148 }
0149 
0150 void KPrPlaceholderStrategy::saveOdf( KoShapeSavingContext & context )
0151 {
0152     KoXmlWriter & writer = context.xmlWriter();
0153     writer.addCompleteElement( m_placeholderData->m_xmlElement );
0154 }
0155 
0156 bool KPrPlaceholderStrategy::loadOdf( const KoXmlElement & element, KoShapeLoadingContext & context )
0157 {
0158     Q_UNUSED( element );
0159     Q_UNUSED( context );
0160     return true;
0161 }
0162 
0163 QString KPrPlaceholderStrategy::text() const
0164 {
0165     return i18n( m_placeholderData->m_text );
0166 }
0167 
0168 void KPrPlaceholderStrategy::init(KoDocumentResourceManager *)
0169 {
0170 }
0171 
0172 KoShapeUserData * KPrPlaceholderStrategy::userData() const
0173 {
0174     return 0;
0175 }