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.

cbooktreechooser.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "cbooktreechooser.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 <tqheader.h>
  16. #include <tqlistview.h>
  17. #include <tqcursor.h>
  18. //KDE includes
  19. #include <tdeapplication.h>
  20. ////////////
  21. CBookTreeChooser::TreeItem::TreeItem(TQListViewItem* parent, TQListViewItem* after, CSwordTreeKey* key, const TQString keyName)
  22. : TDEListViewItem(parent, after),
  23. m_key(key),
  24. m_keyName(keyName) {
  25. //tqDebug("first CBookTreeChooser::TreeItem ctor");
  26. const unsigned long offset = m_key->getOffset();
  27. m_key->key(m_keyName);
  28. //setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
  29. setText(0, m_key->getLocalNameUnicode() );
  30. m_key->setOffset( offset );
  31. };
  32. CBookTreeChooser::TreeItem::TreeItem(TQListViewItem* parent,CSwordTreeKey* key, const TQString keyName)
  33. : TDEListViewItem(parent),
  34. m_key(key),
  35. m_keyName(keyName) {
  36. //tqDebug("second CBookTreeChooser::TreeItem ctor");
  37. const unsigned int offset = m_key->getOffset();
  38. m_key->key(m_keyName);
  39. //setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
  40. setText(0, m_key->getLocalNameUnicode() );
  41. m_key->setOffset( offset );
  42. };
  43. CBookTreeChooser::TreeItem::TreeItem(TQListView* view, TQListViewItem* after,CSwordTreeKey* key, const TQString keyName)
  44. : TDEListViewItem(view,after),
  45. m_key(key),
  46. m_keyName(keyName) {
  47. //tqDebug("third CBookTreeChooser::TreeItem ctor");
  48. const unsigned int offset = m_key->getOffset();
  49. m_key->key(m_keyName);
  50. //setText(0, TQString::fromLocal8Bit(m_key->getLocalName()) );
  51. setText(0, m_key->getLocalNameUnicode() );
  52. m_key->setOffset( offset );
  53. };
  54. const TQString& CBookTreeChooser::TreeItem::key() const {
  55. return m_keyName;
  56. };
  57. /** Initializes this item with the correct caption. */
  58. void CBookTreeChooser::TreeItem::setup() {
  59. const unsigned int offset = m_key->getOffset();
  60. m_key->key(m_keyName);
  61. setExpandable(m_key->hasChildren());
  62. m_key->setOffset( offset );
  63. TDEListViewItem::setup();
  64. }
  65. void CBookTreeChooser::TreeItem::createChilds() {
  66. //make sure that we don't change the status of the key!
  67. const unsigned long offset = m_key->getOffset();
  68. m_key->key(m_keyName);
  69. if (m_key->hasChildren()) {
  70. m_key->firstChild(); //go to the first child
  71. TQListViewItem* oldItem = 0;
  72. do {
  73. if (oldItem) {
  74. oldItem = new TreeItem(this, oldItem, m_key, m_key->key());
  75. }
  76. else {
  77. oldItem = new TreeItem(this, m_key, m_key->key());
  78. }
  79. // oldItem->setExpandable( m_key->hasChildren() );
  80. }
  81. while (m_key->nextSibling());
  82. }
  83. m_key->setOffset( offset ); //restore the old state
  84. }
  85. void CBookTreeChooser::TreeItem::setOpen(bool o) {
  86. //setup the tree under this item
  87. if ((!isOpen() && o) && childCount() == 0) {
  88. listView()->viewport()->setCursor(WaitCursor);
  89. createChilds();
  90. listView()->viewport()->unsetCursor();
  91. }
  92. TDEListViewItem::setOpen(o);
  93. }
  94. //////////////////////////////////
  95. CBookTreeChooser::CBookTreeChooser(ListCSwordModuleInfo modules, CSwordKey *key, TQWidget *parent, const char *name)
  96. : CKeyChooser(modules, key, parent,name),
  97. m_key( dynamic_cast<CSwordTreeKey*>(key) ) {
  98. setModules(modules, false);
  99. if (!modules.count()) {
  100. m_modules.clear();
  101. m_key = 0;
  102. }
  103. //now setup the keychooser widgets
  104. TQHBoxLayout* layout = new TQHBoxLayout(this);
  105. m_treeView = new TDEListView(this);
  106. layout->addWidget(m_treeView);
  107. m_treeView->addColumn("Tree");
  108. m_treeView->header()->hide();
  109. m_treeView->setSorting(-1);
  110. m_treeView->setRootIsDecorated(true);
  111. m_treeView->setFullWidth(true);
  112. connect(m_treeView, TQT_SIGNAL(executed(TQListViewItem*)), TQT_SLOT(itemActivated(TQListViewItem*)));
  113. setKey(key);
  114. adjustFont();
  115. }
  116. CBookTreeChooser::~CBookTreeChooser() {}
  117. /** Sets a new key to this keychooser */
  118. void CBookTreeChooser::setKey(CSwordKey* key/*newKey*/) {
  119. setKey(key, false);
  120. }
  121. /** Sets a new key to this keychooser */
  122. void CBookTreeChooser::setKey(CSwordKey* newKey, const bool emitSignal) {
  123. if (m_key != newKey ) {
  124. m_key = dynamic_cast<CSwordTreeKey*>(newKey);
  125. }
  126. const TQString key = m_key->key();
  127. TQStringList siblings;
  128. if (m_key && !key.isEmpty()) {
  129. siblings = TQStringList::split("/",key,false);
  130. }
  131. //find the right listview item
  132. const int count = siblings.count();
  133. int index = 0;
  134. TQString currentSibling = siblings[index];
  135. TQListViewItem* child = m_treeView->firstChild();
  136. while( child && index < count ) {
  137. if (child->text(0) == currentSibling) { //found a parent of our item
  138. //found right entry?
  139. TreeItem* i = dynamic_cast<TreeItem*>(child);
  140. if (!i || i->key() == key) {
  141. break;
  142. }
  143. i->setOpen(true); //automatically creates childs
  144. child = i->firstChild();
  145. currentSibling = siblings[++index];
  146. }
  147. else {
  148. child = child->nextSibling();
  149. }
  150. }
  151. m_treeView->setCurrentItem( child );
  152. m_treeView->setSelected( child, true );
  153. m_treeView->ensureItemVisible(child);
  154. if (emitSignal) {
  155. emit keyChanged(m_key);
  156. }
  157. }
  158. /** Returns the key of this kechooser. */
  159. CSwordKey* const CBookTreeChooser::key() {
  160. return m_key;
  161. }
  162. /** Sets another module to this keychooser */
  163. void CBookTreeChooser::setModules(const ListCSwordModuleInfo& modules, const bool refresh) {
  164. m_modules.clear();
  165. // for (modules.first(); modules.current(); modules.next()) {
  166. ListCSwordModuleInfo::const_iterator end_it = modules.end();
  167. for (ListCSwordModuleInfo::const_iterator it(modules.begin()); it != end_it; ++it) {
  168. if (CSwordBookModuleInfo* book = dynamic_cast<CSwordBookModuleInfo*>(*it)) {
  169. m_modules.append(book);
  170. }
  171. }
  172. if (refresh && m_modules.count() && m_key) {
  173. const uint offset = m_key->getOffset();
  174. setupTree();
  175. m_key->setOffset( offset );
  176. /* m_key->root();
  177. m_key->firstChild();
  178. setKey(m_key, true);*/
  179. // m_treeView->clear();
  180. //
  181. // const TQString oldKey = m_key->key();
  182. // m_key->root();
  183. // m_key->firstChild();
  184. // setupTree(0,0,m_key);
  185. //
  186. // m_key->key(oldKey);
  187. //
  188. // updateKey(m_key);
  189. adjustFont(); //only when refresh is set.
  190. }
  191. }
  192. /** No descriptions */
  193. void CBookTreeChooser::adjustFont() {
  194. //Make sure the entries are displayed correctly.
  195. // if ( m_modules.first()->isUnicode() ){
  196. m_treeView->setFont( CBTConfig::get
  197. (m_modules.first()->language()).second );
  198. // tqWarning("Unicode Book detected");
  199. // }
  200. // else{
  201. // m_treeView->setFont( CBTConfig::get(CBTConfig::standard) );
  202. // tqWarning("Standard Book detected");
  203. // }
  204. }
  205. /** Refreshes the content. */
  206. void CBookTreeChooser::refreshContent() {
  207. if (m_key) {
  208. updateKey( m_key ); //refresh with current key
  209. }
  210. }
  211. /** No descriptions */
  212. void CBookTreeChooser::itemActivated( TQListViewItem* item ) {
  213. TreeItem* i = dynamic_cast<TreeItem*>(item);
  214. if (!i) {
  215. return;
  216. }
  217. m_key->key( i->key() );
  218. if (i->childCount() > 0 || i->isExpandable()) {
  219. i->setOpen(true);
  220. }
  221. emit keyChanged(m_key);
  222. }
  223. /** No descriptions */
  224. void CBookTreeChooser::updateKey( CSwordKey* key ) {
  225. setKey(key, false);
  226. }
  227. /** Reimplementationm to handle tree creation on show. */
  228. void CBookTreeChooser::show() {
  229. CKeyChooser::show();
  230. if (!m_treeView->childCount()) {
  231. TDEApplication::setOverrideCursor(WaitCursor);
  232. setupTree(); //create the first level of the tree structure
  233. m_treeView->resize(m_treeView->sizeHint());
  234. TDEApplication::restoreOverrideCursor();
  235. }
  236. }
  237. /** Creates the first level of the tree structure. */
  238. void CBookTreeChooser::setupTree() {
  239. m_treeView->clear();
  240. const unsigned long offset = m_key->getOffset();
  241. m_key->root();
  242. if (m_key->hasChildren()) {
  243. TQListViewItem* item = 0;
  244. m_key->firstChild();
  245. do {
  246. //the TreeItem constructor doesn't change the state of the key
  247. item = new TreeItem(m_treeView, item, m_key, m_key->key());
  248. }
  249. while (m_key->nextSibling());
  250. }
  251. /* m_key->root();
  252. m_key->firstChild();*/
  253. m_key->setOffset( offset );
  254. setKey(m_key, false); //the module may have changed
  255. }
  256. #include "cbooktreechooser.moc"