Example #1
0
class ExplodingAnimation(Animation):
    def __init__(self, center, duration, parent = None):
        super(ExplodingAnimation, self).__init__(parent)
        self.center = center
        self.__radius = 1.0
        self.anim = QPropertyAnimation(self, "radius")
        self.anim.setStartValue(1.0)
        self.anim.setEndValue(100.0)
        self.anim.setDuration(duration)
        self.anim.setEasingCurve(QEasingCurve.OutExpo)
        self.anim.valueChanged.connect(self.updated)
        self.anim.finished.connect(self.finished)

    def start(self):
        self.anim.start()

    def stop(self):
        self.anim.stop()

    def getRadius(self):
        return self.__radius

    def setRadius(self, r):
        self.__radius = r

    radius = pyqtProperty("qreal", getRadius, setRadius)

    def paint(self, painter):
        opacity = 1.0 - float(self.anim.currentTime()) / self.anim.duration()
        pen = QPen(QColor(255, 0, 0, opacity * 255))
        pen.setWidth(3)
        painter.setPen(pen)
        painter.setBrush(Qt.transparent)
        rect = QRect(0, 0, self.radius * 2, self.radius * 2)
        rect.moveCenter(self.center)
        painter.drawEllipse(rect)
Example #2
0
class SplitterResizer(QObject):
    """An object able to control the size of a widget in a
    QSpliter instance.

    """
    def __init__(self, parent=None):
        QObject.__init__(self, parent)
        self.__splitter = None
        self.__widget = None
        self.__animationEnabled = True
        self.__size = -1
        self.__expanded = False
        self.__animation = QPropertyAnimation(self, b"size_", self)

        self.__action = QAction("toogle-expanded", self, checkable=True)
        self.__action.triggered[bool].connect(self.setExpanded)

    def setSize(self, size):
        """Set the size of the controlled widget (either width or height
        depending on the orientation).

        """
        if self.__size != size:
            self.__size = size
            self.__update()

    def size(self):
        """Return the size of the widget in the splitter (either height of
        width) depending on the splitter orientation.

        """
        if self.__splitter and self.__widget:
            index = self.__splitter.indexOf(self.__widget)
            sizes = self.__splitter.sizes()
            return sizes[index]
        else:
            return -1

    size_ = Property(int, fget=size, fset=setSize)

    def setAnimationEnabled(self, enable):
        """Enable/disable animation.
        """
        self.__animation.setDuration(0 if enable else 200)

    def animationEnabled(self):
        return self.__animation.duration() == 0

    def setSplitterAndWidget(self, splitter, widget):
        """Set the QSplitter and QWidget instance the resizer should control.

        .. note:: the widget must be in the splitter.

        """
        if splitter and widget and not splitter.indexOf(widget) > 0:
            raise ValueError("Widget must be in a spliter.")

        if self.__widget:
            self.__widget.removeEventFilter()
        self.__splitter = splitter
        self.__widget = widget

        if widget:
            widget.installEventFilter(self)

        self.__update()

    def toogleExpandedAction(self):
        """Return a QAction that can be used to toggle expanded state.
        """
        return self.__action

    def open(self):
        """Open the controlled widget (expand it to it sizeHint).
        """
        self.__expanded = True
        self.__action.setChecked(True)

        if not (self.__splitter and self.__widget):
            return

        size = self.size()
        if size > 0:
            # Already has non zero size.
            return

        hint = self.__widget.sizeHint()

        if self.__splitter.orientation() == Qt.Vertical:
            end = hint.height()
        else:
            end = hint.width()

        self.__animation.setStartValue(0)
        self.__animation.setEndValue(end)
        self.__animation.start()

    def close(self):
        """Close the controlled widget (shrink to size 0).
        """
        self.__expanded = False
        self.__action.setChecked(False)

        if not (self.__splitter and self.__widget):
            return

        self.__animation.setStartValue(self.size())
        self.__animation.setEndValue(0)
        self.__animation.start()

    def setExpanded(self, expanded):
        """Set the expanded state.

        """
        if self.__expanded != expanded:
            if expanded:
                self.open()
            else:
                self.close()

    def expanded(self):
        """Return the expanded state.
        """
        return self.__expanded

    def __update(self):
        """Update the splitter sizes.
        """
        if self.__splitter and self.__widget:
            splitter = self.__splitter
            index = splitter.indexOf(self.__widget)
            sizes = splitter.sizes()
            current = sizes[index]
            diff = current - self.__size
            sizes[index] = self.__size
            sizes[index - 1] = sizes[index - 1] + diff

            self.__splitter.setSizes(sizes)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.Resize:
            if self.__splitter.orientation() == Qt.Vertical:
                size = event.size().height()
            else:
                size = event.size().width()

            if self.__expanded and size == 0:
                self.__action.setChecked(False)
                self.__expanded = False
            elif not self.__expanded and size > 0:
                self.__action.setChecked(True)
                self.__expanded = True

        return QObject.eventFilter(self, obj, event)
