1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/* This file is part of the KDE project
Copyright (C) 2002 Matthias Kretz <kretz@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* $Id$ */
#include "kviewpresenter.h"
#include "imagelistdialog.h"
#include "imagelistitem.h"
#include <tqvbox.h>
#include <tqobjectlist.h>
#include <tqsignalslotimp.h>
#include <tqtimer.h>
#include <tqevent.h>
#include <tqdragobject.h>
#include <tqstringlist.h>
#include <kpushbutton.h>
#include <kapplication.h>
#include <kaction.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <knuminput.h>
#include <kfiledialog.h>
#include <kimageio.h>
#include <kimageviewer/viewer.h>
#include <kimageviewer/canvas.h>
#include <kio/netaccess.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <kurldrag.h>
typedef KGenericFactory<KViewPresenter> KViewPresenterFactory;
K_EXPORT_COMPONENT_FACTORY( kview_presenterplugin, KViewPresenterFactory( "kviewpresenterplugin" ) )
KViewPresenter::KViewPresenter( TQObject* parent, const char* name, const TQStringList & )
: Plugin( parent, name )
, m_pImageList( new ImageListDialog() )
, m_paFileOpen( 0 )
, m_bDontAdd( false )
, m_pCurrentItem( 0 )
, m_pSlideshowTimer( new TQTimer( this ) )
{
kdDebug( 4630 ) << k_funcinfo << endl;
m_imagelist.setAutoDelete( true );
TQObjectList * viewerList = parent->queryList( 0, "KImageViewer Part", false, false );
m_pViewer = static_cast<KImageViewer::Viewer *>( viewerList->getFirst() );
delete viewerList;
if( m_pViewer )
{
( void ) new KAction( i18n( "&Image List..." ), 0, 0,
this, TQT_SLOT( slotImageList() ),
actionCollection(), "plugin_presenter_imageList" );
m_paSlideshow = new KToggleAction( i18n( "Start &Slideshow" ), Key_S, actionCollection(), "plugin_presenter_slideshow" );
( void ) new KAction( i18n( "&Previous Image in List" ), "previous", ALT+Key_Left,
this, TQT_SLOT( prev() ),
actionCollection(), "plugin_presenter_prev" );
( void ) new KAction( i18n( "&Next Image in List" ), "next", ALT+Key_Right,
this, TQT_SLOT( next() ),
actionCollection(), "plugin_presenter_next" );
connect( m_paSlideshow, TQT_SIGNAL( toggled( bool ) ), m_pImageList->m_pSlideshow, TQT_SLOT( setOn( bool ) ) );
connect( m_pImageList->m_pSlideshow, TQT_SIGNAL( toggled( bool ) ), m_paSlideshow, TQT_SLOT( setChecked( bool ) ) );
// search for file_open action
KXMLGUIClient * parentClient = static_cast<KXMLGUIClient*>( parent->tqt_cast( "KXMLGUIClient" ) );
if( parentClient )
{
m_paFileOpen = parentClient->actionCollection()->action( "file_open" );
m_paFileClose = parentClient->actionCollection()->action( "file_close" );
}
if( m_paFileClose )
connect( m_paFileClose, TQT_SIGNAL( activated() ), this, TQT_SLOT( slotClose() ) );
if( m_paFileOpen )
{
disconnect( m_paFileOpen, TQT_SIGNAL( activated() ), parent, TQT_SLOT( slotOpenFile() ) );
connect( m_paFileOpen, TQT_SIGNAL( activated() ), this, TQT_SLOT( slotOpenFiles() ) );
}
else
{
(void) new KAction( i18n( "Open &Multiple Files..." ), "queue", CTRL+SHIFT+Key_O,
this, TQT_SLOT( slotOpenFiles() ),
actionCollection(), "plugin_presenter_openFiles" );
}
connect( m_pViewer, TQT_SIGNAL( imageOpened( const KURL & ) ),
TQT_SLOT( slotImageOpened( const KURL & ) ) );
}
else
kdWarning( 4630 ) << "no KImageViewer interface found - the presenter plugin won't work" << endl;
//( void )new KViewPresenterConfModule( this );
connect( m_pImageList->m_pListView, TQT_SIGNAL( executed( TQListViewItem* ) ),
this, TQT_SLOT( changeItem( TQListViewItem* ) ) );
connect( m_pImageList->m_pPrevious, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( prev() ) );
connect( m_pImageList->m_pNext, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( next() ) );
connect( m_pImageList->m_pListView, TQT_SIGNAL( spacePressed( TQListViewItem* ) ),
this, TQT_SLOT( changeItem( TQListViewItem* ) ) );
connect( m_pImageList->m_pListView, TQT_SIGNAL( returnPressed( TQListViewItem* ) ),
this, TQT_SLOT( changeItem( TQListViewItem* ) ) );
connect( m_pImageList->m_pSlideshow, TQT_SIGNAL( toggled( bool ) ),
this, TQT_SLOT( slideshow( bool ) ) );
connect( m_pImageList->m_pInterval, TQT_SIGNAL( valueChanged( int ) ),
this, TQT_SLOT( setSlideshowInterval( int ) ) );
connect( m_pImageList->m_pShuffle, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( shuffle() ) );
connect( m_pImageList->m_pLoad, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( loadList() ) );
connect( m_pImageList->m_pSave, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( saveList() ) );
connect( m_pImageList->m_pCloseAll, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( closeAll() ) );
// allow drop on the dialog
m_pImageList->installEventFilter( this );
m_pImageList->m_pListView->installEventFilter( this );
m_pImageList->m_pListView->viewport()->installEventFilter( this );
// grab drops on the main view
m_pViewer->widget()->installEventFilter( this );
connect( m_pSlideshowTimer, TQT_SIGNAL( timeout() ),
this, TQT_SLOT( next() ) );
}
KViewPresenter::~KViewPresenter()
{
kdDebug( 4630 ) << k_funcinfo << endl;
if( m_paFileOpen )
{
disconnect( m_paFileOpen, TQT_SIGNAL( activated() ), this, TQT_SLOT( slotOpenFiles() ) );
// If the parent() doesn't exist we either leave the "File Open" action
// in an unusable state or KView was just shutting down and therefor we
// can ignore this. I've only seen the second one happening and to get
// rid of the TQObject::connect warning we do the parent() check.
if( parent() )
connect( m_paFileOpen, TQT_SIGNAL( activated() ), parent(), TQT_SLOT( slotOpenFile() ) );
}
}
bool KViewPresenter::eventFilter( TQObject *obj, TQEvent *ev )
{
if( TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_pImageList) || TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_pImageList->m_pListView) || TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_pImageList->m_pListView->viewport()) || TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_pViewer->widget()) )
{
switch( ev->type() )
{
case TQEvent::DragEnter:
case TQEvent::DragMove:
{
// drag enter event in the image list
//kdDebug( 4630 ) << "DragEnterEvent in the image list: " << obj->className() << endl;
TQDragEnterEvent * e = static_cast<TQDragEnterEvent*>( ev );
//for( int i = 0; e->format( i ); ++i )
//kdDebug( 4630 ) << " - " << e->format( i ) << endl;
if( KURLDrag::canDecode( e ) )// || TQImageDrag::canDecode( e ) )
{
e->accept();
return true;
}
}
case TQEvent::Drop:
{
// drop event in the image list
kdDebug( 4630 ) << "DropEvent in the image list: " << obj->className() << endl;
TQDropEvent * e = static_cast<TQDropEvent*>( ev );
TQStringList l;
//TQImage image;
if( KURLDrag::decodeToUnicodeUris( e, l ) )
{
for( TQStringList::const_iterator it = l.begin(); it != l.end(); ++it )
{
ImageInfo * info = new ImageInfo( KURL( *it ) );
if( ! m_imagelist.contains( info ) )
{
m_imagelist.inSort( info );
( void )new ImageListItem( m_pImageList->m_pListView, KURL( *it ) );
}
else
delete info;
}
return true;
}
//else if( TQImageDrag::decode( e, image ) )
//newImage( image );
}
default: // do nothing
break;
}
}
return KParts::Plugin::eventFilter( obj, ev );
}
void KViewPresenter::slotImageOpened( const KURL & url )
{
kdDebug( 4630 ) << k_funcinfo << endl;
if( ! m_bDontAdd )
{
kdDebug( 4630 ) << k_funcinfo << "imagelist:" << endl;
ImageInfo * info = new ImageInfo( url );
if( ! m_imagelist.contains( info ) )
{
m_imagelist.inSort( info );
TQListViewItem * item = new ImageListItem( m_pImageList->m_pListView, url );
makeCurrent( item );
}
else
delete info;
}
}
void KViewPresenter::slotImageList()
{
kdDebug( 4630 ) << k_funcinfo << endl;
m_pImageList->show();
}
void KViewPresenter::slotOpenFiles()
{
kdDebug( 4630 ) << k_funcinfo << endl;
KURL::List urls = KFileDialog::getOpenURLs( ":load_image", KImageIO::pattern( KImageIO::Reading ), m_pViewer->widget() );
if( urls.isEmpty() )
return;
KURL::List::Iterator it = urls.begin();
m_pViewer->openURL( *it );
for( ++it; it != urls.end(); ++it )
{
ImageInfo * info = new ImageInfo( *it );
if( ! m_imagelist.contains( info ) )
{
m_imagelist.inSort( info );
( void )new ImageListItem( m_pImageList->m_pListView, *it );
}
else
delete info;
}
}
void KViewPresenter::slotClose()
{
TQListViewItem * next = m_pCurrentItem->itemBelow() ? m_pCurrentItem->itemBelow() : m_pImageList->m_pListView->firstChild();
if( next == m_pCurrentItem )
next = 0;
ImageInfo info( m_pCurrentItem->url() );
m_imagelist.remove( &info );
delete m_pCurrentItem;
m_pCurrentItem = 0;
if( next )
changeItem( next );
}
void KViewPresenter::changeItem( TQListViewItem * qitem )
{
kdDebug( 4630 ) << k_funcinfo << endl;
if( qitem->rtti() == 48294 )
{
ImageListItem * item = static_cast<ImageListItem*>( qitem );
if( ! item->url().isEmpty() )
{
if( item->url().isLocalFile() && ! TQFile::exists( item->url().path() ) )
{
kdDebug( 4630 ) << "file doesn't exist. removed." << endl;
ImageInfo info( item->url() );
m_imagelist.remove( &info );
if( m_pCurrentItem == item )
{
TQListViewItem * next = m_pCurrentItem->itemBelow() ? m_pCurrentItem->itemBelow() : m_pImageList->m_pListView->firstChild();
if( next->rtti() != 48294 )
kdWarning( 4630 ) << "unknown ListView item" << endl;
else
m_pCurrentItem = static_cast<ImageListItem*>( next );
if( m_pCurrentItem == item )
m_pCurrentItem = 0; // don't create a dangling pointer
delete item;
if( m_pCurrentItem )
changeItem( m_pCurrentItem );
}
else
{
delete item;
next();
}
return;
}
kdDebug( 4630 ) << "got url" << endl;
makeCurrent( qitem );
bool dontadd = m_bDontAdd;
m_bDontAdd = true;
m_pViewer->openURL( item->url() );
m_bDontAdd = dontadd;
}
else
kdWarning( 4630 ) << "got nothing" << endl;
}
else
kdWarning( 4630 ) << "unknown ListView item" << endl;
}
void KViewPresenter::prev()
{
kdDebug( 4630 ) << k_funcinfo << endl;
if( m_pCurrentItem )
{
TQListViewItem * prev = m_pCurrentItem->itemAbove() ? m_pCurrentItem->itemAbove() : m_pImageList->m_pListView->lastItem();
if( prev )
changeItem( prev );
}
}
void KViewPresenter::next()
{
kdDebug( 4630 ) << k_funcinfo << endl;
if( m_pCurrentItem )
{
TQListViewItem * next = m_pCurrentItem->itemBelow() ? m_pCurrentItem->itemBelow() : m_pImageList->m_pListView->firstChild();
if( next )
changeItem( next );
}
}
void KViewPresenter::makeCurrent( TQListViewItem * item )
{
if( m_pCurrentItem )
m_pCurrentItem->setPixmap( 0, TQPixmap() );
if( item->rtti() != 48294 )
kdWarning( 4630 ) << "unknown ListView item" << endl;
else
{
m_pCurrentItem = static_cast<ImageListItem*>( item );
m_pCurrentItem->setPixmap( 0, TDEGlobal::iconLoader()->loadIcon( "1rightarrow", KIcon::Small ) );
m_pImageList->m_pListView->ensureItemVisible( m_pCurrentItem );
}
}
void KViewPresenter::slideshow( bool running )
{
if( running )
{
m_pSlideshowTimer->start( m_pImageList->m_pInterval->value() );
actionCollection()->action( "plugin_presenter_slideshow" )->setText( i18n( "Stop &Slideshow" ) );
m_pImageList->m_pSlideshow->setText( i18n( "Stop &Slideshow" ) );
}
else
{
m_pSlideshowTimer->stop();
actionCollection()->action( "plugin_presenter_slideshow" )->setText( i18n( "Start &Slideshow" ) );
m_pImageList->m_pSlideshow->setText( i18n( "Start &Slideshow" ) );
}
}
void KViewPresenter::setSlideshowInterval( int msec )
{
if( m_pSlideshowTimer->isActive() )
m_pSlideshowTimer->changeInterval( msec );
}
void KViewPresenter::shuffle()
{
m_pImageList->noSort();
KListView * listview = m_pImageList->m_pListView;
TQPtrList<TQListViewItem> items;
for( TQListViewItem * item = listview->firstChild(); item; item = listview->firstChild() )
{
items.append( item );
listview->takeItem( item );
}
while( ! items.isEmpty() )
listview->insertItem( items.take( TDEApplication::random() % items.count() ) );
}
void KViewPresenter::closeAll()
{
m_imagelist.clear();
m_pImageList->m_pListView->clear();
m_pCurrentItem = 0;
if( m_pViewer->closeURL() )
m_pViewer->canvas()->clear();
}
void KViewPresenter::loadList()
{
KURL url = KFileDialog::getOpenURL( ":load_list", TQString(), m_pImageList );
if( url.isEmpty() )
return;
TQString tempfile;
if( ! KIO::NetAccess::download( url, tempfile, m_pViewer->widget() ) )
{
KMessageBox::error( m_pImageList, i18n( "Could not load\n%1" ).arg( url.prettyURL() ) );
return;
}
TQFile file( tempfile );
if( file.open( IO_ReadOnly ) )
{
TQTextStream t( &file );
if( t.readLine() == "[KView Image List]" )
{
//clear old image list
closeAll();
TQStringList list;
if( ! t.eof() )
m_pViewer->openURL( KURL( t.readLine() ) );
while( ! t.eof() )
{
KURL url ( t.readLine() );
ImageInfo * info = new ImageInfo( url );
if( ! m_imagelist.contains( info ) )
{
m_imagelist.inSort( info );
( void )new ImageListItem( m_pImageList->m_pListView, url );
}
else
delete info;
}
}
else
{
KMessageBox::error( m_pImageList, i18n( "Wrong format\n%1" ).arg( url.prettyURL() ) );
}
file.close();
}
KIO::NetAccess::removeTempFile( tempfile );
}
void KViewPresenter::saveList()
{
KURL url = KFileDialog::getSaveURL( ":save_list", TQString(), m_pImageList );
if( url.isEmpty() )
return;
TQString tempfile;
if( url.isLocalFile() )
tempfile = url.path();
else
{
KTempFile ktempf;
tempfile = ktempf.name();
}
TQFile file( tempfile );
if( file.open( IO_WriteOnly ) )
{
TQTextStream t( &file );
// write header
t << "[KView Image List]" << endl;
TQListViewItem * item = m_pImageList->m_pListView->firstChild();
while( item )
{
if( item->rtti() == 48294 )
t << static_cast<ImageListItem*>( item )->url().url() << endl;
item = item->itemBelow();
}
file.close();
if( ! url.isLocalFile() )
{
KIO::NetAccess::upload( tempfile, url, m_pViewer->widget() );
KIO::NetAccess::removeTempFile( tempfile );
}
}
}
// vim:sw=4:ts=4
#include "kviewpresenter.moc"
|