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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008 Fredy Yanardi <fyanardi@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 "KPrPresenterViewWidget.h"
0021 
0022 #include <QBoxLayout>
0023 #include <QKeyEvent>
0024 #include <QStackedLayout>
0025 
0026 #include <klocalizedstring.h>
0027 
0028 #include <KoPACanvas.h>
0029 #include <KoPAPageBase.h>
0030 #include <KoPAViewBase.h>
0031 #include <KoShape.h>
0032 
0033 #include "StageDebug.h"
0034 #include "KPrAnimationDirector.h"
0035 #include "KPrPresenterViewInterface.h"
0036 #include "KPrPresenterViewSlidesInterface.h"
0037 #include "KPrPresenterViewToolWidget.h"
0038 #include "KPrViewModePresentation.h"
0039 
0040 KPrPresenterViewWidget::KPrPresenterViewWidget( KPrViewModePresentation *viewMode, const QList<KoPAPageBase *> &pages, KoPACanvas *canvas, QWidget *parent )
0041     : QWidget( parent )
0042     , m_viewMode( viewMode )
0043     , m_pages( pages )
0044     , m_canvas( canvas )
0045 {
0046     QVBoxLayout *vLayout = new QVBoxLayout;
0047 
0048     vLayout->setContentsMargins( 20, 20, 20, 0 );
0049 
0050     m_stackedLayout = new QStackedLayout;
0051     m_mainWidget = new KPrPresenterViewInterface( pages, m_canvas );
0052     m_stackedLayout->addWidget( m_mainWidget );
0053 
0054     m_slidesWidget = new KPrPresenterViewSlidesInterface( pages );
0055     m_stackedLayout->addWidget( m_slidesWidget );
0056     connect( m_slidesWidget, SIGNAL(selectedPageChanged(int,bool)), this,
0057             SLOT(requestChangePage(int,bool)) );
0058     
0059     vLayout->addLayout( m_stackedLayout );
0060 
0061     QHBoxLayout *hLayout = new QHBoxLayout;
0062     hLayout->addStretch();
0063     m_toolWidget = new KPrPresenterViewToolWidget;
0064     connect( m_toolWidget, SIGNAL(slideThumbnailsToggled(bool)), this, SLOT(showSlideThumbnails(bool)) );
0065     connect( m_toolWidget, SIGNAL(previousSlideClicked()), this, SLOT(requestPreviousSlide()) );
0066     connect( m_toolWidget, SIGNAL(nextSlideClicked()), this, SLOT(requestNextSlide()) );
0067     hLayout->addWidget( m_toolWidget );
0068     hLayout->addStretch();
0069 
0070     vLayout->addLayout( hLayout );
0071 
0072     setLayout(vLayout);
0073 
0074     m_activeWidget = m_mainWidget;
0075     KoPAPageBase *activePage = m_viewMode->view()->activePage();
0076     if ( !m_pages.contains( activePage ) ) {
0077         activePage = m_pages[0];
0078     }
0079     m_activeWidget->setActivePage( activePage );
0080 }
0081 
0082 KPrPresenterViewWidget::~KPrPresenterViewWidget()
0083 {
0084 }
0085 
0086 void KPrPresenterViewWidget::setActivePage( KoPAPageBase *page )
0087 {
0088     m_activeWidget->setActivePage( page );
0089 }
0090 
0091 void KPrPresenterViewWidget::setActivePage( int pageIndex )
0092 {
0093     m_activeWidget->setActivePage( pageIndex );
0094 }
0095 
0096 void KPrPresenterViewWidget::updateWidget( const QSize &widgetSize, const QSize &canvasSize )
0097 {
0098     // a better way to resize the canvas, still need to find optimum value
0099 
0100     // try to make the height 40% of the widget height
0101     int previewHeight = 0.4 * canvasSize.height();
0102     double ratio = (double)canvasSize.width() / canvasSize.height();
0103     int previewWidth = ratio * previewHeight;
0104 
0105     // if it doesn't fit, make the width 40% of the widget width
0106     if ( previewWidth * 2 > 0.8 * widgetSize.width() ) {
0107         previewWidth = 0.4 * widgetSize.width();
0108         previewHeight = previewWidth / ratio;
0109     }
0110 
0111     QSize previewSize( previewHeight * ratio, previewHeight );
0112 
0113     m_mainWidget->setPreviewSize( previewSize );
0114 }
0115 
0116 void KPrPresenterViewWidget::showSlideThumbnails( bool show )
0117 {
0118     if ( show ) {
0119         m_stackedLayout->setCurrentIndex( 1 );
0120         m_activeWidget = m_slidesWidget;
0121     }
0122     else {
0123         m_stackedLayout->setCurrentIndex( 0 );
0124         m_activeWidget = m_mainWidget;
0125     }
0126 }
0127 
0128 void KPrPresenterViewWidget::requestPreviousSlide()
0129 {
0130     m_viewMode->keyPressEvent( new QKeyEvent( QEvent::KeyPress, Qt::Key_PageUp, Qt::NoModifier ) );
0131 }
0132 
0133 void KPrPresenterViewWidget::requestNextSlide()
0134 {
0135     m_viewMode->keyPressEvent( new QKeyEvent( QEvent::KeyPress, Qt::Key_PageDown, Qt::NoModifier ) );
0136 }
0137 
0138 void KPrPresenterViewWidget::requestChangePage( int index, bool enableMainView )
0139 {
0140     if ( enableMainView ) {
0141         m_toolWidget->toggleSlideThumbnails( false );
0142     }
0143     m_viewMode->navigateToPage( index );
0144     m_mainWidget->setActivePage( index );
0145     m_slidesWidget->setActivePage( index );
0146 }