import datetime import inspect import os from qtpy.QtCore import QTimer from qtpy.QtWidgets import (QApplication, QCheckBox, QDialog, QHBoxLayout, QPushButton, QVBoxLayout, QWidget) from pyqtacc.bdbase.enumkind import MsgSeverity _pymodule = os.path.basename(__file__) # macro def _line(): return inspect.currentframe().f_back.f_lineno class QScreenshot(QDialog): def __init__(self, parent, window_title: str = 'QScreenshot', screen_items: list = [], screen_titles: list = []): super(QScreenshot, self).__init__(parent) self.setWindowTitle(window_title) self.parent = parent self.screenshot_titles = screen_titles self.screenshot_items = screen_items #print(self.parent.appname) self.layout = QVBoxLayout(self) #elem1 = title + "_EntireWindow" #elem2 = title + "_CentralWidget" #elem1 = "EntireWindow" elem2 = "CentralWidget" #self.checkText = [elem1, elem2] + \ self.checkText = [elem2] + \ self.screenshot_titles #grphTitle # +parent.gui.titles self.checkBoxes = [] for i, t in enumerate(self.checkText): self.checkBoxes.append(QCheckBox(t)) self.checkBoxes[i].setObjectName(str(i-1)) # (i-len(self.checkText))) #(i-1)) #(i-2) self.layout.addWidget(self.checkBoxes[i]) btnLayout = QHBoxLayout() self.okBtn = QPushButton('OK') self.okBtn.clicked.connect(self.ok) self.cancelBtn = QPushButton('Cancel') self.cancelBtn.clicked.connect(self.close) btnLayout.addWidget(self.okBtn) btnLayout.addWidget(self.cancelBtn) self.layout.addLayout(btnLayout) #top_level_widgets = QApplication.allWidgets() self.show() def ok(self): self.close() self.parent.update() # not really required QTimer.singleShot(10, self.capture) # QtGui.QPixmap.grabWindow -> QtGui.QScreen.grabWindow # QtGui.QPixmap.grabWidget -> QtWidgets.QWidget.grab # QPixmap check for true=success or false=failure # Deprecated QPixmap replaced by QScreen in Qt 5 def capture(self): now = datetime.datetime.now() # '/afs/psi.ch/intranet/Controls/tmp/elog/ePic/SwissFEL+commissioning/' folderName = self.parent.settings.data["screenshot"][ "destination"] + self.parent.appname + "/" #elogDest if not os.path.exists(folderName): os.makedirs(folderName) maxI = [None] * len(self.checkBoxes) # Find MAXIMUM i for i in range(0, len(self.checkBoxes)): maxI[i] = 0 count = 0 for c in self.checkBoxes: n = int(c.objectName()) i = 0 #cname = socket.gethostname() #name = folderName+str(cname)+':screenshot_'+self.checkText[n+2]+'_' #name = folderName+str(cname)+':'+self.checkText[n+2]+'_' name = folderName+self.checkText[n+1]+'_' #n+2 while os.path.exists(name+str(i)+'.png'): i += 1 if i > maxI[count]: maxI[count] = i count = count + 1 maxII = 0 for i in range(0, len(maxI)): if maxI[i] > maxII: maxII = maxI[i] count = 0 print(self.checkBoxes) print(self.screenshot_items) for c in self.checkBoxes: if c.isChecked(): n = int(c.objectName()) name = folderName+self.checkText[n+1]+'_' #n+2 name += now.strftime("%Y-%m-%d_%H:%M:%S") + '.png' winid = self.parent.winId() # THis is the desktop windowid. i.e. the console window ##winid1 = QApplication.desktop().winId() #print(type(winid)) #print (" n == ", n) _save_success = False if n == -2: qss = QApplication.screens() qs = qss[len(qss)-1] if qs.grabWindow(winid).save(name, 'png', 100): _save_success = True elif n == -1: if QWidget.grab( self.parent.centralWidget()).save(name, 'png', 100): _save_success = True else: if QWidget.grab( self.screenshot_items[n]).save(name, 'png', 100): _save_success = True if not _save_success: self.parent.show_log_message( MsgSeverity.ERROR, _pymodule, _line(), "Failed to save image to:\nfile='{0}'".format(name)) self.parent.show_log_message( MsgSeverity.INFO, _pymodule, _line(), "Screenshot saved to:\nfile='{0}'".format(name)) self.parent.statusbar.showMessage( "Screenshot saved to: {0}".format(name)) count = count + 1 QApplication.processEvents()