-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowsermainwindow.cpp
More file actions
481 lines (367 loc) · 13.1 KB
/
browsermainwindow.cpp
File metadata and controls
481 lines (367 loc) · 13.1 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "browsermainwindow.h"
#include "ui_browsermainwindow.h"
BrowserMainWindow::BrowserMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BrowserMainWindow)
{
ui->setupUi(this);
//Sets the title of the browser window
QWidget::setWindowTitle("Qool Browser");
//Sets the icon of the browser window
QWidget::setWindowIcon(QIcon(":/Icons/Assets/browser_icon.png"));
//Opens browser window as maximized at startup
QWidget::showMaximized();
//Initializes tab index to 0
currentTabIndex = 0;
/*
* QTabWidget object is created and some preferences are set:
* 1. Close buttons appear on each tab
* 2. Tabs are movable within the tabber area
* 3. Shape of tabs are set to triangular
* 4. The height of a tab is increased to 22px using a stylesheet
*/
tabView = new QTabWidget();
tabView->setTabsClosable(true);
tabView->setMovable(true);
tabView->setTabShape(QTabWidget::Triangular);
tabView->setStyleSheet("QTabBar::tab { height: 22px;}");
//Sets the tabView as the central widget of the mainwindow
setCentralWidget(tabView);
//A new tab is created on browser launch. By default Google.com is loaded.
createNewTab("");
}
BrowserMainWindow::~BrowserMainWindow()
{
delete ui;
}
void BrowserMainWindow::setupTabUI(QString mUrl)
{
// the loading gif is set to the label as a QMovie
lblLoad = new QLabel;
movLoad = new QMovie(":/Loader/Assets/loader.gif");
lblLoad->setMovie(movLoad);
lblLoad->show();
UrlBar = new QoolLineEdit();
webView = new QWebEngineView();
bDialog = new AddBookmarkDialog();
//Only one instance of addbookmark dialog will be displayed
bDialog->setModal(true);
backButton = new QPushButton();
backButton->setIcon(QIcon(":/Icons/Assets/back.png"));
backButton->setToolTip("Click to go back");
backButton->setEnabled(false);
forwardButton = new QPushButton();
forwardButton->setIcon(QIcon(":/Icons/Assets/forward.png"));
forwardButton->setToolTip("Click to go forward");
forwardButton->setEnabled(false);
refreshButton = new QPushButton();
refreshButton->setIcon(QIcon(":/Icons/Assets/refresh.png"));
refreshButton->setToolTip("Click to refresh");
searchButton = new QPushButton();
searchButton->setIcon(QIcon(":/Icons/Assets/search.png"));
searchButton->setToolTip("Search");
favButton = new QPushButton();
favButton->setIcon(QIcon(":/Icons/Assets/favourite.png"));
favButton->setToolTip("Add to favourites");
//Setting UrlBar properties
UrlBar->setClearButtonEnabled(true);
//Setting WebView properties
if(mUrl.isEmpty())
{
webView->load(QUrl("http://google.com"));
}else{
webView->load(QUrl(mUrl));
mUrl.clear();
}
webView->show();
}
void BrowserMainWindow::createNewTab(QString nUrl)
{
if(nUrl.isEmpty())
{
setupTabUI("");
}else{
setupTabUI(nUrl);
nUrl.clear();
}
//Finalizing new tab UI placements
QHBoxLayout * hLayout = new QHBoxLayout;
hLayout->addWidget(backButton);
hLayout->addWidget(forwardButton);
hLayout->addWidget(refreshButton);
hLayout->addWidget(UrlBar);
hLayout->addWidget(searchButton);
hLayout->addWidget(favButton);
hLayout->addWidget(lblLoad);
QWidget *hLayWidget = new QWidget();
hLayWidget->setLayout(hLayout);
QVBoxLayout *finalVLayout = new QVBoxLayout;
finalVLayout->addWidget(hLayWidget);
finalVLayout->addWidget(webView);
finalVLayout->setMargin(0);
QWidget *newTab = new QWidget();
newTab->setLayout(finalVLayout);
//New tab will load google.com by default
tabView->addTab(newTab,"New Tab");
//Sets focus to newly created tab
tabView->setCurrentWidget(newTab);
//Adds the newly created instance of thewebView to the list of webViews
webViewList.append(webView);
/* Signal and Slots connections - start */
connect(tabView,SIGNAL(tabCloseRequested(int)),
this,SLOT(slotCloseTabOnRequest(int)));
connect(webView,SIGNAL(loadStarted()),
this,SLOT(slotRunSpinner()));
connect(webView,SIGNAL(loadFinished(bool)),
this,SLOT(slotSetUrlOnBar(bool)));
connect(UrlBar,SIGNAL(returnPressed()),
this,SLOT(slotLoadWebPage()));
connect(UrlBar,SIGNAL(doubleClicked()),
UrlBar,SLOT(selectAll()));
connect(searchButton,SIGNAL(pressed()),
this,SLOT(slotLoadWebPage()));
connect(backButton,SIGNAL(pressed()),
webView,SLOT(back()));
connect(forwardButton,SIGNAL(pressed()),
webView,SLOT(forward()));
connect(refreshButton,SIGNAL(pressed()),
webView,SLOT(reload()));
connect(favButton,SIGNAL(pressed()),
this,SLOT(slotAddBookmark()));
connect(webView,SIGNAL(iconChanged(QIcon)),
this,SLOT(slotSetIconOnTab(QIcon)));
connect(webView,SIGNAL(titleChanged(QString)),
this,SLOT(slotSetTitleOnTab(QString)));
connect(tabView,SIGNAL(currentChanged(int)),
this,SLOT(slotSetFocusToNewTab(int)));
//sets URL and title on add bookmark dialog's linedits
connect(this,SIGNAL(newBookmarkAdded(QString,QString)),
bDialog,SLOT(slotSetBookmarkInfo(QString,QString)));
connect(this,SIGNAL(newHistoryItem(QString,QString,QString)),
this,SLOT(slotSaveHistoryItem(QString,QString,QString)));
/* Signal and Slots connections - end */
//updates the tabCount with the updated count of tabs
tabCount = tabView->count();
}
bool BrowserMainWindow::isUrlValid(QUrl url)
{
QString a = "a";
QTcpSocket socket;
socket.connectToHost(url.host(), 80);
if (socket.waitForConnected()) {
socket.write("HEAD " + url.path().toUtf8() + " HTTP/1.1\r\n"
"Host: " + url.host().toUtf8() + "\r\n\r\n");
if (socket.waitForReadyRead()) {
QByteArray bytes = socket.readAll();
if (bytes.contains("200 OK")) {
return true;
}
}
}
return false;
}
void BrowserMainWindow::slotLoadWebPage()
{
QUrl loadUrl(UrlBar->text());
if(isUrlValid(loadUrl)){
webView->load(loadUrl);
}else{
QString searchKey = UrlBar->text();
webView->load(QUrl("http://www.google.com/search?q="+searchKey));
}
}
void BrowserMainWindow::slotCloseTabOnRequest(int tabIndex)
{
tabView->removeTab(tabIndex);
webViewList.removeAt(tabIndex);
}
void BrowserMainWindow::slotSetIconOnTab(QIcon icon)
{
tabView->setTabIcon(currentTabIndex,icon);
}
void BrowserMainWindow::slotSetTitleOnTab(QString titleStr)
{
tabView->setTabText(currentTabIndex,titleStr);
}
void BrowserMainWindow::slotSetFocusToNewTab(int index)
{
currentTabIndex = index;
}
void BrowserMainWindow::slotAddBookmark()
{
QWebEngineView *wview = new QWebEngineView();
wview = webViewList[currentTabIndex];
emit newBookmarkAdded(wview->title(),wview->url().toString());
bDialog->show();
}
void BrowserMainWindow::slotReceivePageSrc(QString src)
{
// Converted HTML stream is displayed in a new window
QPlainTextEdit *view = new QPlainTextEdit;
view->setWindowIcon(QIcon(":/Icons/Assets/browser_icon.png"));
view->setMinimumWidth(640);
view->setMinimumHeight(480);
view->setAttribute(Qt::WA_DeleteOnClose);
view->setPlainText(src);
view->show();
}
void BrowserMainWindow::slotSaveHistoryItem(QString title, QString url, QString dateTime)
{
mHistoryFilePath = QDir::currentPath();
if(QFileInfo(mHistoryFilePath+"/storage/history.qool").exists()){
//The file exists
}else{
//The file doesn't exist
QDir dir;
dir.mkpath(mHistoryFilePath+"/storage");
//if file not exists, txt file is created in the parent directory under storage folder
QFile f(mHistoryFilePath+"/storage/history.qool");
f.open(QIODevice::ReadWrite);
}
/* Try and open a file for output */
QString outputFilename = "history.qool";
QFile outputFile(mHistoryFilePath+"/storage/"+outputFilename);
outputFile.open(QIODevice::Append);
/* Check it opened OK */
if(!outputFile.isOpen()){
qDebug() << "- Error, unable to open";
}
/* Point a QTextStream object at the file */
QTextStream outStream(&outputFile);
/* Write the line to the file */
outStream << dateTime + " | " + title + " | " + url +"\n";
/* Close the file */
outputFile.close();
}
void BrowserMainWindow::slotOpenRequestedUrl(QString url)
{
//url is passed into the method which sets the url of the webview to the requested url
QWidget::activateWindow();
createNewTab(url);
}
void BrowserMainWindow::slotRunSpinner()
{
// Starts the loading animation
movLoad->start();
}
void BrowserMainWindow::slotSetUrlOnBar(bool loaded)
{
if(loaded){
//sets the current URL on the URL bar
UrlBar->setText(webView->url().toString());
movLoad->stop();
//checks if it's possible to go to previous web page in history
if(webView->history()->canGoBack()){
backButton->setEnabled(true);
}else{
backButton->setEnabled(false);
}
if(webView->history()->canGoForward()){
forwardButton->setEnabled(true);
}else{
forwardButton->setEnabled(false);
}
/* When a new web page is accessed, it's information such as title, url and date accessed is
* stored in the history permanently until deleted.*/
QWebEngineHistoryItem hItem = webView->history()->currentItem();
emit newHistoryItem(hItem.title(),hItem.url().toString(),hItem.lastVisited().toString());
}
}
void BrowserMainWindow::on_actionNew_Tab_triggered()
{
createNewTab("");
}
void BrowserMainWindow::on_actionNew_Window_triggered()
{
//New browser window instance created
BrowserMainWindow *mw = new BrowserMainWindow;
mw->setAttribute(Qt::WA_DeleteOnClose);
mw->show();
}
void BrowserMainWindow::on_actionQuit_triggered()
{
this->close();
}
void BrowserMainWindow::on_actionStop_triggered()
{
QWebEngineView *wview = new QWebEngineView();
wview = webViewList[currentTabIndex];
wview->stop();
}
void BrowserMainWindow::on_actionReload_Page_triggered()
{
QWebEngineView *wview = new QWebEngineView();
wview = webViewList[currentTabIndex];
wview->reload();
}
void BrowserMainWindow::on_actionZoom_in_triggered()
{
QWebEngineView *wview = new QWebEngineView();
//increase zoom
wview = webViewList[currentTabIndex];
wview->setZoomFactor(wview->zoomFactor() + 0.1);
}
void BrowserMainWindow::on_actionZoom_out_triggered()
{
QWebEngineView *wview = new QWebEngineView();
//decrease zoom
wview = webViewList[currentTabIndex];
wview->setZoomFactor(wview->zoomFactor() - 0.1);
}
void BrowserMainWindow::on_actionReset_Zoom_triggered()
{
QWebEngineView *wview = new QWebEngineView();
//resets zoom to original level of webview in the current tab
wview = webViewList[currentTabIndex];
wview->setZoomFactor(1.0);
}
void BrowserMainWindow::on_actionPage_Source_triggered()
{
QWebEngineView *wview = new QWebEngineView();
wview = webViewList[currentTabIndex];
connect(this,SIGNAL(newPageSrcReceived(QString)),
this,SLOT(slotReceivePageSrc(QString)));
wview->page()->toHtml([this](const QString& result) mutable {emit newPageSrcReceived(result);});
}
void BrowserMainWindow::on_actionFull_Screen_triggered()
{
if (isFullScreen())
{
// Show the window normal or maximized (depending on how it was shown before)
if (windowFullscreen)
showMaximized();
else
showNormal();
}
else
{
// Save if the window is currently maximized
windowFullscreen = isMaximized();
showFullScreen();
}
}
void BrowserMainWindow::on_actionShow_all_bookmarks_triggered()
{
allBookMarks = new Bookmarks();
/*When a link is double clicked or opened from bookmarks window, url is opened in the browser*/
connect(allBookMarks, SIGNAL(requestedUrl(QString)),
this,SLOT(slotOpenRequestedUrl(QString)));
allBookMarks->setAttribute(Qt::WA_QuitOnClose, false);//closes bookmarks window if main window is closed
allBookMarks->show();
}
void BrowserMainWindow::on_actionShow_all_history_triggered()
{
historyWindow = new HistoryWindow();
/*When a link is double clicked or opened from history window, url is opened in the browser*/
connect(historyWindow, SIGNAL(requestedUrl(QString)),
this,SLOT(slotOpenRequestedUrl(QString)));
historyWindow->setAttribute(Qt::WA_QuitOnClose, false); //closes history window if main window is closed
historyWindow->show();
}
void BrowserMainWindow::on_actionAbout_Qool_Browser_triggered()
{
QMessageBox::about(this,
"About Qool Browser",
"Developed by: Ramith D Randeniya\n\nProject developed for Cross Platform Application Development in C++\n"
"Special Thanks to Dr.Davide Maracco for continuous guidance and support.\n\n2016-2017");
}