Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Python Hack-a-thon 2010.11 / PyQt4 Hands on
- About QtWebKit 03 : ScreenShot機能を付けてみる
- """
- import sys
- import time
- from PyQt4 import QtCore
- from PyQt4 import QtGui
- from PyQt4 import QtWebKit
- from MainWindowUI import Ui_MainWindow
- from ScreenShot import ScreenShot
- class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
- def __init__(self, *args, **kw):
- QtGui.QMainWindow.__init__(self, *args, **kw)
- self.setupUi(self)
- self.web_page = self.webView.page()
- self.main_frame = self.web_page.mainFrame()
- self.setAddressBarUrl(self.webView.url())
- self.back_button.clicked.connect(self.webView.back)
- self.forward_button.clicked.connect(self.webView.forward)
- self.web_page.linkHovered.connect(self.showUrlInStatusBar)
- self.actionQuit.setShortcut("Ctrl+Q")
- self.load_progress_bar = QtGui.QProgressBar()
- self.statusBar().addPermanentWidget(self.load_progress_bar)
- self.statusBar().setSizeGripEnabled(False)
- self.load_progress_bar.setSizePolicy(
- QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred))
- progress_bar_size = self.load_progress_bar.size()
- progress_bar_size.setWidth(150)
- self.load_progress_bar.setMinimumSize(progress_bar_size)
- self.load_progress_bar.resize(progress_bar_size)
- self.screenshot = ScreenShot()
- self.screenshot.renderFinished.connect(self.saveScreenShot)
- @QtCore.pyqtSlot(bool, name="on_actionQuit_triggered")
- def quitApplication(self, checked=False):
- sys.exit(QtGui.QApplication.instance().quit())
- @QtCore.pyqtSlot(name="on_address_bar_returnPressed")
- def loadNewPage(self):
- url = self.address_bar.text()
- if not QtCore.QRegExp(ur"^(https?)|(ftp)://").exactMatch(url):
- url_str = QtCore.QString(u"http://") + url
- self.webView.setUrl(QtCore.QUrl(url_str))
- @QtCore.pyqtSlot(QtCore.QUrl, name="on_webView_urlChanged")
- def setAddressBarUrl(self, url):
- self.address_bar.setText(url.toString())
- def showUrlInStatusBar(self, link, title, textContent):
- self.statusBar().showMessage(link)
- @QtCore.pyqtSlot(QtCore.QUrl, name="on_webView_linkClicked")
- def setAddressBarAndLoadPage(self, url):
- self.address_bar.setText(url.toString())
- self.webView.load(url)
- @QtCore.pyqtSlot(name="on_webView_loadStarted")
- def resetProgressBar(self):
- self.load_progress_bar.setValue(0)
- @QtCore.pyqtSlot(int, name="on_webView_loadProgress")
- def setProgressBarValue(self, value):
- self.load_progress_bar.setValue(value)
- @QtCore.pyqtSlot(name="on_take_screenshot_button_clicked")
- def takeScreenShot(self):
- self.screenshot.render(self.webView.url())
- def saveScreenShot(self, image):
- image.save("%s.png" % time.time())
- self.statusBar().showMessage(u"saved : %s" % self.webView.url().toString())
- def main():
- application = QtGui.QApplication(sys.argv)
- web_settings = QtWebKit.QWebSettings.globalSettings()
- web_settings.setAttribute(QtWebKit.QWebSettings.PluginsEnabled, False)
- web_settings.setAttribute(QtWebKit.QWebSettings.JavaEnabled, True)
- web_settings.setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
- web_settings.setAttribute(QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls, True)
- main_window = MainWindow()
- main_window.show()
- application.exec_()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement