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

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2008 James Hogan <james@albanarts.com>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 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     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA  02110-1301  USA
0018 */
0019 
0020 #include "KPrViewAdaptor.h"
0021 #include "KPrView.h"
0022 #include "KPrViewModePresentation.h"
0023 #include "KPrAnimationDirector.h"
0024 #include "KPrDocument.h"
0025 #include "KPrNotes.h"
0026 #include "KPrPage.h"
0027 #include <KoTextShapeData.h>
0028 
0029 #include <QUrl>
0030 #include <QTextDocument>
0031 
0032 KPrViewAdaptor::KPrViewAdaptor( KPrView* view )
0033 : KoViewAdaptor( view )
0034 , m_view( view )
0035 {
0036     KPrDocument *doc = m_view->kprDocument();
0037     connect( doc, SIGNAL(activeCustomSlideShowChanged(QString)), this, SIGNAL(activeCustomSlideShowChanged(QString)) );
0038     connect( doc, SIGNAL(customSlideShowsModified()), this, SIGNAL(customSlideShowsModified()) );
0039 
0040     // We need to know when the presentation is started and stopped, and when it is navigated
0041     connect( m_view->presentationMode(), SIGNAL(activated()), this, SLOT(presentationActivated()) );
0042     connect( m_view->presentationMode(), SIGNAL(deactivated()), this, SIGNAL(presentationStopped()) );
0043     connect( m_view->presentationMode(), SIGNAL(pageChanged(int,int)), this, SIGNAL(presentationPageChanged(int,int)) );
0044     connect( m_view->presentationMode(), SIGNAL(stepChanged(int)), this, SIGNAL(presentationStepChanged(int)) );
0045 }
0046 
0047 KPrViewAdaptor::~KPrViewAdaptor()
0048 {
0049 }
0050 
0051 // custom slideshows
0052 
0053 QStringList KPrViewAdaptor::customSlideShows() const
0054 {
0055     KPrDocument *doc = m_view->kprDocument();
0056     return doc->customSlideShows()->names();
0057 }
0058 
0059 QString KPrViewAdaptor::activeCustomSlideShow() const
0060 {
0061     KPrDocument *doc = m_view->kprDocument();
0062     return doc->activeCustomSlideShow();
0063 }
0064 
0065 bool KPrViewAdaptor::setActiveCustomSlideShow( const QString &name )
0066 {
0067     // Check that the custom slideshow exists
0068     if ( name.isEmpty() || customSlideShows().contains( name ) ) {
0069         KPrDocument *doc = m_view->kprDocument();
0070         doc->setActiveCustomSlideShow( name );
0071         return true;
0072     }
0073     else {
0074         return false;
0075     }
0076 }
0077 
0078 // slides in the custom slideshow
0079 
0080 int KPrViewAdaptor::numCustomSlideShowSlides() const
0081 {
0082     KPrDocument *doc = m_view->kprDocument();
0083     return doc->slideShow().size();
0084 }
0085 
0086 QString KPrViewAdaptor::pageName( int page ) const
0087 {
0088     KPrDocument *doc = m_view->kprDocument();
0089 
0090     QList<KoPAPageBase *> slideShow = doc->slideShow();
0091     if ( page >= 0 && page < slideShow.size() ) {
0092         return slideShow[page]->name();
0093     }
0094     return QString();
0095 }
0096 
0097 QString KPrViewAdaptor::pageNotes( int page, const QString &format ) const
0098 {
0099     KPrDocument *doc = m_view->kprDocument();
0100 
0101     QList<KoPAPageBase *> slideShow = doc->slideShow();
0102     if ( page >= 0 && page < slideShow.size() ) {
0103         KPrPage *prPage = dynamic_cast<KPrPage *>( slideShow[page] );
0104         Q_ASSERT( 0 != prPage );
0105         if ( 0 != prPage ) {
0106             KPrNotes *pageNotes = prPage->pageNotes();
0107             KoShape *textShape = pageNotes->textShape();
0108             KoTextShapeData *textShapeData = qobject_cast<KoTextShapeData *>( textShape->userData() );
0109             Q_ASSERT( 0 != textShapeData );
0110             if ( 0 != textShapeData ) {
0111                 if ( format == "plain" ) {
0112                     return textShapeData->document()->toPlainText();
0113                 }
0114                 else if ( format == "html" ) {
0115                     return textShapeData->document()->toHtml();
0116                 }
0117             }
0118         }
0119     }
0120     return QString();
0121 }
0122 
0123 bool KPrViewAdaptor::exportPageThumbnail( int page, int width, int height,
0124                                           const QString &filename, const QString &format, int quality )
0125 {
0126     KPrDocument *doc = m_view->kprDocument();
0127 
0128     QList<KoPAPageBase *> slideShow = doc->slideShow();
0129     if ( page >= 0 && page < slideShow.size() ) {
0130         KoPAPageBase *pageObject = slideShow[page];
0131         Q_ASSERT( pageObject );
0132         return m_view->exportPageThumbnail( pageObject, QUrl::fromLocalFile( filename ),
0133                                             QSize( qMax( 0, width ), qMax( 0, height ) ),
0134                                             format.isEmpty() ? "PNG" : format.toLatin1(),
0135                                             qBound( -1, quality, 100 ) );
0136     }
0137     else {
0138         return false;
0139     }
0140 }
0141     
0142 // Presentation control
0143 
0144 void KPrViewAdaptor::presentationStart()
0145 {
0146     m_view->startPresentation();
0147 }
0148 
0149 void KPrViewAdaptor::presentationStartFromFirst()
0150 {
0151     m_view->startPresentationFromBeginning();
0152 }
0153 
0154 void KPrViewAdaptor::presentationStop()
0155 {
0156     m_view->stopPresentation();
0157 }
0158 
0159 void KPrViewAdaptor::presentationPrev()
0160 {
0161     if ( m_view->isPresentationRunning() ) {
0162         m_view->presentationMode()->navigate( KPrAnimationDirector::PreviousStep );
0163     }
0164 }
0165 
0166 void KPrViewAdaptor::presentationNext()
0167 {
0168     if ( m_view->isPresentationRunning() ) {
0169         m_view->presentationMode()->navigate( KPrAnimationDirector::NextStep );
0170     }
0171 }
0172 
0173 void KPrViewAdaptor::presentationPrevSlide()
0174 {
0175     if ( m_view->isPresentationRunning() ) {
0176         m_view->presentationMode()->navigate( KPrAnimationDirector::PreviousPage );
0177     }
0178 }
0179 
0180 void KPrViewAdaptor::presentationNextSlide()
0181 {
0182     if ( m_view->isPresentationRunning() ) {
0183         m_view->presentationMode()->navigate( KPrAnimationDirector::NextPage );
0184     }
0185 }
0186 
0187 void KPrViewAdaptor::presentationFirst()
0188 {
0189     if ( m_view->isPresentationRunning() ) {
0190         m_view->presentationMode()->navigate( KPrAnimationDirector::FirstPage );
0191     }
0192 }
0193 
0194 void KPrViewAdaptor::presentationLast()
0195 {
0196     if ( m_view->isPresentationRunning() ) {
0197         m_view->presentationMode()->navigate( KPrAnimationDirector::LastPage );
0198     }
0199 }
0200 
0201 void KPrViewAdaptor::gotoPresentationPage( int pg )
0202 {
0203     if ( m_view->isPresentationRunning() ) {
0204         m_view->presentationMode()->navigateToPage( pg );
0205     }
0206 }
0207 
0208 // Presentation accessors
0209 
0210 bool KPrViewAdaptor::isPresentationRunning() const
0211 {
0212     return m_view->isPresentationRunning();
0213 }
0214 
0215 int KPrViewAdaptor::currentPresentationPage() const
0216 {
0217     if ( m_view->isPresentationRunning() ) {
0218         return m_view->presentationMode()->currentPage();
0219     }
0220     else {
0221         return -1;
0222     }
0223 }
0224 
0225 int KPrViewAdaptor::currentPresentationStep() const
0226 {
0227     if ( m_view->isPresentationRunning() ) {
0228         return m_view->presentationMode()->currentStep();
0229     }
0230     else {
0231         return -1;
0232     }
0233 }
0234 
0235 int KPrViewAdaptor::numStepsInPresentationPage() const
0236 {
0237     if ( m_view->isPresentationRunning() ) {
0238         return m_view->presentationMode()->numStepsInPage();
0239     }
0240     else {
0241         return -1;
0242     }
0243 }
0244 
0245 int KPrViewAdaptor::numPresentationPages() const
0246 {
0247     if ( m_view->isPresentationRunning() ) {
0248         return m_view->presentationMode()->numPages();
0249     }
0250     else {
0251         return -1;
0252     }
0253 }
0254 
0255 /**
0256  * Fired when the presentation is activated.
0257  */
0258 void KPrViewAdaptor::presentationActivated()
0259 {
0260     emit presentationStarted( numPresentationPages() );
0261 }
0262