| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* |
| 4 |
* Copyright (C) 2008 Alf. |
| 5 |
* |
| 6 |
* Contact: Alf <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.0 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public |
| 19 |
* License along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
/* @CPPFILE ApvlvWebViewWidget.cc |
| 24 |
* |
| 25 |
* Author: Alf <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <filesystem> |
| 29 |
#include <iostream> |
| 30 |
#include <sstream> |
| 31 |
|
| 32 |
#include <QClipboard> |
| 33 |
#include <QFile> |
| 34 |
#include <QInputDialog> |
| 35 |
#include <QWebEngineScriptCollection> |
| 36 |
#include <qevent.h> |
| 37 |
|
| 38 |
#include "ApvlvUtil.h" |
| 39 |
#include "ApvlvWebViewWidget.h" |
| 40 |
|
| 41 |
namespace apvlv |
| 42 |
{ |
| 43 |
using namespace std; |
| 44 |
|
| 45 |
void |
| 46 |
ApvlvSchemeHandler::requestStarted (QWebEngineUrlRequestJob *job) |
| 47 |
{ |
| 48 |
auto url = job->requestUrl (); |
| 49 |
auto path = url.path ().toStdString (); |
| 50 |
auto key = path.substr (1); |
| 51 |
auto mime = mFile->pathMimeType (key); |
| 52 |
auto roptcont = mFile->pathContent (key); |
| 53 |
if (!roptcont) |
| 54 |
{ |
| 55 |
job->fail (QWebEngineUrlRequestJob::UrlNotFound); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
mArray = std::move (roptcont.value ()); |
| 60 |
mBuffer.setData (mArray); |
| 61 |
job->reply (QByteArray (mime.c_str ()), &mBuffer); |
| 62 |
|
| 63 |
emit webpageUpdated (key); |
| 64 |
} |
| 65 |
|
| 66 |
WebView::WebView () |
| 67 |
{ |
| 68 |
mProfile.setHttpCacheType (QWebEngineProfile::NoCache); |
| 69 |
mProfile.installUrlSchemeHandler ("apvlv", &mSchemeHandler); |
| 70 |
mPage = make_unique<QWebEnginePage> (&mProfile); |
| 71 |
setPage (mPage.get ()); |
| 72 |
|
| 73 |
mCopyAction.setText (tr ("Copy")); |
| 74 |
QObject::connect (&mCopyAction, SIGNAL (triggered (bool)), this, |
| 75 |
SLOT (copy ())); |
| 76 |
mMenu.addAction (&mCopyAction); |
| 77 |
|
| 78 |
mUnderlineAction.setText (tr ("Underline")); |
| 79 |
QObject::connect (&mUnderlineAction, SIGNAL (triggered (bool)), this, |
| 80 |
SLOT (underline ())); |
| 81 |
mMenu.addAction (&mUnderlineAction); |
| 82 |
|
| 83 |
mCommentAction.setText (tr ("Comment")); |
| 84 |
QObject::connect (&mCommentAction, SIGNAL (triggered (bool)), this, |
| 85 |
SLOT (comment ())); |
| 86 |
mMenu.addAction (&mCommentAction); |
| 87 |
} |
| 88 |
|
| 89 |
WebViewWidget::WebViewWidget () |
| 90 |
{ |
| 91 |
QObject::connect (&mWebView, SIGNAL (loadFinished (bool)), this, |
| 92 |
SLOT (webviewLoadFinished (bool))); |
| 93 |
QObject::connect (&mWebView.mSchemeHandler, |
| 94 |
SIGNAL (webpageUpdated (const string &)), this, |
| 95 |
SLOT (webviewUpdate (const string &))); |
| 96 |
loadJavaScriptFromDir (ScriptDir); |
| 97 |
} |
| 98 |
|
| 99 |
bool |
| 100 |
WebViewWidget::loadJavaScriptFromDir (const std::string &dir) |
| 101 |
{ |
| 102 |
std::filesystem::path script_dir{ dir }; |
| 103 |
if (!is_directory (script_dir)) |
| 104 |
return false; |
| 105 |
|
| 106 |
QList<QWebEngineScript> script_list; |
| 107 |
for (auto &entry : std::filesystem::directory_iterator ( |
| 108 |
script_dir, |
| 109 |
std::filesystem::directory_options::follow_directory_symlink)) |
| 110 |
{ |
| 111 |
auto const &path = entry.path (); |
| 112 |
if (entry.is_regular_file () && path.extension () == ".js") |
| 113 |
{ |
| 114 |
QFile file (path); |
| 115 |
if (!file.open (QIODevice::ReadOnly | QIODevice::Text)) |
| 116 |
{ |
| 117 |
qWarning () << "open " << path.c_str () << "to read error"; |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
auto contents = file.readAll (); |
| 122 |
|
| 123 |
auto script = QWebEngineScript{}; |
| 124 |
script.setName (QString::fromStdString (path.filename ())); |
| 125 |
script.setSourceCode (contents); |
| 126 |
script.setInjectionPoint (QWebEngineScript::DocumentCreation); |
| 127 |
script.setWorldId (QWebEngineScript::MainWorld); |
| 128 |
script.setRunsOnSubFrames (true); |
| 129 |
script_list.push_back (script); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
auto page = mWebView.page (); |
| 134 |
page->scripts ().insert (script_list); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
WebViewWidget::showPage (int p, double s) |
| 140 |
{ |
| 141 |
mFile->pageRenderToWebView (p, mZoomrate, 0, &mWebView); |
| 142 |
mPageNumber = p; |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
WebViewWidget::showPage (int p, const string &anchor) |
| 147 |
{ |
| 148 |
mFile->pageRenderToWebView (p, mZoomrate, 0, &mWebView); |
| 149 |
mPageNumber = p; |
| 150 |
mAnchor = anchor; |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
WebViewWidget::scroll (int times, int h, int v) |
| 155 |
{ |
| 156 |
if (!mFile) |
| 157 |
return; |
| 158 |
|
| 159 |
auto scripts = QString("scrollByTimes(%1, %2, %3);").arg(times).arg(h).arg(v); |
| 160 |
auto page = mWebView.page (); |
| 161 |
page->runJavaScript (scripts); |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
WebViewWidget::scrollTo (double xrate, double yrate) |
| 166 |
{ |
| 167 |
if (!mFile) |
| 168 |
return; |
| 169 |
|
| 170 |
auto scripts = QString("scrollToPosition(%1, %2);").arg(xrate).arg(yrate); |
| 171 |
auto page = mWebView.page (); |
| 172 |
page->runJavaScript (scripts); |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
WebViewWidget::scrollUp (int times) |
| 177 |
{ |
| 178 |
scroll (times, 0, -50); |
| 179 |
|
| 180 |
if (mWebView.isScrolledToTop ()) |
| 181 |
{ |
| 182 |
if (mIsInternalScroll) |
| 183 |
{ |
| 184 |
auto scripts = QString("dispatchKeydownEvent(%1);").arg(37); |
| 185 |
auto page = mWebView.page (); |
| 186 |
page->runJavaScript (scripts); |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
auto p = mFile->pageNumberWrap (mPageNumber - 1); |
| 191 |
if (p >= 0) |
| 192 |
{ |
| 193 |
mIsScrollUp = true; |
| 194 |
showPage (p, 0.0); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
WebViewWidget::scrollDown (int times) |
| 202 |
{ |
| 203 |
scroll (times, 0, 50); |
| 204 |
|
| 205 |
if (mWebView.isScrolledToBottom ()) |
| 206 |
{ |
| 207 |
if (mIsInternalScroll) |
| 208 |
{ |
| 209 |
auto scripts = QString("dispatchKeydownEvent(%1);").arg(39); |
| 210 |
auto page = mWebView.page (); |
| 211 |
page->runJavaScript (scripts); |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
auto p = mFile->pageNumberWrap (mPageNumber + 1); |
| 216 |
if (p >= 0) |
| 217 |
showPage (p, 0.0); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
WebViewWidget::scrollLeft (int times) |
| 224 |
{ |
| 225 |
scroll (times, -50, 0); |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
WebViewWidget::scrollRight (int times) |
| 230 |
{ |
| 231 |
scroll (times, 50, 0); |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
WebViewWidget::setSearchStr (const string &str) |
| 236 |
{ |
| 237 |
auto qstr = QString::fromLocal8Bit (str); |
| 238 |
QWebEnginePage::FindFlags flags{}; |
| 239 |
mWebView.findText (qstr, flags); |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
WebViewWidget::setSearchSelect (int select) |
| 244 |
{ |
| 245 |
auto text = mWebView.selectedText (); |
| 246 |
QWebEnginePage::FindFlags flags{}; |
| 247 |
mWebView.findText (text, flags); |
| 248 |
mSearchSelect = select; |
| 249 |
} |
| 250 |
|
| 251 |
void |
| 252 |
WebViewWidget::setZoomrate (double zm) |
| 253 |
{ |
| 254 |
mWebView.setZoomFactor (zm); |
| 255 |
mZoomrate = zm; |
| 256 |
} |
| 257 |
|
| 258 |
bool |
| 259 |
WebView::isScrolledToTop () |
| 260 |
{ |
| 261 |
auto page = this->page (); |
| 262 |
auto p = page->scrollPosition (); |
| 263 |
return p.y () < 0.5; |
| 264 |
} |
| 265 |
|
| 266 |
bool |
| 267 |
WebView::isScrolledToBottom () |
| 268 |
{ |
| 269 |
auto page = this->page (); |
| 270 |
auto p = page->scrollPosition (); |
| 271 |
auto cs = page->contentsSize (); |
| 272 |
return p.y () + QWebEngineView::height () + 0.5 > cs.height (); |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
WebView::contextMenuEvent (QContextMenuEvent *event) |
| 277 |
{ |
| 278 |
qDebug () << "WebView::customeMenuRequest"; |
| 279 |
mMenu.popup (mapToGlobal (event->pos ())); |
| 280 |
} |
| 281 |
|
| 282 |
std::pair<int, int> |
| 283 |
WebView::getSelectionPosition () const |
| 284 |
{ |
| 285 |
int begin = -1; |
| 286 |
int end = -1; |
| 287 |
QEventLoop loop; |
| 288 |
mPage->runJavaScript ( |
| 289 |
"getSelectionOffset(0);", |
| 290 |
[&loop, &begin, &end] (const QVariant &result) { |
| 291 |
if (result.isValid () && result.typeId () == QMetaType::QVariantList) |
| 292 |
{ |
| 293 |
auto offsets = result.toList (); |
| 294 |
begin = offsets[0].toInt (); |
| 295 |
end = offsets[1].toInt (); |
| 296 |
qDebug () << "Begin offset:" << offsets[0].toInt (); |
| 297 |
qDebug () << "End offset:" << offsets[1].toInt (); |
| 298 |
loop.quit (); |
| 299 |
} |
| 300 |
}); |
| 301 |
loop.exec (); |
| 302 |
return std::make_pair (begin, end); |
| 303 |
} |
| 304 |
|
| 305 |
void |
| 306 |
WebView::underLinePosition (int begin, int end) |
| 307 |
{ |
| 308 |
qDebug () << "underLinePosition" << begin << " -> " << end; |
| 309 |
QString src = QString ("underlineByOffset(%1, %2);").arg (begin).arg (end); |
| 310 |
mPage->runJavaScript (src); |
| 311 |
} |
| 312 |
|
| 313 |
void |
| 314 |
WebView::copy () const |
| 315 |
{ |
| 316 |
qDebug () << "copy text"; |
| 317 |
auto text = mPage->selectedText (); |
| 318 |
auto clipboard = QGuiApplication::clipboard (); |
| 319 |
clipboard->setText (text); |
| 320 |
} |
| 321 |
|
| 322 |
void |
| 323 |
WebView::underline () |
| 324 |
{ |
| 325 |
qDebug () << "underline text"; |
| 326 |
auto text = mPage->selectedText (); |
| 327 |
if (text.isEmpty ()) |
| 328 |
{ |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
auto offset = getSelectionPosition (); |
| 333 |
if (offset.first < 0 || offset.second < 0) |
| 334 |
{ |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
auto path = mPage->url ().path ().toStdString (); |
| 339 |
auto file = mSchemeHandler.file (); |
| 340 |
auto note = file->getNote (); |
| 341 |
Comment comment; |
| 342 |
comment.quoteText = text.toStdString (); |
| 343 |
comment.begin.set (0, nullptr, offset.first, path); |
| 344 |
comment.end.set (0, nullptr, offset.second, path); |
| 345 |
note->addComment (comment); |
| 346 |
} |
| 347 |
|
| 348 |
void |
| 349 |
WebView::comment () |
| 350 |
{ |
| 351 |
qDebug () << "comment text"; |
| 352 |
do |
| 353 |
{ |
| 354 |
auto text = mPage->selectedText (); |
| 355 |
if (text.isEmpty ()) |
| 356 |
break; |
| 357 |
|
| 358 |
auto input_text = QInputDialog::getMultiLineText (this, tr ("Input"), |
| 359 |
tr ("Comment")); |
| 360 |
auto commentText = input_text.trimmed (); |
| 361 |
if (commentText.isEmpty ()) |
| 362 |
break; |
| 363 |
|
| 364 |
auto offset = getSelectionPosition (); |
| 365 |
if (offset.first < 0 || offset.second < 0) |
| 366 |
break; |
| 367 |
|
| 368 |
auto path = mPage->url ().path ().toStdString (); |
| 369 |
auto file = mSchemeHandler.file (); |
| 370 |
auto note = file->getNote (); |
| 371 |
Comment comment; |
| 372 |
comment.quoteText = text.toStdString (); |
| 373 |
comment.commentText = commentText.toStdString (); |
| 374 |
comment.begin.set (0, nullptr, offset.first, path); |
| 375 |
comment.end.set (0, nullptr, offset.second, path); |
| 376 |
note->addComment (comment); |
| 377 |
} |
| 378 |
while (false); |
| 379 |
} |
| 380 |
|
| 381 |
void |
| 382 |
WebViewWidget::setComment () |
| 383 |
{ |
| 384 |
auto note = mFile->getNote (); |
| 385 |
if (!note) |
| 386 |
return; |
| 387 |
auto path = mWebView.page ()->url ().path ().toStdString (); |
| 388 |
auto comments = note->getCommentsInPath (path); |
| 389 |
for (auto &comment : comments) |
| 390 |
{ |
| 391 |
auto begin_offset = comment.begin.offset; |
| 392 |
auto end_offset = comment.end.offset; |
| 393 |
mWebView.underLinePosition (begin_offset, end_offset); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
void |
| 398 |
WebViewWidget::webviewLoadFinished (bool suc) |
| 399 |
{ |
| 400 |
if (suc) |
| 401 |
{ |
| 402 |
if (!mAnchor.empty ()) |
| 403 |
{ |
| 404 |
auto page = mWebView.page (); |
| 405 |
auto scripts = QString("scrollToAnchor('%1');").arg(mAnchor.c_str()); |
| 406 |
page->runJavaScript (scripts); |
| 407 |
} |
| 408 |
else if (mIsScrollUp) |
| 409 |
{ |
| 410 |
scrollTo (0.0, 1.0); |
| 411 |
mIsScrollUp = false; |
| 412 |
} |
| 413 |
|
| 414 |
if (!mIsScrollUp) |
| 415 |
{ |
| 416 |
setComment (); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
WebViewWidget::webviewFindTextFinished (const QWebEngineFindTextResult &result) |
| 423 |
{ |
| 424 |
mSearchResult = result; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
// Local Variables: |
| 429 |
// mode: c++ |
| 430 |
// End: |