Show current page's url and load images option

This commit is contained in:
Gres 2020-03-06 20:08:41 +03:00
parent fd45105dc7
commit 86d5ee3aa6
8 changed files with 96 additions and 8 deletions

View File

@ -44,3 +44,8 @@ Also download [data files](https://github.com/tesseract-ocr/tessdata/tree/3.04.0
* see [Tesseract](https://code.google.com/p/tesseract-ocr/)
* see [Leptonica](http://leptonica.com/)
* several online translation services
## Attributions
* icons made by [Smashicons](https://www.flaticon.com/authors/smashicons)
from [Flaticon](https://www.flaticon.com/)

View File

@ -7,5 +7,7 @@
<file alias="st_success.png">share/images/STIconGreen.png</file>
<file alias="st_busy.png">share/images/STIconOrange.png</file>
<file alias="st_error.png">share/images/STIconRed.png</file>
<file alias="loadImages.png">share/images/loadImages.png</file>
<file alias="loadImages@2x.png">share/images/loadImages@2x.png</file>
</qresource>
</RCC>

BIN
share/images/loadImages.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -8,9 +8,12 @@
#include <QBoxLayout>
#include <QCloseEvent>
#include <QLabel>
#include <QLineEdit>
#include <QSplitter>
#include <QTabWidget>
#include <QTextEdit>
#include <QToolBar>
#include <unordered_set>
@ -32,6 +35,9 @@ static std::map<QString, QString> loadScripts(const QString &dir,
Translator::Translator(Manager &manager)
: manager_(manager)
, view_(nullptr)
, url_(new QLineEdit(this))
, loadImages_(
new QAction(QIcon(":/icons/loadImages.png"), tr("Load images"), this))
, tabs_(new QTabWidget(this))
{
#ifdef DEVELOP
@ -50,17 +56,35 @@ Translator::Translator(Manager &manager)
view_ = new QWebEngineView(this);
auto detailsFrame = new QWidget(this);
{
auto toolBar = new QToolBar(this);
toolBar->addWidget(new QLabel(tr("Url:"), this));
toolBar->addWidget(url_);
toolBar->addAction(loadImages_);
auto layout = new QVBoxLayout(detailsFrame);
layout->addWidget(toolBar);
layout->addWidget(tabs_);
}
auto splitter = new QSplitter(Qt::Vertical, this);
splitter->addWidget(view_);
splitter->addWidget(tabs_);
splitter->addWidget(detailsFrame);
auto layout = new QVBoxLayout(this);
layout->addWidget(splitter);
startTimer(1000);
url_->setReadOnly(true);
loadImages_->setCheckable(true);
connect(loadImages_, &QAction::toggled, //
this, &Translator::setPageLoadImages);
connect(tabs_, &QTabWidget::currentChanged, //
this, &Translator::changeCurrentPage);
this, &Translator::udpateCurrentPage);
view_->setMinimumSize(200, 200);
@ -80,6 +104,7 @@ void Translator::updateSettings(const Settings &settings)
{
view_->setPage(nullptr);
pages_.clear();
url_->clear();
tabs_->blockSignals(true);
for (auto i = 0, end = tabs_->count(); i < end; ++i) {
@ -114,6 +139,8 @@ void Translator::updateSettings(const Settings &settings)
connect(page.get(), &WebPage::log, //
log, &QTextEdit::append);
connect(page.get(), &WebPage::urlChanged, //
this, &Translator::updateUrl);
SOFT_ASSERT(log->document(), continue)
log->document()->setMaximumBlockCount(1000);
@ -126,11 +153,46 @@ void Translator::updateSettings(const Settings &settings)
}
}
void Translator::changeCurrentPage(int tabIndex)
WebPage *Translator::currentPage() const
{
const auto name = tabs_->tabText(tabIndex);
SOFT_ASSERT(pages_.count(name), return );
view_->setPage(pages_[name].get());
const auto index = tabs_->currentIndex();
if (index == -1)
return nullptr;
const auto name = tabs_->tabText(index);
SOFT_ASSERT(pages_.count(name), return nullptr);
return pages_.at(name).get();
}
void Translator::udpateCurrentPage()
{
auto page = currentPage();
if (!page)
return;
view_->setPage(page);
QSignalBlocker blocker(loadImages_);
loadImages_->setChecked(page->isLoadImages());
url_->setText(page->url().toString());
}
void Translator::updateUrl()
{
auto page = currentPage();
if (!page)
return;
url_->setText(page->url().toString());
}
void Translator::setPageLoadImages(bool isOn)
{
auto page = currentPage();
if (!page)
return;
page->setLoadImages(isOn);
}
void Translator::processQueue()

View File

@ -6,6 +6,7 @@
class QWebEngineView;
class QTabWidget;
class QLineEdit;
class WebPage;
@ -25,12 +26,17 @@ protected:
void closeEvent(QCloseEvent *event) override;
private:
void changeCurrentPage(int tabIndex);
WebPage *currentPage() const;
void udpateCurrentPage();
void updateUrl();
void setPageLoadImages(bool isOn);
void processQueue();
void markTranslated(const TaskPtr &task);
Manager &manager_;
QWebEngineView *view_;
QLineEdit *url_;
QAction *loadImages_;
QTabWidget *tabs_;
std::vector<TaskPtr> queue_;
std::map<QString, std::unique_ptr<WebPage>> pages_;

View File

@ -24,7 +24,7 @@ WebPage::WebPage(Translator &translator, const QString &script,
scheduleWebchannelInitScript();
scheduleTranslatorScript(script, scriptName);
settings()->setAttribute(QWebEngineSettings::AutoLoadImages, false);
setLoadImages(false);
auto channel = new QWebChannel(this);
channel->registerObject("proxy", proxy_.get());
@ -140,6 +140,16 @@ TaskPtr WebPage::task() const
return task_;
}
bool WebPage::isLoadImages() const
{
return settings()->testAttribute(QWebEngineSettings::AutoLoadImages);
}
void WebPage::setLoadImages(bool isOn)
{
settings()->setAttribute(QWebEngineSettings::AutoLoadImages, isOn);
}
void WebPage::javaScriptConsoleMessage(
QWebEnginePage::JavaScriptConsoleMessageLevel /*level*/,
const QString &message, int lineNumber, const QString &sourceID)

View File

@ -24,6 +24,9 @@ public:
bool isBusy() const;
TaskPtr task() const;
bool isLoadImages() const;
void setLoadImages(bool isOn);
signals:
void log(const QString &text);