summaryrefslogtreecommitdiff
path: root/src/frontend/display/bthtmlreaddisplay.cpp
blob: 7f016cd3dbd4bfbec67bc2b3526b24a132dc7c79 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2009 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "bthtmlreaddisplay.h"
#include "bthtmljsobject.h"
#include "frontend/displaywindow/cdisplaywindow.h"
#include "frontend/displaywindow/creadwindow.h"
#include "frontend/cdragdrop.h"
#include "frontend/cinfodisplay.h"
#include "backend/managers/creferencemanager.h"
#include "backend/keys/cswordkey.h"
#include "backend/config/cbtconfig.h"
#include "util/ctoolclass.h"
#include "util/cpointers.h"
#include "util/directoryutil.h"
#include <boost/scoped_ptr.hpp>
#include <QString>
#include <QMenu>

using namespace InfoDisplay;

void showBtHtmlFindText(CMDIArea*);

static const QString body = "</body>";
static const QString jsBegin = "<script  type=\"text/javascript\">";
static const QString jsEnd = "</script>";
static QString javascript; // Initialized from file bthtml.js


BtHtmlReadDisplay::BtHtmlReadDisplay(CReadWindow* readWindow, QWidget* parentWidget)
	: QWebPage(parentWidget),CReadDisplay(readWindow), m_magTimerId(0), m_view(0), m_jsObject(0)

{
	settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
	m_view = new BtHtmlReadDisplayView(this, parentWidget ? parentWidget : readWindow);
	m_view->setAcceptDrops(true);
	m_view->setPage(this);
	setParent(m_view);
	m_view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	m_view->setHtml("");
	initJavascript();
	bool ok = connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
	Q_ASSERT(ok);
}

BtHtmlReadDisplay::~BtHtmlReadDisplay() 
{
	setView(0);
}

// Read javascript into memory once and create the c++ javascript object
void BtHtmlReadDisplay::initJavascript()
{
	// read bthtml.js javascript file once
	if (javascript.size() == 0)
	{
		QString jsFile = util::filesystem::DirectoryUtil::getJavascriptDir().canonicalPath() + "/bthtml.js";
		QFile file(jsFile);	
 		if (file.open(QFile::ReadOnly)) 
		{
			while (!file.atEnd()) 
			{
				QByteArray line = file.readLine();
				javascript = javascript + line;
			}
			file.close();
		}
	}

	// Setup BtHtmlJsObject which will be called from javascript
	m_jsObject = new BtHtmlJsObject(this);
	m_jsObject->setObjectName("btHtmlJsObject");
}

// When the QWebFrame is cleared, this function is called to install the 
// javascript object (BtHtmlJsObject class) into the Javascript model
void BtHtmlReadDisplay::loadJSObject()
{
	mainFrame()->addToJavaScriptWindowObject(m_jsObject->objectName(), m_jsObject);
}

const QString BtHtmlReadDisplay::text( const CDisplay::TextType format, const CDisplay::TextPart part) 
{
	switch (part) 
	{
		case Document: 
		{
			if (format == HTMLText) 
			{
				return mainFrame()->toHtml();
			}
			else 
			{
				CDisplayWindow* window = parentWindow();
				CSwordKey* const key = window->key();
				CSwordModuleInfo* module = key->module();
				//This is never used for Bibles, so it is not implemented for
				//them.  If it should be, see CReadDisplay::print() for example
				//code.
				Q_ASSERT(module->type() == CSwordModuleInfo::Lexicon ||
						module->type() == CSwordModuleInfo::Commentary ||
						module->type() == CSwordModuleInfo::GenericBook);
				if (module->type() == CSwordModuleInfo::Lexicon ||
						module->type() == CSwordModuleInfo::Commentary ||
						module->type() == CSwordModuleInfo::GenericBook){
					//TODO: This is a BAD HACK, we have to fnd a better solution to manage the settings now
					CSwordBackend::FilterOptions filterOptions;
					filterOptions.footnotes = false;
					filterOptions.strongNumbers = false;
					filterOptions.morphTags = false;
					filterOptions.lemmas = false;
					filterOptions.scriptureReferences = false;
					filterOptions.textualVariants = false;

					CPointers::backend()->setFilterOptions(filterOptions);

					return QString(key->strippedText()).append("\n(")
						.append(key->key())
						.append(", ")
						.append(key->module()->name())
						.append(")");
				}
			}
		}

		case SelectedText: 
		{
			if (!hasSelection()) 
			{
				return QString::null;
			}
			else if (format == HTMLText) 
			{
				//	TODO: It does not appear this is ever called
			}
			else 
			{ //plain text requested
				return selectedText();
			}
		}

		case AnchorOnly: 
		{
			QString moduleName;
			QString keyName;
			CReferenceManager::Type type;
			CReferenceManager::decodeHyperlink(activeAnchor(), moduleName, keyName, type);

			return keyName;
		}

		case AnchorTextOnly: 
		{
			QString moduleName;
			QString keyName;
			CReferenceManager::Type type;
			CReferenceManager::decodeHyperlink(activeAnchor(), moduleName, keyName, type);

			if (CSwordModuleInfo* module = backend()->findModuleByName(moduleName)) 
			{
				boost::scoped_ptr<CSwordKey> key( CSwordKey::createInstance(module) );
				key->key( keyName );

				return key->strippedText();
			}
			return QString::null;
		}

		case AnchorWithText: 
		{
			QString moduleName;
			QString keyName;
			CReferenceManager::Type type;
			CReferenceManager::decodeHyperlink(activeAnchor(), moduleName, keyName, type);

			if (CSwordModuleInfo* module = backend()->findModuleByName(moduleName)) 
			{
				boost::scoped_ptr<CSwordKey> key( CSwordKey::createInstance(module) );
				key->key( keyName );

				//TODO: This is a BAD HACK, we have to fnd a better solution to manage the settings now
				CSwordBackend::FilterOptions filterOptions;
				filterOptions.footnotes = false;
				filterOptions.strongNumbers = false;
				filterOptions.morphTags = false;
				filterOptions.lemmas = false;
				filterOptions.scriptureReferences = false;
				filterOptions.textualVariants = false;

				CPointers::backend()->setFilterOptions(filterOptions);

				return QString(key->strippedText()).append("\n(")
					.append(key->key())
					.append(", ")
					.append(key->module()->name())
					.append(")");
				/*    ("%1\n(%2, %3)")
					.arg()
					.arg(key->key())
					.arg(key->module()->name());*/
			}
			return QString::null;
		}
		default:
		return QString::null;
	}
	return QString();
}

