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.

clexiconkeychooser.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "clexiconkeychooser.h"
  10. #include "ckeychooserwidget.h"
  11. #include "cscrollbutton.h"
  12. #include "backend/cswordlexiconmoduleinfo.h"
  13. #include "frontend/cbtconfig.h"
  14. #include "util/cresmgr.h"
  15. //STL headers
  16. #include <algorithm>
  17. #include <iterator>
  18. #include <map>
  19. //TQt includes
  20. #include <tqcombobox.h>
  21. #include <tqlayout.h>
  22. #include <tqlistbox.h>
  23. //KDE includes
  24. #include <tdelocale.h>
  25. CLexiconKeyChooser::CLexiconKeyChooser(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent, const char *name )
  26. : CKeyChooser(modules, key, parent, name),
  27. m_key(dynamic_cast<CSwordLDKey*>(key)) {
  28. setModules(modules, false);
  29. //we use a layout because the key chooser should be resized to full size
  30. m_layout = new TQHBoxLayout(this, TQBoxLayout::LeftToRight);
  31. m_layout->setResizeMode(TQLayout::FreeResize);
  32. m_widget = new CKeyChooserWidget(0, false, this);
  33. //don't allow a too high width, try to keep as narrow as possible
  34. //to aid users with smaller screen resolutions
  35. m_widget->comboBox()->setMaximumWidth(200);
  36. m_widget->setToolTips(
  37. CResMgr::displaywindows::lexiconWindow::entryList::tooltip,
  38. CResMgr::displaywindows::lexiconWindow::nextEntry::tooltip,
  39. CResMgr::displaywindows::general::scrollButton::tooltip,
  40. CResMgr::displaywindows::lexiconWindow::previousEntry::tooltip
  41. );
  42. m_layout->addWidget(m_widget,0,TQt::AlignLeft);
  43. connect(m_widget,TQT_SIGNAL(changed(int)),TQT_SLOT(activated(int)));
  44. connect(m_widget,TQT_SIGNAL(focusOut(int)),TQT_SLOT(activated(int)));
  45. setModules(modules, true);
  46. setKey(key);
  47. }
  48. CSwordKey* const CLexiconKeyChooser::key() {
  49. // tqWarning("key");
  50. return m_key;
  51. }
  52. void CLexiconKeyChooser::setKey(CSwordKey* key) {
  53. if (!(m_key = dynamic_cast<CSwordLDKey*>(key))) {
  54. return;
  55. }
  56. // tqWarning("setKey start");
  57. TQString newKey = m_key->key();
  58. const int index = m_widget->comboBox()->listBox()->index(m_widget->comboBox()->listBox()->findItem( newKey ));
  59. m_widget->comboBox()->setCurrentItem(index);
  60. // tqWarning("setKey end");
  61. emit keyChanged( m_key );
  62. }
  63. void CLexiconKeyChooser::activated(int index) {
  64. // tqWarning("activated");
  65. const TQString text = m_widget->comboBox()->text(index);
  66. // To prevent from eternal loop, because activated() is emitted again
  67. if (m_key && m_key->key() != text) {
  68. m_key->key(text);
  69. setKey(m_key);
  70. }
  71. // tqWarning("activated end");
  72. }
  73. inline const bool my_cmpEntries(const TQString& a, const TQString& b) {
  74. return a < b;
  75. };
  76. /** Reimplementation. */
  77. void CLexiconKeyChooser::refreshContent() {
  78. if (m_modules.count() == 1) {
  79. m_widget->reset(m_modules.first()->entries(), 0, true);
  80. // tqWarning("resetted");
  81. }
  82. else {
  83. typedef std::multimap<unsigned int, TQStringList*> EntryMap;
  84. EntryMap entryMap;
  85. TQStringList* entries = 0;
  86. for (m_modules.first(); m_modules.current(); m_modules.next()) {
  87. entries = m_modules.current()->entries();
  88. entryMap.insert( std::make_pair(entries->count(), entries) );
  89. }
  90. TQStringList goodEntries; //The string list which contains the entries which are available in all modules
  91. EntryMap::iterator it = entryMap.begin(); //iterator to go thoigh all selected modules
  92. TQStringList refEntries = *(it->second); //copy the items for the first time
  93. TQStringList* cmpEntries = ( ++it )->second; //list for comparision, starts with the second module in the map
  94. while(it != entryMap.end()) {
  95. std::set_intersection(
  96. refEntries.begin(), --(refEntries.end()), //--end() is the last valid entry
  97. cmpEntries->begin(), --(cmpEntries->end()),
  98. std::back_inserter(goodEntries), //append valid entries to the end of goodEntries
  99. my_cmpEntries //ci_cmpEntries is the comparision function
  100. );
  101. cmpEntries = ( ++it )->second; //this is a pointer to the string list of a new module
  102. /*
  103. * use the good entries for next comparision,
  104. * because the final list can only have the entries of goodEntries as maxiumum
  105. */
  106. refEntries = goodEntries;
  107. };
  108. m_widget->reset(goodEntries, 0, true); //write down the entries
  109. } //end of ELSE
  110. //make sure the list sorted
  111. /* This is not the best solution, module()->entries() should be sorted already */
  112. // Q_ASSERT(m_widget->comboBox()->listBox());
  113. // m_widget->comboBox()->listBox()->sort();
  114. }
  115. /** No descriptions */
  116. void CLexiconKeyChooser::adjustFont() {
  117. // //Make sure the entries are displayed correctly.
  118. // m_widget->comboBox()->setFont( CBTConfig::get( m_modules.first()->language() ).second );
  119. }
  120. /** Sets the module and refreshes the combo boxes */
  121. void CLexiconKeyChooser::setModules( const ListCSwordModuleInfo& modules, const bool refresh ) {
  122. Q_ASSERT(!m_modules.autoDelete());
  123. m_modules.clear();
  124. Q_ASSERT(!m_modules.autoDelete());
  125. // for (modules.first(); modules.current(); modules.next()) {
  126. ListCSwordModuleInfo::const_iterator end_it = modules.end();
  127. for (ListCSwordModuleInfo::const_iterator it(modules.begin()); it != end_it; ++it) {
  128. CSwordLexiconModuleInfo* lexicon = dynamic_cast<CSwordLexiconModuleInfo*>(*it);
  129. if (lexicon) {
  130. m_modules.append(lexicon);
  131. }
  132. }
  133. if (refresh) {
  134. refreshContent();
  135. // adjustFont();
  136. }
  137. }
  138. /** No descriptions */
  139. void CLexiconKeyChooser::updateKey(CSwordKey*) {}
  140. #include "clexiconkeychooser.moc"