Example #3
0
class ConnectionWidget(QWidget):
    def __init__(self, parent = None, connect_cb = None):
        QWidget.__init__(self, parent)
        self.widgets = connection_properties.Ui_connection_widget()
        w = self.widgets
        w.setupUi(self)
        self.container_height_exclusive = self.sizeHint().height() - self.widgets.proxy_params.sizeHint().height()
        self.combined_height = self.sizeHint().height()
        
        if HAS_QPROPERTY_ANIMATION:
            self.pp_show_animation = QPropertyAnimation(self, "size", self)
            self.pp_show_animation.setDuration(100)
            self.pp_show_animation.setStartValue(QSize(self.width(), self.container_height_exclusive))
            self.pp_show_animation.setEndValue(QSize(self.width(), self.combined_height))
            
            self.pp_hide_animation = QPropertyAnimation(self, "size", self)
            self.pp_hide_animation.setDuration(100)
            self.pp_hide_animation.setStartValue(QSize(self.width(), self.combined_height))
            self.pp_hide_animation.setEndValue(QSize(self.width(), self.container_height_exclusive))
        
        mkProtocolComboBox(w.w_improto)
        w.proxy_params.proxy_type_group = QButtonGroup(w.proxy_params)
        for i in ("http", "socks4", "socks5"):
            w.proxy_params.proxy_type_group.addButton(getattr(w, "proxy_type_" + i))
        self.connect_cb = connect_cb
        if connect_cb:
            signal_connect(w.w_connect, SIGNAL("clicked()"), self.submit)
            signal_connect(w.w_username, SIGNAL("returnPressed()"), self.submit)
            signal_connect(w.w_password, SIGNAL("returnPressed()"), self.submit)
        
        
        if HAS_QPROPERTY_ANIMATION:
            @QtCore.pyqtSlot("bool", name="setVisible")
            def proxy_setVisible(b):
                if b:
                    self.pp_show_animation.start()
                    QTimer.singleShot(self.pp_show_animation.duration(),
                                      lambda: type(w.proxy_params).setVisible(w.proxy_params, True))
                else:
                    self.pp_hide_animation.start()
                    type(w.proxy_params).setVisible(w.proxy_params, False)
            w.proxy_params.setVisible = proxy_setVisible

                
        def _hideEvent(e):
            if self.parent() and self.parent().layout():
                self.parent().layout().activate()
            type(w.proxy_params).hideEvent(w.proxy_params, e)
            
        w.proxy_params.hideEvent = _hideEvent
        w.proxy_params.setVisible(False)
        signal_connect(w.show_proxy_prefs, SIGNAL("toggled(bool)"), w.proxy_params.setVisible)
        self.setAutoFillBackground(True)
        w.proxy_params.setAutoFillBackground(False)
                        
    @property
    def visible_height(self):
        if not self.widgets.proxy_params.isVisible():
            return self.container_height_exclusive
        else:
            return self.combined_height
        
    def getValues(self):
        w = self.widgets
        username = self.widgets.w_username.text()
        password = self.widgets.w_password.text()
        if not (username and password):
            msg = QMessageBox()
            msg.setIcon(msg.Critical)
            msg.setText("Username or password is empty!")
            msg.setWindowTitle("Missing Input!")
            msg.exec_()
            return None
        improto = self.widgets.w_improto.model().index(self.widgets.w_improto.currentIndex(),
                                     0,QModelIndex()).data(Qt.UserRole).toPyObject()
        
        _d_proxy_params = {"proxy_host" : None, "proxy_port": None, "proxy_type" : None,
                           "proxy_username" : None, "proxy_password" : None}
        _d_proxy_params["proxy_host"] = str(w.proxy_host.text())
        if _d_proxy_params["proxy_host"]:
            _d_proxy_params["proxy_port"] = str(w.proxy_port.text())
            _d_proxy_params["proxy_type"] = str(w.proxy_params.proxy_type_group.checkedButton().text()).lower()
            _d_proxy_params["proxy_username"] = str(w.proxy_username.text())
            _d_proxy_params["proxy_password"] = str(w.proxy_password.text())
        return (username, password, improto, _d_proxy_params)
        
    def submit(self):
        ret = self.getValues()
        if ret:
            username, password, improto, proxy_params = ret
            self.connect_cb(username, password, improto, **proxy_params)
            
    def reset(self):
        self.widgets.w_username.setText("")
        self.widgets.w_password.setText("")
