kpresenter

KPrCustomSlideShowDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Laurent Montel <montel@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qlabel.h>
00021 #include <qlayout.h>
00022 #include <qlineedit.h>
00023 #include <qlistbox.h>
00024 #include <qpushbutton.h>
00025 #include <qtoolbutton.h>
00026 #include <qapplication.h>
00027 #include <qlayout.h>
00028 #include <kdebug.h>
00029 #include <kmessagebox.h>
00030 #include <klocale.h>
00031 
00032 #include "KPrPage.h"
00033 #include "KPrDocument.h"
00034 #include "KPrView.h"
00035 
00036 #include "KPrCustomSlideShowDia.h"
00037 
00038 
00039 KPrCustomSlideShowDia::KPrCustomSlideShowDia( KPrView* _view, KPrDocument *_doc, const char* name )
00040     : KDialogBase( _view, name, true, i18n("Custom Slide Show"), Ok|Cancel ), m_doc( _doc ), m_view( _view )
00041     , m_customSlideShowMap( m_doc->customSlideShows() )
00042 {
00043   QWidget* page = new QWidget( this );
00044   setMainWidget( page );
00045 
00046   QGridLayout *grid1 = new QGridLayout( page,10,3,KDialog::marginHint(), KDialog::spacingHint());
00047 
00048   list=new QListBox(page);
00049   grid1->addMultiCellWidget(list,0,8,0,0);
00050 
00051   m_pAdd=new QPushButton(i18n("&Add..."),page);
00052   grid1->addWidget(m_pAdd,1,2);
00053 
00054   m_pModify=new QPushButton(i18n("&Modify..."),page);
00055   grid1->addWidget(m_pModify,2,2);
00056 
00057   m_pRemove=new QPushButton(i18n("&Remove"),page);
00058   grid1->addWidget(m_pRemove,3,2);
00059 
00060   m_pCopy=new QPushButton(i18n("Co&py"),page);
00061   grid1->addWidget(m_pCopy,4,2);
00062 
00063   m_pTest=new QPushButton(i18n("Test"),page);
00064   grid1->addWidget(m_pTest,5,2);
00065 
00066 
00067   connect( m_pRemove, SIGNAL( clicked() ), this, SLOT( slotRemove() ) );
00068   connect( m_pAdd, SIGNAL( clicked() ), this, SLOT( slotAdd() ) );
00069   connect( m_pModify, SIGNAL( clicked() ), this, SLOT( slotModify() ) );
00070   connect( m_pCopy, SIGNAL( clicked() ), this, SLOT( slotCopy() ) );
00071   connect( m_pTest, SIGNAL( clicked() ), this, SLOT( slotTest() ) );
00072 
00073   connect( list, SIGNAL(doubleClicked(QListBoxItem *)),this,SLOT(slotDoubleClicked(QListBoxItem *)));
00074   connect( list, SIGNAL(clicked ( QListBoxItem * )),this,SLOT(slotTextClicked(QListBoxItem * )));
00075 
00076   connect( m_view, SIGNAL( presentationFinished() ), this, SLOT( slotPresentationFinished() ) );
00077 
00078   init();
00079   updateButton();
00080 
00081   resize( 600, 250 );
00082 
00083   m_bChanged=false;
00084 }
00085 
00086 
00087 KPrCustomSlideShowDia::~KPrCustomSlideShowDia()
00088 {
00089     kdDebug()<<"KPrCustomSlideShowDia::~KPrCustomSlideShowDia()********************\n";
00090 }
00091 
00092 void KPrCustomSlideShowDia::init()
00093 {
00094     CustomSlideShowMap::ConstIterator it( m_customSlideShowMap.begin() );
00095     for ( ; it != m_customSlideShowMap.end(); ++it )
00096     {
00097         list->insertItem( it.key() );
00098     }
00099 }
00100 
00101 void KPrCustomSlideShowDia::updateButton()
00102 {
00103     bool state = ( list->currentItem() >= 0 );
00104     m_pRemove->setEnabled( state );
00105     m_pModify->setEnabled( state );
00106     m_pCopy->setEnabled( state );
00107     m_pTest->setEnabled( state );
00108 }
00109 
00110 void KPrCustomSlideShowDia::slotTextClicked(QListBoxItem*)
00111 {
00112     updateButton();
00113 }
00114 
00115 void KPrCustomSlideShowDia::slotDoubleClicked(QListBoxItem *)
00116 {
00117     updateButton();
00118     slotModify();
00119 }
00120 
00121 void KPrCustomSlideShowDia::slotPresentationFinished()
00122 {
00123     kdDebug()<<"void KPrCustomSlideShowDia::slotPresentationFinished()*************************\n";
00124     show();
00125 }
00126 
00127 
00128 void KPrCustomSlideShowDia::hideEvent( QHideEvent* )
00129 {
00130 }
00131 
00132 void KPrCustomSlideShowDia::slotTest()
00133 {
00134     QListBoxItem *item = list->selectedItem();
00135     if ( item )
00136     {
00137         m_doc->testCustomSlideShow( m_customSlideShowMap[item->text()], m_view );
00138         hide();
00139     }
00140 }
00141 
00142 void KPrCustomSlideShowDia::slotAdd()
00143 {
00144     QStringList listCustomName;
00145     CustomSlideShowMap::Iterator it( m_customSlideShowMap.begin() ) ;
00146     for ( ; it != m_customSlideShowMap.end(); ++it ) 
00147     {
00148         listCustomName.append( it.key() );
00149     }
00150 
00151     KPrDefineCustomSlideShow * dlg = new KPrDefineCustomSlideShow( this, listCustomName, m_doc->getPageList() );
00152     if ( dlg->exec() )
00153     {
00154         //insert new element
00155         m_customSlideShowMap.insert( dlg->customSlideShowName(), dlg->customSlides() );
00156         list->insertItem( dlg->customSlideShowName() );
00157         updateButton();
00158     }
00159     delete dlg;
00160 
00161 }
00162 
00163 void KPrCustomSlideShowDia::slotRemove()
00164 {
00165     if (list->selectedItem() )
00166     {
00167         m_customSlideShowMap.remove( list->selectedItem()->text() );
00168         list->removeItem( list->currentItem() );
00169         updateButton();
00170     }
00171 }
00172 
00173 void KPrCustomSlideShowDia::slotOk()
00174 {
00175     m_doc->setCustomSlideShows( m_customSlideShowMap );
00176     accept();
00177 }
00178 
00179 void KPrCustomSlideShowDia::slotModify()
00180 {
00181     QListBoxItem *item = list->selectedItem();
00182     if ( item )
00183     {
00184         QStringList listCustomName;
00185         CustomSlideShowMap::ConstIterator it( m_customSlideShowMap.begin() );
00186         for ( ; it != m_customSlideShowMap.end(); ++it )
00187         {
00188             if ( it.key() !=item->text() )
00189                 listCustomName.append( it.key() );
00190         }
00191 
00192         KPrDefineCustomSlideShow * dlg = new KPrDefineCustomSlideShow( this, item->text(), listCustomName, 
00193                                                                        m_doc->getPageList(), m_customSlideShowMap[item->text()]);
00194         if ( dlg->exec() )
00195         {
00196             //insert new element
00197             m_customSlideShowMap.remove( list->selectedItem()->text() );
00198             m_customSlideShowMap.insert( dlg->customSlideShowName(), dlg->customSlides() );
00199             list->changeItem( dlg->customSlideShowName(), list->currentItem() );
00200         }
00201         delete dlg;
00202     }
00203 
00204 }
00205 
00206 void KPrCustomSlideShowDia::slotCopy()
00207 {
00208     QListBoxItem *item = list->selectedItem();
00209     if ( item )
00210     {
00211         QString str( list->selectedItem()->text() );
00212         str+=i18n( "(Copy %1)" );
00213         for ( int i =1;; ++i )
00214         {
00215             if ( !uniqueName( i, str ) )
00216             {
00217                 str = str.arg( i );
00218                 m_customSlideShowMap.insert( str, m_customSlideShowMap[item->text()] );
00219                 list->insertItem( str );
00220                 break;
00221             }
00222         }
00223     }
00224 }
00225 
00226 bool KPrCustomSlideShowDia::uniqueName( int val, const QString & name ) const
00227 {
00228     QString str = name.arg( val );
00229     for ( int i= 0; i < ( int )list->count(); ++i )
00230     {
00231         if ( list->text ( i ) == str )
00232             return true;
00233     }
00234     return false;
00235 }
00236 
00237 
00238 KPrCustomSlideShowItem::KPrCustomSlideShowItem( QListBox * listbox, KPrPage * page )
00239 : QListBoxText( listbox, page->pageTitle() )
00240 , m_page( page )    
00241 {
00242 }
00243 
00244 KPrCustomSlideShowItem::KPrCustomSlideShowItem( KPrPage * page )
00245 : QListBoxText( page->pageTitle() )
00246 , m_page( page )    
00247 {
00248 }
00249 
00250 KPrCustomSlideShowItem::KPrCustomSlideShowItem( QListBox * listbox, KPrPage * page, QListBoxItem * after )
00251 : QListBoxText( listbox, page->pageTitle(), after )
00252 , m_page( page )    
00253 {
00254 }
00255 
00256 KPrDefineCustomSlideShow::KPrDefineCustomSlideShow( QWidget* parent, QStringList &_listNameSlideShow, 
00257                                                     const QPtrList<KPrPage> &pages, const char *name )
00258 : KDialogBase( parent, name, true, i18n("Define Custom Slide Show"), Ok|Cancel )
00259 , listNameCustomSlideShow( _listNameSlideShow )
00260 {
00261     init();
00262     for ( QPtrList<KPrPage>::ConstIterator it = pages.begin(); it != pages.end(); ++it )
00263     {
00264         listSlide->insertItem( new KPrCustomSlideShowItem( *it ) );
00265     }
00266 }
00267 
00268 KPrDefineCustomSlideShow::KPrDefineCustomSlideShow( QWidget* parent, const QString &_customName, QStringList &_listNameSlideShow, 
00269                                                     const QPtrList<KPrPage> &pages, QValueList<KPrPage *> &customPages, const char* name )
00270 : KDialogBase( parent, name, true, i18n("Define Custom Slide Show"), Ok|Cancel )
00271 , listNameCustomSlideShow( _listNameSlideShow )
00272 {
00273     init();
00274     m_name->setText( _customName );
00275     for ( QPtrList<KPrPage>::ConstIterator it = pages.begin(); it != pages.end(); ++it )
00276     {
00277         listSlide->insertItem( new KPrCustomSlideShowItem( *it ) );
00278     }
00279     for ( QValueList<KPrPage *>::ConstIterator it = customPages.begin(); it != customPages.end(); ++it )
00280     {
00281         listSlideShow->insertItem( new KPrCustomSlideShowItem( *it ) );
00282     }
00283 }
00284 
00285 void KPrDefineCustomSlideShow::init()
00286 {
00287   QWidget* page = new QWidget( this );
00288   setMainWidget( page );
00289 
00290   QVBoxLayout *lov = new QVBoxLayout( page );
00291   lov->setSpacing( KDialog::spacingHint() );
00292   QHBoxLayout *loh = new QHBoxLayout( lov );
00293 
00294   QLabel *lab = new QLabel( i18n( "Name:" ), page );
00295   loh->addWidget( lab );
00296   m_name = new QLineEdit( page );
00297   loh->addWidget( m_name );
00298 
00299   QHBoxLayout *lo = new QHBoxLayout( lov );
00300   lo->setSpacing( KDialog::spacingHint() );
00301 
00302   QVBoxLayout *loAv = new QVBoxLayout( lo );
00303   lab = new QLabel( i18n("Existing slides:"), page );
00304   loAv->addWidget( lab );
00305   listSlide = new QListBox( page );
00306   loAv->addWidget( listSlide );
00307   lab->setBuddy( listSlide );
00308 
00309   QVBoxLayout *loHBtns = new QVBoxLayout( lo );
00310   loHBtns->addStretch( 1 );
00311   m_insertSlide = new QToolButton( page );
00312   loHBtns->addWidget( m_insertSlide );
00313   m_removeSlide = new QToolButton( page );
00314   loHBtns->addWidget( m_removeSlide );
00315   loHBtns->addStretch( 1 );
00316 
00317   QVBoxLayout *loS = new QVBoxLayout( lo );
00318   lab = new QLabel( i18n("Selected slides:"), page );
00319   loS->addWidget( lab );
00320   listSlideShow = new QListBox( page );
00321   loS->addWidget( listSlideShow );
00322   lab->setBuddy( listSlideShow );
00323 
00324   QVBoxLayout *loVBtns = new QVBoxLayout( lo );
00325   loVBtns->addStretch( 1 );
00326   m_moveUpSlide = new QToolButton( page );
00327   m_moveUpSlide->setAutoRepeat( true );
00328   loVBtns->addWidget( m_moveUpSlide );
00329   m_moveDownSlide = new QToolButton( page );
00330   m_moveDownSlide->setAutoRepeat( true );
00331   loVBtns->addWidget( m_moveDownSlide );
00332   loVBtns->addStretch( 1 );
00333 
00334 
00335   m_name->setFocus();
00336 
00337   connect( m_insertSlide, SIGNAL(clicked()), this, SLOT(slotMoveInsertSlide() ) );
00338   connect( m_removeSlide, SIGNAL(clicked()), this, SLOT(slotMoveRemoveSlide()) );
00339   connect( m_moveUpSlide, SIGNAL(clicked()), this, SLOT( slotMoveUpSlide() ) );
00340   connect( m_moveDownSlide, SIGNAL(clicked()), this, SLOT(slotMoveDownSlide()) );
00341   connect(  m_name, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slideNameChanged( const QString & ) ) );
00342   connect( listSlideShow, SIGNAL( clicked ( QListBoxItem * ) ), this, SLOT( updateButton() ) );
00343   connect( listSlide, SIGNAL( clicked ( QListBoxItem * ) ), this, SLOT( updateButton() ) );
00344   connect( listSlide, SIGNAL( doubleClicked ( QListBoxItem * ) ), this, SLOT( slotMoveInsertSlide() ) );
00345   connect( listSlideShow, SIGNAL( doubleClicked( QListBoxItem * ) ), this, SLOT( slotMoveRemoveSlide() ) );
00346   m_insertSlide->setIconSet( SmallIconSet( ( QApplication::reverseLayout() ? "back" : "forward" ) ) );
00347   m_removeSlide->setIconSet( SmallIconSet( ( QApplication::reverseLayout() ? "forward" : "back") ) );
00348   m_moveUpSlide->setIconSet( SmallIconSet( "up" ) );
00349   m_moveDownSlide->setIconSet( SmallIconSet( "down" ) );
00350 
00351   slideNameChanged( m_name->text() );
00352   updateButton();
00353   resize( 600, 250 );
00354 
00355 }
00356 
00357 void KPrDefineCustomSlideShow::slideNameChanged( const QString & _name)
00358 {
00359      enableButtonOK( !_name.isEmpty() );
00360 }
00361 
00362 void KPrDefineCustomSlideShow::updateButton()
00363 {
00364     int pos = listSlideShow->currentItem();
00365     m_moveUpSlide->setEnabled( pos>0 );
00366     m_moveDownSlide->setEnabled( pos< (( int ) listSlideShow->count()-1 ) );
00367     m_removeSlide->setEnabled( listSlideShow->count()>0 );
00368     m_insertSlide->setEnabled( listSlide->currentItem()>-1 );
00369 }
00370 
00371 void KPrDefineCustomSlideShow::slotMoveUpSlide()
00372 {
00373     int c = listSlideShow->currentItem();
00374     if ( c < 1 ) return;
00375     QListBoxItem *item = listSlideShow->item( c );
00376     listSlideShow->takeItem( item );
00377     listSlideShow->insertItem( item, c-1 );
00378     listSlideShow->setCurrentItem( item );
00379 
00380     updateButton();
00381 }
00382 
00383 void KPrDefineCustomSlideShow::slotMoveDownSlide()
00384 {
00385     int c = listSlideShow->currentItem();
00386     if ( c < 0 || c == int( listSlideShow->count() ) - 1 ) return;
00387     QListBoxItem *item = listSlideShow->item( c );
00388     listSlideShow->takeItem( item );
00389     listSlideShow->insertItem( item, c+1 );
00390     listSlideShow->setCurrentItem( item );
00391     updateButton();
00392 }
00393 
00394 void KPrDefineCustomSlideShow::slotMoveRemoveSlide()
00395 {
00396     // move all selected items from selected to available listbox
00397     QListBoxItem *item = listSlideShow->firstItem();
00398     while ( item ) {
00399         if ( item->isSelected() ) {
00400             listSlideShow->takeItem( item );
00401         }
00402         item = item->next();
00403     }
00404     updateButton();
00405 }
00406 
00407 void KPrDefineCustomSlideShow::slotMoveInsertSlide()
00408 {
00409     QListBoxItem *item = listSlide->firstItem();
00410     while ( item ) {
00411         if ( item->isSelected() ) {
00412             KPrCustomSlideShowItem * i = dynamic_cast<KPrCustomSlideShowItem *>( item );
00413             if ( i )
00414             {
00415                 listSlideShow->insertItem( new KPrCustomSlideShowItem( i->getPage() ), ( listSlideShow->count() )  );
00416             }
00417         }
00418         item = item->next();
00419     }
00420     listSlideShow->setFocus();
00421     updateButton();
00422 }
00423 
00424 QValueList<KPrPage *> KPrDefineCustomSlideShow::customSlides()
00425 {
00426     QValueList<KPrPage *> pages;
00427     QListBoxItem *item = listSlideShow->firstItem();
00428     while ( item ) 
00429     {
00430         KPrCustomSlideShowItem * i = dynamic_cast<KPrCustomSlideShowItem *>( item );
00431         if ( i )
00432         {
00433             pages.push_back( i->getPage() );
00434         }
00435         item = item->next();
00436     }
00437     return pages;
00438 }
00439 
00440 QString KPrDefineCustomSlideShow::customSlideShowName() const
00441 {
00442     return m_name->text();
00443 }
00444 
00445 
00446 void KPrDefineCustomSlideShow::slotOk()
00447 {
00448     if ( listNameCustomSlideShow.contains( m_name->text() ) )
00449     {
00450         KMessageBox::error(this, i18n("Custom Slide Show name is already used."), i18n("Define Custom Slide Show"));
00451     }
00452     else
00453     {
00454         if ( listSlideShow->count() == 0  )
00455             KMessageBox::error(this, i18n("You did not select any slides. Please select some slides."), i18n("Define Custom Slide Show"));
00456         else
00457             accept();
00458     }
00459 }
00460 
00461 #include "KPrCustomSlideShowDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys