class MainWindow(QDialog): def __init__(self, default_status_msg=_('Welcome to') + ' ' + __appname__+' console', parent=None): QDialog.__init__(self, parent) self.restart_requested = False self.l = QVBoxLayout() self.setLayout(self.l) self.resize(800, 600) geom = dynamic.get('console_window_geometry', None) if geom is not None: self.restoreGeometry(geom) # Setup tool bar {{{ self.tool_bar = QToolBar(self) self.tool_bar.setToolButtonStyle(Qt.ToolButtonTextOnly) self.l.addWidget(self.tool_bar) # }}} # Setup status bar {{{ self.status_bar = QStatusBar(self) self.status_bar.defmsg = QLabel(__appname__ + _(' console ') + __version__) self.status_bar._font = QFont() self.status_bar._font.setBold(True) self.status_bar.defmsg.setFont(self.status_bar._font) self.status_bar.addWidget(self.status_bar.defmsg) # }}} self.console = Console(parent=self) self.console.running.connect(partial(self.status_bar.showMessage, _('Code is running'))) self.console.running_done.connect(self.status_bar.clearMessage) self.l.addWidget(self.console) self.l.addWidget(self.status_bar) self.setWindowTitle(__appname__ + ' console') self.setWindowIcon(QIcon(I('console.png'))) self.restart_action = QAction(_('Restart console'), self) self.restart_action.setShortcut(_('Ctrl+R')) self.addAction(self.restart_action) self.restart_action.triggered.connect(self.restart) self.console.context_menu.addAction(self.restart_action) def restart(self): self.restart_requested = True self.reject() def closeEvent(self, *args): dynamic.set('console_window_geometry', bytearray(self.saveGeometry())) self.console.shutdown() return QDialog.closeEvent(self, *args)
def addActionToList(self, txtMenu, whichList, fctnCalled, status="", shortkey=""): """ add an action to 'whichList' """ theAction = QAction(txtMenu, self.parent) theAction.setStatusTip(status) theAction.setShortcut(shortkey) self.connect(theAction, SIGNAL("triggered()"), fctnCalled) whichList.append(theAction)
def createAction(self, text, slot = None, shortcut = None, icon = None, tip = None, checkable = False, signal = "triggered()"): action = QAction(text, self) if icon is not None: action.setIcon(QIcon(osp.sep.join((self.imagepath, "%s.png" % icon)))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: self.connect(action, SIGNAL(signal), slot) if checkable: action.setCheckable(True) return action
def __init__(self): QWidget.__init__(self) # define periodic table widget for element selection self.periodicTableWidget = widgets.PeriodicTableDialog() # initial molecule Zmatrix (can be empty) # self.inp = [] self.inp = [['H'], ['O', 1, 0.9], ['O', 2, 1.4, 1, 105.], ['H', 3, 0.9, 2, 105., 1, 120.]] self.atomList = [] self.highList = [] self.labelList = [] self.fast = False # define & initialize ZMatModel that will contain Zmatrix data self.ZMatModel = QStandardItemModel(len(self.inp), 7, self) self.ZMatTable = QTableView(self) self.ZMatTable.setModel(self.ZMatModel) self.ZMatTable.setFixedWidth(325) #self.ZMatTable.installEventFilter(self) #self.ZMatModel.installEventFilter(self) self.ZMatModel.setHorizontalHeaderLabels(['atom','','bond','','angle','','dihedral']) for j, width in enumerate([40, 22, 65, 22, 65, 22, 65]): self.ZMatTable.setColumnWidth(j, width) # populate the ZMatModel self.populateZMatModel() #define Menu bar menus and their actions self.menuBar = QMenuBar(self) fileMenu = self.menuBar.addMenu('&File') editMenu = self.menuBar.addMenu('&Edit') viewMenu = self.menuBar.addMenu('&View') measureMenu = self.menuBar.addMenu('&Measure') helpMenu = self.menuBar.addMenu('&Help') readZmatAction = QAction('&Read &ZMat', self) readZmatAction.setShortcut('Ctrl+O') readZmatAction.setStatusTip('Read Zmat from file') readZmatAction.triggered.connect(self.readZmat) fileMenu.addAction(readZmatAction) readXYZAction = QAction('&Read &XYZ', self) readXYZAction.setShortcut('Ctrl+Shift+O') readXYZAction.setStatusTip('Read XYZ from file') readXYZAction.triggered.connect(self.readXYZ) fileMenu.addAction(readXYZAction) readGaussianAction = QAction('&Read &Gaussian log', self) readGaussianAction.setShortcut('Ctrl+G') readGaussianAction.setStatusTip('Read Gaussian log file') readGaussianAction.triggered.connect(self.readGaussian) fileMenu.addAction(readGaussianAction) writeZmatAction = QAction('&Write &ZMat', self) writeZmatAction.setShortcut('Ctrl+S') writeZmatAction.setStatusTip('Write Zmat to file') writeZmatAction.triggered.connect(self.writeZmat) fileMenu.addAction(writeZmatAction) writeXYZAction = QAction('&Write &XYZ', self) writeXYZAction.setShortcut('Ctrl+Shift+S') writeXYZAction.setStatusTip('Write XYZ from file') writeXYZAction.triggered.connect(self.writeXYZ) fileMenu.addAction(writeXYZAction) exitAction = QAction('&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(qApp.quit) fileMenu.addAction(exitAction) addRowAction = QAction('&Add &row', self) addRowAction.setShortcut('Ctrl+R') addRowAction.setStatusTip('Add row to ZMatrix') addRowAction.triggered.connect(self.addRow) editMenu.addAction(addRowAction) deleteRowAction = QAction('&Delete &row', self) deleteRowAction.setShortcut('Ctrl+Shift+R') deleteRowAction.setStatusTip('Delete row from ZMatrix') deleteRowAction.triggered.connect(self.deleteRow) editMenu.addAction(deleteRowAction) addAtomAction = QAction('&Add &atom', self) addAtomAction.setShortcut('Ctrl+A') addAtomAction.setStatusTip('Add atom to ZMatrix') addAtomAction.triggered.connect(self.buildB) editMenu.addAction(addAtomAction) drawModeMenu = QMenu('Draw mode', self) viewMenu.addMenu(drawModeMenu) fastDrawAction = QAction('&Fast draw', self) fastDrawAction.triggered.connect(self.fastDraw) normalDrawAction = QAction('&Normal draw', self) normalDrawAction.triggered.connect(self.normalDraw) drawModeMenu.addAction(normalDrawAction) drawModeMenu.addAction(fastDrawAction) clearHighlightsAction = QAction('&Clear selection', self) clearHighlightsAction.setShortcut('Ctrl+C') clearHighlightsAction.setStatusTip('Clear highlighted atoms') clearHighlightsAction.triggered.connect(self.clearHighlights) viewMenu.addAction(clearHighlightsAction) clearLabelsAction = QAction('&Clear labels', self) clearLabelsAction.setShortcut('Ctrl+Alt+C') clearLabelsAction.setStatusTip('Clear labels') clearLabelsAction.triggered.connect(self.clearLabels) viewMenu.addAction(clearLabelsAction) clearUpdateViewAction = QAction('&Clear selection and labels', self) clearUpdateViewAction.setShortcut('Ctrl+Shift+C') clearUpdateViewAction.setStatusTip('Clear highlighted atoms and labels') clearUpdateViewAction.triggered.connect(self.clearUpdateView) viewMenu.addAction(clearUpdateViewAction) self.showGaussAction = QAction('Show &Gaussian geometry optimization', self) self.showGaussAction.setShortcut('Ctrl+G') self.showGaussAction.setStatusTip('Show Gaussian geometry optimization plots for energy, force and displacement.') self.showGaussAction.setEnabled(False) self.showGaussAction.triggered.connect(self.showGauss) viewMenu.addAction(self.showGaussAction) self.showFreqAction = QAction('Show &IR frequency plot', self) self.showFreqAction.setShortcut('Ctrl+I') self.showFreqAction.setStatusTip('Show Gaussian calculated IR frequency plot.') self.showFreqAction.setEnabled(False) self.showFreqAction.triggered.connect(self.showFreq) viewMenu.addAction(self.showFreqAction) measureDistanceAction = QAction('&Measure &distance', self) measureDistanceAction.setShortcut('Ctrl+D') measureDistanceAction.setStatusTip('Measure distance between two atoms') measureDistanceAction.triggered.connect(self.measureDistanceB) measureMenu.addAction(measureDistanceAction) measureAngleAction = QAction('&Measure &angle', self) measureAngleAction.setShortcut('Ctrl+Shift+D') measureAngleAction.setStatusTip('Measure angle between three atoms') measureAngleAction.triggered.connect(self.measureAngleB) measureMenu.addAction(measureAngleAction) aboutAction = QAction('&About', self) aboutAction.setStatusTip('About this program...') aboutAction.triggered.connect(self.about) helpMenu.addAction(aboutAction) aboutQtAction = QAction('&About Qt', self) aboutQtAction.setStatusTip('About Qt...') aboutQtAction.triggered.connect(self.aboutQt) helpMenu.addAction(aboutQtAction) # define GL widget that displays the 3D molecule model self.window = widgets.MyGLView() self.window.installEventFilter(self) self.window.setMinimumSize(500, 500) #self.window.setBackgroundColor((50, 0, 10)) self.updateView() self.gaussianPlot = GraphicsLayoutWidget() self.gaussianPlot.resize(750, 250) self.gaussianPlot.setWindowTitle('Gaussian geometry optimization') #self.gaussianPlot.setAspectLocked(True) #self.gaussianPlot.addLayout(rowspan=3, colspan=1) self.FreqModel = QStandardItemModel(1, 3, self) self.freqTable = QTableView(self) self.freqTable.setModel(self.FreqModel) self.freqTable.setMinimumWidth(240) self.freqTable.installEventFilter(self) self.FreqModel.installEventFilter(self) self.FreqModel.setHorizontalHeaderLabels(['Frequency','IR Intensity','Raman Intensity']) for j, width in enumerate([80, 80, 80]): self.freqTable.setColumnWidth(j, width) self.freqWidget = QWidget() self.freqWidget.setWindowTitle('IR frequency plot & table') self.freqWidget.resize(800, 400) self.freqWidget.layout = QHBoxLayout(self.freqWidget) self.freqWidget.layout.setSpacing(1) self.freqWidget.layout.setContentsMargins(1, 1, 1, 1) self.freqPlot = GraphicsLayoutWidget() self.freqWidget.layout.addWidget(self.freqPlot) self.freqWidget.layout.addWidget(self.freqTable) self.freqTable.clicked.connect(self.freqCellClicked) # define other application parts self.statusBar = QStatusBar(self) self.fileDialog = QFileDialog(self) # define application layout self.layout = QVBoxLayout(self) self.layout.setSpacing(1) self.layout.setContentsMargins(1, 1, 1, 1) self.layout1 = QHBoxLayout() self.layout1.setSpacing(1) self.layout1.addWidget(self.ZMatTable) self.layout1.addWidget(self.window) self.layout.addWidget(self.menuBar) self.layout.addLayout(self.layout1) self.layout.addWidget(self.statusBar) self.adjustSize() self.setWindowTitle('Moldy') iconPath = 'icon.png' icon = QIcon(iconPath) icon.addFile(iconPath, QSize(16, 16)) icon.addFile(iconPath, QSize(24, 24)) icon.addFile(iconPath, QSize(32, 32)) icon.addFile(iconPath, QSize(48, 48)) icon.addFile(iconPath, QSize(256, 256)) self.setWindowIcon(icon) # start monitoring changes in the ZMatModel self.ZMatModel.dataChanged.connect(self.clearUpdateView)
class MainWidget(QWidget): def __init__(self): QWidget.__init__(self) # define periodic table widget for element selection self.periodicTableWidget = widgets.PeriodicTableDialog() # initial molecule Zmatrix (can be empty) # self.inp = [] self.inp = [['H'], ['O', 1, 0.9], ['O', 2, 1.4, 1, 105.], ['H', 3, 0.9, 2, 105., 1, 120.]] self.atomList = [] self.highList = [] self.labelList = [] self.fast = False # define & initialize ZMatModel that will contain Zmatrix data self.ZMatModel = QStandardItemModel(len(self.inp), 7, self) self.ZMatTable = QTableView(self) self.ZMatTable.setModel(self.ZMatModel) self.ZMatTable.setFixedWidth(325) #self.ZMatTable.installEventFilter(self) #self.ZMatModel.installEventFilter(self) self.ZMatModel.setHorizontalHeaderLabels(['atom','','bond','','angle','','dihedral']) for j, width in enumerate([40, 22, 65, 22, 65, 22, 65]): self.ZMatTable.setColumnWidth(j, width) # populate the ZMatModel self.populateZMatModel() #define Menu bar menus and their actions self.menuBar = QMenuBar(self) fileMenu = self.menuBar.addMenu('&File') editMenu = self.menuBar.addMenu('&Edit') viewMenu = self.menuBar.addMenu('&View') measureMenu = self.menuBar.addMenu('&Measure') helpMenu = self.menuBar.addMenu('&Help') readZmatAction = QAction('&Read &ZMat', self) readZmatAction.setShortcut('Ctrl+O') readZmatAction.setStatusTip('Read Zmat from file') readZmatAction.triggered.connect(self.readZmat) fileMenu.addAction(readZmatAction) readXYZAction = QAction('&Read &XYZ', self) readXYZAction.setShortcut('Ctrl+Shift+O') readXYZAction.setStatusTip('Read XYZ from file') readXYZAction.triggered.connect(self.readXYZ) fileMenu.addAction(readXYZAction) readGaussianAction = QAction('&Read &Gaussian log', self) readGaussianAction.setShortcut('Ctrl+G') readGaussianAction.setStatusTip('Read Gaussian log file') readGaussianAction.triggered.connect(self.readGaussian) fileMenu.addAction(readGaussianAction) writeZmatAction = QAction('&Write &ZMat', self) writeZmatAction.setShortcut('Ctrl+S') writeZmatAction.setStatusTip('Write Zmat to file') writeZmatAction.triggered.connect(self.writeZmat) fileMenu.addAction(writeZmatAction) writeXYZAction = QAction('&Write &XYZ', self) writeXYZAction.setShortcut('Ctrl+Shift+S') writeXYZAction.setStatusTip('Write XYZ from file') writeXYZAction.triggered.connect(self.writeXYZ) fileMenu.addAction(writeXYZAction) exitAction = QAction('&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(qApp.quit) fileMenu.addAction(exitAction) addRowAction = QAction('&Add &row', self) addRowAction.setShortcut('Ctrl+R') addRowAction.setStatusTip('Add row to ZMatrix') addRowAction.triggered.connect(self.addRow) editMenu.addAction(addRowAction) deleteRowAction = QAction('&Delete &row', self) deleteRowAction.setShortcut('Ctrl+Shift+R') deleteRowAction.setStatusTip('Delete row from ZMatrix') deleteRowAction.triggered.connect(self.deleteRow) editMenu.addAction(deleteRowAction) addAtomAction = QAction('&Add &atom', self) addAtomAction.setShortcut('Ctrl+A') addAtomAction.setStatusTip('Add atom to ZMatrix') addAtomAction.triggered.connect(self.buildB) editMenu.addAction(addAtomAction) drawModeMenu = QMenu('Draw mode', self) viewMenu.addMenu(drawModeMenu) fastDrawAction = QAction('&Fast draw', self) fastDrawAction.triggered.connect(self.fastDraw) normalDrawAction = QAction('&Normal draw', self) normalDrawAction.triggered.connect(self.normalDraw) drawModeMenu.addAction(normalDrawAction) drawModeMenu.addAction(fastDrawAction) clearHighlightsAction = QAction('&Clear selection', self) clearHighlightsAction.setShortcut('Ctrl+C') clearHighlightsAction.setStatusTip('Clear highlighted atoms') clearHighlightsAction.triggered.connect(self.clearHighlights) viewMenu.addAction(clearHighlightsAction) clearLabelsAction = QAction('&Clear labels', self) clearLabelsAction.setShortcut('Ctrl+Alt+C') clearLabelsAction.setStatusTip('Clear labels') clearLabelsAction.triggered.connect(self.clearLabels) viewMenu.addAction(clearLabelsAction) clearUpdateViewAction = QAction('&Clear selection and labels', self) clearUpdateViewAction.setShortcut('Ctrl+Shift+C') clearUpdateViewAction.setStatusTip('Clear highlighted atoms and labels') clearUpdateViewAction.triggered.connect(self.clearUpdateView) viewMenu.addAction(clearUpdateViewAction) self.showGaussAction = QAction('Show &Gaussian geometry optimization', self) self.showGaussAction.setShortcut('Ctrl+G') self.showGaussAction.setStatusTip('Show Gaussian geometry optimization plots for energy, force and displacement.') self.showGaussAction.setEnabled(False) self.showGaussAction.triggered.connect(self.showGauss) viewMenu.addAction(self.showGaussAction) self.showFreqAction = QAction('Show &IR frequency plot', self) self.showFreqAction.setShortcut('Ctrl+I') self.showFreqAction.setStatusTip('Show Gaussian calculated IR frequency plot.') self.showFreqAction.setEnabled(False) self.showFreqAction.triggered.connect(self.showFreq) viewMenu.addAction(self.showFreqAction) measureDistanceAction = QAction('&Measure &distance', self) measureDistanceAction.setShortcut('Ctrl+D') measureDistanceAction.setStatusTip('Measure distance between two atoms') measureDistanceAction.triggered.connect(self.measureDistanceB) measureMenu.addAction(measureDistanceAction) measureAngleAction = QAction('&Measure &angle', self) measureAngleAction.setShortcut('Ctrl+Shift+D') measureAngleAction.setStatusTip('Measure angle between three atoms') measureAngleAction.triggered.connect(self.measureAngleB) measureMenu.addAction(measureAngleAction) aboutAction = QAction('&About', self) aboutAction.setStatusTip('About this program...') aboutAction.triggered.connect(self.about) helpMenu.addAction(aboutAction) aboutQtAction = QAction('&About Qt', self) aboutQtAction.setStatusTip('About Qt...') aboutQtAction.triggered.connect(self.aboutQt) helpMenu.addAction(aboutQtAction) # define GL widget that displays the 3D molecule model self.window = widgets.MyGLView() self.window.installEventFilter(self) self.window.setMinimumSize(500, 500) #self.window.setBackgroundColor((50, 0, 10)) self.updateView() self.gaussianPlot = GraphicsLayoutWidget() self.gaussianPlot.resize(750, 250) self.gaussianPlot.setWindowTitle('Gaussian geometry optimization') #self.gaussianPlot.setAspectLocked(True) #self.gaussianPlot.addLayout(rowspan=3, colspan=1) self.FreqModel = QStandardItemModel(1, 3, self) self.freqTable = QTableView(self) self.freqTable.setModel(self.FreqModel) self.freqTable.setMinimumWidth(240) self.freqTable.installEventFilter(self) self.FreqModel.installEventFilter(self) self.FreqModel.setHorizontalHeaderLabels(['Frequency','IR Intensity','Raman Intensity']) for j, width in enumerate([80, 80, 80]): self.freqTable.setColumnWidth(j, width) self.freqWidget = QWidget() self.freqWidget.setWindowTitle('IR frequency plot & table') self.freqWidget.resize(800, 400) self.freqWidget.layout = QHBoxLayout(self.freqWidget) self.freqWidget.layout.setSpacing(1) self.freqWidget.layout.setContentsMargins(1, 1, 1, 1) self.freqPlot = GraphicsLayoutWidget() self.freqWidget.layout.addWidget(self.freqPlot) self.freqWidget.layout.addWidget(self.freqTable) self.freqTable.clicked.connect(self.freqCellClicked) # define other application parts self.statusBar = QStatusBar(self) self.fileDialog = QFileDialog(self) # define application layout self.layout = QVBoxLayout(self) self.layout.setSpacing(1) self.layout.setContentsMargins(1, 1, 1, 1) self.layout1 = QHBoxLayout() self.layout1.setSpacing(1) self.layout1.addWidget(self.ZMatTable) self.layout1.addWidget(self.window) self.layout.addWidget(self.menuBar) self.layout.addLayout(self.layout1) self.layout.addWidget(self.statusBar) self.adjustSize() self.setWindowTitle('Moldy') iconPath = 'icon.png' icon = QIcon(iconPath) icon.addFile(iconPath, QSize(16, 16)) icon.addFile(iconPath, QSize(24, 24)) icon.addFile(iconPath, QSize(32, 32)) icon.addFile(iconPath, QSize(48, 48)) icon.addFile(iconPath, QSize(256, 256)) self.setWindowIcon(icon) # start monitoring changes in the ZMatModel self.ZMatModel.dataChanged.connect(self.clearUpdateView) # run and show the application def run(self): self.show() self.ZMatTable.clicked.connect(self.ZMatCellClicked) qt_app.instance().aboutToQuit.connect(self.deleteGLwidget) qt_app.exec_() # fill the ZMatModel with initial data from 'self.inp' def populateZMatModel(self): self.ZMatModel.removeRows(0, self.ZMatModel.rowCount()) for i, row in enumerate(self.inp): for j, cell in enumerate(row): item = QStandardItem(str(cell)) self.ZMatModel.setItem(i, j, item) # some cells should not be editable, they are disabled for i in range(min(len(self.inp), 3)): for j in range(2*i+1, 7): self.ZMatModel.setItem(i, j, QStandardItem()) self.ZMatModel.item(i, j).setBackground(QColor(150,150,150)) self.ZMatModel.item(i, j).setFlags(Qt.ItemIsEnabled) def populateFreqModel(self): self.FreqModel.removeRows(0, self.FreqModel.rowCount()) for i, row in enumerate(zip(self.vibfreqs, self.vibirs, self.vibramans)): for j, cell in enumerate(row): item = QStandardItem(str(cell)) self.FreqModel.setItem(i, j, item) # add a row to the bottom of the ZMatModel def addRow(self): # temporarily stop updating the GL window self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) row = self.ZMatModel.rowCount() self.ZMatModel.insertRow(row) # some cells should not be editable if row < 3: for j in range(2*row+1, 7): self.ZMatModel.setItem(row, j, QStandardItem()) self.ZMatModel.item(row, j).setBackground(QColor(150,150,150)) self.ZMatModel.item(row, j).setFlags(Qt.ItemIsEnabled) # restart GL window updating self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.statusBar.clearMessage() self.statusBar.showMessage('Added 1 row.', 3000) # delete the last row of the ZMatModel def deleteRow(self): xyz = [list(vi) for vi in list(v)] atoms = [str(elements[e]) for e in elems] oldLen = self.ZMatModel.rowCount() idxs = sorted(set(idx.row() for idx in self.ZMatTable.selectedIndexes()), reverse=True) newLen = oldLen - len(idxs) if newLen == oldLen: self.ZMatModel.removeRow(self.ZMatModel.rowCount()-1) else: self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) for idx in idxs: self.ZMatModel.removeRow(idx) if idx < 3: for i in range(idx, min(3, newLen)): for j in range(2*i+1, 7): self.ZMatModel.setItem(i, j, QStandardItem()) self.ZMatModel.item(i, j).setBackground(QColor(150,150,150)) self.ZMatModel.item(i, j).setFlags(Qt.ItemIsEnabled) if len(xyz) > idx: xyz.pop(idx) atoms.pop(idx) self.inp = xyz2zmat(xyz, atoms) self.populateZMatModel() for i in reversed(self.highList): self.window.removeItem(i[1]) self.highList = [] self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.updateView() self.statusBar.clearMessage() if idxs: self.statusBar.showMessage('Deleted row(s): '+str([i+1 for i in idxs]), 3000) else: self.statusBar.showMessage('Deleted last row.', 3000) # show the periodic table widget def periodicTable(self): self.statusBar.clearMessage() self.statusBar.showMessage('Select element from periodic table.') self.periodicTableWidget.exec_() selection = self.periodicTableWidget.selection() return selection # import molecule with zmatrix coordinates def readZmat(self): self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) filename = self.fileDialog.getOpenFileName(self, 'Open file', expanduser('~'), '*.zmat;;*.*') self.inp = [] self.populateZMatModel() if filename: with open(filename, 'r') as f: next(f) next(f) for row in f: self.inp.append(row.split()) f.close() self.populateZMatModel() self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.updateView() self.statusBar.clearMessage() self.statusBar.showMessage('Read molecule from '+filename+'.', 5000) self.showGaussAction.setEnabled(False) self.showFreqAction.setEnabled(False) # import molecule with xyz coordinates def readXYZ(self): self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) filename = self.fileDialog.getOpenFileName(self, 'Open file', expanduser('~'), '*.xyz;;*.*') xyz = [] elems = [] self.inp = [] self.populateZMatModel() if filename: with open(filename, 'r') as f: next(f) next(f) for row in f: rs = row.split() if len(rs) == 4: elems.append(rs[0]) xyz.append([float(f) for f in rs[1:]]) f.close() self.inp = xyz2zmat(xyz, elems) self.populateZMatModel() #print(elems) self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.updateView() self.statusBar.clearMessage() self.statusBar.showMessage('Read molecule from '+filename+'.', 5000) self.showGaussAction.setEnabled(False) self.showFreqAction.setEnabled(False) # import Gaussian log file def readGaussian(self): global vsShifted self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) filename = self.fileDialog.getOpenFileName(self, 'Open file', expanduser('~'), '*.log;;*.*') if filename: self.gaussianPlot.clear() self.inp = [] self.populateZMatModel() file = ccopen(filename) data = file.parse().getattributes() self.natom = data['natom'] self.atomnos = data['atomnos'].tolist() self.atomsymbols = [ str(elements[e]) for e in self.atomnos ] self.atomcoords = data['atomcoords'].tolist() self.scfenergies = data['scfenergies'].tolist() self.geovalues = data['geovalues'].T.tolist() self.geotargets = data['geotargets'].tolist() if 'vibfreqs' in data.keys(): self.vibfreqs = data['vibfreqs'] #print(self.vibfreqs) self.vibirs = data['vibirs'] #print(self.vibirs) #print(data.keys()) if 'vibramans' in data.keys(): self.vibramans = data['vibramans'] else: self.vibramans = [''] * len(self.vibirs) self.vibdisps = data['vibdisps'] #print(self.vibdisps) self.inp = xyz2zmat(self.atomcoords[0], self.atomsymbols) self.populateZMatModel() titles = ['SCF Energies', 'RMS & Max Forces', 'RMS & Max Displacements'] for i in range(3): self.gaussianPlot.addPlot(row=1, col=i+1) plot = self.gaussianPlot.getItem(1, i+1) plot.setTitle(title=titles[i]) if i == 0: c = ['c'] x = [0] y = [self.scfenergies] else: c = ['r', 'y'] x = [0, 0] y = [self.geovalues[2*i-2], self.geovalues[2*i-1]] targety = [self.geotargets[2*i-2], self.geotargets[2*i-1]] plot.clear() plot.maxData = plot.plot(y[0], symbol='o', symbolPen=c[0], symbolBrush=c[0], pen=c[0], symbolSize=5, pxMode=True, antialias=True, autoDownsample=False) plot.highlight=plot.plot(x, [ yy[0] for yy in y ], symbol='o', symbolPen='w', symbolBrush=None, pen=None, symbolSize=15, pxMode=True, antialias=True, autoDownsample=False) plot.maxData.sigPointsClicked.connect(self.gausclicked) if i > 0: for j in range(2): plot.addLine(y=np.log10(targety[j]), pen=mkPen((255, 255*j, 0, int(255/2)), width=1)) plot.RMSData=plot.plot(y[1], symbol='o', symbolPen=c[1], symbolBrush=c[1], pen=c[1], symbolSize=5, pxMode=True, antialias=True, autoDownsample=False) plot.RMSData.sigPointsClicked.connect(self.gausclicked) plot.setLogMode(y=True) self.showGauss() self.updateView() self.statusBar.clearMessage() self.statusBar.showMessage('Read molecule from '+filename+'.', 5000) self.ZMatModel.dataChanged.connect(self.clearUpdateView) if self.natom: self.showGaussAction.setEnabled(True) if 'vibfreqs' in data.keys(): self.showFreqAction.setEnabled(True) # populate the FreqModel self.populateFreqModel() self.freqPlot.clear() irPlot = self.freqPlot.addPlot(row=1, col=1) irPlot.clear() minFreq = np.min(self.vibfreqs) maxFreq = np.max(self.vibfreqs) maxInt = np.max(self.vibirs) x = np.sort(np.concatenate([np.linspace(minFreq-100, maxFreq+100, num=1000), self.vibfreqs])) y = x*0 for f,i in zip(self.vibfreqs, self.vibirs): y += lorentzv(x, f, 2*np.pi, i) #xy = np.array([np.concatenate([x, np.array(self.vibfreqs)]), np.concatenate([y, np.array(self.vibirs)])]).T #xysort = xy[xy[:,0].argsort()] irPlot.maxData = irPlot.plot(x, y, antialias=True) markers = ErrorBarItem(x=self.vibfreqs, y=self.vibirs, top=maxInt/30, bottom=None, pen='r') irPlot.addItem(markers) self.showFreq() #self.vibdisps = np.append(self.vibdisps, [np.mean(self.vibdisps, axis=0)], axis=0) maxt = 100 vsShifted = np.array([ [ vs + self.vibdisps[i]*np.sin(t*2*np.pi/maxt)/3 for t in range(maxt) ] for i in range(len(self.vibfreqs)) ]) else: self.showFreqAction.setEnabled(False) self.freqWidget.hide() def showGauss(self): self.gaussianPlot.show() def showFreq(self): self.freqWidget.show() # export Zmatrix to csv def writeZmat(self): zm = model2list(self.ZMatModel) filename = self.fileDialog.getSaveFileName(self, 'Save file', expanduser('~')+'/'+getFormula(list(list(zip(*zm))[0]))+'.zmat', '*.zmat;;*.*') try: filename except NameError: pass else: if filename: writeOutput(zm, filename) self.statusBar.clearMessage() self.statusBar.showMessage('Wrote molecule to '+filename+'.', 5000) # export XYZ coordinates to csv def writeXYZ(self): xyz = [] zm = model2list(self.ZMatModel) for i in range(len(v)): xyz.append(np.round(v[i], 7).tolist()) xyz[i][:0] = zm[i][0] if len(v) > 0: formula = getFormula(list(list(zip(*xyz))[0])) else: formula = 'moldy_output' filename = self.fileDialog.getSaveFileName(self, 'Save file', expanduser('~')+'/'+formula+'.xyz', '*.xyz;;*.*') try: filename except NameError: pass else: if filename: writeOutput(xyz, filename) self.statusBar.clearMessage() self.statusBar.showMessage('Wrote molecule to '+filename+'.', 5000) # redraw the 3D molecule in GL widget def updateView(self): global r global c global v global vs global elems global nelems data = model2list(self.ZMatModel) try: # create a list with element coordinates v = zmat2xyz(data) except (AssertionError, IndexError, ZMError): pass else: # clear the screen before redraw for item in reversed(self.window.items): self.window.removeItem(item) # create a second coordinate list 'vs' that is centered in the GL view self.atomList = [] if len(v) > 0: shift = np.mean(v, axis=0) vs = np.add(v, -shift) elems = [ 1 + next((i for i, sublist in enumerate(colors) if row[0] in sublist), -1) for row in data ] nelems = len(elems) # define molecule radii and colors r = [] c = [] for i in elems: r.append(elements[i].covalent_radius) c.append(colors[i-1][-1]) # draw atoms for i in range(nelems): addAtom(self.window, i, r, vs, c, fast=self.fast) self.atomList.append([i, self.window.items[-1]]) #print(self.atomList) # draw bonds where appropriate combs = list(itertools.combinations(range(nelems), 2)) bonds = [] for i in combs: bonds.append(addBond(self.window, i[0], i[1], r, vs, c, fast=self.fast)) if self.fast: bondedAtoms = set(filter((None).__ne__, flatten(bonds))) for i in set(range(nelems)) - bondedAtoms: addUnbonded(self.window, i, vs, c) self.atomList[i][1]=self.window.items[-1] #print(self.atomList) for i in self.highList: self.window.addItem(i[1]) for i in self.labelList: self.window.addItem(i) if len(v) > 1: maxDim = float('-inf') for dim in v.T: span = max(dim)-min(dim) if span > maxDim: maxDim = span else: maxDim = 2 self.window.setCameraPosition(distance=maxDim*1.5+1) global index index = 0 def updateFreq(self): global vsShifted, index, r, c index += 1 index = index % len(vsShifted[0]) #print(index) #print(vsShifted[index]) for item in reversed(self.window.items): self.window.removeItem(item) for i in range(nelems): addAtom(self.window, i, r, vsShifted[self.freqIndex, index], c, fast=self.fast) self.atomList.append([i, self.window.items[-1]]) combs = itertools.combinations(range(nelems), 2) bonds = [] for i in combs: bonds.append(addBond(self.window, i[0], i[1], r, vsShifted[self.freqIndex, index], c, fast=self.fast)) if self.fast: bondedAtoms = set(filter((None).__ne__, flatten(bonds))) for i in set(range(nelems)) - bondedAtoms: addUnbonded(self.window, i, vsShifted[self.freqIndex, index], c) self.atomList[i][1]=self.window.items[-1] # detect mouse clicks in GL window and process them def eventFilter(self, obj, event): if obj == self.window: if event.type() == event.MouseButtonPress: itms = obj.itemsAt((event.pos().x()-2, event.pos().y()-2, 4, 4)) if len(itms): self.highlight(obj, [itms[0]]) elif len(self.atomList) == 0: self.build() # also do the default click action return super(MainWidget, self).eventFilter(obj, event) def ZMatCellClicked(self): idxs = sorted(set(idx.row() for idx in self.ZMatTable.selectedIndexes()), reverse=True) itms = [] if self.highList: highIdx = list(np.array(self.highList).T[0]) for idx in idxs: if self.highList and idx in highIdx: itms.append(self.highList[highIdx.index(idx)][1]) elif len(self.atomList) > idx: itms.append(self.atomList[idx][1]) self.highlight(self.window, itms) def freqCellClicked(self): global vsShifted self.timer = QTimer() self.timer.setInterval(30) self.timer.timeout.connect(self.updateFreq) idxs = [ idx.row() for idx in self.freqTable.selectedIndexes() ] if len(idxs) == 1: self.freqIndex = idxs[0] self.timer.stop() self.timer.timeout.connect(self.updateFreq) try: self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) except TypeError: pass self.timer.start() if len(idxs) != 1: self.timer.stop() self.freqTable.clearSelection() self.timer.timeout.disconnect(self.updateFreq) self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.clearUpdateView() def gausclicked(self, item, point): itemdata = item.scatter.data points = [ row[7] for row in itemdata ] idx = points.index(point[0]) for i in range(3): if i == 0: x = [idx] y = [self.scfenergies[idx]] else: x = [idx, idx] y = [self.geovalues[2*i-2][idx], self.geovalues[2*i-1][idx]] plot = self.gaussianPlot.getItem(1, i+1) plot.removeItem(plot.highlight) plot.highlight=plot.plot(x, y, symbol='o', symbolPen='w', symbolBrush=None, pen=None, symbolSize=15, pxMode=True, antialias=True, autoDownsample=False) self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) self.inp = [] self.populateZMatModel() self.inp = xyz2zmat(self.atomcoords[min(idx, len(self.atomcoords)-1)], self.atomsymbols) self.populateZMatModel() self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.updateView() def highlight(self, obj, itms): for itm in itms: idx = next((i for i, sublist in enumerate(self.atomList) if itm in sublist), -1) #print(idx) if idx != -1: addAtom(obj, idx, r, vs, c, opt='highlight', fast=self.fast) self.highList.append([idx, obj.items[-1]]) self.ZMatTable.selectRow(idx) idx = next((i for i, sublist in enumerate(self.highList) if itm in sublist), -1) if idx != -1: obj.removeItem(self.highList[idx][1]) self.highList.pop(idx) self.ZMatTable.clearSelection() self.statusBar.clearMessage() if len(self.highList) > 0: idxs = np.asarray(self.highList).T[0] selected = [] for i in idxs: selected.append(str(i+1)+str(elements[elems[i]])) self.statusBar.showMessage('Selected atoms: '+str(selected), 5000) def buildB(self): try: nelems except NameError: self.build() else: if len(self.highList) <= min(nelems, 3): diff = min(nelems, 3) - len(self.highList) if diff != 0: self.statusBar.clearMessage() self.statusBar.showMessage('Please select '+str(diff)+' more atom(s).') else: self.build() else: self.statusBar.clearMessage() self.statusBar.showMessage('Too many atoms selected.') def build(self): selection = self.periodicTable() row = self.ZMatModel.rowCount() self.addRow() self.ZMatModel.dataChanged.disconnect(self.clearUpdateView) newSymbol = selection[1] newData = [newSymbol] if len(self.highList) >= 1: newBond = round(2.1*gmean([ elements[e].covalent_radius for e in [selection[0], elems[self.highList[0][0]]] ]), 4) newData.append(self.highList[0][0]+1) newData.append(newBond) if len(self.highList) >= 2: newAngle = 109.4712 newData.append(self.highList[1][0]+1) newData.append(newAngle) if len(self.highList) == 3: newDihedral = 120. newData.append(self.highList[2][0]+1) newData.append(newDihedral) for j, cell in enumerate(newData): item = QStandardItem(str(cell)) self.ZMatModel.setItem(row, j, item) self.highList = [] self.ZMatModel.dataChanged.connect(self.clearUpdateView) self.updateView() def measureDistanceB(self): sel = len(self.highList) if sel <= 2: if sel < 2: self.statusBar.clearMessage() self.statusBar.showMessage('Please select '+str(2-sel)+' more atom(s).') else: self.measureDistance() else: self.statusBar.clearMessage() self.statusBar.showMessage('Too many atoms selected.') def measureDistance(self): pts = [] for pt in self.highList: pts.append(vs[pt[0]]) pts = np.array(pts) self.clearHighlights() line = gl.GLLinePlotItem(pos=pts, color=(0., 1., 0., 1.), width=3) self.window.addItem(line) self.labelList.append(line) q = pts[1]-pts[0] dist = round(np.sqrt(np.dot(q, q)), 4) self.window.labelPos.append(np.mean(pts[0:2], axis=0)) self.window.labelText.append(str(dist)) self.statusBar.clearMessage() self.statusBar.showMessage('Measured distance: '+str(dist)+' A.', 3000) def measureAngleB(self): sel = len(self.highList) if sel <= 3: if sel < 3: self.statusBar.clearMessage() self.statusBar.showMessage('Please select '+str(3-sel)+' more atom(s).') else: self.measureAngle() else: self.statusBar.clearMessage() self.statusBar.showMessage('Too many atoms selected.') def measureAngle(self): pts = [] for pt in self.highList: pts.append(vs[pt[0]]) pts = np.array(pts) q = pts[1]-pts[0] r = pts[2]-pts[0] q_u = q / np.sqrt(np.dot(q, q)) r_u = r / np.sqrt(np.dot(r, r)) angle = round(degrees(acos(np.dot(q_u, r_u))), 1) srange = np.array([slerp(q, r, t) for t in np.arange(0.0, 13/12, 1/12)]) self.clearHighlights() for i in range(12): mesh = gl.MeshData(np.array([[0,0,0],srange[i],srange[i+1]])) tri = gl.GLMeshItem(meshdata=mesh, smooth=False, computeNormals=False, color=(0.3, 1., 0.3, 0.5), glOptions=('translucent')) tri.translate(pts[0][0], pts[0][1], pts[0][2]) self.window.addItem(tri) self.labelList.append(tri) self.window.labelPos.append(slerp(q, r, 0.5)+pts[0]) self.window.labelText.append(str(angle)) self.statusBar.clearMessage() self.statusBar.showMessage('Measured angle: '+str(angle)+'°', 3000) def clearLabels(self): self.window.labelPos = [] self.window.labelText = [] self.labelList = [] self.updateView() def clearHighlights(self): for item in reversed(self.highList): self.window.removeItem(item[1]) self.highList = [] self.updateView() def clearUpdateView(self): self.window.labelPos = [] self.window.labelText = [] self.labelList = [] for item in reversed(self.highList): self.window.removeItem(item[1]) self.highList = [] self.updateView() #print(self.highList) def fastDraw(self): if not self.fast: self.fast = True self.updateView() def normalDraw(self): if self.fast: self.fast = False self.updateView() def about(self): QMessageBox.about(self, 'About moldy', 'moldy beta 15. 9. 2015') def aboutQt(self): QMessageBox.aboutQt(self, 'About Qt') def deleteGLwidget(self): self.window.setParent(None) del self.window
class Preferences(QMainWindow): run_wizard_requested = pyqtSignal() def __init__(self, gui, initial_plugin=None, close_after_initial=False): QMainWindow.__init__(self, gui) self.gui = gui self.must_restart = False self.committed = False self.close_after_initial = close_after_initial self.resize(930, 720) nh, nw = min_available_height()-25, available_width()-10 if nh < 0: nh = 800 if nw < 0: nw = 600 nh = min(self.height(), nh) nw = min(self.width(), nw) self.resize(nw, nh) self.esc_action = QAction(self) self.addAction(self.esc_action) self.esc_action.setShortcut(QKeySequence(Qt.Key_Escape)) self.esc_action.triggered.connect(self.esc) geom = gprefs.get('preferences_window_geometry', None) if geom is not None: self.restoreGeometry(geom) # Center if islinux: self.move(gui.rect().center() - self.rect().center()) self.setWindowModality(Qt.WindowModal) self.setWindowTitle(__appname__ + ' - ' + _('Preferences')) self.setWindowIcon(QIcon(I('config.png'))) self.status_bar = StatusBar(self) self.setStatusBar(self.status_bar) self.stack = QStackedWidget(self) self.cw = QWidget(self) self.cw.setLayout(QVBoxLayout()) self.cw.layout().addWidget(self.stack) self.bb = QDialogButtonBox(QDialogButtonBox.Close) self.wizard_button = self.bb.addButton(_('Run welcome wizard'), self.bb.ActionRole) self.wizard_button.setIcon(QIcon(I('wizard.png'))) self.wizard_button.clicked.connect(self.run_wizard, type=Qt.QueuedConnection) self.cw.layout().addWidget(self.bb) self.bb.button(self.bb.Close).setDefault(True) self.bb.rejected.connect(self.close, type=Qt.QueuedConnection) self.setCentralWidget(self.cw) self.browser = Browser(self) self.browser.show_plugin.connect(self.show_plugin) self.stack.addWidget(self.browser) self.scroll_area = QScrollArea(self) self.stack.addWidget(self.scroll_area) self.scroll_area.setWidgetResizable(True) self.bar = QToolBar(self) self.addToolBar(self.bar) self.bar.setVisible(False) self.bar.setIconSize(QSize(ICON_SIZE, ICON_SIZE)) self.bar.setMovable(False) self.bar.setFloatable(False) self.bar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self.apply_action = self.bar.addAction(QIcon(I('ok.png')), _('&Apply'), self.commit) self.cancel_action = self.bar.addAction(QIcon(I('window-close.png')), _('&Cancel'), self.cancel) self.bar_title = BarTitle(self.bar) self.bar.addWidget(self.bar_title) self.restore_action = self.bar.addAction(QIcon(I('clear_left.png')), _('Restore &defaults'), self.restore_defaults) for ac, tt in [('apply', _('Save changes')), ('cancel', _('Cancel and return to overview'))]: ac = getattr(self, ac+'_action') ac.setToolTip(tt) ac.setWhatsThis(tt) ac.setStatusTip(tt) for ch in self.bar.children(): if isinstance(ch, QToolButton): ch.setCursor(Qt.PointingHandCursor) ch.setAutoRaise(True) self.stack.setCurrentIndex(0) if initial_plugin is not None: category, name = initial_plugin plugin = get_plugin(category, name) if plugin is not None: self.show_plugin(plugin) def run_wizard(self): self.close() self.run_wizard_requested.emit() def set_tooltips_for_labels(self): def process_child(child): for g in child.children(): if isinstance(g, QLabel): buddy = g.buddy() if buddy is not None and hasattr(buddy, 'toolTip'): htext = unicode(buddy.toolTip()).strip() etext = unicode(g.toolTip()).strip() if htext and not etext: g.setToolTip(htext) g.setWhatsThis(htext) else: process_child(g) process_child(self.showing_widget) def show_plugin(self, plugin): self.showing_widget = plugin.create_widget(self.scroll_area) self.showing_widget.genesis(self.gui) self.showing_widget.initialize() self.set_tooltips_for_labels() self.scroll_area.setWidget(self.showing_widget) self.stack.setCurrentIndex(1) self.showing_widget.show() self.setWindowTitle(__appname__ + ' - ' + _('Preferences') + ' - ' + plugin.gui_name) self.apply_action.setEnabled(False) self.showing_widget.changed_signal.connect(lambda : self.apply_action.setEnabled(True)) self.restore_action.setEnabled(self.showing_widget.supports_restoring_to_defaults) tt = self.showing_widget.restore_defaults_desc if not self.restore_action.isEnabled(): tt = _('Restoring to defaults not supported for') + ' ' + \ plugin.gui_name self.restore_action.setToolTip(textwrap.fill(tt)) self.restore_action.setWhatsThis(textwrap.fill(tt)) self.restore_action.setStatusTip(tt) self.bar_title.show_plugin(plugin) self.setWindowIcon(QIcon(plugin.icon)) self.bar.setVisible(True) self.bb.setVisible(False) def hide_plugin(self): self.showing_widget = QWidget(self.scroll_area) self.scroll_area.setWidget(self.showing_widget) self.setWindowTitle(__appname__ + ' - ' + _('Preferences')) self.bar.setVisible(False) self.stack.setCurrentIndex(0) self.setWindowIcon(QIcon(I('config.png'))) self.bb.setVisible(True) def esc(self, *args): if self.stack.currentIndex() == 1: self.cancel() elif self.stack.currentIndex() == 0: self.close() def commit(self, *args): try: must_restart = self.showing_widget.commit() except AbortCommit: return rc = self.showing_widget.restart_critical self.committed = True do_restart = False if must_restart: self.must_restart = True msg = _('Some of the changes you made require a restart.' ' Please restart calibre as soon as possible.') if rc: msg = _('The changes you have made require calibre be ' 'restarted immediately. You will not be allowed to ' 'set any more preferences, until you restart.') d = warning_dialog(self, _('Restart needed'), msg, show_copy_button=False) b = d.bb.addButton(_('Restart calibre now'), d.bb.AcceptRole) b.setIcon(QIcon(I('lt.png'))) d.do_restart = False def rf(): d.do_restart = True b.clicked.connect(rf) d.set_details('') d.exec_() b.clicked.disconnect() do_restart = d.do_restart self.showing_widget.refresh_gui(self.gui) self.hide_plugin() if self.close_after_initial or (must_restart and rc) or do_restart: self.close() if do_restart: self.gui.quit(restart=True) def cancel(self, *args): if self.close_after_initial: self.close() else: self.hide_plugin() def restore_defaults(self, *args): self.showing_widget.restore_defaults() def closeEvent(self, *args): gprefs.set('preferences_window_geometry', bytearray(self.saveGeometry())) if self.committed: self.gui.must_restart_before_config = self.must_restart self.gui.tags_view.recount() self.gui.create_device_menu() self.gui.set_device_menu_items_state(bool(self.gui.device_connected)) self.gui.bars_manager.apply_settings() self.gui.bars_manager.update_bars() self.gui.build_context_menus() return QMainWindow.closeEvent(self, *args)
class Splitter(QSplitter): state_changed = pyqtSignal(object) def __init__(self, name, label, icon, initial_show=True, initial_side_size=120, connect_button=True, orientation=Qt.Horizontal, side_index=0, parent=None, shortcut=None): QSplitter.__init__(self, parent) self.resize_timer = QTimer(self) self.resize_timer.setSingleShot(True) self.desired_side_size = initial_side_size self.desired_show = initial_show self.resize_timer.setInterval(5) self.resize_timer.timeout.connect(self.do_resize) self.setOrientation(orientation) self.side_index = side_index self._name = name self.label = label self.initial_side_size = initial_side_size self.initial_show = initial_show self.splitterMoved.connect(self.splitter_moved, type=Qt.QueuedConnection) self.button = LayoutButton(icon, label, self, shortcut=shortcut) if connect_button: self.button.clicked.connect(self.double_clicked) if shortcut is not None: self.action_toggle = QAction(QIcon(icon), _('Toggle') + ' ' + label, self) self.action_toggle.triggered.connect(self.toggle_triggered) if parent is not None: parent.addAction(self.action_toggle) if hasattr(parent, 'keyboard'): parent.keyboard.register_shortcut('splitter %s %s'%(name, label), unicode(self.action_toggle.text()), default_keys=(shortcut,), action=self.action_toggle) else: self.action_toggle.setShortcut(shortcut) else: self.action_toggle.setShortcut(shortcut) def toggle_triggered(self, *args): self.toggle_side_pane() def createHandle(self): return SplitterHandle(self.orientation(), self) def initialize(self): for i in range(self.count()): h = self.handle(i) if h is not None: h.splitter_moved() self.state_changed.emit(not self.is_side_index_hidden) def splitter_moved(self, *args): self.desired_side_size = self.side_index_size self.state_changed.emit(not self.is_side_index_hidden) @property def is_side_index_hidden(self): sizes = list(self.sizes()) try: return sizes[self.side_index] == 0 except IndexError: return True @property def save_name(self): ori = 'horizontal' if self.orientation() == Qt.Horizontal \ else 'vertical' return self._name + '_' + ori def print_sizes(self): if self.count() > 1: print self.save_name, 'side:', self.side_index_size, 'other:', print list(self.sizes())[self.other_index] @dynamic_property def side_index_size(self): def fget(self): if self.count() < 2: return 0 return self.sizes()[self.side_index] def fset(self, val): if self.count() < 2: return if val == 0 and not self.is_side_index_hidden: self.save_state() sizes = list(self.sizes()) for i in range(len(sizes)): sizes[i] = val if i == self.side_index else 10 self.setSizes(sizes) total = sum(self.sizes()) sizes = list(self.sizes()) for i in range(len(sizes)): sizes[i] = val if i == self.side_index else total-val self.setSizes(sizes) self.initialize() return property(fget=fget, fset=fset) def do_resize(self, *args): orig = self.desired_side_size QSplitter.resizeEvent(self, self._resize_ev) if orig > 20 and self.desired_show: c = 0 while abs(self.side_index_size - orig) > 10 and c < 5: self.apply_state(self.get_state(), save_desired=False) c += 1 def resizeEvent(self, ev): if self.resize_timer.isActive(): self.resize_timer.stop() self._resize_ev = ev self.resize_timer.start() def get_state(self): if self.count() < 2: return (False, 200) return (self.desired_show, self.desired_side_size) def apply_state(self, state, save_desired=True): if state[0]: self.side_index_size = state[1] if save_desired: self.desired_side_size = self.side_index_size else: self.side_index_size = 0 self.desired_show = state[0] def default_state(self): return (self.initial_show, self.initial_side_size) # Public API {{{ def update_desired_state(self): self.desired_show = not self.is_side_index_hidden def save_state(self): if self.count() > 1: gprefs[self.save_name+'_state'] = self.get_state() @property def other_index(self): return (self.side_index+1)%2 def restore_state(self): if self.count() > 1: state = gprefs.get(self.save_name+'_state', self.default_state()) self.apply_state(state, save_desired=False) self.desired_side_size = state[1] def toggle_side_pane(self, hide=None): if hide is None: action = 'show' if self.is_side_index_hidden else 'hide' else: action = 'hide' if hide else 'show' getattr(self, action+'_side_pane')() def show_side_pane(self): if self.count() < 2 or not self.is_side_index_hidden: return if self.desired_side_size == 0: self.desired_side_size = self.initial_side_size self.apply_state((True, self.desired_side_size)) def hide_side_pane(self): if self.count() < 2 or self.is_side_index_hidden: return self.apply_state((False, self.desired_side_size)) def double_clicked(self, *args): self.toggle_side_pane()
class JobsButton(QFrame): # {{{ def __init__(self, horizontal=False, size=48, parent=None): QFrame.__init__(self, parent) if horizontal: size = 24 self.pi = ProgressIndicator(self, size) self._jobs = QLabel('<b>'+_('Jobs:')+' 0') self._jobs.mouseReleaseEvent = self.mouseReleaseEvent self.shortcut = _('Shift+Alt+J') if horizontal: self.setLayout(QHBoxLayout()) self.layout().setDirection(self.layout().RightToLeft) else: self.setLayout(QVBoxLayout()) self._jobs.setAlignment(Qt.AlignHCenter|Qt.AlignBottom) self.layout().addWidget(self.pi) self.layout().addWidget(self._jobs) if not horizontal: self.layout().setAlignment(self._jobs, Qt.AlignHCenter) self._jobs.setMargin(0) self.layout().setMargin(0) self._jobs.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setCursor(Qt.PointingHandCursor) b = _('Click to see list of jobs') self.setToolTip(b + u' (%s)'%self.shortcut) self.action_toggle = QAction(b, parent) parent.addAction(self.action_toggle) self.action_toggle.setShortcut(self.shortcut) self.action_toggle.triggered.connect(self.toggle) def initialize(self, jobs_dialog, job_manager): self.jobs_dialog = jobs_dialog job_manager.job_added.connect(self.job_added) job_manager.job_done.connect(self.job_done) self.jobs_dialog.addAction(self.action_toggle) def mouseReleaseEvent(self, event): self.toggle() def toggle(self, *args): if self.jobs_dialog.isVisible(): self.jobs_dialog.hide() else: self.jobs_dialog.show() @property def is_running(self): return self.pi.isAnimated() def start(self): self.pi.startAnimation() def stop(self): self.pi.stopAnimation() def jobs(self): src = unicode(self._jobs.text()) return int(re.search(r'\d+', src).group()) def job_added(self, nnum): jobs = self._jobs src = unicode(jobs.text()) num = self.jobs() text = src.replace(str(num), str(nnum)) jobs.setText(text) self.start() def job_done(self, nnum): jobs = self._jobs src = unicode(jobs.text()) num = self.jobs() text = src.replace(str(num), str(nnum)) jobs.setText(text) if nnum == 0: self.no_more_jobs() def no_more_jobs(self): if self.is_running: self.stop() QCoreApplication.instance().alert(self, 5000)
class DocumentView(QWebView): # {{{ magnification_changed = pyqtSignal(object) DISABLED_BRUSH = QBrush(Qt.lightGray, Qt.Dense5Pattern) def initialize_view(self, debug_javascript=False): self.setRenderHints(QPainter.Antialiasing|QPainter.TextAntialiasing|QPainter.SmoothPixmapTransform) self.flipper = SlideFlip(self) self.is_auto_repeat_event = False self.debug_javascript = debug_javascript self.shortcuts = Shortcuts(SHORTCUTS, 'shortcuts/viewer') self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self._size_hint = QSize(510, 680) self.initial_pos = 0.0 self.to_bottom = False self.document = Document(self.shortcuts, parent=self, debug_javascript=debug_javascript) self.setPage(self.document) self.manager = None self._reference_mode = False self._ignore_scrollbar_signals = False self.loading_url = None self.loadFinished.connect(self.load_finished) self.connect(self.document, SIGNAL('linkClicked(QUrl)'), self.link_clicked) self.connect(self.document, SIGNAL('linkHovered(QString,QString,QString)'), self.link_hovered) self.connect(self.document, SIGNAL('selectionChanged()'), self.selection_changed) self.connect(self.document, SIGNAL('animated_scroll_done()'), self.animated_scroll_done, Qt.QueuedConnection) self.document.page_turn.connect(self.page_turn_requested) copy_action = self.pageAction(self.document.Copy) copy_action.setIcon(QIcon(I('convert.png'))) d = self.document self.unimplemented_actions = list(map(self.pageAction, [d.DownloadImageToDisk, d.OpenLinkInNewWindow, d.DownloadLinkToDisk, d.OpenImageInNewWindow, d.OpenLink, d.Reload])) self.dictionary_action = QAction(QIcon(I('dictionary.png')), _('&Lookup in dictionary'), self) self.dictionary_action.setShortcut(Qt.CTRL+Qt.Key_L) self.dictionary_action.triggered.connect(self.lookup) self.addAction(self.dictionary_action) self.image_popup = ImagePopup(self) self.view_image_action = QAction(_('View &image...'), self) self.view_image_action.triggered.connect(self.image_popup) self.search_action = QAction(QIcon(I('dictionary.png')), _('&Search for next occurrence'), self) self.search_action.setShortcut(Qt.CTRL+Qt.Key_S) self.search_action.triggered.connect(self.search_next) self.addAction(self.search_action) self.goto_location_action = QAction(_('Go to...'), self) self.goto_location_menu = m = QMenu(self) self.goto_location_actions = a = { 'Next Page': self.next_page, 'Previous Page': self.previous_page, 'Section Top' : partial(self.scroll_to, 0), 'Document Top': self.goto_document_start, 'Section Bottom':partial(self.scroll_to, 1), 'Document Bottom': self.goto_document_end, 'Next Section': self.goto_next_section, 'Previous Section': self.goto_previous_section, } for name, key in [(_('Next Section'), 'Next Section'), (_('Previous Section'), 'Previous Section'), (None, None), (_('Document Start'), 'Document Top'), (_('Document End'), 'Document Bottom'), (None, None), (_('Section Start'), 'Section Top'), (_('Section End'), 'Section Bottom'), (None, None), (_('Next Page'), 'Next Page'), (_('Previous Page'), 'Previous Page')]: if key is None: m.addSeparator() else: m.addAction(name, a[key], self.shortcuts.get_sequences(key)[0]) self.goto_location_action.setMenu(self.goto_location_menu) self.grabGesture(Qt.SwipeGesture) def goto_next_section(self, *args): if self.manager is not None: self.manager.goto_next_section() def goto_previous_section(self, *args): if self.manager is not None: self.manager.goto_previous_section() def goto_document_start(self, *args): if self.manager is not None: self.manager.goto_start() def goto_document_end(self, *args): if self.manager is not None: self.manager.goto_end() @property def copy_action(self): return self.pageAction(self.document.Copy) def animated_scroll_done(self): if self.manager is not None: self.manager.scrolled(self.document.scroll_fraction) def reference_mode(self, enable): self._reference_mode = enable self.document.reference_mode(enable) def goto(self, ref): self.document.goto(ref) def goto_bookmark(self, bm): self.document.goto_bookmark(bm) def config(self, parent=None): self.document.do_config(parent) if self.document.in_fullscreen_mode: self.document.switch_to_fullscreen_mode() self.setFocus(Qt.OtherFocusReason) def load_theme(self, theme_id): themes = load_themes() theme = themes[theme_id] opts = config(theme).parse() self.document.apply_settings(opts) if self.document.in_fullscreen_mode: self.document.switch_to_fullscreen_mode() self.setFocus(Qt.OtherFocusReason) def bookmark(self): return self.document.bookmark() def selection_changed(self): if self.manager is not None: self.manager.selection_changed(unicode(self.document.selectedText())) def contextMenuEvent(self, ev): mf = self.document.mainFrame() r = mf.hitTestContent(ev.pos()) img = r.pixmap() self.image_popup.current_img = img self.image_popup.current_url = r.imageUrl() menu = self.document.createStandardContextMenu() for action in self.unimplemented_actions: menu.removeAction(action) text = unicode(self.selectedText()) if text: menu.insertAction(list(menu.actions())[0], self.dictionary_action) menu.insertAction(list(menu.actions())[0], self.search_action) if not img.isNull(): menu.addAction(self.view_image_action) menu.addSeparator() menu.addAction(self.goto_location_action) if self.document.in_fullscreen_mode and self.manager is not None: menu.addSeparator() menu.addAction(self.manager.toggle_toolbar_action) menu.exec_(ev.globalPos()) def lookup(self, *args): if self.manager is not None: t = unicode(self.selectedText()).strip() if t: self.manager.lookup(t.split()[0]) def search_next(self): if self.manager is not None: t = unicode(self.selectedText()).strip() if t: self.manager.search.set_search_string(t) def set_manager(self, manager): self.manager = manager self.scrollbar = manager.horizontal_scrollbar self.connect(self.scrollbar, SIGNAL('valueChanged(int)'), self.scroll_horizontally) def scroll_horizontally(self, amount): self.document.scroll_to(y=self.document.ypos, x=amount) @property def scroll_pos(self): return (self.document.ypos, self.document.ypos + self.document.window_height) @property def viewport_rect(self): # (left, top, right, bottom) of the viewport in document co-ordinates # When in paged mode, left and right are the numbers of the columns # at the left edge and *after* the right edge of the viewport d = self.document if d.in_paged_mode: try: l, r = d.column_boundaries except ValueError: l, r = (0, 1) else: l, r = d.xpos, d.xpos + d.window_width return (l, d.ypos, r, d.ypos + d.window_height) def link_hovered(self, link, text, context): link, text = unicode(link), unicode(text) if link: self.setCursor(Qt.PointingHandCursor) else: self.unsetCursor() def link_clicked(self, url): if self.manager is not None: self.manager.link_clicked(url) def sizeHint(self): return self._size_hint @dynamic_property def scroll_fraction(self): def fget(self): return self.document.scroll_fraction def fset(self, val): self.document.scroll_fraction = float(val) return property(fget=fget, fset=fset) @property def hscroll_fraction(self): return self.document.hscroll_fraction @property def content_size(self): return self.document.width, self.document.height @dynamic_property def current_language(self): def fget(self): return self.document.current_language def fset(self, val): self.document.current_language = val return property(fget=fget, fset=fset) def search(self, text, backwards=False): flags = self.document.FindBackward if backwards else self.document.FindFlags(0) found = self.findText(text, flags) if found and self.document.in_paged_mode: self.document.javascript('paged_display.snap_to_selection()') return found def path(self): return os.path.abspath(unicode(self.url().toLocalFile())) def load_path(self, path, pos=0.0): self.initial_pos = pos self.last_loaded_path = path def callback(lu): self.loading_url = lu if self.manager is not None: self.manager.load_started() load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path, 'mime_type', 'text/html'), pre_load_callback=callback) entries = set() for ie in getattr(path, 'index_entries', []): if ie.start_anchor: entries.add(ie.start_anchor) if ie.end_anchor: entries.add(ie.end_anchor) self.document.index_anchors = entries def initialize_scrollbar(self): if getattr(self, 'scrollbar', None) is not None: if self.document.in_paged_mode: self.scrollbar.setVisible(False) return delta = self.document.width - self.size().width() if delta > 0: self._ignore_scrollbar_signals = True self.scrollbar.blockSignals(True) self.scrollbar.setRange(0, delta) self.scrollbar.setValue(0) self.scrollbar.setSingleStep(1) self.scrollbar.setPageStep(int(delta/10.)) self.scrollbar.setVisible(delta > 0) self.scrollbar.blockSignals(False) self._ignore_scrollbar_signals = False def load_finished(self, ok): if self.loading_url is None: # An <iframe> finished loading return self.loading_url = None self.document.load_javascript_libraries() self.document.after_load() self._size_hint = self.document.mainFrame().contentsSize() scrolled = False if self.to_bottom: self.to_bottom = False self.initial_pos = 1.0 if self.initial_pos > 0.0: scrolled = True self.scroll_to(self.initial_pos, notify=False) self.initial_pos = 0.0 self.update() self.initialize_scrollbar() self.document.reference_mode(self._reference_mode) if self.manager is not None: spine_index = self.manager.load_finished(bool(ok)) if spine_index > -1: self.document.set_reference_prefix('%d.'%(spine_index+1)) if scrolled: self.manager.scrolled(self.document.scroll_fraction, onload=True) if self.flipper.isVisible(): if self.flipper.running: self.flipper.setVisible(False) else: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) @classmethod def test_line(cls, img, y): 'Test if line contains pixels of exactly the same color' start = img.pixel(0, y) for i in range(1, img.width()): if img.pixel(i, y) != start: return False return True def current_page_image(self, overlap=-1): if overlap < 0: overlap = self.height() img = QImage(self.width(), overlap, QImage.Format_ARGB32_Premultiplied) painter = QPainter(img) painter.setRenderHints(self.renderHints()) self.document.mainFrame().render(painter, QRegion(0, 0, self.width(), overlap)) painter.end() return img def find_next_blank_line(self, overlap): img = self.current_page_image(overlap) for i in range(overlap-1, -1, -1): if self.test_line(img, i): self.scroll_by(y=i, notify=False) return self.scroll_by(y=overlap) def previous_page(self): if self.flipper.running and not self.is_auto_repeat_event: return if self.loading_url is not None: return epf = self.document.enable_page_flip and not self.is_auto_repeat_event if self.document.in_paged_mode: loc = self.document.javascript( 'paged_display.previous_screen_location()', typ='int') if loc < 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.manager.previous_document() else: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.document.scroll_to(x=loc, y=0) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return delta_y = self.document.window_height - 25 if self.document.at_top: if self.manager is not None: self.to_bottom = True if epf: self.flipper.initialize(self.current_page_image(), False) self.manager.previous_document() else: opos = self.document.ypos upper_limit = opos - delta_y if upper_limit < 0: upper_limit = 0 if upper_limit < opos: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.document.scroll_to(self.document.xpos, upper_limit) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) def next_page(self): if self.flipper.running and not self.is_auto_repeat_event: return if self.loading_url is not None: return epf = self.document.enable_page_flip and not self.is_auto_repeat_event if self.document.in_paged_mode: loc = self.document.javascript( 'paged_display.next_screen_location()', typ='int') if loc < 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() else: if epf: self.flipper.initialize(self.current_page_image()) self.document.scroll_to(x=loc, y=0) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return window_height = self.document.window_height document_height = self.document.height ddelta = document_height - window_height #print '\nWindow height:', window_height #print 'Document height:', self.document.height delta_y = window_height - 25 if self.document.at_bottom or ddelta <= 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() elif ddelta < 25: self.scroll_by(y=ddelta) return else: oopos = self.document.ypos #print 'Original position:', oopos self.document.set_bottom_padding(0) opos = self.document.ypos #print 'After set padding=0:', self.document.ypos if opos < oopos: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() return #oheight = self.document.height lower_limit = opos + delta_y # Max value of top y co-ord after scrolling max_y = self.document.height - window_height # The maximum possible top y co-ord if max_y < lower_limit: padding = lower_limit - max_y if padding == window_height: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() return #print 'Setting padding to:', lower_limit - max_y self.document.set_bottom_padding(lower_limit - max_y) if epf: self.flipper.initialize(self.current_page_image()) #print 'Document height:', self.document.height #print 'Height change:', (self.document.height - oheight) max_y = self.document.height - window_height lower_limit = min(max_y, lower_limit) #print 'Scroll to:', lower_limit if lower_limit > opos: self.document.scroll_to(self.document.xpos, lower_limit) actually_scrolled = self.document.ypos - opos #print 'After scroll pos:', self.document.ypos #print 'Scrolled by:', self.document.ypos - opos self.find_next_blank_line(window_height - actually_scrolled) #print 'After blank line pos:', self.document.ypos if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) #print 'After all:', self.document.ypos def page_turn_requested(self, backwards): if backwards: self.previous_page() else: self.next_page() def scroll_by(self, x=0, y=0, notify=True): old_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) self.document.scroll_by(x, y) new_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if notify and self.manager is not None and new_pos != old_pos: self.manager.scrolled(self.scroll_fraction) def scroll_to(self, pos, notify=True): if self._ignore_scrollbar_signals: return old_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if self.document.in_paged_mode: if isinstance(pos, basestring): self.document.jump_to_anchor(pos) else: self.document.scroll_fraction = pos else: if isinstance(pos, basestring): self.document.jump_to_anchor(pos) else: if pos >= 1: self.document.scroll_to(0, self.document.height) else: y = int(math.ceil( pos*(self.document.height-self.document.window_height))) self.document.scroll_to(0, y) new_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if notify and self.manager is not None and new_pos != old_pos: self.manager.scrolled(self.scroll_fraction) @dynamic_property def multiplier(self): def fget(self): return self.zoomFactor() def fset(self, val): self.setZoomFactor(val) self.magnification_changed.emit(val) return property(fget=fget, fset=fset) def magnify_fonts(self, amount=None): if amount is None: amount = self.document.font_magnification_step with self.document.page_position: self.multiplier += amount return self.document.scroll_fraction def shrink_fonts(self, amount=None): if amount is None: amount = self.document.font_magnification_step if self.multiplier >= amount: with self.document.page_position: self.multiplier -= amount return self.document.scroll_fraction def changeEvent(self, event): if event.type() == event.EnabledChange: self.update() return QWebView.changeEvent(self, event) def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(self.renderHints()) self.document.mainFrame().render(painter, event.region()) if not self.isEnabled(): painter.fillRect(event.region().boundingRect(), self.DISABLED_BRUSH) painter.end() def wheelEvent(self, event): mods = event.modifiers() if mods & Qt.CTRL: if self.manager is not None and event.delta() != 0: (self.manager.font_size_larger if event.delta() > 0 else self.manager.font_size_smaller)() return if self.document.in_paged_mode: if abs(event.delta()) < 15: return typ = 'screen' if self.document.wheel_flips_pages else 'col' direction = 'next' if event.delta() < 0 else 'previous' loc = self.document.javascript('paged_display.%s_%s_location()'%( direction, typ), typ='int') if loc > -1: self.document.scroll_to(x=loc, y=0) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) event.accept() elif self.manager is not None: if direction == 'next': self.manager.next_document() else: self.manager.previous_document() event.accept() return if event.delta() < -14: if self.document.wheel_flips_pages: self.next_page() event.accept() return if self.document.at_bottom: self.scroll_by(y=15) # at_bottom can lie on windows if self.manager is not None: self.manager.next_document() event.accept() return elif event.delta() > 14: if self.document.wheel_flips_pages: self.previous_page() event.accept() return if self.document.at_top: if self.manager is not None: self.manager.previous_document() event.accept() return ret = QWebView.wheelEvent(self, event) scroll_amount = (event.delta() / 120.0) * .2 * -1 if event.orientation() == Qt.Vertical: self.scroll_by(0, self.document.viewportSize().height() * scroll_amount) else: self.scroll_by(self.document.viewportSize().width() * scroll_amount, 0) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return ret def keyPressEvent(self, event): if not self.handle_key_press(event): return QWebView.keyPressEvent(self, event) def paged_col_scroll(self, forward=True, scroll_past_end=True): dir = 'next' if forward else 'previous' loc = self.document.javascript( 'paged_display.%s_col_location()'%dir, typ='int') if loc > -1: self.document.scroll_to(x=loc, y=0) self.manager.scrolled(self.document.scroll_fraction) elif scroll_past_end: (self.manager.next_document() if forward else self.manager.previous_document()) def handle_key_press(self, event): handled = True key = self.shortcuts.get_match(event) func = self.goto_location_actions.get(key, None) if func is not None: self.is_auto_repeat_event = event.isAutoRepeat() try: func() finally: self.is_auto_repeat_event = False elif key == 'Down': if self.document.in_paged_mode: self.paged_col_scroll(scroll_past_end=not self.document.line_scrolling_stops_on_pagebreaks) else: if (not self.document.line_scrolling_stops_on_pagebreaks and self.document.at_bottom): self.manager.next_document() else: self.scroll_by(y=15) elif key == 'Up': if self.document.in_paged_mode: self.paged_col_scroll(forward=False, scroll_past_end=not self.document.line_scrolling_stops_on_pagebreaks) else: if (not self.document.line_scrolling_stops_on_pagebreaks and self.document.at_top): self.manager.previous_document() else: self.scroll_by(y=-15) elif key == 'Left': if self.document.in_paged_mode: self.paged_col_scroll(forward=False) else: self.scroll_by(x=-15) elif key == 'Right': if self.document.in_paged_mode: self.paged_col_scroll() else: self.scroll_by(x=15) else: handled = False return handled def resizeEvent(self, event): if self.manager is not None: self.manager.viewport_resize_started(event) return QWebView.resizeEvent(self, event) def event(self, ev): if ev.type() == ev.Gesture: swipe = ev.gesture(Qt.SwipeGesture) if swipe is not None: self.handle_swipe(swipe) return True return QWebView.event(self, ev) def handle_swipe(self, swipe): if swipe.state() == Qt.GestureFinished: if swipe.horizontalDirection() == QSwipeGesture.Left: self.previous_page() elif swipe.horizontalDirection() == QSwipeGesture.Right: self.next_page() elif swipe.verticalDirection() == QSwipeGesture.Up: self.goto_previous_section() elif swipe.horizontalDirection() == QSwipeGesture.Down: self.goto_next_section() def mouseReleaseEvent(self, ev): opos = self.document.ypos ret = QWebView.mouseReleaseEvent(self, ev) if self.manager is not None and opos != self.document.ypos: self.manager.internal_link_clicked(opos) self.manager.scrolled(self.scroll_fraction) return ret
class GEditorMenuBar(QMenuBar): def __init__(self, parent): QMenuBar.__init__(self, parent) # Menu items self._fileM = QMenu(self.tr("File")) self._editM = QMenu(self.tr("Edit")) self._viewM = QMenu(self.tr("View")) self._optionM = QMenu(self.tr("Options")) self.addMenu(self._fileM) self.addMenu(self._editM) self.addMenu(self._viewM) self.addMenu(self._optionM) # Actions self._connectDatabaseA = QAction(self.tr("Connect a PostGIS database"), self.parent()) self._connectDatabaseA.setShortcut(QKeySequence("Ctrl+Shift+C")) self.parent().connect(self._connectDatabaseA, SIGNAL("triggered()"), self.parent().dialConnectDatabase) self._importDatabaseDataA = QAction( self.tr("Import data from database"), self.parent()) self._importDatabaseDataA.setShortcut(QKeySequence("Ctrl+Shift+I")) self.parent().connect(self._importDatabaseDataA, SIGNAL("triggered()"), self.parent().dialImportDatabaseData) self._importVectorFileDataA = QAction( self.tr("Import vector from file"), self.parent()) self._importVectorFileDataA.setShortcut(QKeySequence("Ctrl+Shift+V")) self.parent().connect(self._importVectorFileDataA, SIGNAL("triggered()"), self.parent().dialImportVectorFileData) self._importRasterFileDataA = QAction( self.tr("Import raster data from file"), self.parent()) self._importRasterFileDataA.setShortcut(QKeySequence("Ctrl+Shift+R")) self.parent().connect(self._importRasterFileDataA, SIGNAL("triggered()"), self.parent().dialImportRasterFileData) self._colorLayerA = QAction(self.tr("Select layer color"), self.parent()) self.parent().connect(self._colorLayerA, SIGNAL("triggered()"), self.parent().dialSelectLayerColor) # Add actions to menu self._fileM.addAction(self._importVectorFileDataA) self._fileM.addAction(self._importRasterFileDataA) self._fileM.addSeparator() self._fileM.addAction(self._connectDatabaseA) self._fileM.addAction(self._importDatabaseDataA) self._viewM.addAction(self._colorLayerA) def dial(self): dial = QDialog(self) dial.setWindowTitle("Select a layer") layerList = QListWidget(dial) db = GPostGISDatabase() for t in db.tables(): layerList.addItem(t) dial.setBaseSize(layerList.minimumSize()) dial.show()
class EditorBar(QToolBar): saveDocAsSignal = pyqtSignal() spellSignal = pyqtSignal(bool) whiteSpaceSignal = pyqtSignal(bool) boldSignal = pyqtSignal(bool) italicSignal = pyqtSignal(bool) underlineSignal = pyqtSignal(bool) strikethroughSignal = pyqtSignal(bool) subscriptSignal = pyqtSignal(bool) superscriptSignal = pyqtSignal(bool) def __init__(self, parent = None): QtGui.QToolBar.__init__(self, parent) self.setWindowTitle('EditorBar') self.setIconSize(QSize(16, 16)) self.createActions() def createActions(self): self.settingsAction = QAction(self.tr("Settings"), self) self.settingsAction.setIcon(QtGui.QIcon(":/icons/icons/configure.png")) self.settingsAction.triggered.connect(self.settings) self.addAction(self.settingsAction) self.saveDocAsAction = QAction(self.tr("Save As"), self) self.saveDocAsAction.triggered.connect(self.SaveDocumentAs) self.saveDocAsAction.setIcon(QtGui.QIcon(":/icons/icons/filesave.png")) self.addAction(self.saveDocAsAction) self.spellAction = QAction(self.tr("Spellchecking"), self) self.spellAction.setIcon( QtGui.QIcon(":/icons/icons/tools-check-spelling.png")) self.spellAction.setCheckable(True) self.spellAction.setChecked(settings.get('editor:spell')) self.spellAction.toggled.connect(self.spell) self.insertSeparator(self.spellAction) self.addAction(self.spellAction) self.whiteSpaceAction = QAction(self.tr("Show whitespace"), self) self.whiteSpaceAction.setIcon( QtGui.QIcon(":/icons/icons/whiteSpace.png")) self.whiteSpaceAction.setCheckable(True) self.whiteSpaceAction.setChecked(settings.get('editor:whiteSpace')) self.whiteSpaceAction.toggled.connect(self.whiteSpace) self.addAction(self.whiteSpaceAction) self.BoldAction = QtGui.QAction( QtGui.QIcon(":/icons/icons/format-text-bold.png"), self.tr("&Bold"), self, shortcut=QtCore.Qt.CTRL + QtCore.Qt.Key_B, triggered=self.bold, checkable=True) self.addAction(self.BoldAction) self.ItalicAction = QAction(self.tr("Italic"), self) self.ItalicAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-italic.png")) self.ItalicAction.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_I) self.ItalicAction.setCheckable(True) self.ItalicAction.triggered.connect(self.italic) self.addAction(self.ItalicAction) self.UnderlineAction = QAction(self.tr("Underline"), self) self.UnderlineAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-underline.png")) self.UnderlineAction.setCheckable(True) self.UnderlineAction.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_U) self.UnderlineAction.triggered.connect(self.underline) self.addAction(self.UnderlineAction) self.StrikethroughAction = QAction(self.tr("Strikethrough"), self) self.StrikethroughAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-strikethrough.png")) self.StrikethroughAction.setCheckable(True) self.StrikethroughAction.triggered.connect(self.strikethrough) self.addAction(self.StrikethroughAction) self.SubscriptAction = QAction(self.tr("Subscript"), self) self.SubscriptAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-subscript.png")) self.SubscriptAction.setCheckable(True) self.SubscriptAction.triggered.connect(self.subscript) self.addAction(self.SubscriptAction) self.SuperscriptAction = QAction(self.tr("Superscript"), self) self.SuperscriptAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-superscript.png")) self.SuperscriptAction.setCheckable(True) self.SuperscriptAction.triggered.connect(self.superscript) self.addAction(self.SuperscriptAction) def settings(self): lectorSettings = Settings(self, 1) # QObject.connect(lectorSettings, SIGNAL('accepted()'), # self.updateTextEditor) lectorSettings.settingAccepted.connect(self.resetSpell) lectorSettings.show() def SaveDocumentAs(self): self.saveDocAsSignal.emit() def spell(self): state = self.spellAction.isChecked() self.spellSignal.emit(state) def resetSpell(self): ''' Turn off and on spellcheckig to use correct dictionary ''' state = self.spellAction.isChecked() if state: self.spellSignal.emit(False) self.spellSignal.emit(state) def whiteSpace(self): state = self.whiteSpaceAction.isChecked() self.whiteSpaceSignal.emit(state) def toggleFormat(self, CharFormat): font = CharFormat.font() self.BoldAction.setChecked(font.bold()) self.ItalicAction.setChecked(font.italic()) self.UnderlineAction.setChecked(font.underline()) self.StrikethroughAction.setChecked(CharFormat.fontStrikeOut()) if CharFormat.verticalAlignment() == \ QtGui.QTextCharFormat.AlignSuperScript: self.SubscriptAction.setChecked(False) self.SuperscriptAction.setChecked(True) elif CharFormat.verticalAlignment() == \ QtGui.QTextCharFormat.AlignSubScript: self.SubscriptAction.setChecked(True) self.SuperscriptAction.setChecked(False) else: self.SubscriptAction.setChecked(False) self.SuperscriptAction.setChecked(False) def bold(self): state = self.BoldAction.isChecked() self.boldSignal.emit(state) def italic(self): state = self.ItalicAction.isChecked() self.italicSignal.emit(state) def underline(self): state = self.UnderlineAction.isChecked() self.underlineSignal.emit(state) def strikethrough(self): state = self.StrikethroughAction.isChecked() self.strikethroughSignal.emit(state) def subscript(self): state = self.SubscriptAction.isChecked() self.subscriptSignal.emit(state) def superscript(self): state = self.SuperscriptAction.isChecked() self.superscriptSignal.emit(state)
class EditorBar(QToolBar): saveDocAsSignal = pyqtSignal() spellSignal = pyqtSignal(bool) whiteSpaceSignal = pyqtSignal(bool) boldSignal = pyqtSignal(bool) italicSignal = pyqtSignal(bool) underlineSignal = pyqtSignal(bool) strikethroughSignal = pyqtSignal(bool) subscriptSignal = pyqtSignal(bool) superscriptSignal = pyqtSignal(bool) def __init__(self, parent=None): QtGui.QToolBar.__init__(self, parent) self.setWindowTitle('EditorBar') self.setIconSize(QSize(16, 16)) self.createActions() def createActions(self): self.settingsAction = QAction(self.tr("Settings"), self) self.settingsAction.setIcon(QtGui.QIcon(":/icons/icons/configure.png")) self.settingsAction.triggered.connect(self.settings) self.addAction(self.settingsAction) self.saveDocAsAction = QAction(self.tr("Save As"), self) self.saveDocAsAction.triggered.connect(self.SaveDocumentAs) self.saveDocAsAction.setIcon(QtGui.QIcon(":/icons/icons/filesave.png")) self.addAction(self.saveDocAsAction) self.spellAction = QAction(self.tr("Spellchecking"), self) self.spellAction.setIcon( QtGui.QIcon(":/icons/icons/tools-check-spelling.png")) self.spellAction.setCheckable(True) self.spellAction.setChecked(settings.get('editor:spell')) self.spellAction.toggled.connect(self.spell) self.insertSeparator(self.spellAction) self.addAction(self.spellAction) self.whiteSpaceAction = QAction(self.tr("Show whitespace"), self) self.whiteSpaceAction.setIcon( QtGui.QIcon(":/icons/icons/whiteSpace.png")) self.whiteSpaceAction.setCheckable(True) self.whiteSpaceAction.setChecked(settings.get('editor:whiteSpace')) self.whiteSpaceAction.toggled.connect(self.whiteSpace) self.addAction(self.whiteSpaceAction) self.BoldAction = QtGui.QAction( QtGui.QIcon(":/icons/icons/format-text-bold.png"), self.tr("&Bold"), self, shortcut=QtCore.Qt.CTRL + QtCore.Qt.Key_B, triggered=self.bold, checkable=True) self.addAction(self.BoldAction) self.ItalicAction = QAction(self.tr("Italic"), self) self.ItalicAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-italic.png")) self.ItalicAction.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_I) self.ItalicAction.setCheckable(True) self.ItalicAction.triggered.connect(self.italic) self.addAction(self.ItalicAction) self.UnderlineAction = QAction(self.tr("Underline"), self) self.UnderlineAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-underline.png")) self.UnderlineAction.setCheckable(True) self.UnderlineAction.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_U) self.UnderlineAction.triggered.connect(self.underline) self.addAction(self.UnderlineAction) self.StrikethroughAction = QAction(self.tr("Strikethrough"), self) self.StrikethroughAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-strikethrough.png")) self.StrikethroughAction.setCheckable(True) self.StrikethroughAction.triggered.connect(self.strikethrough) self.addAction(self.StrikethroughAction) self.SubscriptAction = QAction(self.tr("Subscript"), self) self.SubscriptAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-subscript.png")) self.SubscriptAction.setCheckable(True) self.SubscriptAction.triggered.connect(self.subscript) self.addAction(self.SubscriptAction) self.SuperscriptAction = QAction(self.tr("Superscript"), self) self.SuperscriptAction.setIcon( QtGui.QIcon(":/icons/icons/format-text-superscript.png")) self.SuperscriptAction.setCheckable(True) self.SuperscriptAction.triggered.connect(self.superscript) self.addAction(self.SuperscriptAction) def settings(self): lectorSettings = Settings(self, 1) # QObject.connect(lectorSettings, SIGNAL('accepted()'), # self.updateTextEditor) lectorSettings.settingAccepted.connect(self.resetSpell) lectorSettings.show() def SaveDocumentAs(self): self.saveDocAsSignal.emit() def spell(self): state = self.spellAction.isChecked() self.spellSignal.emit(state) def resetSpell(self): ''' Turn off and on spellcheckig to use correct dictionary ''' state = self.spellAction.isChecked() if state: self.spellSignal.emit(False) self.spellSignal.emit(state) def whiteSpace(self): state = self.whiteSpaceAction.isChecked() self.whiteSpaceSignal.emit(state) def toggleFormat(self, CharFormat): font = CharFormat.font() self.BoldAction.setChecked(font.bold()) self.ItalicAction.setChecked(font.italic()) self.UnderlineAction.setChecked(font.underline()) self.StrikethroughAction.setChecked(CharFormat.fontStrikeOut()) if CharFormat.verticalAlignment() == \ QtGui.QTextCharFormat.AlignSuperScript: self.SubscriptAction.setChecked(False) self.SuperscriptAction.setChecked(True) elif CharFormat.verticalAlignment() == \ QtGui.QTextCharFormat.AlignSubScript: self.SubscriptAction.setChecked(True) self.SuperscriptAction.setChecked(False) else: self.SubscriptAction.setChecked(False) self.SuperscriptAction.setChecked(False) def bold(self): state = self.BoldAction.isChecked() self.boldSignal.emit(state) def italic(self): state = self.ItalicAction.isChecked() self.italicSignal.emit(state) def underline(self): state = self.UnderlineAction.isChecked() self.underlineSignal.emit(state) def strikethrough(self): state = self.StrikethroughAction.isChecked() self.strikethroughSignal.emit(state) def subscript(self): state = self.SubscriptAction.isChecked() self.subscriptSignal.emit(state) def superscript(self): state = self.SuperscriptAction.isChecked() self.superscriptSignal.emit(state)
class JobsButton(QFrame): # {{{ def __init__(self, horizontal=False, size=48, parent=None): QFrame.__init__(self, parent) if horizontal: size = 24 self.pi = ProgressIndicator(self, size) self._jobs = QLabel('<b>' + _('Jobs:') + ' 0') self._jobs.mouseReleaseEvent = self.mouseReleaseEvent self.shortcut = _('Shift+Alt+J') if horizontal: self.setLayout(QHBoxLayout()) self.layout().setDirection(self.layout().RightToLeft) else: self.setLayout(QVBoxLayout()) self._jobs.setAlignment(Qt.AlignHCenter | Qt.AlignBottom) self.layout().addWidget(self.pi) self.layout().addWidget(self._jobs) if not horizontal: self.layout().setAlignment(self._jobs, Qt.AlignHCenter) self._jobs.setMargin(0) self.layout().setMargin(0) self._jobs.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setCursor(Qt.PointingHandCursor) b = _('Click to see list of jobs') self.setToolTip(b + u' (%s)' % self.shortcut) self.action_toggle = QAction(b, parent) parent.addAction(self.action_toggle) self.action_toggle.setShortcut(self.shortcut) self.action_toggle.triggered.connect(self.toggle) def initialize(self, jobs_dialog, job_manager): self.jobs_dialog = jobs_dialog job_manager.job_added.connect(self.job_added) job_manager.job_done.connect(self.job_done) self.jobs_dialog.addAction(self.action_toggle) def mouseReleaseEvent(self, event): self.toggle() def toggle(self, *args): if self.jobs_dialog.isVisible(): self.jobs_dialog.hide() else: self.jobs_dialog.show() @property def is_running(self): return self.pi.isAnimated() def start(self): self.pi.startAnimation() def stop(self): self.pi.stopAnimation() def jobs(self): src = unicode(self._jobs.text()) return int(re.search(r'\d+', src).group()) def job_added(self, nnum): jobs = self._jobs src = unicode(jobs.text()) num = self.jobs() text = src.replace(str(num), str(nnum)) jobs.setText(text) self.start() def job_done(self, nnum): jobs = self._jobs src = unicode(jobs.text()) num = self.jobs() text = src.replace(str(num), str(nnum)) jobs.setText(text) if nnum == 0: self.no_more_jobs() def no_more_jobs(self): if self.is_running: self.stop() QCoreApplication.instance().alert(self, 5000)
class DocumentView(QWebView): # {{{ magnification_changed = pyqtSignal(object) DISABLED_BRUSH = QBrush(Qt.lightGray, Qt.Dense5Pattern) def initialize_view(self, debug_javascript=False): self.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform) self.flipper = SlideFlip(self) self.is_auto_repeat_event = False self.debug_javascript = debug_javascript self.shortcuts = Shortcuts(SHORTCUTS, 'shortcuts/viewer') self.setSizePolicy( QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self._size_hint = QSize(510, 680) self.initial_pos = 0.0 self.to_bottom = False self.document = Document(self.shortcuts, parent=self, debug_javascript=debug_javascript) self.setPage(self.document) self.manager = None self._reference_mode = False self._ignore_scrollbar_signals = False self.loading_url = None self.loadFinished.connect(self.load_finished) self.connect(self.document, SIGNAL('linkClicked(QUrl)'), self.link_clicked) self.connect(self.document, SIGNAL('linkHovered(QString,QString,QString)'), self.link_hovered) self.connect(self.document, SIGNAL('selectionChanged()'), self.selection_changed) self.connect(self.document, SIGNAL('animated_scroll_done()'), self.animated_scroll_done, Qt.QueuedConnection) copy_action = self.pageAction(self.document.Copy) copy_action.setIcon(QIcon(I('convert.png'))) d = self.document self.unimplemented_actions = list( map(self.pageAction, [ d.DownloadImageToDisk, d.OpenLinkInNewWindow, d.DownloadLinkToDisk, d.OpenImageInNewWindow, d.OpenLink ])) self.dictionary_action = QAction(QIcon(I('dictionary.png')), _('&Lookup in dictionary'), self) self.dictionary_action.setShortcut(Qt.CTRL + Qt.Key_L) self.dictionary_action.triggered.connect(self.lookup) self.addAction(self.dictionary_action) self.search_action = QAction(QIcon(I('dictionary.png')), _('&Search for next occurrence'), self) self.search_action.setShortcut(Qt.CTRL + Qt.Key_S) self.search_action.triggered.connect(self.search_next) self.addAction(self.search_action) self.goto_location_action = QAction(_('Go to...'), self) self.goto_location_menu = m = QMenu(self) self.goto_location_actions = a = { 'Next Page': self.next_page, 'Previous Page': self.previous_page, 'Section Top': partial(self.scroll_to, 0), 'Document Top': self.goto_document_start, 'Section Bottom': partial(self.scroll_to, 1), 'Document Bottom': self.goto_document_end, 'Next Section': self.goto_next_section, 'Previous Section': self.goto_previous_section, } for name, key in [(_('Next Section'), 'Next Section'), (_('Previous Section'), 'Previous Section'), (None, None), (_('Document Start'), 'Document Top'), (_('Document End'), 'Document Bottom'), (None, None), (_('Section Start'), 'Section Top'), (_('Section End'), 'Section Bottom'), (None, None), (_('Next Page'), 'Next Page'), (_('Previous Page'), 'Previous Page')]: if key is None: m.addSeparator() else: m.addAction(name, a[key], self.shortcuts.get_sequences(key)[0]) self.goto_location_action.setMenu(self.goto_location_menu) self.grabGesture(Qt.SwipeGesture) def goto_next_section(self, *args): if self.manager is not None: self.manager.goto_next_section() def goto_previous_section(self, *args): if self.manager is not None: self.manager.goto_previous_section() def goto_document_start(self, *args): if self.manager is not None: self.manager.goto_start() def goto_document_end(self, *args): if self.manager is not None: self.manager.goto_end() @property def copy_action(self): return self.pageAction(self.document.Copy) def animated_scroll_done(self): if self.manager is not None: self.manager.scrolled(self.document.scroll_fraction) def reference_mode(self, enable): self._reference_mode = enable self.document.reference_mode(enable) def goto(self, ref): self.document.goto(ref) def goto_bookmark(self, bm): self.document.goto_bookmark(bm) def config(self, parent=None): self.document.do_config(parent) if self.document.in_fullscreen_mode: self.document.switch_to_fullscreen_mode() self.setFocus(Qt.OtherFocusReason) def bookmark(self): return self.document.bookmark() def selection_changed(self): if self.manager is not None: self.manager.selection_changed( unicode(self.document.selectedText())) def contextMenuEvent(self, ev): menu = self.document.createStandardContextMenu() for action in self.unimplemented_actions: menu.removeAction(action) text = unicode(self.selectedText()) if text: menu.insertAction(list(menu.actions())[0], self.dictionary_action) menu.insertAction(list(menu.actions())[0], self.search_action) menu.addSeparator() menu.addAction(self.goto_location_action) if self.document.in_fullscreen_mode and self.manager is not None: menu.addSeparator() menu.addAction(self.manager.toggle_toolbar_action) menu.exec_(ev.globalPos()) def lookup(self, *args): if self.manager is not None: t = unicode(self.selectedText()).strip() if t: self.manager.lookup(t.split()[0]) def search_next(self): if self.manager is not None: t = unicode(self.selectedText()).strip() if t: self.manager.search.set_search_string(t) def set_manager(self, manager): self.manager = manager self.scrollbar = manager.horizontal_scrollbar self.connect(self.scrollbar, SIGNAL('valueChanged(int)'), self.scroll_horizontally) def scroll_horizontally(self, amount): self.document.scroll_to(y=self.document.ypos, x=amount) @property def scroll_pos(self): return (self.document.ypos, self.document.ypos + self.document.window_height) @property def viewport_rect(self): # (left, top, right, bottom) of the viewport in document co-ordinates # When in paged mode, left and right are the numbers of the columns # at the left edge and *after* the right edge of the viewport d = self.document if d.in_paged_mode: try: l, r = d.column_boundaries except ValueError: l, r = (0, 1) else: l, r = d.xpos, d.xpos + d.window_width return (l, d.ypos, r, d.ypos + d.window_height) def link_hovered(self, link, text, context): link, text = unicode(link), unicode(text) if link: self.setCursor(Qt.PointingHandCursor) else: self.unsetCursor() def link_clicked(self, url): if self.manager is not None: self.manager.link_clicked(url) def sizeHint(self): return self._size_hint @dynamic_property def scroll_fraction(self): def fget(self): return self.document.scroll_fraction def fset(self, val): self.document.scroll_fraction = float(val) return property(fget=fget, fset=fset) @property def hscroll_fraction(self): return self.document.hscroll_fraction @property def content_size(self): return self.document.width, self.document.height @dynamic_property def current_language(self): def fget(self): return self.document.current_language def fset(self, val): self.document.current_language = val return property(fget=fget, fset=fset) def search(self, text, backwards=False): flags = self.document.FindBackward if backwards else self.document.FindFlags( 0) found = self.findText(text, flags) if found and self.document.in_paged_mode: self.document.javascript('paged_display.snap_to_selection()') return found def path(self): return os.path.abspath(unicode(self.url().toLocalFile())) def load_path(self, path, pos=0.0): self.initial_pos = pos def callback(lu): self.loading_url = lu if self.manager is not None: self.manager.load_started() load_html(path, self, codec=getattr(path, 'encoding', 'utf-8'), mime_type=getattr(path, 'mime_type', None), pre_load_callback=callback) entries = set() for ie in getattr(path, 'index_entries', []): if ie.start_anchor: entries.add(ie.start_anchor) if ie.end_anchor: entries.add(ie.end_anchor) self.document.index_anchors = entries def initialize_scrollbar(self): if getattr(self, 'scrollbar', None) is not None: if self.document.in_paged_mode: self.scrollbar.setVisible(False) return delta = self.document.width - self.size().width() if delta > 0: self._ignore_scrollbar_signals = True self.scrollbar.blockSignals(True) self.scrollbar.setRange(0, delta) self.scrollbar.setValue(0) self.scrollbar.setSingleStep(1) self.scrollbar.setPageStep(int(delta / 10.)) self.scrollbar.setVisible(delta > 0) self.scrollbar.blockSignals(False) self._ignore_scrollbar_signals = False def load_finished(self, ok): if self.loading_url is None: # An <iframe> finished loading return self.loading_url = None self.document.load_javascript_libraries() self.document.after_load() self._size_hint = self.document.mainFrame().contentsSize() scrolled = False if self.to_bottom: self.to_bottom = False self.initial_pos = 1.0 if self.initial_pos > 0.0: scrolled = True self.scroll_to(self.initial_pos, notify=False) self.initial_pos = 0.0 self.update() self.initialize_scrollbar() self.document.reference_mode(self._reference_mode) if self.manager is not None: spine_index = self.manager.load_finished(bool(ok)) if spine_index > -1: self.document.set_reference_prefix('%d.' % (spine_index + 1)) if scrolled: self.manager.scrolled(self.document.scroll_fraction, onload=True) if self.flipper.isVisible(): if self.flipper.running: self.flipper.setVisible(False) else: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) @classmethod def test_line(cls, img, y): 'Test if line contains pixels of exactly the same color' start = img.pixel(0, y) for i in range(1, img.width()): if img.pixel(i, y) != start: return False return True def current_page_image(self, overlap=-1): if overlap < 0: overlap = self.height() img = QImage(self.width(), overlap, QImage.Format_ARGB32_Premultiplied) painter = QPainter(img) painter.setRenderHints(self.renderHints()) self.document.mainFrame().render(painter, QRegion(0, 0, self.width(), overlap)) painter.end() return img def find_next_blank_line(self, overlap): img = self.current_page_image(overlap) for i in range(overlap - 1, -1, -1): if self.test_line(img, i): self.scroll_by(y=i, notify=False) return self.scroll_by(y=overlap) def previous_page(self): if self.flipper.running and not self.is_auto_repeat_event: return if self.loading_url is not None: return epf = self.document.enable_page_flip and not self.is_auto_repeat_event if self.document.in_paged_mode: loc = self.document.javascript( 'paged_display.previous_screen_location()', typ='int') if loc < 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.manager.previous_document() else: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.document.scroll_to(x=loc, y=0) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return delta_y = self.document.window_height - 25 if self.document.at_top: if self.manager is not None: self.to_bottom = True if epf: self.flipper.initialize(self.current_page_image(), False) self.manager.previous_document() else: opos = self.document.ypos upper_limit = opos - delta_y if upper_limit < 0: upper_limit = 0 if upper_limit < opos: if epf: self.flipper.initialize(self.current_page_image(), forwards=False) self.document.scroll_to(self.document.xpos, upper_limit) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) def next_page(self): if self.flipper.running and not self.is_auto_repeat_event: return if self.loading_url is not None: return epf = self.document.enable_page_flip and not self.is_auto_repeat_event if self.document.in_paged_mode: loc = self.document.javascript( 'paged_display.next_screen_location()', typ='int') if loc < 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() else: if epf: self.flipper.initialize(self.current_page_image()) self.document.scroll_to(x=loc, y=0) if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return window_height = self.document.window_height document_height = self.document.height ddelta = document_height - window_height #print '\nWindow height:', window_height #print 'Document height:', self.document.height delta_y = window_height - 25 if self.document.at_bottom or ddelta <= 0: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() elif ddelta < 25: self.scroll_by(y=ddelta) return else: oopos = self.document.ypos #print 'Original position:', oopos self.document.set_bottom_padding(0) opos = self.document.ypos #print 'After set padding=0:', self.document.ypos if opos < oopos: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() return #oheight = self.document.height lower_limit = opos + delta_y # Max value of top y co-ord after scrolling max_y = self.document.height - window_height # The maximum possible top y co-ord if max_y < lower_limit: padding = lower_limit - max_y if padding == window_height: if self.manager is not None: if epf: self.flipper.initialize(self.current_page_image()) self.manager.next_document() return #print 'Setting padding to:', lower_limit - max_y self.document.set_bottom_padding(lower_limit - max_y) if epf: self.flipper.initialize(self.current_page_image()) #print 'Document height:', self.document.height #print 'Height change:', (self.document.height - oheight) max_y = self.document.height - window_height lower_limit = min(max_y, lower_limit) #print 'Scroll to:', lower_limit if lower_limit > opos: self.document.scroll_to(self.document.xpos, lower_limit) actually_scrolled = self.document.ypos - opos #print 'After scroll pos:', self.document.ypos #print 'Scrolled by:', self.document.ypos - opos self.find_next_blank_line(window_height - actually_scrolled) #print 'After blank line pos:', self.document.ypos if epf: self.flipper(self.current_page_image(), duration=self.document.page_flip_duration) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) #print 'After all:', self.document.ypos def scroll_by(self, x=0, y=0, notify=True): old_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) self.document.scroll_by(x, y) new_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if notify and self.manager is not None and new_pos != old_pos: self.manager.scrolled(self.scroll_fraction) def scroll_to(self, pos, notify=True): if self._ignore_scrollbar_signals: return old_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if self.document.in_paged_mode: if isinstance(pos, basestring): self.document.jump_to_anchor(pos) else: self.document.scroll_fraction = pos else: if isinstance(pos, basestring): self.document.jump_to_anchor(pos) else: if pos >= 1: self.document.scroll_to(0, self.document.height) else: y = int( math.ceil(pos * (self.document.height - self.document.window_height))) self.document.scroll_to(0, y) new_pos = (self.document.xpos if self.document.in_paged_mode else self.document.ypos) if notify and self.manager is not None and new_pos != old_pos: self.manager.scrolled(self.scroll_fraction) @dynamic_property def multiplier(self): def fget(self): return self.zoomFactor() def fset(self, val): self.setZoomFactor(val) self.magnification_changed.emit(val) return property(fget=fget, fset=fset) def magnify_fonts(self, amount=None): if amount is None: amount = self.document.font_magnification_step with self.document.page_position: self.multiplier += amount return self.document.scroll_fraction def shrink_fonts(self, amount=None): if amount is None: amount = self.document.font_magnification_step if self.multiplier >= amount: with self.document.page_position: self.multiplier -= amount return self.document.scroll_fraction def changeEvent(self, event): if event.type() == event.EnabledChange: self.update() return QWebView.changeEvent(self, event) def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(self.renderHints()) self.document.mainFrame().render(painter, event.region()) if not self.isEnabled(): painter.fillRect(event.region().boundingRect(), self.DISABLED_BRUSH) painter.end() def wheelEvent(self, event): mods = event.modifiers() if mods & Qt.CTRL: if self.manager is not None and event.delta() != 0: (self.manager.font_size_larger if event.delta() > 0 else self.manager.font_size_smaller)() return if self.document.in_paged_mode: if abs(event.delta()) < 15: return typ = 'screen' if self.document.wheel_flips_pages else 'col' direction = 'next' if event.delta() < 0 else 'previous' loc = self.document.javascript('paged_display.%s_%s_location()' % (direction, typ), typ='int') if loc > -1: self.document.scroll_to(x=loc, y=0) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) event.accept() elif self.manager is not None: if direction == 'next': self.manager.next_document() else: self.manager.previous_document() event.accept() return if event.delta() < -14: if self.document.wheel_flips_pages: self.next_page() event.accept() return if self.document.at_bottom: self.scroll_by(y=15) # at_bottom can lie on windows if self.manager is not None: self.manager.next_document() event.accept() return elif event.delta() > 14: if self.document.wheel_flips_pages: self.previous_page() event.accept() return if self.document.at_top: if self.manager is not None: self.manager.previous_document() event.accept() return ret = QWebView.wheelEvent(self, event) scroll_amount = (event.delta() / 120.0) * .2 * -1 if event.orientation() == Qt.Vertical: self.scroll_by( 0, self.document.viewportSize().height() * scroll_amount) else: self.scroll_by( self.document.viewportSize().width() * scroll_amount, 0) if self.manager is not None: self.manager.scrolled(self.scroll_fraction) return ret def keyPressEvent(self, event): if not self.handle_key_press(event): return QWebView.keyPressEvent(self, event) def paged_col_scroll(self, forward=True): dir = 'next' if forward else 'previous' loc = self.document.javascript('paged_display.%s_col_location()' % dir, typ='int') if loc > -1: self.document.scroll_to(x=loc, y=0) self.manager.scrolled(self.document.scroll_fraction) else: (self.manager.next_document() if forward else self.manager.previous_document()) def handle_key_press(self, event): handled = True key = self.shortcuts.get_match(event) func = self.goto_location_actions.get(key, None) if func is not None: self.is_auto_repeat_event = event.isAutoRepeat() try: func() finally: self.is_auto_repeat_event = False elif key == 'Down': if self.document.in_paged_mode: self.paged_col_scroll() else: if (not self.document.line_scrolling_stops_on_pagebreaks and self.document.at_bottom): self.manager.next_document() else: self.scroll_by(y=15) elif key == 'Up': if self.document.in_paged_mode: self.paged_col_scroll(forward=False) else: if (not self.document.line_scrolling_stops_on_pagebreaks and self.document.at_top): self.manager.previous_document() else: self.scroll_by(y=-15) elif key == 'Left': if self.document.in_paged_mode: self.paged_col_scroll(forward=False) else: self.scroll_by(x=-15) elif key == 'Right': if self.document.in_paged_mode: self.paged_col_scroll() else: self.scroll_by(x=15) else: handled = False return handled def resizeEvent(self, event): if self.manager is not None: self.manager.viewport_resize_started(event) return QWebView.resizeEvent(self, event) def event(self, ev): if ev.type() == ev.Gesture: swipe = ev.gesture(Qt.SwipeGesture) if swipe is not None: self.handle_swipe(swipe) return True return QWebView.event(self, ev) def handle_swipe(self, swipe): if swipe.state() == Qt.GestureFinished: if swipe.horizontalDirection() == QSwipeGesture.Left: self.previous_page() elif swipe.horizontalDirection() == QSwipeGesture.Right: self.next_page() elif swipe.verticalDirection() == QSwipeGesture.Up: self.goto_previous_section() elif swipe.horizontalDirection() == QSwipeGesture.Down: self.goto_next_section() def mouseReleaseEvent(self, ev): opos = self.document.ypos ret = QWebView.mouseReleaseEvent(self, ev) if self.manager is not None and opos != self.document.ypos: self.manager.internal_link_clicked(opos) self.manager.scrolled(self.scroll_fraction) return ret