targetselector.cpp 7.96 KB
Newer Older
1
2
#include "targetselector.h"

3
#include <utils/qtcassert.h>
4
#include <utils/stylehelper.h>
5

6
7
8
9
10
11
12
13
14
15
16
#include <QtGui/QPainter>
#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),
17
18
19
20
    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")),
21
    m_targetaddbuttondisabled(QLatin1String(":/projectexplorer/images/targetaddbutton_disabled.png")),
22
    m_targetremovebutton(QLatin1String(":/projectexplorer/images/targetremovebutton.png")),
23
    m_targetremovebuttondisabled(QLatin1String(":/projectexplorer/images/targetremovebutton_disabled.png")),
24
25
26
    m_currentTargetIndex(-1),
    m_addButtonEnabled(true),
    m_removeButtonEnabled(false)
27
{
Tobias Hunger's avatar
Tobias Hunger committed
28
29
30
31
    QFont f = font();
    f.setPixelSize(10);
    f.setBold(true);
    setFont(f);
32
33
34
35
}

void TargetSelector::addTarget(const QString &name)
{
36
37
38
39
40
41
42
    insertTarget(m_targets.count(), name);
}

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

43
44
45
    Target target;
    target.name = name;
    target.currentSubIndex = 0;
46
47
48

    m_targets.insert(index, target);

49
    if (m_currentTargetIndex == -1)
50
        setCurrentIndex(index);
51
52
    else if (m_currentTargetIndex >= index)
        setCurrentIndex(m_currentTargetIndex + 1);
Tobias Hunger's avatar
Tobias Hunger committed
53
54
55
    update();
}

56
57
void TargetSelector::removeTarget(int index)
{
58
59
    QTC_ASSERT(index >= 0 && index < m_targets.count(), return);

60
    m_targets.removeAt(index);
61
62
63
64
65
66
67

    if (m_currentTargetIndex >= m_targets.count())
        setCurrentIndex(m_targets.count() - 1);
    else if (m_currentTargetIndex >= index)
        // force a signal since the target pointed to has changed:
        emit currentChanged(m_currentTargetIndex, m_targets.at(m_currentTargetIndex).currentSubIndex);

Tobias Hunger's avatar
Tobias Hunger committed
68
69
70
71
72
73
74
75
76
77
    update();
}

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

78
79
80
    if (index == -1 && !m_targets.isEmpty())
        return;

Tobias Hunger's avatar
Tobias Hunger committed
81
82
83
    m_currentTargetIndex = index;

    update();
84
    emit currentChanged(m_currentTargetIndex,
Tobias Hunger's avatar
Tobias Hunger committed
85
86
87
                             m_currentTargetIndex >= 0 ? m_targets.at(m_currentTargetIndex).currentSubIndex : -1);
}

88
89
90
91
92
93
94
95
96
97
void TargetSelector::setAddButtonEnabled(bool enabled)
{
    m_addButtonEnabled = enabled;
}

void TargetSelector::setRemoveButtonEnabled(bool enabled)
{
    m_removeButtonEnabled = enabled;
}

Tobias Hunger's avatar
Tobias Hunger committed
98
99
100
101
void TargetSelector::setCurrentSubIndex(int subindex)
{
    if (subindex < 0 ||
        subindex >= 2 ||
102
        m_currentTargetIndex < 0 ||
Tobias Hunger's avatar
Tobias Hunger committed
103
104
105
106
        subindex == m_targets.at(m_currentTargetIndex).currentSubIndex)
        return;
    m_targets[m_currentTargetIndex].currentSubIndex = subindex;

107
    update();
108
    emit currentChanged(m_currentTargetIndex,
Tobias Hunger's avatar
Tobias Hunger committed
109
                             m_targets.at(m_currentTargetIndex).currentSubIndex);
110
111
112
113
114
115
116
}

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

117
118
119
120
121
122
123
124
125
126
bool TargetSelector::isAddButtonEnabled() const
{
    return m_addButtonEnabled;
}

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

