kpresenter

KPrCustomSlideShowDia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Laurent Montel <montel@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqlabel.h>
21#include <tqlayout.h>
22#include <tqlineedit.h>
23#include <tqlistbox.h>
24#include <tqpushbutton.h>
25#include <tqtoolbutton.h>
26#include <tqapplication.h>
27#include <tqlayout.h>
28#include <kdebug.h>
29#include <tdemessagebox.h>
30#include <tdelocale.h>
31
32#include "KPrPage.h"
33#include "KPrDocument.h"
34#include "KPrView.h"
35
36#include "KPrCustomSlideShowDia.h"
37
38
39KPrCustomSlideShowDia::KPrCustomSlideShowDia( KPrView* _view, KPrDocument *_doc, const char* name )
40 : KDialogBase( _view, name, true, i18n("Custom Slide Show"), Ok|Cancel ), m_doc( _doc ), m_view( _view )
41 , m_customSlideShowMap( m_doc->customSlideShows() )
42{
43 TQWidget* page = new TQWidget( this );
44 setMainWidget( page );
45
46 TQGridLayout *grid1 = new TQGridLayout( page,10,3,KDialog::marginHint(), KDialog::spacingHint());
47
48 list=new TQListBox(page);
49 grid1->addMultiCellWidget(list,0,8,0,0);
50
51 m_pAdd=new TQPushButton(i18n("&Add..."),page);
52 grid1->addWidget(m_pAdd,1,2);
53
54 m_pModify=new TQPushButton(i18n("&Modify..."),page);
55 grid1->addWidget(m_pModify,2,2);
56
57 m_pRemove=new TQPushButton(i18n("&Remove"),page);
58 grid1->addWidget(m_pRemove,3,2);
59
60 m_pCopy=new TQPushButton(i18n("Co&py"),page);
61 grid1->addWidget(m_pCopy,4,2);
62
63 m_pTest=new TQPushButton(i18n("Test"),page);
64 grid1->addWidget(m_pTest,5,2);
65
66
67 connect( m_pRemove, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotRemove() ) );
68 connect( m_pAdd, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotAdd() ) );
69 connect( m_pModify, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotModify() ) );
70 connect( m_pCopy, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotCopy() ) );
71 connect( m_pTest, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotTest() ) );
72
73 connect( list, TQ_SIGNAL(doubleClicked(TQListBoxItem *)),this,TQ_SLOT(slotDoubleClicked(TQListBoxItem *)));
74 connect( list, TQ_SIGNAL(clicked ( TQListBoxItem * )),this,TQ_SLOT(slotTextClicked(TQListBoxItem * )));
75
76 connect( m_view, TQ_SIGNAL( presentationFinished() ), this, TQ_SLOT( slotPresentationFinished() ) );
77
78 init();
79 updateButton();
80
81 resize( 600, 250 );
82
83 m_bChanged=false;
84}
85
86
87KPrCustomSlideShowDia::~KPrCustomSlideShowDia()
88{
89 kdDebug()<<"KPrCustomSlideShowDia::~KPrCustomSlideShowDia()********************\n";
90}
91
92void KPrCustomSlideShowDia::init()
93{
94 CustomSlideShowMap::ConstIterator it( m_customSlideShowMap.begin() );
95 for ( ; it != m_customSlideShowMap.end(); ++it )
96 {
97 list->insertItem( it.key() );
98 }
99}
100
101void KPrCustomSlideShowDia::updateButton()
102{
103 bool state = ( list->currentItem() >= 0 );
104 m_pRemove->setEnabled( state );
105 m_pModify->setEnabled( state );
106 m_pCopy->setEnabled( state );
107 m_pTest->setEnabled( state );
108}
109
110void KPrCustomSlideShowDia::slotTextClicked(TQListBoxItem*)
111{
112 updateButton();
113}
114
115void KPrCustomSlideShowDia::slotDoubleClicked(TQListBoxItem *)
116{
117 updateButton();
118 slotModify();
119}
120
121void KPrCustomSlideShowDia::slotPresentationFinished()
122{
123 kdDebug()<<"void KPrCustomSlideShowDia::slotPresentationFinished()*************************\n";
124 show();
125}
126
127
128void KPrCustomSlideShowDia::hideEvent( TQHideEvent* )
129{
130}
131
132void KPrCustomSlideShowDia::slotTest()
133{
134 TQListBoxItem *item = list->selectedItem();
135 if ( item )
136 {
137 m_doc->testCustomSlideShow( m_customSlideShowMap[item->text()], m_view );
138 hide();
139 }
140}
141
142void KPrCustomSlideShowDia::slotAdd()
143{
144 TQStringList listCustomName;
145 CustomSlideShowMap::Iterator it( m_customSlideShowMap.begin() ) ;
146 for ( ; it != m_customSlideShowMap.end(); ++it )
147 {
148 listCustomName.append( it.key() );
149 }
150
151 KPrDefineCustomSlideShow * dlg = new KPrDefineCustomSlideShow( this, listCustomName, m_doc->getPageList() );
152 if ( dlg->exec() )
153 {
154 //insert new element
155 m_customSlideShowMap.insert( dlg->customSlideShowName(), dlg->customSlides() );
156 list->insertItem( dlg->customSlideShowName() );
157 updateButton();
158 }
159 delete dlg;
160
161}
162
163void KPrCustomSlideShowDia::slotRemove()
164{
165 if (list->selectedItem() )
166 {
167 m_customSlideShowMap.remove( list->selectedItem()->text() );
168 list->removeItem( list->currentItem() );
169 updateButton();
170 }
171}
172
173void KPrCustomSlideShowDia::slotOk()
174{
175 m_doc->setCustomSlideShows( m_customSlideShowMap );
176 accept();
177}
178
179void KPrCustomSlideShowDia::slotModify()
180{
181 TQListBoxItem *item = list->selectedItem();
182 if ( item )
183 {
184 TQStringList listCustomName;
185 CustomSlideShowMap::ConstIterator it( m_customSlideShowMap.begin() );
186 for ( ; it != m_customSlideShowMap.end(); ++it )
187 {
188 if ( it.key() !=item->text() )
189 listCustomName.append( it.key() );
190 }
191
192 KPrDefineCustomSlideShow * dlg = new KPrDefineCustomSlideShow( this, item->text(), listCustomName,
193 m_doc->getPageList(), m_customSlideShowMap[item->text()]);
194 if ( dlg->exec() )
195 {
196 //insert new element
197 m_customSlideShowMap.remove( list->selectedItem()->text() );
198 m_customSlideShowMap.insert( dlg->customSlideShowName(), dlg->customSlides() );
199 list->changeItem( dlg->customSlideShowName(), list->currentItem() );
200 }
201 delete dlg;
202 }
203
204}
205
206void KPrCustomSlideShowDia::slotCopy()
207{
208 TQListBoxItem *item = list->selectedItem();
209 if ( item )
210 {
211 TQString str( list->selectedItem()->text() );
212 str+=i18n( "(Copy %1)" );
213 for ( int i =1;; ++i )
214 {
215 if ( !uniqueName( i, str ) )
216 {
217 str = str.arg( i );
218 m_customSlideShowMap.insert( str, m_customSlideShowMap[item->text()] );
219 list->insertItem( str );
220 break;
221 }
222 }
223 }
224}
225
226bool KPrCustomSlideShowDia::uniqueName( int val, const TQString & name ) const
227{
228 TQString str = name.arg( val );
229 for ( int i= 0; i < ( int )list->count(); ++i )
230 {
231 if ( list->text ( i ) == str )
232 return true;
233 }
234 return false;
235}
236
237
238KPrCustomSlideShowItem::KPrCustomSlideShowItem( TQListBox * listbox, KPrPage * page )
239: TQListBoxText( listbox, page->pageTitle() )
240, m_page( page )
241{
242}
243
244KPrCustomSlideShowItem::KPrCustomSlideShowItem( KPrPage * page )
245: TQListBoxText( page->pageTitle() )
246, m_page( page )
247{
248}
249
250KPrCustomSlideShowItem::KPrCustomSlideShowItem( TQListBox * listbox, KPrPage * page, TQListBoxItem * after )
251: TQListBoxText( listbox, page->pageTitle(), after )
252, m_page( page )
253{
254}
255
256KPrDefineCustomSlideShow::KPrDefineCustomSlideShow( TQWidget* parent, TQStringList &_listNameSlideShow,
257 const TQPtrList<KPrPage> &pages, const char *name )
258: KDialogBase( parent, name, true, i18n("Define Custom Slide Show"), Ok|Cancel )
259, listNameCustomSlideShow( _listNameSlideShow )
260{
261 init();
262 for ( TQPtrList<KPrPage>::ConstIterator it = pages.begin(); it != pages.end(); ++it )
263 {
264 listSlide->insertItem( new KPrCustomSlideShowItem( *it ) );
265 }
266}
267
268KPrDefineCustomSlideShow::KPrDefineCustomSlideShow( TQWidget* parent, const TQString &_customName, TQStringList &_listNameSlideShow,
269 const TQPtrList<KPrPage> &pages, TQValueList<KPrPage *> &customPages, const char* name )
270: KDialogBase( parent, name, true, i18n("Define Custom Slide Show"), Ok|Cancel )
271, listNameCustomSlideShow( _listNameSlideShow )
272{
273 init();
274 m_name->setText( _customName );
275 for ( TQPtrList<KPrPage>::ConstIterator it = pages.begin(); it != pages.end(); ++it )
276 {
277 listSlide->insertItem( new KPrCustomSlideShowItem( *it ) );
278 }
279 for ( TQValueList<KPrPage *>::ConstIterator it = customPages.begin(); it != customPages.end(); ++it )
280 {
281 listSlideShow->insertItem( new KPrCustomSlideShowItem( *it ) );
282 }
283}
284
285void KPrDefineCustomSlideShow::init()
286{
287 TQWidget* page = new TQWidget( this );
288 setMainWidget( page );
289
290 TQVBoxLayout *lov = new TQVBoxLayout( page );
291 lov->setSpacing( KDialog::spacingHint() );
292 TQHBoxLayout *loh = new TQHBoxLayout( lov );
293
294 TQLabel *lab = new TQLabel( i18n( "Name:" ), page );
295 loh->addWidget( lab );
296 m_name = new TQLineEdit( page );
297 loh->addWidget( m_name );
298
299 TQHBoxLayout *lo = new TQHBoxLayout( lov );
300 lo->setSpacing( KDialog::spacingHint() );
301
302 TQVBoxLayout *loAv = new TQVBoxLayout( lo );
303 lab = new TQLabel( i18n("Existing slides:"), page );
304 loAv->addWidget( lab );
305 listSlide = new TQListBox( page );
306 loAv->addWidget( listSlide );
307 lab->setBuddy( listSlide );
308
309 TQVBoxLayout *loHBtns = new TQVBoxLayout( lo );
310 loHBtns->addStretch( 1 );
311 m_insertSlide = new TQToolButton( page );
312 loHBtns->addWidget( m_insertSlide );
313 m_removeSlide = new TQToolButton( page );
314 loHBtns->addWidget( m_removeSlide );
315 loHBtns->addStretch( 1 );
316
317 TQVBoxLayout *loS = new TQVBoxLayout( lo );
318 lab = new TQLabel( i18n("Selected slides:"), page );
319 loS->addWidget( lab );
320 listSlideShow = new TQListBox( page );
321 loS->addWidget( listSlideShow );
322 lab->setBuddy( listSlideShow );
323
324 TQVBoxLayout *loVBtns = new TQVBoxLayout( lo );
325 loVBtns->addStretch( 1 );
326 m_moveUpSlide = new TQToolButton( page );
327 m_moveUpSlide->setAutoRepeat( true );
328 loVBtns->addWidget( m_moveUpSlide );
329 m_moveDownSlide = new TQToolButton( page );
330 m_moveDownSlide->setAutoRepeat( true );
331 loVBtns->addWidget( m_moveDownSlide );
332 loVBtns->addStretch( 1 );
333
334
335 m_name->setFocus();
336
337 connect( m_insertSlide, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotMoveInsertSlide() ) );
338 connect( m_removeSlide, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotMoveRemoveSlide()) );
339 connect( m_moveUpSlide, TQ_SIGNAL(clicked()), this, TQ_SLOT( slotMoveUpSlide() ) );
340 connect( m_moveDownSlide, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotMoveDownSlide()) );
341 connect( m_name, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slideNameChanged( const TQString & ) ) );
342 connect( listSlideShow, TQ_SIGNAL( clicked ( TQListBoxItem * ) ), this, TQ_SLOT( updateButton() ) );
343 connect( listSlide, TQ_SIGNAL( clicked ( TQListBoxItem * ) ), this, TQ_SLOT( updateButton() ) );
344 connect( listSlide, TQ_SIGNAL( doubleClicked ( TQListBoxItem * ) ), this, TQ_SLOT( slotMoveInsertSlide() ) );
345 connect( listSlideShow, TQ_SIGNAL( doubleClicked( TQListBoxItem * ) ), this, TQ_SLOT( slotMoveRemoveSlide() ) );
346 m_insertSlide->setIconSet( SmallIconSet( ( TQApplication::reverseLayout() ? "back" : "forward" ) ) );
347 m_removeSlide->setIconSet( SmallIconSet( ( TQApplication::reverseLayout() ? "forward" : "back") ) );
348 m_moveUpSlide->setIconSet( SmallIconSet( "go-up" ) );
349 m_moveDownSlide->setIconSet( SmallIconSet( "go-down" ) );
350
351 slideNameChanged( m_name->text() );
352 updateButton();
353 resize( 600, 250 );
354
355}
356
357void KPrDefineCustomSlideShow::slideNameChanged( const TQString & _name)
358{
359 enableButtonOK( !_name.isEmpty() );
360}
361
362void KPrDefineCustomSlideShow::updateButton()
363{
364 int pos = listSlideShow->currentItem();
365 m_moveUpSlide->setEnabled( pos>0 );
366 m_moveDownSlide->setEnabled( pos< (( int ) listSlideShow->count()-1 ) );
367 m_removeSlide->setEnabled( listSlideShow->count()>0 );
368 m_insertSlide->setEnabled( listSlide->currentItem()>-1 );
369}
370
371void KPrDefineCustomSlideShow::slotMoveUpSlide()
372{
373 int c = listSlideShow->currentItem();
374 if ( c < 1 ) return;
375 TQListBoxItem *item = listSlideShow->item( c );
376 listSlideShow->takeItem( item );
377 listSlideShow->insertItem( item, c-1 );
378 listSlideShow->setCurrentItem( item );
379
380 updateButton();
381}
382
383void KPrDefineCustomSlideShow::slotMoveDownSlide()
384{
385 int c = listSlideShow->currentItem();
386 if ( c < 0 || c == int( listSlideShow->count() ) - 1 ) return;
387 TQListBoxItem *item = listSlideShow->item( c );
388 listSlideShow->takeItem( item );
389 listSlideShow->insertItem( item, c+1 );
390 listSlideShow->setCurrentItem( item );
391 updateButton();
392}
393
394void KPrDefineCustomSlideShow::slotMoveRemoveSlide()
395{
396 // move all selected items from selected to available listbox
397 TQListBoxItem *item = listSlideShow->firstItem();
398 while ( item ) {
399 if ( item->isSelected() ) {
400 listSlideShow->takeItem( item );
401 }
402 item = item->next();
403 }
404 updateButton();
405}
406
407void KPrDefineCustomSlideShow::slotMoveInsertSlide()
408{
409 TQListBoxItem *item = listSlide->firstItem();
410 while ( item ) {
411 if ( item->isSelected() ) {
412 KPrCustomSlideShowItem * i = dynamic_cast<KPrCustomSlideShowItem *>( item );
413 if ( i )
414 {
415 listSlideShow->insertItem( new KPrCustomSlideShowItem( i->getPage() ), ( listSlideShow->count() ) );
416 }
417 }
418 item = item->next();
419 }
420 listSlideShow->setFocus();
421 updateButton();
422}
423
424TQValueList<KPrPage *> KPrDefineCustomSlideShow::customSlides()
425{
426 TQValueList<KPrPage *> pages;
427 TQListBoxItem *item = listSlideShow->firstItem();
428 while ( item )
429 {
430 KPrCustomSlideShowItem * i = dynamic_cast<KPrCustomSlideShowItem *>( item );
431 if ( i )
432 {
433 pages.push_back( i->getPage() );
434 }
435 item = item->next();
436 }
437 return pages;
438}
439
440TQString KPrDefineCustomSlideShow::customSlideShowName() const
441{
442 return m_name->text();
443}
444
445
446void KPrDefineCustomSlideShow::slotOk()
447{
448 if ( listNameCustomSlideShow.contains( m_name->text() ) )
449 {
450 KMessageBox::error(this, i18n("Custom Slide Show name is already used."), i18n("Define Custom Slide Show"));
451 }
452 else
453 {
454 if ( listSlideShow->count() == 0 )
455 KMessageBox::error(this, i18n("You did not select any slides. Please select some slides."), i18n("Define Custom Slide Show"));
456 else
457 accept();
458 }
459}
460
461#include "KPrCustomSlideShowDia.moc"