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.

402 lines
8.5KB

  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 "kbfxplasmacanvasitem.h"
  22. KbfxPlasmaCanvasItem::KbfxPlasmaCanvasItem ( TQCanvasPixmapArray * a , TQCanvas * canvas ) :KbfxPlasmaCanvasAbstractItem ( a, canvas )
  23. {
  24. this->setItemMode ( 1 );
  25. setAnimated ( false );
  26. m_desktopFile = 0L;
  27. m_isCurrent = false;
  28. // m_pixmapArray = a;
  29. TQPixmap * _img = this->image ( 0 );
  30. m_commentText ="No Comment Set By Plugin";
  31. m_height =_img->height();
  32. m_width =_img->width();
  33. connect ( this,TQT_SIGNAL ( clicked() ),this,TQT_SLOT ( exec() ) );
  34. m_type = OTHER;
  35. // ConfigInit().read();
  36. m_noComments = ConfigInit().m_noComments;
  37. m_source = 0;
  38. m_margin = ConfigInit().m_commentMargine;
  39. m_lineColor = ConfigInit().m_lineColor;
  40. m_iconSize = ConfigInit().m_iconSize;
  41. m_commentColor = ConfigInit().m_fontAppCommentColor;
  42. m_commentFont = ConfigInit().m_fontAppCommentFont;
  43. m_fontAppNameColor = ConfigInit().m_fontAppNameColor;
  44. m_fontAppNameFont = ConfigInit().m_fontAppNameFont;
  45. m_sepNameFont = ConfigInit().m_sepNameFont;
  46. m_boldFonts = ConfigInit().m_fontHoverBold;
  47. }
  48. KbfxPlasmaCanvasItem::~KbfxPlasmaCanvasItem()
  49. {
  50. setCanvas ( 0L );
  51. }
  52. /*
  53. TQCanvasPixmapArray * KbfxPlasmaCanvasItem::getPixmapArray()
  54. {
  55. return m_pixmapArray;
  56. }
  57. */
  58. void
  59. KbfxPlasmaCanvasItem::setLabelText ( TQString str )
  60. {
  61. TQFont * _font = new TQFont ( m_fontAppNameFont );
  62. TQFontMetrics fm ( *_font );
  63. int _commentWidth = fm.width ( str+"..." );
  64. int _strLen = str.length();
  65. if ( _commentWidth > ( this->width() - m_margin ) )
  66. {
  67. for ( int i = 0; i < _strLen /*&& ( fm.width(str+"...") < width()-40 )*/ ;i++ )
  68. {
  69. str.truncate ( str.length()-2 );
  70. if ( fm.width ( str+"..." ) < width() - m_margin )
  71. break;
  72. }
  73. str+="...";
  74. }
  75. m_labelText = str ;
  76. }
  77. bool
  78. KbfxPlasmaCanvasItem::lookup ( TQString str )
  79. {
  80. if ( str.isNull() )
  81. return false;
  82. if ( m_type == SEPARATOR || m_type == INDEX )
  83. return false;
  84. if ( m_labelText.contains ( str,false ) > 0 )
  85. return true;
  86. if ( m_commentText.contains ( str,false ) > 0 )
  87. return true;
  88. if ( m_exec.contains ( str,false ) >0 )
  89. return true;
  90. return false;
  91. }
  92. void
  93. KbfxPlasmaCanvasItem::setExec ( TQString desktopfile )
  94. {
  95. m_desktopFile = new KDesktopFile ( desktopfile );
  96. m_desktopFilePath = desktopfile;
  97. bool check = m_desktopFile->isAuthorizedDesktopFile ( desktopfile );
  98. setLabelText ( m_desktopFile->readName() );
  99. setComment ( m_desktopFile->readComment() );
  100. setIconPath ( m_desktopFile->readIcon() );
  101. if ( check == 0 )
  102. {
  103. m_restricted = true;
  104. m_error = "Adiministrator Blocked";
  105. delete m_desktopFile;
  106. return ;
  107. }
  108. delete m_desktopFile;
  109. }
  110. void
  111. KbfxPlasmaCanvasItem::setIconPath ( TQString str )
  112. {
  113. TDEIconLoader *iconload = TDEGlobal::iconLoader ();
  114. m_iconPath = iconload->iconPath ( str, TDEIcon::Desktop, false );
  115. // m_icon.load(m_iconPath);
  116. TQImage _img ( m_iconPath );
  117. if ( _img.height() > 128 )
  118. {
  119. _img = _img.smoothScale ( 32,32 );
  120. }
  121. m_icon.convertFromImage ( _img );
  122. }
  123. void
  124. KbfxPlasmaCanvasItem::setIcon ( TQPixmap pixmap )
  125. {
  126. m_icon = pixmap;
  127. }
  128. void
  129. KbfxPlasmaCanvasItem::setSource ( KbfxDataSource src )
  130. {
  131. m_source = new KbfxDataSource();
  132. *m_source = src;
  133. if ( src.type() == KbfxDataSource::DESKTOP )
  134. {
  135. this->setExec ( src.desktopFile () );
  136. }
  137. else
  138. {
  139. setLabelText ( src.name() );
  140. setName ( src.name().ascii() );
  141. setComment ( src.comment() );
  142. setIconPath ( src.icon() );
  143. }
  144. }
  145. void
  146. KbfxPlasmaCanvasItem::hideit ()
  147. {
  148. m_hidden = 0;
  149. this->hide ();
  150. }
  151. void
  152. KbfxPlasmaCanvasItem::setType ( Type t )
  153. {
  154. m_type = t;
  155. }
  156. bool KbfxPlasmaCanvasItem::itemMode ()
  157. {
  158. return m_skined;
  159. }
  160. void
  161. KbfxPlasmaCanvasItem::setItemMode ( bool mode )
  162. {
  163. m_skined = mode;
  164. }
  165. void
  166. KbfxPlasmaCanvasItem::drawBackDrop ( TQPainter * pe,TQRect & r )
  167. {
  168. r = r ;
  169. if ( pe == NULL )
  170. return ;
  171. }
  172. /*
  173. void
  174. KbfxPlasmaCanvasItem::drawText(TQPainter * pe,TQString str)
  175. {
  176. }
  177. */
  178. void
  179. KbfxPlasmaCanvasItem::draw ( TQPainter & pe )
  180. {
  181. drawContent ( &pe );
  182. }
  183. TQPixmap
  184. KbfxPlasmaCanvasItem::dragPixmap()
  185. {
  186. double _x = x();
  187. double _y = y();
  188. setX ( 0.0 );
  189. setY ( 0.0 );
  190. TQPixmap dragpixmap ( this->width(),this->height() );
  191. dragpixmap.fill ( TQColor ( 255,255,255 ) );
  192. TQPainter p;
  193. p.begin ( &dragpixmap );
  194. this->drawContent ( &p );
  195. p.end();
  196. setX ( _x );
  197. setY ( _y );
  198. return dragpixmap;
  199. }
  200. void
  201. KbfxPlasmaCanvasItem::drawContent ( TQPainter * pe )
  202. {
  203. if ( m_isCurrent )
  204. {
  205. this->setFrame ( 1 );
  206. }
  207. else
  208. {
  209. this->setFrame ( 0 );
  210. }
  211. //int _currentFrame =
  212. TQCanvasPixmap *cp = this->image ( this->frame () );
  213. m_height = cp->height();
  214. m_width = cp->width();
  215. //Draw the Background
  216. if ( m_skined == true )
  217. {
  218. pe->drawPixmap ( this->boundingRect (), *cp );
  219. }
  220. else
  221. {
  222. pe->setPen ( TQColor ( 91,178,62 ) );
  223. pe->drawRect ( this->boundingRect() );
  224. pe->setPen ( TQColor ( 0,10,0 ) );
  225. }
  226. /**
  227. We will split the Item box into two
  228. **/
  229. TQRect tmp ( 0,0,0,0 );
  230. //TODO: Should be read from kbfxconfig.cpp
  231. //
  232. TQRect * rectTop = new TQRect ( m_margin, ( int ) y(), cp->width() - m_margin, cp->height() /2 );
  233. TQRect * rectBot = new TQRect ( m_margin, ( int ) y() + rectTop->height(), cp->width() - m_margin, cp->height() /2 );
  234. ///Draw Icon
  235. //TODO::remover hard coded icon size : read from config
  236. if ( !m_isCurrent )
  237. pe->drawPixmap ( TQRect ( ( m_margin-m_iconSize ) /2, ( int ) y() + ( cp->height()-m_iconSize ) /2,m_iconSize,m_iconSize ),m_icon );
  238. else
  239. pe->drawPixmap ( TQRect ( ( m_margin-m_iconSize ) /2, ( int ) y() + ( cp->height()-m_iconSize ) /2,m_iconSize+2,m_iconSize+2 ),m_icon );
  240. //End of DrawIcon
  241. TQFont * _font = new TQFont ( m_fontAppNameFont );
  242. TQFont * _font_comment = new TQFont ( m_commentFont );
  243. TQFont * _font_separator = new TQFont ( m_sepNameFont );
  244. pe->setFont ( *_font );
  245. if ( this->type() == EXECUTABLE )
  246. {
  247. pe->setPen ( m_fontAppNameColor );
  248. if ( m_noComments == false )
  249. {
  250. if ( m_isCurrent && m_boldFonts == true ) { _font->setBold ( true );pe->setFont ( *_font ); }
  251. pe->drawText ( *rectTop, TQt::AlignLeft| TQt::AlignBottom ,TQString ( m_labelText ), -1, &tmp );
  252. pe->setPen ( m_lineColor );
  253. pe->drawLine ( m_margin, ( int ) y() + rectTop->height(), ( int ) cp->width() - 2, ( int ) y() + rectTop->height() );
  254. pe->setFont ( *_font_comment );
  255. pe->setPen ( m_commentColor );
  256. pe->drawText ( *rectBot, TQt::AlignLeft| TQt::AlignTop ,TQString ( m_commentText ), -1, &tmp );
  257. }
  258. else
  259. {
  260. pe->drawText ( TQRect ( m_margin+1, ( int ) y()+1,cp->width(),cp->height() ), TQt::AlignLeft | TQt::AlignVCenter ,TQString ( m_labelText ), -1, &tmp );
  261. // pe->drawText ( TQRect ( m_margin, ( int ) y(),cp->width(),cp->height() ), TQt::AlignLeft | TQt::AlignVCenter ,TQString ( m_labelText ), -1, &tmp );
  262. }
  263. }
  264. else
  265. {
  266. if ( this->type() == SEPARATOR )
  267. {
  268. pe->setFont ( *_font_separator );
  269. pe->setPen ( ConfigInit().m_sepNameColor );
  270. }
  271. else
  272. {
  273. pe->setPen ( m_fontAppNameColor );
  274. }
  275. pe->drawText ( TQRect ( ( int ) x(), ( int ) y(),cp->width(),cp->height() ), TQt::AlignCenter ,TQString ( m_labelText ), -1, &tmp );
  276. }
  277. ///Draw the line
  278. delete rectTop;
  279. delete rectBot;
  280. delete _font;
  281. delete _font_comment;
  282. delete _font_separator;
  283. }
  284. void::KbfxPlasmaCanvasItem::setComment ( TQString str )
  285. {
  286. TQFont * _font = new TQFont ( m_commentFont );
  287. TQFontMetrics fm ( *_font );
  288. int _commentWidth = fm.width ( str+"..." );
  289. int _strLen = str.length();
  290. if ( _commentWidth > ( this->width()-m_margin ) )
  291. {
  292. for ( int i = 0; i < _strLen /*&& ( fm.width(str+"...") < width()-40 )*/ ;i++ )
  293. {
  294. str.truncate ( str.length()-2 );
  295. if ( fm.width ( str+"..." ) < width()-m_margin )
  296. break;
  297. }
  298. str+="...";
  299. }
  300. m_commentText = str;
  301. }
  302. void
  303. KbfxPlasmaCanvasItem::advance ( int phase )
  304. {
  305. if ( phase == 0 )
  306. {
  307. setXVelocity ( 0 );
  308. setYVelocity ( yVelocity () * 1 - 0.001 );
  309. }
  310. else
  311. {
  312. move ( x () + xVelocity (), y () + yVelocity () );
  313. }
  314. }
  315. #include "kbfxplasmacanvasitem.moc"