targetselector.cpp 3.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "targetselector.h"

#include <QtGui/QPainter>
#include <QtGui/QMouseEvent>
#include <QtGui/QFontMetrics>

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

using namespace ProjectExplorer::Internal;

TargetSelector::TargetSelector(QWidget *parent) :
    QWidget(parent),
    m_currentTargetIndex(-1)
{
        QFont f = font();
        f.setPixelSize(10);
        f.setBold(true);
        setFont(f);
}

void TargetSelector::addTarget(const QString &name)
{
    Target target;
    target.name = name;
    target.currentSubIndex = 0;
    m_targets.append(target);
    if (m_currentTargetIndex == -1)
        m_currentTargetIndex = m_targets.size() - 1;
    update();
}

void TargetSelector::removeTarget(int index)
{
    m_targets.removeAt(index);
    update();
}

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

QSize TargetSelector::minimumSizeHint() const
{
    return QSize((TARGET_WIDTH + 1) * m_targets.size() + ADDBUTTON_WIDTH + 2, TARGET_HEIGHT + 2);
}

void TargetSelector::mousePressEvent(QMouseEvent *event)
{
    if (event->x() > (TARGET_WIDTH + 1) * m_targets.size()) {
        // check for add button
        event->accept();
        emit addButtonClicked();
    } else {
        // find the clicked target button
        int x = 1;
        int index;
        for (index = 0; index < m_targets.size(); ++index) {
            if (event->x() <= x) {
                break;
            }
            x += TARGET_WIDTH + 1;
        }
        --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) {
                if ((event->x() - (TARGET_WIDTH + 1) * index) - 2 > TARGET_WIDTH / 2) {
                    m_targets[index].currentSubIndex = 1;
                } else {
                    m_targets[index].currentSubIndex = 0;
                }
            }
            m_currentTargetIndex = index;
            //TODO don't emit if nothing changed!
            update();
            emit currentIndexChanged(m_currentTargetIndex, m_targets.at(m_currentTargetIndex).currentSubIndex);
        } else {
            event->ignore();
        }
    }
}

void TargetSelector::paintEvent(QPaintEvent *event)
{
con's avatar
con committed
89
90
91
92
    static QPixmap unselected(":/projectexplorer/images/targetunselected.png");
    static QPixmap runselected(":/projectexplorer/images/targetrunselected.png");
    static QPixmap buildselected(":/projectexplorer/images/targetbuildselected.png");
    static QPixmap targetaddbutton(":/projectexplorer/images/targetaddbutton.png");
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    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());
    foreach (const Target &target, m_targets) {
        QPixmap *pixmap = &unselected;
        if (index == m_currentTargetIndex) {
            p.setPen(QColor(255, 255, 255));
            if (target.currentSubIndex == 0) {
                pixmap = &buildselected;
            } else {
                pixmap = &runselected;
            }
        } else {
            p.setPen(QColor(0, 0, 0));
        }
        p.drawPixmap(x, 1, *pixmap);
        p.drawText(x + (TARGET_WIDTH - fm.width(target.name))/2 + 1, 7 + fm.ascent(),
            target.name);
        x += TARGET_WIDTH;
        p.drawLine(x, 1, x, TARGET_HEIGHT);
        ++x;
        ++index;
    }
    // draw add button
    p.drawPixmap(x, 1, targetaddbutton);
}