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.

155 lines
4.3KB

  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 "cmodulechooserbar.h"
  10. #include "cmodulechooserbutton.h"
  11. #include <tqtimer.h>
  12. CModuleChooserBar::CModuleChooserBar(ListCSwordModuleInfo useModules, CSwordModuleInfo::ModuleType type, TQWidget *parent, const char *name )
  13. : TDEToolBar(parent,name),
  14. m_moduleType(type),
  15. m_idCounter(0),
  16. m_buttonLimit(-1) //-1 means no limit
  17. {
  18. //insert buttons if useModules != 0
  19. // for (useModules.first(); useModules.current(); useModules.next()) {
  20. ListCSwordModuleInfo::iterator end_it = useModules.end();
  21. for (ListCSwordModuleInfo::iterator it(useModules.begin()); it != end_it; ++it) {
  22. if ((m_buttonLimit != -1) && ( m_buttonLimit <= (int)m_buttonList.count()) ) { //we reached the button limit
  23. break;
  24. };
  25. addButton( *it );
  26. }
  27. // We can add a button to choose an additional module
  28. if ( (m_buttonLimit == -1) || (m_buttonLimit > (int)m_buttonList.count()) ) {
  29. addButton(0); //add a button without module set
  30. }
  31. }
  32. /** Adds a button to the toolbar */
  33. CModuleChooserButton* const CModuleChooserBar::addButton( CSwordModuleInfo* const module ) {
  34. CModuleChooserButton* b = new CModuleChooserButton(module, m_moduleType, ++m_idCounter, this);
  35. insertWidget( m_idCounter, b->size().width(), b );
  36. m_buttonList.append(b);
  37. connect( b, TQT_SIGNAL(sigAddButton()), this, TQT_SLOT(addButton()) );
  38. connect( b, TQT_SIGNAL(sigRemoveButton(const int)), this, TQT_SLOT(removeButton(const int)) );
  39. connect( b, TQT_SIGNAL(sigChanged()), TQT_SIGNAL(sigChanged()) );
  40. connect( b, TQT_SIGNAL(sigChanged()), TQT_SLOT(updateMenuItems()) );
  41. b->show();
  42. updateMenuItems(); //make sure the items are up to date with the newest module list
  43. return b;
  44. }
  45. void CModuleChooserBar::addButton( ) {
  46. addButton(0);
  47. }
  48. /** Removes a button from the toolbar */
  49. void CModuleChooserBar::removeButton( const int ID ) {
  50. for (m_buttonList.first(); m_buttonList.current(); m_buttonList.next()) {
  51. if (m_buttonList.current()->getId() == ID) { //found the right button to remove
  52. CModuleChooserButton* b = m_buttonList.current();
  53. m_buttonList.remove(b);
  54. b->hide();
  55. b->deleteLater();
  56. break;
  57. }
  58. }
  59. emit sigChanged();
  60. updateMenuItems(); //make sure the items are up to date with the newest module list
  61. }
  62. /** Returns a list of selected modules. */
  63. ListCSwordModuleInfo CModuleChooserBar::getModuleList() {
  64. const int currentItemIndex = m_buttonList.at(); //make sure we don't change the current list item
  65. ListCSwordModuleInfo list;
  66. CSwordModuleInfo* m = 0;
  67. for ( m_buttonList.first(); m_buttonList.current(); m_buttonList.next()) {
  68. m = m_buttonList.current()->module();
  69. if ( m ) {
  70. list.append( m );
  71. }
  72. }
  73. m_buttonList.at( currentItemIndex );
  74. return list;
  75. }
  76. /** Sets the number of the maximum count of buttons. */
  77. void CModuleChooserBar::setButtonLimit(const int limit) {
  78. m_buttonLimit = limit;
  79. if (limit == -1) //no need to delete buttons
  80. return;
  81. const int tooMuch = m_buttonList.count() - limit;
  82. for (int i = 0; i < tooMuch; ++i) {
  83. CModuleChooserButton* b = m_buttonList.getLast();
  84. m_buttonList.remove(b);
  85. b->hide();
  86. delete b;
  87. }
  88. updateMenuItems();
  89. }
  90. /** Sets the modules which are chosen in this module chooser bar. */
  91. void CModuleChooserBar::setModules( ListCSwordModuleInfo useModules ) {
  92. setButtonLimit(0);
  93. setButtonLimit(-1); //these two lines clear the bar
  94. if (!useModules.count()) {
  95. return;
  96. }
  97. // for (useModules.first(); useModules.current(); useModules.next()) {
  98. ListCSwordModuleInfo::iterator end_it = useModules.end();
  99. for (ListCSwordModuleInfo::iterator it(useModules.begin()); it != end_it; ++it) {
  100. if ( (m_buttonLimit != -1) && (m_buttonLimit <= (int)m_buttonList.count()) ) {
  101. break;
  102. }
  103. addButton( *it );
  104. }
  105. if ( (m_buttonLimit == -1) || (m_buttonLimit > (int)m_buttonList.count()) ) {
  106. addButton(0);//add button without module set
  107. }
  108. updateMenuItems();
  109. }
  110. /*!
  111. \fn CModuleChooserBar::updateMenuItems()
  112. */
  113. void CModuleChooserBar::updateMenuItems() {
  114. TQPtrList<CModuleChooserButton> buttons = m_buttonList ;
  115. for (buttons.first(); buttons.current(); buttons.next()) {
  116. buttons.current()->updateMenuItems();
  117. }
  118. }
  119. #include "cmodulechooserbar.moc"