コード例 #1
0
class FileEntryLabel(QHBoxLayout):
    def __init__(self, file_text, parent):
        super().__init__()

        self.parent = parent

        self.ico_label = QLabel()
        if file_text.split('-')[0] == '0':
            logo_path = "res/file_logo.png"
        else:
            logo_path = "res/folder_logo.png"
        self.ico_label.setPixmap(QPixmap(logo_path).scaled(20, 20))
        self.ico_label.show()

        self.root_label = QLabel(file_text.split(constants.GENERIC_PATH)[1])
        self.root_label.setAccessibleName(file_text)

        self.addWidget(self.ico_label)
        self.addWidget(self.root_label)
        self.addStretch(1)

        self.root_label.mousePressEvent = lambda x: self.mousePressEvent(x)

    def mousePressEvent(self, event):
        """if event.button() == Qt.RightButton:
            self.contextMenuEvent(event)"""
        if event.button() == Qt.LeftButton:
            dlg = QFileDialog()
            dlg.setFileMode(QFileDialog.Directory)
            filenames = []

            if dlg.exec_():
                filenames = dlg.selectedFiles()

            if len(filenames):
                dest_dir = filenames[0]
                print(self.root_label.accessibleName(), " ", dest_dir)
                input("::")
                self.parent.client.transfer_data(
                    self.root_label.accessibleName(), dest_dir)

    """def contextMenuEvent(self, event):
コード例 #2
0
class Display(QScrollArea):
    def __init__(self, parts):
        super().__init__()

        self.parts = parts   # Part files of the unit
        self.num = len(parts)
        self.counter = 0

        # main layout of the tab
        mstack = QHBoxLayout()

        # Contains the drawing on display and button stack for traversing the drawings and the caption of the image on display
        dstack = QVBoxLayout()

        # Contains the thumbnails of all images available for that unit
        self.tstack = QVBoxLayout()
        self.tstack.setContentsMargins(0, 20, 0, 20)
        self.create_tstack()
        tscroll = QScrollArea()
        twidget = QWidget()
        twidget.setLayout(self.tstack)
        tscroll.setWidget(twidget)
        tscroll.setMaximumWidth(150)
        tscroll.setWidgetResizable(True)
        tscroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        tscroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        tscroll.setFrameStyle(0)

        # The buttons for traversing the drawings of the unit are in the layout bstack
        bstack = QHBoxLayout()
        self.next_ = QPushButton(QIcon(static('arrow.png', 'ico')), 'Next')
        self.prev = QPushButton(QIcon(static('arrow-180.png', 'ico')), 'Previous')
        self.prev.clicked.connect(self._prev)
        self.next_.clicked.connect(self._next)
        bstack.addStretch()
        bstack.addWidget(self.prev, 0, Qt.AlignLeft)
        bstack.addSpacing(50)
        bstack.addWidget(self.next_, 0, Qt.AlignRight)
        bstack.addStretch()

        # A widget to display the drawings for that component
        self.display = QLabel()
        self.display.setMaximumSize(900, 900)
        self.display.setAlignment(Qt.AlignCenter)
        # self.display.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
        self.image = QImage(self.size(), QImage.Format_RGB32)

        self.display.setPixmap(QPixmap(static(parts[0][0], 'img')).scaled(self.display.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
        self.display.setScaledContents(True)

        # Implements a context menu for interacting with the displayed image
        self.display.setContextMenuPolicy(Qt.CustomContextMenu)
        self.display.customContextMenuRequested.connect(self.context_menu)

        # A description and title of the displayed image
        self.caption = QLabel(parts[0][1])
        self.caption.setAccessibleName(parts[0][0])
        self.caption.setFont(QFont('Helvetica', 18))
        self.caption.setAlignment(Qt.AlignCenter)

        # TODO: Can image be made to popup and show in full?

        dstack.addWidget(self.display, 0, Qt.AlignCenter)
        dstack.addSpacing(20)
        dstack.setContentsMargins(0, 20, 0, 20)
        dstack.addWidget(self.caption)
        dstack.addSpacing(20)
        dstack.addLayout(bstack)
        dstack.addStretch()

        mstack.addStretch()
        mstack.addLayout(dstack)
        mstack.addStretch()
        mstack.addWidget(tscroll)

        self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.chkbtns()
        mwidget = QWidget()
        mwidget.setLayout(mstack)
        self.setAlignment(Qt.AlignCenter)
        self.setWidget(mwidget)
        self.setWidgetResizable(True)

    def _next(self):
        self.counter += 1
        self.slide()

    def _prev(self):

        self.counter -= 1
        self.slide()

    def slide(self):
        self.chkbtns()
        self.caption.setText(self.parts[self.counter][1])
        self.caption.setAccessibleName(self.parts[self.counter][0])
        self.display.setPixmap(QPixmap(static(self.parts[self.counter][0], 'img')))

    def chkbtns(self):
        if self.num <= self.counter + 1:
            self.next_.setEnabled(False)
        else:
            self.next_.setEnabled(True)

        if self.counter <= 0:
            self.prev.setEnabled(False)
        else:
            self.prev.setEnabled(True)

    def goto(self, x=0):
        self.counter = x
        self.slide()

    def create_tstack(self):
        """
        Populate the thumbnails layout tstack with 100X100 thumbnails of the drawings for the unit
        :param self:
        :return:
        """
        for f, c in self.parts:
            tindex = self.parts.index((f, c))
            i = QLabel()
            i.setMaximumSize(100, 100)
            # i.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
            i.setPixmap(QPixmap(static(f, 'img')).scaled(i.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
            i.setScaledContents(True)
            c = QPushButton(c)
            c.pressed.connect(lambda x=tindex: self.goto(x=x))
            self.tstack.addWidget(i, 0, Qt.AlignCenter)
            self.tstack.addWidget(c, 0, Qt.AlignCenter)
            self.tstack.addSpacing(5)
        self.tstack.addStretch()

    def context_menu(self, pos):
        context = QMenu(self.display)
        self.save = QAction("save", self.display)
        self.save.triggered.connect(self.save_image)
        self.print = QAction("print", self.display)
        self.print.triggered.connect(self.print_image)
        context.addAction(self.save)
        context.addAction(self.print)
        context.exec_(self.display.mapToGlobal(pos))

    def save_image(self):
        path = str(homedir('i') / self.caption.accessibleName())
        image_file, _ = QFileDialog.getSaveFileName(self, "Save Image", path, "JPG Files (*.jpeg *.jpg );;PNG Files (*.png);;Bitmap Files (*.bmp)")
        if image_file:  # and self.image.isNull() == False:
            self.display.pixmap().save(image_file)
        else:
            QMessageBox.information(self, "Error", "Unable to save image.", QMessageBox.Ok)

    def print_image(self):
        printer = QPrinter()
        printer.setOutputFormat(QPrinter.NativeFormat)

        # Create printer dialog to configure printer
        print_dialog = QPrintDialog(printer)

        if print_dialog.exec_() == QPrintDialog.Accepted:
            # Use QPainter to output a PDF file
            painter = QPainter()
            # Begin painting device
            painter.begin(printer)
            # Set QRect to hold painter's current viewport, which is the display
            rect = QRect(painter.viewport())
            # Get the size of display and use it to set the size of the viewport
            size = QSize(self.display.pixmap().size())
            size.scale(rect.size(), Qt.KeepAspectRatio)

            painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
            painter.setWindow(self.display.pixmap().rect())
            # Scale the image_label to fit the rect source (0, 0)
            painter.drawPixmap(0, 0, self.display.pixmap())
            # End painting
            painter.end()