def getMap(self, kode, kmapCache, bkode_bg=False): key = kode.toZ3a2s() + "-{}-{}".format(self.unitsize, self.fix.value) pixmap = kmapCache.find(key) if pixmap: return pixmap pixmap = QPixmap() pixmap.loadFromData(self.mapBytes(kode)) if not (bkode_bg) and isinstance(kode, (BKode, SKode)): pixmap.setMask(pixmap) kmapCache.insert(key, pixmap) return pixmap
def icon(self, name, extension='png', use_inheritance=True, allow_theme=True): """ Find an icon with the given ``name`` and return a :class:`~PySide.QtGui.QIcon` of that icon. If ``use_inheritance`` is True and this style doesn't have an icon with the given name, the icon will be searched for in the style this style inherits. If ``allow_theme`` is True and the icon can't be located in a style, it will be retrieved with :func:`PySide.QtGui.QIcon.fromTheme` as a last resort as long as the style allows the use of system icons. """ icon = None fn = '%s.%s' % (name, extension) path = profile.join('images', fn) if self.path_source != profile.SOURCE_PKG_RESOURCES: file = self.get_path(path, use_inheritance) if file and os.path.exists(file): icon = QIcon(file) else: if self.has_file(path, use_inheritance): f = self.get_file(path, use_inheritance=use_inheritance) if f: pixmap = QPixmap() pixmap.loadFromData(f.read()) icon = QIcon(pixmap) del pixmap f.close() if not icon and use_inheritance and self.inherits: icon = loaded_styles[self.inherits].icon(name, extension, use_inheritance, allow_theme) if not icon and allow_theme: if QIcon.hasThemeIcon(name): icon = QIcon.fromTheme(name) if not icon: icon = QIcon() return icon
def finished_request(self, reply): """Processes replies and dispatches results to requesters. This method will be invoked on each finished request, submitted by :py:meth:`.fetch_banner`. It then converts the loaded data into a banner and passes the result on to the model of the request's origin. :param reply: The network reply from a banner load request. :type reply: :class:`.QNetworkReply` """ pixmap = QPixmap() if reply.error() == QNetworkReply.NoError: image_bytes = reply.readAll() pixmap.loadFromData(image_bytes) else: pixmap.load(":/icons/image-missing.png") index = self._ready_signal.pop(reply.request().url().toString()) index.model().setData(index, pixmap, Qt.DecorationRole)
def icon(name, extension=None, style=None, use_inheritance=True, allow_theme=True, _always_return=True): """ Find an icon with the given ``name`` and ``extension`` and return a :class:`PySide.QtGui.QIcon` for that icon. ================ =========== ============ Argument Default Description ================ =========== ============ name The name of the icon to load. extension The desired filename extension of the icon to load. If this isn't set, a list of supported formats will be used. style The style to load the icon from. If this isn't set, the current style will be assumed. use_inheritance ``True`` Whether or not to search the parent style if the given style doesn't contain an icon. allow_theme ``True`` Whether or not to fall back on Qt icon themes if an icon cannot be found. ================ =========== ============ """ if style: if isinstance(style, basestring): style = addons.get('style', style) elif not isinstance(style, StyleInfo): raise TypeError("Can only activate StyleInfo instances!") else: style = _current_style # If we don't have a style, return a null icon now. if not style: return QIcon() # Right, time to find the icon. if isinstance(extension, (tuple, list)): extensions = extension elif extension: extensions = [extension] else: extensions = (str(ext) for ext in QImageReader.supportedImageFormats()) # Iteration powers, activate! for ext in extensions: filename = '%s.%s' % (name, ext) icon_path = path.join('images', filename) if style.path.exists(icon_path): # We've got it, but what is it? if (not isinstance(style.path_source, basestring) or style.path_source.startswith('py:')): # pkg_resource! Do things the fun and interesting way. with style.path.open(icon_path) as f: pixmap = QPixmap() pixmap.loadFromData(f.read()) return QIcon(pixmap) # Just a regular file. Open normally. return QIcon(style.path.abspath(icon_path)) # Still here? We didn't find our icon then. If we're inheriting, then call # icon again for the style we inherit from. if use_inheritance and style.inherits: for parent in style.inherits: result = icon(name, extension, parent, True, False, False) if result: return result # For one last try, see if we can use the theme icons. if allow_theme and QIcon.hasThemeIcon(name): return QIcon.fromTheme(name) # We don't have an icon. Return a null QIcon. if _always_return: return QIcon()
def switch_to_next_image(self): """ Takes next image from the list and displays it e.g. when sentence ends. """ self.change_subtitles() temptext = "" try: if not self.image_list.empty(): images = self.image_list.get() img1 = QPixmap() if len(images) == 1: self.image_holder1.setPixmap(None) img2 = QPixmap() img2.loadFromData(images[0]) self.image_holder2.setPixmap(img2) self.image_holder3.setPixmap(None) elif len(images) == 2: img1 = QPixmap() img1.loadFromData(images[1]) self.image_holder1.setPixmap(img1) self.image_holder2.setPixmap(None) img2 = QPixmap() img2.loadFromData(images[0]) self.image_holder3.setPixmap(img2) elif len(images) == 3: img1 = QPixmap() img1.loadFromData(images[2]) self.image_holder1.setPixmap(img1) img2 = QPixmap() img2.loadFromData(images[1]) self.image_holder2.setPixmap(img2) img3 = QPixmap() img3.loadFromData(images[0]) self.image_holder3.setPixmap(img3) except IndexError: # gets thrown if one sentence is told pass QtGui.QApplication.processEvents()
def getPixmap(raw_image_data): pixmap = QPixmap() pixmap.loadFromData(raw_image_data) return pixmap
def _set_poster_pixmap(self, poster): """Get poster pixmap""" pixmap = QPixmap() pixmap.loadFromData(poster) self.poster.setPixmap(pixmap)
def qpixmap_from_url(url): img_data = urllib.urlopen(url).read() itemicon = QPixmap() itemicon.loadFromData(img_data) return itemicon
def loadIcon(self): bytearr = QByteArray.fromBase64(self.i) pixmap = QPixmap() pixmap.loadFromData(bytearr) return QIcon(pixmap)
class HygieneReportPop(QDialog): """ The Report Form Handler """ def __init__(self, parent=None, code=None, table=None): super(HygieneReportPop, self).__init__() self.setup_pop() # do not change the order of these two function self.code = code self.parent_object = parent self.image_data = None self.process_override( table=table) # do not change the order of these two function if not code: self.update_button.setHidden(True) self.delete_button.setHidden(True) self.calculate_code() else: self.create_button.setHidden(True) self.update_data() def process_override(self, table): """ Function that assigns the table and tryton backend handler objects based on the tables :param table: the table name """ try: if table == 'pest_table': self.backend_handle = self.parent_object.pest self.table = self.parent_object.report_hyginepest_table elif table == 'water_table': self.backend_handle = self.parent_object.water self.table = self.parent_object.report_hyginewater_table elif table == 'health_table': self.backend_handle = self.parent_object.health self.table = self.parent_object.report_health_table except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error' def setup_pop(self): """ sets up the form. """ self.grid_layout = QGridLayout(self) self.label_1 = QLabel(self) self.grid_layout.addWidget(self.label_1, 0, 0, 1, 1) self.code_line = QLineEdit(self) self.code_line.setValidator(QIntValidator(0, 99999)) self.grid_layout.addWidget(self.code_line, 0, 1, 1, 1) self.label_2 = QLabel(self) self.grid_layout.addWidget(self.label_2, 1, 0, 1, 1) self.date_line = QDateEdit(self) self.date_line.setCalendarPopup(True) self.grid_layout.addWidget(self.date_line, 1, 1, 1, 1) self.label_3 = QLabel(self) self.grid_layout.addWidget(self.label_3, 2, 0, 1, 1) self.organization_line = QLineEdit(self) self.grid_layout.addWidget(self.organization_line, 2, 1, 1, 1) self.label_4 = QLabel(self) self.grid_layout.addWidget(self.label_4, 3, 0, 1, 1) self.test_line = QLineEdit(self) self.grid_layout.addWidget(self.test_line, 3, 1, 1, 1) self.label_5 = QLabel(self) self.grid_layout.addWidget(self.label_5, 4, 0, 1, 1) self.description_line = QLineEdit(self) self.grid_layout.addWidget(self.description_line, 4, 1, 1, 1) self.image_label = QLabel(self) self.pixmap = QPixmap(':/images/upload.png') # self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.FastTransformation) #not used self.image_label.setPixmap(self.pixmap) self.image_label.setScaledContents(True) self.grid_layout.addWidget(self.image_label, 0, 2, 5, 2) self.horizontal = QHBoxLayout() self.delete_button = QPushButton(self) self.horizontal.addWidget(self.delete_button) self.create_button = QPushButton(self) self.horizontal.addWidget(self.create_button) self.update_button = QPushButton(self) self.horizontal.addWidget(self.update_button) self.upload_button = QPushButton(self) self.horizontal.addWidget(self.upload_button) self.grid_layout.addLayout(self.horizontal, 5, 0, 1, 4) ### retanslate self.label_1.setText( QApplication.translate("MainWindow", "Code", None, QApplication.UnicodeUTF8)) self.label_2.setText( QApplication.translate("MainWindow", "Date", None, QApplication.UnicodeUTF8)) self.date_line.setDisplayFormat( QApplication.translate("MainWindow", "dd/MM/yyyy", None, QApplication.UnicodeUTF8)) self.label_3.setText( QApplication.translate("MainWindow", "Organization Name", None, QApplication.UnicodeUTF8)) self.label_4.setText( QApplication.translate("MainWindow", "Test", None, QApplication.UnicodeUTF8)) self.label_5.setText( QApplication.translate("MainWindow", "Description", None, QApplication.UnicodeUTF8)) self.delete_button.setText( QApplication.translate("MainWindow", "Delete", None, QApplication.UnicodeUTF8)) self.create_button.setText( QApplication.translate("MainWindow", "Create", None, QApplication.UnicodeUTF8)) self.update_button.setText( QApplication.translate("MainWindow", "Update", None, QApplication.UnicodeUTF8)) self.upload_button.setText( QApplication.translate("MainWindow", "Upload", None, QApplication.UnicodeUTF8)) self.create_button.clicked.connect(self.create_report) self.upload_button.clicked.connect(self.open_file) self.update_button.clicked.connect(self.update_report) self.delete_button.clicked.connect(self.delete_report) self.image_label.mouseReleaseEvent = self.image_viewer def update_data(self): """ Updates the data in the form """ try: data = self.backend_handle.read_report(code=int(self.code)) if data: if data[0]: data = data[1] self.code_line.setText(data['code']) self.code_line.setDisabled(True) self.date_line.setDate(data['date']) self.organization_line.setText(data['organization']) self.test_line.setText(data['test']) self.description_line.setText(data['description']) self.image_data = str(data['report']) self.pixmap = QPixmap() self.pixmap.loadFromData(self.image_data) self.image_label.setPixmap( self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.FastTransformation)) except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error' def image_viewer(self, event=None): """pops up a image viewer to check the details""" dialog = QDialog() dialog.setWindowFlags(Qt.WindowTitleHint | Qt.WindowStaysOnTopHint) layout = QHBoxLayout(dialog) label = QLabel(dialog) # self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.FastTransformation) label.setPixmap(self.pixmap) label.setScaledContents(True) layout.addWidget(label) dialog.exec_() def open_file(self): """ saves the file """ dialog = QFileDialog(self) dialog.setNameFilter("Image Files (*.png *.jpg *.bmp)") dialog.setStyleSheet('color:black;') name = '' if dialog.exec_(): name = dialog.selectedFiles() if name: self.image_data = open(name[0], 'rb').read() self.pixmap = QPixmap(name[0]) self.image_label.setPixmap( self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.FastTransformation)) def create_report(self): """ Creates a new Report """ try: data = self.get_data() if data: status = self.backend_handle.create_report(data=data) if status: if status[0]: QMessageBox.information(self, 'Success', status[1], QMessageBox.Ok) self.create_button.setHidden(True) self.delete_button.setVisible(True) self.update_button.setVisible(True) self.code = data['code'] self.update_data() else: QMessageBox.critical(self, 'Error', status[1], QMessageBox.Ok) except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error' def update_report(self): """ Updates an existing Report """ try: data = self.get_data() if data: status = self.backend_handle.update_report(data=data) if status: if status[0]: QMessageBox.information(self, 'Success', status[1], QMessageBox.Ok) else: QMessageBox.critical(self, 'Error', status[1], QMessageBox.Ok) except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error' def get_data(self): """ Gets the data from the form as a dictionary :return:dictionary """ try: data = {} data['code'] = self.code_line.text() data['date'] = self.date_line.text() data['organization'] = self.organization_line.text() data['test'] = self.test_line.text() data['description'] = self.description_line.text() data['report'] = self.image_data for key, value in data.iteritems(): if not value: QMessageBox.critical( self, 'Error', 'Insert Proper value for %s' % key.title(), QMessageBox.Ok) return False return data except Exception: if settings.level == 10: logger.exception('raised exception') return False def delete_report(self): """ Deletes the existing report :return: """ try: code = self.code_line.text() status = self.backend_handle.delete_report(code=code) if status: if status[0]: QMessageBox.information(self, 'Success', status[1], QMessageBox.Ok) self.close() else: QMessageBox.critical(self, 'Error', status[1], QMessageBox.Ok) except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error' def calculate_code(self): """ Calculates the entry number, Eases the user for entering the code """ try: table = self.table if table: code = [] rows = table.rowCount() for row in range(rows): code.append(table.item(row, 0).text()) if code: new_code = str(int(max(code)) + 1) self.code_line.setText(new_code) except Exception: if settings.level == 10: logger.exception('raised exception') return False, 'Some Internal Error'
def show_image(self, data, image): px = QPixmap() px.loadFromData(data) self.lblPreview.setPixmap(px)