1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
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.
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.
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."""
18
from PyQt4.QtCore import pyqtSignal
19
from PyQt4.QtGui import QHBoxLayout, QLabel, QSizePolicy, QVBoxLayout, QWidget
21
from ubuntu_sso.qt.arrow import QArrow
23
# we are following the Qt style, lets tell pylint to ignore it
24
# pylint: disable=C0103
27
class QExpanderLabel(QWidget):
28
"""Widget used to show the label of a QExpander."""
30
clicked = pyqtSignal()
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)
43
def mousePressEvent(self, event):
45
if self.arrow.direction == QArrow.DOWN:
46
self.arrow.direction = QArrow.RIGHT
48
self.arrow.direction = QArrow.DOWN
52
"""Return the text of the label."""
53
return self.label.text()
55
def setText(self, text):
56
"""Set the text of the label."""
57
self.label.setText(text)
60
class QExpander(QWidget):
61
"""A Qt implementation similar to GtkExpander."""
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)
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)
77
def _on_label_clicked(self):
78
"""The expander widget was clicked."""
79
self._expanded = not self._expanded
80
self.setExpanded(self._expanded)
82
def addWidget(self, widget):
83
"""Add a widget to the expander.
85
The previous widget will be removed.
87
if self.content is not None:
88
self.layout.removeWidget(self.content)
90
self.content.setVisible(self._expanded)
91
self.layout.insertWidget(1, self.content)
94
"""Return the text of the label."""
95
return self.label.text()
97
def setText(self, text):
98
"""Set the text of the label."""
99
self.label.setText(text)
102
"""Return if widget is expanded."""
103
return self._expanded
105
# pylint: disable=W0201
106
def setExpanded(self, is_expanded):
107
"""Expand the widget or not."""
108
self._expanded = is_expanded
110
self.label.arrow.direction = QArrow.DOWN
112
self.label.arrow.direction = QArrow.RIGHT
113
if self.content is not None:
114
self.content.setVisible(self._expanded)
115
# pylint: enable=W0201