fancyactionbar.cpp 9.16 KB
Newer Older
1
/********************Q******************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "fancyactionbar.h"
con's avatar
con committed
31
#include "coreconstants.h"
con's avatar
con committed
32

33
34
35
36
37
#include <utils/stylehelper.h>

#include <coreplugin/icore.h>
#include <coreplugin/mainwindow.h>

con's avatar
con committed
38
39
40
41
#include <QtGui/QHBoxLayout>
#include <QtGui/QPainter>
#include <QtGui/QPicture>
#include <QtGui/QVBoxLayout>
42
#include <QtGui/QAction>
43
44
45
#include <QtGui/QStatusBar>
#include <QtGui/QStyle>
#include <QtGui/QStyleOption>
46
47
48
49
#include <QtCore/QEvent>
#include <QtGui/QMouseEvent>
#include <QtCore/QAnimationGroup>
#include <QtCore/QPropertyAnimation>
con's avatar
con committed
50
51
52
53
54

using namespace Core;
using namespace Internal;

FancyToolButton::FancyToolButton(QWidget *parent)
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
55
    : QToolButton(parent), m_fader(0)
con's avatar
con committed
56
57
{
    setAttribute(Qt::WA_Hover, true);
mae's avatar
mae committed
58
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
con's avatar
con committed
59
60
}

61
62
63
64
65
66
67
68
69
70
71
72
73
void FancySeparator::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.fillRect(rect(), Qt::red);
}

bool FancyToolButton::event(QEvent *e)
{
    switch(e->type()) {
    case QEvent::Enter:
        {
            QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
74
            animation->setDuration(125);
75
76
77
78
79
80
81
            animation->setEndValue(1.0);
            animation->start(QAbstractAnimation::DeleteWhenStopped);
        }
        break;
    case QEvent::Leave:
        {
            QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
82
            animation->setDuration(125);
83
84
85
86
87
88
89
90
91
92
            animation->setEndValue(0.0);
            animation->start(QAbstractAnimation::DeleteWhenStopped);
        }
        break;
    default:
        return QToolButton::event(e);
    }
    return false;
}

con's avatar
con committed
93
94
95
void FancyToolButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
96
    QPainter painter(this);
97

98
    // draw borders
99
100
    bool isTitledAction = defaultAction()->property("titledAction").toBool();

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
101
#ifndef Q_WS_MAC // Mac UIs usually don't hover
102
103
    if (m_fader > 0 && isEnabled() && !isDown()) {
        painter.save();
104
        QColor whiteOverlay(Qt::white);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
105
        whiteOverlay.setAlpha(int(10 * m_fader));
106
107
108
109
        QRect roundRect = rect().adjusted(5, 3, -5, -3);
        painter.translate(0.5, 0.5);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setBrush(whiteOverlay);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
110
        whiteOverlay.setAlpha(int(20*m_fader));
111
112
113
        painter.setPen(whiteOverlay);
        painter.drawRoundedRect(roundRect, 3, 3);
        painter.restore();
114
    }
con's avatar
con committed
115
#endif
mae's avatar
mae committed
116

117
118
    if (isDown() || isChecked()) {
        painter.save();
119
120
        QColor color(Qt::black);
        color.setAlpha(15);
121
122
123
        QRect roundRect = rect().adjusted(5, 3, -5, -3);
        painter.translate(0.5, 0.5);
        painter.setRenderHint(QPainter::Antialiasing);
124
        painter.setPen(QColor(255, 255, 255, 20));
125
126
        static int rounding = 3;
        painter.drawRoundedRect(roundRect.adjusted(-1, -1, 1, 1), rounding, rounding);
127
128
        painter.setPen(QColor(0, 0, 0, 20));
        painter.setBrush(QColor(0, 0, 0, 15));
129
        painter.drawRoundedRect(roundRect.adjusted(1, 1, 0, 0), rounding, rounding);
130
131
        color.setAlpha(75);
        painter.setPen(color);
132
133
134
        painter.setBrush(Qt::NoBrush);
        painter.drawRoundedRect(roundRect, 3, 3);
        painter.restore();
135
    }
mae's avatar
mae committed
136

137
138
    QPixmap borderPixmap;
    QMargins margins;
139

con's avatar
con committed
140
    QPixmap pix = icon().pixmap(Core::Constants::TARGET_ICON_SIZE, Core::Constants::TARGET_ICON_SIZE, isEnabled() ? QIcon::Normal : QIcon::Disabled);
141
142
    QPoint center = rect().center();
    QSizeF halfPixSize = pix.size()/2.0;
143

144
    painter.drawPixmap(center-QPointF(halfPixSize.width()-1, halfPixSize.height()-1), pix);
mae's avatar
mae committed
145

146
    // draw popup texts
147
    if (isTitledAction) {
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        QFont normalFont(painter.font());
        normalFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
        QFont boldFont(normalFont);
        boldFont.setBold(true);
        QFontMetrics fm(normalFont);
        QFontMetrics boldFm(boldFont);
        int lineHeight = boldFm.height();

        int textFlags = Qt::AlignVCenter|Qt::AlignHCenter;

        painter.setFont(normalFont);

        QPoint textOffset = center - QPoint(pix.rect().width()/2, pix.rect().height()/2);
        textOffset = textOffset - QPoint(0, lineHeight+5);
        QRectF r(0, textOffset.y(), rect().width(), lineHeight);
163
164
165
166
167
        QColor penColor;
        if (isEnabled())
            penColor = Qt::white;
        else
            penColor = Qt::gray;
168
        painter.setPen(penColor);
169
170
171
        const QString projectName = defaultAction()->property("heading").toString();
        QString ellidedProjectName = fm.elidedText(projectName, Qt::ElideMiddle, r.width());
        if (isEnabled()) {
172
173
174
175
            const QRectF shadowR = r.translated(0, 1);
            painter.setPen(QColor(30, 30, 30, 80));
            painter.drawText(shadowR, textFlags, ellidedProjectName);
            painter.setPen(penColor);
176
        }
177
178
179
        painter.drawText(r, textFlags, ellidedProjectName);
        textOffset = center + QPoint(pix.rect().width()/2, pix.rect().height()/2);
        r = QRectF(0, textOffset.y()+5, rect().width(), lineHeight);
180
        const QString buildConfiguration = defaultAction()->property("subtitle").toString();
181
182
        painter.setFont(boldFont);
        QString ellidedBuildConfiguration = boldFm.elidedText(buildConfiguration, Qt::ElideMiddle, r.width());
183
        if (isEnabled()) {
184
185
186
187
            const QRectF shadowR = r.translated(0, 1);
            painter.setPen(QColor(30, 30, 30, 80));
            painter.drawText(shadowR, textFlags, ellidedBuildConfiguration);
            painter.setPen(penColor);
188
        }
189
        painter.drawText(r, textFlags, ellidedBuildConfiguration);
190
191
192
193
        QStyleOption opt;
        opt.initFrom(this);
        opt.rect = rect().adjusted(rect().width() - 18, 0, -10, 0);
        Utils::StyleHelper::drawArrow(QStyle::PE_IndicatorArrowRight, &painter, &opt);
con's avatar
con committed
194
195
196
197
198
    }
}

void FancyActionBar::paintEvent(QPaintEvent *event)
{
199
    QPainter painter(this);
con's avatar
con committed
200
    Q_UNUSED(event)
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
201
202
    QColor light = Utils::StyleHelper::sidebarHighlight();
    QColor dark = Utils::StyleHelper::sidebarShadow();
203
204
205
206
    painter.setPen(dark);
    painter.drawLine(rect().topLeft(), rect().topRight());
    painter.setPen(light);
    painter.drawLine(rect().topLeft() + QPoint(0,1), rect().topRight() + QPoint(0,1));
con's avatar
con committed
207
}
208

con's avatar
con committed
209
210
QSize FancyToolButton::sizeHint() const
{
211
    QSizeF buttonSize = iconSize().expandedTo(QSize(64, 36));
212
    if (defaultAction()->property("titledAction").toBool()) {
213
214
215
216
217
218
        QFont boldFont(font());
        boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
        boldFont.setBold(true);
        QFontMetrics fm(boldFont);
        qreal lineHeight = fm.height();
        buttonSize += QSizeF(0, (lineHeight*3.5));
219
    }
220
    return buttonSize.toSize();
con's avatar
con committed
221
222
}

mae's avatar
mae committed
223
224
225
226
227
QSize FancyToolButton::minimumSizeHint() const
{
    return QSize(8, 8);
}

228
229
230
231
232
233
234
235
void FancyToolButton::actionChanged()
{
    // the default action changed in some way, e.g. it might got hidden
    // since we inherit a tool button we won't get invisible, so do this here
    if (QAction* action = defaultAction())
        setVisible(action->isVisible());
}

con's avatar
con committed
236
237
238
239
FancyActionBar::FancyActionBar(QWidget *parent)
    : QWidget(parent)
{
    m_actionsLayout = new QVBoxLayout;
240
241
242
243
244
245
    QVBoxLayout *spacerLayout = new QVBoxLayout;
    spacerLayout->addLayout(m_actionsLayout);
    int sbh = ICore::instance()->statusBar()->height();
    spacerLayout->addSpacing(sbh);
    spacerLayout->setMargin(0);
    spacerLayout->setSpacing(0);
246
247
    setLayout(spacerLayout);
    setContentsMargins(0,2,0,0);
con's avatar
con committed
248
249
}

250
251
252
253
254
255
256
257
void FancyActionBar::addProjectSelector(QAction *action)
{
    FancyToolButton* toolButton = new FancyToolButton(this);
    toolButton->setDefaultAction(action);
    connect(action, SIGNAL(changed()), toolButton, SLOT(actionChanged()));
    m_actionsLayout->insertWidget(0, toolButton);

}
258
void FancyActionBar::insertAction(int index, QAction *action)
con's avatar
con committed
259
260
261
{
    FancyToolButton *toolButton = new FancyToolButton(this);
    toolButton->setDefaultAction(action);
262
    connect(action, SIGNAL(changed()), toolButton, SLOT(actionChanged()));
con's avatar
con committed
263
264
    m_actionsLayout->insertWidget(index, toolButton);
}
265

266
267
268
269
QLayout *FancyActionBar::actionsLayout() const
{
    return m_actionsLayout;
}