-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
144 lines (127 loc) · 4.65 KB
/
mainwindow.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "mainwindow.h"
MainWindoW::MainWindoW()
{
this->setWindowIcon(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/web.png"));
tabs = new QTabWidget(this);
createActions();
createToolBar();
createMenus();
createProgressBar();
setCentralWidget(tabs);
addTab();
}
void MainWindoW::createMenus()
{
file = menuBar()->addMenu("&File");
file->addAction(newTab);
file->addAction(closeTab);
file->addSeparator();
file->addAction(quit);
navigation = menuBar()->addMenu("&Navigation");
navigation->addAction(next);
navigation->addAction(prev);
navigation->addAction(reload);
navigation->addAction(startPage);
menuBar()->addSeparator();
questionMark = menuBar()->addMenu("&About");
questionMark->addAction(about);
questionMark->addAction(aboutQt);
}
void MainWindoW::createActions()
{
newTab = new QAction("&New tab", this);
connect(newTab, SIGNAL(triggered()),this,SLOT(addTab()));
closeTab = new QAction("&Close tab", this);
connect(closeTab, SIGNAL(triggered()), this, SLOT(removeTab()));
quit = new QAction("&Quit",this);
connect(quit,SIGNAL(triggered()),qApp,SLOT(quit()));
prev = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/prec.png"),"&Previous",this);
connect(prev,SIGNAL(triggered()),this, SLOT(previousPage()));
next = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/suiv.png"),"&Next",this);
connect(next,SIGNAL(triggered()),this , SLOT(forwardPage()));
reload = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/actu.png"), "&Reload",this);
connect(reload, SIGNAL(triggered()), tabs->currentWidget()->findChild<QWebView *>(), SLOT(reload()));
stop = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/stop.png"),"&Stop",this);
connect(stop, SIGNAL(triggered()), tabs->currentWidget()->findChild<QWebView *>(), SLOT(stop()));
go = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/go.png"),"&Go",this);
connect(go, SIGNAL(triggered()), tabs->currentWidget()->findChild<QWebView *>(), SLOT(goAddress()) );
startPage = new QAction(QIcon("/home/bsafwene/compSci/C++/Qt/webNavigator/home.png"),"&Start page",this);
about = new QAction("&About", this);
connect(about, SIGNAL(triggered()), this, SLOT(aboutDialog()));
aboutQt = new QAction("About &Qt", this);
connect(aboutQt, SIGNAL(triggered()), this, SLOT(aboutDialogQt()));
}
void MainWindoW::createToolBar()
{
navigationToolBar = addToolBar("Navigation");
navigationToolBar->addAction(prev);
navigationToolBar->addAction(next);
navigationToolBar->addAction(reload);
navigationToolBar->addAction(stop);
barAddress = new QLineEdit ;
connect(barAddress, SIGNAL(editingFinished()),this, SLOT(goAddress()));
navigationToolBar->addWidget(barAddress);
navigationToolBar->addAction(go);
}
void MainWindoW::createProgressBar()
{
progressBar = new QProgressBar ;
progressBar->setRange(0,100);
statusBar()->addWidget(progressBar,1);
}
void MainWindoW::addTab()
{
QWidget *t = new QWidget ;
QWebView *tv = new QWebView(this);
tv->load(QUrl("http://www.google.fr/"));
tv->show();
QVBoxLayout *layout = new QVBoxLayout ;
layout->addWidget(tv);
t->setLayout(layout);
tabs->addTab(t,"Google");
barAddress->setText("http://www.google.fr/");
connect(tv, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
connect(tv,SIGNAL(urlChanged(QUrl)),this, SLOT(setTabTitle()));
connect(tv, SIGNAL(loadProgress(int)), this, SLOT(updatepBar(int)));
}
void MainWindoW::removeTab()
{
tabs->removeTab(tabs->currentIndex());
}
void MainWindoW::previousPage()
{
QWebView *ptr = tabs->currentWidget()->findChild<QWebView *>() ;
ptr->history()->back();
}
void MainWindoW::goAddress()
{
QWebView *ptr = tabs->currentWidget()->findChild<QWebView *>() ;
QUrl url = QUrl::fromUserInput(barAddress->text());
ptr->load(url);
}
void MainWindoW::forwardPage()
{
QWebView *ptr = tabs->currentWidget()->findChild<QWebView *>();
ptr->history()->forward();
}
void MainWindoW::aboutDialog()
{
QMessageBox::information(this, "About", "This my homework");
}
void MainWindoW::aboutDialogQt()
{
QMessageBox::information(this,"About Qt","whatever");
}
void MainWindoW::adjustTitle()
{
setWindowTitle(tabs->currentWidget()->findChild<QWebView *>()->title());
}
void MainWindoW::setTabTitle()
{
barAddress->setText(tabs->currentWidget()->findChild<QWebView *>()->url().toDisplayString());
tabs->setTabText(tabs->currentIndex(),tabs->currentWidget()->findChild<QWebView *>()->url().host());
}
void MainWindoW::updatepBar(int progress)
{
progressBar->setValue(progress);
}