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.

290 lines
8.4KB

  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 "crangechooser.h"
  10. #include "backend/cswordkey.h"
  11. #include "backend/cswordversekey.h"
  12. #include "frontend/cbtconfig.h"
  13. #include "util/cresmgr.h"
  14. #include "util/ctoolclass.h"
  15. //TQt includes
  16. #include <tqhbox.h>
  17. #include <tqvbox.h>
  18. #include <tqptrlist.h>
  19. #include <tqpainter.h>
  20. #include <tqlayout.h>
  21. #include <tqmap.h>
  22. #include <tqlineedit.h>
  23. #include <tqtextedit.h>
  24. #include <tqlabel.h>
  25. #include <tqsizepolicy.h>
  26. #include <tqpushbutton.h>
  27. #include <tqheader.h>
  28. #include <tqregexp.h>
  29. #include <tqmessagebox.h>
  30. //KDE includes
  31. #include <tdeapplication.h>
  32. #include <tdefiledialog.h>
  33. #include <tdelocale.h>
  34. #include <kiconloader.h>
  35. namespace Search {
  36. namespace Options {
  37. /****************************/
  38. CRangeChooserDialog::RangeItem::RangeItem(TQListView* view, TQListViewItem* afterThis, const TQString caption, const TQString range) : TDEListViewItem(view, afterThis) {
  39. setCaption(caption);
  40. setRange(range);
  41. }
  42. CRangeChooserDialog::RangeItem::~RangeItem() {}
  43. const TQString& CRangeChooserDialog::RangeItem::range() {
  44. // tqWarning("ange is %s", (const char*)m_range.utf8());
  45. return m_range;
  46. }
  47. void CRangeChooserDialog::RangeItem::setRange(TQString newRange) {
  48. m_range = newRange;
  49. }
  50. const TQString CRangeChooserDialog::RangeItem::caption() {
  51. return text(0);
  52. }
  53. void CRangeChooserDialog::RangeItem::setCaption(const TQString newCaption) {
  54. setText(0, newCaption);
  55. }
  56. /**************************/
  57. CRangeChooserDialog::CRangeChooserDialog( TQWidget* parentDialog ) : KDialogBase(Plain, i18n("Search range editor"), Default | Ok | Cancel, Ok, parentDialog, "CRangeChooserDialog", false, true) {
  58. initView();
  59. initConnections();
  60. //add the existing scopes
  61. CBTConfig::StringMap map = CBTConfig::get
  62. (CBTConfig::searchScopes);
  63. CBTConfig::StringMap::Iterator it;
  64. for (it = map.begin(); it != map.end(); ++it) {
  65. new RangeItem(m_rangeList, 0, it.key(), it.data());
  66. };
  67. editRange(0);
  68. if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
  69. ) {
  70. nameChanged(i->caption());
  71. }
  72. }
  73. CRangeChooserDialog::~CRangeChooserDialog() {}
  74. /** Initializes the view of this object. */
  75. void CRangeChooserDialog::initView() {
  76. // setButtonOKText(i18n(""));
  77. TQGridLayout* grid = new TQGridLayout(plainPage(),6,5,0,3);
  78. m_rangeList = new TDEListView(plainPage());
  79. m_rangeList->addColumn(i18n("Search range"));
  80. m_rangeList->setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Expanding));
  81. m_rangeList->setFullWidth(true);
  82. m_rangeList->setSorting(0, true);
  83. m_rangeList->header()->setClickEnabled(false);
  84. m_rangeList->header()->setMovingEnabled(false);
  85. grid->addMultiCellWidget(m_rangeList,0,4,0,1);
  86. m_newRangeButton = new TQPushButton(i18n("Add new range"),plainPage());
  87. connect(m_newRangeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addNewRange()));
  88. grid->addWidget(m_newRangeButton,5,0);
  89. m_deleteRangeButton = new TQPushButton(i18n("Delete current range"),plainPage());
  90. connect(m_deleteRangeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteCurrentRange()));
  91. grid->addWidget(m_deleteRangeButton,5,1);
  92. grid->addColSpacing(2, 5);
  93. TQLabel* label = new TQLabel(i18n("Name:"), plainPage());
  94. m_nameEdit = new TQLineEdit(plainPage());
  95. grid->addWidget(label,0,3);
  96. grid->addWidget(m_nameEdit,0,4);
  97. label = new TQLabel(i18n("Edit current search range:"), plainPage());
  98. label->setFixedSize(label->sizeHint());
  99. m_rangeEdit = new TQTextEdit(plainPage());
  100. m_rangeEdit->setTextFormat(TQt::PlainText);
  101. grid->addMultiCellWidget(label,1,1,3,4);
  102. grid->addMultiCellWidget(m_rangeEdit,2,2,3,4);
  103. grid->addRowSpacing(3, 10);
  104. m_resultList = new TDEListView(plainPage());
  105. m_resultList->addColumn(i18n("Parsed search range:"));
  106. m_resultList->setFullWidth(true);
  107. m_resultList->setSorting(-1);
  108. m_resultList->setShowSortIndicator(false);
  109. m_resultList->header()->setClickEnabled(false);
  110. m_resultList->header()->setMovingEnabled(false);
  111. m_resultList->setSelectionModeExt(TDEListView::NoSelection);
  112. grid->addMultiCellWidget(m_resultList, 4,5,3,4);
  113. grid->setRowStretch(4,5);
  114. }
  115. /** Initializes the connections of this widget. */
  116. void CRangeChooserDialog::initConnections() {
  117. connect(m_rangeList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
  118. this, TQT_SLOT(editRange(TQListViewItem*)));
  119. connect(m_rangeEdit, TQT_SIGNAL(textChanged()),
  120. this, TQT_SLOT(parseRange()));
  121. connect(m_rangeEdit, TQT_SIGNAL(textChanged()),
  122. this, TQT_SLOT(rangeChanged()));
  123. connect(m_nameEdit, TQT_SIGNAL(textChanged(const TQString&)),
  124. this, TQT_SLOT(nameChanged(const TQString&)));
  125. }
  126. /** Adds a new range to the list. */
  127. void CRangeChooserDialog::addNewRange() {
  128. RangeItem* i = new RangeItem(m_rangeList, m_rangeList->lastItem(), i18n("New range"));
  129. m_rangeList->setSelected(i, true);
  130. m_rangeList->setCurrentItem(i);
  131. editRange(i);
  132. m_nameEdit->setFocus();
  133. }
  134. /** No descriptions */
  135. void CRangeChooserDialog::editRange(TQListViewItem* item) {
  136. RangeItem* const range = dynamic_cast<RangeItem*>(item);
  137. m_nameEdit->setEnabled( range ); //only if an item is selected enable the edit part
  138. m_rangeEdit->setEnabled( range );
  139. m_resultList->setEnabled( range );
  140. m_deleteRangeButton->setEnabled( range );
  141. if (range) {
  142. m_nameEdit->setText(range->caption());
  143. m_rangeEdit->setText(range->range());
  144. }
  145. }
  146. /** Parses the entered text and prints out the result in the list box below the edit area. */
  147. void CRangeChooserDialog::parseRange() {
  148. m_resultList->clear();
  149. //hack: repair range to work with Sword 1.5.6
  150. TQString range( m_rangeEdit->text() );
  151. range.replace(TQRegExp("\\s{0,}-\\s{0,}"), "-" );
  152. sword::VerseKey key;
  153. sword::ListKey verses = key.ParseVerseList((const char*)range.utf8(), "Genesis 1:1", true);
  154. for (int i = 0; i < verses.Count(); ++i) {
  155. new TDEListViewItem(m_resultList, TQString::fromUtf8(verses.GetElement(i)->getRangeText()));
  156. // tqWarning("range=%s, text=%s",verses.GetElement(i)->getRangeText(), verses.GetElement(i)->getText() );
  157. }
  158. }
  159. /** No descriptions */
  160. void CRangeChooserDialog::rangeChanged() {
  161. if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
  162. ) {
  163. TQString range( m_rangeEdit->text() );
  164. //hack: repair range to work with Sword 1.5.6
  165. range.replace(TQRegExp("\\s{0,}-\\s{0,}"), "-" );
  166. i->setRange(range);
  167. };
  168. }
  169. /** No descriptions */
  170. void CRangeChooserDialog::nameChanged(const TQString& newCaption) {
  171. m_rangeEdit->setEnabled(!newCaption.isEmpty());
  172. m_resultList->setEnabled(!newCaption.isEmpty());
  173. m_resultList->header()->setEnabled(!newCaption.isEmpty());
  174. if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
  175. ) {
  176. if (!newCaption.isEmpty()) {
  177. m_newRangeButton->setEnabled(true);
  178. i->setCaption(newCaption);
  179. m_rangeList->sort();
  180. }
  181. else { //invalid name
  182. i->setCaption(i18n("<invalid name of search range>"));
  183. m_newRangeButton->setEnabled(false);
  184. };
  185. };
  186. }
  187. /** Deletes the selected range. */
  188. void CRangeChooserDialog::deleteCurrentRange() {
  189. if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
  190. ) {
  191. if (TQListViewItem* selection = i->itemBelow() ? i->itemBelow() : i->itemAbove()) {
  192. m_rangeList->setSelected(selection, true);
  193. m_rangeList->setCurrentItem(selection);
  194. }
  195. else {
  196. m_rangeList->setSelected(m_rangeList->firstChild(), true);
  197. m_rangeList->setCurrentItem(m_rangeList->firstChild());
  198. }
  199. delete i;
  200. }
  201. editRange(m_rangeList->currentItem());
  202. }
  203. void CRangeChooserDialog::slotOk() {
  204. //save the new map of search scopes
  205. CBTConfig::StringMap map;
  206. TQListViewItemIterator it( m_rangeList );
  207. for (;it.current(); ++it) {
  208. if ( RangeItem* i = dynamic_cast<RangeItem*>(it.current()) ) {
  209. map[i->caption()] = i->range();
  210. };
  211. };
  212. CBTConfig::set
  213. (CBTConfig::searchScopes, map);
  214. KDialogBase::slotOk();
  215. }
  216. void CRangeChooserDialog::slotDefault() {
  217. m_rangeList->clear();
  218. CBTConfig::StringMap map = CBTConfig::getDefault(CBTConfig::searchScopes);
  219. CBTConfig::StringMap::Iterator it;
  220. for (it = map.begin(); it != map.end(); ++it) {
  221. new RangeItem(m_rangeList, 0, it.key(), it.data());
  222. };
  223. m_rangeList->setSelected(m_rangeList->selectedItem(), false);
  224. m_rangeList->setCurrentItem(0);
  225. editRange(0);
  226. if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
  227. ) {
  228. nameChanged(i->caption());
  229. }
  230. KDialogBase::slotDefault();
  231. }
  232. } //end of namespace Search::Options
  233. } //end of namespace Search
  234. #include "crangechooser.moc"