Bibletime – a bible study tool
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cbookkeychooser.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*********
  2. *
  3. * This file is part of BibleTime's source code, http://www.bibletime.info/.
  4. *
  5. * Copyright 1999-2007 by the BibleTime developers.
  6. * The BibleTime source code is licensed under the GNU General Public License version 2.0.
  7. *
  8. **********/
  9. #include "cbookkeychooser.h"
  10. #include "backend/cswordtreekey.h"
  11. #include "backend/cswordbookmoduleinfo.h"
  12. #include "frontend/cbtconfig.h"
  13. //TQt includes
  14. #include <tqlayout.h>
  15. #include <tqmap.h>
  16. TQMap<TQObject*, int> boxes;
  17. CBookKeyChooser::CBookKeyChooser(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent, const char *name)
  18. : CKeyChooser(modules, key, parent,name), m_layout(0) {
  19. setModules(modules, false);
  20. m_key = dynamic_cast<CSwordTreeKey*>(key);
  21. if (!m_modules.count()) {
  22. m_key = 0;
  23. }
  24. setModules(modules, true);
  25. setKey(key);
  26. adjustFont();
  27. }
  28. CBookKeyChooser::~CBookKeyChooser() {}
  29. void CBookKeyChooser::setKey(CSwordKey* newKey) {
  30. setKey(newKey, true);
  31. }
  32. /** Sets a new key to this keychooser */
  33. void CBookKeyChooser::setKey(CSwordKey* newKey, const bool emitSignal) {
  34. //tqDebug("CBookKeyChooser::setKey");
  35. if (m_key != newKey) {
  36. m_key = dynamic_cast<CSwordTreeKey*>(newKey);
  37. }
  38. /*const */TQString oldKey = m_key->key();
  39. if (oldKey.isEmpty()) { //don't set keys equal to "/", always use a key which may have content
  40. m_key->firstChild();
  41. oldKey = m_key->key();
  42. }
  43. const int oldOffset = m_key->getOffset();
  44. TQStringList siblings;
  45. if (m_key && !oldKey.isEmpty()) {
  46. siblings = TQStringList::split("/", oldKey, false);
  47. }
  48. int depth = 0;
  49. int index = 0;
  50. m_key->root();
  51. while( m_key->firstChild() && (depth <= int(siblings.count())) ) {
  52. const TQString key = m_key->key();
  53. index = (depth == 0) ? -1 : 0;
  54. const TQString sibling = siblings[depth];
  55. if (!sibling.isEmpty()) { //found it
  56. bool found = false;
  57. do {
  58. ++index;
  59. //tqDebug("set 'found'");
  60. //found = (TQString::fromLocal8Bit(m_key->getLocalName()) == sibling);
  61. found = (m_key->getLocalNameUnicode() == sibling);
  62. }
  63. while (!found && m_key->nextSibling());
  64. if (!found) {
  65. m_key->key( key );
  66. }
  67. }
  68. setupCombo(key, depth, index);
  69. depth++;
  70. }
  71. //clear the combos which were not filled
  72. for (; depth < m_modules.first()->depth(); ++depth) {
  73. CKeyChooserWidget* chooser = m_chooserWidgets.at(depth);
  74. if (chooser) {
  75. chooser->reset(0,0,false);
  76. }
  77. }
  78. if (oldKey.isEmpty()) {
  79. m_key->root();
  80. }
  81. else {
  82. //m_key->key(oldKey);
  83. m_key->setOffset(oldOffset);
  84. }
  85. if (emitSignal) {
  86. emit keyChanged(m_key);
  87. }
  88. }
  89. /** Returns the key of this kechooser. */
  90. CSwordKey* const CBookKeyChooser::key() {
  91. return m_key;
  92. }
  93. /** Sets another module to this keychooser */
  94. void CBookKeyChooser::setModules(const ListCSwordModuleInfo& modules, const bool refresh) {
  95. m_modules.clear();
  96. // for (modules.first(); modules.current(); modules.next()) {
  97. ListCSwordModuleInfo::const_iterator end_it = modules.end();
  98. for (ListCSwordModuleInfo::const_iterator it(modules.begin()); it != end_it; ++it) {
  99. if ( (*it)->type() == CSwordModuleInfo::GenericBook ) {
  100. if (CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(*it)) {
  101. m_modules.append(book);
  102. }
  103. }
  104. }
  105. //refresh the number of combos
  106. if (refresh && m_modules.count() && m_key) {
  107. if (!m_layout) {
  108. m_layout = new TQHBoxLayout(this);
  109. }
  110. //delete old widgets
  111. m_chooserWidgets.setAutoDelete(true);
  112. m_chooserWidgets.clear();
  113. m_chooserWidgets.setAutoDelete(false);
  114. for (int i = 0; i < m_modules.first()->depth(); ++i) {
  115. // Create an empty keychooser, don't handle next/prev signals
  116. CKeyChooserWidget* w = new CKeyChooserWidget(0, false, this);
  117. m_chooserWidgets.append( w );
  118. //don't allow a too high width, try to keep as narrow as possible
  119. //to aid users with smaller screen resolutions
  120. int totalWidth = 200; //only 1 level
  121. if (m_modules.first()->depth() > 1) {
  122. if (m_modules.first()->depth() > 3)
  123. totalWidth = 400; //4+ levels
  124. else
  125. totalWidth = 300; //2-3 levels
  126. }
  127. int maxWidth = (int) ((float) totalWidth / (float) m_modules.first()->depth());
  128. w->comboBox()->setMaximumWidth(maxWidth);
  129. w->comboBox()->setCurrentItem(0);
  130. connect(w, TQT_SIGNAL(changed(int)), TQT_SLOT(keyChooserChanged(int)));
  131. connect(w, TQT_SIGNAL(focusOut(int)), TQT_SLOT(keyChooserChanged(int)));
  132. m_layout->addWidget(w);
  133. boxes[TQT_TQOBJECT(w)] = i;
  134. w->show();
  135. }
  136. //set the tab order of the key chooser widgets
  137. CKeyChooserWidget* chooser = 0;
  138. CKeyChooserWidget* chooser_prev = 0;
  139. const int count = m_chooserWidgets.count();
  140. for (int i = 0; i < count; ++i) {
  141. chooser = m_chooserWidgets.at(i);
  142. Q_ASSERT(chooser);
  143. if (chooser && chooser_prev) {
  144. TQWidget::setTabOrder(chooser_prev, chooser);
  145. }
  146. chooser_prev = chooser;
  147. }
  148. TQWidget::setTabOrder(chooser, 0);
  149. updateKey(m_key);
  150. adjustFont(); // only when refresh is set.
  151. }
  152. }
  153. /** No descriptions */
  154. void CBookKeyChooser::adjustFont() {
  155. //Make sure the entries are displayed correctly.
  156. for ( CKeyChooserWidget* idx = m_chooserWidgets.first(); idx; idx = m_chooserWidgets.next() ) {
  157. idx->comboBox()->setFont( CBTConfig::get
  158. ( m_modules.first()->language() ).second );
  159. }
  160. }
  161. /** Refreshes the content. */
  162. void CBookKeyChooser::refreshContent() {
  163. if (m_key) {
  164. updateKey( m_key ); //refresh with current key
  165. }
  166. }
  167. void CBookKeyChooser::setupCombo(const TQString key, const int depth, const int currentItem) {
  168. CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth);
  169. const unsigned long oldOffset = m_key->getOffset();
  170. m_key->key(key);
  171. if ((depth == 0) && chooserWidget && chooserWidget->comboBox()->count()) { //has already items
  172. //set now the right item
  173. if (CKeyChooserWidget* chooserWidget = m_chooserWidgets.at(depth)) {
  174. chooserWidget->setItem( chooserWidget->comboBox()->text(currentItem) );
  175. }
  176. m_key->setOffset(oldOffset);
  177. return;
  178. }
  179. //insert an empty item at the top
  180. TQStringList items;
  181. if (depth > 0) {
  182. items << TQString();
  183. }
  184. do {
  185. //items << TQString::fromLocal8Bit(m_key->getLocalName());
  186. items << m_key->getLocalNameUnicode();
  187. }
  188. while (m_key->nextSibling());
  189. if (chooserWidget) {
  190. chooserWidget->reset(items,currentItem,false);
  191. }
  192. //restore old key
  193. // m_key->key(oldKey);
  194. m_key->setOffset( oldOffset );
  195. }
  196. /** A keychooser changed. Update and emit a signal if necessary. */
  197. void CBookKeyChooser::keyChooserChanged(int /*newIndex*/) {
  198. const int activeID = boxes[TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))]; //no so good code!
  199. TQStringList items;
  200. CKeyChooserWidget* chooser;
  201. const int count = m_chooserWidgets.count();
  202. for (int i = 0; i < count; ++i) {
  203. chooser = m_chooserWidgets.at(i);
  204. const TQString currentText =
  205. (chooser && chooser->comboBox())
  206. ? chooser->comboBox()->currentText()
  207. : TQString();
  208. if (currentText.isEmpty() || i > activeID) {
  209. break;
  210. }
  211. items << currentText;
  212. }
  213. TQString newKey("/");
  214. newKey.append(items.join("/"));
  215. if (newKey.length() > 1) {
  216. newKey.remove(newKey.length(),1); //remove the traling slash
  217. }
  218. // tqWarning("key changed: setting to %s", newKey.latin1());
  219. m_key->key(newKey);
  220. setKey(m_key);
  221. }
  222. /** Updates the keychoosers for the given key but emit no signal. */
  223. void CBookKeyChooser::updateKey(CSwordKey* key) {
  224. setKey(key, false);
  225. }
  226. #include "cbookkeychooser.moc"