From b525d64592f6435ca26ebb2653dad833220192bf Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Wed, 17 Jul 2013 20:42:42 +0200 Subject: [PATCH] Made changes to web browser + several new classes. --- CMakeLists.txt | 4 +++ src/gui/baselineedit.cpp | 19 ++++++++++++++ src/gui/baselineedit.h | 23 +++++++++++++++++ src/gui/basewebview.cpp | 15 ++++++++++- src/gui/formsettings.cpp | 36 ++++++++++++++++++++++++++- src/gui/formsettings.h | 1 + src/gui/formsettings.ui | 6 ++--- src/gui/locationlineedit.cpp | 40 ++++++++++++++++++++++++++++++ src/gui/locationlineedit.h | 29 ++++++++++++++++++++++ src/gui/webbrowser.cpp | 48 +++++++++++++++++++++++++++++++----- src/gui/webbrowser.h | 11 ++++++++- 11 files changed, 220 insertions(+), 12 deletions(-) create mode 100644 src/gui/baselineedit.cpp create mode 100644 src/gui/baselineedit.h create mode 100644 src/gui/locationlineedit.cpp create mode 100644 src/gui/locationlineedit.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dced58528..6448b7456 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,8 @@ set(APP_SOURCES src/gui/webbrowser.cpp src/gui/basewebview.cpp src/gui/basewebpage.cpp + src/gui/baselineedit.cpp + src/gui/locationlineedit.cpp # CORE sources. src/core/debugging.cpp @@ -205,6 +207,8 @@ set(APP_HEADERS src/gui/webbrowser.h src/gui/basewebview.h src/gui/basewebpage.h + src/gui/baselineedit.h + src/gui/locationlineedit.h # CORE headers. src/core/basenetworkaccessmanager.h diff --git a/src/gui/baselineedit.cpp b/src/gui/baselineedit.cpp new file mode 100644 index 000000000..05010c94c --- /dev/null +++ b/src/gui/baselineedit.cpp @@ -0,0 +1,19 @@ +#include + +#include "gui/baselineedit.h" + + +BaseLineEdit::BaseLineEdit(QWidget *parent) : QLineEdit(parent) { +} + +BaseLineEdit::~BaseLineEdit() { +} + +void BaseLineEdit::keyPressEvent(QKeyEvent *event) { + if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { + emit submitted(text()); + event->accept(); + } + + QLineEdit::keyPressEvent(event); +} diff --git a/src/gui/baselineedit.h b/src/gui/baselineedit.h new file mode 100644 index 000000000..3fd87ebbd --- /dev/null +++ b/src/gui/baselineedit.h @@ -0,0 +1,23 @@ +#ifndef BASELINEEDIT_H +#define BASELINEEDIT_H + +#include + + +class BaseLineEdit : public QLineEdit { + Q_OBJECT + public: + explicit BaseLineEdit(QWidget *parent = 0); + virtual ~BaseLineEdit(); + + protected: + void keyPressEvent(QKeyEvent *event); + + signals: + void submitted(const QString &text); + + public slots: + +}; + +#endif // BASELINEEDIT_H diff --git a/src/gui/basewebview.cpp b/src/gui/basewebview.cpp index 2d3488730..35c54ce04 100644 --- a/src/gui/basewebview.cpp +++ b/src/gui/basewebview.cpp @@ -12,5 +12,18 @@ BaseWebView::BaseWebView(QWidget *parent) void BaseWebView::paintEvent(QPaintEvent *event) { QWebView::paintEvent(event); - // TODO: Add frame, inspire from QFrame source code - paint event. + // Draw additional frame. + QPainter painter(this); + QStyleOptionFrameV3 style_option; + int frame_shape = QFrame::Sunken & QFrame::Shape_Mask; + + style_option.init(this); + style_option.frameShape = QFrame::Shape(int(style_option.frameShape) | + QFrame::StyledPanel | + frame_shape); + style_option.rect = rect(); + style_option.lineWidth = 1; + style_option.midLineWidth = 0; + + style()->drawControl(QStyle::CE_ShapedFrame, &style_option, &painter, this); } diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index 208918cb2..0a8fd628f 100644 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -39,6 +39,8 @@ FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::Form connect(this, &FormSettings::accepted, this, &FormSettings::saveSettings); connect(m_ui->m_cmbProxyType, static_cast(&QComboBox::currentIndexChanged), this, &FormSettings::onProxyTypeChanged); + connect(m_ui->m_checkShowPassword, &QCheckBox::stateChanged, + this, &FormSettings::displayProxyPassword); // Load all settings. loadGeneral(); @@ -52,6 +54,15 @@ FormSettings::~FormSettings() { delete m_ui; } +void FormSettings::displayProxyPassword(int state) { + if (state == Qt::Checked) { + m_ui->m_txtProxyPassword->setEchoMode(QLineEdit::Normal); + } + else { + m_ui->m_txtProxyPassword->setEchoMode(QLineEdit::PasswordEchoOnEdit); + } +} + void FormSettings::saveSettings() { // Save all settings. saveGeneral(); @@ -78,10 +89,33 @@ void FormSettings::loadProxy() { m_ui->m_cmbProxyType->addItem(tr("No proxy"), QNetworkProxy::NoProxy); m_ui->m_cmbProxyType->addItem(tr("Socks5"), QNetworkProxy::Socks5Proxy); m_ui->m_cmbProxyType->addItem(tr("Http"), QNetworkProxy::HttpProxy); + + // Load the settings. + QNetworkProxy::ProxyType selected_proxy_type = static_cast(Settings::getInstance()->value(APP_CFG_PROXY, + "proxy_type", + QNetworkProxy::NoProxy).toInt()); + m_ui->m_cmbProxyType->setCurrentIndex(m_ui->m_cmbProxyType->findData(selected_proxy_type)); + m_ui->m_txtProxyHost->setText(Settings::getInstance()->value(APP_CFG_PROXY, + "host").toString()); + m_ui->m_txtProxyUsername->setText(Settings::getInstance()->value(APP_CFG_PROXY, + "username").toString()); + m_ui->m_txtProxyPassword->setText(Settings::getInstance()->value(APP_CFG_PROXY, + "password").toString()); + m_ui->m_spinProxyPort->setValue(Settings::getInstance()->value(APP_CFG_PROXY, + "port", 80).toInt()); } void FormSettings::saveProxy() { - + Settings::getInstance()->setValue(APP_CFG_PROXY, "proxy_type", + m_ui->m_cmbProxyType->itemData(m_ui->m_cmbProxyType->currentIndex())); + Settings::getInstance()->setValue(APP_CFG_PROXY, "host", + m_ui->m_txtProxyHost->text()); + Settings::getInstance()->setValue(APP_CFG_PROXY, "username", + m_ui->m_txtProxyUsername->text()); + Settings::getInstance()->setValue(APP_CFG_PROXY, "password", + m_ui->m_txtProxyPassword->text()); + Settings::getInstance()->setValue(APP_CFG_PROXY, "port", + m_ui->m_spinProxyPort->value()); } void FormSettings::loadLanguage() { diff --git a/src/gui/formsettings.h b/src/gui/formsettings.h index 5d2c6fd33..66504fde7 100644 --- a/src/gui/formsettings.h +++ b/src/gui/formsettings.h @@ -36,6 +36,7 @@ class FormSettings : public QDialog { void loadProxy(); void saveProxy(); + void displayProxyPassword(int state); void onProxyTypeChanged(int index); diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui index 871ff599e..ad4948291 100644 --- a/src/gui/formsettings.ui +++ b/src/gui/formsettings.ui @@ -117,8 +117,8 @@ 0 0 - 506 - 368 + 100 + 30 @@ -423,7 +423,7 @@ true - QLineEdit::Password + QLineEdit::PasswordEchoOnEdit Your password for proxy server authentication diff --git a/src/gui/locationlineedit.cpp b/src/gui/locationlineedit.cpp new file mode 100644 index 000000000..c1ab2b9b2 --- /dev/null +++ b/src/gui/locationlineedit.cpp @@ -0,0 +1,40 @@ +#include + +#include "gui/locationlineedit.h" + + +LocationLineEdit::LocationLineEdit(QWidget *parent) + : BaseLineEdit(parent), m_progress(0), m_mouseSelectsAllText(true) { +} + +LocationLineEdit::~LocationLineEdit() { +} + +void LocationLineEdit::setProgress(int progress) { + m_progress = progress; +} + +void LocationLineEdit::focusOutEvent(QFocusEvent *event) { + BaseLineEdit::focusOutEvent(event); + + // User now left text box, when he enters it again and clicks, + // then all text should be selected. + m_mouseSelectsAllText = true; +} + +void LocationLineEdit::mousePressEvent(QMouseEvent *event) { + if (m_mouseSelectsAllText) { + event->ignore(); + selectAll(); + + // User clicked and all text was selected. + m_mouseSelectsAllText = false; + } + else { + BaseLineEdit::mousePressEvent(event); + } +} + +void LocationLineEdit::paintEvent(QPaintEvent *event) { + BaseLineEdit::paintEvent(event); +} diff --git a/src/gui/locationlineedit.h b/src/gui/locationlineedit.h new file mode 100644 index 000000000..022c41e3f --- /dev/null +++ b/src/gui/locationlineedit.h @@ -0,0 +1,29 @@ +#ifndef LOCATIONLINEEDIT_H +#define LOCATIONLINEEDIT_H + +#include "gui/baselineedit.h" + + +class WebBrowser; + +class LocationLineEdit : public BaseLineEdit { + public: + explicit LocationLineEdit(QWidget *parent = 0); + virtual ~LocationLineEdit(); + + public slots: + // Sets percentual value of web page loading action. + // NOTE: Number ranging from 0 to 100 is expected. + void setProgress(int progress); + + protected: + void focusOutEvent(QFocusEvent *event); + void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); + + private: + int m_progress; + bool m_mouseSelectsAllText; +}; + +#endif // LOCATIONLINEEDIT_H diff --git a/src/gui/webbrowser.cpp b/src/gui/webbrowser.cpp index 3174693a1..4c99e9ccc 100644 --- a/src/gui/webbrowser.cpp +++ b/src/gui/webbrowser.cpp @@ -2,11 +2,13 @@ #include #include #include -#include + +#include #include "core/basenetworkaccessmanager.h" #include "gui/basewebview.h" #include "gui/webbrowser.h" +#include "gui/locationlineedit.h" #include "gui/themefactory.h" @@ -17,6 +19,7 @@ WebBrowser::WebBrowser(QWidget *parent) : QWidget(parent), m_layout(new QVBoxLayout(this)), m_toolBar(new QToolBar(tr("Navigation panel"), this)), m_webView(new BaseWebView(this)), + m_txtLocation(new LocationLineEdit(this)), m_actionBack(m_webView->pageAction(QWebPage::Back)), m_actionForward(m_webView->pageAction(QWebPage::Forward)), m_actionReload(m_webView->pageAction(QWebPage::Reload)), @@ -31,27 +34,60 @@ WebBrowser::WebBrowser(QWidget *parent) // and main window will be responsible for reloading // icons on all web browsers. - // Set toolbar properties. + // Set properties of some components. m_toolBar->layout()->setMargin(0); m_toolBar->setFloatable(false); m_toolBar->setMovable(false); + // Modify action texts. + m_actionBack->setText(tr("Back")); + m_actionBack->setToolTip(tr("Go back")); + m_actionForward->setText(tr("Forward")); + m_actionForward->setToolTip(tr("Go forward")); + m_actionReload->setText(tr("Reload")); + m_actionReload->setToolTip(tr("Reload current web page")); + m_actionStop->setText(tr("Stop")); + m_actionStop->setToolTip(tr("Stop web page loading")); + // Add needed actions into toolbar. m_toolBar->addAction(m_actionBack); m_toolBar->addAction(m_actionForward); m_toolBar->addSeparator(); m_toolBar->addAction(m_actionReload); m_toolBar->addAction(m_actionStop); - - QLineEdit *ed = new QLineEdit(this); - m_toolBar->addWidget(ed); + m_toolBar->addWidget(m_txtLocation); // Setup layout. m_layout->addWidget(m_toolBar); m_layout->addWidget(m_webView); m_layout->setMargin(0); - m_webView->load(QUrl("http://www.seznam.cz")); + createConnections(); +} + +void WebBrowser::createConnections() { + // When user confirms new url, then redirect to it. + connect(m_txtLocation, &LocationLineEdit::submitted, + this, &WebBrowser::navigateToUrl); + // If new page loads, then update current url. + connect(m_webView, &BaseWebView::urlChanged, + this, &WebBrowser::updateUrl); + + // Change location textbox status according to webpage status. + connect(m_webView, &BaseWebView::loadProgress, + m_txtLocation, &LocationLineEdit::setProgress); +} + +void WebBrowser::updateUrl(const QUrl &url) { + m_txtLocation->setText(url.toString()); +} + +void WebBrowser::navigateToUrl(const QString &textual_url) { + QUrl extracted_url = QUrl::fromUserInput(textual_url); + + if (extracted_url.isValid()) { + m_webView->setUrl(extracted_url); + } } WebBrowser::~WebBrowser() { diff --git a/src/gui/webbrowser.h b/src/gui/webbrowser.h index d720262a6..7eb92a756 100644 --- a/src/gui/webbrowser.h +++ b/src/gui/webbrowser.h @@ -6,6 +6,7 @@ class QToolBar; class QVBoxLayout; +class LocationLineEdit; class BaseWebView; class BaseNetworkAccessManager; @@ -21,10 +22,18 @@ class WebBrowser : public QWidget { static BaseNetworkAccessManager *globalNetworkManager(); static QList runningWebBrowsers(); + protected: + void createConnections(); + + protected slots: + void updateUrl(const QUrl &url); + void navigateToUrl(const QString &url); + private: - QToolBar *m_toolBar; QVBoxLayout *m_layout; + QToolBar *m_toolBar; BaseWebView *m_webView; + LocationLineEdit *m_txtLocation; QAction *m_actionBack; QAction *m_actionForward;