def testFetchUrlContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html
def testFetchEncodedContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/encoded_html.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert chr(6040) in html
def testDoubleFetch(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://www.qgis.org/')) # double fetch - this should happen before previous request finishes fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html
def testDoubleFetch(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://www.qgis.org/')) # double fetch - this should happen before previous request finishes fetcher.fetchContent( QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html
class VersionChecker: def __init__(self, dialog: QDialog, url): """ Update the dialog when versions has been fetched. """ self.dialog = dialog self.url = url self.fetcher = None self.json = None def fetch(self): """ Fetch the JSON file and call the function when it's finished. """ self.fetcher = QgsNetworkContentFetcher() self.fetcher.finished.connect(self.request_finished) self.fetcher.fetchContent(QUrl(self.url)) def request_finished(self): """ Dispatch the answer to update the GUI. """ content = self.fetcher.contentAsString() if not content: return # Update the UI released_versions = json.loads(content) self.update_lwc_releases(released_versions) self.update_lwc_selector(released_versions) # Cache the file content += '\n' folder = lizmap_user_folder() with open(os.path.join(folder, "released_versions.json"), "w") as output: output.write(content) def update_lwc_selector(self, released_versions: dict): """ Update LWC selector showing outdated versions. """ for i, json_version in enumerate(released_versions): if not json_version['maintained']: index = self.dialog.combo_lwc_version.findData(LwcVersions(json_version['branch'])) text = self.dialog.combo_lwc_version.itemText(index) if i == 0: # If it's the first item in the list AND not maintained, then it's the next LWC version new_text = text + ' - ' + tr('Next') else: new_text = text + ' - ' + tr('Not maintained') self.dialog.combo_lwc_version.setItemText(index, new_text) def update_lwc_releases(self, released_versions: dict): """ Update labels about latest releases. """ template = ( '<a href="https://github.com/3liz/lizmap-web-client/releases/tag/{tag}">' '{tag} - {date}' '</a>') i = 0 for json_version in released_versions: qdate = QDate.fromString( json_version['latest_release_date'], "yyyy-MM-dd") date_string = qdate.toString(QLocale().dateFormat(QLocale.ShortFormat)) if json_version['maintained']: if i == 0: text = template.format( tag=json_version['latest_release_version'], date=date_string, ) self.dialog.lwc_version_latest.setText(text) elif i == 1: text = template.format( tag=json_version['latest_release_version'], date=date_string, ) self.dialog.lwc_version_oldest.setText(text) i += 1
class AutoSuggest(QObject): def __init__(self, geturl_func, parseresult_func, parent = None): QObject.__init__(self, parent) self.geturl_func = geturl_func self.parseresult_func = parseresult_func self.editor = parent self.networkFetcher = QgsNetworkContentFetcher() self.selectedObject = None self.isUnloaded = False self.popup = QTreeWidget(parent) #self.popup.setColumnCount(2) self.popup.setColumnCount(1) self.popup.setUniformRowHeights(True) self.popup.setRootIsDecorated(False) self.popup.setEditTriggers(QTreeWidget.NoEditTriggers) self.popup.setSelectionBehavior(QTreeWidget.SelectRows) self.popup.setFrameStyle(QFrame.Box | QFrame.Plain) self.popup.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.popup.header().hide() self.popup.installEventFilter(self) self.popup.setMouseTracking(True) #self.connect(self.popup, SIGNAL("itemClicked(QTreeWidgetItem*, int)"), # self.doneCompletion) self.popup.itemClicked.connect( self.doneCompletion ) self.popup.setWindowFlags(Qt.Popup) self.popup.setFocusPolicy(Qt.NoFocus) self.popup.setFocusProxy(parent) self.timer = QTimer(self) self.timer.setSingleShot(True) self.timer.setInterval(500) #self.connect(self.timer, SIGNAL("timeout()"), self.autoSuggest) self.timer.timeout.connect( self.autoSuggest ) #self.connect(self.editor, SIGNAL("textEdited(QString)"), self.timer, SLOT("start()")) #self.editor.textEdited.connect( self.timer.start ) self.editor.textEdited.connect( self.timer.start ) #self.editor.textChanged.connect( self.timer.start ) #self.connect(self.networkManager, SIGNAL("finished(QNetworkReply*)"), # self.handleNetworkData) self.networkFetcher.finished.connect( self.handleNetworkData ) def eventFilter(self, obj, ev): if obj != self.popup: return False if ev.type() == QEvent.MouseButtonPress: self.popup.hide() self.editor.setFocus() return True if ev.type() == QEvent.KeyPress: consumed = False key = ev.key() if key == Qt.Key_Enter or key == Qt.Key_Return: self.doneCompletion() consumed = True elif key == Qt.Key_Escape: self.editor.setFocus() self.popup.hide() consumed = True elif key in (Qt.Key_Up, Qt.Key_Down, Qt.Key_Home, Qt.Key_End, Qt.Key_PageUp, Qt.Key_PageDown): pass else: self.editor.setFocus() self.editor.event(ev) self.popup.hide() return consumed return False def showCompletion(self, rows): # Rows is an iterable of tuples like [("text",object1),("text2", object2),...] pal = self.editor.palette() color = pal.color(QPalette.Disabled, QPalette.WindowText) self.popup.setUpdatesEnabled(False) self.popup.clear() if rows is None or len( rows ) < 1: return for row in rows: item = QTreeWidgetItem(self.popup) item.setText(0, row[0]) #item.setText(1, hit['type']) item.setTextAlignment(1, Qt.AlignRight) item.setForeground(1, color) item.setData(2, Qt.UserRole, (row[1],)) # Try immutable py obj #http://stackoverflow.com/questions/9257422/how-to-get-the-original-python-data-from-qvariant self.popup.setCurrentItem(self.popup.topLevelItem(0)) self.popup.resizeColumnToContents(0) #self.popup.resizeColumnToContents(1) self.popup.adjustSize() self.popup.setUpdatesEnabled(True) h = self.popup.sizeHintForRow(0) * min(15, len(rows)) + 3 w = max(self.popup.width(), self.editor.width()) self.popup.resize(w, h) self.popup.move(self.editor.mapToGlobal(QPoint(0, self.editor.height()))) self.popup.setFocus() self.popup.show() def doneCompletion(self): self.timer.stop() self.popup.hide() self.editor.setFocus() item = self.popup.currentItem() if item: self.editor.setText(item.text(0) ) obj = item.data(2, Qt.UserRole) #.toPyObject() self.selectedObject = obj[0] e = QKeyEvent(QEvent.KeyPress, Qt.Key_Enter, Qt.NoModifier) QApplication.postEvent(self.editor, e) e = QKeyEvent(QEvent.KeyRelease, Qt.Key_Enter, Qt.NoModifier) QApplication.postEvent(self.editor, e) def preventSuggest(self): self.timer.stop() def autoSuggest(self): term = self.editor.text() if term: qurl = self.geturl_func( term ) if qurl: # TODO: Cancel existing requests: http://qt-project.org/forums/viewthread/18073 self.networkFetcher.fetchContent(qurl) # QUrl(url))) def handleNetworkData(self): reply = self.networkFetcher.reply() if reply: if reply.error() == QNetworkReply.NoError: content = self.networkFetcher.contentAsString() rows = self.parseresult_func( content ) self.showCompletion( rows ) else: QgsApplication.messageLog().logMessage( 'Server returned: ' + reply.error(), __package__ ) def unload( self ): # Avoid processing events after QGIS shutdown has begun self.popup.removeEventFilter(self) self.isUnloaded = True