summaryrefslogtreecommitdiffstats
path: root/bibletime/frontend/searchdialog/crangechooser.cpp
blob: d9d5456156f8f670211420145db68bf98f7587ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/



#include "crangechooser.h"

#include "backend/cswordkey.h"
#include "backend/cswordversekey.h"

#include "frontend/cbtconfig.h"

#include "util/cresmgr.h"
#include "util/ctoolclass.h"

//TQt includes
#include <tqhbox.h>
#include <tqvbox.h>
#include <tqptrlist.h>
#include <tqpainter.h>
#include <tqlayout.h>
#include <tqmap.h>
#include <tqlineedit.h>
#include <tqtextedit.h>
#include <tqlabel.h>
#include <tqsizepolicy.h>
#include <tqpushbutton.h>
#include <tqheader.h>
#include <tqregexp.h>
#include <tqmessagebox.h>

//KDE includes
#include <tdeapplication.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <kiconloader.h>

namespace Search {
	namespace Options {

/****************************/
CRangeChooserDialog::RangeItem::RangeItem(TQListView* view, TQListViewItem* afterThis, const TQString caption, const TQString range) : TDEListViewItem(view, afterThis) {
	setCaption(caption);
	setRange(range);
}

CRangeChooserDialog::RangeItem::~RangeItem() {}

const TQString& CRangeChooserDialog::RangeItem::range() {
	//  tqWarning("ange is %s", (const char*)m_range.utf8());
	return m_range;
}

void CRangeChooserDialog::RangeItem::setRange(TQString newRange) {
	m_range = newRange;
}

const TQString CRangeChooserDialog::RangeItem::caption() {
	return text(0);
}

void CRangeChooserDialog::RangeItem::setCaption(const TQString newCaption) {
	setText(0, newCaption);
}


/**************************/
CRangeChooserDialog::CRangeChooserDialog( TQWidget* parentDialog ) : KDialogBase(Plain, i18n("Search range editor"), Default | Ok | Cancel, Ok, parentDialog, "CRangeChooserDialog", false, true) {
	initView();
	initConnections();

	//add the existing scopes
	CBTConfig::StringMap map = CBTConfig::get
								   (CBTConfig::searchScopes);
	CBTConfig::StringMap::Iterator it;
	for (it = map.begin(); it != map.end(); ++it) {
		new RangeItem(m_rangeList, 0, it.key(), it.data());
	};

	editRange(0);
	if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
	   ) {
		nameChanged(i->caption());
	}
}

CRangeChooserDialog::~CRangeChooserDialog() {}

/** Initializes the view of this object. */
void CRangeChooserDialog::initView() {
	//  setButtonOKText(i18n(""));

	TQGridLayout* grid = new TQGridLayout(plainPage(),6,5,0,3);

	m_rangeList = new TDEListView(plainPage());
	m_rangeList->addColumn(i18n("Search range"));
	m_rangeList->setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Expanding));
	m_rangeList->setFullWidth(true);
	m_rangeList->setSorting(0, true);
	m_rangeList->header()->setClickEnabled(false);
	m_rangeList->header()->setMovingEnabled(false);
	grid->addMultiCellWidget(m_rangeList,0,4,0,1);

	m_newRangeButton = new TQPushButton(i18n("Add new range"),plainPage());
	connect(m_newRangeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addNewRange()));
	grid->addWidget(m_newRangeButton,5,0);

	m_deleteRangeButton = new TQPushButton(i18n("Delete current range"),plainPage());
	connect(m_deleteRangeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteCurrentRange()));
	grid->addWidget(m_deleteRangeButton,5,1);

	grid->addColSpacing(2, 5);

	TQLabel* label = new TQLabel(i18n("Name:"), plainPage());
	m_nameEdit = new TQLineEdit(plainPage());
	grid->addWidget(label,0,3);
	grid->addWidget(m_nameEdit,0,4);

	label = new TQLabel(i18n("Edit current search range:"), plainPage());
	label->setFixedSize(label->sizeHint());
	m_rangeEdit = new TQTextEdit(plainPage());
	m_rangeEdit->setTextFormat(TQt::PlainText);
	grid->addMultiCellWidget(label,1,1,3,4);
	grid->addMultiCellWidget(m_rangeEdit,2,2,3,4);

	grid->addRowSpacing(3, 10);

	m_resultList = new TDEListView(plainPage());
	m_resultList->addColumn(i18n("Parsed search range:"));
	m_resultList->setFullWidth(true);
	m_resultList->setSorting(-1);
	m_resultList->setShowSortIndicator(false);
	m_resultList->header()->setClickEnabled(false);
	m_resultList->header()->setMovingEnabled(false);
	m_resultList->setSelectionModeExt(TDEListView::NoSelection);

	grid->addMultiCellWidget(m_resultList, 4,5,3,4);

	grid->setRowStretch(4,5);
}

