Advertisement
Guest User

PyQt4 ApplicationMain.py

a guest
Nov 12th, 2010
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.60 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Python Hack-a-thon 2010.11 / PyQt4 Hands on
  4. About QtWebKit 03 : ScreenShot機能を付けてみる
  5. """
  6.  
  7. import sys
  8. import time
  9. from PyQt4 import QtCore
  10. from PyQt4 import QtGui
  11. from PyQt4 import QtWebKit
  12. from MainWindowUI import Ui_MainWindow
  13. from ScreenShot import ScreenShot
  14.  
  15. class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
  16.     def __init__(self, *args, **kw):
  17.         QtGui.QMainWindow.__init__(self, *args, **kw)
  18.         self.setupUi(self)
  19.  
  20.         self.web_page = self.webView.page()
  21.         self.main_frame = self.web_page.mainFrame()
  22.         self.setAddressBarUrl(self.webView.url())
  23.         self.back_button.clicked.connect(self.webView.back)
  24.         self.forward_button.clicked.connect(self.webView.forward)
  25.         self.web_page.linkHovered.connect(self.showUrlInStatusBar)
  26.         self.actionQuit.setShortcut("Ctrl+Q")
  27.  
  28.         self.load_progress_bar = QtGui.QProgressBar()
  29.         self.statusBar().addPermanentWidget(self.load_progress_bar)
  30.         self.statusBar().setSizeGripEnabled(False)
  31.  
  32.         self.load_progress_bar.setSizePolicy(
  33.             QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Preferred))
  34.        
  35.         progress_bar_size = self.load_progress_bar.size()
  36.         progress_bar_size.setWidth(150)
  37.        
  38.         self.load_progress_bar.setMinimumSize(progress_bar_size)
  39.         self.load_progress_bar.resize(progress_bar_size)
  40.  
  41.         self.screenshot = ScreenShot()
  42.         self.screenshot.renderFinished.connect(self.saveScreenShot)
  43.  
  44.  
  45.  
  46.     @QtCore.pyqtSlot(bool, name="on_actionQuit_triggered")
  47.     def quitApplication(self, checked=False):
  48.         sys.exit(QtGui.QApplication.instance().quit())
  49.  
  50.  
  51.     @QtCore.pyqtSlot(name="on_address_bar_returnPressed")
  52.     def loadNewPage(self):
  53.         url = self.address_bar.text()
  54.         if not QtCore.QRegExp(ur"^(https?)|(ftp)://").exactMatch(url):
  55.             url_str = QtCore.QString(u"http://") + url
  56.  
  57.         self.webView.setUrl(QtCore.QUrl(url_str))
  58.  
  59.  
  60.     @QtCore.pyqtSlot(QtCore.QUrl, name="on_webView_urlChanged")
  61.     def setAddressBarUrl(self, url):
  62.         self.address_bar.setText(url.toString())
  63.  
  64.  
  65.     def showUrlInStatusBar(self, link, title, textContent):
  66.         self.statusBar().showMessage(link)
  67.  
  68.  
  69.     @QtCore.pyqtSlot(QtCore.QUrl, name="on_webView_linkClicked")
  70.     def setAddressBarAndLoadPage(self, url):
  71.         self.address_bar.setText(url.toString())
  72.         self.webView.load(url)
  73.  
  74.  
  75.     @QtCore.pyqtSlot(name="on_webView_loadStarted")
  76.     def resetProgressBar(self):
  77.         self.load_progress_bar.setValue(0)
  78.  
  79.  
  80.     @QtCore.pyqtSlot(int, name="on_webView_loadProgress")
  81.     def setProgressBarValue(self, value):
  82.         self.load_progress_bar.setValue(value)
  83.  
  84.  
  85.     @QtCore.pyqtSlot(name="on_take_screenshot_button_clicked")
  86.     def takeScreenShot(self):
  87.         self.screenshot.render(self.webView.url())
  88.  
  89.  
  90.     def saveScreenShot(self, image):
  91.         image.save("%s.png" % time.time())
  92.         self.statusBar().showMessage(u"saved : %s" % self.webView.url().toString())
  93.    
  94.  
  95. def main():
  96.     application = QtGui.QApplication(sys.argv)
  97.  
  98.     web_settings = QtWebKit.QWebSettings.globalSettings()
  99.     web_settings.setAttribute(QtWebKit.QWebSettings.PluginsEnabled, False)
  100.     web_settings.setAttribute(QtWebKit.QWebSettings.JavaEnabled, True)
  101.     web_settings.setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
  102.     web_settings.setAttribute(QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls, True)
  103.  
  104.     main_window = MainWindow()
  105.     main_window.show()
  106.  
  107.     application.exec_()
  108.  
  109.  
  110. if __name__ == "__main__":
  111.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement