~nataliabidart/ubuntu-sso-client/stable-3-0-update-2.99.90

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/arrow.py

  • Committer: Natalia B. Bidart
  • Date: 2012-03-06 14:53:38 UTC
  • mfrom: (812.1.89 ubuntu-sso-client)
  • Revision ID: natalia.bidart@canonical.com-20120306145338-x28na428035jkqwh
- Updating from trunk up to revno 901.

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
"""Widget written in Qt that works as a GtkArrow."""
 
17
 
 
18
from PyQt4.QtGui import QPainter, QStyle, QStyleOption, QWidget
 
19
 
 
20
 
 
21
class QArrow(QWidget):
 
22
    """Custom widget."""
 
23
 
 
24
    UP = 0
 
25
    DOWN = 1
 
26
    LEFT = 2
 
27
    RIGHT = 3
 
28
 
 
29
    def __init__(self, direction, parent=None):
 
30
        """Create a new instance."""
 
31
        super(QArrow, self).__init__(parent)
 
32
        self._set_direction(direction)
 
33
        self.setFixedWidth(16)
 
34
 
 
35
    # pylint: disable=C0103
 
36
    def paintEvent(self, event):
 
37
        """Paint the widget."""
 
38
        opt = QStyleOption()
 
39
        opt.initFrom(self)
 
40
        painter = QPainter(self)
 
41
        if self._direction == QArrow.UP:
 
42
            primitive = QStyle.PE_IndicatorArrowUp
 
43
        elif self._direction == QArrow.DOWN:
 
44
            primitive = QStyle.PE_IndicatorArrowDown
 
45
        elif self._direction == QArrow.LEFT:
 
46
            primitive = QStyle.PE_IndicatorArrowLeft
 
47
        else:
 
48
            primitive = QStyle.PE_IndicatorArrowRight
 
49
        painter.translate(-5, 0)
 
50
        painter.setViewTransformEnabled(True)
 
51
        self.style().drawPrimitive(primitive, opt, painter, self)
 
52
    # pylint: enable=C0103
 
53
 
 
54
    def _get_direction(self):
 
55
        """Return the direction used."""
 
56
        return self._direction
 
57
 
 
58
    # pylint: disable=W0201
 
59
    def _set_direction(self, direction):
 
60
        """Set the direction."""
 
61
        if direction not in (QArrow.UP, QArrow.DOWN,
 
62
                             QArrow.LEFT, QArrow.RIGHT):
 
63
            raise ValueError('Wrong arrow direction.')
 
64
        self._direction = direction
 
65
        self.repaint()
 
66
    # pylint: enable=W0201
 
67
 
 
68
    direction = property(_get_direction, _set_direction)