Skip to content
Snippets Groups Projects
qt4projectconfigwidget.cpp 13.8 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** Commercial Usage
** 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.
** GNU Lesser General Public License Usage
** 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.
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

#include "qt4projectconfigwidget.h"
hjk's avatar
hjk committed

#include "makestep.h"
#include "qmakestep.h"
con's avatar
con committed
#include "qt4project.h"
#include "qt4projectmanagerconstants.h"
con's avatar
con committed
#include "qt4projectmanager.h"
#include "ui_qt4projectconfigwidget.h"
#include <coreplugin/icore.h>
#include <coreplugin/mainwindow.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed

#include <QtGui/QFileDialog>

namespace {
bool debug = false;
}

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

Qt4ProjectConfigWidget::Qt4ProjectConfigWidget(Qt4Project *project)
con's avatar
con committed
    : BuildStepConfigWidget(),
      m_pro(project)
{
    m_ui = new Ui::Qt4ProjectConfigWidget();
con's avatar
con committed
    m_ui->setupUi(this);

    // fix the layout
    QAbstractButton *browseButton = m_ui->shadowBuildDirEdit->buttonAtIndex(0);
#ifdef Q_OS_WIN
con's avatar
con committed
    browseButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
#endif
    m_ui->gridLayout->addWidget(browseButton, 4, 2);
    int minimumHeight = qMax(m_ui->qtVersionComboBox->sizeHint().height(), m_ui->manageQtVersionPushButtons->sizeHint().height());
    Qt::Alignment labelAlignment = Qt::Alignment(style()->styleHint(QStyle::SH_FormLayoutLabelAlignment));
    for (int i = 0; i < m_ui->gridLayout->rowCount(); ++i) {
        m_ui->gridLayout->setRowMinimumHeight(i, minimumHeight);
        QLayoutItem *item = m_ui->gridLayout->itemAtPosition(i, 0);
        if (item)
            item->setAlignment(labelAlignment);
    }

    m_ui->shadowBuildDirEdit->setPromptDialogTitle(tr("Shadow Build Directory"));
    m_ui->shadowBuildDirEdit->setExpectedKind(Core::Utils::PathChooser::Directory);
con's avatar
con committed
    m_ui->invalidQtWarningLabel->setVisible(false);

    connect(m_ui->nameLineEdit, SIGNAL(textEdited(QString)),
            this, SLOT(changeConfigName(QString)));

    connect(m_ui->shadowBuildCheckBox, SIGNAL(clicked(bool)),
            this, SLOT(shadowBuildCheckBoxClicked(bool)));

    connect(m_ui->shadowBuildDirEdit, SIGNAL(beforeBrowsing()),
            this, SLOT(onBeforeBeforeShadowBuildDirBrowsed()));
con's avatar
con committed

con's avatar
con committed
    connect(m_ui->shadowBuildDirEdit, SIGNAL(changed(QString)),
con's avatar
con committed
            this, SLOT(shadowBuildLineEditTextChanged()));

    connect(m_ui->qtVersionComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(qtVersionComboBoxCurrentIndexChanged(QString)));

    connect(m_ui->toolChainComboBox, SIGNAL(activated(int)),
            this, SLOT(selectToolChain(int)));

con's avatar
con committed
    connect(m_ui->importLabel, SIGNAL(linkActivated(QString)),
            this, SLOT(importLabelClicked()));

    connect(m_ui->manageQtVersionPushButtons, SIGNAL(clicked()),
            this, SLOT(manageQtVersions()));

    QtVersionManager *vm = QtVersionManager::instance();

    connect(vm, SIGNAL(qtVersionsChanged()),
con's avatar
con committed
            this, SLOT(setupQtVersionsComboBox()));
}

Qt4ProjectConfigWidget::~Qt4ProjectConfigWidget()
con's avatar
con committed
{
    delete m_ui;
}

void Qt4ProjectConfigWidget::manageQtVersions()
    Core::ICore *core = Core::ICore::instance();
    core->showOptionsDialog(Constants::QT_CATEGORY, Constants::QTVERSION_PAGE);
