fancyactionbar.cpp 7.9 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>
con's avatar
con committed
46
47
48
49
50
51
52
53

using namespace Core;
using namespace Internal;

FancyToolButton::FancyToolButton(QWidget *parent)
    : QToolButton(parent)
{
    setAttribute(Qt::WA_Hover, true);
mae's avatar
mae committed
54
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
con's avatar
con committed
55
56
57
58
59
}

void FancyToolButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
60
    QPainter painter(this);
61

62
    // draw borders
63
64
65
    QLayout *parentLayout = qobject_cast<FancyActionBar*>(parentWidget())->actionsLayout();
    bool isTitledAction = defaultAction()->property("titledAction").toBool();

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
66
#ifndef Q_WS_MAC // Mac UIs usually don't hover
67
68
69
    if (underMouse() && isEnabled() && !isDown()) {
        QColor whiteOverlay(Qt::white);
        whiteOverlay.setAlpha(20);
70
        painter.fillRect(rect().adjusted(1,  1, -1, -1), whiteOverlay);
71
    }
con's avatar
con committed
72
#endif
mae's avatar
mae committed
73

74
75
76
    if (isDown()) {
        QColor whiteOverlay(Qt::black);
        whiteOverlay.setAlpha(20);
77
        painter.fillRect(rect().adjusted(1,  1, -1, -1), whiteOverlay);
78
    }
mae's avatar
mae committed
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    QPixmap borderPixmap;
    QMargins margins;
    if (parentLayout && parentLayout->count() > 0 &&
        parentLayout->itemAt(parentLayout->count()-1)->widget() == this) {
        margins = QMargins(3, 3, 2, 0);
        borderPixmap = QPixmap(
                QLatin1String(":/fancyactionbar/images/fancytoolbutton_bottom_outline.png"));
    } else if (parentLayout && parentLayout->count() > 0 &&
               parentLayout->itemAt(0)->widget() == this) {
        margins = QMargins(3, 3, 2, 3);
        borderPixmap = QPixmap(
                QLatin1String(":/fancyactionbar/images/fancytoolbutton_top_outline.png"));
    } else {
        margins = QMargins(3, 3, 2, 0);
        borderPixmap = QPixmap(
                QLatin1String(":/fancyactionbar/images/fancytoolbutton_normal_outline.png"));
    }
97

98
99
100
    // draw pixmap
    QRect drawRect = rect();
    qDrawBorderPixmap(&painter, drawRect, margins, borderPixmap);
101

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

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

108
    // draw popup texts
109
    if (isTitledAction) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        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);
125
126
127
128
129
        QColor penColor;
        if (isEnabled())
            penColor = Qt::white;
        else
            penColor = Qt::gray;
130
        painter.setPen(penColor);
131
132
133
        const QString projectName = defaultAction()->property("heading").toString();
        QString ellidedProjectName = fm.elidedText(projectName, Qt::ElideMiddle, r.width());
        if (isEnabled()) {
134
135
136
137
            const QRectF shadowR = r.translated(0, 1);
            painter.setPen(QColor(30, 30, 30, 80));
            painter.drawText(shadowR, textFlags, ellidedProjectName);
            painter.setPen(penColor);
138
        }
139
140
141
        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);
142
        const QString buildConfiguration = defaultAction()->property("subtitle").toString();
143
144
        painter.setFont(boldFont);
        QString ellidedBuildConfiguration = boldFm.elidedText(buildConfiguration, Qt::ElideMiddle, r.width());
145
        if (isEnabled()) {
146
147
148
149
            const QRectF shadowR = r.translated(0, 1);
            painter.setPen(QColor(30, 30, 30, 80));
            painter.drawText(shadowR, textFlags, ellidedBuildConfiguration);
            painter.setPen(penColor);
150
        }
151
        painter.drawText(r, textFlags, ellidedBuildConfiguration);
con's avatar
con committed
152
    }
mae's avatar
mae committed
153

con's avatar
con committed
154
155
156
157
158
159
160
161
}

void FancyActionBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
}
QSize FancyToolButton::sizeHint() const
{
162
    QSizeF buttonSize = iconSize().expandedTo(QSize(64, 40));
163
    if (defaultAction()->property("titledAction").toBool()) {
164
165
166
167
168
169
170
        QFont boldFont(font());
        boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
        boldFont.setBold(true);
        QFontMetrics fm(boldFont);
        qreal lineHeight = fm.height();
        buttonSize += QSizeF(0, (lineHeight*3.5));

171
    }
172
    return buttonSize.toSize();
con's avatar
con committed
173
174
}

mae's avatar
mae committed
175
176
177
178
179
QSize FancyToolButton::minimumSizeHint() const
{
    return QSize(8, 8);
}

180
181
182
183
184
185
186
187
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
188
189
190
191
192
FancyActionBar::FancyActionBar(QWidget *parent)
    : QWidget(parent)
{
    m_actionsLayout = new QVBoxLayout;

193
194
195
196
197
198
199
200
201
202
203
204
205
206
    QVBoxLayout *spacerLayout = new QVBoxLayout;
    spacerLayout->addLayout(m_actionsLayout);
    int sbh = ICore::instance()->statusBar()->height();
    spacerLayout->addSpacing(sbh);
    spacerLayout->setMargin(0);
    spacerLayout->setSpacing(0);

    QHBoxLayout *orientRightLayout = new QHBoxLayout;
    orientRightLayout->addStretch();
    orientRightLayout->setMargin(0);
    orientRightLayout->setSpacing(0);
    orientRightLayout->setContentsMargins(0, 0, 1, 0);
    orientRightLayout->addLayout(spacerLayout);
    setLayout(orientRightLayout);
con's avatar
con committed
207
208
}

209
210
211
212
213
214
215
216
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);

}
217
void FancyActionBar::insertAction(int index, QAction *action)
con's avatar
con committed
218
219
220
{
    FancyToolButton *toolButton = new FancyToolButton(this);
    toolButton->setDefaultAction(action);
221
    connect(action, SIGNAL(changed()), toolButton, SLOT(actionChanged()));
con's avatar
con committed
222
223
    m_actionsLayout->insertWidget(index, toolButton);
}
224

225
226
227
228
QLayout *FancyActionBar::actionsLayout() const
{
    return m_actionsLayout;
}