~brand-nmapsi4/nmapsi4/master

« back to all changes in this revision

Viewing changes to nmapsi4/common/qpushbuttonorientated.cpp

  • Committer: Francesco Cecconi
  • Date: 2011-12-18 11:02:04 UTC
  • Revision ID: git-v1:60a69e132c8ffeb2727e558b2b2e8ac1713e332e
Removed nmapsi4-logr (dedicate repository) but it could be deprecated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2010-2011 by Francesco Cecconi                          *
3
 
 *   francesco.cecconi@gmail.com                                           *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License.        *
8
 
 *                                                                         *
9
 
 *   This program is distributed in the hope that it will be useful,       *
10
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 
 *   GNU General Public License for more details.                          *
13
 
 *                                                                         *
14
 
 *   You should have received a copy of the GNU General Public License     *
15
 
 *   along with this program; if not, write to the                         *
16
 
 *   Free Software Foundation, Inc.,                                       *
17
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 
 ***************************************************************************/
19
 
 
20
 
#include "qpushbuttonorientated.h"
21
 
 
22
 
#include <QtGui/QStylePainter>
23
 
#include <QtGui/QMenu>
24
 
 
25
 
QPushButtonOrientated::QPushButtonOrientated(QWidget* parent)
26
 
: QPushButton(parent)
27
 
{
28
 
    initObject();
29
 
}
30
 
 
31
 
QPushButtonOrientated::QPushButtonOrientated(const QString& text, QWidget* parent)
32
 
: QPushButton(text, parent)
33
 
{
34
 
    initObject();
35
 
}
36
 
 
37
 
QPushButtonOrientated::QPushButtonOrientated(const QIcon& icon, const QString& text, QWidget* parent)
38
 
: QPushButton(icon, text, parent)
39
 
{
40
 
    initObject();
41
 
}
42
 
 
43
 
QPushButtonOrientated::~QPushButtonOrientated()
44
 
{
45
 
}
46
 
 
47
 
void QPushButtonOrientated::initObject()
48
 
{
49
 
    _orientation = Qt::Horizontal;
50
 
    _mirrored = false;
51
 
}
52
 
 
53
 
Qt::Orientation QPushButtonOrientated::getOrientation() const
54
 
{
55
 
    return _orientation;
56
 
}
57
 
 
58
 
void QPushButtonOrientated::setOrientation(Qt::Orientation orientation)
59
 
{
60
 
    _orientation = orientation;
61
 
    switch (orientation)
62
 
    {
63
 
    case Qt::Horizontal:
64
 
        setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
65
 
        break;
66
 
 
67
 
    case Qt::Vertical:
68
 
        setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
69
 
        break;
70
 
    }
71
 
}
72
 
 
73
 
bool QPushButtonOrientated::mirrored() const
74
 
{
75
 
    return _mirrored;
76
 
}
77
 
 
78
 
void QPushButtonOrientated::setMirrored(bool mirrored)
79
 
{
80
 
    _mirrored = mirrored;
81
 
}
82
 
 
83
 
QSize QPushButtonOrientated::sizeHint() const
84
 
{
85
 
    QSize size = QPushButton::sizeHint();
86
 
    
87
 
    if (_orientation == Qt::Vertical)
88
 
    {
89
 
        size.transpose();
90
 
    }
91
 
    
92
 
    return size;
93
 
}
94
 
 
95
 
QSize QPushButtonOrientated::minimumSizeHint() const
96
 
{
97
 
    QSize size = QPushButton::minimumSizeHint();
98
 
    
99
 
    if (_orientation == Qt::Vertical)
100
 
    {
101
 
        size.transpose();
102
 
    }
103
 
    
104
 
    return size;
105
 
}
106
 
 
107
 
void QPushButtonOrientated::paintEvent(QPaintEvent* event)
108
 
{
109
 
    Q_UNUSED(event);
110
 
    QStylePainter painter(this);
111
 
 
112
 
    switch (_orientation)
113
 
    {
114
 
    case Qt::Horizontal:
115
 
        if (_mirrored)
116
 
        {
117
 
            painter.rotate(180);
118
 
            painter.translate(-width(), -height());
119
 
        }
120
 
        break;
121
 
 
122
 
    case Qt::Vertical:
123
 
        if (_mirrored)
124
 
        {
125
 
            painter.rotate(-90);
126
 
            painter.translate(-height(), 0);
127
 
        }
128
 
        else
129
 
        {
130
 
            painter.rotate(90);
131
 
            painter.translate(0, -width());
132
 
        }
133
 
        break;
134
 
    }
135
 
 
136
 
    painter.drawControl(QStyle::CE_PushButton, getStyleOption());
137
 
}
138
 
 
139
 
QStyleOptionButton QPushButtonOrientated::getStyleOption() const
140
 
{
141
 
    QStyleOptionButton option;
142
 
    option.initFrom(this);
143
 
    if (_orientation == Qt::Vertical)
144
 
    {
145
 
        QSize size = option.rect.size();
146
 
        size.transpose();
147
 
        option.rect.setSize(size);
148
 
    }
149
 
    option.features = QStyleOptionButton::None;
150
 
    
151
 
    if (isFlat())
152
 
    {
153
 
        option.features |= QStyleOptionButton::Flat;
154
 
    }
155
 
    
156
 
    if (menu())
157
 
    {
158
 
        option.features |= QStyleOptionButton::HasMenu;
159
 
    }
160
 
    
161
 
    if (autoDefault() || isDefault())
162
 
    {
163
 
        option.features |= QStyleOptionButton::AutoDefaultButton;
164
 
    }
165
 
    
166
 
    if (isDefault())
167
 
    {
168
 
        option.features |= QStyleOptionButton::DefaultButton;
169
 
    }
170
 
    
171
 
    if (isDown() || (menu() && menu()->isVisible()))
172
 
    {
173
 
        option.state |= QStyle::State_Sunken;
174
 
    }
175
 
    
176
 
    if (isChecked())
177
 
    {
178
 
        option.state |= QStyle::State_On;
179
 
    }
180
 
    
181
 
    if (!isFlat() && !isDown())
182
 
    {
183
 
        option.state |= QStyle::State_Raised;
184
 
    }
185
 
    
186
 
    option.text = text();
187
 
    option.icon = icon();
188
 
    option.iconSize = iconSize();
189
 
    return option;
190
 
}