QString Qt4ProjectConfigWidget::displayName() const
con's avatar
con committed
{
    return tr("General");
}

void Qt4ProjectConfigWidget::init(const QString &buildConfiguration)
con's avatar
con committed
{
    if (debug)
        qDebug() << "Qt4ProjectConfigWidget::init()";
con's avatar
con committed

    m_buildConfiguration = buildConfiguration;

    m_ui->nameLineEdit->setText(m_pro->displayNameFor(m_buildConfiguration));

    setupQtVersionsComboBox();

    bool shadowBuild = m_pro->value(buildConfiguration, "useShadowBuild").toBool();
    m_ui->shadowBuildCheckBox->setChecked(shadowBuild);
    m_ui->shadowBuildDirEdit->setEnabled(shadowBuild);
    m_ui->shadowBuildDirEdit->setPath(m_pro->buildDirectory(buildConfiguration));
    updateImportLabel();
    updateToolChainCombo();
con's avatar
con committed
}

void Qt4ProjectConfigWidget::changeConfigName(const QString &newName)
con's avatar
con committed
{
    m_pro->setDisplayNameFor(m_buildConfiguration, newName);
}

void Qt4ProjectConfigWidget::setupQtVersionsComboBox()
con's avatar
con committed
{
    if (m_buildConfiguration.isEmpty()) // not yet initialized
        return;

hjk's avatar
hjk committed
    disconnect(m_ui->qtVersionComboBox, SIGNAL(currentIndexChanged(QString)),
        this, SLOT(qtVersionComboBoxCurrentIndexChanged(QString)));
con's avatar
con committed

    m_ui->qtVersionComboBox->clear();
    m_ui->qtVersionComboBox->addItem(tr("Default Qt Version"), 0);

hjk's avatar
hjk committed
    if (m_pro->qtVersionId(m_buildConfiguration) == 0) {
con's avatar
con committed
        m_ui->qtVersionComboBox->setCurrentIndex(0);
        m_ui->invalidQtWarningLabel->setVisible(false);
    }
    // Add Qt Versions to the combo box
    QtVersionManager *vm = QtVersionManager::instance();
con's avatar
con committed
    const QList<QtVersion *> &versions = vm->versions();
hjk's avatar
hjk committed
    for (int i = 0; i < versions.size(); ++i) {
con's avatar
con committed
        m_ui->qtVersionComboBox->addItem(versions.at(i)->name(), versions.at(i)->uniqueId());

hjk's avatar
hjk committed
        if (versions.at(i)->uniqueId() == m_pro->qtVersionId(m_buildConfiguration)) {
            m_ui->qtVersionComboBox->setCurrentIndex(i + 1);
con's avatar
con committed
            m_ui->invalidQtWarningLabel->setVisible(!versions.at(i)->isValid());
        }
    }

    // And connect again
hjk's avatar
hjk committed
    connect(m_ui->qtVersionComboBox, SIGNAL(currentIndexChanged(QString)),
        this, SLOT(qtVersionComboBoxCurrentIndexChanged(QString)));
con's avatar
con committed
}

void Qt4ProjectConfigWidget::onBeforeBeforeShadowBuildDirBrowsed()
con's avatar
con committed
{
    QString initialDirectory = QFileInfo(m_pro->file()->fileName()).absolutePath();
    if (!initialDirectory.isEmpty())
        m_ui->shadowBuildDirEdit->setInitialBrowsePathBackup(initialDirectory);
con's avatar
con committed
}

void Qt4ProjectConfigWidget::shadowBuildCheckBoxClicked(bool checked)
con's avatar
con committed
{
    m_ui->shadowBuildDirEdit->setEnabled(checked);
con's avatar
con committed
    bool b = m_ui->shadowBuildCheckBox->isChecked();
    m_pro->setValue(m_buildConfiguration, "useShadowBuild", b);
    if (b)
        m_pro->setValue(m_buildConfiguration, "buildDirectory", m_ui->shadowBuildDirEdit->path());
con's avatar
con committed
    else
        m_pro->setValue(m_buildConfiguration, "buildDirectory", QVariant(QString::null));
}

