summaryrefslogtreecommitdiffstats
path: root/Widget.cpp
blob: 5ff6ea12192f3e24de8a301a28cd1508744c0912 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "Widget.h"
#include <QWebView>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>

Widget::Widget(QWidget *parent)
	: QWidget(parent),
	  m_reloadButtonOn(true)
{
	QVBoxLayout *vLayout = new QVBoxLayout;
	m_webView = new QWebView;
	m_webView->setUrl(QUrl("http://www.zx2c4.com"));
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	QPushButton *back = new QPushButton(tr("Back"));
	QPushButton *forward = new QPushButton(tr("Forward"));
	m_stopReload = new QPushButton(tr("Reload"));
	m_entry = new QLineEdit;
	buttonLayout->addWidget(back);
	buttonLayout->addWidget(forward);
	buttonLayout->addWidget(m_stopReload);
	buttonLayout->addWidget(m_entry);
	connect(back, SIGNAL(clicked()), m_webView, SLOT(back()));
	connect(forward, SIGNAL(clicked()), m_webView, SLOT(forward()));
	connect(m_stopReload, SIGNAL(clicked()), this, SLOT(doStopReload()));
	connect(m_entry, SIGNAL(returnPressed()), this, SLOT(navigate()));
	connect(m_webView, SIGNAL(urlChanged(QUrl)), this, SLOT(navigated(QUrl)));
	connect(m_webView, SIGNAL(loadStarted()), this, SLOT(setReloadOff()));
	connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(setReloadOn()));
	vLayout->addLayout(buttonLayout);
	vLayout->addWidget(m_webView);
	setLayout(vLayout);
}
void Widget::navigate()
{
	if (!m_entry->text().startsWith("http://") && !m_entry->text().startsWith("https://"))
		m_entry->setText(m_entry->text().prepend("http://"));
	m_webView->setUrl(QUrl(m_entry->text()));
}
void Widget::navigated(const QUrl &url)
{
	m_entry->setText(url.toString());
}
void Widget::doStopReload()
{
	if (m_reloadButtonOn)
		m_webView->reload();
	else
		m_webView->stop();
}
void Widget::setReloadOff()
{
	m_reloadButtonOn = false;
	m_stopReload->setText(tr("Stop"));
}
void Widget::setReloadOn()
{
	m_reloadButtonOn = true;
	m_stopReload->setText(tr("Reload"));
}