KBFX – an alternative to T-Menu
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.

668 lines
16KB

  1. /*
  2. * Copyright (C) 2006
  3. * Siraj Razick <siraj@kdemail.net>
  4. * PhobosK <phobosk@mail.kbfx.net>
  5. * see Also AUTHORS
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License version 2 as
  9. * published by the Free Software Foundation
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include "kbfxplasmacanvasview.h"
  22. KbfxPlasmaCanvasView::KbfxPlasmaCanvasView ( TQWidget * parent,
  23. const char *name, WFlags fl ) :
  24. TQCanvasView ( parent, name,fl )
  25. {
  26. viewport ()->setMouseTracking ( TRUE );
  27. viewport ()->setAcceptDrops ( true );
  28. this->setVScrollBarMode ( TQScrollView::AlwaysOff );
  29. this->setHScrollBarMode ( TQScrollView::AlwaysOff );
  30. setFrameShape ( TQFrame::NoFrame );
  31. m_currentItem = 0L;
  32. m_stack = 0;
  33. m_currentView = new KbfxPlasmaCanvasGroupView ();
  34. m_lastSignalData = "";
  35. gstack = 0;
  36. m_exeCandidate = 0;
  37. m_findDone = true;
  38. m_selectedItem = 0L;
  39. m_search = new TQCanvas ( this->width (), this->height () );
  40. m_scrollbar_top = new KbfxSpinxScrollBar ( this );
  41. m_scrollbar_bot = new KbfxSpinxScrollBar ( this,"bottom",1);
  42. m_scrollbar_top->setType ( KbfxSpinxScrollBar::UP );
  43. m_scrollbar_bot->setType ( KbfxSpinxScrollBar::DOWN );
  44. connect ( m_scrollbar_top, TQT_SIGNAL ( scroll ( int, int ) ), this,
  45. TQT_SLOT ( scrollBy ( int, int ) ) );
  46. connect ( m_scrollbar_bot, TQT_SIGNAL ( scroll ( int, int ) ), this,
  47. TQT_SLOT ( scrollBy ( int, int ) ) );
  48. placeScrollBars ();
  49. }
  50. void
  51. KbfxPlasmaCanvasView::placeScrollBars ()
  52. {
  53. m_scrollbar_top->move ( 0, 0 );
  54. m_scrollbar_bot->move ( 0, ( this->height () + 1 ) - m_scrollbar_bot->height () );
  55. }
  56. KbfxPlasmaCanvasView::~KbfxPlasmaCanvasView ()
  57. {
  58. }
  59. void
  60. KbfxPlasmaCanvasView::keyPressEvent ( TQKeyEvent * ke )
  61. {
  62. tqDebug ( "TQCanvasView got Key events" );
  63. ke->ignore();
  64. }
  65. void
  66. KbfxPlasmaCanvasView::execFirst ()
  67. {
  68. //if ( m_search->allItems ().count () < 1 )
  69. // {
  70. // return;
  71. // }
  72. tqDebug("_____----------------------------------------");
  73. if ( m_exeCandidate != 0 )
  74. m_exeCandidate->exec ();
  75. }
  76. void
  77. KbfxPlasmaCanvasView::clearAllButOne ( KbfxPlasmaCanvasItem * i )
  78. {
  79. i = i;
  80. TQCanvasItemList list = canvas ()->allItems ();
  81. TQCanvasItemList::Iterator it = list.begin ();
  82. for ( ; it != list.end (); ++it )
  83. {
  84. if ( *it )
  85. {
  86. if ( *it != m_selectedItem )
  87. {
  88. KbfxPlasmaIndexItem *t = ( KbfxPlasmaIndexItem * ) ( *it );
  89. t->setCurrent ( false );
  90. }
  91. }
  92. }
  93. canvas ()->update ();
  94. }
  95. void
  96. KbfxPlasmaCanvasView::execAt ( int i )
  97. {
  98. int e = i - 48;
  99. int e_count = m_search->allItems ().count ();
  100. if ( e_count < 2 )
  101. {
  102. return;
  103. }
  104. if ( e >= e_count - 1 )
  105. return;
  106. KbfxPlasmaCanvasItem *tmp = m_searchMap[e];
  107. if ( tmp == 0 )
  108. return;
  109. else
  110. tmp->exec ();
  111. }
  112. void
  113. KbfxPlasmaCanvasView::leaveEvent ( TQEvent * e )
  114. {
  115. e = e;
  116. clearAllButOne ( m_currentItem );
  117. }
  118. void
  119. KbfxPlasmaCanvasView::enterEvent ( TQEvent * e )
  120. {
  121. e = e;
  122. clearAllButOne ( m_currentItem );
  123. }
  124. void
  125. KbfxPlasmaCanvasView::mouseMoveEvent ( TQMouseEvent * me )
  126. {
  127. TQPoint p = inverseWorldMatrix ().map ( viewportToContents ( me->pos () ) );
  128. if ( canvas () == NULL )
  129. return;
  130. // KbfxPlasmaCanvasGroup *tmp = 0;
  131. TQCanvasItemList l = canvas ()->collisions ( p );
  132. for ( TQCanvasItemList::Iterator it = l.begin (); it != l.end (); ++it )
  133. {
  134. if ( *it == 0 )
  135. return;
  136. if ( ( *it )->rtti () == CANVASITEM )
  137. {
  138. KbfxPlasmaCanvasItem *t = ( KbfxPlasmaCanvasItem * ) ( *it );
  139. t->mouseMoveEvent ( me );
  140. if ( m_currentItem != NULL )
  141. {
  142. m_currentItem->setCurrent ( false );
  143. }
  144. m_currentItem = t;
  145. t->setCurrent ( true );
  146. }
  147. }
  148. canvas ()->update ();
  149. }
  150. void
  151. KbfxPlasmaCanvasView::contentsMouseMoveEvent ( TQMouseEvent * me )
  152. {
  153. if ( canvas () == NULL )
  154. return;
  155. if ( me->state () & Qt::LeftButton )
  156. {
  157. int distance = ( me->pos () - m_dragPos ).manhattanLength ();
  158. if ( distance > TQApplication::startDragDistance () )
  159. this->startDrag ();
  160. }
  161. TQScrollView::contentsMouseMoveEvent ( me );
  162. TQCanvasItemList l = canvas ()->collisions ( me->pos () );
  163. if ( l.count () <= 0 )
  164. return;
  165. for ( TQCanvasItemList::Iterator it = l.begin (); it != l.end (); ++it )
  166. {
  167. if ( *it == 0 )
  168. return;
  169. if ( ( *it )->rtti () == CANVASITEM )
  170. {
  171. KbfxPlasmaCanvasItem *t = ( KbfxPlasmaCanvasItem * ) ( *it );
  172. t->mouseMoveEvent ( me );
  173. if ( m_currentItem != NULL )
  174. {
  175. m_currentItem->setCurrent ( false );
  176. }
  177. m_currentItem = t;
  178. t->setCurrent ( true );
  179. this->setCursor ( TQCursor ( TQt::PointingHandCursor ) );
  180. }
  181. }
  182. canvas ()->update ();
  183. }
  184. void
  185. KbfxPlasmaCanvasView::contentsMousePressEvent ( TQMouseEvent * me )
  186. {
  187. KbfxPlasmaCanvasGroup *tmp = 0;
  188. TQCanvasItemList l = canvas ()->collisions ( me->pos () );
  189. for ( TQCanvasItemList::Iterator it = l.begin (); it != l.end (); ++it )
  190. {
  191. if ( ( *it )->rtti () == CANVASITEM )
  192. {
  193. KbfxPlasmaCanvasItem *t = ( KbfxPlasmaCanvasItem * ) ( *it );
  194. t->mousePressEvent ( me );
  195. if ( t->type () == KbfxPlasmaCanvasItem::SEPARATOR )
  196. tmp = KbfxPlasmaCanvasGroup::groupContaining ( t );
  197. }
  198. }
  199. if ( tmp != 0 )
  200. tmp->shade ();
  201. m_clickPos = me->pos ();
  202. if ( me->button () == Qt::LeftButton )
  203. m_dragPos = me->pos ();
  204. TQScrollView::contentsMousePressEvent ( me );
  205. canvas ()->update ();
  206. }
  207. void
  208. KbfxPlasmaCanvasView::startDrag ()
  209. {
  210. if ( m_currentItem != 0 )
  211. {
  212. KbfxDataSource *src = new KbfxDataSource ();
  213. src = m_currentItem->source ();
  214. if ( src == 0 )
  215. {
  216. tqDebug ( "null source" );
  217. return;
  218. }
  219. TQStrList uriList;
  220. uriList.append(src->contentPath().ascii());
  221. TQUriDrag *drag =
  222. new TQUriDrag ( uriList, this,
  223. src->name ().ascii () );
  224. drag->setFileNames ( TQStringList ( src->contentPath () ) );
  225. drag->setPixmap ( m_currentItem->dragPixmap () );
  226. drag->drag ();
  227. emit clicked ();
  228. }
  229. }
  230. void
  231. KbfxPlasmaCanvasView::resizeEvent ( TQResizeEvent * )
  232. {
  233. placeScrollBars ();
  234. }
  235. /*
  236. void
  237. KbfxPlasmaCanvasView::enterEvent ( TQEvent * e )
  238. {
  239. e = e;
  240. if ( canvas () == 0 )
  241. return;
  242. // KbfxPlasmaCanvasGroup *tmp = 0;
  243. TQPoint _mPos = TQCursor::pos ();
  244. TQCanvasItemList l = canvas ()->collisions ( this->mapFromGlobal ( _mPos ) );
  245. for ( TQCanvasItemList::Iterator it = l.begin (); it != l.end (); ++it )
  246. {
  247. if ( *it == 0 )
  248. break;
  249. if ( ( *it )->rtti () == CANVASITEM )
  250. {
  251. KbfxPlasmaCanvasItem *t = ( KbfxPlasmaCanvasItem * ) ( *it );
  252. // t->enterEvent ( e);
  253. m_currentItem = t;
  254. }
  255. }
  256. }
  257. */
  258. void
  259. KbfxPlasmaCanvasView::reload()
  260. {
  261. clearAll();
  262. clearSearch();
  263. m_appletMap.clear();
  264. }
  265. void
  266. KbfxPlasmaCanvasView::clearAll ()
  267. {
  268. if ( canvas() != m_search )
  269. return ;
  270. TQCanvasItemList list = canvas ()->allItems ();
  271. TQCanvasItemList::Iterator it = list.begin ();
  272. for ( ; it != list.end (); ++it )
  273. {
  274. if ( *it )
  275. {
  276. // KbfxPlasmaCanvasItem *t = (KbfxPlasmaCanvasItem *) (*it);
  277. m_search->removeItem ( ( *it ) );
  278. ( *it )->setCanvas ( 0L );
  279. // delete (*it);
  280. }
  281. }
  282. canvas()->update();
  283. }
  284. void
  285. KbfxPlasmaCanvasView::setKbfxCanvas ( TQCanvas * cnv )
  286. {
  287. m_default = cnv;
  288. setCanvas ( m_default );
  289. // canvas ()->setBackgroundPixmap ( ( *KbfxPlasmaPixmapProvider::pixmap ( "appviewbg" )) );
  290. TQPixmap * pixmap = KbfxPlasmaPixmapProvider::pixmap ( "middleboxbg" );
  291. if ( pixmap != NULL )
  292. {
  293. TQImage img = pixmap->convertToImage();
  294. img = img.smoothScale ( this->width(),pixmap->height(),TQ_ScaleFree );
  295. pixmap->convertFromImage ( img );
  296. this->canvas ()->setBackgroundPixmap ( *pixmap );
  297. }
  298. }
  299. void
  300. KbfxPlasmaCanvasView::expandAll ()
  301. {
  302. if ( m_currentView != 0 )
  303. {
  304. if ( ConfigInit ().m_startHidden == false )
  305. m_currentView->unfoldGroupAll ();
  306. scroll ( 0, 0 );
  307. }
  308. }
  309. void
  310. KbfxPlasmaCanvasView::handleLoadRequest ( KbfxSignal signal )
  311. {
  312. TQString tm = signal.name ();
  313. KbfxDataStack *_stack = m_dataStack[signal.name () ];
  314. if ( _stack == 0 )
  315. {
  316. tqDebug ( "Empty Stack" );
  317. return;
  318. }
  319. TQString _activateGroup = _stack->getSubGroupName ( signal.id () );
  320. KbfxDataGroupList *_dataList = _stack->getStack ( _activateGroup );
  321. KbfxPlasmaCanvasGroupView *gview = new KbfxPlasmaCanvasGroupView ();
  322. KbfxPlasmaCanvasStack *v_stack = m_appletMap[signal.name () ];
  323. if ( m_currentView->name () == _activateGroup && canvas() != m_search)
  324. {
  325. return;
  326. }
  327. setCanvas ( m_default );
  328. scrollBy ( 0, - ( canvas ()->height () ) );
  329. stackMap::Iterator it;
  330. for ( it = m_appletMap.begin (); it != m_appletMap.end (); ++it )
  331. {
  332. ( *it )->hide ();
  333. }
  334. if ( m_appletMap[signal.name () ]->contains ( _activateGroup ) != true )
  335. {
  336. loadGroup ( _dataList, gview );
  337. m_currentView = gview;
  338. m_appletMap[signal.name () ]->setName ( signal.name () );
  339. m_appletMap[signal.name () ]->addGroup ( gview );
  340. m_appletMap[signal.name () ]->raiseByName ( _activateGroup );
  341. int _height =
  342. v_stack->height () + m_scrollbar_top->height () +
  343. m_scrollbar_bot->height ();
  344. if ( _height > this->visibleHeight () )
  345. canvas ()->resize ( v_stack->width (), _height );
  346. else
  347. canvas ()->resize ( v_stack->width (), this->visibleHeight () );
  348. canvas()->update();
  349. repaint();
  350. // return;
  351. }
  352. else
  353. {
  354. m_appletMap[signal.name () ]->raiseByName ( _activateGroup );
  355. m_currentView =
  356. m_appletMap[signal.name () ]->activeViewByName ( _activateGroup );
  357. int _height =
  358. m_appletMap[signal.name () ]->height () + m_scrollbar_top->height () +
  359. m_scrollbar_bot->height ();
  360. if ( _height > this->visibleHeight () )
  361. canvas ()->resize ( m_appletMap[signal.name () ]->width (), _height );
  362. else
  363. canvas ()->resize ( m_appletMap[signal.name () ]->width (),
  364. this->visibleHeight () );
  365. canvas()->update();
  366. repaint();
  367. }
  368. }
  369. void
  370. KbfxPlasmaCanvasView::contentsMouseReleaseEvent ( TQMouseEvent * me )
  371. {
  372. // KbfxPlasmaCanvasGroup *tmp = 0;
  373. TQCanvasItemList l = canvas ()->collisions ( me->pos () );
  374. for ( TQCanvasItemList::Iterator it = l.begin (); it != l.end (); ++it )
  375. {
  376. if ( ( *it )->rtti () == CANVASITEM )
  377. {
  378. KbfxPlasmaCanvasItem *t = ( KbfxPlasmaCanvasItem * ) ( *it );
  379. t->mouseReleaseEvent ( me );
  380. if ( t->type () != KbfxPlasmaCanvasItem::SEPARATOR )
  381. emit clicked ();
  382. }
  383. }
  384. }
  385. void
  386. KbfxPlasmaCanvasView::clearSearch ()
  387. {
  388. searchMap::Iterator it;
  389. for ( it = m_searchMap.begin (); it != m_searchMap.end (); ++it )
  390. {
  391. if ( it != 0 )
  392. m_searchMap.remove ( it );
  393. }
  394. }
  395. void
  396. KbfxPlasmaCanvasView::search_R ( const TQString name )
  397. {
  398. gstack = new KbfxPlasmaCanvasStack ();
  399. KbfxPlasmaCanvasGroup *visualGroup = new KbfxPlasmaCanvasGroup ();
  400. KbfxPlasmaCanvasGroupView *glist = new KbfxPlasmaCanvasGroupView ();
  401. KbfxPlasmaCanvasItem *box = 0;
  402. KbfxPlasmaCanvasItemWrapper *boxwrapper = 0;
  403. glist->setName ( "Search Result" );
  404. this->setCanvas ( m_search );
  405. TQPixmap * pixmap = KbfxPlasmaPixmapProvider::pixmap ( "middleboxbg" );
  406. if ( pixmap != NULL )
  407. {
  408. TQImage img = pixmap->convertToImage();
  409. img = img.smoothScale ( this->width(),pixmap->height(),TQ_ScaleFree );
  410. pixmap->convertFromImage ( img );
  411. this->canvas ()->setBackgroundPixmap ( *pixmap );
  412. }
  413. this->setCursor ( TQCursor ( TQt::BusyCursor ) );
  414. clearAll ();
  415. dataStack::Iterator sit;
  416. for ( sit = m_dataStack.begin (); sit != m_dataStack.end (); ++sit )
  417. {
  418. if ( sit == NULL )
  419. continue;
  420. visualGroup = new KbfxPlasmaCanvasGroup ();
  421. KbfxDataGroup *appGroup =
  422. KbfxPlasmaPluginLoader::search ( sit.key (), name );
  423. boxwrapper = new KbfxPlasmaCanvasItemWrapper ( m_search );
  424. box =
  425. ( KbfxPlasmaCanvasItem * ) boxwrapper->
  426. item ( KbfxPlasmaCanvasItem::SEPARATOR );
  427. if ( appGroup != NULL )
  428. box->setLabelText ( "Hits for " + sit.key () );
  429. if ( appGroup == NULL )
  430. {
  431. tqDebug ( "Null Search Group" );
  432. continue;
  433. }
  434. if ( appGroup->count () <= 0 )
  435. continue;
  436. if ( visualGroup->addItem ( box ) == false )
  437. {}
  438. KbfxDataGroup::Data dat = appGroup->getData ();
  439. KbfxDataGroup::Data::Iterator it;
  440. KbfxDataSource * firstItem = appGroup->itemAt(0);
  441. boxwrapper = new KbfxPlasmaCanvasItemWrapper ( m_search );
  442. m_exeCandidate =
  443. ( KbfxPlasmaCanvasItem * ) boxwrapper->
  444. item ( KbfxPlasmaCanvasItem::EXECUTABLE );
  445. m_exeCandidate->setSource ( *firstItem );
  446. for ( it = dat.begin(); it != dat.end (); it++ )
  447. {
  448. tqApp->processEvents ();
  449. boxwrapper = new KbfxPlasmaCanvasItemWrapper ( m_search );
  450. box =
  451. ( KbfxPlasmaCanvasItem * ) boxwrapper->
  452. item ( KbfxPlasmaCanvasItem::EXECUTABLE );
  453. box->setSource ( * ( it.data () ) );
  454. if ( visualGroup->addItem ( box ) == true )
  455. {
  456. }
  457. }
  458. visualGroup->move ( 0, m_scrollbar_top->height () );
  459. glist->addGroup ( visualGroup );
  460. }
  461. gstack->addGroup ( glist );
  462. gstack->raise ( 0 );
  463. if ( gstack->height() < this->visibleHeight() )
  464. m_search->resize ( gstack->width(),this->visibleHeight() );
  465. else
  466. m_search->resize ( gstack->width (),
  467. gstack->height () + m_scrollbar_top->height () +
  468. m_scrollbar_bot->height () );
  469. m_search->update ();
  470. this->setCursor ( TQCursor ( TQt::ArrowCursor ) );
  471. }
  472. void
  473. KbfxPlasmaCanvasView::search ( const TQString & str )
  474. {
  475. if ( str.isEmpty () )
  476. return;
  477. if ( str == "" )
  478. return;
  479. search_R ( str );
  480. }
  481. void
  482. KbfxPlasmaCanvasView::addStack ( KbfxPlasmaCanvasStack * stak, TQString name )
  483. {
  484. if ( m_appletMap.contains ( name ) )
  485. {
  486. return;
  487. }
  488. m_appletMap[name] = stak;
  489. }
  490. void
  491. KbfxPlasmaCanvasView::addStack ( KbfxDataStack * stak, TQString name )
  492. {
  493. if ( m_dataStack.contains ( name ) )
  494. {
  495. return;
  496. }
  497. m_dataStack[name] = stak;
  498. addStack ( new KbfxPlasmaCanvasStack (), name );
  499. }
  500. void
  501. KbfxPlasmaCanvasView::loadGroup ( KbfxDataGroupList * src,
  502. KbfxPlasmaCanvasGroupView * dest )
  503. {
  504. if ( src == 0 )
  505. return ;
  506. KbfxDataGroupList::GroupList list = src->getGroup ();
  507. dest->setName ( src->name () );
  508. KbfxPlasmaCanvasItem *box = 0;
  509. KbfxPlasmaCanvasItemWrapper *boxwrapper = 0;
  510. KbfxPlasmaCanvasGroup *appGroup = 0; //new KbfxPlasmaCanvasGroup ();
  511. TQString l_lastAdd = "";
  512. for ( int i = 0; i < src->count (); i++ )
  513. {
  514. if ( list.at ( i ) != 0 )
  515. {
  516. if ( list.at ( i )->count () <= 0 )
  517. continue;
  518. appGroup = new KbfxPlasmaCanvasGroup ();
  519. boxwrapper = new KbfxPlasmaCanvasItemWrapper ( m_default );
  520. box =
  521. ( KbfxPlasmaCanvasItem * ) boxwrapper->
  522. item ( KbfxPlasmaCanvasItem::SEPARATOR );
  523. if ( list.at ( i )->name ().compare ( l_lastAdd ) == 0 )
  524. {
  525. box->setLabelText ( src->name () );
  526. }
  527. else
  528. {
  529. l_lastAdd = list.at ( i )->name () ;
  530. box->setLabelText ( list.at ( i )->name () );
  531. }
  532. appGroup->addItem ( box );
  533. KbfxDataGroup::Data::Iterator git;
  534. KbfxDataGroup::Data data;
  535. data = list.at ( i )->getData ();
  536. for ( git = data.begin (); git != data.end (); git++ )
  537. {
  538. boxwrapper = new KbfxPlasmaCanvasItemWrapper ( m_default );
  539. box =
  540. ( KbfxPlasmaCanvasItem * ) boxwrapper->
  541. item ( KbfxPlasmaCanvasItem::EXECUTABLE );
  542. box->setSource ( * ( git.data () ) );
  543. appGroup->addItem ( box );
  544. }
  545. if ( appGroup->count () > 0 )
  546. {
  547. dest->addGroup ( appGroup );
  548. appGroup->move ( 0, m_scrollbar_top->height () );
  549. }
  550. }
  551. }
  552. if ( ConfigInit ().m_startHidden )
  553. {
  554. dest->foldGroupAll ();
  555. }
  556. else
  557. {
  558. dest->unfoldGroupAll ();
  559. }
  560. }
  561. void
  562. KbfxPlasmaCanvasView::contentsDragLeaveEvent ( TQDragEnterEvent * event )
  563. {
  564. event->accept ( true );
  565. }
  566. void
  567. KbfxPlasmaCanvasView::contentsDragEnterEvent ( TQDragEnterEvent * event )
  568. {
  569. event->accept ( true );
  570. }
  571. #include "kbfxplasmacanvasview.moc"