// Puts html text and javascript into QWebView
void BtHtmlReadDisplay::setText( const QString& newText ) 
{
	QString jsText = newText;
	jsText.replace(body,jsBegin+javascript+jsEnd+body);

	// Disconnect any previous connect and connect to slot that loads the javascript object
	QWebFrame* frame = mainFrame();
	disconnect(frame,SIGNAL(javaScriptWindowObjectCleared()),0,0);
	bool ok = connect(frame,SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(loadJSObject()));
	Q_ASSERT(ok);

	// Send text to the html viewer
	m_view->setHtml(jsText);
}

// See if any text is selected
bool BtHtmlReadDisplay::hasSelection() 
{
	if (selectedText().isEmpty())
		return false;
	return true;
}

// Reimplementation
// Returns the BtHtmlReadDisplayView object
QWidget* BtHtmlReadDisplay::view()
{
	return m_view;
}

// Select all text in the viewer
void BtHtmlReadDisplay::selectAll() 
{
	m_jsObject->setBodyEditable(true);
    m_view->triggerPageAction( QWebPage::MoveToStartOfDocument, true );
    m_view->triggerPageAction( QWebPage::SelectEndOfDocument,   true );
	m_jsObject->setBodyEditable(false);
}

// Scroll QWebView to the correct location as specified by the anchor
void BtHtmlReadDisplay::moveToAnchor( const QString& anchor ) 
{
	slotGoToAnchor(anchor);
}

// Scroll the QWebView to the correct location specified by anchor
void BtHtmlReadDisplay::slotGoToAnchor(const QString& anchor) 
{
	m_jsObject->moveToAnchor(anchor);
}

// Save the Lemma (Strongs number) attribute 
void BtHtmlReadDisplay::setLemma(const QString& lemma)
{
	m_nodeInfo[ CDisplay::Lemma ] = lemma;
}

// Open the Find text dialog
void BtHtmlReadDisplay::openFindTextDialog() 
{
	CMDIArea* mdiArea = parentWindow()->mdi();
	showBtHtmlFindText(mdiArea);
}

// Send "completed" signal when the text is finished loading into the viewer
void BtHtmlReadDisplay::slotLoadFinished(bool)
{
	emit completed();
}

// For debugging javascript
#if 0
void BtHtmlReadDisplay::javaScriptConsoleMessage (const QString& message, int lineNumber, const QString& sourceID )
{
}
#endif




// ----------------- BtHtmlReadDisplayView -------------------------------------

BtHtmlReadDisplayView::BtHtmlReadDisplayView(BtHtmlReadDisplay* displayWidget, QWidget* parent) 
	: QWebView(parent), m_display(displayWidget) 
{
}

BtHtmlReadDisplayView::~BtHtmlReadDisplayView()
	{
		setPage(0);
	}

// Create the right mouse context menus
void BtHtmlReadDisplayView::contextMenuEvent(QContextMenuEvent* event)
{
	if (QMenu* popup = m_display->installedPopup()) 
	{
		popup->exec(event->globalPos());
	}
}

// Reimplementation from QWidget
void BtHtmlReadDisplayView::dropEvent( QDropEvent* e ) 
{
	if (e->mimeData()->hasFormat("BibleTime/Bookmark")) 
	{
		//see docs for BTMimeData and QMimeData
		const QMimeData* mimedata = e->mimeData();
		if (mimedata != 0)
		{
			const BTMimeData* btmimedata = qobject_cast<const BTMimeData*>(mimedata);
			if (btmimedata != 0)
			{
				BookmarkItem item = (qobject_cast<const BTMimeData*>(e->mimeData()))->bookmark();
				m_display->connectionsProxy()->emitReferenceDropped(item.key());
				e->acceptProposedAction();
				return;
			}
		}
	};
	//don't accept the action!
	e->ignore();
}

// Reimplementation from QWebView
void BtHtmlReadDisplayView::dragEnterEvent( QDragEnterEvent* e ) 
{
	if (e->mimeData()->hasFormat("BibleTime/Bookmark")) 
	{
		e->acceptProposedAction();
		return;
	}
	//don't accept the action!
	e->ignore();
}

// Reimplementation from QWebView
void BtHtmlReadDisplayView::dragMoveEvent( QDragMoveEvent* e ) 
{
	if (e->mimeData()->hasFormat("BibleTime/Bookmark")) 
	{
		e->acceptProposedAction();
		return;
	}
	//don't accept the action!
	e->ignore();
}