Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt4.QtCore import Qt
- from PyQt4.QtCore import QRectF
- from PyQt4.QtWidgets import QApplication
- from PyQt4.QtGui import QColor
- from PyQt4.QtGui import QFont
- from PyQt4.QtGui import QPainter
- from PyQt4.QtGui import QPixmap
- from PyQt4.QtGui import QTextOption
- from PyQt4.QtGui import QToolTip
- from PyQt4.QtGui import QWidget
- class AreaSelector(QWidget):
- def __init__(self, parent=None):
- QWidget.__init__(self, None, Qt.FramelessWindowHint)
- self.setAttribute(Qt.WA_TranslucentBackground)
- self.setAttribute(Qt.WA_DeleteOnClose)
- self.setWindowState(Qt.WindowFullScreen)
- self.setAutoFillBackground(False)
- self.parent = parent
- self.start_x = 0
- self.start_y = 0
- self.end_x = 0
- self.end_y = 0
- self.current_x = 0
- self.current_y = 0
- def showEvent(self, event):
- self.bg = QPixmap.grabWindow(QApplication.desktop().winId())
- self.screen_geometry = QApplication.desktop().screenGeometry(self)
- def mousePressEvent(self, event):
- self.start_x = event.globalX()
- self.start_y = event.globalY()
- def mouseReleaseEvent(self, event):
- self.end_x = event.globalX()
- self.end_y = event.globalY()
- def mouseMoveEvent(self, event):
- self.current_x = event.globalX()
- self.current_y = event.globalY()
- self.repaint()
- text = "Start: %sx%s \nEnd: %sx%s" % (self.start_x, self.start_y, self.current_x, self.current_y)
- QToolTip.showText(event.pos(), text)
- def keyPressEvent(self, event):
- if event.key() == Qt.Key_Return:
- self._acceptSelection()
- elif event.key() == Qt.Key_Escape:
- self.close()
- def _acceptSelection(self):
- if self.parent is not None:
- self.parent.areaSelectEvent(self.start_x, self.start_y, self.end_x, self.end_y)
- self.close()
- def paintEvent(self, event):
- painter = QPainter()
- painter.begin(self)
- painter.fillRect(self.screen_geometry, QColor(10, 10, 10, 125))
- self._paint_selection(painter)
- self._paint_usage_text(painter)
- painter.end()
- def _paint_usage_text(self, painter):
- font = QFont("Helvetica [Cronyx]", 26, QFont.Bold)
- painter.setFont(font)
- painter.setPen(QColor(255, 255, 255, 255))
- screen_width = self.screen_geometry.width()
- text_width = 800
- text_start_x = screen_width / 2 - text_width / 2
- screen_height = self.screen_geometry.height()
- text_height = 200
- text_start_y = screen_height / 2 - text_height / 2
- textoption = QTextOption(Qt.AlignCenter)
- textbox = QRectF(text_start_x, text_start_y, text_width, text_height)
- painter.drawText(textbox,
- "Click & Drag to select an area\n"
- "ENTER to confirm or ESC to cancel",
- textoption)
- painter.drawRoundedRect(textbox, 20, 20)
- def _paint_selection(self, painter):
- '''Draws the current user selection'''
- rectangle = QRectF()
- if self.start_x > self.current_x:
- rectangle.setLeft(self.current_x)
- rectangle.setRight(self.start_x)
- else:
- rectangle.setLeft(self.start_x)
- rectangle.setRight(self.current_x)
- if self.start_y > self.current_y:
- rectangle.setTop(self.current_y)
- rectangle.setBottom(self.start_y)
- else:
- rectangle.setTop(self.start_y)
- rectangle.setBottom(self.current_y)
- painter.drawPixmap(rectangle, self.bg, rectangle)
- painter.drawRect(rectangle)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- main = AreaSelector()
- main.show()
- sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment