1#############################################################################
2##
3## Copyright (C) 2013 Riverbank Computing Limited.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
8##
9## $QT_BEGIN_LICENSE:BSD$
10## You may use this file under the terms of the BSD license as follows:
11##
12## "Redistribution and use in source and binary forms, with or without
13## modification, are permitted provided that the following conditions are
14## met:
15##   * Redistributions of source code must retain the above copyright
16##     notice, this list of conditions and the following disclaimer.
17##   * Redistributions in binary form must reproduce the above copyright
18##     notice, this list of conditions and the following disclaimer in
19##     the documentation and/or other materials provided with the
20##     distribution.
21##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22##     the names of its contributors may be used to endorse or promote
23##     products derived from this software without specific prior written
24##     permission.
25##
26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37## $QT_END_LICENSE$
38##
39#############################################################################
40
41
42import sys
43
44from PyQt5.QtCore import pyqtSlot
45from PyQt5.QtWidgets import (QApplication, QLabel, QLineEdit, QMainWindow,
46        QMessageBox, QProgressBar)
47
48import mainwindow_rc
49from ui_mainwindow import Ui_MainWindow
50
51
52class MainWindow(QMainWindow, Ui_MainWindow):
53    # Maintain the list of browser windows so that they do not get garbage
54    # collected.
55    _window_list = []
56
57    def __init__(self):
58        super(MainWindow, self).__init__()
59
60        MainWindow._window_list.append(self)
61
62        self.setupUi(self)
63
64        # Qt Designer (at least to v4.2.1) can't handle arbitrary widgets in a
65        # QToolBar - even though uic can, and they are in the original .ui
66        # file.  Therefore we manually add the problematic widgets.
67        self.lblAddress = QLabel("Address", self.tbAddress)
68        self.tbAddress.insertWidget(self.actionGo, self.lblAddress)
69        self.addressEdit = QLineEdit(self.tbAddress)
70        self.tbAddress.insertWidget(self.actionGo, self.addressEdit)
71
72        self.addressEdit.returnPressed.connect(self.actionGo.trigger)
73        self.actionBack.triggered.connect(self.WebBrowser.GoBack)
74        self.actionForward.triggered.connect(self.WebBrowser.GoForward)
75        self.actionStop.triggered.connect(self.WebBrowser.Stop)
76        self.actionRefresh.triggered.connect(self.WebBrowser.Refresh)
77        self.actionHome.triggered.connect(self.WebBrowser.GoHome)
78        self.actionSearch.triggered.connect(self.WebBrowser.GoSearch)
79
80        self.pb = QProgressBar(self.statusBar())
81        self.pb.setTextVisible(False)
82        self.pb.hide()
83        self.statusBar().addPermanentWidget(self.pb)
84
85        self.WebBrowser.dynamicCall('GoHome()')
86
87    def closeEvent(self, e):
88        MainWindow._window_list.remove(self)
89        e.accept()
90
91    def on_WebBrowser_TitleChange(self, title):
92        self.setWindowTitle("Qt WebBrowser - " + title)
93
94    def on_WebBrowser_ProgressChange(self, a, b):
95        if a <= 0 or b <= 0:
96            self.pb.hide()
97            return
98
99        self.pb.show()
100        self.pb.setRange(0, b)
101        self.pb.setValue(a)
102
103    def on_WebBrowser_CommandStateChange(self, cmd, on):
104        if cmd == 1:
105            self.actionForward.setEnabled(on)
106        elif cmd == 2:
107            self.actionBack.setEnabled(on)
108
109    def on_WebBrowser_BeforeNavigate(self):
110        self.actionStop.setEnabled(True)
111
112    def on_WebBrowser_NavigateComplete(self, _):
113        self.actionStop.setEnabled(False)
114
115    @pyqtSlot()
116    def on_actionGo_triggered(self):
117        self.WebBrowser.dynamicCall('Navigate(const QString&)',
118                self.addressEdit.text())
119
120    @pyqtSlot()
121    def on_actionNewWindow_triggered(self):
122        window = MainWindow()
123        window.show()
124        if self.addressEdit.text().isEmpty():
125            return;
126
127        window.addressEdit.setText(self.addressEdit.text())
128        window.actionStop.setEnabled(True)
129        window.on_actionGo_triggered()
130
131    @pyqtSlot()
132    def on_actionAbout_triggered(self):
133        QMessageBox.about(self, "About WebBrowser",
134                "This Example has been created using the ActiveQt integration into Qt Designer.\n"
135                "It demonstrates the use of QAxWidget to embed the Internet Explorer ActiveX\n"
136                "control into a Qt application.")
137
138    @pyqtSlot()
139    def on_actionAboutQt_triggered(self):
140        QMessageBox.aboutQt(self, "About Qt")
141
142
143if __name__ == "__main__":
144    a = QApplication(sys.argv)
145    w = MainWindow()
146    w.show()
147    sys.exit(a.exec_())
148