void Qt4ProjectConfigWidget::updateImportLabel()
con's avatar
con committed
{
    m_ui->importLabel->setVisible(false);
    if (m_ui->shadowBuildCheckBox->isChecked()) {
        QString qtPath = QtVersionManager::findQtVersionFromMakefile(m_ui->shadowBuildDirEdit->path());
hjk's avatar
hjk committed
        if (!qtPath.isEmpty()) {
con's avatar
con committed
            m_ui->importLabel->setVisible(true);
        }
    }
void Qt4ProjectConfigWidget::shadowBuildLineEditTextChanged()
{
    if (m_pro->value(m_buildConfiguration, "buildDirectory").toString() == m_ui->shadowBuildDirEdit->path())
    m_pro->setValue(m_buildConfiguration, "buildDirectory", m_ui->shadowBuildDirEdit->path());
    // if the directory already exists
    // check if we have a build in there and
    // offer to import it
    updateImportLabel();

    m_pro->invalidateCachedTargetInformation();
con's avatar
con committed

//    QFileInfo fi(m_ui->shadowBuildDirEdit->path());
con's avatar
con committed
//    if (fi.exists()) {
//        m_ui->shadowBuildLineEdit->setStyleSheet("");
//        m_ui->shadowBuildLineEdit->setToolTip("");
//    } else {
//        m_ui->shadowBuildLineEdit->setStyleSheet("background: red;");
//        m_ui->shadowBuildLineEdit->setToolTip(tr("Directory does not exist."));
//    }
}

void Qt4ProjectConfigWidget::importLabelClicked()
con's avatar
con committed
{
    if (m_ui->shadowBuildCheckBox->isChecked()) {
        QString directory = m_ui->shadowBuildDirEdit->path();
con's avatar
con committed
        if (!directory.isEmpty()) {
            QString qtPath = QtVersionManager::findQtVersionFromMakefile(directory);
con's avatar
con committed
            if (!qtPath.isEmpty()) {
                QtVersionManager *vm = QtVersionManager::instance();
con's avatar
con committed
                QtVersion *version = vm->qtVersionForDirectory(qtPath);
                if (!version) {
                    version = new QtVersion(QFileInfo(qtPath).baseName(), qtPath);
                    vm->addVersion(version);
                }

                QPair<QtVersion::QmakeBuildConfig, QStringList> result =
                        QtVersionManager::scanMakeFile(directory, version->defaultBuildConfig());
                QtVersion::QmakeBuildConfig qmakeBuildConfig = result.first;
                QStringList additionalArguments = result.second;
con's avatar
con committed

                // So we got all the information now apply it...
                m_pro->setQtVersion(m_buildConfiguration, version->uniqueId());
                // Combo box will be updated at the end

                QMakeStep *qmakeStep = m_pro->qmakeStep();
                qmakeStep->setValue(m_buildConfiguration, "qmakeArgs", additionalArguments);
con's avatar
con committed
                MakeStep *makeStep = m_pro->makeStep();

                m_pro->setValue(m_buildConfiguration, "buildConfiguration", int(qmakeBuildConfig));
con's avatar
con committed
                // Adjust command line arguments, this is ugly as hell
                // If we are switching to BuildAll we want "release" in there and no "debug"
                // or "debug" in there and no "release"
                // If we are switching to not BuildAl we want neither "release" nor "debug" in there
                QStringList makeCmdArguments = makeStep->value(m_buildConfiguration, "makeargs").toStringList();
                bool debug = qmakeBuildConfig & QtVersion::DebugBuild;
                if (qmakeBuildConfig & QtVersion::BuildAll) {
                    makeCmdArguments.removeAll(debug ? "release" : "debug");
                    if (!makeCmdArguments.contains(debug ? "debug" : "release"))
                        makeCmdArguments.append(debug ? "debug" : "release");
                } else {
                    makeCmdArguments.removeAll("debug");
Michael Karcher's avatar
Michael Karcher committed
                    makeCmdArguments.removeAll("release");
con's avatar
con committed
                }
                makeStep->setValue(m_buildConfiguration, "makeargs", makeCmdArguments);
            }
        }
    }
    setupQtVersionsComboBox();
}

void Qt4ProjectConfigWidget::qtVersionComboBoxCurrentIndexChanged(const QString &)
con's avatar
con committed
{
    //Qt Version
    int newQtVersion;
    if (m_ui->qtVersionComboBox->currentIndex() == 0) {
        newQtVersion = 0;
    } else {
        newQtVersion = m_ui->qtVersionComboBox->itemData(m_ui->qtVersionComboBox->currentIndex()).toInt();
    }
    QtVersionManager *vm = QtVersionManager::instance();
    bool isValid = vm->version(newQtVersion)->isValid();
con's avatar
con committed
    m_ui->invalidQtWarningLabel->setVisible(!isValid);
hjk's avatar
hjk committed
    if (newQtVersion != m_pro->qtVersionId(m_buildConfiguration)) {
con's avatar
con committed
        m_pro->setQtVersion(m_buildConfiguration, newQtVersion);
        updateToolChainCombo();
        m_pro->update();
con's avatar
con committed
    }
}

void Qt4ProjectConfigWidget::updateToolChainCombo()
{
    m_ui->toolChainComboBox->clear();
    QList<ProjectExplorer::ToolChain::ToolChainType> toolchains = m_pro->qtVersion(m_buildConfiguration)->possibleToolChainTypes();
    foreach (ProjectExplorer::ToolChain::ToolChainType toolchain, toolchains) {
        switch (toolchain) {
        case ProjectExplorer::ToolChain::GCC:
            m_ui->toolChainComboBox->addItem(tr("gcc"), qVariantFromValue(ProjectExplorer::ToolChain::GCC));
            break;
        case ProjectExplorer::ToolChain::LinuxICC:
            m_ui->toolChainComboBox->addItem(tr("icc"), qVariantFromValue(ProjectExplorer::ToolChain::LinuxICC));
            break;
        case ProjectExplorer::ToolChain::MinGW:
            m_ui->toolChainComboBox->addItem(tr("mingw"), qVariantFromValue(ProjectExplorer::ToolChain::MinGW));
            break;
        case ProjectExplorer::ToolChain::MSVC:
            m_ui->toolChainComboBox->addItem(tr("msvc"), qVariantFromValue(ProjectExplorer::ToolChain::MSVC));
            break;
        case ProjectExplorer::ToolChain::WINCE:
            m_ui->toolChainComboBox->addItem(tr("wince"), qVariantFromValue(ProjectExplorer::ToolChain::WINCE));
            break;
#ifdef QTCREATOR_WITH_S60
        case ProjectExplorer::ToolChain::WINSCW:
            m_ui->toolChainComboBox->addItem(tr("winscw"), qVariantFromValue(ProjectExplorer::ToolChain::WINSCW));
            break;
        case ProjectExplorer::ToolChain::GCCE:
            m_ui->toolChainComboBox->addItem(tr("gcce"), qVariantFromValue(ProjectExplorer::ToolChain::GCCE));
        case ProjectExplorer::ToolChain::OTHER:
        case ProjectExplorer::ToolChain::INVALID:
        case ProjectExplorer::ToolChain::UNKNOWN:
            break;
        }
    }
    m_ui->toolChainComboBox->setEnabled(toolchains.size() > 1);
    setToolChain(toolchains.indexOf(m_pro->toolChainType(m_buildConfiguration)));
}

void Qt4ProjectConfigWidget::selectToolChain(int index)
{
    setToolChain(index);
    m_pro->update();
}

void Qt4ProjectConfigWidget::setToolChain(int index)
{
    ProjectExplorer::ToolChain::ToolChainType selectedToolChainType =
        m_ui->toolChainComboBox->itemData(index,
            Qt::UserRole).value<ProjectExplorer::ToolChain::ToolChainType>();
con's avatar
con committed
    m_pro->setToolChainType(m_buildConfiguration, selectedToolChainType);
    if (m_ui->toolChainComboBox->currentIndex() != index)
        m_ui->toolChainComboBox->setCurrentIndex(index);