Bibletime – a bible study tool
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

872 linhas
26KB

  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2006 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. //BibleTime includes
  10. #include "cmainindex.h"
  11. #include "cindexitem.h"
  12. #include "backend/creferencemanager.h"
  13. #include "backend/cswordmoduleinfo.h"
  14. #include "frontend/searchdialog/csearchdialog.h"
  15. #include "frontend/cbtconfig.h"
  16. #include "frontend/cdragdropmgr.h"
  17. #include "frontend/cinfodisplay.h"
  18. #include "frontend/cprinter.h"
  19. #include "util/cresmgr.h"
  20. #include "util/scoped_resource.h"
  21. //TQt includes
  22. #include <tqheader.h>
  23. #include <tqlistview.h>
  24. #include <tqdragobject.h>
  25. #include <tqinputdialog.h>
  26. #include <tqregexp.h>
  27. //KDE includes
  28. #include <tdelocale.h>
  29. #include <kstandarddirs.h>
  30. #include <tdepopupmenu.h>
  31. #include <tdemessagebox.h>
  32. #include <tdeglobalsettings.h>
  33. using namespace Printing;
  34. CMainIndex::ToolTip::ToolTip(CMainIndex* parent) : TQToolTip(parent->viewport()), m_mainIndex(parent) {}
  35. void CMainIndex::ToolTip::maybeTip(const TQPoint& p) {
  36. CItemBase* i = dynamic_cast<CItemBase*>(m_mainIndex->itemAt(p));
  37. Q_ASSERT(i);
  38. if ( !i ) {
  39. return;
  40. }
  41. TQRect r = m_mainIndex->itemRect(i);
  42. if (!r.isValid()) {
  43. return;
  44. }
  45. //get type of item and display correct text
  46. const TQString text = i->toolTip();
  47. if (!text.isEmpty()) {
  48. CBookmarkItem* bookmark = dynamic_cast<CBookmarkItem*>(i);
  49. if (bookmark) {
  50. (CPointers::infoDisplay())->setInfo(
  51. InfoDisplay::CInfoDisplay::CrossReference,
  52. bookmark->module()->name() + ":" + bookmark->key()
  53. );
  54. }
  55. else {
  56. CPointers::infoDisplay()->clearInfo();
  57. }
  58. tip(r, text);
  59. }
  60. else {
  61. CPointers::infoDisplay()->clearInfo();
  62. }
  63. }
  64. CMainIndex::CMainIndex(TQWidget *parent) : TDEListView(parent),
  65. m_searchDialog(0), m_toolTip(0), m_itemsMovable(false), m_autoOpenFolder(0), m_autoOpenTimer(this) {
  66. initView();
  67. initConnections();
  68. //initTree() is called in polish()
  69. }
  70. CMainIndex::~CMainIndex() {
  71. saveSettings();
  72. saveBookmarks();
  73. m_toolTip->remove(this);
  74. delete m_toolTip;
  75. }
  76. /** Reimplementation. Adds the given group to the tree. */
  77. void CMainIndex::addGroup(const CItemBase::Type type, const TQString language) {
  78. CTreeFolder *i = 0;
  79. switch (type) {
  80. case CItemBase::BookmarkFolder:
  81. i = new CBookmarkFolder(this);
  82. break;
  83. case CItemBase::GlossaryModuleFolder:
  84. //we have no second language
  85. i = new CGlossaryFolder(this, type, language, TQString());
  86. break;
  87. default:
  88. i = new CTreeFolder(this, type, language);
  89. break;
  90. }
  91. if (i) {
  92. i->init();
  93. if (i->childCount() == 0 && type != CItemBase::BookmarkFolder) {
  94. delete i;
  95. }
  96. }
  97. }
  98. /** Initializes the view. */
  99. void CMainIndex::initView() {
  100. addColumn(TQString());
  101. header()->hide();
  102. m_toolTip = new ToolTip(this);
  103. setTooltipColumn(-1);
  104. //to disable TQt's tooltips
  105. setShowToolTips(false);
  106. setBackgroundMode(PaletteBase);
  107. setFullWidth(true);
  108. setFocusPolicy(TQ_WheelFocus);
  109. setAcceptDrops( true );
  110. setDragEnabled( true );
  111. setDropVisualizer( true );
  112. setDropHighlighter( true );
  113. setAutoOpen(true);
  114. viewport()->setAcceptDrops(true);
  115. setRootIsDecorated(false);
  116. setAllColumnsShowFocus(true);
  117. setItemsMovable(false);
  118. setSelectionModeExt(Extended);
  119. //setup the popup menu
  120. m_popup = new TDEPopupMenu(viewport());
  121. m_popup->insertTitle(i18n("Bookshelf"));
  122. m_actions.newFolder = new TDEAction(i18n("Create a new folder"), CResMgr::mainIndex::newFolder::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(createNewFolder()), TQT_TQOBJECT(this));
  123. m_actions.changeFolder = new TDEAction(i18n("Change this folder"),CResMgr::mainIndex::changeFolder::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(changeFolder()), TQT_TQOBJECT(this));
  124. m_actions.changeBookmark = new TDEAction(i18n("Change bookmark description"),CResMgr::mainIndex::changeBookmark::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(changeBookmark()), TQT_TQOBJECT(this));
  125. m_actions.importBookmarks = new TDEAction(i18n("Import bookmarks"),CResMgr::mainIndex::importBookmarks::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(importBookmarks()), TQT_TQOBJECT(this));
  126. m_actions.exportBookmarks = new TDEAction(i18n("Export bookmarks"),CResMgr::mainIndex::exportBookmarks::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(exportBookmarks()), TQT_TQOBJECT(this));
  127. m_actions.printBookmarks = new TDEAction(i18n("Print bookmarks"),CResMgr::mainIndex::printBookmarks::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(printBookmarks()), TQT_TQOBJECT(this));
  128. m_actions.deleteEntries = new TDEAction(i18n("Remove selected item(s)"),CResMgr::mainIndex::deleteItems::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(deleteEntries()), TQT_TQOBJECT(this));
  129. m_actions.editModuleMenu = new TDEActionMenu(i18n("Edit this work"),CResMgr::mainIndex::editModuleMenu::icon, TQT_TQOBJECT(this));
  130. m_actions.editModuleMenu->setDelayed(false);
  131. m_actions.editModulePlain = new TDEAction(i18n("Plain text"),CResMgr::mainIndex::editModulePlain::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(editModulePlain()), TQT_TQOBJECT(this));
  132. m_actions.editModuleHTML = new TDEAction(i18n("HTML"),CResMgr::mainIndex::editModuleHTML::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(editModuleHTML()), TQT_TQOBJECT(this));
  133. m_actions.searchInModules = new TDEAction(i18n("Search in selected work(s)"),CResMgr::mainIndex::search::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(searchInModules()), TQT_TQOBJECT(this));
  134. m_actions.unlockModule = new TDEAction(i18n("Unlock this work"),CResMgr::mainIndex::unlockModule::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(unlockModule()), TQT_TQOBJECT(this));
  135. m_actions.aboutModule = new TDEAction(i18n("About this work"),CResMgr::mainIndex::aboutModule::icon, 0, TQT_TQOBJECT(this), TQT_SLOT(aboutModule()), TQT_TQOBJECT(this));
  136. //fill the popup menu itself
  137. m_actions.newFolder->plug(m_popup);
  138. m_actions.changeFolder->plug(m_popup);
  139. (new TDEActionSeparator(TQT_TQOBJECT(this)))->plug(m_popup);
  140. m_actions.changeBookmark->plug(m_popup);
  141. m_actions.importBookmarks->plug(m_popup);
  142. m_actions.exportBookmarks->plug(m_popup);
  143. m_actions.printBookmarks->plug(m_popup);
  144. (new TDEActionSeparator(TQT_TQOBJECT(this)))->plug(m_popup);
  145. m_actions.deleteEntries->plug(m_popup);
  146. (new TDEActionSeparator(TQT_TQOBJECT(this)))->plug(m_popup);
  147. m_actions.editModuleMenu->plug(m_popup);
  148. //sub item of edit module menu
  149. m_actions.editModuleMenu->insert(m_actions.editModulePlain);
  150. //sub item of edit module menu
  151. m_actions.editModuleMenu->insert(m_actions.editModuleHTML);
  152. m_actions.searchInModules->plug(m_popup);
  153. m_actions.unlockModule->plug(m_popup);
  154. m_actions.aboutModule->plug(m_popup);
  155. }
  156. /** Initialize the SIGNAL<->TQT_SLOT connections */
  157. void CMainIndex::initConnections() {
  158. connect(this, TQT_SIGNAL(executed(TQListViewItem*)),
  159. TQT_SLOT(slotExecuted(TQListViewItem*)));
  160. connect(this, TQT_SIGNAL(dropped(TQDropEvent*, TQListViewItem*, TQListViewItem*)),
  161. TQT_SLOT(dropped(TQDropEvent*, TQListViewItem*, TQListViewItem*)));
  162. connect(this, TQT_SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)),
  163. TQT_SLOT(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)));
  164. connect(&m_autoOpenTimer, TQT_SIGNAL(timeout()),
  165. this, TQT_SLOT(autoOpenTimeout()));
  166. }
  167. /** Is called when an item was clicked/double clicked. */
  168. void CMainIndex::slotExecuted( TQListViewItem* i ) {
  169. CItemBase* ci = dynamic_cast<CItemBase*>(i);
  170. if (!ci) {
  171. return;
  172. }
  173. if (ci->isFolder()) {
  174. i->setOpen(!i->isOpen());
  175. }
  176. else if (CModuleItem* m = dynamic_cast<CModuleItem*>(i)) { //clicked on a module
  177. CSwordModuleInfo* mod = m->module();
  178. ListCSwordModuleInfo modules;
  179. modules.append(mod);
  180. emit createReadDisplayWindow(modules, TQString());
  181. }
  182. else if (CBookmarkItem* b = dynamic_cast<CBookmarkItem*>(i) ) { //clicked on a bookmark
  183. if (CSwordModuleInfo* mod = b->module()) {
  184. ListCSwordModuleInfo modules;
  185. modules.append(mod);
  186. emit createReadDisplayWindow(modules, b->key());
  187. }
  188. }
  189. }
  190. /** Reimplementation. Returns the drag object for the current selection. */
  191. TQDragObject* CMainIndex::dragObject() {
  192. if (!m_itemsMovable) {
  193. return 0;
  194. }
  195. CDragDropMgr::ItemList dndItems;
  196. TQPtrList<TQListViewItem> items = selectedItems();
  197. for (items.first(); items.current(); items.next()) {
  198. if (CItemBase* i = dynamic_cast<CItemBase*>(items.current())) {
  199. //we can move this item!
  200. if (!i->isMovable()) { //we can only drag items which allow us to do it, e.g. which are movable
  201. continue;
  202. }
  203. if (CBookmarkItem* bookmark = dynamic_cast<CBookmarkItem*>( items.current() )) {
  204. //take care of bookmarks which have no valid module any more, e.g. if it was uninstalled
  205. const TQString moduleName = bookmark->module() ? bookmark->module()->name() : TQString();
  206. dndItems.append( CDragDropMgr::Item(moduleName, bookmark->key(), bookmark->description()) );
  207. }
  208. }
  209. }
  210. return CDragDropMgr::dragObject( dndItems, viewport() );
  211. }
  212. /** Reimplementation from TDEListView. Returns true if the drag is acceptable for the listview. */
  213. bool CMainIndex::acceptDrag( TQDropEvent* event ) const {
  214. const TQPoint pos = contentsToViewport(event->pos());
  215. CItemBase* i = dynamic_cast<CItemBase*>(itemAt(pos));
  216. return i ? (i->acceptDrop(event) || i->isMovable()) : false;
  217. }
  218. /** No descriptions */
  219. void CMainIndex::initTree() {
  220. addGroup(CItemBase::BookmarkFolder, TQString("*"));
  221. addGroup(CItemBase::BibleModuleFolder, TQString("*"));
  222. addGroup(CItemBase::BookModuleFolder, TQString("*"));
  223. addGroup(CItemBase::CommentaryModuleFolder, TQString("*"));
  224. addGroup(CItemBase::DevotionalModuleFolder, TQString("*"));
  225. addGroup(CItemBase::GlossaryModuleFolder, TQString("*"));
  226. addGroup(CItemBase::LexiconModuleFolder, TQString("*"));
  227. }
  228. /** No descriptions */
  229. void CMainIndex::dropped( TQDropEvent* e, TQListViewItem* parent, TQListViewItem* after) {
  230. Q_ASSERT(after);
  231. Q_ASSERT(parent);
  232. //the drop was started in this main index widget
  233. if (m_itemsMovable && ((e->source()) == (viewport()))) {
  234. /*
  235. * If the drag was started from the main index and should move items and if the destination is the bookmark
  236. * folder or one of its subfolders
  237. * we remove the current items because the new ones will be inserted soon.
  238. */
  239. if (dynamic_cast<CBookmarkFolder*>(parent) || dynamic_cast<Bookmarks::SubFolder*>(parent)) {
  240. // we drop onto the bookmark folder or one of it's subfolders
  241. // TQPtrList<TQListViewItem> items = selectedItems();
  242. // items.setAutoDelete(true);
  243. // items.clear(); //delete the selected items we dragged
  244. }
  245. }
  246. //finally do the drop, either with external drop data or with the moved items' data
  247. CItemBase* const parentItem = dynamic_cast<CItemBase*>(parent);
  248. CItemBase* const afterItem = dynamic_cast<CItemBase*>(after);
  249. bool removeSelectedItems = true;
  250. bool moveSelectedItems = false;
  251. if (afterItem && afterItem->isFolder()) {
  252. moveSelectedItems = false;
  253. removeSelectedItems = false; //why TRUE?
  254. afterItem->setOpen(true);
  255. afterItem->dropped(e); //inserts new items, moving only works on the same level
  256. }
  257. else if (afterItem && !afterItem->isFolder() && parentItem) {
  258. const bool justMoveSelected =
  259. (e->source() == viewport())
  260. && m_itemsMovable
  261. && parentItem->acceptDrop(e)
  262. && !afterItem->acceptDrop(e);
  263. if (justMoveSelected) {
  264. moveSelectedItems = true;
  265. removeSelectedItems = false;
  266. }
  267. else {
  268. moveSelectedItems = false;
  269. removeSelectedItems = false;
  270. if (afterItem->acceptDrop(e)) {
  271. afterItem->dropped(e, after);
  272. }
  273. else { //insert in the parent folder and then move the inserted items
  274. parentItem->dropped(e, after);
  275. }
  276. }
  277. parentItem->setOpen(true);
  278. }
  279. else if (parentItem) { //no after item present, but a parent is there
  280. moveSelectedItems = false;
  281. removeSelectedItems = false;
  282. parentItem->setOpen(true);
  283. parentItem->dropped(e);
  284. }
  285. if (moveSelectedItems) {
  286. //move all selected items after the afterItem
  287. if (m_itemsMovable) {
  288. TQPtrList<TQListViewItem> items = selectedItems();
  289. TQListViewItem* i = items.first();
  290. TQListViewItem* after = afterItem;
  291. while (i && afterItem) {
  292. i->moveItem(after);
  293. after = i;
  294. i = items.next();
  295. }
  296. }
  297. }
  298. if (removeSelectedItems) {
  299. TQPtrList<TQListViewItem> items = selectedItems();
  300. items.setAutoDelete(true);
  301. items.clear(); //delete the selected items we dragged
  302. }
  303. }
  304. /** No descriptions */
  305. void CMainIndex::emitModulesChosen( ListCSwordModuleInfo modules, TQString key ) {
  306. emit createReadDisplayWindow(modules, key);
  307. }
  308. /** Returns the correct TDEAction object for the given type of action. */
  309. TDEAction* const CMainIndex::action( const CItemBase::MenuAction type ) const {
  310. switch (type) {
  311. case CItemBase::NewFolder:
  312. return m_actions.newFolder;
  313. case CItemBase::ChangeFolder:
  314. return m_actions.changeFolder;
  315. case CItemBase::ChangeBookmark:
  316. return m_actions.changeBookmark;
  317. case CItemBase::ImportBookmarks:
  318. return m_actions.importBookmarks;
  319. case CItemBase::ExportBookmarks:
  320. return m_actions.exportBookmarks;
  321. case CItemBase::PrintBookmarks:
  322. return m_actions.printBookmarks;
  323. case CItemBase::DeleteEntries:
  324. return m_actions.deleteEntries;
  325. case CItemBase::EditModule:
  326. return m_actions.editModuleMenu;
  327. case CItemBase::SearchInModules:
  328. return m_actions.searchInModules;
  329. case CItemBase::UnlockModule:
  330. return m_actions.unlockModule;
  331. case CItemBase::AboutModule:
  332. return m_actions.aboutModule;
  333. default:
  334. return 0;
  335. }
  336. }
  337. /** Shows the context menu at the given position. */
  338. void CMainIndex::contextMenu(TDEListView* /*list*/, TQListViewItem* i, const TQPoint& p) {
  339. //setup menu entries depending on current selection
  340. TQPtrList<TQListViewItem> items = selectedItems();
  341. if (items.count() == 0) {
  342. //special handling for no selection
  343. }
  344. else if (items.count() == 1) {
  345. //special handling for one selected item
  346. CItemBase* item = dynamic_cast<CItemBase*>(i);
  347. CItemBase::MenuAction actionType;
  348. for (int index = CItemBase::ActionBegin; index <= CItemBase::ActionEnd; ++index) {
  349. actionType = static_cast<CItemBase::MenuAction>(index);
  350. if (TDEAction* a = action(actionType))
  351. a->setEnabled( item->enableAction(actionType) );
  352. }
  353. }
  354. else {
  355. //first disable all actions
  356. CItemBase::MenuAction actionType;
  357. for (int index = CItemBase::ActionBegin; index <= CItemBase::ActionEnd; ++index) {
  358. actionType = static_cast<CItemBase::MenuAction>(index);
  359. if (TDEAction* a = action(actionType))
  360. a->setEnabled(false);
  361. }
  362. //enable the menu items depending on the types of the selected items.
  363. for (int index = CItemBase::ActionBegin; index <= CItemBase::ActionEnd; ++index) {
  364. actionType = static_cast<CItemBase::MenuAction>(index);
  365. bool enableAction = isMultiAction(actionType);
  366. for (items.first(); items.current(); items.next()) {
  367. CItemBase* i = dynamic_cast<CItemBase*>(items.current());
  368. enableAction = enableAction && i->enableAction(actionType);
  369. }
  370. if (enableAction) {
  371. TDEAction* a = action(actionType) ;
  372. if (i && a)
  373. a->setEnabled(enableAction);
  374. }
  375. }
  376. }
  377. //finally, open the popup
  378. m_popup->exec(p);
  379. }
  380. /** Adds a new subfolder to the current item. */
  381. void CMainIndex::createNewFolder() {
  382. CFolderBase* i = dynamic_cast<CFolderBase*>(currentItem());
  383. Q_ASSERT(i);
  384. if (i) {
  385. i->newSubFolder();
  386. }
  387. }
  388. /** Opens a dialog to change the current folder. */
  389. void CMainIndex::changeFolder() {
  390. CFolderBase* i = dynamic_cast<CFolderBase*>(currentItem());
  391. Q_ASSERT(i);
  392. if (i) {
  393. i->rename();
  394. }
  395. }
  396. /** Changes the current bookmark. */
  397. void CMainIndex::changeBookmark() {
  398. CBookmarkItem* i = dynamic_cast<CBookmarkItem*>(currentItem());
  399. Q_ASSERT(i);
  400. if (i) {
  401. i->rename();
  402. }
  403. }
  404. /** Exports the bookmarks being in the selected folder. */
  405. void CMainIndex::exportBookmarks() {
  406. CBookmarkFolder* i = dynamic_cast<CBookmarkFolder*>(currentItem());
  407. Q_ASSERT(i);
  408. if (i) {
  409. i->exportBookmarks();
  410. }
  411. }
  412. /** Import bookmarks from a file and add them to the selected folder. */
  413. void CMainIndex::importBookmarks() {
  414. CBookmarkFolder* i = dynamic_cast<CBookmarkFolder*>(currentItem());
  415. Q_ASSERT(i);
  416. if (i) {
  417. i->importBookmarks();
  418. }
  419. }
  420. /** Prints the selected bookmarks. */
  421. void CMainIndex::printBookmarks() {
  422. CPrinter::KeyTree tree;
  423. CPrinter::KeyTreeItem::Settings settings;
  424. settings.keyRenderingFace = CPrinter::KeyTreeItem::Settings::CompleteShort;
  425. TQPtrList<TQListViewItem> items;
  426. CBookmarkFolder* bf = dynamic_cast<CBookmarkFolder*>(currentItem());
  427. if (bf) {
  428. items = bf->getChildList();
  429. }
  430. else {
  431. items = selectedItems();
  432. }
  433. //create a tree of keytreeitems using the bookmark hierarchy.
  434. for (items.first(); items.current(); items.next()) {
  435. CBookmarkItem* i = dynamic_cast<CBookmarkItem*>(items.current());
  436. if (i) {
  437. tree.append( new CPrinter::KeyTreeItem( i->key(), i->module(), settings ) );
  438. }
  439. }
  440. util::scoped_ptr<CPrinter> printer(
  441. new CPrinter( TQT_TQOBJECT(this), CBTConfig::getDisplayOptionDefaults(), CBTConfig::getFilterOptionDefaults() )
  442. );
  443. printer->printKeyTree(tree);
  444. }
  445. /** Deletes the selected entries. */
  446. void CMainIndex::deleteEntries() {
  447. TQPtrList<TQListViewItem> items = selectedItems();
  448. if (!items.count())
  449. return;
  450. if (KMessageBox::warningYesNo(this, i18n("Do you really want to delete the selected items and child-items?"), i18n("Delete Items")) != KMessageBox::Yes) {
  451. return;
  452. }
  453. // We have to go backwards because otherwise deleting folders would delete their childs => crash before we delete those
  454. for (items.last(); items.current(); items.prev()) {
  455. if (CItemBase* i = dynamic_cast<CItemBase*>(items.current())) {
  456. if (i->enableAction(CItemBase::DeleteEntries)) {
  457. delete i;
  458. }
  459. }
  460. }
  461. }
  462. /** Opens the searchdialog for the selected modules. */
  463. void CMainIndex::searchInModules() {
  464. TQPtrList<TQListViewItem> items = selectedItems();
  465. ListCSwordModuleInfo modules;
  466. for (items.first(); items.current(); items.next()) {
  467. if (CModuleItem* i = dynamic_cast<CModuleItem*>(items.current())) {
  468. if (i->module()) {
  469. modules.append(i->module());
  470. }
  471. }
  472. }
  473. if (modules.isEmpty()) { //get a list of useful default modules for the search if no modules were selected
  474. CSwordModuleInfo* m = CBTConfig::get(CBTConfig::standardBible);
  475. if (m) {
  476. modules.append(m);
  477. }
  478. }
  479. Search::CSearchDialog::openDialog(modules, TQString());
  480. }
  481. /** Unlocks the current module. */
  482. void CMainIndex::unlockModule() {
  483. if (CModuleItem* i = dynamic_cast<CModuleItem*>(currentItem())) {
  484. bool ok = false;
  485. const TQString unlockKey = TQInputDialog::getText(i18n("BibleTime - Unlock work"),
  486. i18n("Enter the unlock key for this work."),
  487. TQLineEdit::Normal, i->module()->config(CSwordModuleInfo::CipherKey), &ok);
  488. if (ok) {
  489. i->module()->unlock( unlockKey );
  490. emit signalSwordSetupChanged();
  491. }
  492. }
  493. }
  494. /** Shows information about the current module. */
  495. void CMainIndex::aboutModule() {
  496. if (CModuleItem* i = dynamic_cast<CModuleItem*>(currentItem())) {
  497. KMessageBox::about(this, i->module()->aboutText(), i->module()->config(CSwordModuleInfo::Description), false);
  498. }
  499. }
  500. /** Reimplementation. Takes care of movable items. */
  501. void CMainIndex::startDrag() {
  502. TQPtrList<TQListViewItem> items = selectedItems();
  503. m_itemsMovable = true;
  504. for (items.first(); items.current() && m_itemsMovable; items.next()) {
  505. if (CItemBase* i = dynamic_cast<CItemBase*>(items.current())) {
  506. m_itemsMovable = (m_itemsMovable && i->isMovable());
  507. }
  508. else {
  509. m_itemsMovable = false;
  510. }
  511. }
  512. TDEListView::startDrag();
  513. }
  514. /** Reimplementation to support the items dragEnter and dragLeave functions. */
  515. void CMainIndex::contentsDragMoveEvent( TQDragMoveEvent* event ) {
  516. // tqWarning("void CMainIndex:: drag move event ( TQDragLeaveEvent* e )");
  517. CItemBase* i = dynamic_cast<CItemBase*>( itemAt( contentsToViewport(event->pos())) );
  518. if (i) {
  519. if (i->allowAutoOpen(event) || (i->acceptDrop(event) && i->isFolder() && i->allowAutoOpen(event) && !i->isOpen() && autoOpen()) ) {
  520. if (m_autoOpenFolder != i) {
  521. m_autoOpenTimer.stop();
  522. }
  523. m_autoOpenFolder = i;
  524. m_autoOpenTimer.start( 400, true );
  525. }
  526. else {
  527. m_autoOpenFolder = 0;
  528. }
  529. }
  530. else {
  531. m_autoOpenFolder = 0;
  532. }
  533. TDEListView::contentsDragMoveEvent(event);
  534. }
  535. TQRect CMainIndex::drawItemHighlighter(TQPainter* painter, TQListViewItem* item) {
  536. CBookmarkItem* bookmark = dynamic_cast<CBookmarkItem*>(item);
  537. if (bookmark) {
  538. //no drops on bookmarks allowed, just moving items after it
  539. return TQRect();
  540. }
  541. return TDEListView::drawItemHighlighter(painter, item);
  542. }
  543. void CMainIndex::autoOpenTimeout() {
  544. m_autoOpenTimer.stop();
  545. if (m_autoOpenFolder && !m_autoOpenFolder->isOpen() && m_autoOpenFolder->childCount()) {
  546. m_autoOpenFolder->setOpen(true);
  547. }
  548. }
  549. /** No descriptions */
  550. void CMainIndex::contentsDragLeaveEvent( TQDragLeaveEvent* e ) {
  551. m_autoOpenTimer.stop();
  552. TDEListView::contentsDragLeaveEvent(e);
  553. }
  554. /** Returns true if more than one netry is supported by this action type. Returns false for actions which support only one entry, e.g. about module etc. */
  555. const bool CMainIndex::isMultiAction( const CItemBase::MenuAction type ) const {
  556. switch (type) {
  557. case CItemBase::NewFolder:
  558. return false;
  559. case CItemBase::ChangeFolder:
  560. return false;
  561. case CItemBase::ChangeBookmark:
  562. return false;
  563. case CItemBase::ImportBookmarks:
  564. return false;
  565. case CItemBase::ExportBookmarks:
  566. return false;
  567. case CItemBase::PrintBookmarks:
  568. return true;
  569. case CItemBase::DeleteEntries:
  570. return true;
  571. case CItemBase::EditModule:
  572. return false;
  573. case CItemBase::SearchInModules:
  574. return true;
  575. case CItemBase::UnlockModule:
  576. return false;
  577. case CItemBase::AboutModule:
  578. return false;
  579. }
  580. return false;
  581. }
  582. /** Is called when items should be moved. */
  583. void CMainIndex::moved( TQPtrList<TQListViewItem>& /*items*/, TQPtrList<TQListViewItem>& /*afterFirst*/, TQPtrList<TQListViewItem>& /*afterNow*/) {
  584. tqDebug("move items");
  585. }
  586. /** Opens an editor window to edit the modules content. */
  587. void CMainIndex::editModulePlain() {
  588. TQPtrList<TQListViewItem> items = selectedItems();
  589. ListCSwordModuleInfo modules;
  590. for (items.first(); items.current(); items.next()) {
  591. if (CModuleItem* i = dynamic_cast<CModuleItem*>(items.current())) {
  592. modules.append(i->module());
  593. }
  594. }
  595. if (modules.count() == 1) {
  596. emit createWriteDisplayWindow(modules.first(), TQString(), CDisplayWindow::PlainTextWindow);
  597. };
  598. }
  599. /** Opens an editor window to edit the modules content. */
  600. void CMainIndex::editModuleHTML() {
  601. TQPtrList<TQListViewItem> items = selectedItems();
  602. ListCSwordModuleInfo modules;
  603. for (items.first(); items.current(); items.next()) {
  604. if (CModuleItem* i = dynamic_cast<CModuleItem*>(items.current())) {
  605. modules.append(i->module());
  606. }
  607. }
  608. if (modules.count() == 1) {
  609. emit createWriteDisplayWindow(modules.first(), TQString(), CDisplayWindow::HTMLWindow);
  610. }
  611. }
  612. /** Reloads the main index's Sword dependend things like modules */
  613. void CMainIndex::reloadSword() {
  614. //reload the modules, save the open groups before removing the items
  615. saveSettings();
  616. clear();
  617. initTree();
  618. readSettings();
  619. }
  620. /** Saves the bookmarks to disk */
  621. void CMainIndex::saveBookmarks() {
  622. //find the bookmark folder
  623. CItemBase* i = 0;
  624. TQListViewItemIterator it( this );
  625. while ( it.current() ) {
  626. i = dynamic_cast<CItemBase*>( it.current() );
  627. if (i && (i->type() == CItemBase::BookmarkFolder)) {
  628. //found the bookmark folder
  629. TDEStandardDirs stdDirs;
  630. const TQString path = stdDirs.saveLocation("data", "bibletime/");
  631. if (!path.isEmpty()) {
  632. //save the bookmarks to the right file
  633. if (CBookmarkFolder* f = dynamic_cast<CBookmarkFolder*>(i)) {
  634. f->saveBookmarks( path + "bookmarks.xml" );
  635. }
  636. }
  637. break;
  638. }
  639. ++it;
  640. }
  641. }
  642. void CMainIndex::readSettings() {
  643. tqDebug("CMainIndex::readSettings");
  644. TQStringList openGroups = CBTConfig::get(CBTConfig::bookshelfOpenGroups);
  645. for (TQStringList::Iterator it( openGroups.begin() ); it != openGroups.end(); ++it) {
  646. TQStringList path = TQStringList::split("/", (*it)); //e.g. with items parent, child
  647. TQListViewItem* item = firstChild(); //begin on the top for each item
  648. Q_ASSERT(item);
  649. unsigned int index = 1;
  650. for (TQStringList::Iterator p_it( path.begin() ); p_it != path.end(); ++p_it) {
  651. TQString itemName = (*p_it).replace("\\/", "/");
  652. while (item && (item->text(0) != itemName)) {
  653. item = item->nextSibling();
  654. }
  655. if (item && (item->text(0) == itemName)) {
  656. if (index < path.count()) { //don't call firstChild on the right, i.e. last item of the list
  657. item = item->firstChild();
  658. }
  659. ++index;
  660. }
  661. }
  662. if (item) {
  663. item->setOpen(true);
  664. }
  665. }
  666. //restore the content position
  667. // setContentsPos(
  668. // CBTConfig::get(CBTConfig::bookshelfContentsX),
  669. // CBTConfig::get(CBTConfig::bookshelfContentsY)
  670. // );
  671. // horizontalScrollBar()->setValue(CBTConfig::get(CBTConfig::bookshelfContentsX));
  672. // verticalScrollBar()->setValue(CBTConfig::get(CBTConfig::bookshelfContentsY));
  673. //restore the selected item
  674. TQStringList path = TQStringList::split("/", CBTConfig::get(CBTConfig::bookshelfCurrentItem));
  675. TQListViewItem* item = firstChild();
  676. Q_ASSERT(item);
  677. unsigned int index = 1;
  678. for (TQStringList::iterator it( path.begin() ); it != path.end(); ++it) {
  679. //search for the current caption and go down to it's childs
  680. while (item && (item->text(0) != (*it)) ) {
  681. item = item->nextSibling();
  682. }
  683. if (item && ((*it) == item->text(0))) {
  684. if (index == path.count()) { //last item reached
  685. setCurrentItem( item );
  686. setSelected( item, true );
  687. break;//for loop
  688. }
  689. else {
  690. item = item->firstChild();
  691. }
  692. index++;
  693. }
  694. }
  695. }
  696. void CMainIndex::saveSettings() {
  697. //save the complete names of all open groups to the settings file (e.g. Bibles/German/,Bookmarks/Jeuss Christ
  698. TQStringList openGroups;
  699. TQListViewItemIterator it( this );
  700. while ( it.current() ) {
  701. if ( it.current()->isOpen() ) { //is a group and open
  702. //it.current()'s full name needs to be added to the list
  703. TQListViewItem* i = it.current();
  704. TQString fullName = i->text(0);
  705. while (i->parent()) {
  706. i = i->parent();
  707. fullName.prepend("/").prepend( i->text(0).replace("/", "\\/")); // parent / child
  708. }
  709. openGroups << fullName;
  710. }
  711. ++it;
  712. }
  713. CBTConfig::set(CBTConfig::bookshelfOpenGroups, openGroups);
  714. //now save the position of the scrollbars
  715. // CBTConfig::set(CBTConfig::bookshelfContentsX,
  716. // horizontalScrollBar() ? horizontalScrollBar()->value() : 0);
  717. // CBTConfig::set(CBTConfig::bookshelfContentsY,
  718. // verticalScrollBar() ? verticalScrollBar()->value() : 0);
  719. //save the currently selected item
  720. TQListViewItem* item = currentItem();
  721. TQString path;
  722. while (item) {
  723. path.prepend( item->text(0) + "/" );
  724. item = item->parent();
  725. }
  726. CBTConfig::set(CBTConfig::bookshelfCurrentItem, path);
  727. }
  728. void CMainIndex::polish()
  729. {
  730. TDEListView::polish();
  731. initTree();
  732. readSettings();
  733. }
  734. #include "cmainindex.moc"