syncthingtray/widgets/webview/webviewdialog.cpp

130 lines
3.2 KiB
C++
Raw Normal View History

#ifndef SYNCTHINGWIDGETS_NO_WEBVIEW
2016-08-25 00:45:32 +02:00
#include "./webviewdialog.h"
#include "./webpage.h"
#include "../settings/settings.h"
2016-08-25 00:45:32 +02:00
#include <qtutilities/misc/dialogutils.h>
#include <QIcon>
#include <QCloseEvent>
2016-11-01 17:16:27 +01:00
#include <QKeyEvent>
2016-08-25 00:45:32 +02:00
using namespace Dialogs;
namespace QtGui {
WebViewDialog::WebViewDialog(QWidget *parent) :
QMainWindow(parent),
m_view(new SYNCTHINGWIDGETS_WEB_VIEW(this))
2016-08-25 00:45:32 +02:00
{
setWindowTitle(tr("Syncthing"));
setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
setCentralWidget(m_view);
2016-09-03 20:14:52 +02:00
m_view->setPage(new WebPage(this, m_view));
connect(m_view, &SYNCTHINGWIDGETS_WEB_VIEW::titleChanged, this, &WebViewDialog::setWindowTitle);
2016-08-25 00:45:32 +02:00
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
m_view->installEventFilter(this);
if(m_view->focusProxy()) {
m_view->focusProxy()->installEventFilter(this);
}
#endif
2016-11-02 20:03:38 +01:00
if(Settings::values().webView.geometry.isEmpty()) {
2016-08-25 00:45:32 +02:00
resize(1200, 800);
centerWidget(this);
} else {
2016-11-02 20:03:38 +01:00
restoreGeometry(Settings::values().webView.geometry);
2016-08-25 00:45:32 +02:00
}
}
QtGui::WebViewDialog::~WebViewDialog()
{
2016-11-02 20:03:38 +01:00
Settings::values().webView.geometry = saveGeometry();
2016-08-25 00:45:32 +02:00
}
void QtGui::WebViewDialog::applySettings(const Data::SyncthingConnectionSettings &connectionSettings)
2016-08-25 00:45:32 +02:00
{
2016-09-03 20:14:52 +02:00
m_settings = connectionSettings;
if(!WebPage::isSamePage(m_view->url(), connectionSettings.syncthingUrl)) {
m_view->setUrl(connectionSettings.syncthingUrl);
}
2016-11-02 20:03:38 +01:00
m_view->setZoomFactor(Settings::values().webView.zoomFactor);
2016-08-25 00:45:32 +02:00
}
#if defined(SYNCTHINGWIDGETS_USE_WEBKIT)
2016-11-16 20:40:54 +01:00
bool WebViewDialog::isModalVisible() const
{
if(m_view->page()->mainFrame()) {
return m_view->page()->mainFrame()->evaluateJavaScript(QStringLiteral("$('.modal-dialog').is(':visible')")).toBool();
}
return false;
}
#endif
void WebViewDialog::closeUnlessModalVisible()
{
#if defined(SYNCTHINGWIDGETS_USE_WEBKIT)
2016-11-16 20:40:54 +01:00
if(!isModalVisible()) {
close();
}
#elif defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
2016-11-16 20:40:54 +01:00
m_view->page()->runJavaScript(QStringLiteral("$('.modal-dialog').is(':visible')"), [this] (const QVariant &modalVisible) {
if(!modalVisible.toBool()) {
close();
}
});
#else
close();
#endif
}
2016-08-25 00:45:32 +02:00
void QtGui::WebViewDialog::closeEvent(QCloseEvent *event)
{
2016-11-02 20:03:38 +01:00
if(!Settings::values().webView.keepRunning) {
2016-08-25 00:45:32 +02:00
deleteLater();
}
event->accept();
}
2016-11-01 17:16:27 +01:00
void WebViewDialog::keyPressEvent(QKeyEvent *event)
{
switch(event->key()) {
case Qt::Key_F5:
m_view->reload();
event->accept();
break;
2016-11-16 20:40:54 +01:00
case Qt::Key_Escape:
closeUnlessModalVisible();
event->accept();
break;
2016-11-01 17:16:27 +01:00
default:
QMainWindow::keyPressEvent(event);
}
}
#if defined(SYNCTHINGWIDGETS_USE_WEBENGINE)
bool WebViewDialog::eventFilter(QObject *watched, QEvent *event)
{
switch(event->type()) {
case QEvent::ChildAdded:
if(m_view->focusProxy()) {
m_view->focusProxy()->installEventFilter(this);
}
break;
case QEvent::KeyPress:
keyPressEvent(static_cast<QKeyEvent *>(event));
break;
default:
;
}
return QMainWindow::eventFilter(watched, event);
}
#endif
2016-08-25 00:45:32 +02:00
}
#endif // SYNCTHINGWIDGETS_NO_WEBVIEW