TDE base libraries and programs
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.

addapplet.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*****************************************************************
  2. Copyright (c) 2005 Marc Cramdal
  3. Copyright (c) 2005 Aaron Seigo <aseigo@kde.org>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  16. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. ******************************************************************/
  19. #include <tqapplication.h>
  20. #include <tqcombobox.h>
  21. #include <tqdir.h>
  22. #include <tqframe.h>
  23. #include <tqlabel.h>
  24. #include <tqlayout.h>
  25. #include <tqlineedit.h>
  26. #include <tqpalette.h>
  27. #include <tqscrollview.h>
  28. #include <tqtimer.h>
  29. #include <tqsizepolicy.h>
  30. #include <kiconloader.h>
  31. #include <kdebug.h>
  32. #include <tdeglobalsettings.h>
  33. #include <kpushbutton.h>
  34. #include <kstandarddirs.h>
  35. #include <kstdguiitem.h>
  36. #include <paneldrag.h>
  37. #include "addapplet.h"
  38. #include "addappletvisualfeedback.h"
  39. #include "appletwidget.h"
  40. #include "appletview.h"
  41. #include "container_applet.h"
  42. #include "container_extension.h"
  43. #include "containerarea.h"
  44. #include "kicker.h"
  45. #include "kickerSettings.h"
  46. #include "menuinfo.h"
  47. #include "pluginmanager.h"
  48. AppletWidget::AppletWidget(const AppletInfo& info, bool odd, TQWidget *parent)
  49. : AppletItem(parent),
  50. m_appletInfo(info),
  51. m_odd(odd),
  52. m_selected(false)
  53. {
  54. setFocusPolicy(TQ_StrongFocus);
  55. setSelected(m_selected);
  56. itemTitle->setText("<h3>" + info.name() + "</h3>");
  57. itemTitle->installEventFilter(this);
  58. if (info.comment() != info.name())
  59. {
  60. itemDescription->setText(info.comment());
  61. }
  62. itemDescription->installEventFilter(this);
  63. TDEIconLoader * ldr = TDEGlobal::iconLoader();
  64. TQPixmap icon = ldr->loadIcon(info.icon(), TDEIcon::Panel, TDEIcon::SizeLarge);
  65. itemPixmap->setPixmap(icon);
  66. itemPixmap->installEventFilter(this);
  67. }
  68. bool AppletWidget::eventFilter(TQObject*, TQEvent* e)
  69. {
  70. if (e->type() == TQEvent::MouseButtonPress)
  71. {
  72. TQMouseEvent* me = TQT_TQMOUSEEVENT(e);
  73. if (me->button() & Qt::LeftButton)
  74. {
  75. m_dragStart = me->pos();
  76. }
  77. }
  78. else if (m_dragStart.isNull())
  79. {
  80. return false;
  81. }
  82. if (e->type() == TQEvent::MouseMove)
  83. {
  84. TQMouseEvent* me = TQT_TQMOUSEEVENT(e);
  85. if ((me->pos() - m_dragStart).manhattanLength() >
  86. TDEGlobalSettings::dndEventDelay())
  87. {
  88. AppletInfoDrag* drag = new AppletInfoDrag(m_appletInfo, this);
  89. if (itemPixmap->pixmap())
  90. {
  91. drag->setPixmap(*itemPixmap->pixmap());
  92. }
  93. drag->dragCopy();
  94. return true;
  95. }
  96. }
  97. else if (e->type() == TQEvent::MouseButtonRelease)
  98. {
  99. m_dragStart = TQPoint();
  100. }
  101. return false;
  102. }
  103. void AppletWidget::keyPressEvent(TQKeyEvent *e)
  104. {
  105. if (e->key() == Qt::Key_Enter ||
  106. e->key() == Qt::Key_Return)
  107. {
  108. emit doubleClicked(this);
  109. }
  110. else if (e->key() == Qt::Key_Up)
  111. {
  112. TQKeyEvent fakedKeyPress(TQEvent::KeyPress, TQt::Key_BackTab, 0, 0);
  113. TQKeyEvent fakedKeyRelease(TQEvent::KeyRelease, Key_BackTab, 0, 0);
  114. TQApplication::sendEvent(this, &fakedKeyPress);
  115. TQApplication::sendEvent(this, &fakedKeyRelease);
  116. }
  117. else if (e->key() == Qt::Key_Down)
  118. {
  119. TQKeyEvent fakedKeyPress(TQEvent::KeyPress, Qt::Key_Tab, 0, 0);
  120. TQKeyEvent fakedKeyRelease(TQEvent::KeyRelease, Key_Escape, 0, 0);
  121. TQApplication::sendEvent(this, &fakedKeyPress);
  122. TQApplication::sendEvent(this, &fakedKeyRelease);
  123. }
  124. else
  125. {
  126. AppletItem::keyPressEvent(e);
  127. }
  128. }
  129. void AppletWidget::mousePressEvent(TQMouseEvent *e)
  130. {
  131. if (e->button() == Qt::LeftButton)
  132. {
  133. emit clicked(this);
  134. m_dragStart = e->pos();
  135. }
  136. setFocus();
  137. TQWidget::mousePressEvent(e);
  138. }
  139. void AppletWidget::mouseMoveEvent(TQMouseEvent *e)
  140. {
  141. if (e->button() == Qt::LeftButton &&
  142. !m_dragStart.isNull() &&
  143. (e->pos() - m_dragStart).manhattanLength() >
  144. TDEGlobalSettings::dndEventDelay())
  145. {
  146. AppletInfoDrag* drag = new AppletInfoDrag(m_appletInfo, this);
  147. if (itemPixmap->pixmap())
  148. {
  149. drag->setPixmap(*itemPixmap->pixmap());
  150. }
  151. drag->dragCopy();
  152. }
  153. }
  154. void AppletWidget::mouseReleaseEvent(TQMouseEvent *e)
  155. {
  156. m_dragStart = TQPoint();
  157. TQWidget::mouseReleaseEvent(e);
  158. }
  159. void AppletWidget::mouseDoubleClickEvent(TQMouseEvent *e)
  160. {
  161. if (!e->button() == Qt::LeftButton)
  162. {
  163. AppletItem::mouseDoubleClickEvent(e);
  164. return;
  165. }
  166. emit doubleClicked(this);
  167. }
  168. void AppletWidget::setSelected(bool selected)
  169. {
  170. m_selected = selected;
  171. // for now just used to switch colours around =)
  172. if (m_selected)
  173. {
  174. setPaletteBackgroundColor(TDEGlobalSettings::highlightColor());
  175. setPaletteForegroundColor(TDEGlobalSettings::highlightedTextColor());
  176. }
  177. else if (m_odd)
  178. {
  179. setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
  180. setPaletteForegroundColor(TDEGlobalSettings::textColor());
  181. }
  182. else
  183. {
  184. setPaletteBackgroundColor(TDEGlobalSettings::alternateBackgroundColor());
  185. setPaletteForegroundColor(TDEGlobalSettings::textColor());
  186. }
  187. }
  188. void AppletWidget::setOdd(bool odd)
  189. {
  190. m_odd = odd;
  191. setSelected(m_selected);
  192. }
  193. void AppletWidget::focusInEvent(TQFocusEvent*)
  194. {
  195. emit clicked(this);
  196. }
  197. AddAppletDialog::AddAppletDialog(ContainerArea* cArea,
  198. TQWidget* parent,
  199. const char* name)
  200. : KDialogBase(parent, name, false, i18n("Add Applet"), 0),
  201. m_selectedApplet(0),
  202. m_containerArea(cArea),
  203. m_insertionPoint(Kicker::the()->insertionPoint()),
  204. m_closing(false),
  205. m_searchDelay(new TQTimer(this))
  206. {
  207. m_mainWidget = new AppletView(this, "AddAppletDialog::m_mainWidget");
  208. m_mainWidget->appletScrollView->setResizePolicy(TQScrollView::Manual);
  209. m_mainWidget->appletScrollView->setHScrollBarMode(TQScrollView::AlwaysOff);
  210. m_mainWidget->appletScrollView->viewport()->setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
  211. setMainWidget(m_mainWidget);
  212. resize(configDialogSize("AddAppletDialog Settings"));
  213. centerOnScreen(this);
  214. KGuiItem addGuiItem = KStdGuiItem::add();
  215. addGuiItem.setText(m_mainWidget->appletInstall->text());
  216. m_mainWidget->appletInstall->setEnabled(false);
  217. m_mainWidget->appletInstall->setGuiItem(addGuiItem);
  218. m_mainWidget->closeButton->setGuiItem(KStdGuiItem::close());
  219. connect(m_mainWidget->appletSearch, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(delayedSearch()));
  220. connect(m_searchDelay, TQT_SIGNAL(timeout()), this, TQT_SLOT(search()));
  221. connect(m_mainWidget->appletFilter, TQT_SIGNAL(activated(int)), this, TQT_SLOT(filter(int)));
  222. connect(m_mainWidget->appletInstall, TQT_SIGNAL(clicked()), this, TQT_SLOT(addCurrentApplet()));
  223. connect(m_mainWidget->closeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(close()));
  224. m_selectedType = AppletInfo::Undefined;
  225. m_appletBox = 0;
  226. TQTimer::singleShot(0, this, TQT_SLOT(populateApplets()));
  227. }
  228. void AddAppletDialog::updateInsertionPoint()
  229. {
  230. m_insertionPoint = Kicker::the()->insertionPoint();
  231. }
  232. void AddAppletDialog::closeEvent(TQCloseEvent* e)
  233. {
  234. m_closing = true;
  235. saveDialogSize("AddAppletDialog Settings");
  236. KDialogBase::closeEvent(e);
  237. }
  238. void AddAppletDialog::resizeAppletView()
  239. {
  240. int w, h;
  241. TQScrollView *v = m_mainWidget->appletScrollView;
  242. if (m_closing)
  243. return;
  244. for (int i = 0; i < 3; i++)
  245. {
  246. m_appletBox->layout()->activate();
  247. w = v->visibleWidth();
  248. h = m_appletBox->layout()->minimumSize().height();
  249. v->resizeContents(w, TQMAX(h, v->visibleHeight()));
  250. if (w == m_appletBox->width() && h == m_appletBox->height())
  251. break;
  252. m_appletBox->resize(w, h);
  253. v->updateScrollBars();
  254. }
  255. }
  256. bool AddAppletDialog::eventFilter(TQObject *o, TQEvent *e)
  257. {
  258. if (e->type() == TQEvent::Resize)
  259. TQTimer::singleShot(0, this, TQT_SLOT(resizeAppletView()));
  260. return TQT_TQOBJECT(this)->TQObject::eventFilter(o, e);
  261. }
  262. void AddAppletDialog::populateApplets()
  263. {
  264. m_appletBox = new TQWidget(m_mainWidget->appletScrollView->viewport());
  265. m_appletBox->setPaletteBackgroundColor(TDEGlobalSettings::baseColor());
  266. m_mainWidget->appletScrollView->addChild(m_appletBox, 0, 0);
  267. m_appletBox->show();
  268. TQVBoxLayout* layout = new TQVBoxLayout(m_appletBox);
  269. layout->setMargin(0);
  270. m_mainWidget->appletScrollView->installEventFilter(this);
  271. /* Three steps
  272. * - First we load the applets
  273. * - We load the special buttons
  274. * - Then we begin to populate the scrollview with the AppletWidget(s)
  275. */
  276. AppletInfo::List appletInfoList;
  277. // Loading applets
  278. appletInfoList = PluginManager::applets(false, &appletInfoList);
  279. // Loading built in buttons
  280. appletInfoList = PluginManager::builtinButtons(false, &appletInfoList);
  281. // Loading special buttons
  282. appletInfoList = PluginManager::specialButtons(false, &appletInfoList);
  283. qHeapSort(appletInfoList);
  284. int i = 0;
  285. bool odd = true;
  286. TQWidget* prevTabWidget = m_mainWidget->appletFilter;
  287. for (AppletInfo::List::iterator it = appletInfoList.begin();
  288. !m_closing && it != appletInfoList.end();
  289. ++i)
  290. {
  291. if ((*it).isHidden() || (*it).name().isEmpty() ||
  292. ((*it).isUniqueApplet() &&
  293. PluginManager::the()->hasInstance(*it)))
  294. {
  295. it = appletInfoList.erase(it);
  296. --i;
  297. continue;
  298. }
  299. AppletWidget *itemWidget = new AppletWidget(*it, odd, m_appletBox);
  300. if (m_mainWidget->appletSearch->text().isEmpty() ||
  301. appletMatchesSearch(itemWidget, m_mainWidget->appletSearch->text()))
  302. {
  303. itemWidget->show();
  304. odd = !odd;
  305. }
  306. else
  307. {
  308. itemWidget->hide();
  309. }
  310. layout->insertWidget(i, itemWidget);
  311. m_appletWidgetList.append(itemWidget);
  312. setTabOrder(prevTabWidget, itemWidget);
  313. prevTabWidget = itemWidget;
  314. connect(itemWidget, TQT_SIGNAL(clicked(AppletWidget*)),
  315. this, TQT_SLOT(selectApplet(AppletWidget*)));
  316. connect(itemWidget, TQT_SIGNAL(doubleClicked(AppletWidget*)),
  317. this, TQT_SLOT(addApplet(AppletWidget*)));
  318. if (m_closing)
  319. {
  320. return;
  321. }
  322. ++it;
  323. }
  324. resizeAppletView();
  325. m_mainWidget->closeButton->setEnabled(true);
  326. }
  327. void AddAppletDialog::selectApplet(AppletWidget *applet)
  328. {
  329. m_mainWidget->appletInstall->setEnabled(true);
  330. if (m_selectedApplet)
  331. {
  332. m_selectedApplet->setSelected(false);
  333. }
  334. m_selectedApplet = applet;
  335. if (m_selectedApplet)
  336. {
  337. m_selectedApplet->setSelected(true);
  338. }
  339. }
  340. void AddAppletDialog::addCurrentApplet()
  341. {
  342. addApplet(m_selectedApplet);
  343. }
  344. void AddAppletDialog::addApplet(AppletWidget* applet)
  345. {
  346. if (!applet)
  347. {
  348. return;
  349. }
  350. TQPoint prevInsertionPoint = Kicker::the()->insertionPoint();
  351. Kicker::the()->setInsertionPoint(m_insertionPoint);
  352. const TQWidget* appletContainer = 0;
  353. if (applet->info().type() == AppletInfo::Applet)
  354. {
  355. appletContainer = m_containerArea->addApplet(applet->info());
  356. if (applet->info().isUniqueApplet() &&
  357. PluginManager::the()->hasInstance(applet->info()))
  358. {
  359. applet->hide();
  360. // reset the odd/even colouring from this item on down in the list
  361. bool odd = applet->odd();
  362. AppletWidget::List::const_iterator it = m_appletWidgetList.find(applet);
  363. for (; it != m_appletWidgetList.constEnd(); ++it)
  364. {
  365. if ((*it)->isHidden())
  366. {
  367. continue;
  368. }
  369. (*it)->setOdd(odd);
  370. odd = !odd;
  371. }
  372. }
  373. }
  374. else if (applet->info().type() & AppletInfo::Button)
  375. {
  376. appletContainer = m_containerArea->addButton(applet->info());
  377. }
  378. if (appletContainer)
  379. {
  380. ExtensionContainer* ec =
  381. dynamic_cast<ExtensionContainer*>(m_containerArea->topLevelWidget());
  382. if (ec)
  383. {
  384. // unhide the panel and keep it unhidden for at least the time the
  385. // helper tip will be there
  386. ec->unhideIfHidden(KickerSettings::mouseOversSpeed() + 2500);
  387. }
  388. new AddAppletVisualFeedback(applet, appletContainer,
  389. m_containerArea->popupDirection());
  390. }
  391. Kicker::the()->setInsertionPoint(prevInsertionPoint);
  392. }
  393. bool AddAppletDialog::appletMatchesSearch(const AppletWidget* w,
  394. const TQString& s)
  395. {
  396. if (w->info().type() == AppletInfo::Applet &&
  397. w->info().isUniqueApplet() &&
  398. PluginManager::the()->hasInstance(w->info()))
  399. {
  400. return false;
  401. }
  402. return (m_selectedType == AppletInfo::Undefined ||
  403. w->info().type() & m_selectedType) &&
  404. (w->info().name().contains(s, false) ||
  405. w->info().comment().contains(s, false));
  406. }
  407. void AddAppletDialog::delayedSearch()
  408. {
  409. if (!m_searchDelay->isActive())
  410. {
  411. m_searchDelay->start(300, true);
  412. }
  413. }
  414. void AddAppletDialog::search()
  415. {
  416. TQString s = m_mainWidget->appletSearch->text();
  417. bool odd = true;
  418. AppletWidget::List::const_iterator it = m_appletWidgetList.constBegin();
  419. AppletWidget::List::const_iterator itEnd = m_appletWidgetList.constEnd();
  420. for (; it != itEnd; ++it)
  421. {
  422. AppletWidget* w = *it;
  423. if (appletMatchesSearch(w, s))
  424. {
  425. w->setOdd(odd);
  426. w->show();
  427. odd = !odd;
  428. }
  429. else
  430. {
  431. w->hide();
  432. }
  433. }
  434. TQTimer::singleShot(0, this, TQT_SLOT(resizeAppletView()));
  435. }
  436. void AddAppletDialog::filter(int i)
  437. {
  438. m_selectedType = AppletInfo::Undefined;
  439. if (i == 1)
  440. {
  441. m_selectedType = AppletInfo::Applet;
  442. }
  443. else if (i == 2)
  444. {
  445. m_selectedType = AppletInfo::Button;
  446. }
  447. search();
  448. }
  449. #include "addapplet.moc"
  450. #include "appletwidget.moc"