Example #4
0
class SplitterResizer(QObject):
    """An object able to control the size of a widget in a
    QSpliter instance.

    """

    def __init__(self, parent=None):
        QObject.__init__(self, parent)
        self.__splitter = None
        self.__widget = None
        self.__animationEnabled = True
        self.__size = -1
        self.__expanded = False
        self.__animation = QPropertyAnimation(self, "size_", self)

        self.__action = QAction("toogle-expanded", self, checkable=True)
        self.__action.triggered[bool].connect(self.setExpanded)

    def setSize(self, size):
        """Set the size of the controlled widget (either width or height
        depending on the orientation).

        """
        if self.__size != size:
            self.__size = size
            self.__update()

    def size(self):
        """Return the size of the widget in the splitter (either height of
        width) depending on the splitter orientation.

        """
        if self.__splitter and self.__widget:
            index = self.__splitter.indexOf(self.__widget)
            sizes = self.__splitter.sizes()
            return sizes[index]
        else:
            return -1

    size_ = Property(int, fget=size, fset=setSize)

    def setAnimationEnabled(self, enable):
        """Enable/disable animation.
        """
        self.__animation.setDuration(0 if enable else 200)

    def animationEnabled(self):
        return self.__animation.duration() == 0

    def setSplitterAndWidget(self, splitter, widget):
        """Set the QSplitter and QWidget instance the resizer should control.

        .. note:: the widget must be in the splitter.

        """
        if splitter and widget and not splitter.indexOf(widget) > 0:
            raise ValueError("Widget must be in a spliter.")

        if self.__widget:
            self.__widget.removeEventFilter()
        self.__splitter = splitter
        self.__widget = widget

        if widget:
            widget.installEventFilter(self)

        self.__update()

    def toogleExpandedAction(self):
        """Return a QAction that can be used to toggle expanded state.
        """
        return self.__action

    def open(self):
        """Open the controlled widget (expand it to it sizeHint).
        """
        self.__expanded = True
        self.__action.setChecked(True)

        if not (self.__splitter and self.__widget):
            return

        size = self.size()
        if size > 0:
            # Already has non zero size.
            return

        hint = self.__widget.sizeHint()

        if self.__splitter.orientation() == Qt.Vertical:
            end = hint.height()
        else:
            end = hint.width()

        self.__animation.setStartValue(0)
        self.__animation.setEndValue(end)
        self.__animation.start()

    def close(self):
        """Close the controlled widget (shrink to size 0).
        """
        self.__expanded = False
        self.__action.setChecked(False)

        if not (self.__splitter and self.__widget):
            return

        self.__animation.setStartValue(self.size())
        self.__animation.setEndValue(0)
        self.__animation.start()

    def setExpanded(self, expanded):
        """Set the expanded state.

        """
        if self.__expanded != expanded:
            if expanded:
                self.open()
            else:
                self.close()

    def expanded(self):
        """Return the expanded state.
        """
        return self.__expanded

    def __update(self):
        """Update the splitter sizes.
        """
        if self.__splitter and self.__widget:
            splitter = self.__splitter
            index = splitter.indexOf(self.__widget)
            sizes = splitter.sizes()
            current = sizes[index]
            diff = current - self.__size
            sizes[index] = self.__size
            sizes[index - 1] = sizes[index - 1] + diff

            self.__splitter.setSizes(sizes)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.Resize:
            if self.__splitter.orientation() == Qt.Vertical:
                size = event.size().height()
            else:
                size = event.size().width()

            if self.__expanded and size == 0:
                self.__action.setChecked(False)
                self.__expanded = False
            elif not self.__expanded and size > 0:
                self.__action.setChecked(True)
                self.__expanded = True

        return QObject.eventFilter(self, obj, event)