/** Initializes the connections of this widget. */
void CRangeChooserDialog::initConnections() {
	connect(m_rangeList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
			this, TQT_SLOT(editRange(TQListViewItem*)));

	connect(m_rangeEdit, TQT_SIGNAL(textChanged()),
			this, TQT_SLOT(parseRange()));
	connect(m_rangeEdit, TQT_SIGNAL(textChanged()),
			this, TQT_SLOT(rangeChanged()));

	connect(m_nameEdit, TQT_SIGNAL(textChanged(const TQString&)),
			this, TQT_SLOT(nameChanged(const TQString&)));
}

/** Adds a new range to the list. */
void CRangeChooserDialog::addNewRange() {
	RangeItem* i = new RangeItem(m_rangeList, m_rangeList->lastItem(), i18n("New range"));
	m_rangeList->setSelected(i, true);
	m_rangeList->setCurrentItem(i);
	editRange(i);

	m_nameEdit->setFocus();
}

/** No descriptions */
void CRangeChooserDialog::editRange(TQListViewItem* item) {
	RangeItem* const range = dynamic_cast<RangeItem*>(item);

	m_nameEdit->setEnabled( range ); //only if an item is selected enable the edit part
	m_rangeEdit->setEnabled( range );
	m_resultList->setEnabled( range );
	m_deleteRangeButton->setEnabled( range );

	if (range) {
		m_nameEdit->setText(range->caption());
		m_rangeEdit->setText(range->range());
	}
}

/** Parses the entered text and prints out the result in the list box below the edit area. */
void CRangeChooserDialog::parseRange() {
	m_resultList->clear();

	//hack: repair range to  work with Sword 1.5.6
	TQString range( m_rangeEdit->text() );
	range.replace(TQRegExp("\\s{0,}-\\s{0,}"), "-" );

	sword::VerseKey key;
	sword::ListKey verses = key.ParseVerseList((const char*)range.utf8(), "Genesis 1:1", true);
	for (int i = 0; i < verses.Count(); ++i) {
		new TDEListViewItem(m_resultList, TQString::fromUtf8(verses.GetElement(i)->getRangeText()));
		//    tqWarning("range=%s, text=%s",verses.GetElement(i)->getRangeText(), verses.GetElement(i)->getText() );
	}

}

/** No descriptions */
void CRangeChooserDialog::rangeChanged() {
	if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
	   ) {
		TQString range( m_rangeEdit->text() );
		//hack: repair range to work with Sword 1.5.6
		range.replace(TQRegExp("\\s{0,}-\\s{0,}"), "-" );
		i->setRange(range);
	};
}

/** No descriptions */
void CRangeChooserDialog::nameChanged(const TQString& newCaption) {
	m_rangeEdit->setEnabled(!newCaption.isEmpty());
	m_resultList->setEnabled(!newCaption.isEmpty());
	m_resultList->header()->setEnabled(!newCaption.isEmpty());

	if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
	   ) {
		if (!newCaption.isEmpty()) {
			m_newRangeButton->setEnabled(true);
			i->setCaption(newCaption);
			m_rangeList->sort();
		}
		else { //invalid name
			i->setCaption(i18n("<invalid name of search range>"));
			m_newRangeButton->setEnabled(false);
		};
	};
}

/** Deletes the selected range. */
void CRangeChooserDialog::deleteCurrentRange() {
	if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
	   ) {
		if (TQListViewItem* selection = i->itemBelow() ? i->itemBelow() : i->itemAbove()) {
			m_rangeList->setSelected(selection, true);
			m_rangeList->setCurrentItem(selection);
		}
		else {
			m_rangeList->setSelected(m_rangeList->firstChild(), true);
			m_rangeList->setCurrentItem(m_rangeList->firstChild());
		}
		delete i;
	}
	editRange(m_rangeList->currentItem());
}

void CRangeChooserDialog::slotOk() {
	//save the new map of search scopes
	CBTConfig::StringMap map;
	TQListViewItemIterator it( m_rangeList );
	for (;it.current(); ++it) {
		if ( RangeItem* i = dynamic_cast<RangeItem*>(it.current()) ) {
			map[i->caption()] = i->range();
		};
	};
	CBTConfig::set
		(CBTConfig::searchScopes, map);

	KDialogBase::slotOk();
}

void CRangeChooserDialog::slotDefault() {
	m_rangeList->clear();
	CBTConfig::StringMap map = CBTConfig::getDefault(CBTConfig::searchScopes);
	CBTConfig::StringMap::Iterator it;
	for (it = map.begin(); it != map.end(); ++it) {
		new RangeItem(m_rangeList, 0, it.key(), it.data());
	};
	m_rangeList->setSelected(m_rangeList->selectedItem(), false);
	m_rangeList->setCurrentItem(0);

	editRange(0);
	if (RangeItem* i = dynamic_cast<RangeItem*>(m_rangeList->currentItem())
	   ) {
		nameChanged(i->caption());
	}

	KDialogBase::slotDefault();
}
 
	} //end of namespace Search::Options
} //end of namespace Search