def __init__(self, parent=None): super(XSnapshotWidget, self).__init__(parent) # define custom properties self._region = QRect() self._filepath = '' # define custom properties palette = self.palette() palette.setColor(palette.Window, QColor('white')) self.setPalette(palette) self.setWindowOpacity(0.5) self.setWindowFlags(Qt.SplashScreen) self.setFocus()
def visualRect(self, action): """ Returns the visual rect for the inputed action, or a blank QRect if no matching action was found. :param action | <QAction> :return <QRect> """ for widget in self.actionLabels(): if widget.action() == action: return widget.geometry() return QRect()
def rebuildWeek(self, opt): """ Rebuilds the scale for the week mode. :param opt | <XOrbRenderOptions> """ self._labels = [] self._hlines = [] self._vlines = [] self._weekendRects = [] self._alternateRects = [] self._topLabels = [] top_format = 'MMM' label_format = 'd' increment = 1 # days # generate vertical lines x = 0 i = 0 half = opt.header_height / 2.0 curr = opt.start top_label = opt.start.toString(top_format) top_rect = QRect(0, 0, 0, half) alt_rect = None while curr <= opt.end: # update the month rect new_top_label = curr.toString(top_format) if new_top_label != top_label: top_rect.setRight(x) self._topLabels.append((top_rect, top_label)) top_rect = QRect(x, 0, 0, half) top_label = new_top_label if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) alt_rect = None else: alt_rect = QRect(x, 0, 0, opt.height) # create the line self._hlines.append(QLine(x, 0, x, opt.height)) # create the header label/rect label = nativestring(curr.toString(label_format)) rect = QRect(x, half, opt.cell_width, half) self._labels.append((rect, label)) # store weekend rectangles if curr.dayOfWeek() in (6, 7): rect = QRect(x, 0, opt.cell_width, opt.height) self._weekendRects.append(rect) # increment the dates curr = curr.addDays(increment) x += opt.cell_width i += 1 # update the month rect top_rect.setRight(x) top_label = opt.end.toString(top_format) self._topLabels.append((top_rect, top_label)) if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) # resize the width to match the last date range new_width = x self.setSceneRect(0, 0, new_width, opt.height) # generate horizontal lines y = 0 h = opt.height width = new_width while y < h: self._vlines.append(QLine(0, y, width, y)) y += opt.cell_height # clear the dirty flag self._dirty = False
def rebuildHour(self, opt): """ Rebuilds the scale for the minute mode. :param opt | <XGanttRenderOptions> """ self._labels = [] self._hlines = [] self._vlines = [] self._weekendRects = [] self._alternateRects = [] self._topLabels = [] top_format = 'MMM d h:00a' gantt = self.ganttWidget() if gantt.timescale() == gantt.Timescale.Minute: label_format = 'h:mma' else: label_format = 'mm' increment = 1 # minute # generate vertical lines x = 0 i = 0 half = opt.header_height / 2.0 curr = opt.start top_label = opt.start.toString(top_format) top_rect = QRect(0, 0, 0, half) alt_rect = None while curr <= opt.end: # update the top rect new_top_label = curr.toString(top_format) if new_top_label != top_label: top_rect.setRight(x) self._topLabels.append((top_rect, top_label)) top_rect = QRect(x, 0, 0, half) top_label = new_top_label if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) alt_rect = None else: alt_rect = QRect(x, 0, 0, opt.height) # create the line self._hlines.append(QLine(x, 0, x, opt.height)) # create the header label/rect label = nativestring(curr.toString(label_format)) rect = QRect(x, half, opt.cell_width, half) self._labels.append((rect, label)) # increment the dates curr = curr.addSecs(increment * 60) x += opt.cell_width i += 1 # update the top rect top_rect.setRight(x) top_label = opt.end.toString(top_format) self._topLabels.append((top_rect, top_label)) if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) # resize the width to match the last date range new_width = x self.setSceneRect(0, 0, new_width, opt.height) # generate horizontal lines y = 0 h = opt.height width = new_width while y < h: self._vlines.append(QLine(0, y, width, y)) y += opt.cell_height # clear the dirty flag self._dirty = False
def rebuildDay(self, opt): """ Rebuilds the scale for the day mode. :param opt | <XGanttRenderOptions> """ self._labels = [] self._hlines = [] self._vlines = [] self._weekendRects = [] self._alternateRects = [] self._topLabels = [] top_format = 'dddd MMMM dd' label_format = 'ha' increment = 60 # hour # generate vertical lines x = 0 i = 0 half = opt.header_height / 2.0 curr = QDateTime(opt.start, QTime(0, 0, 0)) end = QDateTime(opt.end, QTime(23, 0, 0)) top_label = opt.start.toString(top_format) top_rect = QRect(0, 0, 0, half) alt_rect = None while curr <= end: # update the top rect new_top_label = curr.toString(top_format) if new_top_label != top_label: top_rect.setRight(x) self._topLabels.append((top_rect, top_label)) top_rect = QRect(x, 0, 0, half) top_label = new_top_label if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) alt_rect = None else: alt_rect = QRect(x, 0, 0, opt.height) # create the line self._hlines.append(QLine(x, 0, x, opt.height)) # create the header label/rect label = nativestring(curr.toString(label_format))[:-1] rect = QRect(x, half, opt.cell_width, half) self._labels.append((rect, label)) # increment the dates curr = curr.addSecs(increment * 60) x += opt.cell_width i += 1 # update the top rect top_rect.setRight(x) top_label = opt.end.toString(top_format) self._topLabels.append((top_rect, top_label)) if alt_rect is not None: alt_rect.setRight(x) self._alternateRects.append(alt_rect) # resize the width to match the last date range new_width = x self.setSceneRect(0, 0, new_width, opt.height) # generate horizontal lines y = 0 h = opt.height width = new_width while y < h: self._vlines.append(QLine(0, y, width, y)) y += opt.cell_height # clear the dirty flag self._dirty = False
class XSnapshotWidget(QWidget): def __init__(self, parent=None): super(XSnapshotWidget, self).__init__(parent) # define custom properties self._region = QRect() self._filepath = '' # define custom properties palette = self.palette() palette.setColor(palette.Window, QColor('white')) self.setPalette(palette) self.setWindowOpacity(0.5) self.setWindowFlags(Qt.SplashScreen) self.setFocus() def accept(self): """ Prompts the user for the filepath to save and then saves the image. """ filetypes = 'PNG Files (*.png);;JPG Files (*.jpg);;All Files (*.*)' filename = QFileDialog.getSaveFileName(None, 'Save Snapshot', self.filepath(), filetypes) if type(filename) == tuple: filename = filename[0] filename = nativestring(filename) if not filename: self.reject() else: self.setFilepath(filename) self.save() def filepath(self): """ Returns the filepath that is going to be asved for this snapshot widget. :return <str> """ return self._filepath def hideWindow(self): """ Sets the window to hide/show while taking the snapshot. :param window | <QMainWindow> || <QDialog> """ return self._hideWindow def keyPressEvent(self, event): """ Listens for the escape key to cancel out from this snapshot. :param event | <QKeyPressEvent> """ # reject on a cancel if event.key() == Qt.Key_Escape: self.reject() super(XSnapshotWidget, self).keyPressEvent(event) def mousePressEvent(self, event): """ Starts the selection process for this widget and snapshot area. :param event | <QMousePressEvent> """ self._region.setX(event.pos().x()) self._region.setY(event.pos().y()) super(XSnapshotWidget, self).mousePressEvent(event) def mouseMoveEvent(self, event): """ Drags the selection view for this widget. :param event | <QMouseMoveEvent> """ w = event.pos().x() - self._region.x() h = event.pos().y() - self._region.y() self._region.setWidth(w) self._region.setHeight(h) self.repaint() super(XSnapshotWidget, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): """ Finishes the selection event. :param event | <QMouseReleaseEvent> """ self.accept() super(XSnapshotWidget, self).mouseReleaseEvent(event) def paintEvent(self, event): """ Handles the drawing for this widget and its selection region. :param event | <QPaintEvent> """ pen = QPen(Qt.DashLine) pen.setColor(QColor('red')) with XPainter(self) as painter: painter.setPen(pen) clr = QColor('black') clr.setAlpha(100) painter.setBrush(clr) painter.drawRect(self._region) def reject(self): """ Rejects the snapshot and closes the widget. """ if self.hideWindow(): self.hideWindow().show() self.close() self.deleteLater() def region(self): """ Returns the selection region defined by the rectangle for snapshoting. :return <QRect> """ return self._region def save(self): """ Saves the snapshot based on the current region. """ # close down the snapshot widget if self.hideWindow(): self.hideWindow().hide() self.hide() QApplication.processEvents() time.sleep(1) # create the pixmap to save wid = QApplication.desktop().winId() if not self._region.isNull(): x = self._region.x() y = self._region.y() w = self._region.width() h = self._region.height() else: x = self.x() y = self.y() w = self.width() h = self.height() pixmap = QPixmap.grabWindow(wid, x, y, w, h) pixmap.save(self.filepath()) self.close() self.deleteLater() if self.hideWindow(): self.hideWindow().show() def show(self): """ Shows this widget and hides the specified window if necessary. """ super(XSnapshotWidget, self).show() if self.hideWindow(): self.hideWindow().hide() QApplication.processEvents() def setFilepath(self, filepath): """ Sets the filepath that will be saved for this snapshot. :param filepath | <str> """ self._filepath = filepath def setHideWindow(self, window): """ Sets the window that will be hidden while this snapshot is being taken. :param window | <QMainWindow> """ self._hideWindow = window def setRegion(self, rect): """ Sets the region rectangle to the inputed rect. :param rect | <QRect> """ if rect is not None: self._region = rect @staticmethod def capture(rect=None, filepath='', prompt=True, hideWindow=None): """ Prompts the user to capture the screen. :param rect | <QRect> filepath | <str> prompt | <bool> :return (<str> filepath, <bool> accepted) """ widget = XSnapshotWidget(QApplication.desktop()) widget.setRegion(rect) widget.setHideWindow(hideWindow) widget.setFilepath(filepath) widget.move(1, 1) widget.resize(QApplication.desktop().size()) if prompt or not filepath: widget.show() else: widget.save()