1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited
7## Copyright (C) 2010 Hans-Peter Jansen <hpj@urpla.net>.
8## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
9## All rights reserved.
10##
11## This file is part of the examples of PyQt.
12##
13## $QT_BEGIN_LICENSE:BSD$
14## You may use this file under the terms of the BSD license as follows:
15##
16## "Redistribution and use in source and binary forms, with or without
17## modification, are permitted provided that the following conditions are
18## met:
19##   * Redistributions of source code must retain the above copyright
20##     notice, this list of conditions and the following disclaimer.
21##   * Redistributions in binary form must reproduce the above copyright
22##     notice, this list of conditions and the following disclaimer in
23##     the documentation and/or other materials provided with the
24##     distribution.
25##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
26##     the names of its contributors may be used to endorse or promote
27##     products derived from this software without specific prior written
28##     permission.
29##
30## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
41## $QT_END_LICENSE$
42##
43###########################################################################
44
45
46from PyQt5.QtCore import pyqtSlot, QUrl
47from PyQt5.QtGui import QKeySequence
48from PyQt5.QtWidgets import (QAction, QApplication, QMainWindow, QMessageBox,
49        QWidget)
50
51import formextractor_rc
52from ui_formextractor import Ui_Form
53
54
55class FormExtractor(QWidget):
56    def __init__(self, parent=None):
57        super(FormExtractor, self).__init__(parent)
58
59        self.ui = Ui_Form()
60        self.ui.setupUi(self)
61
62        webView = self.ui.webView
63        webView.setUrl(QUrl('qrc:/form.html'))
64        webView.page().mainFrame().javaScriptWindowObjectCleared.connect(
65                self.populateJavaScriptWindowObject)
66
67        self.resize(300, 300)
68
69    @pyqtSlot()
70    def submit(self):
71        frame = self.ui.webView.page().mainFrame()
72        firstName = frame.findFirstElement('#firstname')
73        lastName = frame.findFirstElement('#lastname')
74        maleGender = frame.findFirstElement('#genderMale')
75        femaleGender = frame.findFirstElement('#genderFemale')
76        updates = frame.findFirstElement('#updates')
77
78        self.ui.firstNameEdit.setText(firstName.evaluateJavaScript('this.value'))
79        self.ui.lastNameEdit.setText(lastName.evaluateJavaScript('this.value'))
80
81        if maleGender.evaluateJavaScript('this.checked'):
82            self.ui.genderEdit.setText(
83                    maleGender.evaluateJavaScript('this.value'))
84        elif femaleGender.evaluateJavaScript('this.checked'):
85            self.ui.genderEdit.setText(
86                    femaleGender.evaluateJavaScript('this.value'))
87
88        if updates.evaluateJavaScript('this.checked'):
89            self.ui.updatesEdit.setText("Yes")
90        else:
91            self.ui.updatesEdit.setText("No")
92
93    def populateJavaScriptWindowObject(self):
94        self.ui.webView.page().mainFrame().addToJavaScriptWindowObject(
95                'formExtractor', self)
96
97
98class MainWindow(QMainWindow):
99    def __init__(self):
100        super(MainWindow, self).__init__()
101
102        self.createActions()
103        self.createMenus()
104        self.centralWidget = FormExtractor(self)
105        self.setCentralWidget(self.centralWidget)
106
107    def createActions(self):
108        self.exitAct = QAction("E&xit", self, statusTip="Exit the application",
109                shortcut=QKeySequence.Quit, triggered=self.close)
110
111        self.aboutAct = QAction("&About", self,
112                statusTip="Show the application's About box",
113                triggered=self.about)
114
115        self.aboutQtAct = QAction("About &Qt", self,
116                statusTip="Show the Qt library's About box",
117                triggered=QApplication.instance().aboutQt)
118
119    def createMenus(self):
120        fileMenu = self.menuBar().addMenu("&File")
121        fileMenu.addAction(self.exitAct)
122        self.menuBar().addSeparator()
123        helpMenu = self.menuBar().addMenu("&Help")
124        helpMenu.addAction(self.aboutAct)
125        helpMenu.addAction(self.aboutQtAct)
126
127    def about(self):
128        QMessageBox.about(self, "About Form Extractor",
129                "The <b>Form Extractor</b> example demonstrates how to "
130                "extract data from a web form using QtWebKit.")
131
132
133if __name__ == '__main__':
134
135    import sys
136
137    app = QApplication(sys.argv)
138
139    mainWindow = MainWindow()
140    mainWindow.setWindowTitle("Form Extractor")
141    mainWindow.show()
142
143    sys.exit(app.exec_())
144