targetsettingswidget.cpp 6.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Tobias Hunger's avatar
Tobias Hunger committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
25

26 27
#include "targetsettingswidget.h"

28
#include <utils/theme/theme.h>
29
#include <utils/stylehelper.h>
30

31 32
#include <QPainter>
#include <QPaintEvent>
33
#include <QPushButton>
34
#include <QVBoxLayout>
35

36 37
#include <cmath>

38 39
using namespace ProjectExplorer::Internal;

40
class TargetSettingsWidgetHeader : public QWidget
41
{
42 43 44 45 46 47 48 49 50
public:
    TargetSettingsWidgetHeader(QWidget *parent) : QWidget(parent)
    {
        QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        setSizePolicy(sizePolicy);
        setAutoFillBackground(true);
    }
51

52 53 54 55 56 57
    void paintEvent(QPaintEvent *event) override
    {
        if (!Utils::creatorTheme()->flag(Utils::Theme::FlatProjectsMode)) {
            QPainter p(this);
            static const QPixmap bg(Utils::StyleHelper::dpiSpecificImageFile(
                                        QLatin1String(":/projectexplorer/images/targetseparatorbackground.png")));
58
            const int tileCount = int(std::ceil(qreal(width()) / bg.width() * devicePixelRatio()));
59 60 61 62 63 64
            for (int tile = 0; tile < tileCount; ++tile)
                p.drawPixmap(tile * bg.width() / devicePixelRatio(), 0, bg);
        }
        QWidget::paintEvent(event);
    }
};
65

66 67 68 69
TargetSettingsWidget::TargetSettingsWidget(QWidget *parent) : QWidget(parent),
    m_targetSelector(new TargetSelector(this))
{
    auto header = new TargetSettingsWidgetHeader(this);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    auto separator = new QWidget(this);
    separator->setMinimumSize(QSize(0, 1));
    separator->setMaximumSize(QSize(QWIDGETSIZE_MAX, 1));
    separator->setAutoFillBackground(true);

    auto shadow = new QWidget(this);
    shadow->setMinimumSize(QSize(0, 2));
    shadow->setMaximumSize(QSize(QWIDGETSIZE_MAX, 2));
    shadow->setAutoFillBackground(true);

    m_scrollAreaWidgetContents = new QWidget(this);
    auto scrollLayout = new QVBoxLayout(m_scrollAreaWidgetContents);
    scrollLayout->setSpacing(0);
    scrollLayout->setContentsMargins(0, 0, 0, 0);

    auto verticalLayout = new QVBoxLayout(this);
    verticalLayout->setSpacing(0);
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    verticalLayout->addWidget(header);
    verticalLayout->addWidget(separator);
    verticalLayout->addWidget(shadow);
    verticalLayout->addWidget(m_scrollAreaWidgetContents);
93

94
    if (Utils::creatorTheme()->flag(Utils::Theme::FlatProjectsMode)) {
95 96
        separator->setVisible(false);
        shadow->setVisible(false);
97
    } else {
98 99 100 101 102 103 104 105 106 107
        QPalette separatorPalette;
        separatorPalette.setColor(QPalette::Window, QColor(115, 115, 115, 255));
        separator->setPalette(separatorPalette);

        QPalette shadowPalette;
        QLinearGradient shadowGradient(0, 0, 0, 2);
        shadowGradient.setColorAt(0, QColor(0, 0, 0, 60));
        shadowGradient.setColorAt(1, Qt::transparent);
        shadowPalette.setBrush(QPalette::All, QPalette::Window, shadowGradient);
        shadow->setPalette(shadowPalette);
108
    }
109

110
    auto headerLayout = new QHBoxLayout;
111
    headerLayout->setContentsMargins(5, 2, 0, 0);
112
    header->setLayout(headerLayout);
113

114
    auto buttonWidget = new QWidget(header);
115
    auto buttonLayout = new QVBoxLayout;
116 117 118
    buttonLayout->setContentsMargins(0, 0, 0, 0);
    buttonLayout->setSpacing(4);
    buttonWidget->setLayout(buttonLayout);
Tobias Hunger's avatar
Tobias Hunger committed
119
    m_addButton = new QPushButton(tr("Add Kit"), buttonWidget);
120
    buttonLayout->addWidget(m_addButton);
Tobias Hunger's avatar
Tobias Hunger committed
121
    m_manageButton = new QPushButton(tr("Manage Kits..."), buttonWidget);
122 123
    connect(m_manageButton, &QAbstractButton::clicked,
            this, &TargetSettingsWidget::manageButtonClicked);
124 125 126 127 128
    buttonLayout->addWidget(m_manageButton);
    headerLayout->addWidget(buttonWidget, 0, Qt::AlignVCenter);

    headerLayout->addWidget(m_targetSelector, 0, Qt::AlignBottom);
    headerLayout->addStretch(10);
129 130 131 132 133 134
    connect(m_targetSelector, &TargetSelector::currentChanged,
            this, &TargetSettingsWidget::currentChanged);
    connect(m_targetSelector, &TargetSelector::toolTipRequested,
            this, &TargetSettingsWidget::toolTipRequested);
    connect(m_targetSelector, &TargetSelector::menuShown,
            this, &TargetSettingsWidget::menuShown);
135 136
}

137
void TargetSettingsWidget::insertTarget(int index, int subIndex, const QString &name)
138
{
139
    m_targetSelector->insertTarget(index, subIndex, name);
140 141
}

Tobias Hunger's avatar
Tobias Hunger committed
142 143 144 145 146
void TargetSettingsWidget::renameTarget(int index, const QString &name)
{
    m_targetSelector->renameTarget(index, name);
}

147 148 149 150 151
void TargetSettingsWidget::removeTarget(int index)
{
    m_targetSelector->removeTarget(index);
}

Tobias Hunger's avatar
Tobias Hunger committed
152 153 154 155 156 157 158 159 160 161
void TargetSettingsWidget::setCurrentIndex(int index)
{
    m_targetSelector->setCurrentIndex(index);
}

void TargetSettingsWidget::setCurrentSubIndex(int index)
{
    m_targetSelector->setCurrentSubIndex(index);
}

162 163
void TargetSettingsWidget::setAddButtonEnabled(bool enabled)
{
164
    m_addButton->setEnabled(enabled);
165 166
}

167 168
void TargetSettingsWidget::setAddButtonMenu(QMenu *menu)
{
169
    m_addButton->setMenu(menu);
170 171
}

172 173 174 175 176
void TargetSettingsWidget::setTargetMenu(QMenu *menu)
{
    m_targetSelector->setTargetMenu(menu);
}

177 178 179 180 181 182 183
QString TargetSettingsWidget::targetNameAt(int index) const
{
    return m_targetSelector->targetAt(index).name;
}

void TargetSettingsWidget::setCentralWidget(QWidget *widget)
{
184
    if (m_centralWidget)
185
        m_scrollAreaWidgetContents->layout()->removeWidget(m_centralWidget);
186
    m_centralWidget = widget;
187
    m_scrollAreaWidgetContents->layout()->addWidget(m_centralWidget);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}

int TargetSettingsWidget::targetCount() const
{
    return m_targetSelector->targetCount();
}

int TargetSettingsWidget::currentIndex() const
{
    return m_targetSelector->currentIndex();
}

int TargetSettingsWidget::currentSubIndex() const
{
    return m_targetSelector->currentSubIndex();
}