~diegosarmentero/ubuntu-sso-client/934502

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/expander.py

  • Committer: Diego Sarmentero
  • Date: 2012-03-01 17:59:16 UTC
  • mfrom: (889.2.3 ubuntu-sso-client)
  • Revision ID: diego.sarmentero@canonical.com-20120301175916-0u1y822j028l1skl
Conflict Resolved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
"""A Expander widget similar to the GtkExpander."""
 
17
 
 
18
from PyQt4.QtCore import pyqtSignal
 
19
from PyQt4.QtGui import QHBoxLayout, QLabel, QSizePolicy, QVBoxLayout, QWidget
 
20
 
 
21
from ubuntu_sso.qt.arrow import QArrow
 
22
 
 
23
# we are following the Qt style, lets tell pylint to ignore it
 
24
# pylint: disable=C0103
 
25
 
 
26
 
 
27
class QExpanderLabel(QWidget):
 
28
    """Widget used to show the label of a QExpander."""
 
29
 
 
30
    clicked = pyqtSignal()
 
31
 
 
32
    def __init__(self, label, parent=None):
 
33
        """Create a new instance."""
 
34
        super(QExpanderLabel, self).__init__(parent)
 
35
        self.arrow = QArrow(QArrow.RIGHT)
 
36
        self.label = QLabel(label)
 
37
        layout = QHBoxLayout()
 
38
        layout.setContentsMargins(0, 0, 0, 0)
 
39
        self.setLayout(layout)
 
40
        layout.addWidget(self.arrow)
 
41
        layout.addWidget(self.label)
 
42
 
 
43
    def mousePressEvent(self, event):
 
44
        """Mouse clicked."""
 
45
        if self.arrow.direction == QArrow.DOWN:
 
46
            self.arrow.direction = QArrow.RIGHT
 
47
        else:
 
48
            self.arrow.direction = QArrow.DOWN
 
49
        self.clicked.emit()
 
50
 
 
51
    def text(self):
 
52
        """Return the text of the label."""
 
53
        return self.label.text()
 
54
 
 
55
    def setText(self, text):
 
56
        """Set the text of the label."""
 
57
        self.label.setText(text)
 
58
 
 
59
 
 
60
class QExpander(QWidget):
 
61
    """A Qt implementation similar to GtkExpander."""
 
62
 
 
63
    def __init__(self, label, expanded=False, parent=None):
 
64
        """Create a new instance."""
 
65
        super(QExpander, self).__init__(parent)
 
66
        self.label = QExpanderLabel(label)
 
67
        self.label.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
 
68
        self.content = None
 
69
        self.layout = QVBoxLayout()
 
70
        self.layout.setContentsMargins(0, 0, 0, 0)
 
71
        self.setLayout(self.layout)
 
72
        self.layout.addWidget(self.label)
 
73
        self.layout.addStretch()
 
74
        self.label.clicked.connect(self._on_label_clicked)
 
75
        self.setExpanded(expanded)
 
76
 
 
77
    def _on_label_clicked(self):
 
78
        """The expander widget was clicked."""
 
79
        self._expanded = not self._expanded
 
80
        self.setExpanded(self._expanded)
 
81
 
 
82
    def addWidget(self, widget):
 
83
        """Add a widget to the expander.
 
84
 
 
85
        The previous widget will be removed.
 
86
        """
 
87
        if self.content is not None:
 
88
            self.layout.removeWidget(self.content)
 
89
        self.content = widget
 
90
        self.content.setVisible(self._expanded)
 
91
        self.layout.insertWidget(1, self.content)
 
92
 
 
93
    def text(self):
 
94
        """Return the text of the label."""
 
95
        return self.label.text()
 
96
 
 
97
    def setText(self, text):
 
98
        """Set the text of the label."""
 
99
        self.label.setText(text)
 
100
 
 
101
    def expanded(self):
 
102
        """Return if widget is expanded."""
 
103
        return self._expanded
 
104
 
 
105
    # pylint: disable=W0201
 
106
    def setExpanded(self, is_expanded):
 
107
        """Expand the widget or not."""
 
108
        self._expanded = is_expanded
 
109
        if self._expanded:
 
110
            self.label.arrow.direction = QArrow.DOWN
 
111
        else:
 
112
            self.label.arrow.direction = QArrow.RIGHT
 
113
        if self.content is not None:
 
114
            self.content.setVisible(self._expanded)
 
115
    # pylint: enable=W0201