def startDrag(self): self.drag = QtGui.QDrag(self) mime = QtCore.QMimeData() #mime.setPlainText("asd") self.drag.setMimeData(mime) self.widgetArea.setStyleSheet(self.dragStyle) self.update() action = self.drag.exec_() self.updateStyle()
def mouseMoveEvent(self, event): '''If the current movement is a drag convert it into a QDrag. If the drag ends outside the tab bar emit an onDetachTabSignal. - event : a mouse move event. ''' # Determine if the current movement is detected as a drag if not self.dragStartPos.isNull() and ( (event.pos() - self.dragStartPos).manhattanLength() < QtWidgets.QApplication.startDragDistance()): self.dragInitiated = True # If the current movement is a drag initiated by the left button if (((event.buttons() & QtCore.Qt.LeftButton)) and self.dragInitiated): # Stop the move event finishMoveEvent = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, event.pos(), QtCore.Qt.NoButton, QtCore.Qt.NoButton, QtCore.Qt.NoModifier) QtWidgets.QTabBar.mouseMoveEvent(self, finishMoveEvent) # Convert the move event into a drag drag = QtGui.QDrag(self) mimeData = QtCore.QMimeData() drag.setMimeData(mimeData) # Create the appearance of dragging the tab content pixmap = self.parent().widget(self.tabAt(self.dragStartPos)).grab() targetPixmap = QtGui.QPixmap(pixmap.size()) targetPixmap.fill(QtCore.Qt.transparent) painter = QtGui.QPainter(targetPixmap) painter.setOpacity(0.85) painter.drawPixmap(0, 0, pixmap) painter.end() drag.setPixmap(targetPixmap) # Initiate the drag dropAction = drag.exec_(QtCore.Qt.MoveAction | QtCore.Qt.CopyAction) # For Linux: Here, drag.exec_() will not return MoveAction on Linux. So it # must be set manually if self.dragDropedPos.x() != 0 and self.dragDropedPos.y() != 0: dropAction = QtCore.Qt.MoveAction # If the drag completed outside of the tab bar, detach the tab and move # the content to the current cursor position if dropAction == QtCore.Qt.IgnoreAction: event.accept() self.onDetachTabSignal.emit(self.tabAt(self.dragStartPos), self.mouseCursor.pos()) else: QtWidgets.QTabBar.mouseMoveEvent(self, event)
def export(self, fileName=None, toBytes=False, copy=False): if toBytes is False and copy is False and fileName is None: self.fileSaveDialog(filter="Scalable Vector Graphics (*.svg)") return #self.svg = QtSvg.QSvgGenerator() #self.svg.setFileName(fileName) #dpi = QtGui.QDesktopWidget().physicalDpiX() ### not really sure why this works, but it seems to be important: #self.svg.setSize(QtCore.QSize(self.params['width']*dpi/90., self.params['height']*dpi/90.)) #self.svg.setResolution(dpi) ##self.svg.setViewBox() #targetRect = QtCore.QRect(0, 0, self.params['width'], self.params['height']) #sourceRect = self.getSourceRect() #painter = QtGui.QPainter(self.svg) #try: #self.setExportMode(True) #self.render(painter, QtCore.QRectF(targetRect), sourceRect) #finally: #self.setExportMode(False) #painter.end() ## Workaround to set pen widths correctly #data = open(fileName).readlines() #for i in range(len(data)): #line = data[i] #m = re.match(r'(<g .*)stroke-width="1"(.*transform="matrix\(([^\)]+)\)".*)', line) #if m is not None: ##print "Matched group:", line #g = m.groups() #matrix = list(map(float, g[2].split(','))) ##print "matrix:", matrix #scale = max(abs(matrix[0]), abs(matrix[3])) #if scale == 0 or scale == 1.0: #continue #data[i] = g[0] + ' stroke-width="%0.2g" ' % (1.0/scale) + g[1] + '\n' ##print "old line:", line ##print "new line:", data[i] #open(fileName, 'w').write(''.join(data)) ## Qt's SVG generator is not complete. (notably, it lacks clipping) ## Instead, we will use Qt to generate SVG for each item independently, ## then manually reconstruct the entire document. xml = generateSvg(self.item) if toBytes: return xml.encode('UTF-8') elif copy: md = QtCore.QMimeData() md.setData('image/svg+xml', QtCore.QByteArray(xml.encode('UTF-8'))) QtGui.QApplication.clipboard().setMimeData(md) else: with open(fileName, 'w') as fh: fh.write(xml.encode('UTF-8'))
def mimeData(self, indexes): print "start drag" mimedata = QtCore.QMimeData() #data = QtCore.QByteArray() #stream = QtCore.QDataStream(data, QtCore.QIODevice.WriteOnly) #stream << indexes[0].internalPointer() mimedata.setData('application/x-mavplot', str(indexes[0].internalPointer().getFullKey())) mimedata.setText(str(indexes[0].internalPointer().getFullKey())) self.lastDraggedNode = indexes[0].internalPointer() return mimedata
def copyHtml(self): txt = '<table>\n' txt += self._itemToHtml(self.ui.solutionTable, self.ui.solutionTable.headerItem()) root = self.ui.solutionTable.invisibleRootItem() for i in range(root.childCount()): txt += self._itemToHtml(self.ui.solutionTable, root.child(i)) txt += '</table>\n' # copy to clipboard if os.sys.platform in ['darwin']: QtGui.QApplication.clipboard().setText(txt) else: md = QtCore.QMimeData() md.setHtml(txt) QtGui.QApplication.clipboard().setMimeData(md)
def export(self, fileName=None, toBytes=False, copy=False): if toBytes is False and copy is False and fileName is None: self.fileSaveDialog(filter="Scalable Vector Graphics (*.svg)") return ## Qt's SVG generator is not complete. (notably, it lacks clipping) ## Instead, we will use Qt to generate SVG for each item independently, ## then manually reconstruct the entire document. xml = generateSvg(self.item) if toBytes: return xml.encode('UTF-8') elif copy: md = QtCore.QMimeData() md.setData('image/svg+xml', QtCore.QByteArray(xml.encode('UTF-8'))) QtGui.QApplication.clipboard().setMimeData(md) else: with open(fileName, 'wb') as fh: fh.write(asUnicode(xml).encode('utf-8'))
def copyHtml(self): table = self.ui.recipeTable # decide which columns to skip skip = [] for col in range(table.columnCount()): s = False for row in (0, 1): item = table.item(row, col) if item is not None and str(item.text()) == '+': s = True break skip.append(s) # generate HTML table txt = '<div style="font-family: sans-serif"><h2>%s</h2><table>\n' % self.recipeSet.name for row in range(table.rowCount()): txt += ' <tr>\n' spanskip = 0 for col in range(table.columnCount()): # skip cell if a previous cell has wide span if spanskip > 0: spanskip -= 1 continue # skip column if it was a placeholder / adder column if skip[col]: continue item = table.item(row, col) span = table.columnSpan(row, col) spanskip = span - 1 for c in range(col + 1, col + span): if skip[c]: span -= 1 width = table.horizontalHeader().sectionSize(col) if item is None: w = table.cellWidget(row, col) if w is None: t = '' style = '' else: t = str(w.text()) fs = w.font().pointSize() a = w.alignment() if a & QtCore.Qt.AlignRight > 0: align = 'right' elif a & QtCore.Qt.AlignLeft > 0: align = 'left' elif a & QtCore.Qt.AlignCenter > 0: align = 'center' style = 'font-size: %dpt; text-align: %s' % (fs, align) else: t = asUnicode(item.text()) bg = item.background().color().name() fg = item.foreground().color().name() style = 'color: %s; background-color: %s;' % (fg, bg) if hasattr(item, 'borders'): for k, v in item.borders.items(): style += ' border-%s: 1px solid %s;' % ( k, v.color().name()) txt += ' <td style="font-family: sans-serif; font-size: 10pt; vertical-align: middle; width: %dpx; %s" colspan="%s">%s</td>\n' % ( width, style, span, t) txt += ' </tr>\n' txt += '</table>\n' # copy notes txt += '<span style="font-size: 10pt;">\n' for i in range(self.ui.notesTree.topLevelItemCount()): item = self.ui.notesTree.topLevelItem(i) note = item.noteHtml() if note != '': txt += '\n<br><br>' + note txt += '</span>\n' txt += '\n</div>' # copy to clipboard if os.sys.platform in ['darwin']: QtGui.QApplication.clipboard().setText(txt) else: md = QtCore.QMimeData() md.setHtml(txt) QtGui.QApplication.clipboard().setMimeData(md)