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