Bibletime – a bible study tool
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.

cmodulechooser.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "cmodulechooser.h"
  10. #include "backend/cswordkey.h"
  11. #include "backend/cswordversekey.h"
  12. #include "frontend/cbtconfig.h"
  13. #include "util/cresmgr.h"
  14. #include "util/ctoolclass.h"
  15. //TQt includes
  16. #include <tqhbox.h>
  17. #include <tqvbox.h>
  18. #include <tqptrlist.h>
  19. #include <tqpainter.h>
  20. #include <tqlayout.h>
  21. #include <tqmap.h>
  22. #include <tqlineedit.h>
  23. #include <tqtextedit.h>
  24. #include <tqlabel.h>
  25. #include <tqsizepolicy.h>
  26. #include <tqpushbutton.h>
  27. #include <tqheader.h>
  28. #include <tqregexp.h>
  29. #include <tqmessagebox.h>
  30. //KDE includes
  31. #include <tdeapplication.h>
  32. #include <tdefiledialog.h>
  33. #include <tdelocale.h>
  34. #include <kiconloader.h>
  35. namespace Search {
  36. namespace Options {
  37. /****************************/
  38. /****************************/
  39. /****************************/
  40. CModuleChooser::ModuleCheckBoxItem::ModuleCheckBoxItem(TQListViewItem* item, CSwordModuleInfo* module) : TQCheckListItem(item, TQString(), TQCheckListItem::CheckBox) {
  41. m_module = module;
  42. setText(0,m_module->name());
  43. };
  44. CModuleChooser::ModuleCheckBoxItem::~ModuleCheckBoxItem() {}
  45. ;
  46. /** Returns the used module. */
  47. CSwordModuleInfo* const CModuleChooser::ModuleCheckBoxItem::module() const {
  48. return m_module;
  49. }
  50. /****************************/
  51. /****************************/
  52. /****************************/
  53. CModuleChooser::CModuleChooser(TQWidget* parent) : TDEListView(parent) {
  54. initView();
  55. initTree();
  56. }
  57. CModuleChooser::~CModuleChooser() {}
  58. void CModuleChooser::show() {
  59. TDEListView::show();
  60. //open module items
  61. TQListViewItemIterator it( this );
  62. for ( ; it.current(); ++it ) {
  63. if ( ModuleCheckBoxItem* i = dynamic_cast<ModuleCheckBoxItem*>(it.current()) ) {
  64. if (i->isOn()) {
  65. ensureItemVisible(i);
  66. };
  67. }
  68. }
  69. }
  70. /** Initializes this widget and the childs of it. */
  71. void CModuleChooser::initView() {
  72. addColumn( i18n("Work") );
  73. setRootIsDecorated(true);
  74. // header()->hide();
  75. setFullWidth(true);
  76. }
  77. /** Initializes the tree of this widget. */
  78. void CModuleChooser::initTree() {
  79. ListCSwordModuleInfo mods = backend()->moduleList();
  80. /**
  81. * The next steps:
  82. * 1. Sort by type
  83. * 2. Sort the modules of this type by their language
  84. * 3. Create the subfolders for this
  85. */
  86. TQMap<CSwordModuleInfo::ModuleType, TQString> typenameMap;
  87. typenameMap.insert(CSwordModuleInfo::Bible, i18n("Bibles"));
  88. typenameMap.insert(CSwordModuleInfo::Commentary, i18n("Commentaries"));
  89. typenameMap.insert(CSwordModuleInfo::Lexicon, i18n("Lexicons"));
  90. typenameMap.insert(CSwordModuleInfo::GenericBook, i18n("Books"));
  91. int type = CSwordModuleInfo::Bible;
  92. bool ok = true;
  93. bool addedDevotionals = false;
  94. bool addedGlossaries = false;
  95. bool addedLexs = false;
  96. bool incType = false;
  97. while (ok) {
  98. ListCSwordModuleInfo modsForType;
  99. TQString typeFolderCaption = TQString();
  100. incType = false;
  101. if (static_cast<CSwordModuleInfo::ModuleType>(type) == CSwordModuleInfo::Lexicon) {
  102. if (!addedLexs) {
  103. // for (mods.first(); mods.current(); mods.next()) {
  104. ListCSwordModuleInfo::iterator end_it = mods.end();
  105. for (ListCSwordModuleInfo::iterator it(mods.begin()); it != end_it; ++it) {
  106. if (((*it)->type() == CSwordModuleInfo::Lexicon)
  107. && ((*it)->category() != CSwordModuleInfo::DailyDevotional)
  108. && ((*it)->category() != CSwordModuleInfo::Glossary)
  109. ) {
  110. modsForType.append( *it );
  111. };
  112. };
  113. addedLexs = true;
  114. typeFolderCaption = TQString();
  115. }
  116. else if (!addedDevotionals) {
  117. // for (mods.first(); mods.current(); mods.next()) {
  118. ListCSwordModuleInfo::iterator end_it = mods.end();
  119. for (ListCSwordModuleInfo::iterator it(mods.begin()); it != end_it; ++it) {
  120. if ((*it)->category() == CSwordModuleInfo::DailyDevotional) {
  121. modsForType.append(*it);
  122. };
  123. };
  124. addedDevotionals = true;
  125. typeFolderCaption = i18n("Daily Devotionals");
  126. }
  127. else if (!addedGlossaries) {
  128. // for (mods.first(); mods.current(); mods.next()) {
  129. ListCSwordModuleInfo::iterator end_it = mods.end();
  130. for (ListCSwordModuleInfo::iterator it(mods.begin()); it != end_it; ++it) {
  131. if ((*it)->category() == CSwordModuleInfo::Glossary) {
  132. modsForType.append(*it);
  133. };
  134. };
  135. addedGlossaries = true;
  136. typeFolderCaption = i18n("Glossaries");
  137. };
  138. if (addedLexs && addedDevotionals && addedGlossaries)
  139. incType = true;
  140. }
  141. else if (type == CSwordModuleInfo::Bible || type == CSwordModuleInfo::Commentary || type == CSwordModuleInfo::GenericBook) {
  142. // for (mods.first(); mods.current(); mods.next()) {
  143. ListCSwordModuleInfo::iterator end_it = mods.end();
  144. for (ListCSwordModuleInfo::iterator it(mods.begin()); it != end_it; ++it) {
  145. if ((*it)->type() == type) {
  146. modsForType.append(*it);
  147. };
  148. };
  149. incType = true;
  150. }
  151. else
  152. ok = false;
  153. if (typeFolderCaption.isEmpty()) {
  154. typeFolderCaption = typenameMap[static_cast<CSwordModuleInfo::ModuleType>(type)];
  155. }
  156. //get the available languages of the selected modules
  157. TQStringList langs;
  158. // for (modsForType.first(); modsForType.current(); modsForType.next()) {
  159. ListCSwordModuleInfo::iterator end_it = modsForType.end();
  160. for (ListCSwordModuleInfo::iterator it(modsForType.begin()); it != end_it; ++it) {
  161. if ( !langs.contains(TQString( (*it)->module()->Lang() ))) {
  162. langs.append( (*it)->module()->Lang() );
  163. }
  164. };
  165. langs.sort();
  166. //go through the list of languages and create subfolders for each language and the modules of the language
  167. TQListViewItem* typeFolder = 0;
  168. if (modsForType.count()) {
  169. typeFolder = new TQListViewItem(this, typeFolder, typeFolderCaption);
  170. }
  171. else {
  172. if (incType) {
  173. type++;
  174. }
  175. continue;
  176. };
  177. TQString language = TQString();
  178. CLanguageMgr* langMgr = languageMgr();
  179. for ( TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it ) {
  180. language = langMgr->languageForAbbrev(*it)->translatedName();
  181. if (language.isEmpty()) {
  182. language = (*it);
  183. }
  184. TQListViewItem* langFolder = new TQListViewItem(typeFolder,language);
  185. langFolder->setPixmap(0, SmallIcon(CResMgr::mainIndex::closedFolder::icon, 16));
  186. //create the module items of this lang folder
  187. // for (modsForType.first(); modsForType.current(); modsForType.next()) {
  188. ListCSwordModuleInfo::iterator end_modItr = modsForType.end();
  189. for (ListCSwordModuleInfo::iterator mod_Itr(modsForType.begin()); mod_Itr != end_modItr; ++mod_Itr) {
  190. if (TQString( (*mod_Itr)->module()->Lang() ) == (*it) ) { //found correct language
  191. ModuleCheckBoxItem* i = new ModuleCheckBoxItem(langFolder, *mod_Itr);
  192. i->setPixmap(0, CToolClass::getIconForModule(*mod_Itr));
  193. };
  194. };
  195. };
  196. typeFolder->setPixmap(0,SmallIcon(CResMgr::mainIndex::closedFolder::icon, 16));
  197. if (incType) {
  198. ++type;
  199. }
  200. };
  201. }
  202. /** Returns a list of selected modules. */
  203. ListCSwordModuleInfo CModuleChooser::modules() {
  204. ListCSwordModuleInfo mods;
  205. TQListViewItemIterator it( this );
  206. for ( ; it.current(); ++it ) {
  207. if ( ModuleCheckBoxItem* i = dynamic_cast<ModuleCheckBoxItem*>(it.current()) ) {
  208. //add the module if the box is checked
  209. if (i->isOn()) {
  210. mods.append(i->module());
  211. };
  212. };
  213. };
  214. return mods;
  215. }
  216. /** Sets the list of modules and updates the state of the checkbox items. */
  217. void CModuleChooser::setModules( ListCSwordModuleInfo modules ) {
  218. // tqWarning("CModuleChooser::setModules( ListCSwordModuleInfo modules )");
  219. TQListViewItemIterator it( this );
  220. for ( ; it.current(); ++it ) {
  221. if ( ModuleCheckBoxItem* i = dynamic_cast<ModuleCheckBoxItem*>(it.current()) ) {
  222. i->setOn(modules.contains(i->module())); //set the status for the module checkbox item
  223. // if (i->isOn()) { //if it's checked, show the item
  224. // tqWarning("show item!");
  225. // ensureItemVisible(i);
  226. // }
  227. }
  228. };
  229. }
  230. /****************************/
  231. CModuleChooserDialog::CModuleChooserDialog( TQWidget* parentDialog, ListCSwordModuleInfo modules ) :
  232. KDialogBase(Plain, i18n("Choose work(s)"), Ok, Ok, parentDialog, "CModuleChooser", false, true) {
  233. initView();
  234. initConnections();
  235. m_moduleChooser->setModules(modules);
  236. };
  237. CModuleChooserDialog::~CModuleChooserDialog() {}
  238. ;
  239. /** Initializes the view of this dialog */
  240. void CModuleChooserDialog::initView() {
  241. setButtonOKText(i18n("Use chosen work(s)"));
  242. TQFrame* page = plainPage();
  243. TQHBoxLayout* layout = new TQHBoxLayout(page);
  244. m_moduleChooser = new CModuleChooser(page);
  245. m_moduleChooser->setMinimumSize(320,400);
  246. layout->addWidget(m_moduleChooser);
  247. }
  248. /** Initializes the connections of this dialog. */
  249. void CModuleChooserDialog::initConnections() {}
  250. /** Reimplementation to handle the modules. */
  251. void CModuleChooserDialog::slotOk() {
  252. emit modulesChanged( m_moduleChooser->modules() );
  253. KDialogBase::slotOk();
  254. }
  255. } //end of namespace Search::Options
  256. } //end of namespace Search
  257. #include "cmodulechooser.moc"