targetselector.cpp 9.69 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
Tobias Hunger's avatar
Tobias Hunger committed
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
18
19
**
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Tobias Hunger's avatar
Tobias Hunger committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Tobias Hunger's avatar
Tobias Hunger committed
30
31
32
**
**************************************************************************/

33
34
#include "targetselector.h"

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

38
#include <QtGui/QPainter>
39
#include <QtGui/QMenu>
40
41
42
43
44
45
46
47
48
49
#include <QtGui/QMouseEvent>
#include <QtGui/QFontMetrics>

static const int TARGET_HEIGHT = 43;
static const int ADDBUTTON_WIDTH = 27;

using namespace ProjectExplorer::Internal;

TargetSelector::TargetSelector(QWidget *parent) :
    QWidget(parent),
50
51
52
53
    m_unselected(QLatin1String(":/projectexplorer/images/targetunselected.png")),
    m_runselected(QLatin1String(":/projectexplorer/images/targetrunselected.png")),
    m_buildselected(QLatin1String(":/projectexplorer/images/targetbuildselected.png")),
    m_targetaddbutton(QLatin1String(":/projectexplorer/images/targetaddbutton.png")),
54
    m_targetaddbuttondisabled(QLatin1String(":/projectexplorer/images/targetaddbutton_disabled.png")),
55
    m_targetremovebutton(QLatin1String(":/projectexplorer/images/targetremovebutton.png")),
56
    m_targetremovebuttondisabled(QLatin1String(":/projectexplorer/images/targetremovebutton_disabled.png")),
57
58
    m_currentTargetIndex(-1),
    m_addButtonEnabled(true),
59
60
    m_removeButtonEnabled(false),
    m_addButtonMenu(0)
61
{
Tobias Hunger's avatar
Tobias Hunger committed
62
63
64
65
    QFont f = font();
    f.setPixelSize(10);
    f.setBold(true);
    setFont(f);
66
67
68
69
}

void TargetSelector::addTarget(const QString &name)
{
70
71
72
73
74
75
76
    insertTarget(m_targets.count(), name);
}

void TargetSelector::insertTarget(int index, const QString &name)
{
    QTC_ASSERT(index >= 0 && index <= m_targets.count(), return);

77
78
79
    Target target;
    target.name = name;
    target.currentSubIndex = 0;
80
81
82

    m_targets.insert(index, target);

dt's avatar
dt committed
83
    if (m_currentTargetIndex >= index)
84
        setCurrentIndex(m_currentTargetIndex + 1);
Tobias Hunger's avatar
Tobias Hunger committed
85
86
87
    update();
}

88
89
void TargetSelector::removeTarget(int index)
{
90
91
    QTC_ASSERT(index >= 0 && index < m_targets.count(), return);

92
    m_targets.removeAt(index);
93

dt's avatar
dt committed
94
    if (m_currentTargetIndex > index) {
dt's avatar
dt committed
95
96
        --m_currentTargetIndex;
        // force a signal since the index has changed
97
        emit currentChanged(m_currentTargetIndex, m_targets.at(m_currentTargetIndex).currentSubIndex);
dt's avatar
dt committed
98
    }
Tobias Hunger's avatar
Tobias Hunger committed
99
100
101
102
103
104
105
106
107
108
    update();
}

void TargetSelector::setCurrentIndex(int index)
{
    if (index < -1 ||
        index >= m_targets.count() ||
        index == m_currentTargetIndex)
        return;

109
110
111
    if (index == -1 && !m_targets.isEmpty())
        return;

Tobias Hunger's avatar
Tobias Hunger committed
112
113
114
    m_currentTargetIndex = index;

    update();
115
    emit currentChanged(m_currentTargetIndex,
Tobias Hunger's avatar
Tobias Hunger committed
116
117
118
                             m_currentTargetIndex >= 0 ? m_targets.at(m_currentTargetIndex).currentSubIndex : -1);
}

119
120
121
void TargetSelector::setAddButtonEnabled(bool enabled)
{
    m_addButtonEnabled = enabled;
122
    update();
123
124
125
126
127
}

void TargetSelector::setRemoveButtonEnabled(bool enabled)
{
    m_removeButtonEnabled = enabled;
128
    update();
129
130
}

131
132
133
134
135
void TargetSelector::setAddButtonMenu(QMenu *menu)
{
    m_addButtonMenu = menu;
}

Tobias Hunger's avatar
Tobias Hunger committed
136
137
138
139
void TargetSelector::setCurrentSubIndex(int subindex)
{
    if (subindex < 0 ||
        subindex >= 2 ||
140
        m_currentTargetIndex < 0 ||
Tobias Hunger's avatar
Tobias Hunger committed
141
142
143
144
        subindex == m_targets.at(m_currentTargetIndex).currentSubIndex)
        return;
    m_targets[m_currentTargetIndex].currentSubIndex = subindex;

145
    update();
146
    emit currentChanged(m_currentTargetIndex,
Tobias Hunger's avatar
Tobias Hunger committed
147
                             m_targets.at(m_currentTargetIndex).currentSubIndex);
148
149
150
151
152
153
154
}

TargetSelector::Target TargetSelector::targetAt(int index) const
{
    return m_targets.at(index);
}

155
156
157
158
159
160
161
162
163
164
bool TargetSelector::isAddButtonEnabled() const
{
    return m_addButtonEnabled;
}

bool TargetSelector::isRemoveButtonEnabled() const
{
    return m_removeButtonEnabled;
}

165
166
167
168
169
170
171
172
173
174
175
int TargetSelector::targetWidth() const
{
    static int width = -1;
    if (width < 0) {
        QFontMetrics fm = fontMetrics();
        width = qMax(fm.width(runButtonString()), fm.width(buildButtonString()));
        width = qMax(129, width * 2 + 31);
    }
    return width;
}

176
177
QSize TargetSelector::minimumSizeHint() const
{
178
    return QSize((targetWidth() + 1) * m_targets.size() + (ADDBUTTON_WIDTH + 1) * 2 + 3, TARGET_HEIGHT + 4);
179
180
181
182
}

void TargetSelector::mousePressEvent(QMouseEvent *event)
{
183
184
185
186
    if (event->x() < ADDBUTTON_WIDTH) {
        event->accept();
        if (m_removeButtonEnabled)
            emit removeButtonClicked();
187
    } else if (event->x() > ADDBUTTON_WIDTH + (targetWidth() + 1) * m_targets.size()) {
188
189
        // check for add button
        event->accept();
190
191
        if (m_addButtonEnabled && m_addButtonMenu)
            m_addButtonMenu->popup(mapToGlobal(event->pos()));
192
193
    } else {
        // find the clicked target button
194
        int x = ADDBUTTON_WIDTH;
195
196
197
198
199
        int index;
        for (index = 0; index < m_targets.size(); ++index) {
            if (event->x() <= x) {
                break;
            }
200
            x += targetWidth() + 1;
201
202
203
204
205
206
        }
        --index;
        if (index >= 0 && index < m_targets.size()) {
            // handle clicked target
            // check if user clicked on Build or Run
            if (event->y() > TARGET_HEIGHT * 3/5) {
207
                if ((event->x() - (ADDBUTTON_WIDTH + (targetWidth() + 1) * index)) - 2 > targetWidth() / 2) {
208
209
210
211
212
213
214
215
                    m_targets[index].currentSubIndex = 1;
                } else {
                    m_targets[index].currentSubIndex = 0;
                }
            }
            m_currentTargetIndex = index;
            //TODO don't emit if nothing changed!
            update();
216
            emit currentChanged(m_currentTargetIndex, m_targets.at(m_currentTargetIndex).currentSubIndex);
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        } else {
            event->ignore();
        }
    }
}

