class HtmlViewer(QTextBrowser): def __init__(self, parent=None, host="localhost", port=5432, home="index.html", paths=None): QTextBrowser.__init__(self, parent) self.http = QHttp() self.http.setHost(host, port) home = QUrl(home) self.base = home self.html = QString() self.setOpenLinks(True) if not paths is None: self.setSearchPaths(paths) self.http.done.connect(self.getData) self.anchor = QString() self.setSource(home) def setSource(self, url): url = self.source().resolved(url) QTextBrowser.setSource(self, url) def loadResource(self, type, name): ret = QVariant() name.setFragment(QString()) if type == QTextDocument.HtmlResource: loop = QEventLoop() self.connect(self.http, SIGNAL("done(bool)"), loop, SLOT("quit()")) self.http.get(name.toString()) loop.exec_(QEventLoop.AllEvents) data = QVariant(QString(self.html)) if data.toString().trimmed().isEmpty(): fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) else: fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) return data def getData(self, error): if error: self.html = self.http.errorString() else: self.html = self.http.readAll() # the following is a hack to handle redirects... regexp = QRegExp(r'Redirect\sto\s\<a\shref=\"(.*)\">') if regexp.indexIn(QString(self.html)) > -1: self.setSource(QUrl(regexp.capturedTexts()[1]))
class HtmlViewer(QTextBrowser): def __init__(self, parent=None, host="localhost", port=5432, home="index.html", paths=None): QTextBrowser.__init__(self, parent) self.http = QHttp() self.http.setHost(host, port) home = QUrl(home) self.base = home self.html = QString() self.setOpenLinks(True) if not paths is None: self.setSearchPaths(paths) self.connect(self.http, SIGNAL("done(bool)"), self.getData) self.anchor = QString() self.setSource(home) def setSource(self, url): url = self.source().resolved(url) print url QTextBrowser.setSource(self, url) def loadResource(self, type, name): ret = QVariant() name.setFragment(QString()) if type == QTextDocument.HtmlResource: loop = QEventLoop() self.connect(self.http, SIGNAL("done(bool)"), loop, SLOT("quit()")) self.http.get(name.toString()) loop.exec_(QEventLoop.AllEvents|QEventLoop.WaitForMoreEvents) data = QVariant(QString(self.html)) if data.toString().trimmed().isEmpty(): fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) else: fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) return data def getData(self, error): if error: self.html = self.http.errorString() else: self.html = self.http.readAll()
class VersionManager(): _versions_file_path = 'versions.xml' _update_host = 'www.ahmetalpbalkan.com' _update_path = '/versions.xml' proxyHost, proxyIP = '', '' updateContents = '' def __init__(self): self.versions = [] self.parseDefinitionsFile() self.http = QHttp(None) QtCore.QObject.connect(self.http, QtCore.SIGNAL('readyRead(QHttpResponseHeader)'), self.updateProgress) QtCore.QObject.connect(self.http, QtCore.SIGNAL('done(bool)'), self.updateFinished) def updateProxy(self, host, ip): self.proxyHost = host self.proxyIP = ip def useProxy(self): if self.proxyHost and self.proxyIP: self.http.setProxy(self.proxyHost, int(self.proxyIP)) log.error('Proxy activated in update transfer.') def readFromFile(self): try: with open(self._versions_file_path,'r') as definitionsFile: self._xmlContent = definitionsFile.read() except IOError: self.err = "Could not read version definitions file." log.error(self.err) # a module for extracting texts in a nodelist def _getText(self, nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) # assume that the XML scheme is: # + pardus # + version # id,name,size etc. # mirrors # + mirror # hostname, login, username etc. # + mirror # ... # + version... def parseDefinitionsFile(self): self.readFromFile() self.versions = [] try: dom = xml.dom.minidom.parseString(self._xmlContent) versions = dom.getElementsByTagName("pardus")[0].getElementsByTagName("version") for version in versions: self.versions.append(self._handleVersion(version)) except: self.err = "Error while parsing definitions XML file." log.error(self.err) def _handleVersion(self, version): ver = Version() fields = ["size", "name", "type", "minmemory", "memory", "minspace", "space", "md5sum", "kernel", "kernelparams", "initrd", "img"] for field in fields: value =self._getText(version.getElementsByTagName(field)[0].childNodes).encode('utf8') setattr(ver, field, value) ver.id = version.getAttribute("id").encode('utf8') mirrors = version.getElementsByTagName("mirrors")[0].getElementsByTagName("source") mirrorList = list() for mirror in mirrors: mirrorList.append(self._handleMirror(mirror)) ver.mirrors = mirrorList return ver def _handleMirror(self, mirror): mir = Mirror() fields = ["hostname", "country", "login", "username", "password", "port", "path", "filename"] for field in fields: value = self._getText(mirror.getElementsByTagName(field)[0].childNodes).encode('utf8') setattr(mir, field, value) return mir def updateDefinitionsFile(self): self.useProxy() self.http.setHost(self._update_host) self.http.get(self._update_path) self.http.close() def updateProgress(self, header): self.updateContents += self.http.readAll() def updateFinished(self, bool): if True: if self.updateContents: log.debug('Update file retrieved successfully.') try: writestream = open(self._versions_file_path, 'w') writestream.write(self.updateContents) self.updateContents = None writestream.close() log.debug('Update file written successfully.') except: self.err = 'Could not write to version definitions file.' log.error(self.err) self.transferDone.emit(False) self.parseDefinitionsFile() # inefficient but OK self.guiCallback(True) else: self.err = 'Retrieved update is empty.\nMost probably update server is broken. If you use proxy, make sure that it works fine.' log.error(self.err) self.transferDone.emit(False) else: self.err = "Could not reach version definitions URL. Check your internet connection." log.error(self.err) self.guiCallback(False) def getBySize(self, size): 'Size in bytes.' for version in self.versions: if long(version.size) == long(size): return version return None def getByDistance(self, name, tolerance = 10): """Returns version if there is a version within Levenshtein distance of 'tolerance' parameter for 'name' parameter. Nearest version is returned. Comparison is done case-insensitively. First appearing in versions.xml is chosen on tie. None is returned if no version is in given distance. """ nearest = None minDistance = 999 for version in self.versions: n1 = version.name.lower() n2 = name.lower() l_distance = levenshtein(n1, n2) if l_distance < tolerance and l_distance < minDistance: minDistance = l_distance nearest = version return nearest def getByMD5Sum(self, hash): 'MD5 hash as in versions.xml' for version in self.versions: if str(version.md5sum) == str(hash): return version return None def connectGui(self, guiCallback): self.guiCallback = guiCallback def __repr__(self): r = '' for v in self.versions: r+= ' '.join((v.name, v.size, v.id, '\n')) for m in v.mirrors: r+= ' '.join((' ', m.hostname, m.country, m.port, m.username, m.password, m.path, '\n')) return r
class QWingsChat(QtGui.QWidget): def __init__(self, parent=None, app=None): super(QWingsChat, self).__init__(parent) self.app = app self.error = 0 self.setWindowTitle('КрыльяITV :: Чат') self.setWindowIcon(QtGui.QIcon(os.path.join(os.path.dirname(__file__), 'wings_logo.png'))) self.parser = ChatMsgParser() self.template = open(os.path.join(os.path.dirname(__file__), 'chattemplate.html')).read() if config['chat']['login'] is None: while True: login, ok = QtGui.QInputDialog.getText(self, 'Вход в чат', 'Выберите имя:') if ok: break config['chat']['login'] = login save_config() else: login = config['chat']['login'] self.chat = QWebView() self.chat.setContextMenuPolicy(QtCore.Qt.PreventContextMenu) self.message = QWingsChatLineEdit(login) vb = QtGui.QVBoxLayout() vb.setMargin(0) vb.setSpacing(0) vb.addWidget(self.chat) vb.addWidget(self.message) self.setLayout(vb) self.chat.setHtml(self.template) self.http = QHttp('torrent.mnx.net.ru') self.connect(self.http, QtCore.SIGNAL("done(bool)"), self.update_chat) self.timer = QtCore.QTimer() self.timer.singleShot(0, self.send_update_request) def format_msg(self, msg): def get_color(nick): m = 12345 for i in nick: m *= ord(i) d = 190 r = m % d; m /= d g = m % d; m /= d b = m % d; m /= d return "#%.2X%.2X%.2X" % (r, g, b) return '<li>({}) <span style="color:{};">{}:</span> {}</li>'.format(msg['datetime'].time(), get_color(msg['nickname']), msg['nickname'], msg['message']) def send_update_request(self): self.http.get('/ajaxchat/getChatData.php?lastID=-1&rand=%d' % randint(0, 1000000)) def update_chat(self, error): data = str(self.http.readAll(), 'utf-8') if not error: mf = self.chat.page().mainFrame() if self.error > 0: if self.error >= 30: mf.findFirstElement("ul").appendInside( '<li style="color: gray;">({}) Подключение восстановлено.</li>'.format( datetime.now().time().strftime("%H:%M:%S"))) self.error = 0 msgs = self.parser.parse(data) for msg in msgs: mf.findFirstElement("ul").appendInside(self.format_msg(msg)) mf.scrollToAnchor("bottom") else: self.error += 1 if self.error == 30: mf.findFirstElement("ul").appendInside( '<li style="color: gray;">({}) Проблема с подключением.</li>'.format( datetime.now().time().strftime("%H:%M:%S"))) self.timer.singleShot(1000, self.send_update_request) def closeEvent(self, event): if self.app is not None: self.app.toggle_chat() event.ignore() else: super(QWingsChat, self).closeEvent(event)
class Downloader(QtGui.QMainWindow): def setupUi(self, downloader): downloader.setObjectName(_fromUtf8("downloader")) downloader.resize(619, 270) self.centralwidget = QtGui.QWidget(downloader) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(30, 20, 31, 21)) self.label.setObjectName(_fromUtf8("label")) self.url_text = QtGui.QLineEdit(self.centralwidget) self.url_text.setGeometry(QtCore.QRect(90, 20, 411, 27)) self.url_text.setObjectName(_fromUtf8("url_text")) self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(30, 70, 66, 17)) self.label_2.setObjectName(_fromUtf8("label_2")) self.txt_location = QtGui.QLineEdit(self.centralwidget) self.txt_location.setGeometry(QtCore.QRect(90, 60, 211, 27)) self.txt_location.setObjectName(_fromUtf8("txt_location")) self.frame_2 = QtGui.QFrame(self.centralwidget) self.frame_2.setGeometry(QtCore.QRect(10, 130, 581, 80)) self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtGui.QFrame.Raised) self.frame_2.setObjectName(_fromUtf8("frame_2")) self.progressBar = QtGui.QProgressBar(self.frame_2) self.progressBar.setGeometry(QtCore.QRect(10, 10, 561, 61)) self.progressBar.setObjectName(_fromUtf8("progressBar")) self.frame = QtGui.QFrame(self.centralwidget) self.frame.setGeometry(QtCore.QRect(10, 10, 581, 91)) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName(_fromUtf8("frame")) self.button_start = QtGui.QPushButton(self.frame) self.button_start.setGeometry(QtCore.QRect(500, 10, 71, 31)) self.button_start.setObjectName(_fromUtf8("button_start")) self.closebutton = QtGui.QPushButton(self.frame) self.closebutton.setGeometry(QtCore.QRect(500, 50, 71, 31)) self.closebutton.setObjectName(_fromUtf8("closebutton")) self.button_browse = QtGui.QPushButton(self.frame) self.button_browse.setGeometry(QtCore.QRect(300, 50, 81, 27)) self.button_browse.setObjectName(_fromUtf8("button_browse")) downloader.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(downloader) self.menubar.setGeometry(QtCore.QRect(0, 0, 619, 25)) self.menubar.setObjectName(_fromUtf8("menubar")) downloader.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(downloader) self.statusbar.setObjectName(_fromUtf8("statusbar")) downloader.setStatusBar(self.statusbar) self.retranslateUi(downloader) QtCore.QMetaObject.connectSlotsByName(downloader) def retranslateUi(self, downloader): downloader.setWindowTitle( QtGui.QApplication.translate("downloader", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText( QtGui.QApplication.translate("downloader", "URL", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText( QtGui.QApplication.translate("downloader", "Save to", None, QtGui.QApplication.UnicodeUTF8)) self.button_start.setText( QtGui.QApplication.translate("downloader", "Start", None, QtGui.QApplication.UnicodeUTF8)) self.closebutton.setText( QtGui.QApplication.translate("downloader", "Cancel", None, QtGui.QApplication.UnicodeUTF8)) self.button_browse.setText( QtGui.QApplication.translate("downloader", "Browse", None, QtGui.QApplication.UnicodeUTF8)) self.statusbar.showMessage("Preparing") def __init__(self, parent=None): super(Downloader, self).__init__(parent) self.setupUi(self) self.httpGetId = 0 self.httpRequestAborted = False self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closebutton.clicked.connect(self.cancelDownload) self.button_browse.clicked.connect(self.open_file) self.button_start.clicked.connect(self.downloadFile) self.setWindowTitle('Download Example') def open_file(self): fd = QtGui.QFileDialog(self) self.loc = str(fd.getExistingDirectory()) self.txt_location.setText(self.loc) def downloadFile(self): url = QUrl(self.url_text.text()) if self.txt_location.text() == "": QDir.setCurrent("$HOME/Downloads") else: QDir.setCurrent(self.loc) self.statusbar.showMessage("Downloading") fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information( self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '$HOME/Downloads' # Download the file. self.httpGetId = self.http.get(path, self.outFile) def cancelDownload(self): self.statusbar.showMessage("Download canceled") self.httpRequestAborted = True self.http.abort() def httpRequestFinished(self, requestId, error): if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information( self, 'Error', 'Download failed: %s.' % self.http.errorString()) self.statusbar.showMessage("Download Finished") def readResponseHeader(self, responseHeader): # Check for genuine error conditions. if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information( self, 'Error', 'Download failed: %s.' % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead)
class AdLoader(QWidget): """ AdLoader(QWidget) Banner-style display widget. Rotates through a series of remote or local image and displays them for a certain amount of time. Sets a clickable link for each one that opens in a webbrowser. Ad data being passed in via setAdData must be of the following format: myAdData = [ {u'image': u'/path/to/local/image.jpg', u'link': u'http://somesite.com/', u'name': u'my ad #1'}, {u'image': u'http://www.coolimages.com/anotherImage.png', u'link': u'http://www.anotherSite.com', u'name': u'my ad #2'}, ] """ AD_SIZE = (728, 90) AD_INTERVAL = 10 # seconds AD_BGCOLOR = "rgb(41, 41, 41)" def __init__(self, parent=None): super(AdLoader, self).__init__(parent) # # Setup # self._adBgColor = self.AD_BGCOLOR self._adSize = self.AD_SIZE self._adInterval = self.AD_INTERVAL self._link = None self._data = deque([]) self._imageCache = {} self._loadFailed = defaultdict(int) self.__http = QHttp() self.__lastReqId = -1 self._timer = QTimer(self) self._timer.setInterval(self._adInterval * 1000) self.setObjectName("AdLoader") self.resize(*self._adSize) self.mainLayout = QHBoxLayout(self) self.mainLayout.setMargin(0) self.adDisplay = QLabel(self) self.adDisplay.setObjectName("AdDisplay") self.adDisplay.setText("") self.adDisplay.setScaledContents(False) self.adDisplay.setTextInteractionFlags(Qt.NoTextInteraction) self.mainLayout.addWidget(self.adDisplay) self.setStyleSheet("#AdDisplay {background-color: %s}" % self._adBgColor) # self._page = QWebPage() # self._page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) # self._page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) # self._lastNetworkReply = None # self._nam = self._page.networkAccessManager() self.adDisplay.mousePressEvent = lambda event: self.adDisplay.emit(SIGNAL("clicked")) # self.connect(self._page, SIGNAL("loadFinished(bool)"), self._loadFinished) # self.connect(self._nam, SIGNAL("finished (QNetworkReply *)"), self._networkReply) self.connect(self.__http, SIGNAL("requestFinished (int,bool)"), self._loadFinished) self.connect(self.adDisplay, SIGNAL("clicked"), self.linkClicked) self.connect(self._timer, SIGNAL("timeout()"), self.rotateAd) def linkClicked(self): """ Launch the currently set URL """ if not self._link: return QDesktopServices.openUrl(self._link) def loadCurrentAd(self): """ Load the current Ad in the rotation """ self.loadUrl(self._data[0]['image']) def loadUrl(self, url): """ loadUrl(str url) Load the given ad image url into the display """ pixmap = self._imageCache.get(url, None) if pixmap: self.adDisplay.setPixmap(pixmap) link = self._data[0].get('link', None) self._link = QUrl(link) if link else None else: self._loadUrl(url) def rotateAd(self): """ Rotate to the next Ad in the list and display it """ if not self._data: return self._data.rotate(-1) self.loadCurrentAd() def setBgColor(self, colorStr): """ setBgColor(str colorStr) Sets the background color of the display to the given CSS-style color string. Can be hex, name, or rgb(r,g,b) format. """ self._adBgColor = colorStr self.setStyleSheet("#AdDisplay {background-color: %s}" % self._adBgColor) def setAdSize(self, size): """ setAdSize(tuple size) Set the Ad display size in (width, height) pixels """ self._adSize = size def setAdData(self, data): """ setAdData(list data) Set the Ad data to rotate through Format: myAdData = [ {u'image': u'/path/to/local/image.jpg', u'link': u'http://somesite.com/', u'name': u'my ad #1'}, {u'image': u'http://www.coolimages.com/anotherImage.png', u'link': u'http://www.anotherSite.com', u'name': u'my ad #2'}, ] """ active = self._timer.isActive() if active: self._timer.stop() self._data = deque(data) if active: self._timer.start() def setAdInterval(self, seconds): """ setAdInterval(float seconds) Set how many seconds each ad should display, before rotating to the next. """ self._adInterval = seconds*1000 self._timer.setInterval(self._adInterval) def start(self): """ Starts the rotator and loads the first image""" self.loadCurrentAd() self._timer.start() def stop(self): """ Stops the rotator """ self._timer.stop() def _renderUrl(self, buffer): """ Private slot Slot called when url has been loaded and is ready for rendering into the display as an image """ adSize = QSize(*self._adSize) if not buffer.isOpen(): buffer.open(QIODevice.ReadOnly) img = Image.open(buffer) temp = os.path.join(tempfile.gettempdir(), "ad_tmp_%s_%s.png" % (os.getpid(), int(time.time()))) img.save(temp) pixmap = QPixmap(temp) pixmap = pixmap.scaled(adSize, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) self.adDisplay.setPixmap(pixmap) link = self._data[0].get('link', None) self._link = QUrl(link) if link else None self._imageCache[self._data[0]['image']] = pixmap os.remove(temp) def _loadFinished(self, reqid, error): """ Private slot Called when the url has finished loading and is ready to be validated, before then starting the display render. Filters out images that have excessive loading errors. """ if reqid != self.__lastReqId: return errorCode = self.__http.error() statusCode = self.__http.lastResponse().statusCode() if errorCode == 0 and not error: buffer = QBuffer() buffer.setData(self.__http.readAll()) if buffer.size() > 0: self._renderUrl(buffer) self._timer.start() return self.stop() url = self._data[0]['image'] print "Failed to load", url, self._loadFailed[url] self._loadFailed[url] += 1 if self._loadFailed[url] >= 3: del self._data[0] self._loadFailed[url] = 0 if self._data: self.start() return else: self.rotateAd() self._timer.start() return def _loadUrl(self, url): """ _loadUrl(str url) Loads the given image url into the display """ self._timer.stop() self._link = None # self._page.mainFrame().load(QUrl(url)) qurl = QUrl(url) self.__http.setHost(qurl.host()) self.__lastReqId = self.__http.get(qurl.path())
class IDESearchDialog(QtGui.QDialog): """ """ def __init__(self, iface): """ """ self.iface = iface self.canvas = iface.mapCanvas() QtGui.QDialog.__init__(self) # Set up the user interface from Designer. self.ui = Ui_IDESearch() self.ui.setupUi(self) self.http = QHttp() self.httpogr = QHttp() self.url = QUrl() self._radio = 0 self._point = None self._pointutm = None self.i = None self.layer = None self.layerid = '' self.chkRemote = False self.tblResultHeader = [QString.fromUtf8('Nombre'), QString.fromUtf8('Clasificación'), QString.fromUtf8('Localización')] self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) #self.connect(self.ui.btnSearch, SIGNAL("clicked()"), self.onClick_btnSearch) self.connect(self.ui.txtSearch, SIGNAL("returnPressed(void)"), self.onClick_btnSearch) self.connect(self.ui.tblResult, SIGNAL("cellDoubleClicked(int,int)"), self.onDblClick_tblResult) #self.connect(self.ui.chkRemote, SIGNAL("clicked()"), self.onClick_chkRemote) self.connect(self.http, QtCore.SIGNAL("done(bool)"), self.onDone_http) self.connect(self.httpogr, QtCore.SIGNAL("done(bool)"), self.onDone_httpogr) self.connect(self.ui.radiodms, SIGNAL("toggled(bool)"), self.__setRadiodms) self.connect(self.ui.radiodm, SIGNAL("toggled(bool)"), self.__setRadiodm) self.connect(self.ui.radiod, SIGNAL("toggled(bool)"), self.__setRadiod) self.connect(self.ui.radioutm, SIGNAL("toggled(bool)"), self.__setRadioutm) self.connect(self.ui.btnGet, SIGNAL("clicked()"), self.onClick_btnGet) self.connect(self.ui.btnGo, SIGNAL("clicked()"), self.onClick_btnGo) self.connect(self.ui.txtCoordinates, SIGNAL("returnPressed(void)"), self.onClick_btnGo) self.connect(self.ui.btnClipboard, SIGNAL("clicked()"), self.onClick_btnClipboard) baseDirectory = os.path.dirname(__file__) fillPath = lambda x: os.path.join(baseDirectory, x) staticPath, templatePath, databasePath, filenamelog = map(fillPath, ['static', 'templates', '.database', 'idecanarias.log']) databaseName, databaseUser, databasePassword, databaseHost = open(databasePath).read().splitlines() self.conn_string = "host='%s' dbname='%s' user='******' password='******'" % (databaseHost, databaseName, databaseUser, databasePassword) # Log file self.DEBUG = True self.filenamelog = filenamelog self.Log("init app") def Log(self, msg): """ """ if self.DEBUG: f = open(self.filenamelog, "a") f.write("%s: %s\n" % (datetime.now(), msg)) f.close() def alert(self, msg): """ """ QtGui.QMessageBox.warning(self, QString.fromUtf8("Búsquedas IDECANARIAS"), msg) def __setRadiodms(self, checked): """ """ if checked: self._radio = 0 self.reverse_action(None) def __setRadiodm(self, checked): """ """ if checked: self._radio = 1 self.reverse_action(None) def __setRadiod(self, checked): """ """ if checked: self._radio = 2 self.reverse_action(None) def __setRadioutm(self, checked): """ """ if checked: self._radio = 3 self.reverse_action(None) def onClick_chkRemote(self): """ """ self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) def onClick_btnClipboard(self): """ """ clipboard = QApplication.clipboard() clipboard.setText(self.ui.txtCoordinates.text()) def onClick_btnGet(self): """ """ ct = ClickTool(self.iface, self.reverse_action); self.iface.mapCanvas().setMapTool(ct) def onClick_btnGo(self): """ develop er to parse this: 28º 07' 9.7249'' S, 15º 25' 30.9814'' O 28º 07.16208' S, 15º 25.51637' O 28.11936800, -15.42527283 458232.06, 3110498.55 Algorithm lat = 45 + (25 / 60) + (2.98 / 3600) lng = 10 + (11 / 60) + (30.29 / 3600) """ lat = None lng = None x = None y = None texto = self.ui.txtCoordinates.text().toUtf8() if not texto: texto = "28º 07' 9.7249'' N, 15º 25' 30.9814'' O" patterndms = r"^([\d]{1,3})\º ([\d]{1,3})\' ([\d]{1,3}(\.\d+)?)\'\' ([NS]),\s*([\d]{1,3})\º ([\d]{1,3})\' ([\d]{1,3}(\.\d+)?)\'\' ([EO])$" m = re.match(patterndms, texto) if m: lat = int(m.group(1)) + (float(m.group(2)) / 60) + (float(m.group(3)) / 3600) lng = int(m.group(6)) + (float(m.group(7)) / 60) + (float(m.group(8)) / 3600) if m.group(5) == "S": lat = -lat if m.group(10) == "O": lng = -lng self.ui.radiodms.setChecked(True) patterndm = r"^([\d]{1,3})\º ([\d]{1,3}(\.\d+)?)\' ([NS]),\s*([\d]{1,3})\º ([\d]{1,3}(\.\d+)?)\' ([EO])$" m = re.match(patterndm, texto) if m: lat = int(m.group(1)) + (float(m.group(2)) / 60) lng = int(m.group(5)) + (float(m.group(6)) / 60) if m.group(4) == "S": lat = -lat if m.group(8) == "O": lng = -lng self.ui.radiodm.setChecked(True) m = re.match(r"^(\-?[\d]{1,3}(\.\d+)?),\s*(\-?[\d]{1,3}(\.\d+)?)$", texto) if m: lat = float(m.group(1)) lng = float(m.group(3)) self.ui.radiod.setChecked(True) # convert to UTM self.Log("%s, %s (%s)" % (lat, lng, texto)) if lat and lng: point = QgsPoint(lng, lat) self._point = point self._pointutm = pointFromWGS84(point) x = self._pointutm[0] y = self._pointutm[1] m = re.match(r"^(\d+(\.\d+)?),\s*(\d+(\.\d+)?)$", texto) if m: x = float(m.group(1)) y = float(m.group(3)) self._pointutm = QgsPoint(x, y) self._point = pointToWGS84(self._pointutm) self.ui.radioutm.setChecked(True) # reverse lat/lon # Use pdb for debugging #import pdb ## These lines allow you to set a breakpoint in the app #pyqtRemoveInputHook() #pdb.set_trace() #qDebug('Reversing point %s %s' % (x, y)) if x and y: # Get current extent scale = 1 extent = self.canvas.extent() width = extent.width() * scale height = extent.height() * scale # Recenter rect = QgsRectangle( \ x - width/2.0 \ , y - height/2.0 \ , x + width/2.0 \ , y + height/2.0) # Set the extent to our new rectangle self.canvas.setExtent(rect) # Refresh the map self.canvas.refresh() # create layer if not QgsMapLayerRegistry.instance().mapLayer(self.layerid): self.layer = QgsVectorLayer("Point", QString.fromUtf8("Resultados de conversión"), "memory") self.provider = self.layer.dataProvider() self.layer.setCrs(get_dest_projection()) # add fields self.provider.addAttributes( [ QgsField("nombre", QVariant.String), QgsField("x", QVariant.Double), QgsField("y", QVariant.Double), ] ) # Makes fields visible self.layer.updateFieldMap() # Labels on label = self.layer.label() label.setLabelField(QgsLabel.Text, 0) self.layer.enableLabels(True) # add layer if not already QgsMapLayerRegistry.instance().addMapLayer(self.layer) # store layer id self.layerid = QgsMapLayerRegistry.instance().mapLayers().keys()[-1] self.canvas.refresh() text = "" text, ok = QInputDialog.getText(self, QString.fromUtf8('IDECanarias'), QString.fromUtf8('Introduzca una descripción:')) # add a feature fet = QgsFeature() fet.setGeometry(QgsGeometry.fromPoint(self._pointutm)) fet.setAttributeMap( { 0 : QVariant(str(text)), 1 : QVariant(self._pointutm[0]), 2 : QVariant(self._pointutm[1]), } ) self.provider.addFeatures([fet]) # update layer's extent when new features have been added # because change of extent in provider is not propagated to the layer self.layer.updateExtents() self.canvas.refresh() else: self.alert("Coordenadas incorrectas") def reverse_action(self, point): """ """ if point and (point != self._pointutm): self._pointutm = point if self._pointutm == None: return pt = pointToWGS84(self._pointutm) self._point = pt latitude = pt[1] # 28.... longitude = pt[0] # -16.... # Convert to deg, min, secs latitude_sign = 0 if latitude < 0: latitude_sign = -1 longitude_sign = 0 if longitude < 0: longitude_sign = -1 latitude_deg = math.floor(math.fabs(latitude)) latitude_min = math.floor((math.fabs(latitude) - latitude_deg) * 60) latitude_min_ = (math.fabs(latitude) - latitude_deg) * 60 #latitude_sec = math.ceil(((math.fabs(latitude) - latitude_deg) * 60 - latitude_min) * 60) latitude_sec = ((math.fabs(latitude) - latitude_deg) * 60 - latitude_min) * 60 latitude_dir = "S" if latitude_sign == 0: latitude_dir = "N" longitude_deg = math.floor(math.fabs(longitude)) longitude_min = math.floor((math.fabs(longitude) - longitude_deg) * 60) longitude_min_ = (math.fabs(longitude) - longitude_deg) * 60 #longitude_sec = math.ceil(((math.fabs(longitude) - longitude_deg) * 60 - longitude_min) * 60) longitude_sec = ((math.fabs(longitude) - longitude_deg) * 60 - longitude_min) * 60 longitude_dir = "O" if longitude_sign == 0: longitude_dir = "E" data = "" if self._radio == 0: data = "%02.0fº %02.0f\' %06.4f\'\' %s, %02.0fº %02.0f\' %06.4f\'\' %s" % (latitude_deg, latitude_min, latitude_sec, latitude_dir, longitude_deg, longitude_min, longitude_sec, longitude_dir) elif self._radio == 1: data = "%02.0fº %08.5f\' %s, %02.0fº %08.5f\' %s" % (latitude_deg, latitude_min_, latitude_dir, longitude_deg, longitude_min_, longitude_dir) elif self._radio == 2: data = "%.8f, %.8f" % (latitude, longitude) elif self._radio == 3: #data = "%s; %s" % ('{0:,.2f}'.format(self._point[0]), '{0:,.2f}'.format(self._point[1])) data = "%s, %s" % ('{0:.2f}'.format(self._pointutm[0]), '{0:.2f}'.format(self._pointutm[1])) else: data = "" self.ui.txtCoordinates.setText(QString.fromUtf8(data)) def onClick_btnSearch(self): """ """ texto = self.ui.txtSearch.text() if not texto: texto = "grafcan" if self.chkRemote: self.http.setHost('visor.grafcan.es', 80) url = QUrl('/busquedas/toponimoxml/1/50/?texto=%s' % texto) path = url.toEncoded() self.http.get(str(path)) else: try: conn = psycopg2.connect(self.conn_string) conn.set_client_encoding('LATIN1') cursor = conn.cursor() sql = "select * from topo.getbytext('%s',1,50) as (id integer, localizacion text, clasificacion character varying(255), nombre text, descripcion text, rank real, x double precision, y double precision, imagen character varying(64), codigo character varying(10), total bigint)" % texto cursor.execute(sql) self.ui.lblResult.setText(self.tr("%1 lugar(es) encontrados").arg(cursor.rowcount) + QString.fromUtf8(" (Haz doble click para ver su localización)")) self.lid = [] lidd = [] self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) for record in cursor.fetchall(): lidd.append("%s - %s [%s]" % (record[3], record[2], record[1])) self.lid.append(record) row = self.ui.tblResult.rowCount() self.ui.tblResult.insertRow(row) item001 = QTableWidgetItem(record[3]) item002 = QTableWidgetItem(record[2]) item003 = QTableWidgetItem(record[1]) self.ui.tblResult.setItem(row, 0, item001) self.ui.tblResult.setItem(row, 1, item002) self.ui.tblResult.setItem(row, 2, item003) self.ui.tblResult.resizeColumnsToContents() cursor.close() except: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() self.alert(self.tr("Database connection failed!\n ->%1").arg(exceptionValue)) def onDblClick_tblResult(self, i, j): """ """ if self.chkRemote: id = self.lid[i]["id"] self.i = i self.httpogr.setHost('visor.grafcan.es', 80) self.httpogr.get('/busquedas/toponimiagml/1/50/qgis/1/%d/' % int(id)) else: id = self.lid[i][0] localizacion = self.lid[i][1] clasificacion = self.lid[i][2] nombre = self.lid[i][3] fngml = "topo.getgml" try: conn = psycopg2.connect(self.conn_string) conn.set_client_encoding('LATIN1') # GEOM cursor = conn.cursor() sql = "select * from %s(%d)" % (fngml, id) cursor.execute(sql) row = cursor.fetchone() gml = "" if row[0]: geometria = row[0] cursor.close() # FILE filename = os.path.join(str(QtCore.QDir.tempPath()), "XXXXXX.gml") file = QtCore.QTemporaryFile(filename) file.setAutoRemove(False) if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): self.alert(self.tr("No puedo escribir en %1:\n%2.").arg(filename).arg(file.errorString())) return False bbox = "" gml = """<?xml version="1.0" encoding="UTF-8"?> <wfs:FeatureCollection xmlns:ms="http://mapserver.gis.umn.edu/mapserver" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd"> <gml:boundedBy> <gml:Box srsName="EPSG:32628"> <gml:coordinates>%s</gml:coordinates> </gml:Box> </gml:boundedBy> <gml:featureMember> <ms:capa fid="1"> <ms:Nombre>%s</ms:Nombre> <ms:Clasificacion>%s</ms:Clasificacion> <ms:Localizacion>%s</ms:Localizacion> <ms:msGeometry>%s</ms:msGeometry> </ms:capa> </gml:featureMember> </wfs:FeatureCollection>""" % (bbox, nombre, clasificacion, localizacion, geometria) outstr = QtCore.QTextStream(file) outstr.setCodec("UTF-8") outstr << gml filename = str(file.fileName()) file.close() # show in qgis basename = os.path.basename(filename) self.iface.addVectorLayer(filename, "%s_%d" % (nombre, id), 'ogr') src = self.canvas.layers()[0].srs() dest = self.canvas.mapRenderer().destinationSrs() coodTrans = QgsCoordinateTransform(src, dest) extent = self.canvas.layers()[0].extent() newextent = coodTrans.transform(extent) self.canvas.setExtent(newextent) self.canvas.refresh() except: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() print exceptionType, exceptionValue, exceptionTraceback self.alert(self.tr("Database connection failed!\n ->%1").arg(exceptionValue)) def onDone_httpogr(self, error): """ """ filename = os.path.join(str(QtCore.QDir.tempPath()), "XXXXXX.gml") file = QtCore.QTemporaryFile(filename) file.setAutoRemove(False) if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): self.alert(self.tr("No puedo escribir en %1:\n%2.").arg(filename).arg(file.errorString())) return False outstr = QtCore.QTextStream(file) outstr << self.httpogr.readAll() filename = str(file.fileName()) file.close() basename = os.path.basename(filename) id = None nombre = None if self.i <> None: id = self.lid[self.i]["id"] nombre = self.lid[self.i]["nombre"] # show in qgis basename = os.path.basename(filename) self.iface.addVectorLayer(filename, "%s_%s" % (nombre, id), 'ogr') src = self.canvas.layers()[0].srs() dest = self.canvas.mapRenderer().destinationSrs() coodTrans = QgsCoordinateTransform(src, dest) extent = self.canvas.layers()[0].extent() newextent = coodTrans.transform(extent) self.canvas.setExtent(newextent) self.canvas.refresh() def onDone_http(self, error): """ """ doc = QtXml.QDomDocument("IDE") response = str(self.http.readAll()) doc.setContent(response) id = nombre = clasificacion = localizacion = None self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) self.lid = [] lidd = [] root = doc.documentElement() node = root.firstChild() while (not node.isNull()): if(node.toElement().tagName() == "row"): child = node.firstChild() while (not child.isNull()): if(child.toElement().tagName() == "id"): try: child2 = child.firstChild() id = child2.toText().data() #lidd.append(e["id"]) except: QMessageBox.warning(self, "Error", "Could not parse xml file. Problem parsing %s." % (child2.toElement().tagName())) elif (child.toElement().tagName() == "nombre"): try: child2 = child.firstChild() nombre = child2.toText().data() #lidd.append(e["nombre"]) except: QMessageBox.warning(self, "Error", "Could not parse xml file. Problem parsing %s." % (child2.toElement().tagName())) elif (child.toElement().tagName() == "clasificacion"): try: child2 = child.firstChild() clasificacion = child2.toText().data() except: QMessageBox.warning(self, "Error", "Could not parse xml file. Problem parsing %s." % (child2.toElement().tagName())) elif (child.toElement().tagName() == "localizacion"): try: child2 = child.firstChild() localizacion = child2.toText().data() except: QMessageBox.warning(self, "Error", "Could not parse xml file. Problem parsing %s." % (child2.toElement().tagName())) child = child.nextSibling() e = {'id': id, 'nombre': nombre, 'clasificacion': clasificacion, 'localizacion': localizacion } #lidd.append(e["nombre"]) lidd.append("%s - %s [%s]" % (clasificacion, localizacion, nombre)) self.lid.append(e) row = self.ui.tblResult.rowCount() self.ui.tblResult.insertRow(row) item001 = QTableWidgetItem(nombre) item002 = QTableWidgetItem(clasificacion) item003 = QTableWidgetItem(localizacion) self.ui.tblResult.setItem(row, 0, item001) self.ui.tblResult.setItem(row, 1, item002) self.ui.tblResult.setItem(row, 2, item003) node = node.nextSibling() self.ui.lblResult.setText(self.tr("%1 lugar(es) encontrados").arg(len(self.lid)) + QString.fromUtf8(" (Haz doble click para ver su localización)")) self.ui.tblResult.resizeColumnsToContents()
class Downloader(QDialog): " MultiThreaded GUI Downloader Dialog " def __init__(self, parent=None, url_to_download=None, fout=None): " init class " super(Downloader, self).__init__(parent) self.timer = time() self.httpGetId = 0 self.httpRequestAborted = False self.url = QUrl(url_to_download) self.fileInfo = QFileInfo(self.url.path()) self.fout = fout self.fileName = path.join(fout, str(self.fileInfo.fileName())) self.statusLabel = QLabel( "<center><h3>Downloading from:</h3>%s<h3>Saving to:</h3>%s</center>" % (url_to_download, fout) ) self.bytes = QLabel(" ??? ") self.closeButton = QPushButton(" Cancel ") self.closeButton.setAutoDefault(False) self.progressBar = QProgressBar() buttonBox = QDialogButtonBox() buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closeButton.clicked.connect(self.cancelDownload) mainLayout = QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.progressBar) mainLayout.addWidget(self.bytes) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle(__doc__ + " - MultiThreaded Downloader ") self.resize(parent.size().width(), self.size().height()) self.downloadFile(url_to_download, fout) def downloadFile(self, url_to_download, fout): " get the file from remote url to local folder " url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) fileName = path.join(fout, str(fileInfo.fileName())) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information( self, "ERROR", "Cant save the file %s: %s." % (fileName, self.outFile.errorString()) ) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False tpath = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if tpath: tpath = str(tpath) else: tpath = sep # Download the file. print(" INFO: Blender Download Started ! ") self.httpGetId = self.http.get(tpath, self.outFile) def cancelDownload(self): " abort the download " self.statusLabel.setText(" INFO: Download Canceled ! ") self.httpRequestAborted = True self.http.abort() self.close() def httpRequestFinished(self, requestId, error): " what to do when download is finised " if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, "ERROR", "Download Failed: %s." % self.http.errorString()) self.statusLabel.setText(" <center> <h1> ✔ OK! ʘ‿ʘ </h1> <center> ") print(" INFO: Blender Download Finished ! ") if self.fileName.lower().endswith((".tar.gz", ".tgz", ".tar.bz2", ".tbz")): self.extract_file(self.fileName) def readResponseHeader(self, responseHeader): " Check for genuine error conditions " print(" INFO: HTTP Response is " + responseHeader.reasonPhrase()) print(" INFO: HTTP Status Code is " + responseHeader.statusCode()) if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, "ERROR", "Download Failed: %s." % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): " update the GUI Dialog " if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead) self.bytes.setText(" %s of %s Bytes on %s Minutes " % (bytesRead, totalBytes, int(time() - self.timer) / 60)) def extract_file(self, filepath): " extract the file " tarfile.debug = 3 zipfile.debug = 3 if filepath is None: return elif filepath.endswith(".zip"): opener, mode = zipfile.ZipFile, "r" print((" INFO: Extraction Mode is zipfile.ZipFile " + mode)) elif filepath.endswith(".tar.gz") or filepath.endswith(".tgz"): opener, mode = tarfile.open, "r:gz" print((" INFO: Extraction Mode is tarfile.open " + mode)) elif filepath.endswith(".tar.bz2") or filepath.endswith(".tbz"): opener, mode = tarfile.open, "r:bz2" print((" INFO: Extraction Mode is tarfile.open " + mode)) else: raise ValueError("Could not extract %s " % filepath) cwd = getcwd() chdir(self.fout) try: print((" INFO: Extracting Compressed file " + filepath)) filez = opener(filepath, mode) filez.extractall() filez.close() except: print((" ERROR: Could not extract %s " + filepath)) raise ValueError("Could not extract %s " % filepath) finally: print((" INFO: Finished Extraction of Compressed file " + filepath)) chdir(cwd) print(" INFO: Done. ")
class Downloader(QDialog): def __init__(self, parent=None): super(Downloader, self).__init__(parent) self.setMaximumSize(400, 200) self.setMinimumSize(400, 200) self.resize(400, 200) Box = QDialogButtonBox() anime = QLabel('Dialog', self) anime.setMaximumSize(60, 60) anime.setGeometry(QRect(0, 0, 71, 71)) anime.sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) anime.sizePolicy.setHorizontalStretch(0) anime.sizePolicy.setVerticalStretch(0) anime.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.Preferred) anime.setScaledContents(True) anime.setObjectName(_fromUtf8("Downloadgif")) movie = QMovie("dw.gif", QByteArray(), self) movie.setCacheMode(QMovie.CacheAll) movie.setSpeed(100) anime.setMovie(movie) self.httpGetId = 0 self.httpRequestAborted = False self.statusLabel = QLabel('Downloading...\nThe New version (%s)' % version +' of Profiler') # setGeometry(x_pos, y_pos, width, height) self.statusLabel.setGeometry(QRect(90, 20, 221, 41)) sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.statusLabel.sizePolicy().hasHeightForWidth()) self.statusLabel.setSizePolicy(sizePolicy) self.statusLabel.setAlignment(Qt.AlignLeft) font = QFont() font.setFamily(_fromUtf8("URW Gothic L")) font.setPointSize(11) font.setWeight(75) font.setItalic(True) font.setBold(True) self.statusLabel.setFont(font) self.statusLabel.setWordWrap(True) self.statusLabel.setObjectName(_fromUtf8("statusLabel")) self.closeButton = QPushButton("Close") self.closeButton.setAutoDefault(False) self.progressBar = QProgressBar() Box.addButton(self.closeButton, QDialogButtonBox.RejectRole) self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closeButton.clicked.connect(self.cancelDownload) mainLayout = QVBoxLayout() mainLayout.addWidget(anime) mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.progressBar) mainLayout.addWidget(Box) self.setLayout(mainLayout) movie.start() self.setWindowTitle('Profiler Updates Download') self.downloadFile() self.retranslateUi(Downloader) #QMetaObject.connectSlotsByName(Downloader) def downloadFile(self): url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' self.httpGetId = self.http.get(path, self.outFile) def cancelDownload(self): self.statusLabel.setText("Download canceled.") self.httpRequestAborted = True self.http.abort() self.close() def httpRequestFinished(self, requestId, error): if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, 'Error', 'Download failed: %s.' % self.http.errorString()) self.statusLabel.setText('Finished!') def readResponseHeader(self, responseHeader): # Check for genuine error conditions. if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, 'Error', 'Download failed: %s.' % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead) def retranslateUi(self, Downloader): self.setWindowTitle(QApplication.translate("Downloader", "Downloader", None, QApplication.UnicodeUTF8)) self.statusLabel.setText(QApplication.translate("Downloader", 'Downloading...\nThe New version (%s)' % version +' of Profiler', None, QApplication.UnicodeUTF8)) self.closeButton.setText(QApplication.translate("Downloader", "Cancel", None, QApplication.UnicodeUTF8))
class VersionManager(): _versions_file_path = 'versions.xml' _update_host = 'www.ahmetalpbalkan.com' _update_path = '/versions.xml' proxyHost, proxyIP = '', '' updateContents = '' def __init__(self): self.versions = [] self.parseDefinitionsFile() self.http = QHttp(None) QtCore.QObject.connect(self.http, QtCore.SIGNAL('readyRead(QHttpResponseHeader)'), self.updateProgress) QtCore.QObject.connect(self.http, QtCore.SIGNAL('done(bool)'), self.updateFinished) def updateProxy(self, host, ip): self.proxyHost = host self.proxyIP = ip def useProxy(self): if self.proxyHost and self.proxyIP: self.http.setProxy(self.proxyHost, int(self.proxyIP)) log.error('Proxy activated in update transfer.') def readFromFile(self): try: with open(self._versions_file_path, 'r') as definitionsFile: self._xmlContent = definitionsFile.read() except IOError: self.err = "Could not read version definitions file." log.error(self.err) # a module for extracting texts in a nodelist def _getText(self, nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) # assume that the XML scheme is: # + pardus # + version # id,name,size etc. # mirrors # + mirror # hostname, login, username etc. # + mirror # ... # + version... def parseDefinitionsFile(self): self.readFromFile() self.versions = [] try: dom = xml.dom.minidom.parseString(self._xmlContent) versions = dom.getElementsByTagName( "pardus")[0].getElementsByTagName("version") for version in versions: self.versions.append(self._handleVersion(version)) except: self.err = "Error while parsing definitions XML file." log.error(self.err) def _handleVersion(self, version): ver = Version() fields = [ "size", "name", "type", "minmemory", "memory", "minspace", "space", "md5sum", "kernel", "kernelparams", "initrd", "img" ] for field in fields: value = self._getText( version.getElementsByTagName(field)[0].childNodes).encode( 'utf8') setattr(ver, field, value) ver.id = version.getAttribute("id").encode('utf8') mirrors = version.getElementsByTagName( "mirrors")[0].getElementsByTagName("source") mirrorList = list() for mirror in mirrors: mirrorList.append(self._handleMirror(mirror)) ver.mirrors = mirrorList return ver def _handleMirror(self, mirror): mir = Mirror() fields = [ "hostname", "country", "login", "username", "password", "port", "path", "filename" ] for field in fields: value = self._getText( mirror.getElementsByTagName(field)[0].childNodes).encode( 'utf8') setattr(mir, field, value) return mir def updateDefinitionsFile(self): self.useProxy() self.http.setHost(self._update_host) self.http.get(self._update_path) self.http.close() def updateProgress(self, header): self.updateContents += self.http.readAll() def updateFinished(self, bool): if True: if self.updateContents: log.debug('Update file retrieved successfully.') try: writestream = open(self._versions_file_path, 'w') writestream.write(self.updateContents) self.updateContents = None writestream.close() log.debug('Update file written successfully.') except: self.err = 'Could not write to version definitions file.' log.error(self.err) self.transferDone.emit(False) self.parseDefinitionsFile() # inefficient but OK self.guiCallback(True) else: self.err = 'Retrieved update is empty.\nMost probably update server is broken. If you use proxy, make sure that it works fine.' log.error(self.err) self.transferDone.emit(False) else: self.err = "Could not reach version definitions URL. Check your internet connection." log.error(self.err) self.guiCallback(False) def getBySize(self, size): 'Size in bytes.' for version in self.versions: if long(version.size) == long(size): return version return None def getByDistance(self, name, tolerance=10): """Returns version if there is a version within Levenshtein distance of 'tolerance' parameter for 'name' parameter. Nearest version is returned. Comparison is done case-insensitively. First appearing in versions.xml is chosen on tie. None is returned if no version is in given distance. """ nearest = None minDistance = 999 for version in self.versions: n1 = version.name.lower() n2 = name.lower() l_distance = levenshtein(n1, n2) if l_distance < tolerance and l_distance < minDistance: minDistance = l_distance nearest = version return nearest def getByMD5Sum(self, hash): 'MD5 hash as in versions.xml' for version in self.versions: if str(version.md5sum) == str(hash): return version return None def connectGui(self, guiCallback): self.guiCallback = guiCallback def __repr__(self): r = '' for v in self.versions: r += ' '.join((v.name, v.size, v.id, '\n')) for m in v.mirrors: r += ' '.join((' ', m.hostname, m.country, m.port, m.username, m.password, m.path, '\n')) return r
class Descargar(QDialog): def __init__(self, parent, url, archivo_destino): super(Descargar, self).__init__(parent) self.url = url self.httpGetId = 0 self.httpRequestAborted = False self.statusLabel = QLabel('Descargando el manual completo ...') self.closeButton = QPushButton("Cerrar") self.closeButton.setAutoDefault(False) self.barra = QProgressBar() buttonBox = QDialogButtonBox() buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) self.http = QHttp(self) self.http.requestFinished.connect(self.cuando_finalizar_request) self.http.dataReadProgress.connect(self.cuando_actualiza_descarga) self.http.responseHeaderReceived.connect(self.cuando_responder_header) self.closeButton.clicked.connect(self.cuando_cancela) mainLayout = QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.barra) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle('Descargando manual') self.downloadFile(url, archivo_destino) def downloadFile(self, url, archivo_destino): url = QUrl(url) fileName = archivo_destino directorio_usuario_para_pilas = os.path.dirname(archivo_destino) if not os.path.exists(directorio_usuario_para_pilas): os.mkdir(directorio_usuario_para_pilas) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Lo siento, no se puede descargar el archivo desde %s: %s.' % (self.url, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' self.httpGetId = self.http.get(path, self.outFile) def cuando_cancela(self): self.statusLabel.setText("Descarga cancelada") self.httpRequestAborted = True self.http.abort() self.close() def cuando_finalizar_request(self, request_id, error): if request_id != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, 'Error', u'Hay un error de conexión: %s.' % self.http.errorString()) self.statusLabel.setText(u'Perfecto, ahora podrás explorar el manual.') def cuando_responder_header(self, responseHeader): if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): mensaje = 'Ha fallado la descarga del archivo: \n"%s" \n%s.' %(self.url, responseHeader.reasonPhrase()) QMessageBox.information(self, 'Error', mensaje) self.httpRequestAborted = True self.http.abort() self.close() def cuando_actualiza_descarga(self, bytes_leidos, total_bytes): if self.httpRequestAborted: return self.barra.setMaximum(total_bytes) self.barra.setValue(bytes_leidos)
class Downloader(QDialog): file_name = None def __init__(self, url_to_download, download_location, parent=None): super(Downloader, self).__init__(parent) self.httpGetId = 0 self.httpRequestAborted = False self.statusLabel = QLabel('Downloading %s' % url_to_download) self.closeButton = QPushButton("Close") self.closeButton.setAutoDefault(False) self.progressBar = QProgressBar() buttonBox = QDialogButtonBox() buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closeButton.clicked.connect(self.cancelDownload) mainLayout = QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.progressBar) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) self.setWindowTitle('Downloader') self.downloadFile(url_to_download, download_location) def downloadFile(self, url_to_download, download_location): url = QUrl(url_to_download) fileInfo = QFileInfo(url.path()) #fileName = fileInfo.fileName() fileName = download_location + fileInfo.fileName() self.file_name = fileName print("Filename = " + fileName) if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' # Download the file. self.httpGetId = self.http.get(path, self.outFile) return fileName def cancelDownload(self): self.statusLabel.setText("Download canceled.") self.httpRequestAborted = True self.http.abort() self.close() def httpRequestFinished(self, requestId, error): if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, 'Error', 'Download failed: %s.' % self.http.errorString()) self.statusLabel.setText('Done') def readResponseHeader(self, responseHeader): # Check for genuine error conditions. if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, 'Error', 'Download failed: %s.' % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead)
class Downloader(QtGui.QMainWindow): def setupUi(self, downloader): downloader.setObjectName(_fromUtf8("downloader")) downloader.resize(619, 270) self.centralwidget = QtGui.QWidget(downloader) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.label = QtGui.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(30, 20, 31, 21)) self.label.setObjectName(_fromUtf8("label")) self.url_text = QtGui.QLineEdit(self.centralwidget) self.url_text.setGeometry(QtCore.QRect(90, 20, 411, 27)) self.url_text.setObjectName(_fromUtf8("url_text")) self.label_2 = QtGui.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(30, 70, 66, 17)) self.label_2.setObjectName(_fromUtf8("label_2")) self.txt_location = QtGui.QLineEdit(self.centralwidget) self.txt_location.setGeometry(QtCore.QRect(90, 60, 211, 27)) self.txt_location.setObjectName(_fromUtf8("txt_location")) self.frame_2 = QtGui.QFrame(self.centralwidget) self.frame_2.setGeometry(QtCore.QRect(10, 130, 581, 80)) self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel) self.frame_2.setFrameShadow(QtGui.QFrame.Raised) self.frame_2.setObjectName(_fromUtf8("frame_2")) self.progressBar = QtGui.QProgressBar(self.frame_2) self.progressBar.setGeometry(QtCore.QRect(10, 10, 561, 61)) self.progressBar.setObjectName(_fromUtf8("progressBar")) self.frame = QtGui.QFrame(self.centralwidget) self.frame.setGeometry(QtCore.QRect(10, 10, 581, 91)) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName(_fromUtf8("frame")) self.button_start = QtGui.QPushButton(self.frame) self.button_start.setGeometry(QtCore.QRect(500, 10, 71, 31)) self.button_start.setObjectName(_fromUtf8("button_start")) self.closebutton = QtGui.QPushButton(self.frame) self.closebutton.setGeometry(QtCore.QRect(500, 50, 71, 31)) self.closebutton.setObjectName(_fromUtf8("closebutton")) self.button_browse = QtGui.QPushButton(self.frame) self.button_browse.setGeometry(QtCore.QRect(300, 50, 81, 27)) self.button_browse.setObjectName(_fromUtf8("button_browse")) downloader.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(downloader) self.menubar.setGeometry(QtCore.QRect(0, 0, 619, 25)) self.menubar.setObjectName(_fromUtf8("menubar")) downloader.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(downloader) self.statusbar.setObjectName(_fromUtf8("statusbar")) downloader.setStatusBar(self.statusbar) self.retranslateUi(downloader) QtCore.QMetaObject.connectSlotsByName(downloader) def retranslateUi(self, downloader): downloader.setWindowTitle(QtGui.QApplication.translate("downloader", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("downloader", "URL", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("downloader", "Save to", None, QtGui.QApplication.UnicodeUTF8)) self.button_start.setText(QtGui.QApplication.translate("downloader", "Start", None, QtGui.QApplication.UnicodeUTF8)) self.closebutton.setText(QtGui.QApplication.translate("downloader", "Cancel", None, QtGui.QApplication.UnicodeUTF8)) self.button_browse.setText(QtGui.QApplication.translate("downloader", "Browse", None, QtGui.QApplication.UnicodeUTF8)) self.statusbar.showMessage("Preparing") def __init__(self, parent=None): super(Downloader, self).__init__(parent) self.setupUi(self) self.httpGetId = 0 self.httpRequestAborted = False self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closebutton.clicked.connect(self.cancelDownload) self.button_browse.clicked.connect(self.open_file) self.button_start.clicked.connect(self.downloadFile) self.setWindowTitle('Download Example') def open_file(self): fd = QtGui.QFileDialog(self) self.loc = str(fd.getExistingDirectory()) self.txt_location.setText(self.loc) def downloadFile(self): url = QUrl(self.url_text.text()) if self.txt_location.text()=="": QDir.setCurrent("$HOME/Downloads") else: QDir.setCurrent(self.loc) self.statusbar.showMessage("Downloading") fileInfo = QFileInfo(url.path()) fileName = fileInfo.fileName() if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Error', 'Unable to save the file %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return mode = QHttp.ConnectionModeHttp port = url.port() if port == -1: port = 0 self.http.setHost(url.host(), mode, port) self.httpRequestAborted = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '$HOME/Downloads' # Download the file. self.httpGetId = self.http.get(path, self.outFile) def cancelDownload(self): self.statusbar.showMessage("Download canceled") self.httpRequestAborted = True self.http.abort() def httpRequestFinished(self, requestId, error): if requestId != self.httpGetId: return if self.httpRequestAborted: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() if error: self.outFile.remove() QMessageBox.information(self, 'Error', 'Download failed: %s.' % self.http.errorString()) self.statusbar.showMessage("Download Finished") def readResponseHeader(self, responseHeader): # Check for genuine error conditions. if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, 'Error', 'Download failed: %s.' % responseHeader.reasonPhrase()) self.httpRequestAborted = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): if self.httpRequestAborted: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead)
class IDECanariasDock(QtGui.QDockWidget): """ """ def __init__(self, iface): """ """ self.iface = iface self.canvas = iface.mapCanvas() QtGui.QDialog.__init__(self) # Set up the user interface from Designer. self.ui = Ui_IDECanariasDock() self.ui.setupUi(self) self.http = QHttp() self.httpogr = QHttp() self.url = QUrl() self._radio = 0 self._point = None self._pointutm = None self.i = None self.conn_string = "" self.layer = None self.layerid = '' self.chkRemote = False self.chkBBOX = False self.tblResultHeader = [u'Nombre', u'Clasificación', u'Localización'] self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) self.connect( self.ui.txtSearch, SIGNAL("returnPressed(void)"), self.onClick_btnSearch ) self.connect( self.ui.tblResult, SIGNAL("cellDoubleClicked(int,int)"), self.onDblClick_tblResult ) """self.connect( self.ui.chkRemote, SIGNAL("clicked()"), self.onClick_chkRemote )""" self.connect( self.http, QtCore.SIGNAL("done(bool)"), self.onDone_http ) self.connect( self.httpogr, QtCore.SIGNAL("done(bool)"), self.onDone_httpogr ) self.connect( self.ui.radiodms, SIGNAL("toggled(bool)"), self.__setRadiodms ) self.connect( self.ui.radiodm, SIGNAL("toggled(bool)"), self.__setRadiodm ) self.connect( self.ui.radiod, SIGNAL("toggled(bool)"), self.__setRadiod ) self.connect( self.ui.radioutm, SIGNAL("toggled(bool)"), self.__setRadioutm ) self.connect( self.ui.btnGet, SIGNAL("clicked()"), self.onClick_btnGet ) self.connect( self.ui.btnGo, SIGNAL("clicked()"), self.onClick_btnGo ) self.connect( self.ui.txtCoordinates, SIGNAL("returnPressed(void)"), self.onClick_btnGo ) self.connect( self.ui.btnClipboard, SIGNAL("clicked()"), self.onClick_btnClipboard ) baseDirectory = os.path.dirname(__file__) def fillPath(x): return os.path.join(baseDirectory, x) databasePath, filenamelog = map( fillPath, ['.database', 'idecanarias.log'] ) # Log file self.DEBUG = True self.filenamelog = filenamelog self.Log("init app") try: databaseName, databaseUser, databasePassword, databaseHost = open( databasePath).read().splitlines() self.conn_string = ( "host='%s' dbname='%s' user='******' password='******'" % ( databaseHost, databaseName, databaseUser, databasePassword ) ) except: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() self.Log(str(exceptionTraceback)) self.chkRemote = True def Log(self, msg): """ """ if self.DEBUG: f = open(self.filenamelog, "a") f.write("%s: %s\n" % (datetime.now(), msg.encode('utf-8'))) f.close() def alert(self, msg): """ """ QtGui.QMessageBox.warning(self, u'Búsquedas IDECANARIAS', msg) def __setRadiodms(self, checked): """ """ if checked: self._radio = 0 self.reverse_action(None) def __setRadiodm(self, checked): """ """ if checked: self._radio = 1 self.reverse_action(None) def __setRadiod(self, checked): """ """ if checked: self._radio = 2 self.reverse_action(None) def __setRadioutm(self, checked): """ """ if checked: self._radio = 3 self.reverse_action(None) def onClick_btnLoad(self): """ """ _pointutm = None data = "" # check num items selected rows = self.ui.tblResult.selectionModel().selectedRows() rowscount = len(rows) if rowscount == 0: QMessageBox.warning( self, "Aviso", 'Debe seleccionar algún elemento' ) return False if rowscount == 1: self.onDblClick_tblResult(rows[0].row(), 0) return False # for idx in self.ui.tblResult.selectedItems(): for idx in rows: _pointutm = pointFromWGS84( QgsPoint(self.lid[idx.row()][6], self.lid[idx.row()][7]) ) fid = """<ms:capa fid="%d"> <ms:Nombre>%s</ms:Nombre> <ms:Clasificacion>%s</ms:Clasificacion> <ms:Localizacion>%s</ms:Localizacion> <ms:msGeometry><gml:Point srsName="EPSG:32628"> <gml:coordinates>%f,%f</gml:coordinates></gml:Point> </ms:msGeometry> </ms:capa>""" % ( int(idx.row()) + 1, self.lid[idx.row()][3], self.lid[idx.row()][2], self.lid[idx.row()][1], _pointutm[0], _pointutm[1] ) data += fid # FILE filename = os.path.join(str(QtCore.QDir.tempPath()), "XXXXXX.gml") file = QtCore.QTemporaryFile(filename) file.setAutoRemove(False) if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): self.alert( self.tr("No puedo escribir en %1:\n%2.").arg( filename ).arg(file.errorString()) ) return False # id = 1 # nombre = "test" bbox = "" gml = ( '<?xml version="1.0" encoding="UTF-8"?>' '<wfs:FeatureCollection' ' xmlns:ms="http://mapserver.gis.umn.edu/mapserver"' ' xmlns:wfs="http://www.opengis.net/wfs"' ' xmlns:gml="http://www.opengis.net/gml"' ' xmlns:ogc="http://www.opengis.net/ogc"' ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ' xsi:schemaLocation="http://www.opengis.net/wfs ' ' http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">' '<gml:boundedBy>' '<gml:Box srsName="EPSG:32628">' ' <gml:coordinates>%s</gml:coordinates>' '</gml:Box>' '</gml:boundedBy>' '<gml:featureMember>%s</gml:featureMember>' '</wfs:FeatureCollection>' ) % (bbox, data) outstr = QtCore.QTextStream(file) outstr.setCodec("UTF-8") outstr << gml filename = str(file.fileName()) file.close() texto = self.ui.txtSearch.text() if not texto: texto = "consulta" # basename = os.path.basename(filename) self.iface.addVectorLayer(filename, "%s" % (texto), 'ogr') src = self.canvas.layers()[0].srs() dest = self.canvas.mapRenderer().destinationSrs() coodTrans = QgsCoordinateTransform(src, dest) extent = self.canvas.layers()[0].extent() newextent = coodTrans.transform(extent) self.canvas.setExtent(newextent) self.canvas.refresh() def onClick_chkRemote(self): """ """ self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) def onClick_btnClipboard(self): """ """ clipboard = QtGui.QApplication.clipboard() clipboard.setText(self.ui.txtCoordinates.text()) def onClick_btnGet(self): """ """ ct = ClickTool(self.iface, self.reverse_action) self.iface.mapCanvas().setMapTool(ct) def onClick_btnGo(self): """ develop er to parse this: 28º 07' 9.7249'' S, 15º 25' 30.9814'' O 28º 07.16208' S, 15º 25.51637' O 28.11936800, -15.42527283 458232.06, 3110498.55 Algorithm lat = 45 + (25 / 60) + (2.98 / 3600) lng = 10 + (11 / 60) + (30.29 / 3600) """ lat = None lng = None x = None y = None texto = self.ui.txtCoordinates.text().encode('utf-8') if not texto: texto = "28º 07' 9.7249'' N, 15º 25' 30.9814'' O" texto = "28º 07' 9.7248'' N, 15º 25' 30.9822'' O" patterndms = ( r"^([\d]{1,3})\º ([\d]{1,3})\'" r" ([\d]{1,3}(\.\d+)?)\'\'" r" ([NS]),\s*([\d]{1,3})\º ([\d]{1,3})\'" r" ([\d]{1,3}(\.\d+)?)\'\' ([EO])$" ) m = re.match(patterndms, texto, re.UNICODE) if m: lat = int(m.group(1)) + \ (float(m.group(2)) / 60) + \ (float(m.group(3)) / 3600) lng = int(m.group(6)) + \ (float(m.group(7)) / 60) + \ (float(m.group(8)) / 3600) if m.group(5) == "S": lat = -lat if m.group(10) == "O": lng = -lng self.ui.radiodms.setChecked(True) patterndm = ( r"^([\d]{1,3})\º ([\d]{1,3}(\.\d+)?)\'" r" ([NS]),\s*([\d]{1,3})\º ([\d]{1,3}(\.\d+)?)\' ([EO])$" ) m = re.match(patterndm, texto, re.UNICODE) if m: lat = int(m.group(1)) + (float(m.group(2)) / 60) lng = int(m.group(5)) + (float(m.group(6)) / 60) if m.group(4) == "S": lat = -lat if m.group(8) == "O": lng = -lng self.ui.radiodm.setChecked(True) patterndm = r"^(\-?[\d]{1,3}(\.\d+)?),\s*(\-?[\d]{1,3}(\.\d+)?)$" m = re.match(patterndm, texto, re.UNICODE) if m: lat = float(m.group(1)) lng = float(m.group(3)) self.ui.radiod.setChecked(True) # convert to UTM self.Log("%s, %s (%s)" % (lat, lng, type(texto))) if lat and lng: point = QgsPoint(lng, lat) self._point = point self._pointutm = pointFromWGS84(point) x = self._pointutm[0] y = self._pointutm[1] m = re.match(r"^(\d+(\.\d+)?),\s*(\d+(\.\d+)?)$", texto) if m: x = float(m.group(1)) y = float(m.group(3)) self._pointutm = QgsPoint(x, y) self._point = pointToWGS84(self._pointutm) self.ui.radioutm.setChecked(True) if x and y: # create layer if not QgsMapLayerRegistry.instance().mapLayer(self.layerid): self.layer = QgsVectorLayer( "Point", u'Resultados de conversión', "memory" ) self.provider = self.layer.dataProvider() self.layer.setCrs(get_dest_projection()) # add fields self.provider.addAttributes([ QgsField("nombre", QtCore.QVariant.String), QgsField("x", QtCore.QVariant.Double), QgsField("y", QtCore.QVariant.Double), ]) # Makes fields visible self.layer.updateFields() # Labels on label = self.layer.label() label.setLabelField(QgsLabel.Text, 0) self.layer.enableLabels(True) # add layer if not already QgsMapLayerRegistry.instance().addMapLayer(self.layer) # store layer id self.layerid = QgsMapLayerRegistry.instance( ).mapLayers().keys()[-1] self.canvas.refresh() text = "" text, ok = QtGui.QInputDialog.getText( self, u'IDECanarias', u'Introduzca una descripción:' ) # add a feature fields = self.layer.pendingFields() fet = QgsFeature(fields) fet.setGeometry(QgsGeometry.fromPoint(self._pointutm)) fet[0] = text fet[1] = self._pointutm[0] fet[2] = self._pointutm[1] self.provider.addFeatures([fet]) # update layer's extent when new features have been added # because change of extent in provider is not # propagated to the layer self.layer.updateExtents() # Get current extent scale = 1 extent = self.canvas.extent() width = extent.width() * scale height = extent.height() * scale # Recenter rect = QgsRectangle( x - width / 2.0, y - height / 2.0, x + width / 2.0, y + height / 2.0 ) # Set the extent to our new rectangle self.canvas.setExtent(rect) # Refresh the map self.canvas.refresh() else: self.alert("Coordenadas incorrectas") def reverse_action(self, point): """ """ if point and (point != self._pointutm): self._pointutm = point if self._pointutm is None: return pt = pointToWGS84(self._pointutm) self._point = pt latitude = pt[1] # 28.... longitude = pt[0] # -16.... # Convert to deg, min, secs latitude_sign = 0 if latitude < 0: latitude_sign = -1 longitude_sign = 0 if longitude < 0: longitude_sign = -1 latitude_deg = math.floor(math.fabs(latitude)) latitude_min = math.floor((math.fabs(latitude) - latitude_deg) * 60) latitude_min_ = (math.fabs(latitude) - latitude_deg) * 60 latitude_sec = ( (math.fabs(latitude) - latitude_deg) * 60 - latitude_min ) * 60 latitude_dir = "S" if latitude_sign == 0: latitude_dir = "N" longitude_deg = math.floor(math.fabs(longitude)) longitude_min = math.floor((math.fabs(longitude) - longitude_deg) * 60) longitude_min_ = (math.fabs(longitude) - longitude_deg) * 60 longitude_sec = ( (math.fabs(longitude) - longitude_deg) * 60 - longitude_min ) * 60 longitude_dir = "O" if longitude_sign == 0: longitude_dir = "E" data = "" if self._radio == 0: data = ( u"%02.0fº %02.0f\'" u" %06.4f\'\' %s, %02.0fº %02.0f\' %06.4f\'\' %s" ) % ( latitude_deg, latitude_min, latitude_sec, latitude_dir, longitude_deg, longitude_min, longitude_sec, longitude_dir ) elif self._radio == 1: data = ( u"%02.0fº %08.5f\' %s, %02.0fº %08.5f\' %s" ) % ( latitude_deg, latitude_min_, latitude_dir, longitude_deg, longitude_min_, longitude_dir ) elif self._radio == 2: data = u"%.8f, %.8f" % (latitude, longitude) elif self._radio == 3: data = u"%s, %s" % ( '{0:.2f}'.format(self._pointutm[0]), '{0:.2f}'.format(self._pointutm[1]) ) else: data = "" self.ui.txtCoordinates.setText(data) def onClick_btnSearch(self): """ TODO: 121203, limit bbox search /busquedas/toponimoxmlbbox/1/10/151186.2703860851,2928780.363515307,682750.3992649722,3334856.301118972/0/0/?texto=chineguas """ texto = self.ui.txtSearch.text() if not texto: texto = "grafcan" if self.chkRemote: self.http.setHost('visor.grafcan.es', 80) if not self.ui.chkBBOX.isChecked(): url = QUrl('/busquedas/toponimoxml/1/50/?texto=%s' % texto) else: self.Log("retrive bbox") _bbox = None _bbox = self.canvas.extent() bbox = [ _bbox.xMinimum(), _bbox.yMinimum(), _bbox.xMaximum(), _bbox.yMaximum() ] url = QUrl( ( '/busquedas/toponimoxmlbbox' '/1/10/%s,%s,%s,%s/0/0/?texto=%s' ) % ( bbox[0], bbox[1], bbox[2], bbox[3], texto ) ) path = url.toEncoded() self.http.get(str(path)) else: try: conn = psycopg2.connect(self.conn_string) conn.set_client_encoding('LATIN1') cursor = conn.cursor() if not self.ui.chkBBOX.isChecked(): sql = ( "select id, nombre, clasificacion," " localizacion from grafcan.gettoponimo('%s', 1, 50)" ) % texto else: _bbox = None _bbox = self.canvas.extent() bbox = [ _bbox.xMinimum(), _bbox.yMinimum(), _bbox.xMaximum(), _bbox.yMaximum() ] sql = ( "select id, nombre, clasificacion," " localizacion from grafcan.gettoponimo" "('%s', 1, 50, %f, %f, %f, %f, true," "'30,39,160,10,11,12,13,15,16,17,18,40,50," "14,19,20,21,31,32,33,34,35,36,37,38,151," "152,153,154,191')" ) % ( texto, float(bbox[0]), float(bbox[1]), float(bbox[2]), float(bbox[3]) ) cursor.execute(sql) self.ui.lblResult.setText( ( u"%d lugar(es) encontrados" u"(Haz doble click para ver su localización)" ) % cursor.rowcount ) self.lid = [] lidd = [] self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels( self.tblResultHeader ) for record in cursor.fetchall(): lidd.append("%s - %s [%s]" % ( record[3], record[2], record[1]) ) self.lid.append(record) row = self.ui.tblResult.rowCount() self.ui.tblResult.insertRow(row) item001 = QtGui.QTableWidgetItem(record[1]) item002 = QtGui.QTableWidgetItem(record[2]) item003 = QtGui.QTableWidgetItem(record[3]) self.ui.tblResult.setItem(row, 0, item001) self.ui.tblResult.setItem(row, 1, item002) self.ui.tblResult.setItem(row, 2, item003) self.ui.tblResult.resizeColumnsToContents() cursor.close() except: exceptionValue = sys.exc_info()[1] self.alert( u"Database connection failed!\n -> %s" % exceptionValue ) def onDblClick_tblResult(self, i, j): """ """ if self.chkRemote: id = self.lid[i][0] self.i = i self.httpogr.setHost('visor.grafcan.es', 80) self.httpogr.get( '/busquedas/toponimiagml/1/50/qgis/1/%d/' % int(id) ) else: id = self.lid[i][0] localizacion = self.lid[i][3] clasificacion = self.lid[i][2] nombre = self.lid[i][1] fngml = "grafcan.gettoponimo_gml" try: conn = psycopg2.connect(self.conn_string) conn.set_client_encoding('LATIN1') # GEOM cursor = conn.cursor() sql = "select * from %s(%d)" % (fngml, id) cursor.execute(sql) row = cursor.fetchone() gml = "" if row[0]: geometria = row[0] cursor.close() # FILE filename = os.path.join( str(QtCore.QDir.tempPath()), "XXXXXX.gml" ) file = QtCore.QTemporaryFile(filename) file.setAutoRemove(False) if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): self.alert( u"No puedo escribir en %s:\n%s." % ( filename, file.errorString() ) ) return False bbox = "" gml = ( '<?xml version="1.0" encoding="UTF-8"?>' '<wfs:FeatureCollection' ' xmlns:ms="http://mapserver.gis.umn.edu/mapserver"' ' xmlns:wfs="http://www.opengis.net/wfs"' ' xmlns:gml="http://www.opengis.net/gml"' ' xmlns:ogc="http://www.opengis.net/ogc"' ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' ' xsi:schemaLocation="http://www.opengis.net/wfs' ' http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">' '<gml:boundedBy>' '<gml:Box srsName="EPSG:32628">' '<gml:coordinates>%s</gml:coordinates>' '</gml:Box>' '</gml:boundedBy>' '<gml:featureMember>' '<ms:capa fid="1">' '<ms:Nombre>%s</ms:Nombre>' '<ms:Clasificacion>%s</ms:Clasificacion>' '<ms:Localizacion>%s</ms:Localizacion>' '<ms:msGeometry>%s</ms:msGeometry>' '</ms:capa>' '</gml:featureMember>' '</wfs:FeatureCollection>' ) % ( bbox, nombre, clasificacion, localizacion, geometria ) outstr = QtCore.QTextStream(file) outstr.setCodec("UTF-8") outstr << gml filename = str(file.fileName()) file.close() layerid = self.iface.addVectorLayer( filename, "%s_%s" % (nombre, id), 'ogr' ) src = layerid.crs() dest = self.canvas.mapRenderer().destinationCrs() coodTrans = QgsCoordinateTransform(src, dest) # extent = self.canvas.layers()[0].extent() extent = layerid.extent() newextent = coodTrans.transform(extent) self.canvas.setExtent(newextent) self.canvas.refresh() except: self.Log(traceback.print_exc()) self.alert(traceback.print_exc()) def onDone_httpogr(self, error): """ """ filename = os.path.join(str(QtCore.QDir.tempPath()), "XXXXXX.gml") file = QtCore.QTemporaryFile(filename) file.setAutoRemove(False) if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): self.alert( u"No puedo escribir en %1:\n%2." % ( filename, file.errorString() ) ) return False self.Log("Save") outstr = QtCore.QTextStream(file) outstr.setCodec("UTF-8") all = str(self.httpogr.readAll()).decode('utf-8') outstr << all filename = str(file.fileName()) file.close() # basename = os.path.basename(filename) id = None nombre = None if self.i is not None: id = self.lid[self.i][0] nombre = self.lid[self.i][3] # show in qgis # basename = os.path.basename(filename) layerid = self.iface.addVectorLayer( filename, "%s_%s" % (nombre, id), 'ogr' ) # src = QgsCoordinateReferenceSystem() # src.createFromSrid(32628) # src = self.canvas.layers()[0].crs() src = layerid.crs() dest = self.canvas.mapRenderer().destinationCrs() coodTrans = QgsCoordinateTransform(src, dest) # extent = self.canvas.layers()[0].extent() extent = layerid.extent() newextent = coodTrans.transform(extent) self.canvas.setExtent(newextent) self.canvas.refresh() def onDone_http(self, error): """ """ doc = QtXml.QDomDocument("IDE") response = str(self.http.readAll()) doc.setContent(response) id = nombre = clasificacion = localizacion = None self.ui.tblResult.clear() self.ui.tblResult.setRowCount(0) self.ui.tblResult.setHorizontalHeaderLabels(self.tblResultHeader) self.lid = [] lidd = [] root = doc.documentElement() node = root.firstChild() while (not node.isNull()): if(node.toElement().tagName() == "row"): child = node.firstChild() while (not child.isNull()): if(child.toElement().tagName() == "id"): try: child2 = child.firstChild() id = child2.toText().data() # lidd.append(e["id"]) except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) elif (child.toElement().tagName() == "nombre"): try: child2 = child.firstChild() nombre = child2.toText().data() # lidd.append(e["nombre"]) except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) elif (child.toElement().tagName() == "clasificacion"): try: child2 = child.firstChild() clasificacion = child2.toText().data() except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) elif (child.toElement().tagName() == "localizacion"): try: child2 = child.firstChild() localizacion = child2.toText().data() except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) elif (child.toElement().tagName() == "x"): try: child2 = child.firstChild() x = child2.toText().data() except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) elif (child.toElement().tagName() == "y"): try: child2 = child.firstChild() y = child2.toText().data() except: QMessageBox.warning( self, "Error", "Could not parse xml file. Problem %s." % ( child2.toElement().tagName() ) ) child = child.nextSibling() e = ( id, localizacion, clasificacion, nombre, nombre, 0.0, x[0], y[0] ) lidd.append( "%s - %s [%s]" % (clasificacion, localizacion, nombre) ) self.lid.append(e) row = self.ui.tblResult.rowCount() self.ui.tblResult.insertRow(row) item001 = QtGui.QTableWidgetItem(nombre) item002 = QtGui.QTableWidgetItem(clasificacion) item003 = QtGui.QTableWidgetItem(localizacion) self.ui.tblResult.setItem(row, 0, item001) self.ui.tblResult.setItem(row, 1, item002) self.ui.tblResult.setItem(row, 2, item003) node = node.nextSibling() self.ui.lblResult.setText( ( u'%d lugar(es) encontrados ' u'(Haz doble click para ver su localización)' ) % len(self.lid) ) self.ui.tblResult.resizeColumnsToContents()
class Saver(QDialog): '''Classe qui permet d'enregistrer un fichier sur le disque''' def __init__(self, login, password, url, nameFile, parent = None): ''' Constructeur @type login: String @param login: Compte utilisateur @type password: String @param password: Mot de passe utilisateur @type url:String @param url: URL de la ressource @type nameFile: String @param nameFile: Nom du fichier ''' super(Saver, self).__init__(parent) self.url_to_download = url self.login = login self.password = password self.nameFile = nameFile #Création des parametres self.parameters = parse_qs(urlparse(url).query) self.httpGetId = 0 self.requeteHTTPannule = False self.statusLabel = QLabel(u'Enregistrement %s' % self.url_to_download) self.closeButton = QPushButton("Fermer") self.closeButton.setAutoDefault(False) self.openFileButton = QPushButton("Ouvrir le fichier") self.openFileButton.setAutoDefault(False) self.openDirectoryButton = QPushButton(u"Ouvrir le répertoire") self.openDirectoryButton.setAutoDefault(False) self.progressBar = QProgressBar() #Gestion des boutons inférieurs buttonBox = QDialogButtonBox() buttonBox.addButton(self.openFileButton, QDialogButtonBox.ActionRole) buttonBox.addButton(self.openDirectoryButton, QDialogButtonBox.ActionRole) buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) #Construction de l'objet QHTTP et mise en place des signaux/slots self.http = QHttp(self) self.http.requestFinished.connect(self.httpRequestFinished) self.http.dataReadProgress.connect(self.updateDataReadProgress) self.http.responseHeaderReceived.connect(self.readResponseHeader) self.closeButton.clicked.connect(self.cancelDownload) self.openDirectoryButton.clicked.connect(self.openDirectory) self.openFileButton.clicked.connect(self.openFile) #Construction de l'interface mainLayout = QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addWidget(self.progressBar) mainLayout.addWidget(buttonBox) self.setLayout(mainLayout) #Création de la fenêtre et affichage self.setWindowTitle(u'Enregistrement') self.openFileButton.hide() self.openDirectoryButton.hide() self.show() #Lancement du téléchargement self.downloadFile() def downloadFile(self): url = QUrl(self.url_to_download) fileName = QFileDialog.getSaveFileName(self, "Enregistrer la liste des dossiers", self.nameFile, "conf") if QFile.exists(fileName): QFile.remove(fileName) self.outFile = QFile(fileName) if not self.outFile.open(QIODevice.WriteOnly): QMessageBox.information(self, 'Erreur','Impossible d\'enregistrer le fichier %s: %s.' % (fileName, self.outFile.errorString())) self.outFile = None return #Création que la connexion HTTPS mode = QHttp.ConnectionModeHttps port = 0 self.http.setHost(url.host(), mode, port) self.requeteHTTPannule = False path = QUrl.toPercentEncoding(url.path(), "!$&'()*+,;=:@/") if path: path = str(path) else: path = '/' #Concaténation des paramètres à l'URL path = path+("?") for item in url.queryItems(): path = path + item[0] + "=" + item[1] + "&" self.http.setUser(self.login, self.password) self.httpGetId = self.http.get(path, self.outFile) def cancelDownload(self): self.statusLabel.setText(u"Enregistrement annulé") self.requeteHTTPannule = True self.http.abort() self.close() def httpRequestFinished(self, requestId, error): if requestId != self.httpGetId: return if self.requeteHTTPannule: if self.outFile is not None: self.outFile.close() self.outFile.remove() self.outFile = None return self.outFile.close() self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) self.progressBar.setValue(100) if error: self.outFile.remove() QMessageBox.information(self, 'Erreur','Le téléchargement a échoué : %s.' % self.http.errorString()) self.statusLabel.setText('Enregistrement ok') self.openDirectoryButton.show() self.openFileButton.show() def readResponseHeader(self, responseHeader): '''Slot pour analyser les headers HTTP''' if responseHeader.statusCode() not in (200, 300, 301, 302, 303, 307): QMessageBox.information(self, 'Erreur','Le téléchargement a échoué : %s.' % responseHeader.reasonPhrase()) self.requeteHTTPannule = True self.http.abort() def updateDataReadProgress(self, bytesRead, totalBytes): '''Slot pour mettre à jour la barre de progression''' if self.requeteHTTPannule: return self.progressBar.setMaximum(totalBytes) self.progressBar.setValue(bytesRead) def openFile(self): '''Fonction permettant d'ouvrir le fichier''' desktopService = QDesktopServices() desktopService.openUrl(QUrl(self.outFile.fileName())) def openDirectory(self): '''Fonction permettant d'ouvrir le répertoire''' desktopService = QDesktopServices() fileInfo = QFileInfo(QFile(self.outFile.fileName())) directory = fileInfo.dir() desktopService.openUrl(QUrl(directory.path()))
class AdLoader(QWidget): """ AdLoader(QWidget) Banner-style display widget. Rotates through a series of remote or local image and displays them for a certain amount of time. Sets a clickable link for each one that opens in a webbrowser. Ad data being passed in via setAdData must be of the following format: myAdData = [ {u'image': u'/path/to/local/image.jpg', u'link': u'http://somesite.com/', u'name': u'my ad #1'}, {u'image': u'http://www.coolimages.com/anotherImage.png', u'link': u'http://www.anotherSite.com', u'name': u'my ad #2'}, ] """ AD_SIZE = (728, 90) AD_INTERVAL = 10 # seconds AD_BGCOLOR = "rgb(41, 41, 41)" def __init__(self, parent=None): super(AdLoader, self).__init__(parent) # # Setup # self._adBgColor = self.AD_BGCOLOR self._adSize = self.AD_SIZE self._adInterval = self.AD_INTERVAL self._link = None self._data = deque([]) self._imageCache = {} self._loadFailed = defaultdict(int) self.__http = QHttp() self.__lastReqId = -1 self._timer = QTimer(self) self._timer.setInterval(self._adInterval * 1000) self.setObjectName("AdLoader") self.resize(*self._adSize) self.mainLayout = QHBoxLayout(self) self.mainLayout.setMargin(0) self.adDisplay = QLabel(self) self.adDisplay.setObjectName("AdDisplay") self.adDisplay.setText("") self.adDisplay.setScaledContents(False) self.adDisplay.setTextInteractionFlags(Qt.NoTextInteraction) self.mainLayout.addWidget(self.adDisplay) self.setStyleSheet("#AdDisplay {background-color: %s}" % self._adBgColor) # self._page = QWebPage() # self._page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) # self._page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) # self._lastNetworkReply = None # self._nam = self._page.networkAccessManager() self.adDisplay.mousePressEvent = lambda event: self.adDisplay.emit( SIGNAL("clicked")) # self.connect(self._page, SIGNAL("loadFinished(bool)"), self._loadFinished) # self.connect(self._nam, SIGNAL("finished (QNetworkReply *)"), self._networkReply) self.connect(self.__http, SIGNAL("requestFinished (int,bool)"), self._loadFinished) self.connect(self.adDisplay, SIGNAL("clicked"), self.linkClicked) self.connect(self._timer, SIGNAL("timeout()"), self.rotateAd) def linkClicked(self): """ Launch the currently set URL """ if not self._link: return QDesktopServices.openUrl(self._link) def loadCurrentAd(self): """ Load the current Ad in the rotation """ self.loadUrl(self._data[0]['image']) def loadUrl(self, url): """ loadUrl(str url) Load the given ad image url into the display """ pixmap = self._imageCache.get(url, None) if pixmap: self.adDisplay.setPixmap(pixmap) link = self._data[0].get('link', None) self._link = QUrl(link) if link else None else: self._loadUrl(url) def rotateAd(self): """ Rotate to the next Ad in the list and display it """ if not self._data: return self._data.rotate(-1) self.loadCurrentAd() def setBgColor(self, colorStr): """ setBgColor(str colorStr) Sets the background color of the display to the given CSS-style color string. Can be hex, name, or rgb(r,g,b) format. """ self._adBgColor = colorStr self.setStyleSheet("#AdDisplay {background-color: %s}" % self._adBgColor) def setAdSize(self, size): """ setAdSize(tuple size) Set the Ad display size in (width, height) pixels """ self._adSize = size def setAdData(self, data): """ setAdData(list data) Set the Ad data to rotate through Format: myAdData = [ {u'image': u'/path/to/local/image.jpg', u'link': u'http://somesite.com/', u'name': u'my ad #1'}, {u'image': u'http://www.coolimages.com/anotherImage.png', u'link': u'http://www.anotherSite.com', u'name': u'my ad #2'}, ] """ active = self._timer.isActive() if active: self._timer.stop() self._data = deque(data) if active: self._timer.start() def setAdInterval(self, seconds): """ setAdInterval(float seconds) Set how many seconds each ad should display, before rotating to the next. """ self._adInterval = seconds * 1000 self._timer.setInterval(self._adInterval) def start(self): """ Starts the rotator and loads the first image""" self.loadCurrentAd() self._timer.start() def stop(self): """ Stops the rotator """ self._timer.stop() def _renderUrl(self, buffer): """ Private slot Slot called when url has been loaded and is ready for rendering into the display as an image """ adSize = QSize(*self._adSize) if not buffer.isOpen(): buffer.open(QIODevice.ReadOnly) img = Image.open(buffer) temp = os.path.join( tempfile.gettempdir(), "ad_tmp_%s_%s.png" % (os.getpid(), int(time.time()))) img.save(temp) pixmap = QPixmap(temp) pixmap = pixmap.scaled(adSize, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) self.adDisplay.setPixmap(pixmap) link = self._data[0].get('link', None) self._link = QUrl(link) if link else None self._imageCache[self._data[0]['image']] = pixmap os.remove(temp) def _loadFinished(self, reqid, error): """ Private slot Called when the url has finished loading and is ready to be validated, before then starting the display render. Filters out images that have excessive loading errors. """ if reqid != self.__lastReqId: return errorCode = self.__http.error() statusCode = self.__http.lastResponse().statusCode() if errorCode == 0 and not error: buffer = QBuffer() buffer.setData(self.__http.readAll()) if buffer.size() > 0: self._renderUrl(buffer) self._timer.start() return self.stop() url = self._data[0]['image'] print "Failed to load", url, self._loadFailed[url] self._loadFailed[url] += 1 if self._loadFailed[url] >= 3: del self._data[0] self._loadFailed[url] = 0 if self._data: self.start() return else: self.rotateAd() self._timer.start() return def _loadUrl(self, url): """ _loadUrl(str url) Loads the given image url into the display """ self._timer.stop() self._link = None # self._page.mainFrame().load(QUrl(url)) qurl = QUrl(url) self.__http.setHost(qurl.host()) self.__lastReqId = self.__http.get(qurl.path())