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.

cmodulechooserbutton.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "cmodulechooserbutton.h"
  10. #include "cmodulechooserbar.h"
  11. #include "backend/cswordbackend.h"
  12. #include "util/cresmgr.h"
  13. //TQt includes
  14. #include <tqstring.h>
  15. #include <tqtooltip.h>
  16. #include <tqdict.h>
  17. #include <tqvaluelist.h>
  18. //KDE includes
  19. #include <tdelocale.h>
  20. #include <tdeglobal.h>
  21. #include <kiconloader.h>
  22. CModuleChooserButton::CModuleChooserButton(CSwordModuleInfo* useModule,CSwordModuleInfo::ModuleType type, const int id, CModuleChooserBar *parent, const char *name )
  23. : TDEToolBarButton(iconName(), id, parent, name),
  24. m_id(id), m_popup(0), m_moduleChooserBar(parent) {
  25. m_moduleType = type;
  26. m_module = useModule;
  27. if (!m_module) {
  28. m_hasModule = false;
  29. }
  30. else {
  31. m_hasModule = true;
  32. }
  33. setIcon( iconName() );
  34. setPopupDelay(1);
  35. populateMenu();
  36. }
  37. CModuleChooserButton::~CModuleChooserButton() {
  38. m_submenus.setAutoDelete(true); //delete all submenus
  39. m_submenus.clear();
  40. delete m_popup;
  41. }
  42. /** Returns the icon used for the current status. */
  43. const TQString CModuleChooserButton::iconName() {
  44. switch (m_moduleType) {
  45. case CSwordModuleInfo::Bible:
  46. if (m_hasModule)
  47. return CResMgr::modules::bible::icon_unlocked;
  48. else
  49. return CResMgr::modules::bible::icon_add;
  50. case CSwordModuleInfo::Commentary:
  51. if (m_hasModule)
  52. return CResMgr::modules::commentary::icon_unlocked;
  53. else
  54. return CResMgr::modules::commentary::icon_add;
  55. case CSwordModuleInfo::Lexicon:
  56. if (m_hasModule)
  57. return CResMgr::modules::lexicon::icon_unlocked;
  58. else
  59. return CResMgr::modules::lexicon::icon_add;
  60. case CSwordModuleInfo::GenericBook:
  61. if (m_hasModule)
  62. return CResMgr::modules::book::icon_unlocked;
  63. else
  64. return CResMgr::modules::book::icon_add;
  65. default: //return as default the bible icon
  66. return CResMgr::modules::bible::icon_unlocked;
  67. }
  68. }
  69. CSwordModuleInfo* CModuleChooserButton::module() {
  70. for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {
  71. for (unsigned int i = 0; i < popup->count(); i++) {
  72. if ( m_popup->isItemChecked(popup->idAt(i)) ) {
  73. TQString mod = popup->text(popup->idAt(i)).remove('&');
  74. return backend()->findModuleByName( mod.left(mod.find(" ")) );
  75. }
  76. }
  77. }
  78. return 0; //"none" selected
  79. }
  80. /** Returns the id used for this button. */
  81. int CModuleChooserButton::getId() const {
  82. return m_id;
  83. }
  84. /** Is called after a module was selected in the popup */
  85. void CModuleChooserButton::moduleChosen( int ID ) {
  86. for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {
  87. for (unsigned int i = 0; i < popup->count(); i++) {
  88. popup->setItemChecked(popup->idAt(i),false);
  89. }
  90. popup->setItemChecked(ID, true);
  91. }
  92. m_popup->setItemChecked(m_noneId, false); //uncheck the "none" item
  93. if (m_popup->text(ID).remove('&') == i18n("NONE")) { // note: this is for m_popup, the toplevel!
  94. if (m_hasModule) {
  95. emit sigRemoveButton(m_id);
  96. return;
  97. }
  98. }
  99. else {
  100. if (!m_hasModule) {
  101. emit sigAddButton();
  102. }
  103. m_hasModule = true;
  104. m_module = module();
  105. setIcon( iconName() );
  106. emit sigChanged();
  107. setText( i18n("Select a work") );
  108. m_popup->changeTitle(m_titleId, i18n("Select a work"));
  109. TQToolTip::remove
  110. (this);
  111. if (module()) {
  112. TQToolTip::add
  113. (this, module()->name());
  114. }
  115. }
  116. }
  117. /** No descriptions */
  118. void CModuleChooserButton::populateMenu() {
  119. m_submenus.setAutoDelete(true); //delete all submenus
  120. m_submenus.clear();
  121. delete m_popup;
  122. //create a new, empty popup
  123. m_popup = new TDEPopupMenu(this);
  124. if (m_module) {
  125. m_titleId = m_popup->insertTitle( i18n("Select a work") );
  126. }
  127. else {
  128. m_titleId = m_popup->insertTitle( i18n("Select an additional work") );
  129. }
  130. m_popup->setCheckable(true);
  131. m_noneId = m_popup->insertItem(i18n("NONE"));
  132. if ( !m_module ) {
  133. m_popup->setItemChecked(m_noneId, true);
  134. }
  135. m_popup->insertSeparator();
  136. connect(m_popup, TQT_SIGNAL(activated(int)), this, TQT_SLOT(moduleChosen(int)));
  137. setPopup(m_popup, true);
  138. TQStringList languages;
  139. TQDict<TDEPopupMenu> langdict;
  140. //the modules list contains only the modules we can use, i.e. same type and same features
  141. ListCSwordModuleInfo modules;
  142. ListCSwordModuleInfo allMods = backend()->moduleList();
  143. // for (allMods.first(); allMods.current(); allMods.next()) {
  144. ListCSwordModuleInfo::iterator end_it = allMods.end();
  145. for (ListCSwordModuleInfo::iterator it(allMods.begin()); it != end_it; ++it) {
  146. if ((*it)->type() != m_moduleType) {
  147. continue;
  148. }
  149. modules.append( *it );
  150. };
  151. //iterate through all found modules of the type we support
  152. // for (modules.first(); modules.current(); modules.next()) {
  153. /*ListCSwordModuleInfo::iterator*/
  154. end_it = modules.end();
  155. for (ListCSwordModuleInfo::iterator it(modules.begin()); it != end_it; ++it) {
  156. TQString lang = (*it)->language()->translatedName();
  157. if (lang.isEmpty()) {
  158. //lang = TQString::fromLatin1("xx"); //unknown language -- do not use English as default!!
  159. lang = (*it)->language()->abbrev();
  160. if (lang.isEmpty()) {
  161. lang = "xx";
  162. }
  163. }
  164. if (languages.find( lang ) == languages.end() ) { //this lang was not yet added
  165. languages += lang;
  166. TDEPopupMenu* menu = new TDEPopupMenu;
  167. langdict.insert(lang, menu );
  168. m_submenus.append(menu);
  169. connect(menu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(moduleChosen(int)));
  170. }
  171. }
  172. //Check the appropriate entry
  173. // for (modules.first(); modules.current(); modules.next()) {
  174. /*ListCSwordModuleInfo::iterator*/ end_it = modules.end();
  175. for (ListCSwordModuleInfo::iterator it(modules.begin()); it != end_it; ++it) {
  176. TQString lang = (*it)->language()->translatedName();
  177. if (lang.isEmpty()) {
  178. lang = (*it)->language()->abbrev();
  179. if (lang.isEmpty()) {
  180. lang = "xx";
  181. }
  182. }
  183. TQString name((*it)->name());
  184. name.append(" ").append((*it)->isLocked() ? i18n("[locked]") : TQString());
  185. const int id = langdict[lang]->insertItem( name );
  186. if ( m_module && (*it)->name() == m_module->name()) {
  187. langdict[lang]->setItemChecked(id, true);
  188. }
  189. }
  190. languages.sort();
  191. for ( TQStringList::Iterator it = languages.begin(); it != languages.end(); ++it ) {
  192. m_popup->insertItem( *it, langdict[*it]);
  193. }
  194. if (module()) {
  195. TQToolTip::add
  196. (this, module()->name());
  197. }
  198. else {
  199. TQToolTip::add
  200. (this, i18n("No work selected"));
  201. }
  202. }
  203. /*!
  204. \fn CModuleChooserButton::updateMenuItems()
  205. */
  206. void CModuleChooserButton::updateMenuItems() {
  207. TQString moduleName;
  208. CSwordModuleInfo* module = 0;
  209. ListCSwordModuleInfo chosenModules = m_moduleChooserBar->getModuleList();
  210. for ( TDEPopupMenu* popup = m_submenus.first(); popup; popup = m_submenus.next() ) {
  211. for (unsigned int i = 0; i < popup->count(); i++) {
  212. moduleName = popup->text(popup->idAt(i)).remove('&');
  213. module = backend()->findModuleByName( moduleName.left(moduleName.findRev(" ")) );
  214. Q_ASSERT(module);
  215. // if (!module) {
  216. // tqWarning("Can't find module with name %s", moduleName.latin1());
  217. // }
  218. bool alreadyChosen = chosenModules.contains( module );
  219. if (m_module) {
  220. alreadyChosen = alreadyChosen && (m_module->name() != moduleName);
  221. }
  222. popup->setItemEnabled(popup->idAt(i), !alreadyChosen); //grey it out, it was chosen already
  223. }
  224. }
  225. }
  226. #include "cmodulechooserbutton.moc"