127
128
129
130
131
132
133
134
135
136
137
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;
}

138
139
QSize TargetSelector::minimumSizeHint() const
{
140
    return QSize((targetWidth() + 1) * m_targets.size() + (ADDBUTTON_WIDTH + 1) * 2 + 1, TARGET_HEIGHT + 2);
141
142
143
144
}

void TargetSelector::mousePressEvent(QMouseEvent *event)
{
145
146
147
148
    if (event->x() < ADDBUTTON_WIDTH) {
        event->accept();
        if (m_removeButtonEnabled)
            emit removeButtonClicked();
149
    } else if (event->x() > ADDBUTTON_WIDTH + (targetWidth() + 1) * m_targets.size()) {
150
151
        // check for add button
        event->accept();
152
153
        if (m_addButtonEnabled)
            emit addButtonClicked();
154
155
    } else {
        // find the clicked target button
156
        int x = ADDBUTTON_WIDTH;
157
158
159
160
161
        int index;
        for (index = 0; index < m_targets.size(); ++index) {
            if (event->x() <= x) {
                break;
            }
162
            x += targetWidth() + 1;
163
164
165
166
167
168
        }
        --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) {
169
                if ((event->x() - (ADDBUTTON_WIDTH + (targetWidth() + 1) * index)) - 2 > targetWidth() / 2) {
170
171
172
173
174
175
176
177
                    m_targets[index].currentSubIndex = 1;
                } else {
                    m_targets[index].currentSubIndex = 0;
                }
            }
            m_currentTargetIndex = index;
            //TODO don't emit if nothing changed!
            update();
178
            emit currentChanged(m_currentTargetIndex, m_targets.at(m_currentTargetIndex).currentSubIndex);
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        } 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);
    p.drawLine(1, size.height() - 1, size.width() - 2, size.height() - 1);
    p.drawLine(0, 1, 0, size.height() - 2);
    p.drawLine(size.width() - 1, 1, size.width() - 1, size.height() - 2);
    //draw targets
    int x = 1;
    int index = 0;
    QFontMetrics fm(font());
201
202
203
204
    if (m_removeButtonEnabled)
        p.drawPixmap(x, 1, m_targetremovebutton);
    else
        p.drawPixmap(x, 1, m_targetremovebuttondisabled);
205
206
207
208
    x += m_targetremovebutton.width();
    p.setPen(QColor(0, 0, 0));
    p.drawLine(x, 1, x, TARGET_HEIGHT);
    x += 1;
209
210
211

    const QString runString = runButtonString();
    const QString buildString = buildButtonString();
212
    foreach (const Target &target, m_targets) {
213
214
        QImage image = m_unselected;
        bool buildSelected = target.currentSubIndex == 0;
215
216
        if (index == m_currentTargetIndex) {
            p.setPen(QColor(255, 255, 255));
217
218
            if (buildSelected) {
                image = m_buildselected;
219
            } else {
220
                image= m_runselected;
221
222
            }
        }
223
224
225
226

        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(),
227
            target.name);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

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

        p.drawText(textRect, Qt::AlignHCenter, runString);

        // Run
        textRect.moveLeft(x + targetWidth()/2 - 2 * margin);
        if (index!=m_currentTargetIndex)
            p.setPen(QColor(0x555555));
        else
            p.setPen(buildSelected ? Qt::white: Qt::black);
        p.drawText(textRect, Qt::AlignHCenter, buildString);

        x += targetWidth();

        p.setPen(index == m_currentTargetIndex ? QColor(0x222222) : QColor(0xcccccc));
252
253
254
255
256
        p.drawLine(x, 1, x, TARGET_HEIGHT);
        ++x;
        ++index;
    }
    // draw add button
257
258
259
260
    if (m_addButtonEnabled)
        p.drawPixmap(x, 1, m_targetaddbutton);
    else
        p.drawPixmap(x, 1, m_targetaddbuttondisabled);
261
}