KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kmylistbox.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /***************************************************************************
  2. kmylistbox.cpp - description
  3. -------------------
  4. begin : Tue Oct 16 2001
  5. copyright : (C) 2001 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. // QT includes
  17. #include <tqcursor.h>
  18. #include <tqdir.h>
  19. #include <tqdragobject.h>
  20. #include <tqpainter.h>
  21. #include <tqpalette.h>
  22. #include <tqregexp.h>
  23. // KDE includes
  24. #include <tdeapplication.h>
  25. #include <kdirlister.h>
  26. #include <kiconloader.h>
  27. #include <tdelocale.h>
  28. #include <tdeio/previewjob.h>
  29. #include <tdeio/netaccess.h>
  30. #include <tqptrlist.h>
  31. #include <kurldrag.h>
  32. #include <kurllabel.h>
  33. #include <kpixmap.h>
  34. #include <kpixmapeffect.h>
  35. // Own includes
  36. #include "kmylistbox.h"
  37. #include "krecursivelister.h"
  38. #include "threadedlister.h"
  39. using namespace TDEIO;
  40. KMyListBox::KMyListBox(TQWidget* parent, const char* name, WFlags fl)
  41. :TDEListBox(parent, name, fl)
  42. {
  43. m_running_lister_counter = 0;
  44. drag = ctrlPressed = shiftPressed = mousePressed = false;
  45. moving = false;
  46. m_sorting = UNSORTED;
  47. label = new KURLLabel( TQString(), "<br>" + i18n("Please add some files...") + "<br>", this );
  48. label->setFrameStyle( TQFrame::GroupBoxPanel | TQFrame::Sunken );
  49. setAcceptDrops( true );
  50. setSelectionMode(Extended); // was extended before 2.9.0
  51. connect( this, TQT_SIGNAL(doubleClicked(TQListBoxItem*)), this, TQT_SLOT(openFile(TQListBoxItem*)));
  52. connect( this, TQT_SIGNAL(returnPressed(TQListBoxItem*)), this, TQT_SLOT(openFile(TQListBoxItem*)));
  53. connect( label, TQT_SIGNAL( leftClickedURL() ), this, TQT_SIGNAL( addFiles() ) );
  54. positionLabel();
  55. }
  56. KMyListBox::~KMyListBox()
  57. {
  58. }
  59. KURL KMyListBox::url( int index ) const
  60. {
  61. return static_cast<KMyListBoxItem*>( item( index ) )->url();
  62. }
  63. bool KMyListBox::dir( int index ) const
  64. {
  65. return static_cast<KMyListBoxItem*>( item( index ) )->dir();
  66. }
  67. void KMyListBox::setPreviewSize( int size )
  68. {
  69. previewSize = size;
  70. }
  71. void KMyListBox::removeItem( int index )
  72. {
  73. TDEListBox::removeItem( index );
  74. }
  75. void KMyListBox::addFile( const KURL & filename, bool isfile )
  76. {
  77. // Check if the URL is a file and no dir
  78. // Is the file already in our list ?
  79. if( !isfile && !isFile( filename ) )
  80. return;
  81. if( isInList( filename ) )
  82. return;
  83. insertItem( new KMyListBoxItem( filename, false ), -1 );
  84. positionLabel();
  85. }
  86. void KMyListBox::addDirName( const KURL & dirname )
  87. {
  88. if(!isInList( dirname ) )
  89. {
  90. insertItem( new KMyListBoxItem( dirname, true ), -1 );
  91. positionLabel();
  92. }
  93. }
  94. bool KMyListBox::isFile( const KURL & f, bool autoadd )
  95. {
  96. UDSEntry entry;
  97. NetAccess::stat(f, entry, 0);
  98. KFileItem file( entry, f );
  99. if( !file.isReadable() )
  100. return false;
  101. if( file.isDir() ) {
  102. if( autoadd )
  103. addDir( f, "*", false, true );
  104. return false;
  105. }
  106. return true;
  107. }
  108. void KMyListBox::addDir( const KURL & dirname, const TQString & filter, bool hidden, bool recursively, bool dirnames )
  109. {
  110. ThreadedLister* thl = new ThreadedLister( &m_add_mutex, &m_running_lister_counter, this );
  111. thl->setDirname( dirname );
  112. thl->setDirnames( dirnames );
  113. thl->setFilter( filter );
  114. thl->setHidden( hidden );
  115. thl->setRecursive( recursively );
  116. TDEApplication::setOverrideCursor( TQt::waitCursor );
  117. thl->start();
  118. }
  119. void KMyListBox::addDirName( const KURL & dirname, const TQString & filter, bool hidden, bool recursive )
  120. {
  121. TDEApplication::setOverrideCursor( TQt::waitCursor );
  122. if( recursive ) {
  123. ThreadedLister* thl = new ThreadedLister( &m_add_mutex, &m_running_lister_counter, this );
  124. thl->setDirname( dirname );
  125. thl->setDirnames( true );
  126. thl->setFilter( filter );
  127. thl->setHidden( hidden );
  128. thl->setRecursive( recursive );
  129. thl->setRecursiveDirOnlyMode( true );
  130. thl->start();
  131. } else {
  132. // escape hiden directories
  133. TQString name = dirname.fileName();
  134. if( !hidden && name.right( 1 ) != TQString::fromLatin1(".") )
  135. if( !isInList( dirname ) )
  136. addDirName( dirname );
  137. listerDone( NULL );
  138. }
  139. }
  140. void KMyListBox::dropEvent(TQDropEvent* e)
  141. {
  142. if( e->source() != this )
  143. e->accept(TQTextDrag::canDecode(e));
  144. KURL::List list;
  145. if( KURLDrag::decode( e, list ) )
  146. {
  147. TDEApplication::setOverrideCursor( TQt::waitCursor );
  148. setUpdatesEnabled( false );
  149. for( unsigned int i = 0; i < list.count(); i++ )
  150. addFile( list[i], false );
  151. setUpdatesEnabled( true );
  152. listerDone( NULL );
  153. }
  154. }
  155. void KMyListBox::dragEnterEvent(TQDragEnterEvent* e)
  156. {
  157. if( e->source() != this )
  158. e->accept(TQTextDrag::canDecode(e));
  159. }
  160. void KMyListBox::viewportMousePressEvent( TQMouseEvent* e )
  161. {
  162. if( moving )
  163. move( index( itemAt( e->pos() ) ) );
  164. else {
  165. TDEListBox::viewportMousePressEvent( e );
  166. TQPoint p( e->pos() );
  167. TQListBoxItem *i = itemAt( p );
  168. if ( i ) {
  169. presspos = e->pos();
  170. mousePressed = TRUE;
  171. }
  172. }
  173. }
  174. void KMyListBox::viewportMouseMoveEvent( TQMouseEvent* e )
  175. {
  176. if ( mousePressed && ( presspos - e->pos() ).manhattanLength() > TDEApplication::startDragDistance() ) {
  177. mousePressed = FALSE;
  178. TQListBoxItem *item = itemAt( presspos );
  179. if ( item ) {
  180. TQStringList source = text( index( item ) );
  181. for( int i = 0; i < (signed int)count(); i++ )
  182. if( isSelected(i) && (i != index(item) ))
  183. source.append( text(i) );
  184. TQUriDrag* ud = new TQUriDrag(viewport());
  185. ud->setUnicodeUris( source );
  186. ud->drag();
  187. }
  188. } else
  189. TDEListBox::viewportMouseMoveEvent( e );
  190. }
  191. void KMyListBox::viewportMouseReleaseEvent( TQMouseEvent* e )
  192. {
  193. mousePressed = FALSE;
  194. TDEListBox::viewportMouseReleaseEvent( e );
  195. }
  196. void KMyListBox::keyPressEvent( TQKeyEvent* e )
  197. {
  198. /*
  199. * TODO: Document all this keyboard commands
  200. */
  201. if( e->key() == Key_Control )
  202. ctrlPressed = true;
  203. else if( e->key() == Key_Shift )
  204. shiftPressed = true;
  205. else if( e->key() == Key_Up )
  206. setCurrentItem( currentItem()-1 );
  207. else if( e->key() == Key_Down )
  208. setCurrentItem( currentItem()+1 );
  209. else if( e->key() == Key_Space )
  210. select( item(currentItem()) );
  211. else if( e->key() == Key_Return )
  212. openFile( item(currentItem()) );
  213. else if( e->key() == Key_Delete )
  214. emit deletePressed();
  215. else if( e->key() == Key_A )
  216. for( unsigned int i = 0; i < count(); i++ )
  217. setSelected( i, true );
  218. else if( e->key() == Key_M ) {
  219. moveMode();
  220. return;
  221. } else if( e->key() == Key_N )
  222. for( unsigned int i = 0; i < count(); i++ )
  223. setSelected( i, false );
  224. else if( e->key() == Key_End )
  225. setCurrentItem( count()-1 );
  226. else if( e->key() == Key_Home )
  227. setCurrentItem( 0 );
  228. else
  229. e->ignore();
  230. emit updateCount();
  231. setPreview( KMyListBoxItem::preview() );
  232. emit updatePreview();
  233. }
  234. void KMyListBox::keyReleaseEvent( TQKeyEvent* e )
  235. {
  236. if( e->key() == Key_Control )
  237. ctrlPressed = false;
  238. else if( e->key() == Key_Shift )
  239. shiftPressed = false;
  240. }
  241. void KMyListBox::openFile( TQListBoxItem* item )
  242. {
  243. if( item ) {
  244. KMyListBoxItem* it = static_cast<KMyListBoxItem*>(item);
  245. KFileItem* fileItem = new KFileItem( KFileItem::Unknown, KFileItem::Unknown, it->url() );
  246. fileItem->run();
  247. delete fileItem;
  248. }
  249. }
  250. void KMyListBox::moveMode()
  251. {
  252. if ( !moving ) {
  253. moving = true;
  254. TDEApplication::setOverrideCursor( TQt::sizeAllCursor );
  255. }
  256. }
  257. void KMyListBox::select( TQListBoxItem* item )
  258. {
  259. if( !ctrlPressed && !shiftPressed )
  260. /* Single click on the list box,
  261. * make all items but the clicked
  262. * one not selected */
  263. for( int i = 0; i < (signed int)count(); i++ )
  264. if( i != index(item) )
  265. setSelected( i, false );
  266. if( shiftPressed ) {
  267. if( currentItem() == -1 ) {
  268. setSelected( item, !isSelected( item ));
  269. setCurrentItem( item );
  270. return;
  271. }
  272. if( currentItem() > index(item) ) {
  273. for( int i = index(item); i <= currentItem(); i++ )
  274. setSelected( i, !isSelected( i ));
  275. setCurrentItem( item );
  276. } else if( currentItem() < index(item) ) { /* Works !*/
  277. for( int i = currentItem()+1; i <= index(item); i++ )
  278. setSelected( i, !isSelected( i ));
  279. } else /* c == index(item) */ /* Works !*/
  280. setSelected( item, !isSelected( item ));
  281. } else {
  282. setSelected( item, !isSelected( item ));
  283. setCurrentItem( item );
  284. }
  285. }
  286. void KMyListBox::preview( KURL::List list )
  287. {
  288. TDEIO::PreviewJob* job = TDEIO::filePreview( list, previewSize, previewSize, 0, 100, false, true, 0 );
  289. connect( job, TQT_SIGNAL( gotPreview( const KFileItem*, const TQPixmap &) ), this, TQT_SLOT( previewDone( const KFileItem*, const TQPixmap &) ) );
  290. connect( job, TQT_SIGNAL( failed( const KFileItem*)), this, TQT_SLOT( previewFailed( const KFileItem* ) ));
  291. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ), this, TQT_SLOT( previewFinished() ) );
  292. TDEApplication::setOverrideCursor( TQt::waitCursor );
  293. }
  294. void KMyListBox::previewDone( const KFileItem* item, const TQPixmap &pixmap )
  295. {
  296. for( unsigned int i = 0; i < count(); i++ )
  297. if( url( i ) == item->url() ) {
  298. KMyListBoxItem* it = static_cast<KMyListBoxItem*>(this->item( i ));
  299. if( it && !pixmap.isNull() ) {
  300. it->setPixmap( pixmap );
  301. //updateItem( i );
  302. }
  303. break;
  304. }
  305. }
  306. void KMyListBox::previewFailed( const KFileItem* item )
  307. {
  308. for( unsigned int i = 0; i < count(); i++ )
  309. if( url( i ) == item->url() ) {
  310. KMyListBoxItem* it = static_cast<KMyListBoxItem*>(this->item( i ));
  311. if( it ) {
  312. it->setPixmap( item->pixmap( getPreviewSize(), TDEIcon::DefaultState ) );
  313. }
  314. break;
  315. }
  316. }
  317. void KMyListBox::previewFinished()
  318. {
  319. triggerUpdate( true ); //maybe false is enough
  320. TDEApplication::restoreOverrideCursor();
  321. }
  322. void KMyListBox::setPreview( bool prv )
  323. {
  324. KMyListBoxItem::setPreview( prv );
  325. if( prv ) {
  326. KURL::List list;
  327. for( unsigned int i = 0; i < count(); i++ ) {
  328. KMyListBoxItem* it = static_cast<KMyListBoxItem*>(item( i ) );
  329. if( !it->hasPreview() )
  330. list.append( it->url() );
  331. }
  332. preview( list );
  333. }
  334. }
  335. void KMyListBox::setName( bool name )
  336. {
  337. KMyListBoxItem::setName( name );
  338. setPreview( KMyListBoxItem::preview() );
  339. if( name ) {
  340. setColumnMode( FixedNumber );
  341. } else {
  342. setColumnMode( FitToWidth );
  343. }
  344. }
  345. void KMyListBox::move( int i )
  346. {
  347. TDEApplication::restoreOverrideCursor();
  348. moving = false;
  349. if( !count() )
  350. return;
  351. int nbSelectedBefore = 0;
  352. for( unsigned int j = 0 ; j < count() ; j++ ) {
  353. if( isSelected( j ) ) {
  354. if( j < (unsigned int)i )
  355. nbSelectedBefore++;
  356. KMyListBoxItem* item = new KMyListBoxItem( static_cast<KMyListBoxItem*>(this->item( j )) );
  357. removeItem( j );
  358. j--;
  359. insertItem( item, i - nbSelectedBefore );
  360. }
  361. }
  362. }
  363. void KMyListBox::moveUp()
  364. {
  365. if( count() == 0 )
  366. return;
  367. unsigned int i = 0;
  368. setUpdatesEnabled( false );
  369. do {
  370. if( isSelected( i ) && i ) {
  371. moveUp( i );
  372. setSelected( i-1, true );
  373. setCurrentItem( i-1 );
  374. }
  375. i++;
  376. } while( i < count() );
  377. setUpdatesEnabled( true );
  378. }
  379. void KMyListBox::moveUp( int i )
  380. {
  381. if( count() == 0 )
  382. return;
  383. KMyListBoxItem* item = new KMyListBoxItem( static_cast<KMyListBoxItem*>(this->item( i )) );
  384. removeItem( i );
  385. insertItem( item, i - 1 );
  386. }
  387. void KMyListBox::moveDown()
  388. {
  389. if( count() == 0 )
  390. return;
  391. unsigned int i = count();
  392. setUpdatesEnabled( false );
  393. do {
  394. i--;
  395. if( isSelected( i ) && (i < count()) ) {
  396. moveDown( i );
  397. setSelected( i+1, true );
  398. setCurrentItem( i+1 );
  399. }
  400. } while( i > 0 );
  401. setUpdatesEnabled( true );
  402. }
  403. void KMyListBox::moveDown( int i )
  404. {
  405. if( count() == 0 )
  406. return;
  407. KMyListBoxItem* item = new KMyListBoxItem( static_cast<KMyListBoxItem*>(this->item( i )) );
  408. removeItem( i );
  409. insertItem( item, i + 1 );
  410. }
  411. bool KMyListBox::isInList( KURL text )
  412. {
  413. // TODO: faster find algorithm
  414. for( unsigned int i = 0; i < count(); i++ ) {
  415. KMyListBoxItem* it = dynamic_cast<KMyListBoxItem*>(item( i ) );
  416. if( it && it->url() == text ) {
  417. return true;
  418. }
  419. }
  420. return false;
  421. }
  422. void KMyListBox::customEvent( TQCustomEvent* e )
  423. {
  424. if( e->type() == ThreadedLister::TYPE() )
  425. {
  426. listerDone( (ThreadedLister*)e->data() );
  427. }
  428. }
  429. void KMyListBox::listerDone( ThreadedLister* lister )
  430. {
  431. m_add_mutex.lock();
  432. if( lister )
  433. delete lister;
  434. TDEApplication::restoreOverrideCursor();
  435. setUpdatesEnabled( false );
  436. setPreview( KMyListBoxItem::preview() );
  437. sortList();
  438. setUpdatesEnabled( true );
  439. emit updateCount();
  440. emit updatePreview();
  441. m_add_mutex.unlock();
  442. }
  443. unsigned int KMyListBox::runningAddListeners()
  444. {
  445. unsigned int u;
  446. m_add_mutex.lock();
  447. u = m_running_lister_counter;
  448. m_add_mutex.unlock();
  449. return u;
  450. }
  451. void KMyListBox::sortAscending()
  452. {
  453. m_sorting = ASCENDING;
  454. sortList();
  455. }
  456. void KMyListBox::sortDescending()
  457. {
  458. m_sorting = DESCENDING;
  459. sortList();
  460. }
  461. void KMyListBox::sortRandom()
  462. {
  463. m_sorting = RANDOM;
  464. sortList();
  465. }
  466. void KMyListBox::sortUnsorted()
  467. {
  468. m_sorting = UNSORTED;
  469. sortList();
  470. }
  471. void KMyListBox::sortNummeric()
  472. {
  473. m_sorting = NUMMERIC;
  474. sortList();
  475. }
  476. void KMyListBox::sortList()
  477. {
  478. TDEApplication::setOverrideCursor( TQt::WaitCursor );
  479. if( m_sorting == ASCENDING )
  480. sort( true );
  481. else if( m_sorting == DESCENDING )
  482. sort( false );
  483. else if( m_sorting == RANDOM )
  484. {
  485. unsigned int p = 0;
  486. for( unsigned int i = 0;i<count();i++)
  487. {
  488. p = TDEApplication::random() % count();
  489. if( p != i ) // This prevents the creation of a new ListBoxItem
  490. // on the same position as before. It would not change
  491. // the position of item, but cost a little bit of speed
  492. {
  493. KMyListBoxItem* item = new KMyListBoxItem( static_cast<KMyListBoxItem*>(this->item( i )) );
  494. removeItem( i );
  495. insertItem( item, p );
  496. }
  497. }
  498. }
  499. else if( m_sorting == NUMMERIC )
  500. {
  501. // a very simple bubble sort which sorts filenames by a number inside them.
  502. // if no number is found a lexical comparison is performed
  503. unsigned int z = count();
  504. while( z-- )
  505. for( unsigned int i=1;i<=z;i++)
  506. {
  507. KURL url1 = url( i - 1 );
  508. KURL url2 = url( i );
  509. if( url1.directory() != url2.directory() )
  510. {
  511. // different directory, do a lexical comparison
  512. if( text( i - 1 ).compare( text( i ) ) >= 0 )
  513. moveDown( i - 1 );
  514. }
  515. else
  516. {
  517. if( compareNummeric( url1.filename(), url2.filename() ) >= 0 )
  518. moveDown( i - 1 );
  519. }
  520. }
  521. }
  522. TDEApplication::restoreOverrideCursor();
  523. }
  524. void KMyListBox::setSorting( int s )
  525. {
  526. switch( s )
  527. {
  528. default:
  529. case UNSORTED:
  530. sortUnsorted();
  531. break;
  532. case ASCENDING:
  533. sortAscending();
  534. break;
  535. case DESCENDING:
  536. sortDescending();
  537. break;
  538. case RANDOM:
  539. sortRandom();
  540. break;
  541. case NUMMERIC:
  542. sortNummeric();
  543. break;
  544. }
  545. emit updatePreview();
  546. }
  547. int KMyListBox::compareNummeric( const TQString & s1, const TQString & s2 )
  548. {
  549. unsigned int z = 0;
  550. unsigned int max = ( s1.length() > s2.length() ? s1.length() : s2.length() );
  551. TQString num1;
  552. TQString num2;
  553. for( z=0;z<max;z++)
  554. {
  555. //if( z >= s1.length() || z >= s2.length() )
  556. // break;
  557. if( s1[z] != s2[z] )
  558. {
  559. if( z < s1.length() && s1[z].isDigit() )
  560. num1 = findNumInString( z, s1 );
  561. if( z < s2.length() && s2[z].isDigit() )
  562. num2 = findNumInString( z, s2 );
  563. if( num1.isNull() && num2.isNull() )
  564. break;
  565. int a = num1.toInt();
  566. int b = num2.toInt();
  567. if( a == b )
  568. return s1.compare( s2 );
  569. else
  570. return ( a > b ) ? 1 : -1;
  571. }
  572. }
  573. return s1.compare( s2 );
  574. }
  575. const TQString KMyListBox::findNumInString( unsigned int pos, const TQString & s )
  576. {
  577. TQString num;
  578. for( int i = (int)pos; i >= 0; i-- )
  579. if( s[i].isDigit() )
  580. num.prepend( s[i] );
  581. else
  582. break;
  583. for( unsigned int i = pos + 1; i < s.length(); i++ )
  584. if( s[i].isDigit() )
  585. num.append( s[i] );
  586. else
  587. break;
  588. return num;
  589. }
  590. void KMyListBox::resizeEvent( TQResizeEvent* e )
  591. {
  592. TDEListBox::resizeEvent( e );
  593. positionLabel();
  594. }
  595. void KMyListBox::clear()
  596. {
  597. TDEListBox::clear();
  598. positionLabel();
  599. }
  600. void KMyListBox::positionLabel()
  601. {
  602. if( count() )
  603. {
  604. label->hide();
  605. }
  606. else
  607. {
  608. int x = (width() - label->minimumSizeHint().width()) / 2;
  609. int y = (height() - label->minimumSizeHint().height()) / 2;
  610. label->setGeometry( x, y, label->minimumSizeHint().width(), label->minimumSizeHint().height() );
  611. label->show();
  612. }
  613. }
  614. void KMyListBox::paintEvent( TQPaintEvent* e )
  615. {
  616. // tqDebug("Updates=%i", (int)isUpdatesEnabled() );
  617. //if( isUpdatesEnabled() )
  618. TDEListBox::paintEvent( e );
  619. }
  620. KMyListBoxItem::KMyListBoxItem( const KMyListBoxItem* item )
  621. : TQListBoxItem()
  622. {
  623. m_url = item->url();
  624. m_dir = item->dir();
  625. m_has_preview = false;
  626. pm = *item->pixmap();
  627. }
  628. KMyListBoxItem::KMyListBoxItem( const KURL & u, bool b )
  629. : TQListBoxItem()
  630. {
  631. m_url = u;
  632. m_dir = b;
  633. m_has_preview = false;
  634. }
  635. void KMyListBoxItem::setPixmap( const TQPixmap & pix )
  636. {
  637. KMyListBox* box = static_cast<KMyListBox*>(this->listBox());
  638. pm.resize( box->getPreviewSize(), box->getPreviewSize() );
  639. pm.fill( box->colorGroup().base() );
  640. TQPainter painter( &pm );
  641. painter.drawPixmap( (pm.width()-pix.width())/2, (pm.height()-pix.height())/2, pix );
  642. m_has_preview = true;
  643. }
  644. void KMyListBoxItem::setName( bool b )
  645. {
  646. KMyListBoxItem::m_name = b;
  647. }
  648. void KMyListBoxItem::setPreview( bool b )
  649. {
  650. KMyListBoxItem::m_preview = b;
  651. }
  652. void KMyListBoxItem::paint( TQPainter *painter )
  653. {
  654. if( !KMyListBoxItem::m_preview ) {
  655. int itemHeight = height( listBox() );
  656. TQFontMetrics fm = painter->fontMetrics();
  657. int yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
  658. painter->drawText( 3, yPos, text() );
  659. } else {
  660. int itemHeight = height( listBox() );
  661. int yPos;
  662. if( pm.isNull() ) {
  663. KFileItem item( KFileItem::Unknown, KFileItem::Unknown, m_url );
  664. KMyListBox* box = static_cast<KMyListBox*>(this->listBox());
  665. setPixmap( item.pixmap( box->getPreviewSize(), TDEIcon::DefaultState ) );
  666. }
  667. yPos = ( itemHeight - pm.height() ) / 2;
  668. if( !isSelected() )
  669. painter->drawPixmap( 3, yPos, pm);
  670. else
  671. {
  672. KPixmap pix = KPixmapEffect::selectedPixmap( pm, listBox()->colorGroup().highlight() );
  673. painter->drawPixmap( 3, yPos, pix );
  674. }
  675. if( KMyListBoxItem::m_name && !m_url.isEmpty() ) {
  676. TQFontMetrics fm = painter->fontMetrics();
  677. yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
  678. painter->drawText( pm.width() + 5, yPos, text() );
  679. }
  680. }
  681. }
  682. int KMyListBoxItem::height( const TQListBox* lb ) const
  683. {
  684. if( !KMyListBoxItem::m_preview ) {
  685. int h = listBox() ? listBox()->fontMetrics().lineSpacing() + 2 : 0;
  686. return TQMAX( h, TQApplication::globalStrut().height() );
  687. } else {
  688. int h;
  689. if ( KMyListBoxItem::m_name && !m_url.prettyURL().isEmpty() )
  690. h = pm.height();
  691. else
  692. h = TQMAX( pm.height(), lb->fontMetrics().lineSpacing() + 2 );
  693. return TQMAX( h, TQApplication::globalStrut().height() );
  694. }
  695. }
  696. int KMyListBoxItem::width( const TQListBox* ) const
  697. {
  698. if( !KMyListBoxItem::m_preview ) {
  699. int w = listBox() ? listBox()->fontMetrics().width( text() ) + 6 : 0;
  700. return TQMAX( w, TQApplication::globalStrut().width() );
  701. } else {
  702. if ( m_url.path().isEmpty() || !KMyListBoxItem::m_name)
  703. return TQMAX( pm.width() + 6, TQApplication::globalStrut().width() );
  704. return TQMAX( pm.width() + listBox()->fontMetrics().width( text() ) + 6, TQApplication::globalStrut().width() );
  705. }
  706. }
  707. TQString KMyListBoxItem::text() const
  708. {
  709. return m_url.prettyURL( 0, m_url.isLocalFile() ? KURL::StripFileProtocol : KURL::NoAdjustements );
  710. }
  711. bool KMyListBoxItem::m_preview = false;
  712. bool KMyListBoxItem::m_name = false;
  713. #include "kmylistbox.moc"