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

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 "KPrPageLayoutWidget.h"
0021 
0022 #include <QListWidget>
0023 #include <QSize>
0024 #include <QGridLayout>
0025 
0026 #include <klocalizedstring.h>
0027 
0028 #include <KoPADocument.h>
0029 
0030 #include "StageDebug.h"
0031 #include "KPrPage.h"
0032 #include "KPresenter.h"
0033 #include "KPrView.h"
0034 #include "pagelayout/KPrPageLayout.h"
0035 #include "pagelayout/KPrPageLayouts.h"
0036 
0037 
0038 // this is needed so it can be used in a QVariant
0039 Q_DECLARE_METATYPE( KPrPageLayout* )
0040 
0041 KPrPageLayoutWidget::KPrPageLayoutWidget( QWidget* parent)
0042 : QWidget(parent)
0043 , m_view(0)
0044 , m_previousItem(0)
0045 {
0046     setWindowTitle(i18n( "Layout"));
0047     setObjectName("Slide Layouts");
0048 
0049     m_layoutsView = new QListWidget();
0050     m_layoutsView->setIconSize(QSize( 80, 60));
0051     m_layoutsView->setGridSize(QSize( 80, 60));
0052     m_layoutsView->setViewMode(QListView::IconMode);
0053     m_layoutsView->setResizeMode(QListView::Adjust);
0054     m_layoutsView->setMovement(QListView::Static);
0055     m_layoutsView->setSelectionRectVisible(false);
0056 
0057     QGridLayout* layout = new QGridLayout;
0058     layout->addWidget(m_layoutsView);
0059     layout->setMargin(0);
0060 
0061     setLayout(layout);
0062 }
0063 
0064 void KPrPageLayoutWidget::setView( KPrView* view )
0065 {
0066     Q_ASSERT( view );
0067     if (m_view) {
0068         // don't disconnect the m_view->proxyObject as the object is already deleted
0069         disconnect(m_layoutsView, 0, this, 0);
0070     }
0071     m_view = view;
0072     connect( m_view->proxyObject, SIGNAL(activePageChanged()),
0073              this, SLOT(slotActivePageChanged()) );
0074 
0075     // remove the layouts from the last view
0076     m_layoutsView->clear();
0077 
0078     KPrPageLayouts *layouts = view->kopaDocument()->resourceManager()->resource(KPresenter::PageLayouts).value<KPrPageLayouts*>();
0079 
0080     Q_ASSERT( layouts );
0081 
0082     const QList<KPrPageLayout *> layoutMap = layouts->layouts();
0083 
0084     // TODO add empty layout
0085 
0086     foreach( KPrPageLayout * layout, layoutMap ) {
0087         if ( layout->type() == KPrPageLayout::Page ) {
0088             addLayout( layout );
0089         }
0090     }
0091 
0092     slotActivePageChanged();
0093 
0094     connect( m_layoutsView, SIGNAL(itemPressed(QListWidgetItem*)),
0095              this, SLOT(slotItemPressed(QListWidgetItem*)) );
0096     connect( m_layoutsView, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
0097              this, SLOT(slotCurrentItemChanged(QListWidgetItem*,QListWidgetItem*)) );
0098 }
0099 
0100 void KPrPageLayoutWidget::slotActivePageChanged()
0101 {
0102     Q_ASSERT( m_view );
0103 
0104     KPrPage * page = dynamic_cast<KPrPage*>( m_view->activePage() );
0105     if ( page ) {
0106         KPrPageLayout * layout = page->layout();
0107         QListWidgetItem * item = m_layout2item.value( layout, 0 );
0108         if ( item == 0 && layout != 0 && layout->type() == KPrPageLayout::Page ) {
0109             item = addLayout( layout );
0110         }
0111 
0112         if ( item ) {
0113             m_layoutsView->blockSignals( true );
0114             item->setSelected( true );
0115             m_layoutsView->blockSignals( false );
0116             m_layoutsView->scrollToItem( item );
0117         }
0118         else {
0119             QList<QListWidgetItem*> items = m_layoutsView->selectedItems();
0120             foreach ( QListWidgetItem * i, items ) {
0121                 m_layoutsView->blockSignals( true );
0122                 i->setSelected( false );
0123                 m_layoutsView->blockSignals( false );
0124             }
0125         }
0126     }
0127 }
0128 
0129 void KPrPageLayoutWidget::slotItemPressed( QListWidgetItem * item )
0130 {
0131     if ( item == m_previousItem ) {
0132         applyLayout( item );
0133     }
0134     else {
0135         m_previousItem = item;
0136     }
0137 }
0138 
0139 void KPrPageLayoutWidget::slotCurrentItemChanged( QListWidgetItem * item, QListWidgetItem * previous )
0140 {
0141     applyLayout( item );
0142     m_previousItem = previous;
0143 }
0144 
0145 
0146 QListWidgetItem * KPrPageLayoutWidget::addLayout( KPrPageLayout * layout )
0147 {
0148     QListWidgetItem * item = new QListWidgetItem( QIcon( layout->thumbnail() ), "", m_layoutsView );
0149     item->setData( Qt::UserRole, QVariant::fromValue( layout ) );
0150     m_layout2item.insert( layout, item );
0151     return item;
0152 }
0153 
0154 void KPrPageLayoutWidget::applyLayout( QListWidgetItem * item )
0155 {
0156     // don't crash when all items are replaced
0157     if ( item ) {
0158         Q_ASSERT( m_view );
0159         KPrPage * page = dynamic_cast<KPrPage*>( m_view->activePage() );
0160         if ( page ) {
0161             page->setLayout( item->data( Qt::UserRole ).value<KPrPageLayout *>(), m_view->kopaDocument() );
0162         }
0163     }
0164 }