def __init__(self , parent=None , initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width() / 2 if parent else 10 self.setGeometry(x, 10, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() # SubWindow do not have title bar with buttons # Thanks to button1 widget user can close MessageDlg instance manually self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon (QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2 , SIGNAL("clicked()") , self.close) # create main UI hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 1, Qt.AlignLeft) hbox.addSpacing(10) hbox.addLayout(self.centreLayout , 1) hbox.addSpacing(10) hbox.addWidget(self.button1, 0, Qt.AlignTop) hbox.addWidget(button2, 0, Qt.AlignTop) self.setLayout(hbox) # If this widgets will be hide, then we can using more space for content of centreLayout # or another widget, when we do not have button1 or _message is empty self._message.hide() self.button1.hide()
def __init__(self, parent, originalRect, absolutePos): QDialog.__init__(self, parent, Qt.CustomizeWindowHint | Qt.WindowTitleHint) self.setAttribute(Qt.WA_DeleteOnClose) self.setWindowTitle("Adjust View") self.originalRect = originalRect self.startPos = absolutePos buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal) self.widthSpinBox = self.makeSpinBox(originalRect.width(), self._minValue, 1001.0, None) self.heightSpinBox = self.makeSpinBox(originalRect.height(), self._minValue, 1001.0, None) self.xyzWidget = XYZWidget(None, -1000, 1000, originalRect.x(), originalRect.y(), 0.0) self.conn = ExtendedLabel(self) self.conn.setSwitchablePixmap(QPixmap(":/link_break"),QPixmap(":/link")) self.conn.setPixmap(QPixmap(":/link_break")) find = ExtendedLabel(self) find.setPixmap(QPixmap(":/find")) grid = QGridLayout() grid.addWidget(QLabel("W:"),1,1,Qt.AlignRight) grid.addWidget(self.widthSpinBox,1,2) grid.addWidget(QLabel("H:"),2,1,Qt.AlignRight) grid.addWidget(self.heightSpinBox,2,2) hbox = QHBoxLayout() hbox.addLayout(grid,1) hbox.addWidget(self.conn) hbox.addSpacing(15) master = QGridLayout() master.addLayout(hbox, 1, 1 ,Qt.AlignHCenter) master.addWidget(self.xyzWidget, 2, 1) master.addWidget(find, 2, 2 ,Qt.AlignTop) master.addWidget(buttonBox, 3, 0, 1, 3) self.setLayout(master) self.move( parent.window().pos().x() , parent.mapToGlobal(parent.pos()).y() ) self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) self.connect(find, SIGNAL('clicked()'), self.findPointSignal) self.connect(self.widthSpinBox, SIGNAL("valueChanged(int)"), self.changeWidth) self.connect(self.heightSpinBox, SIGNAL("valueChanged(int)"), self.changeHeight) self.connect(self.xyzWidget.xSpinBox, SIGNAL("valueChanged(int)"), self.change) self.connect(self.xyzWidget.ySpinBox, SIGNAL("valueChanged(int)"), self.change) self.xyzWidget.zSpinBox.setEnabled(False) self.widthSpinBox.selectAll() self.heightSpinBox.selectAll() # Can not use setModal or setWindowModality. Because a modal window is one that blocks input to other windows. # So We need have still access to scene. win = parent.window() win.menuBar().setEnabled(False) win.treeWidget.setEnabled(False)
def __init__(self, parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - 100 if parent else 1 self.setGeometry(x, 1, 200, 100) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._page = QLabel() self._thumbnail = QGraphicsPixmapItem() self._step = QLabel() self._warning = QLabel() view = QGraphicsView(QGraphicsScene(0, 0, 64, 64), self) view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.scene().addItem(self._thumbnail) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap( QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL('clicked()'), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap( QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip( self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL('clicked()'), self.close) self._page.setFont(serifFont) self._step.setFont(serifFont) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addWidget(self._page) content.addWidget(view) content.addWidget(self._step) grid = QGridLayout() grid.addLayout(actions, 1, 0, Qt.AlignTop) grid.addLayout(content, 1, 1, Qt.AlignLeft) grid.addWidget(self._warning, 2, 1, Qt.AlignHCenter) self.setLayout(grid)
def __init__(self, parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - 100 if parent else 1 self.setGeometry(x, 1, 200, 100) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._page = QLabel() self._thumbnail = QGraphicsPixmapItem() self._step = QLabel() self._warning = QLabel() view = QGraphicsView(QGraphicsScene(0, 0, 64, 64), self) view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.scene().addItem(self._thumbnail) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap(QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL("clicked()"), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap(QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip(self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL("clicked()"), self.close) self._page.setFont(serifFont) self._step.setFont(serifFont) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addWidget(self._page) content.addWidget(view) content.addWidget(self._step) grid = QGridLayout() grid.addLayout(actions, 1, 0, Qt.AlignTop) grid.addLayout(content, 1, 1, Qt.AlignLeft) grid.addWidget(self._warning, 2, 1, Qt.AlignHCenter) self.setLayout(grid)
class MessageDlg(QWidget): def __init__(self, parent=None, initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width() / 2 if parent else 1 self.setGeometry(x, 1, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon( QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2, SIGNAL("clicked()"), self.close) hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 1, Qt.AlignLeft) hbox.addLayout(self.centreLayout, 1) hbox.addSpacing(5) hbox.addWidget(self.button1) hbox.addWidget(button2) self.setLayout(hbox) def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black), Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): self.emit(SIGNAL('finished(int)'), event.type()) return QWidget.closeEvent(self, event) def setText(self, text): self._message.setText(text) def setAcceptAction(self, callable): self.button1.setPixmap(QCommonStyle().standardIcon( QStyle.SP_DialogApplyButton).pixmap(16, 16)) self.connect(self.button1, SIGNAL("clicked()"), callable)
class MessageDlg(QWidget): def __init__(self ,parent=None ,initSize=QSize(400,40)): QWidget.__init__(self,parent,Qt.SubWindow) x = parent.width()/2 -initSize.width()/2 if parent else 1 self.setGeometry(x,1,initSize.width(),initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32,32)) self._message = QLabel() self.centreLayout = QHBoxLayout() self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap( QCommonStyle().standardIcon (QStyle.SP_BrowserStop).pixmap(16,16) ) self.connect(button2 ,SIGNAL("clicked()") ,self.close) hbox= QHBoxLayout() hbox.addWidget(self._icon,0,Qt.AlignTop) hbox.addWidget(self._message,1,Qt.AlignLeft) hbox.addLayout(self.centreLayout ,1) hbox.addSpacing(5) hbox.addWidget(self.button1) hbox.addWidget(button2) self.setLayout(hbox) def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black) ,Qt.Dense6Pattern ), 2.0) p.setPen(p_new) p.drawRect( QRectF(1, 1, self.width() -2, self.height() -2) ) p.setPen(p_old) def closeEvent(self, event): self.emit(SIGNAL('finished(int)') ,event.type()) return QWidget.closeEvent(self, event) def setText(self,text): self._message.setText(text) def setAcceptAction(self, callable): self.button1.setPixmap( QCommonStyle().standardIcon (QStyle.SP_DialogApplyButton).pixmap(16,16) ) self.connect(self.button1 ,SIGNAL("clicked()") ,callable)
def __init__(self , parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - self._thumbSize*6 if parent else 10 self.setGeometry(x, 10, self._thumbSize*6, self._thumbSize*2) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None self.partList = [] warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._thumbnailbox = QHBoxLayout() self._warning = QLabel("") self._worker = LicWorker() self._thumbnailbox.setSpacing(0) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap (QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL('clicked()'), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap (QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip(self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL('clicked()'), self.close) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addLayout(actions) content.addLayout(self._thumbnailbox) box = QVBoxLayout() box.addLayout(content) box.addWidget(self._warning) self.setLayout(box)
def __init__(self, parent=None, initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width() / 2 if parent else 1 self.setGeometry(x, 1, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon( QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2, SIGNAL("clicked()"), self.close) hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 1, Qt.AlignLeft) hbox.addLayout(self.centreLayout, 1) hbox.addSpacing(5) hbox.addWidget(self.button1) hbox.addWidget(button2) self.setLayout(hbox)
def __init__(self ,parent=None ,initSize=QSize(400,40)): QWidget.__init__(self,parent,Qt.SubWindow) x = parent.width()/2 -initSize.width()/2 if parent else 1 self.setGeometry(x,1,initSize.width(),initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32,32)) self._message = QLabel() self.centreLayout = QHBoxLayout() self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap( QCommonStyle().standardIcon (QStyle.SP_BrowserStop).pixmap(16,16) ) self.connect(button2 ,SIGNAL("clicked()") ,self.close) hbox= QHBoxLayout() hbox.addWidget(self._icon,0,Qt.AlignTop) hbox.addWidget(self._message,1,Qt.AlignLeft) hbox.addLayout(self.centreLayout ,1) hbox.addSpacing(5) hbox.addWidget(self.button1) hbox.addWidget(button2) self.setLayout(hbox)
def __init__(self, parent=None, initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width( ) / 2 if parent else self.minXpos self.setGeometry(x if x > self.minXpos else self.minXpos, self.minXpos, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() # SubWindow do not have title bar with buttons # Thanks to button1 widget user can close MessageDlg instance manually self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon( QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2, SIGNAL("clicked()"), self.close) # create main UI hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 0, Qt.AlignLeft) hbox.addSpacing(10) hbox.addLayout(self.centreLayout, 1) hbox.addSpacing(10) hbox.addWidget(self.button1, 0, Qt.AlignTop) hbox.addWidget(button2, 0, Qt.AlignTop) self.setLayout(hbox) # If this widgets will be hide, then we can using more space for content of centreLayout # or another widget, when we do not have button1 or _message is empty self._message.hide() self.button1.hide()
class MessageDlg(QWidget): minXpos = 10 def __init__(self, parent=None, initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width( ) / 2 if parent else self.minXpos self.setGeometry(x if x > self.minXpos else self.minXpos, self.minXpos, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() # SubWindow do not have title bar with buttons # Thanks to button1 widget user can close MessageDlg instance manually self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon( QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2, SIGNAL("clicked()"), self.close) # create main UI hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 0, Qt.AlignLeft) hbox.addSpacing(10) hbox.addLayout(self.centreLayout, 1) hbox.addSpacing(10) hbox.addWidget(self.button1, 0, Qt.AlignTop) hbox.addWidget(button2, 0, Qt.AlignTop) self.setLayout(hbox) # If this widgets will be hide, then we can using more space for content of centreLayout # or another widget, when we do not have button1 or _message is empty self._message.hide() self.button1.hide() def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black), Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): self.emit(SIGNAL('finished(int)'), event.type()) return QWidget.closeEvent(self, event) def setText(self, text): # put text and show widget self._message.setText(text) self._message.show() # refresh window QCoreApplication.processEvents() def releaseText(self): self._message.clear() self._message.hide() def setAcceptAction(self, fn): self.button1.setPixmap(QCommonStyle().standardIcon( QStyle.SP_DialogApplyButton).pixmap(16, 16)) self.button1.show() self.connect(self.button1, SIGNAL("clicked()"), fn)
class LicPlacementAssistant(QWidget): _buttonTip = { QStyle.SP_DialogCancelButton : "Release this item and close window" ,QStyle.SP_DialogApplyButton : "Put this item on scene" } _noSpecialPage = "You can not add parts here" _noPLIText = "non a PLI" _noMoveText = "You're still on the same page" _noEmptyText = "Page can not be empty" _lockedPageText = "Stuff is locked on this page" _processingText = "Processing..." _noMoreParts = "You can not add more parts to transport" _maxQuantity = 3 _thumbSize = 48 def __init__(self , parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - self._thumbSize*6 if parent else 10 self.setGeometry(x, 10, self._thumbSize*6, self._thumbSize*2) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None self.partList = [] warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._thumbnailbox = QHBoxLayout() self._warning = QLabel("") self._worker = LicWorker() self._thumbnailbox.setSpacing(0) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap (QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL('clicked()'), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap (QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip(self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL('clicked()'), self.close) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addLayout(actions) content.addLayout(self._thumbnailbox) box = QVBoxLayout() box.addLayout(content) box.addWidget(self._warning) self.setLayout(box) def moveItemToStep(self): self._warning.clear() if self._item is not None: self.scene = self._item.scene() srcPage = self._item.getStep().parentItem() try: self.destItem = self.scene.selectedItems()[0] except IndexError: self.destItem = None # Find Step assigned to currently selected item if self.destItem and self.destItem.__class__.__name__ != "Page": while self.destItem and not isinstance(self.destItem, Step): try: self.destItem = self.destItem.parent() except: break # Convert Page to first step on the list if self.destItem and self.destItem.__class__.__name__ == "Page": if srcPage.number == self.destItem.number: self._warning.setText(self._noMoveText) else: if self.destItem.steps: self.destItem = self.destItem.steps[0] else: self._warning.setText(self._noEmptyText) # Find the selected item's parent page, then flip to that page # Move Part into Step canMove = True message = "" if isinstance(self.destItem, Step): destPage = self.destItem.parentItem() if srcPage.number == destPage.number: canMove = False message = self._noMoveText if destPage.isLocked(): canMove = False message = self._lockedPageText if destPage.isEmpty(): canMove = False message = self._noEmptyText if destPage.__class__.__name__.lower() in ['titlepage' , 'templatepage' ,'partlistpage']: canMove = False message = self._noSpecialPage if canMove: self._worker.start([self.job_1S , self.job_2 , self.job_3]) if message: self._warning.setText(string.rjust(message, self._thumbSize)) def setItemtoMove(self , part=None): self.destItem = None self._item = part step = part while step and not isinstance(step, Step): step = step.parent() self._warning.clear() if not self.isVisible(): self.show() if part: try: index = self.partList.index(part, ) except ValueError: index = -1 if self.partList.__len__() >= self._maxQuantity: self._warning.setText(self._noMoreParts) return elif index == -1: self.partList.append(part) pItem = None if step and step.hasPLI(): for pliItem in step.pli.pliItems: if pliItem.abstractPart.filename == part.abstractPart.filename: pItem = pliItem break sRect = QRect(0, 0, self._thumbSize *1.2, self._thumbSize *1.2) image = QImage(sRect.size()) if isinstance(pItem, (Part, PLIItem)): a = pItem.abstractPart filename = os.path.splitext(a.filename)[0] + ".png" filepath = os.path.join(config.partsCachePath() ,filename ).lower() if os.path.exists(filepath): image.load(filepath,"LA") else: writeLogEntry("FileNotFoundError: [Errno 2] No such file or directory: '%s'" % filename) image = QImage(":/no_pli") else: image = QImage(":/no_pli") # propagate to display image = image.scaledToHeight(sRect.height(), Qt.SmoothTransformation) image = image.copy(sRect) # add to layout thumb = QLabel() thumb.setGeometry(sRect); thumb.setPixmap(QPixmap.fromImage(image)) self._thumbnailbox.addWidget(thumb) def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black) , Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): # clear all widgets in a layout for i in reversed(range(self._thumbnailbox.count())): self._thumbnailbox.itemAt(i).widget().deleteLater() # restore cursor self.window().setCursor(Qt.ArrowCursor) # reset variables self.destItem = None self.partList = [] # take action return QWidget.closeEvent(self, event) def job_1S(self): if self.destItem: self._warning.setText( string.rjust(self._processingText, self._thumbSize) ) self.window().setCursor(Qt.WaitCursor) self.scene.setFocus(Qt.MouseFocusReason) self.scene.setFocusItem(self.destItem , Qt.MouseFocusReason) self.destItem.setSelected(True) def job_2(self): if self.destItem: self.scene.undoStack.push(MovePartsToStepCommand(self.partList, self.destItem)) def job_3(self): self.close()
def __init__(self, parent, originalRect, absolutePos): QDialog.__init__(self, parent, Qt.CustomizeWindowHint | Qt.WindowTitleHint) self.setAttribute(Qt.WA_DeleteOnClose) self.setWindowTitle("Adjust View") self.originalRect = originalRect self.startPos = absolutePos buttonBox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal) self.widthSpinBox = self.makeSpinBox(originalRect.width(), self._minValue, 1001.0, None) self.heightSpinBox = self.makeSpinBox(originalRect.height(), self._minValue, 1001.0, None) self.xyzWidget = XYZWidget(None, -1000, 1000, originalRect.x(), originalRect.y(), 0.0) self.conn = ExtendedLabel(self) self.conn.setSwitchablePixmap(QPixmap(":/link_break"), QPixmap(":/link")) self.conn.setPixmap(QPixmap(":/link_break")) find = ExtendedLabel(self) find.setPixmap(QPixmap(":/find")) grid = QGridLayout() grid.addWidget(QLabel("W:"), 1, 1, Qt.AlignRight) grid.addWidget(self.widthSpinBox, 1, 2) grid.addWidget(QLabel("H:"), 2, 1, Qt.AlignRight) grid.addWidget(self.heightSpinBox, 2, 2) hbox = QHBoxLayout() hbox.addLayout(grid, 1) hbox.addWidget(self.conn) hbox.addSpacing(15) master = QGridLayout() master.addLayout(hbox, 1, 1, Qt.AlignHCenter) master.addWidget(self.xyzWidget, 2, 1) master.addWidget(find, 2, 2, Qt.AlignTop) master.addWidget(buttonBox, 3, 0, 1, 3) self.setLayout(master) self.move(parent.window().pos().x(), parent.mapToGlobal(parent.pos()).y()) self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) self.connect(find, SIGNAL('clicked()'), self.findPointSignal) self.connect(self.widthSpinBox, SIGNAL("valueChanged(int)"), self.changeWidth) self.connect(self.heightSpinBox, SIGNAL("valueChanged(int)"), self.changeHeight) self.connect(self.xyzWidget.xSpinBox, SIGNAL("valueChanged(int)"), self.change) self.connect(self.xyzWidget.ySpinBox, SIGNAL("valueChanged(int)"), self.change) self.xyzWidget.zSpinBox.setEnabled(False) self.widthSpinBox.selectAll() self.heightSpinBox.selectAll() # Can not use setModal or setWindowModality. Because a modal window is one that blocks input to other windows. # So We need have still access to scene. win = parent.window() win.menuBar().setEnabled(False) win.treeWidget.setEnabled(False)
class AdjustAreaDialog(QDialog): _minValue = 10 _dialog = None def __init__(self, parent, originalRect, absolutePos): QDialog.__init__(self, parent, Qt.CustomizeWindowHint | Qt.WindowTitleHint) self.setAttribute(Qt.WA_DeleteOnClose) self.setWindowTitle("Adjust View") self.originalRect = originalRect self.startPos = absolutePos buttonBox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal) self.widthSpinBox = self.makeSpinBox(originalRect.width(), self._minValue, 1001.0, None) self.heightSpinBox = self.makeSpinBox(originalRect.height(), self._minValue, 1001.0, None) self.xyzWidget = XYZWidget(None, -1000, 1000, originalRect.x(), originalRect.y(), 0.0) self.conn = ExtendedLabel(self) self.conn.setSwitchablePixmap(QPixmap(":/link_break"), QPixmap(":/link")) self.conn.setPixmap(QPixmap(":/link_break")) find = ExtendedLabel(self) find.setPixmap(QPixmap(":/find")) grid = QGridLayout() grid.addWidget(QLabel("W:"), 1, 1, Qt.AlignRight) grid.addWidget(self.widthSpinBox, 1, 2) grid.addWidget(QLabel("H:"), 2, 1, Qt.AlignRight) grid.addWidget(self.heightSpinBox, 2, 2) hbox = QHBoxLayout() hbox.addLayout(grid, 1) hbox.addWidget(self.conn) hbox.addSpacing(15) master = QGridLayout() master.addLayout(hbox, 1, 1, Qt.AlignHCenter) master.addWidget(self.xyzWidget, 2, 1) master.addWidget(find, 2, 2, Qt.AlignTop) master.addWidget(buttonBox, 3, 0, 1, 3) self.setLayout(master) self.move(parent.window().pos().x(), parent.mapToGlobal(parent.pos()).y()) self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) self.connect(find, SIGNAL('clicked()'), self.findPointSignal) self.connect(self.widthSpinBox, SIGNAL("valueChanged(int)"), self.changeWidth) self.connect(self.heightSpinBox, SIGNAL("valueChanged(int)"), self.changeHeight) self.connect(self.xyzWidget.xSpinBox, SIGNAL("valueChanged(int)"), self.change) self.connect(self.xyzWidget.ySpinBox, SIGNAL("valueChanged(int)"), self.change) self.xyzWidget.zSpinBox.setEnabled(False) self.widthSpinBox.selectAll() self.heightSpinBox.selectAll() # Can not use setModal or setWindowModality. Because a modal window is one that blocks input to other windows. # So We need have still access to scene. win = parent.window() win.menuBar().setEnabled(False) win.treeWidget.setEnabled(False) def hideEvent(self, *args, **kwargs): scene = self.parent().scene() win = self.parent().window() if self._dialog: self._dialog.close() self.discard() win.menuBar().setEnabled(True) win.treeWidget.setEnabled(True) return QDialog.hideEvent(self, *args, **kwargs) def findPointSignal(self): view = self.parent() self._dialog = MessageDlg(view) self._dialog.setText("Choose the point on scene.") self._dialog.show() # Inform QGraphicsScene to doing nothing ,except return mouse cursor coordinates view.scene().catchTheMouse = True self.connect(view.scene(), SIGNAL("sceneClick"), self.findPoint) self.connect(self._dialog, SIGNAL("finished(int)"), self.discard) def findPoint(self, event): sp = self.startPos ptF = event.scenePos() x = ptF.x() - sp.x() y = ptF.y() - sp.y() self.xyzWidget.xSpinBox.setValue(x) self.xyzWidget.ySpinBox.setValue(y) self._dialog.close() def changeWidth(self): if self.conn.switched: self.heightSpinBox.setValue(self.widthSpinBox.value()) self.change() def changeHeight(self): if self.conn.switched: self.widthSpinBox.setValue(self.heightSpinBox.value()) self.change() def discard(self): scene = self.parent().scene() # This is necessary to not run into TypeError: findPoint() takes exactly 2 arguments (1 given) self.disconnect(scene, SIGNAL("sceneClick"), self.findPoint) scene.catchTheMouse = False def change(self): wt = self.widthSpinBox.value() ht = self.heightSpinBox.value() if wt >= self._minValue and ht >= self._minValue: newSize = QSize(wt, ht) newPoint = QPoint(self.xyzWidget.xyz()[0], self.xyzWidget.xyz()[1]) newRect = QRect(newPoint, newSize) if newSize.isValid() and newRect.isValid(): self.emit(SIGNAL("changeRect"), newRect) def accept(self): self.change() self.parent().window().setWindowModified(True) QDialog.accept(self) def reject(self): self.emit(SIGNAL("changeRect"), self.originalRect) QDialog.reject(self)
class LicPlacementAssistant(QWidget): _buttonTip = { QStyle.SP_DialogCancelButton: "Release this item and close window", QStyle.SP_DialogApplyButton: "Put this item on scene" } _noPLIText = "non a PLI" _noMoveText = "You're still on the same page" _noBlankText = "Page or Step can not be blank" _lockedPageText = "Stuff is locked on this page" _processingText = "Processing..." def __init__(self, parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - 100 if parent else 1 self.setGeometry(x, 1, 200, 100) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._page = QLabel() self._thumbnail = QGraphicsPixmapItem() self._step = QLabel() self._warning = QLabel() view = QGraphicsView(QGraphicsScene(0, 0, 64, 64), self) view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.scene().addItem(self._thumbnail) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap( QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL('clicked()'), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap( QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip( self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL('clicked()'), self.close) self._page.setFont(serifFont) self._step.setFont(serifFont) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addWidget(self._page) content.addWidget(view) content.addWidget(self._step) grid = QGridLayout() grid.addLayout(actions, 1, 0, Qt.AlignTop) grid.addLayout(content, 1, 1, Qt.AlignLeft) grid.addWidget(self._warning, 2, 1, Qt.AlignHCenter) self.setLayout(grid) def moveItemToStep(self): self._warning.clear() if self._item is not None: self.scene = self._item.scene() srcPage = self._item.getStep().parentItem() try: self.destItem = self.scene.selectedItems()[0] except IndexError: self.destItem = None # Find Step assigned to currently selected item if self.destItem and self.destItem.__class__.__name__ != "Page": while self.destItem and not isinstance(self.destItem, Step): try: self.destItem = self.destItem.parent() except: break # Convert Page to first step on the list if self.destItem and self.destItem.__class__.__name__ == "Page": if srcPage.number == self.destItem.number: self._warning.setText(self._noMoveText) else: self.destItem = self.destItem.steps[0] # Find the selected item's parent page, then flip to that page # Move Part into Step canMove = True if isinstance(self.destItem, Step): destPage = self.destItem.parentItem() if srcPage.number == destPage.number: canMove = False self._warning.setText(self._noMoveText) if destPage.isLocked(): canMove = False self._warning.setText(self._lockedPageText) if destPage.isEmpty(): canMove = False self._warning.setText(self._noBlankText) if canMove: self._worker = LicWorker( [self.job_1S, self.job_2, self.job_3]) self._worker.start() def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black), Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): self.window().setCursor(Qt.ArrowCursor) self.destItem = None return QWidget.closeEvent(self, event) def job_1S(self): if self.destItem: self._warning.setText(self._processingText) self.window().setCursor(Qt.WaitCursor) self.scene.setFocus(Qt.MouseFocusReason) self.scene.setFocusItem(self.destItem, Qt.MouseFocusReason) self.destItem.setSelected(True) def job_2(self): if self.destItem: self.scene.undoStack.push( MovePartsToStepCommand([self._item], self.destItem)) def job_3(self): self.close() def setItemtoMove(self, part=None): self.destItem = None self._item = part step = part while step and not isinstance(step, Step): step = step.parent() self._step.setText(step.data(Qt.DisplayRole)) self._page.setText(step.parentItem().data(Qt.DisplayRole)) self._warning.clear() if part: pItem = None if step and step.hasPLI(): for pliItem in step.pli.pliItems: if pliItem.abstractPart.filename == part.abstractPart.filename: pItem = pliItem break sRect = self._thumbnail.scene().sceneRect() if isinstance(pItem, (Part, PLIItem)): a = pItem.abstractPart filename = os.path.join( config.grayscalePath(), os.path.splitext(a.filename)[0] + ".png").lower() if not os.path.exists(filename): pRect = pItem.sceneBoundingRect().toRect() wt = Page.PageSize.width() ht = Page.PageSize.height() mx = int(PLI.margin.x() / 2) bufferManager = LicGLHelpers.FrameBufferManager(wt, ht) try: bufferManager.bindMSFB() LicGLHelpers.initFreshContext(True) step.parentItem().drawGLItemsOffscreen( QRectF(0, 0, wt, ht), 1.0) bufferManager.blitMSFB() temp_data = bufferManager.readFB() temp_cord = (pRect.left() - mx, pRect.top() - mx, a.width + pRect.left() + mx, a.height + pRect.top() + mx) temp_name = tempfile.TemporaryFile( ).name + ".png".lower() temp = Image.fromstring("RGBA", (wt, ht), temp_data) temp = temp.transpose(Image.FLIP_TOP_BOTTOM) temp = temp.crop(temp_cord) temp.save(temp_name) finally: image = QImage(temp_name, "LA") #convertToGrayscale for i in range(0, image.width()): for j in range(0, image.height()): pix = image.pixel(i, j) if pix > 0: color = qGray(pix) image.setPixel(i, j, qRgb(color, color, color)) #saveResult image.save(filename, "PNG") #cleanUp bufferManager.cleanup() os.remove(temp_name) else: image = QImage(filename, "LA") image = image.scaledToHeight(sRect.height(), Qt.SmoothTransformation) else: image = QImage(sRect.width(), sRect.height(), QImage.Format_Mono) painter = QPainter(image) painter.fillRect(sRect, Qt.white) painter.setFont(QFont("Helvetica [Cronyx]", 10, QFont.Bold)) painter.drawLine(1, 1, sRect.width() - 1, sRect.height() - 1) painter.drawLine(sRect.width() - 1, 1, 1, sRect.height() - 1) painter.drawText(sRect, Qt.TextSingleLine | Qt.AlignVCenter, self._noPLIText) painter.end() self._thumbnail.setPixmap(QPixmap.fromImage(image)) if not self.isVisible(): self.show()
class MessageDlg(QWidget): def __init__(self , parent=None , initSize=QSize(400, 40)): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - initSize.width() / 2 if parent else 10 self.setGeometry(x, 10, initSize.width(), initSize.height()) self.setBackgroundRole(QPalette.Base) self._icon = QLabel() self._icon.setPixmap(QIcon(":/lic_logo").pixmap(32, 32)) self._message = QLabel() self.centreLayout = QHBoxLayout() # SubWindow do not have title bar with buttons # Thanks to button1 widget user can close MessageDlg instance manually self.button1 = ExtendedLabel() button2 = ExtendedLabel() button2.setPixmap(QCommonStyle().standardIcon (QStyle.SP_BrowserStop).pixmap(16, 16)) self.connect(button2 , SIGNAL("clicked()") , self.close) # create main UI hbox = QHBoxLayout() hbox.addWidget(self._icon, 0, Qt.AlignTop) hbox.addWidget(self._message, 1, Qt.AlignLeft) hbox.addSpacing(10) hbox.addLayout(self.centreLayout , 1) hbox.addSpacing(10) hbox.addWidget(self.button1, 0, Qt.AlignTop) hbox.addWidget(button2, 0, Qt.AlignTop) self.setLayout(hbox) # If this widgets will be hide, then we can using more space for content of centreLayout # or another widget, when we do not have button1 or _message is empty self._message.hide() self.button1.hide() def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black) , Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): self.emit(SIGNAL('finished(int)') , event.type()) return QWidget.closeEvent(self, event) def setText(self, text): # put text and show widget self._message.setText(text) self._message.show() # refresh window QCoreApplication.processEvents() def releaseText(self): self._message.clear() self._message.hide() def setAcceptAction(self, fn): self.button1.setPixmap(QCommonStyle().standardIcon (QStyle.SP_DialogApplyButton).pixmap(16, 16)) self.button1.show() self.connect(self.button1 , SIGNAL("clicked()") , fn)
class LicPlacementAssistant(QWidget): _buttonTip = { QStyle.SP_DialogCancelButton: "Release this item and close window", QStyle.SP_DialogApplyButton: "Put this item on scene", } _noPLIText = "non a PLI" _noMoveText = "You're still on the same page" _noBlankText = "Page or Step can not be blank" _lockedPageText = "Stuff is locked on this page" _processingText = "Processing..." def __init__(self, parent=None): QWidget.__init__(self, parent, Qt.SubWindow) x = parent.width() / 2 - 100 if parent else 1 self.setGeometry(x, 1, 200, 100) self.setBackgroundRole(QPalette.Base) self._item = None self.destItem = None warningFont = QFont("Times", 9) serifFont = QFont("Times", 12, QFont.Bold) serifFont.setCapitalization(QFont.SmallCaps) self._page = QLabel() self._thumbnail = QGraphicsPixmapItem() self._step = QLabel() self._warning = QLabel() view = QGraphicsView(QGraphicsScene(0, 0, 64, 64), self) view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) view.scene().addItem(self._thumbnail) self._apply = ExtendedLabel() self._apply.setPixmap(QCommonStyle().standardPixmap(QStyle.SP_DialogApplyButton)) self._apply.setStatusTip(self._buttonTip[QStyle.SP_DialogApplyButton]) self.connect(self._apply, SIGNAL("clicked()"), self.moveItemToStep) self._cancel = ExtendedLabel() self._cancel.move(0, 0) self._cancel.setPixmap(QCommonStyle().standardPixmap(QStyle.SP_DialogCancelButton)) self._cancel.setStatusTip(self._buttonTip[QStyle.SP_DialogCancelButton]) self.connect(self._cancel, SIGNAL("clicked()"), self.close) self._page.setFont(serifFont) self._step.setFont(serifFont) self._warning.setFont(warningFont) self._warning.setStyleSheet("QLabel { color : red; }") actions = QVBoxLayout() actions.addWidget(self._cancel) actions.addWidget(self._apply) content = QHBoxLayout() content.addWidget(self._page) content.addWidget(view) content.addWidget(self._step) grid = QGridLayout() grid.addLayout(actions, 1, 0, Qt.AlignTop) grid.addLayout(content, 1, 1, Qt.AlignLeft) grid.addWidget(self._warning, 2, 1, Qt.AlignHCenter) self.setLayout(grid) def moveItemToStep(self): self._warning.clear() if self._item is not None: self.scene = self._item.scene() srcPage = self._item.getStep().parentItem() try: self.destItem = self.scene.selectedItems()[0] except IndexError: self.destItem = None # Find Step assigned to currently selected item if self.destItem and self.destItem.__class__.__name__ != "Page": while self.destItem and not isinstance(self.destItem, Step): try: self.destItem = self.destItem.parent() except: break # Convert Page to first step on the list if self.destItem and self.destItem.__class__.__name__ == "Page": if srcPage.number == self.destItem.number: self._warning.setText(self._noMoveText) else: self.destItem = self.destItem.steps[0] # Find the selected item's parent page, then flip to that page # Move Part into Step canMove = True if isinstance(self.destItem, Step): destPage = self.destItem.parentItem() if srcPage.number == destPage.number: canMove = False self._warning.setText(self._noMoveText) if destPage.isLocked(): canMove = False self._warning.setText(self._lockedPageText) if destPage.isEmpty(): canMove = False self._warning.setText(self._noBlankText) if canMove: self._worker = LicWorker([self.job_1S, self.job_2, self.job_3]) self._worker.start() def paintEvent(self, event): # prepare canvas p = QPainter(self) p.fillRect(self.rect(), QColor(LicHelpers.SUBWINDOW_BACKGROUND)) # draw border p_old = p.pen() p_new = QPen(QBrush(QColor(Qt.black), Qt.Dense6Pattern), 2.0) p.setPen(p_new) p.drawRect(QRectF(1, 1, self.width() - 2, self.height() - 2)) p.setPen(p_old) def closeEvent(self, event): self.window().setCursor(Qt.ArrowCursor) self.destItem = None return QWidget.closeEvent(self, event) def job_1S(self): if self.destItem: self._warning.setText(self._processingText) self.window().setCursor(Qt.WaitCursor) self.scene.setFocus(Qt.MouseFocusReason) self.scene.setFocusItem(self.destItem, Qt.MouseFocusReason) self.destItem.setSelected(True) def job_2(self): if self.destItem: self.scene.undoStack.push(MovePartsToStepCommand([self._item], self.destItem)) def job_3(self): self.close() def setItemtoMove(self, part=None): self.destItem = None self._item = part step = part while step and not isinstance(step, Step): step = step.parent() self._step.setText(step.data(Qt.DisplayRole)) self._page.setText(step.parentItem().data(Qt.DisplayRole)) self._warning.clear() if part: pItem = None if step and step.hasPLI(): for pliItem in step.pli.pliItems: if pliItem.abstractPart.filename == part.abstractPart.filename: pItem = pliItem break sRect = self._thumbnail.scene().sceneRect() if isinstance(pItem, (Part, PLIItem)): a = pItem.abstractPart filename = os.path.join(config.grayscalePath(), os.path.splitext(a.filename)[0] + ".png").lower() if not os.path.exists(filename): pRect = pItem.sceneBoundingRect().toRect() wt = Page.PageSize.width() ht = Page.PageSize.height() mx = int(PLI.margin.x() / 2) bufferManager = LicGLHelpers.FrameBufferManager(wt, ht) try: bufferManager.bindMSFB() LicGLHelpers.initFreshContext(True) step.parentItem().drawGLItemsOffscreen(QRectF(0, 0, wt, ht), 1.0) bufferManager.blitMSFB() temp_data = bufferManager.readFB() temp_cord = ( pRect.left() - mx, pRect.top() - mx, a.width + pRect.left() + mx, a.height + pRect.top() + mx, ) temp_name = tempfile.TemporaryFile().name + ".png".lower() temp = Image.fromstring("RGBA", (wt, ht), temp_data) temp = temp.transpose(Image.FLIP_TOP_BOTTOM) temp = temp.crop(temp_cord) temp.save(temp_name) finally: image = QImage(temp_name, "LA") # convertToGrayscale for i in range(0, image.width()): for j in range(0, image.height()): pix = image.pixel(i, j) if pix > 0: color = qGray(pix) image.setPixel(i, j, qRgb(color, color, color)) # saveResult image.save(filename, "PNG") # cleanUp bufferManager.cleanup() os.remove(temp_name) else: image = QImage(filename, "LA") image = image.scaledToHeight(sRect.height(), Qt.SmoothTransformation) else: image = QImage(sRect.width(), sRect.height(), QImage.Format_Mono) painter = QPainter(image) painter.fillRect(sRect, Qt.white) painter.setFont(QFont("Helvetica [Cronyx]", 10, QFont.Bold)) painter.drawLine(1, 1, sRect.width() - 1, sRect.height() - 1) painter.drawLine(sRect.width() - 1, 1, 1, sRect.height() - 1) painter.drawText(sRect, Qt.TextSingleLine | Qt.AlignVCenter, self._noPLIText) painter.end() self._thumbnail.setPixmap(QPixmap.fromImage(image)) if not self.isVisible(): self.show()
class AdjustAreaDialog(QDialog): _minValue = 10 _dialog = None def __init__(self, parent, originalRect, absolutePos): QDialog.__init__(self, parent, Qt.CustomizeWindowHint | Qt.WindowTitleHint) self.setAttribute(Qt.WA_DeleteOnClose) self.setWindowTitle("Adjust View") self.originalRect = originalRect self.startPos = absolutePos buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal) self.widthSpinBox = self.makeSpinBox(originalRect.width(), self._minValue, 1001.0, None) self.heightSpinBox = self.makeSpinBox(originalRect.height(), self._minValue, 1001.0, None) self.xyzWidget = XYZWidget(None, -1000, 1000, originalRect.x(), originalRect.y(), 0.0) self.conn = ExtendedLabel(self) self.conn.setSwitchablePixmap(QPixmap(":/link_break"),QPixmap(":/link")) self.conn.setPixmap(QPixmap(":/link_break")) find = ExtendedLabel(self) find.setPixmap(QPixmap(":/find")) grid = QGridLayout() grid.addWidget(QLabel("W:"),1,1,Qt.AlignRight) grid.addWidget(self.widthSpinBox,1,2) grid.addWidget(QLabel("H:"),2,1,Qt.AlignRight) grid.addWidget(self.heightSpinBox,2,2) hbox = QHBoxLayout() hbox.addLayout(grid,1) hbox.addWidget(self.conn) hbox.addSpacing(15) master = QGridLayout() master.addLayout(hbox, 1, 1 ,Qt.AlignHCenter) master.addWidget(self.xyzWidget, 2, 1) master.addWidget(find, 2, 2 ,Qt.AlignTop) master.addWidget(buttonBox, 3, 0, 1, 3) self.setLayout(master) self.move( parent.window().pos().x() , parent.mapToGlobal(parent.pos()).y() ) self.connect(buttonBox, SIGNAL("accepted()"), self, SLOT("accept()")) self.connect(buttonBox, SIGNAL("rejected()"), self, SLOT("reject()")) self.connect(find, SIGNAL('clicked()'), self.findPointSignal) self.connect(self.widthSpinBox, SIGNAL("valueChanged(int)"), self.changeWidth) self.connect(self.heightSpinBox, SIGNAL("valueChanged(int)"), self.changeHeight) self.connect(self.xyzWidget.xSpinBox, SIGNAL("valueChanged(int)"), self.change) self.connect(self.xyzWidget.ySpinBox, SIGNAL("valueChanged(int)"), self.change) self.xyzWidget.zSpinBox.setEnabled(False) self.widthSpinBox.selectAll() self.heightSpinBox.selectAll() # Can not use setModal or setWindowModality. Because a modal window is one that blocks input to other windows. # So We need have still access to scene. win = parent.window() win.menuBar().setEnabled(False) win.treeWidget.setEnabled(False) def hideEvent(self, *args, **kwargs): scene = self.parent().scene() win = self.parent().window() if self._dialog: self._dialog.close() self.discard() win.menuBar().setEnabled(True) win.treeWidget.setEnabled(True) return QDialog.hideEvent(self, *args, **kwargs) def findPointSignal(self): view = self.parent() self._dialog = MessageDlg(view) self._dialog.setText("Choose the point on scene.") self._dialog.show() # Inform QGraphicsScene to doing nothing ,except return mouse cursor coordinates view.scene().catchTheMouse = True self.connect(view.scene() ,SIGNAL("sceneClick") ,self.findPoint) self.connect(self._dialog ,SIGNAL("finished(int)") ,self.discard) def findPoint(self ,event): sp = self.startPos ptF= event.scenePos() x = ptF.x() -sp.x() y = ptF.y() -sp.y() self.xyzWidget.xSpinBox.setValue(x) self.xyzWidget.ySpinBox.setValue(y) self._dialog.close() def changeWidth(self): if self.conn.switched: self.heightSpinBox.setValue(self.widthSpinBox.value()) self.change() def changeHeight(self): if self.conn.switched: self.widthSpinBox.setValue(self.heightSpinBox.value()) self.change() def discard(self): scene = self.parent().scene() # This is necessary to not run into TypeError: findPoint() takes exactly 2 arguments (1 given) self.disconnect(scene, SIGNAL("sceneClick") ,self.findPoint) scene.catchTheMouse = False def change(self): wt = self.widthSpinBox.value() ht = self.heightSpinBox.value() if wt >= self._minValue and ht >= self._minValue: newSize = QSize( wt , ht ) newPoint= QPoint( self.xyzWidget.xyz()[0] , self.xyzWidget.xyz()[1] ) newRect = QRect(newPoint,newSize) if newSize.isValid() and newRect.isValid(): self.emit(SIGNAL("changeRect"), newRect) def accept(self): self.change() self.parent().window().setWindowModified(True) QDialog.accept(self) def reject(self): self.emit(SIGNAL("changeRect"), self.originalRect) QDialog.reject(self)