def __init__(self, *args, **kwargs): super(AladinLiteQtWidget, self).__init__(*args, **kwargs) web = QWebEngineView() web.setHtml(ALADIN_LITE_HTML) layout = QVBoxLayout() self.setLayout(layout) layout.addWidget(web) self.page = web.page()
class CoreWWTQtWidget(QtWidgets.QWidget): def __init__(self, parent=None): super(CoreWWTQtWidget, self).__init__(parent=parent) self.web = QWebEngineView() self.page = WWTQWebEnginePage() self.page.setView(self.web) self.web.setPage(self.page) self.web.setHtml(WWT_HTML) layout = QtWidgets.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) layout.addWidget(self.web) self._wwt_ready = False self._js_queue = "" self.page.wwt_ready.connect(self._on_wwt_ready) def send_msg(self, **kwargs): msg = json.dumps(kwargs) return self._run_js("wwt_apply_json_message(wwt, {0})".format(msg)) def _on_wwt_ready(self): self._run_js(WWT_JSON) self._wwt_ready = True self._run_js(self._js_queue, async=True) self._js_queue = "" def _run_js(self, js, async=True): if not js: return if self._wwt_ready: logger.debug('Running javascript: %s' % js) return self.page.runJavaScript(js, async=async) else: logger.debug('Caching javascript: %s' % js) self._js_queue += js + '\n'
class ZhuNote(QWidget): html_hi = '<html> <body> <p> HTML Viewer </p> </body> </html>' html_no = '<html> <body> <p> No HTML </p> </body> </html>' def __init__(self, path=None): QWidget.__init__(self) self.setPath(path) self.initUi() self.setFont() self.loadMaster() def setPath(self, path): if path is None: self.path = os.getcwd() else: self.path = path print('Working directory:', self.path) def initUi(self): print('Initializing GUI...') w, h = 1000, 1000 self.find = ZhuNoteFind(self) # self as parent self.tree = ZhuNoteTree() self.form = ZhuNoteForm(self.path) self.wbrs = QWebEngineView() splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(self.form) splitter1.addWidget(self.wbrs) splitter1.setSizes([w / 2, w / 2]) splitter = QSplitter(Qt.Vertical) splitter.addWidget(self.tree) splitter.addWidget(splitter1) splitter.setStretchFactor(0, 1) splitter.setStretchFactor(1, 2) vbox = QVBoxLayout() vbox.addWidget(self.find) vbox.addWidget(splitter) self.setLayout(vbox) self.wbrs.setHtml(self.html_hi) self.tree.sigViewItem.connect(self.form.viewDict) self.tree.sigViewItem.connect(self.viewHtml) self.find.sigClear.connect(self.clear) self.find.sigString.connect(self.search) self.find.sigUpdateMaster.connect(self.updateMaster) self.find.sigFont.connect(self.setFont) self.setWindowTitle('Main - ZhuNote') #self.setGeometry(x, y, w, h) #self.move(x, y) self.resize(w, h) #self.show() #self.tree.show() #self.form.show() styleName = 'Cleanlooks' # QStyleFactory.keys() # ['Windows', 'Motif', 'CDE', 'Plastique', 'GTK+', 'Cleanlooks'] QApplication.setStyle(QStyleFactory.create(styleName)) self.find.txtSearch.setFocus() # not work yet self.actExit = QAction('Exit', self) self.actExit.setShortcut('Ctrl+Q') self.actExit.triggered.connect(self.closeAllWindows) self.addAction(self.actExit) def viewHtml(self, dictNote): htmlfn = dictNote['HTML'] fn = os.path.join(self.path, htmlfn) if os.path.isfile(fn): url = QUrl.fromLocalFile(fn) self.wbrs.load(url) else: #self.wbrs.setHtml('') # blank page self.wbrs.setHtml(self.html_no) def setFont(self, font=None): if font is None: font = QFont() # default font font.setFamily('Courier New') font.setPointSize(11) self.find.txtSearch.setFont(font) self.tree.setFont(font) self.form.setFont(font) def closeAllWindows(self): app = QApplication.instance() app.closeAllWindows() def loadMaster(self): fn = 'notemaster.pickle' self.masterfn = os.path.join(self.path, fn) if os.path.isfile(self.masterfn): print('Loading database:', self.masterfn) with open(self.masterfn, 'rb') as f: self.dod = pickle.load(f) def clear(self): self.find.txtSearch.clear() self.tree.clear() self.form.clear() self.wbrs.setHtml(self.html_hi) def search(self, string): self.tree.clear() # clear tree before a new search self.tree.lod = [] # used in tree class # break string to words by space stringLC = string.lower() words = stringLC.split() for key in self.dod: dictNote = self.dod[key] keyword = dictNote['Keyword'] title = dictNote['Title'] sstring = title + ' ' + keyword # string to be searched sstring = sstring.lower() if any(word in sstring for word in words): # weak search #if all(word in kw for word in words): # strong search self.tree.addItem(dictNote) self.tree.sortItems(0, Qt.AscendingOrder) def updateMaster(self): """ i self.path : string, path o file : write pickle file (list of dictionaries) In path, each pkl file contains one dictionary. This method merges all the dictionaries to a list for search. """ print('Number of entries old =', len(self.dod)) mt_master = os.path.getmtime(self.masterfn) for fn in os.listdir(self.path): if fn.endswith(".pkl"): ffn = os.path.join(self.path, fn) mt_entry = os.path.getmtime(ffn) if mt_entry >= mt_master: # if True: with open(ffn, 'rb') as f: dictNote = pickle.load(f) title = dictNote['Title'] if title in self.dod: print("Modify existing entry:", ffn) else: print("Add new entry:", ffn) self.dod[title] = dictNote with open(self.masterfn, 'wb') as f: pickle.dump(self.dod, f, -1) print('Merged file is', self.masterfn) print('Number of entries new =', len(self.dod))
print("url changed to: ") print(qulr) app = QApplication([]) view = QWebEngineView() page = view.page() view.loadStarted.connect(print("loadStarted")) view.loadProgress.connect(print("loadProgress")) view.loadFinished.connect(finishLoading) view.titleChanged.connect(titleChanged) page.selectionChanged.connect(selectionChanged) page.linkHovered.connect(linkHovered) page.urlChanged.connect(urlChanged) # content in unicode html format - Content larger than 2 MB cannot be displayed html = "<h1>Heading</h1><p>paragraph</p><p><a href=\"https://marvel.bible\"><img src='marvel.png' alt='Marvel.Bible icon'></a></p>" # External objects, such as stylesheets or images referenced in the HTML document, are located RELATIVE TO baseUrl . # e.g. put all local files linked by html's content in folder "htmlResources" relativePath = os.path.join("htmlResources", "marvel.png") absolutePath = os.path.abspath(relativePath) baseUrl = QUrl.fromLocalFile(absolutePath) view.setHtml(html, baseUrl) view.show() app.exec_()
class WWTQtWidget(QtWidgets.QWidget): def __init__(self, parent=None): super(WWTQtWidget, self).__init__(parent=parent) self.web = QWebEngineView() self.page = WWTQWebEnginePage() self.page.setView(self.web) self.web.setPage(self.page) self.web.setHtml(WWT_HTML) layout = QtWidgets.QVBoxLayout() self.setLayout(layout) layout.addWidget(self.web) self.imagery_layers = get_imagery_layers() self.markers = WWTMarkersHelper(self) self._wwt_ready = False self._js_queue = "" self.page.wwt_ready.connect(self._on_wwt_ready) self._opacity = 50 @property def foreground_opacity(self): return self._opacity @foreground_opacity.setter def foreground_opacity(self, value): if value < 0 or value > 100: raise ValueError('opacity should be in the range [0:100]') self._opacity = value self._update_opacity() def _update_opacity(self): self.run_js('wwt.setForegroundOpacity({0})'.format( self.foreground_opacity)) @property def galactic(self): return self._galactic @galactic.setter def galactic(self, value): if not isinstance(value, bool): raise TypeError('galactic should be set to a boolean value') self.run_js('wwt.settings.set_galacticMode({0});'.format( str(value).lower())) @property def foreground(self): return self._foreground @foreground.setter def foreground(self, value): if value not in self.imagery_layers: raise ValueError('unknown foreground: {0}'.format(value)) self.run_js('wwt.setForegroundImageByName("{0}");'.format(value)) self._update_opacity() @property def background(self): return self._background @background.setter def background(self, value): if value not in self.imagery_layers: raise ValueError('unknown background: {0}'.format(value)) self.run_js('wwt.setBackgroundImageByName("{0}");'.format(value)) self._update_opacity() def _on_wwt_ready(self): self._wwt_ready = True self.run_js(self._js_queue) self._js_queue = "" def run_js(self, js): if not js: return if self._wwt_ready: logger.debug('Running javascript: %s' % js) self.page.runJavaScript(js) else: logger.debug('Caching javascript: %s' % js) self._js_queue += js + '\n'