void TargetSelector::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter p(this);
    p.setPen(QColor(89, 89, 89));
    QSize size = minimumSizeHint();
    //draw frame
    p.drawLine(1, 0, size.width() - 2, 0);
232
233
234
235
236
237
238
239
240
241
    p.drawLine(1, size.height() - 3, size.width() - 2, size.height() - 3);
    p.drawLine(1, 1, 1, size.height() - 4);
    p.drawLine(size.width() - 2, 1, size.width() - 2, size.height() - 4);

    //draw shadow
    p.setPen(QColor(0, 0, 0, 50));
    p.drawLine(1, size.height() - 2, size.width() - 2, size.height() - 2);
    p.setPen(QColor(0, 0, 0, 20));
    p.drawLine(0, size.height() - 2, 0, size.height() - 9);
    p.drawLine(size.width()-1, size.height() - 2, size.width()-1, size.height() - 9);
242
    p.drawLine(1, size.height() - 1, size.width() - 2, size.height() - 1);
243

244
    //draw targets
245
    int x = 2;
246
247
    int index = 0;
    QFontMetrics fm(font());
248
249
250
251
    if (m_removeButtonEnabled)
        p.drawPixmap(x, 1, m_targetremovebutton);
    else
        p.drawPixmap(x, 1, m_targetremovebuttondisabled);
252
253
254
255
    x += m_targetremovebutton.width();
    p.setPen(QColor(0, 0, 0));
    p.drawLine(x, 1, x, TARGET_HEIGHT);
    x += 1;
256
257
258

    const QString runString = runButtonString();
    const QString buildString = buildButtonString();
259
    foreach (const Target &target, m_targets) {
260
261
        QImage image = m_unselected;
        bool buildSelected = target.currentSubIndex == 0;
262
263
        if (index == m_currentTargetIndex) {
            p.setPen(QColor(255, 255, 255));
264
265
            if (buildSelected) {
                image = m_buildselected;
266
            } else {
267
                image= m_runselected;
268
            }
269
270
        } else {
            p.setPen(Qt::black);
271
        }
272
273
274
275

        QRect buttonRect(x, 1, targetWidth() , image.height());
        Utils::StyleHelper::drawCornerImage(image, &p, buttonRect, 16, 0, 16, 0);
        p.drawText(x + (targetWidth()- fm.width(target.name))/2 + 1, 7 + fm.ascent(),
276
            target.name);
277
278
279
280

        // Build
        int margin = 2; // position centered within the rounded buttons
        QFontMetrics fm = fontMetrics();
281
        QRect textRect(x + margin, size.height() - fm.height() - 7, targetWidth()/2, fm.height());
282
283
284
285
286
287
        p.setPen(buildSelected ? Qt::black : Qt::white);
        if (index!=m_currentTargetIndex)
            p.setPen(QColor(0x555555));
        else
            p.setPen(buildSelected ? Qt::black : Qt::white);

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
288
        p.drawText(textRect, Qt::AlignHCenter, buildString);
289
290
291
292
293
294
295

        // Run
        textRect.moveLeft(x + targetWidth()/2 - 2 * margin);
        if (index!=m_currentTargetIndex)
            p.setPen(QColor(0x555555));
        else
            p.setPen(buildSelected ? Qt::white: Qt::black);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
296
        p.drawText(textRect, Qt::AlignHCenter, runString);
297
298
299
300

        x += targetWidth();

        p.setPen(index == m_currentTargetIndex ? QColor(0x222222) : QColor(0xcccccc));
301
302
303
304
305
        p.drawLine(x, 1, x, TARGET_HEIGHT);
        ++x;
        ++index;
    }
    // draw add button
306
307
308
309
    if (m_addButtonEnabled)
        p.drawPixmap(x, 1, m_targetaddbutton);
    else
        p.drawPixmap(x, 1, m_targetaddbuttondisabled);
310
}