|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the demonstration applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "browsermainwindow.h" |
|
43 |
|
44 #include "autosaver.h" |
|
45 #include "bookmarks.h" |
|
46 #include "browserapplication.h" |
|
47 #include "chasewidget.h" |
|
48 #include "downloadmanager.h" |
|
49 #include "history.h" |
|
50 #include "settings.h" |
|
51 #include "tabwidget.h" |
|
52 #include "toolbarsearch.h" |
|
53 #include "ui_passworddialog.h" |
|
54 #include "webview.h" |
|
55 |
|
56 #include <QtCore/QSettings> |
|
57 |
|
58 #include <QtGui/QDesktopWidget> |
|
59 #include <QtGui/QFileDialog> |
|
60 #include <QtGui/QPlainTextEdit> |
|
61 #include <QtGui/QPrintDialog> |
|
62 #include <QtGui/QPrintPreviewDialog> |
|
63 #include <QtGui/QPrinter> |
|
64 #include <QtGui/QMenuBar> |
|
65 #include <QtGui/QMessageBox> |
|
66 #include <QtGui/QStatusBar> |
|
67 #include <QtGui/QToolBar> |
|
68 #include <QtGui/QInputDialog> |
|
69 |
|
70 #include <QtWebKit/QWebFrame> |
|
71 #include <QtWebKit/QWebHistory> |
|
72 |
|
73 #include <QtCore/QDebug> |
|
74 |
|
75 BrowserMainWindow::BrowserMainWindow(QWidget *parent, Qt::WindowFlags flags) |
|
76 : QMainWindow(parent, flags) |
|
77 , m_tabWidget(new TabWidget(this)) |
|
78 , m_autoSaver(new AutoSaver(this)) |
|
79 , m_historyBack(0) |
|
80 , m_historyForward(0) |
|
81 , m_stop(0) |
|
82 , m_reload(0) |
|
83 { |
|
84 setToolButtonStyle(Qt::ToolButtonFollowStyle); |
|
85 setAttribute(Qt::WA_DeleteOnClose, true); |
|
86 statusBar()->setSizeGripEnabled(true); |
|
87 setupMenu(); |
|
88 setupToolBar(); |
|
89 |
|
90 QWidget *centralWidget = new QWidget(this); |
|
91 BookmarksModel *boomarksModel = BrowserApplication::bookmarksManager()->bookmarksModel(); |
|
92 m_bookmarksToolbar = new BookmarksToolBar(boomarksModel, this); |
|
93 connect(m_bookmarksToolbar, SIGNAL(openUrl(QUrl)), |
|
94 m_tabWidget, SLOT(loadUrlInCurrentTab(QUrl))); |
|
95 connect(m_bookmarksToolbar->toggleViewAction(), SIGNAL(toggled(bool)), |
|
96 this, SLOT(updateBookmarksToolbarActionText(bool))); |
|
97 |
|
98 QVBoxLayout *layout = new QVBoxLayout; |
|
99 layout->setSpacing(0); |
|
100 layout->setMargin(0); |
|
101 #if defined(Q_WS_MAC) |
|
102 layout->addWidget(m_bookmarksToolbar); |
|
103 layout->addWidget(new QWidget); // <- OS X tab widget style bug |
|
104 #else |
|
105 addToolBarBreak(); |
|
106 addToolBar(m_bookmarksToolbar); |
|
107 #endif |
|
108 layout->addWidget(m_tabWidget); |
|
109 centralWidget->setLayout(layout); |
|
110 setCentralWidget(centralWidget); |
|
111 |
|
112 connect(m_tabWidget, SIGNAL(loadPage(QString)), |
|
113 this, SLOT(loadPage(QString))); |
|
114 connect(m_tabWidget, SIGNAL(setCurrentTitle(QString)), |
|
115 this, SLOT(slotUpdateWindowTitle(QString))); |
|
116 connect(m_tabWidget, SIGNAL(showStatusBarMessage(QString)), |
|
117 statusBar(), SLOT(showMessage(QString))); |
|
118 connect(m_tabWidget, SIGNAL(linkHovered(QString)), |
|
119 statusBar(), SLOT(showMessage(QString))); |
|
120 connect(m_tabWidget, SIGNAL(loadProgress(int)), |
|
121 this, SLOT(slotLoadProgress(int))); |
|
122 connect(m_tabWidget, SIGNAL(tabsChanged()), |
|
123 m_autoSaver, SLOT(changeOccurred())); |
|
124 connect(m_tabWidget, SIGNAL(geometryChangeRequested(QRect)), |
|
125 this, SLOT(geometryChangeRequested(QRect))); |
|
126 connect(m_tabWidget, SIGNAL(printRequested(QWebFrame*)), |
|
127 this, SLOT(printRequested(QWebFrame*))); |
|
128 connect(m_tabWidget, SIGNAL(menuBarVisibilityChangeRequested(bool)), |
|
129 menuBar(), SLOT(setVisible(bool))); |
|
130 connect(m_tabWidget, SIGNAL(statusBarVisibilityChangeRequested(bool)), |
|
131 statusBar(), SLOT(setVisible(bool))); |
|
132 connect(m_tabWidget, SIGNAL(toolBarVisibilityChangeRequested(bool)), |
|
133 m_navigationBar, SLOT(setVisible(bool))); |
|
134 connect(m_tabWidget, SIGNAL(toolBarVisibilityChangeRequested(bool)), |
|
135 m_bookmarksToolbar, SLOT(setVisible(bool))); |
|
136 #if defined(Q_WS_MAC) |
|
137 connect(m_tabWidget, SIGNAL(lastTabClosed()), |
|
138 this, SLOT(close())); |
|
139 #else |
|
140 connect(m_tabWidget, SIGNAL(lastTabClosed()), |
|
141 m_tabWidget, SLOT(newTab())); |
|
142 #endif |
|
143 |
|
144 slotUpdateWindowTitle(); |
|
145 loadDefaultState(); |
|
146 m_tabWidget->newTab(); |
|
147 |
|
148 int size = m_tabWidget->lineEditStack()->sizeHint().height(); |
|
149 m_navigationBar->setIconSize(QSize(size, size)); |
|
150 |
|
151 } |
|
152 |
|
153 BrowserMainWindow::~BrowserMainWindow() |
|
154 { |
|
155 m_autoSaver->changeOccurred(); |
|
156 m_autoSaver->saveIfNeccessary(); |
|
157 } |
|
158 |
|
159 void BrowserMainWindow::loadDefaultState() |
|
160 { |
|
161 QSettings settings; |
|
162 settings.beginGroup(QLatin1String("BrowserMainWindow")); |
|
163 QByteArray data = settings.value(QLatin1String("defaultState")).toByteArray(); |
|
164 restoreState(data); |
|
165 settings.endGroup(); |
|
166 } |
|
167 |
|
168 QSize BrowserMainWindow::sizeHint() const |
|
169 { |
|
170 QRect desktopRect = QApplication::desktop()->screenGeometry(); |
|
171 QSize size = desktopRect.size() * qreal(0.9); |
|
172 return size; |
|
173 } |
|
174 |
|
175 void BrowserMainWindow::save() |
|
176 { |
|
177 BrowserApplication::instance()->saveSession(); |
|
178 |
|
179 QSettings settings; |
|
180 settings.beginGroup(QLatin1String("BrowserMainWindow")); |
|
181 QByteArray data = saveState(false); |
|
182 settings.setValue(QLatin1String("defaultState"), data); |
|
183 settings.endGroup(); |
|
184 } |
|
185 |
|
186 static const qint32 BrowserMainWindowMagic = 0xba; |
|
187 |
|
188 QByteArray BrowserMainWindow::saveState(bool withTabs) const |
|
189 { |
|
190 int version = 2; |
|
191 QByteArray data; |
|
192 QDataStream stream(&data, QIODevice::WriteOnly); |
|
193 |
|
194 stream << qint32(BrowserMainWindowMagic); |
|
195 stream << qint32(version); |
|
196 |
|
197 stream << size(); |
|
198 stream << !m_navigationBar->isHidden(); |
|
199 stream << !m_bookmarksToolbar->isHidden(); |
|
200 stream << !statusBar()->isHidden(); |
|
201 if (withTabs) |
|
202 stream << tabWidget()->saveState(); |
|
203 else |
|
204 stream << QByteArray(); |
|
205 return data; |
|
206 } |
|
207 |
|
208 bool BrowserMainWindow::restoreState(const QByteArray &state) |
|
209 { |
|
210 int version = 2; |
|
211 QByteArray sd = state; |
|
212 QDataStream stream(&sd, QIODevice::ReadOnly); |
|
213 if (stream.atEnd()) |
|
214 return false; |
|
215 |
|
216 qint32 marker; |
|
217 qint32 v; |
|
218 stream >> marker; |
|
219 stream >> v; |
|
220 if (marker != BrowserMainWindowMagic || v != version) |
|
221 return false; |
|
222 |
|
223 QSize size; |
|
224 bool showToolbar; |
|
225 bool showBookmarksBar; |
|
226 bool showStatusbar; |
|
227 QByteArray tabState; |
|
228 |
|
229 stream >> size; |
|
230 stream >> showToolbar; |
|
231 stream >> showBookmarksBar; |
|
232 stream >> showStatusbar; |
|
233 stream >> tabState; |
|
234 |
|
235 resize(size); |
|
236 |
|
237 m_navigationBar->setVisible(showToolbar); |
|
238 updateToolbarActionText(showToolbar); |
|
239 |
|
240 m_bookmarksToolbar->setVisible(showBookmarksBar); |
|
241 updateBookmarksToolbarActionText(showBookmarksBar); |
|
242 |
|
243 statusBar()->setVisible(showStatusbar); |
|
244 updateStatusbarActionText(showStatusbar); |
|
245 |
|
246 if (!tabWidget()->restoreState(tabState)) |
|
247 return false; |
|
248 |
|
249 return true; |
|
250 } |
|
251 |
|
252 void BrowserMainWindow::setupMenu() |
|
253 { |
|
254 new QShortcut(QKeySequence(Qt::Key_F6), this, SLOT(slotSwapFocus())); |
|
255 |
|
256 // File |
|
257 QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
|
258 |
|
259 fileMenu->addAction(tr("&New Window"), this, SLOT(slotFileNew()), QKeySequence::New); |
|
260 fileMenu->addAction(m_tabWidget->newTabAction()); |
|
261 fileMenu->addAction(tr("&Open File..."), this, SLOT(slotFileOpen()), QKeySequence::Open); |
|
262 fileMenu->addAction(tr("Open &Location..."), this, |
|
263 SLOT(slotSelectLineEdit()), QKeySequence(Qt::ControlModifier + Qt::Key_L)); |
|
264 fileMenu->addSeparator(); |
|
265 fileMenu->addAction(m_tabWidget->closeTabAction()); |
|
266 fileMenu->addSeparator(); |
|
267 fileMenu->addAction(tr("&Save As..."), this, |
|
268 SLOT(slotFileSaveAs()), QKeySequence(QKeySequence::Save)); |
|
269 fileMenu->addSeparator(); |
|
270 BookmarksManager *bookmarksManager = BrowserApplication::bookmarksManager(); |
|
271 fileMenu->addAction(tr("&Import Bookmarks..."), bookmarksManager, SLOT(importBookmarks())); |
|
272 fileMenu->addAction(tr("&Export Bookmarks..."), bookmarksManager, SLOT(exportBookmarks())); |
|
273 fileMenu->addSeparator(); |
|
274 fileMenu->addAction(tr("P&rint Preview..."), this, SLOT(slotFilePrintPreview())); |
|
275 fileMenu->addAction(tr("&Print..."), this, SLOT(slotFilePrint()), QKeySequence::Print); |
|
276 fileMenu->addSeparator(); |
|
277 QAction *action = fileMenu->addAction(tr("Private &Browsing..."), this, SLOT(slotPrivateBrowsing())); |
|
278 action->setCheckable(true); |
|
279 fileMenu->addSeparator(); |
|
280 |
|
281 #if defined(Q_WS_MAC) |
|
282 fileMenu->addAction(tr("&Quit"), BrowserApplication::instance(), SLOT(quitBrowser()), QKeySequence(Qt::CTRL | Qt::Key_Q)); |
|
283 #else |
|
284 fileMenu->addAction(tr("&Quit"), this, SLOT(close()), QKeySequence(Qt::CTRL | Qt::Key_Q)); |
|
285 #endif |
|
286 |
|
287 // Edit |
|
288 QMenu *editMenu = menuBar()->addMenu(tr("&Edit")); |
|
289 QAction *m_undo = editMenu->addAction(tr("&Undo")); |
|
290 m_undo->setShortcuts(QKeySequence::Undo); |
|
291 m_tabWidget->addWebAction(m_undo, QWebPage::Undo); |
|
292 QAction *m_redo = editMenu->addAction(tr("&Redo")); |
|
293 m_redo->setShortcuts(QKeySequence::Redo); |
|
294 m_tabWidget->addWebAction(m_redo, QWebPage::Redo); |
|
295 editMenu->addSeparator(); |
|
296 QAction *m_cut = editMenu->addAction(tr("Cu&t")); |
|
297 m_cut->setShortcuts(QKeySequence::Cut); |
|
298 m_tabWidget->addWebAction(m_cut, QWebPage::Cut); |
|
299 QAction *m_copy = editMenu->addAction(tr("&Copy")); |
|
300 m_copy->setShortcuts(QKeySequence::Copy); |
|
301 m_tabWidget->addWebAction(m_copy, QWebPage::Copy); |
|
302 QAction *m_paste = editMenu->addAction(tr("&Paste")); |
|
303 m_paste->setShortcuts(QKeySequence::Paste); |
|
304 m_tabWidget->addWebAction(m_paste, QWebPage::Paste); |
|
305 editMenu->addSeparator(); |
|
306 |
|
307 QAction *m_find = editMenu->addAction(tr("&Find")); |
|
308 m_find->setShortcuts(QKeySequence::Find); |
|
309 connect(m_find, SIGNAL(triggered()), this, SLOT(slotEditFind())); |
|
310 new QShortcut(QKeySequence(Qt::Key_Slash), this, SLOT(slotEditFind())); |
|
311 |
|
312 QAction *m_findNext = editMenu->addAction(tr("&Find Next")); |
|
313 m_findNext->setShortcuts(QKeySequence::FindNext); |
|
314 connect(m_findNext, SIGNAL(triggered()), this, SLOT(slotEditFindNext())); |
|
315 |
|
316 QAction *m_findPrevious = editMenu->addAction(tr("&Find Previous")); |
|
317 m_findPrevious->setShortcuts(QKeySequence::FindPrevious); |
|
318 connect(m_findPrevious, SIGNAL(triggered()), this, SLOT(slotEditFindPrevious())); |
|
319 |
|
320 editMenu->addSeparator(); |
|
321 editMenu->addAction(tr("&Preferences"), this, SLOT(slotPreferences()), tr("Ctrl+,")); |
|
322 |
|
323 // View |
|
324 QMenu *viewMenu = menuBar()->addMenu(tr("&View")); |
|
325 |
|
326 m_viewBookmarkBar = new QAction(this); |
|
327 updateBookmarksToolbarActionText(true); |
|
328 m_viewBookmarkBar->setShortcut(tr("Shift+Ctrl+B")); |
|
329 connect(m_viewBookmarkBar, SIGNAL(triggered()), this, SLOT(slotViewBookmarksBar())); |
|
330 viewMenu->addAction(m_viewBookmarkBar); |
|
331 |
|
332 m_viewToolbar = new QAction(this); |
|
333 updateToolbarActionText(true); |
|
334 m_viewToolbar->setShortcut(tr("Ctrl+|")); |
|
335 connect(m_viewToolbar, SIGNAL(triggered()), this, SLOT(slotViewToolbar())); |
|
336 viewMenu->addAction(m_viewToolbar); |
|
337 |
|
338 m_viewStatusbar = new QAction(this); |
|
339 updateStatusbarActionText(true); |
|
340 m_viewStatusbar->setShortcut(tr("Ctrl+/")); |
|
341 connect(m_viewStatusbar, SIGNAL(triggered()), this, SLOT(slotViewStatusbar())); |
|
342 viewMenu->addAction(m_viewStatusbar); |
|
343 |
|
344 viewMenu->addSeparator(); |
|
345 |
|
346 m_stop = viewMenu->addAction(tr("&Stop")); |
|
347 QList<QKeySequence> shortcuts; |
|
348 shortcuts.append(QKeySequence(Qt::CTRL | Qt::Key_Period)); |
|
349 shortcuts.append(Qt::Key_Escape); |
|
350 m_stop->setShortcuts(shortcuts); |
|
351 m_tabWidget->addWebAction(m_stop, QWebPage::Stop); |
|
352 |
|
353 m_reload = viewMenu->addAction(tr("Reload Page")); |
|
354 m_reload->setShortcuts(QKeySequence::Refresh); |
|
355 m_tabWidget->addWebAction(m_reload, QWebPage::Reload); |
|
356 |
|
357 viewMenu->addAction(tr("Zoom &In"), this, SLOT(slotViewZoomIn()), QKeySequence(Qt::CTRL | Qt::Key_Plus)); |
|
358 viewMenu->addAction(tr("Zoom &Out"), this, SLOT(slotViewZoomOut()), QKeySequence(Qt::CTRL | Qt::Key_Minus)); |
|
359 viewMenu->addAction(tr("Reset &Zoom"), this, SLOT(slotViewResetZoom()), QKeySequence(Qt::CTRL | Qt::Key_0)); |
|
360 QAction *zoomTextOnlyAction = viewMenu->addAction(tr("Zoom &Text Only")); |
|
361 connect(zoomTextOnlyAction, SIGNAL(toggled(bool)), this, SLOT(slotViewZoomTextOnly(bool))); |
|
362 zoomTextOnlyAction->setCheckable(true); |
|
363 zoomTextOnlyAction->setChecked(false); |
|
364 |
|
365 viewMenu->addSeparator(); |
|
366 viewMenu->addAction(tr("Page S&ource"), this, SLOT(slotViewPageSource()), tr("Ctrl+Alt+U")); |
|
367 QAction *a = viewMenu->addAction(tr("&Full Screen"), this, SLOT(slotViewFullScreen(bool)), Qt::Key_F11); |
|
368 a->setCheckable(true); |
|
369 |
|
370 // History |
|
371 HistoryMenu *historyMenu = new HistoryMenu(this); |
|
372 connect(historyMenu, SIGNAL(openUrl(QUrl)), |
|
373 m_tabWidget, SLOT(loadUrlInCurrentTab(QUrl))); |
|
374 connect(historyMenu, SIGNAL(hovered(QString)), this, |
|
375 SLOT(slotUpdateStatusbar(QString))); |
|
376 historyMenu->setTitle(tr("Hi&story")); |
|
377 menuBar()->addMenu(historyMenu); |
|
378 QList<QAction*> historyActions; |
|
379 |
|
380 m_historyBack = new QAction(tr("Back"), this); |
|
381 m_tabWidget->addWebAction(m_historyBack, QWebPage::Back); |
|
382 m_historyBack->setShortcuts(QKeySequence::Back); |
|
383 m_historyBack->setIconVisibleInMenu(false); |
|
384 |
|
385 m_historyForward = new QAction(tr("Forward"), this); |
|
386 m_tabWidget->addWebAction(m_historyForward, QWebPage::Forward); |
|
387 m_historyForward->setShortcuts(QKeySequence::Forward); |
|
388 m_historyForward->setIconVisibleInMenu(false); |
|
389 |
|
390 QAction *m_historyHome = new QAction(tr("Home"), this); |
|
391 connect(m_historyHome, SIGNAL(triggered()), this, SLOT(slotHome())); |
|
392 m_historyHome->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_H)); |
|
393 |
|
394 m_restoreLastSession = new QAction(tr("Restore Last Session"), this); |
|
395 connect(m_restoreLastSession, SIGNAL(triggered()), BrowserApplication::instance(), SLOT(restoreLastSession())); |
|
396 m_restoreLastSession->setEnabled(BrowserApplication::instance()->canRestoreSession()); |
|
397 |
|
398 historyActions.append(m_historyBack); |
|
399 historyActions.append(m_historyForward); |
|
400 historyActions.append(m_historyHome); |
|
401 historyActions.append(m_tabWidget->recentlyClosedTabsAction()); |
|
402 historyActions.append(m_restoreLastSession); |
|
403 historyMenu->setInitialActions(historyActions); |
|
404 |
|
405 // Bookmarks |
|
406 BookmarksMenu *bookmarksMenu = new BookmarksMenu(this); |
|
407 connect(bookmarksMenu, SIGNAL(openUrl(QUrl)), |
|
408 m_tabWidget, SLOT(loadUrlInCurrentTab(QUrl))); |
|
409 connect(bookmarksMenu, SIGNAL(hovered(QString)), |
|
410 this, SLOT(slotUpdateStatusbar(QString))); |
|
411 bookmarksMenu->setTitle(tr("&Bookmarks")); |
|
412 menuBar()->addMenu(bookmarksMenu); |
|
413 |
|
414 QList<QAction*> bookmarksActions; |
|
415 |
|
416 QAction *showAllBookmarksAction = new QAction(tr("Show All Bookmarks"), this); |
|
417 connect(showAllBookmarksAction, SIGNAL(triggered()), this, SLOT(slotShowBookmarksDialog())); |
|
418 m_addBookmark = new QAction(QIcon(QLatin1String(":addbookmark.png")), tr("Add Bookmark..."), this); |
|
419 m_addBookmark->setIconVisibleInMenu(false); |
|
420 |
|
421 connect(m_addBookmark, SIGNAL(triggered()), this, SLOT(slotAddBookmark())); |
|
422 m_addBookmark->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_D)); |
|
423 |
|
424 bookmarksActions.append(showAllBookmarksAction); |
|
425 bookmarksActions.append(m_addBookmark); |
|
426 bookmarksMenu->setInitialActions(bookmarksActions); |
|
427 |
|
428 // Window |
|
429 m_windowMenu = menuBar()->addMenu(tr("&Window")); |
|
430 connect(m_windowMenu, SIGNAL(aboutToShow()), |
|
431 this, SLOT(slotAboutToShowWindowMenu())); |
|
432 slotAboutToShowWindowMenu(); |
|
433 |
|
434 QMenu *toolsMenu = menuBar()->addMenu(tr("&Tools")); |
|
435 toolsMenu->addAction(tr("Web &Search"), this, SLOT(slotWebSearch()), QKeySequence(tr("Ctrl+K", "Web Search"))); |
|
436 #ifndef Q_CC_MINGW |
|
437 a = toolsMenu->addAction(tr("Enable Web &Inspector"), this, SLOT(slotToggleInspector(bool))); |
|
438 a->setCheckable(true); |
|
439 #endif |
|
440 |
|
441 QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); |
|
442 helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt())); |
|
443 helpMenu->addAction(tr("About &Demo Browser"), this, SLOT(slotAboutApplication())); |
|
444 } |
|
445 |
|
446 void BrowserMainWindow::setupToolBar() |
|
447 { |
|
448 setUnifiedTitleAndToolBarOnMac(true); |
|
449 m_navigationBar = addToolBar(tr("Navigation")); |
|
450 connect(m_navigationBar->toggleViewAction(), SIGNAL(toggled(bool)), |
|
451 this, SLOT(updateToolbarActionText(bool))); |
|
452 |
|
453 m_historyBack->setIcon(style()->standardIcon(QStyle::SP_ArrowBack, 0, this)); |
|
454 m_historyBackMenu = new QMenu(this); |
|
455 m_historyBack->setMenu(m_historyBackMenu); |
|
456 connect(m_historyBackMenu, SIGNAL(aboutToShow()), |
|
457 this, SLOT(slotAboutToShowBackMenu())); |
|
458 connect(m_historyBackMenu, SIGNAL(triggered(QAction*)), |
|
459 this, SLOT(slotOpenActionUrl(QAction*))); |
|
460 m_navigationBar->addAction(m_historyBack); |
|
461 |
|
462 m_historyForward->setIcon(style()->standardIcon(QStyle::SP_ArrowForward, 0, this)); |
|
463 m_historyForwardMenu = new QMenu(this); |
|
464 connect(m_historyForwardMenu, SIGNAL(aboutToShow()), |
|
465 this, SLOT(slotAboutToShowForwardMenu())); |
|
466 connect(m_historyForwardMenu, SIGNAL(triggered(QAction*)), |
|
467 this, SLOT(slotOpenActionUrl(QAction*))); |
|
468 m_historyForward->setMenu(m_historyForwardMenu); |
|
469 m_navigationBar->addAction(m_historyForward); |
|
470 |
|
471 m_stopReload = new QAction(this); |
|
472 m_reloadIcon = style()->standardIcon(QStyle::SP_BrowserReload); |
|
473 m_stopReload->setIcon(m_reloadIcon); |
|
474 |
|
475 m_navigationBar->addAction(m_stopReload); |
|
476 |
|
477 m_navigationBar->addWidget(m_tabWidget->lineEditStack()); |
|
478 |
|
479 m_toolbarSearch = new ToolbarSearch(m_navigationBar); |
|
480 m_navigationBar->addWidget(m_toolbarSearch); |
|
481 connect(m_toolbarSearch, SIGNAL(search(QUrl)), SLOT(loadUrl(QUrl))); |
|
482 |
|
483 m_chaseWidget = new ChaseWidget(this); |
|
484 m_navigationBar->addWidget(m_chaseWidget); |
|
485 } |
|
486 |
|
487 void BrowserMainWindow::slotShowBookmarksDialog() |
|
488 { |
|
489 BookmarksDialog *dialog = new BookmarksDialog(this); |
|
490 connect(dialog, SIGNAL(openUrl(QUrl)), |
|
491 m_tabWidget, SLOT(loadUrlInCurrentTab(QUrl))); |
|
492 dialog->show(); |
|
493 } |
|
494 |
|
495 void BrowserMainWindow::slotAddBookmark() |
|
496 { |
|
497 WebView *webView = currentTab(); |
|
498 QString url = webView->url().toString(); |
|
499 QString title = webView->title(); |
|
500 AddBookmarkDialog dialog(url, title); |
|
501 dialog.exec(); |
|
502 } |
|
503 |
|
504 void BrowserMainWindow::slotViewToolbar() |
|
505 { |
|
506 if (m_navigationBar->isVisible()) { |
|
507 updateToolbarActionText(false); |
|
508 m_navigationBar->close(); |
|
509 } else { |
|
510 updateToolbarActionText(true); |
|
511 m_navigationBar->show(); |
|
512 } |
|
513 m_autoSaver->changeOccurred(); |
|
514 } |
|
515 |
|
516 void BrowserMainWindow::slotViewBookmarksBar() |
|
517 { |
|
518 if (m_bookmarksToolbar->isVisible()) { |
|
519 updateBookmarksToolbarActionText(false); |
|
520 m_bookmarksToolbar->close(); |
|
521 } else { |
|
522 updateBookmarksToolbarActionText(true); |
|
523 m_bookmarksToolbar->show(); |
|
524 } |
|
525 m_autoSaver->changeOccurred(); |
|
526 } |
|
527 |
|
528 void BrowserMainWindow::updateStatusbarActionText(bool visible) |
|
529 { |
|
530 m_viewStatusbar->setText(!visible ? tr("Show Status Bar") : tr("Hide Status Bar")); |
|
531 } |
|
532 |
|
533 void BrowserMainWindow::updateToolbarActionText(bool visible) |
|
534 { |
|
535 m_viewToolbar->setText(!visible ? tr("Show Toolbar") : tr("Hide Toolbar")); |
|
536 } |
|
537 |
|
538 void BrowserMainWindow::updateBookmarksToolbarActionText(bool visible) |
|
539 { |
|
540 m_viewBookmarkBar->setText(!visible ? tr("Show Bookmarks bar") : tr("Hide Bookmarks bar")); |
|
541 } |
|
542 |
|
543 void BrowserMainWindow::slotViewStatusbar() |
|
544 { |
|
545 if (statusBar()->isVisible()) { |
|
546 updateStatusbarActionText(false); |
|
547 statusBar()->close(); |
|
548 } else { |
|
549 updateStatusbarActionText(true); |
|
550 statusBar()->show(); |
|
551 } |
|
552 m_autoSaver->changeOccurred(); |
|
553 } |
|
554 |
|
555 void BrowserMainWindow::loadUrl(const QUrl &url) |
|
556 { |
|
557 if (!currentTab() || !url.isValid()) |
|
558 return; |
|
559 |
|
560 m_tabWidget->currentLineEdit()->setText(QString::fromUtf8(url.toEncoded())); |
|
561 m_tabWidget->loadUrlInCurrentTab(url); |
|
562 } |
|
563 |
|
564 void BrowserMainWindow::slotDownloadManager() |
|
565 { |
|
566 BrowserApplication::downloadManager()->show(); |
|
567 } |
|
568 |
|
569 void BrowserMainWindow::slotSelectLineEdit() |
|
570 { |
|
571 m_tabWidget->currentLineEdit()->selectAll(); |
|
572 m_tabWidget->currentLineEdit()->setFocus(); |
|
573 } |
|
574 |
|
575 void BrowserMainWindow::slotFileSaveAs() |
|
576 { |
|
577 BrowserApplication::downloadManager()->download(currentTab()->url(), true); |
|
578 } |
|
579 |
|
580 void BrowserMainWindow::slotPreferences() |
|
581 { |
|
582 SettingsDialog *s = new SettingsDialog(this); |
|
583 s->show(); |
|
584 } |
|
585 |
|
586 void BrowserMainWindow::slotUpdateStatusbar(const QString &string) |
|
587 { |
|
588 statusBar()->showMessage(string, 2000); |
|
589 } |
|
590 |
|
591 void BrowserMainWindow::slotUpdateWindowTitle(const QString &title) |
|
592 { |
|
593 if (title.isEmpty()) { |
|
594 setWindowTitle(tr("Qt Demo Browser")); |
|
595 } else { |
|
596 #if defined(Q_WS_MAC) |
|
597 setWindowTitle(title); |
|
598 #else |
|
599 setWindowTitle(tr("%1 - Qt Demo Browser", "Page title and Browser name").arg(title)); |
|
600 #endif |
|
601 } |
|
602 } |
|
603 |
|
604 void BrowserMainWindow::slotAboutApplication() |
|
605 { |
|
606 QMessageBox::about(this, tr("About"), tr( |
|
607 "Version %1" |
|
608 "<p>This demo demonstrates Qt's " |
|
609 "webkit facilities in action, providing an example " |
|
610 "browser for you to experiment with.<p>" |
|
611 "<p>QtWebKit is based on the Open Source WebKit Project developed at <a href=\"http://webkit.org/\">http://webkit.org/</a>." |
|
612 ).arg(QCoreApplication::applicationVersion())); |
|
613 } |
|
614 |
|
615 void BrowserMainWindow::slotFileNew() |
|
616 { |
|
617 BrowserApplication::instance()->newMainWindow(); |
|
618 BrowserMainWindow *mw = BrowserApplication::instance()->mainWindow(); |
|
619 mw->slotHome(); |
|
620 } |
|
621 |
|
622 void BrowserMainWindow::slotFileOpen() |
|
623 { |
|
624 QString file = QFileDialog::getOpenFileName(this, tr("Open Web Resource"), QString(), |
|
625 tr("Web Resources (*.html *.htm *.svg *.png *.gif *.svgz);;All files (*.*)")); |
|
626 |
|
627 if (file.isEmpty()) |
|
628 return; |
|
629 |
|
630 loadPage(file); |
|
631 } |
|
632 |
|
633 void BrowserMainWindow::slotFilePrintPreview() |
|
634 { |
|
635 #ifndef QT_NO_PRINTER |
|
636 if (!currentTab()) |
|
637 return; |
|
638 QPrintPreviewDialog *dialog = new QPrintPreviewDialog(this); |
|
639 connect(dialog, SIGNAL(paintRequested(QPrinter*)), |
|
640 currentTab(), SLOT(print(QPrinter*))); |
|
641 dialog->exec(); |
|
642 #endif |
|
643 } |
|
644 |
|
645 void BrowserMainWindow::slotFilePrint() |
|
646 { |
|
647 if (!currentTab()) |
|
648 return; |
|
649 printRequested(currentTab()->page()->mainFrame()); |
|
650 } |
|
651 |
|
652 void BrowserMainWindow::printRequested(QWebFrame *frame) |
|
653 { |
|
654 #ifndef QT_NO_PRINTER |
|
655 QPrinter printer; |
|
656 QPrintDialog *dialog = new QPrintDialog(&printer, this); |
|
657 dialog->setWindowTitle(tr("Print Document")); |
|
658 if (dialog->exec() != QDialog::Accepted) |
|
659 return; |
|
660 frame->print(&printer); |
|
661 #endif |
|
662 } |
|
663 |
|
664 void BrowserMainWindow::slotPrivateBrowsing() |
|
665 { |
|
666 QWebSettings *settings = QWebSettings::globalSettings(); |
|
667 bool pb = settings->testAttribute(QWebSettings::PrivateBrowsingEnabled); |
|
668 if (!pb) { |
|
669 QString title = tr("Are you sure you want to turn on private browsing?"); |
|
670 QString text = tr("<b>%1</b><br><br>When private browsing in turned on," |
|
671 " webpages are not added to the history," |
|
672 " items are automatically removed from the Downloads window," \ |
|
673 " new cookies are not stored, current cookies can't be accessed," \ |
|
674 " site icons wont be stored, session wont be saved, " \ |
|
675 " and searches are not addded to the pop-up menu in the Google search box." \ |
|
676 " Until you close the window, you can still click the Back and Forward buttons" \ |
|
677 " to return to the webpages you have opened.").arg(title); |
|
678 |
|
679 QMessageBox::StandardButton button = QMessageBox::question(this, QString(), text, |
|
680 QMessageBox::Ok | QMessageBox::Cancel, |
|
681 QMessageBox::Ok); |
|
682 if (button == QMessageBox::Ok) { |
|
683 settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true); |
|
684 } |
|
685 } else { |
|
686 settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, false); |
|
687 |
|
688 QList<BrowserMainWindow*> windows = BrowserApplication::instance()->mainWindows(); |
|
689 for (int i = 0; i < windows.count(); ++i) { |
|
690 BrowserMainWindow *window = windows.at(i); |
|
691 window->m_lastSearch = QString::null; |
|
692 window->tabWidget()->clear(); |
|
693 } |
|
694 } |
|
695 } |
|
696 |
|
697 void BrowserMainWindow::closeEvent(QCloseEvent *event) |
|
698 { |
|
699 if (m_tabWidget->count() > 1) { |
|
700 int ret = QMessageBox::warning(this, QString(), |
|
701 tr("Are you sure you want to close the window?" |
|
702 " There are %1 tabs open").arg(m_tabWidget->count()), |
|
703 QMessageBox::Yes | QMessageBox::No, |
|
704 QMessageBox::No); |
|
705 if (ret == QMessageBox::No) { |
|
706 event->ignore(); |
|
707 return; |
|
708 } |
|
709 } |
|
710 event->accept(); |
|
711 deleteLater(); |
|
712 } |
|
713 |
|
714 void BrowserMainWindow::slotEditFind() |
|
715 { |
|
716 if (!currentTab()) |
|
717 return; |
|
718 bool ok; |
|
719 QString search = QInputDialog::getText(this, tr("Find"), |
|
720 tr("Text:"), QLineEdit::Normal, |
|
721 m_lastSearch, &ok); |
|
722 if (ok && !search.isEmpty()) { |
|
723 m_lastSearch = search; |
|
724 if (!currentTab()->findText(m_lastSearch)) |
|
725 slotUpdateStatusbar(tr("\"%1\" not found.").arg(m_lastSearch)); |
|
726 } |
|
727 } |
|
728 |
|
729 void BrowserMainWindow::slotEditFindNext() |
|
730 { |
|
731 if (!currentTab() && !m_lastSearch.isEmpty()) |
|
732 return; |
|
733 currentTab()->findText(m_lastSearch); |
|
734 } |
|
735 |
|
736 void BrowserMainWindow::slotEditFindPrevious() |
|
737 { |
|
738 if (!currentTab() && !m_lastSearch.isEmpty()) |
|
739 return; |
|
740 currentTab()->findText(m_lastSearch, QWebPage::FindBackward); |
|
741 } |
|
742 |
|
743 void BrowserMainWindow::slotViewZoomIn() |
|
744 { |
|
745 if (!currentTab()) |
|
746 return; |
|
747 currentTab()->setZoomFactor(currentTab()->zoomFactor() + 0.1); |
|
748 } |
|
749 |
|
750 void BrowserMainWindow::slotViewZoomOut() |
|
751 { |
|
752 if (!currentTab()) |
|
753 return; |
|
754 currentTab()->setZoomFactor(currentTab()->zoomFactor() - 0.1); |
|
755 } |
|
756 |
|
757 void BrowserMainWindow::slotViewResetZoom() |
|
758 { |
|
759 if (!currentTab()) |
|
760 return; |
|
761 currentTab()->setZoomFactor(1.0); |
|
762 } |
|
763 |
|
764 void BrowserMainWindow::slotViewZoomTextOnly(bool enable) |
|
765 { |
|
766 if (!currentTab()) |
|
767 return; |
|
768 currentTab()->page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, enable); |
|
769 } |
|
770 |
|
771 void BrowserMainWindow::slotViewFullScreen(bool makeFullScreen) |
|
772 { |
|
773 if (makeFullScreen) { |
|
774 showFullScreen(); |
|
775 } else { |
|
776 if (isMinimized()) |
|
777 showMinimized(); |
|
778 else if (isMaximized()) |
|
779 showMaximized(); |
|
780 else showNormal(); |
|
781 } |
|
782 } |
|
783 |
|
784 void BrowserMainWindow::slotViewPageSource() |
|
785 { |
|
786 if (!currentTab()) |
|
787 return; |
|
788 |
|
789 QString markup = currentTab()->page()->mainFrame()->toHtml(); |
|
790 QPlainTextEdit *view = new QPlainTextEdit(markup); |
|
791 view->setWindowTitle(tr("Page Source of %1").arg(currentTab()->title())); |
|
792 view->setMinimumWidth(640); |
|
793 view->setAttribute(Qt::WA_DeleteOnClose); |
|
794 view->show(); |
|
795 } |
|
796 |
|
797 void BrowserMainWindow::slotHome() |
|
798 { |
|
799 QSettings settings; |
|
800 settings.beginGroup(QLatin1String("MainWindow")); |
|
801 QString home = settings.value(QLatin1String("home"), QLatin1String("http://qt.nokia.com/")).toString(); |
|
802 loadPage(home); |
|
803 } |
|
804 |
|
805 void BrowserMainWindow::slotWebSearch() |
|
806 { |
|
807 m_toolbarSearch->lineEdit()->selectAll(); |
|
808 m_toolbarSearch->lineEdit()->setFocus(); |
|
809 } |
|
810 |
|
811 void BrowserMainWindow::slotToggleInspector(bool enable) |
|
812 { |
|
813 QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, enable); |
|
814 if (enable) { |
|
815 int result = QMessageBox::question(this, tr("Web Inspector"), |
|
816 tr("The web inspector will only work correctly for pages that were loaded after enabling.\n" |
|
817 "Do you want to reload all pages?"), |
|
818 QMessageBox::Yes | QMessageBox::No); |
|
819 if (result == QMessageBox::Yes) { |
|
820 m_tabWidget->reloadAllTabs(); |
|
821 } |
|
822 } |
|
823 } |
|
824 |
|
825 void BrowserMainWindow::slotSwapFocus() |
|
826 { |
|
827 if (currentTab()->hasFocus()) |
|
828 m_tabWidget->currentLineEdit()->setFocus(); |
|
829 else |
|
830 currentTab()->setFocus(); |
|
831 } |
|
832 |
|
833 void BrowserMainWindow::loadPage(const QString &page) |
|
834 { |
|
835 QUrl url = QUrl::fromUserInput(page); |
|
836 loadUrl(url); |
|
837 } |
|
838 |
|
839 TabWidget *BrowserMainWindow::tabWidget() const |
|
840 { |
|
841 return m_tabWidget; |
|
842 } |
|
843 |
|
844 WebView *BrowserMainWindow::currentTab() const |
|
845 { |
|
846 return m_tabWidget->currentWebView(); |
|
847 } |
|
848 |
|
849 void BrowserMainWindow::slotLoadProgress(int progress) |
|
850 { |
|
851 if (progress < 100 && progress > 0) { |
|
852 m_chaseWidget->setAnimated(true); |
|
853 disconnect(m_stopReload, SIGNAL(triggered()), m_reload, SLOT(trigger())); |
|
854 if (m_stopIcon.isNull()) |
|
855 m_stopIcon = style()->standardIcon(QStyle::SP_BrowserStop); |
|
856 m_stopReload->setIcon(m_stopIcon); |
|
857 connect(m_stopReload, SIGNAL(triggered()), m_stop, SLOT(trigger())); |
|
858 m_stopReload->setToolTip(tr("Stop loading the current page")); |
|
859 } else { |
|
860 m_chaseWidget->setAnimated(false); |
|
861 disconnect(m_stopReload, SIGNAL(triggered()), m_stop, SLOT(trigger())); |
|
862 m_stopReload->setIcon(m_reloadIcon); |
|
863 connect(m_stopReload, SIGNAL(triggered()), m_reload, SLOT(trigger())); |
|
864 m_stopReload->setToolTip(tr("Reload the current page")); |
|
865 } |
|
866 } |
|
867 |
|
868 void BrowserMainWindow::slotAboutToShowBackMenu() |
|
869 { |
|
870 m_historyBackMenu->clear(); |
|
871 if (!currentTab()) |
|
872 return; |
|
873 QWebHistory *history = currentTab()->history(); |
|
874 int historyCount = history->count(); |
|
875 for (int i = history->backItems(historyCount).count() - 1; i >= 0; --i) { |
|
876 QWebHistoryItem item = history->backItems(history->count()).at(i); |
|
877 QAction *action = new QAction(this); |
|
878 action->setData(-1*(historyCount-i-1)); |
|
879 QIcon icon = BrowserApplication::instance()->icon(item.url()); |
|
880 action->setIcon(icon); |
|
881 action->setText(item.title()); |
|
882 m_historyBackMenu->addAction(action); |
|
883 } |
|
884 } |
|
885 |
|
886 void BrowserMainWindow::slotAboutToShowForwardMenu() |
|
887 { |
|
888 m_historyForwardMenu->clear(); |
|
889 if (!currentTab()) |
|
890 return; |
|
891 QWebHistory *history = currentTab()->history(); |
|
892 int historyCount = history->count(); |
|
893 for (int i = 0; i < history->forwardItems(history->count()).count(); ++i) { |
|
894 QWebHistoryItem item = history->forwardItems(historyCount).at(i); |
|
895 QAction *action = new QAction(this); |
|
896 action->setData(historyCount-i); |
|
897 QIcon icon = BrowserApplication::instance()->icon(item.url()); |
|
898 action->setIcon(icon); |
|
899 action->setText(item.title()); |
|
900 m_historyForwardMenu->addAction(action); |
|
901 } |
|
902 } |
|
903 |
|
904 void BrowserMainWindow::slotAboutToShowWindowMenu() |
|
905 { |
|
906 m_windowMenu->clear(); |
|
907 m_windowMenu->addAction(m_tabWidget->nextTabAction()); |
|
908 m_windowMenu->addAction(m_tabWidget->previousTabAction()); |
|
909 m_windowMenu->addSeparator(); |
|
910 m_windowMenu->addAction(tr("Downloads"), this, SLOT(slotDownloadManager()), QKeySequence(tr("Alt+Ctrl+L", "Download Manager"))); |
|
911 |
|
912 m_windowMenu->addSeparator(); |
|
913 QList<BrowserMainWindow*> windows = BrowserApplication::instance()->mainWindows(); |
|
914 for (int i = 0; i < windows.count(); ++i) { |
|
915 BrowserMainWindow *window = windows.at(i); |
|
916 QAction *action = m_windowMenu->addAction(window->windowTitle(), this, SLOT(slotShowWindow())); |
|
917 action->setData(i); |
|
918 action->setCheckable(true); |
|
919 if (window == this) |
|
920 action->setChecked(true); |
|
921 } |
|
922 } |
|
923 |
|
924 void BrowserMainWindow::slotShowWindow() |
|
925 { |
|
926 if (QAction *action = qobject_cast<QAction*>(sender())) { |
|
927 QVariant v = action->data(); |
|
928 if (v.canConvert<int>()) { |
|
929 int offset = qvariant_cast<int>(v); |
|
930 QList<BrowserMainWindow*> windows = BrowserApplication::instance()->mainWindows(); |
|
931 windows.at(offset)->activateWindow(); |
|
932 windows.at(offset)->currentTab()->setFocus(); |
|
933 } |
|
934 } |
|
935 } |
|
936 |
|
937 void BrowserMainWindow::slotOpenActionUrl(QAction *action) |
|
938 { |
|
939 int offset = action->data().toInt(); |
|
940 QWebHistory *history = currentTab()->history(); |
|
941 if (offset < 0) |
|
942 history->goToItem(history->backItems(-1*offset).first()); // back |
|
943 else if (offset > 0) |
|
944 history->goToItem(history->forwardItems(history->count() - offset + 1).back()); // forward |
|
945 } |
|
946 |
|
947 void BrowserMainWindow::geometryChangeRequested(const QRect &geometry) |
|
948 { |
|
949 setGeometry(geometry); |
|
950 } |
|
951 |