1 #include "bookbrowser.h"
2 #include "configure.h"
3 #include "ebcache.h"
4 #include "referencepopup.h"
5
6 #include <QApplication>
7 #include <QContextMenuEvent>
8 #include <QDebug>
9 #include <QMenu>
10
11 #include <eb/eb.h>
12
BookBrowser(QWidget * parent)13 BookBrowser::BookBrowser(QWidget *parent)
14 : QTextBrowser(parent)
15 {
16 setSearchPaths(QStringList() << EbCache::cachePath);
17 document()->setDefaultFont(CONF->browserFont);
18
19 connect(this, SIGNAL(selectionChanged()), SLOT(changeTextSelection()));
20 }
21
setSource(const QUrl & name)22 void BookBrowser::setSource(const QUrl &name)
23 {
24 const QString path = name.path();
25 const QStringList args = name.query().split('?');
26
27 if (path == "sound") {
28 // args[0] : wave file
29 emit soundRequested(args[0]);
30 } else if (path == "book" || path == "menu") {
31 // args[0] : book index
32 // args[1] : page
33 // args[2] : offset
34 if (args.count() == 3) {
35 const int index = args[0].toInt();
36 if (index >= bookList_.count()) {
37 qWarning() << "Invalid book index" << args[0];
38 emit statusRequested("ERROR: Invalid book index: " + args[0]);
39 return;
40 }
41 EB_Position pos;
42 pos.page = args[1].toInt();
43 pos.offset = args[2].toInt();
44
45 ReferencePopup *popup =
46 new ReferencePopup(bookList_[index], pos, this, path == "menu");
47 connect(popup->bookBrowser(), SIGNAL(statusRequested(QString)),
48 SIGNAL(statusRequested(QString)));
49 connect(popup->bookBrowser(),
50 SIGNAL(searchRequested(SearchDirection,QString)),
51 SIGNAL(searchRequested(SearchDirection,QString)));
52 connect(popup->bookBrowser(), SIGNAL(pasteRequested(QString)),
53 SIGNAL(pasteRequested(QString)));
54 connect(popup->bookBrowser(), SIGNAL(soundRequested(QString)),
55 SIGNAL(soundRequested(QString)));
56 connect(popup->bookBrowser(), SIGNAL(videoRequested(QString)),
57 SIGNAL(videoRequested(QString)));
58 connect(popup->bookBrowser(), SIGNAL(externalLinkRequested(QString)),
59 SIGNAL(externalLinkRequested(QString)));
60 connect(popup->bookBrowser(), SIGNAL(selectionRequested(QString)),
61 SIGNAL(selectionRequested(QString)));
62 popup->show();
63 } else {
64 qWarning() << "Invalid Reference Parameter" << args.count();
65 }
66 } else if (path == "mpeg") {
67 // args[0] : mpeg file
68 emit videoRequested(args[0]);
69 } else if (path == "close") {
70 parentWidget()->close();
71 } else {
72 qWarning() << "Invalid Command" << path;
73 }
74 }
75
fontSize() const76 int BookBrowser::fontSize() const
77 {
78 return document()->defaultFont().pointSize();
79 }
80
addBookList(Book * book)81 void BookBrowser::addBookList(Book *book)
82 {
83 bookList_ << book;
84 }
85
setBrowser(const QString & str)86 void BookBrowser::setBrowser(const QString &str)
87 {
88 QApplication::setOverrideCursor(Qt::WaitCursor);
89 setHtml(str);
90 QApplication::restoreOverrideCursor();
91 }
92
contextMenuEvent(QContextMenuEvent * event)93 void BookBrowser::contextMenuEvent(QContextMenuEvent* event)
94 {
95 QMenu *menu = createStandardContextMenu();
96
97 //if (textCursor().hasSelection() &&
98 // parent()->parent()->objectName() == "main_browser") {
99 if (textCursor().hasSelection()) {
100 menu->addSeparator();
101 addDirectionMenu(menu);
102 menu->addSeparator();
103 menu->addAction(QObject::tr("&Paste selected string to edit line"),
104 this, SLOT(pasteSearchText()));
105 }
106 QAction *a = menu->exec(event->globalPos());
107 if (a && a->data().isValid()){
108 // qDebug() << a->data().typeName();
109 SearchDirection d = (SearchDirection)a->data().toInt();
110 if (d <= MenuRead) {
111 emit searchRequested(d, textCursor().selectedText());
112 } else {
113 QString addr;
114 if (d == GoogleSearch) {
115 addr = CONF->googleUrl;
116 } else if (d == WikipediaSearch) {
117 addr = CONF->wikipediaUrl;
118 } else if (d == Option1Search) {
119 addr = CONF->userDefUrl;
120 } else {
121 qWarning() << "Selected Invalid Menu?";
122 delete menu;
123 return;
124 }
125 foreach(const char c, textCursor().selectedText().toUtf8()) {
126 addr += "%" + QString::number((ushort)((uchar)c), 16);
127 }
128 emit externalLinkRequested(addr);
129 }
130 }
131 delete menu;
132 }
133
134 // Implement for linux.
135 // For "selectionChanged" SIGNAL not allways invoked at mouse move and
136 // release timing.
mouseReleaseEvent(QMouseEvent * ev)137 void BookBrowser::mouseReleaseEvent(QMouseEvent *ev)
138 {
139 if (ev->button() == Qt::LeftButton)
140 changeTextSelection();
141
142 QTextEdit::mouseReleaseEvent(ev);
143 }
144
changeTextSelection()145 void BookBrowser::changeTextSelection()
146 {
147 emit selectionRequested(textCursor().selectedText());
148 }
149
pasteSearchText()150 void BookBrowser::pasteSearchText()
151 {
152 emit pasteRequested(textCursor().selectedText());
153 }
154
155