projectwindow.cpp 25.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "projectwindow.h"
hjk's avatar
hjk committed
31 32

#include "project.h"
33
#include "environment.h"
con's avatar
con committed
34 35 36 37 38
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "iprojectproperties.h"
#include "session.h"
#include "projecttreewidget.h"
dt's avatar
dt committed
39
#include "runconfiguration.h"
40
#include "buildconfiguration.h"
con's avatar
con committed
41 42 43 44

#include <coreplugin/minisplitter.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
45
#include <coreplugin/ifile.h>
46
#include <extensionsystem/pluginmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
47
#include <utils/qtcassert.h>
con's avatar
con committed
48
#include <utils/styledbar.h>
49
#include <utils/stylehelper.h>
con's avatar
con committed
50

51
#include <QtGui/QApplication>
con's avatar
con committed
52 53
#include <QtGui/QBoxLayout>
#include <QtGui/QComboBox>
54
#include <QtGui/QScrollArea>
dt's avatar
dt committed
55
#include <QtGui/QLabel>
dt's avatar
dt committed
56 57
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
58 59
#include <QtGui/QMenu>

con's avatar
con committed
60 61 62 63
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
Tobias Hunger's avatar
Tobias Hunger committed
64
const int ICON_SIZE(64);
Tobias Hunger's avatar
Tobias Hunger committed
65 66 67 68

const int ABOVE_HEADING_MARGIN(10);
const int ABOVE_CONTENTS_MARGIN(4);
const int BELOW_CONTENTS_MARGIN(16);
con's avatar
con committed
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
///
// OnePixelBlackLine
///

class OnePixelBlackLine : public QWidget
{
public:
    OnePixelBlackLine(QWidget *parent)
        : QWidget(parent)
    {
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        setMinimumHeight(1);
        setMaximumHeight(1);
    }
    void paintEvent(QPaintEvent *e)
    {
Tobias Hunger's avatar
Tobias Hunger committed
87
        Q_UNUSED(e);
88
        QPainter p(this);
Tobias Hunger's avatar
Tobias Hunger committed
89
        p.fillRect(contentsRect(), QBrush(Utils::StyleHelper::borderColor()));
90 91 92
    }
};

93 94 95 96 97
///
// PanelsWidget::Panel
///

PanelsWidget::Panel::Panel(QWidget * w) :
Tobias Hunger's avatar
Tobias Hunger committed
98
    iconLabel(0), lineWidget(0), nameLabel(0), panelWidget(w)
99 100 101 102
{ }

PanelsWidget::Panel::~Panel()
{
Tobias Hunger's avatar
Tobias Hunger committed
103 104
    delete iconLabel;
    delete lineWidget;
105
    delete nameLabel;
Tobias Hunger's avatar
Tobias Hunger committed
106
    // do not delete panelWidget, we do not own it!
107 108
}

109 110 111 112
///
// PanelsWidget
///

Tobias Hunger's avatar
Tobias Hunger committed
113 114 115
PanelsWidget::PanelsWidget(QWidget *parent) :
    QScrollArea(parent),
    m_root(new QWidget(this))
dt's avatar
dt committed
116
{
117
    // We want a 900px wide widget with and the scrollbar at the
Tobias Hunger's avatar
Tobias Hunger committed
118
    // side of the screen.
119
    m_root->setFixedWidth(900);
Tobias Hunger's avatar
Tobias Hunger committed
120
    // The layout holding the individual panels:
Tobias Hunger's avatar
Tobias Hunger committed
121 122 123
    m_layout = new QGridLayout(m_root);
    m_layout->setColumnMinimumWidth(0, ICON_SIZE + 4);
    m_layout->setSpacing(0);
Tobias Hunger's avatar
Tobias Hunger committed
124

125
    setWidget(m_root);
dt's avatar
dt committed
126
    setFrameStyle(QFrame::NoFrame);
dt's avatar
dt committed
127
    setWidgetResizable(true);
dt's avatar
dt committed
128 129 130 131 132 133 134
}

PanelsWidget::~PanelsWidget()
{
    clear();
}

Tobias Hunger's avatar
Tobias Hunger committed
135 136 137 138
/*
 * Add a widget into the grid layout of the PanelsWidget.
 *
 *     ...
Tobias Hunger's avatar
Tobias Hunger committed
139 140 141
 * +--------+-------------------------------------------+ ABOVE_CONTENTS_MARGIN
 * |          widget (with contentsmargins adjusted!)   |
 * +--------+-------------------------------------------+ BELOW_CONTENTS_MARGIN
Tobias Hunger's avatar
Tobias Hunger committed
142
 */
dt's avatar
dt committed
143 144
void PanelsWidget::addWidget(QWidget *widget)
{
Tobias Hunger's avatar
Tobias Hunger committed
145 146 147 148 149
    QTC_ASSERT(widget, return);

    const int row(m_layout->rowCount() - 1);
    m_layout->setRowStretch(row, 0);
    addPanelWidget(new Panel(widget), row);
dt's avatar
dt committed
150 151
}

Tobias Hunger's avatar
Tobias Hunger committed
152 153 154 155 156
/*
 * Add a widget with heading information into the grid
 * layout of the PanelsWidget.
 *
 *     ...
Tobias Hunger's avatar
Tobias Hunger committed
157
 * +--------+-------------------------------------------+ ABOVE_HEADING_MARGIN
Tobias Hunger's avatar
Tobias Hunger committed
158 159
 * | icon   | name                                      |
 * +        +-------------------------------------------+
Tobias Hunger's avatar
Tobias Hunger committed
160 161 162 163
 * |        | line                                      |
 * +--------+-------------------------------------------+ ABOVE_CONTENTS_MARGIN
 * |          widget (with contentsmargins adjusted!)   |
 * +--------+-------------------------------------------+ BELOW_CONTENTS_MARGIN
Tobias Hunger's avatar
Tobias Hunger committed
164 165
 */
void PanelsWidget::addWidget(const QString &name, QWidget *widget, const QIcon & icon)
dt's avatar
dt committed
166
{
Tobias Hunger's avatar
Tobias Hunger committed
167
    QTC_ASSERT(widget, return);
Tobias Hunger's avatar
Tobias Hunger committed
168

Tobias Hunger's avatar
Tobias Hunger committed
169
    Panel *p = new Panel(widget);
Tobias Hunger's avatar
Tobias Hunger committed
170 171

    // icon:
Tobias Hunger's avatar
Tobias Hunger committed
172 173 174
    const int headerRow(m_layout->rowCount() - 1);
    m_layout->setRowStretch(headerRow, 0);

Tobias Hunger's avatar
Tobias Hunger committed
175 176 177
    if (!icon.isNull()) {
        p->iconLabel = new QLabel(m_root);
        p->iconLabel->setPixmap(icon.pixmap(ICON_SIZE, ICON_SIZE));
Tobias Hunger's avatar
Tobias Hunger committed
178 179
        p->iconLabel->setContentsMargins(0, ABOVE_HEADING_MARGIN, 0, 0);
        m_layout->addWidget(p->iconLabel, headerRow, 0, 2, 1, Qt::AlignTop | Qt::AlignHCenter);
Tobias Hunger's avatar
Tobias Hunger committed
180 181 182 183
    }

    // name:
    p->nameLabel = new QLabel(m_root);
184
    p->nameLabel->setText(name);
Tobias Hunger's avatar
Tobias Hunger committed
185
    p->nameLabel->setContentsMargins(0, ABOVE_HEADING_MARGIN, 0, 0);
186
    QFont f = p->nameLabel->font();
dt's avatar
dt committed
187
    f.setBold(true);
Tobias Hunger's avatar
Tobias Hunger committed
188
    f.setPointSizeF(f.pointSizeF() * 1.4);
189
    p->nameLabel->setFont(f);
Tobias Hunger's avatar
Tobias Hunger committed
190
    m_layout->addWidget(p->nameLabel, headerRow, 1, 1, 1, Qt::AlignBottom | Qt::AlignLeft);
Tobias Hunger's avatar
Tobias Hunger committed
191 192 193 194 195 196

    // line:
    const int lineRow(headerRow + 1);
    p->lineWidget = new OnePixelBlackLine(m_root);
    m_layout->addWidget(p->lineWidget, lineRow, 1);

Tobias Hunger's avatar
Tobias Hunger committed
197
    // add the widget:
Tobias Hunger's avatar
Tobias Hunger committed
198
    const int widgetRow(lineRow + 1);
Tobias Hunger's avatar
Tobias Hunger committed
199
    addPanelWidget(p, widgetRow);
dt's avatar
dt committed
200 201
}

Tobias Hunger's avatar
Tobias Hunger committed
202 203 204 205 206
QWidget *PanelsWidget::rootWidget() const
{
    return m_root;
}

dt's avatar
dt committed
207 208
void PanelsWidget::clear()
{
209
    foreach (Panel *p, m_panels) {
Tobias Hunger's avatar
Tobias Hunger committed
210 211 212 213
        if (p->iconLabel)
            m_layout->removeWidget(p->iconLabel);
        if (p->lineWidget)
            m_layout->removeWidget(p->lineWidget);
214 215
        if (p->nameLabel)
            m_layout->removeWidget(p->nameLabel);
Tobias Hunger's avatar
Tobias Hunger committed
216 217
        if (p->panelWidget)
            m_layout->removeWidget(p->panelWidget);
218
        delete p;
dt's avatar
dt committed
219 220 221 222
    }
    m_panels.clear();
}

Tobias Hunger's avatar
Tobias Hunger committed
223 224 225 226 227 228 229 230 231 232 233 234 235
void PanelsWidget::addPanelWidget(Panel *panel, int row)
{
    panel->panelWidget->setContentsMargins(m_layout->columnMinimumWidth(0),
                                           ABOVE_CONTENTS_MARGIN, 0,
                                           BELOW_CONTENTS_MARGIN);
    m_layout->addWidget(panel->panelWidget, row, 0, 1, 2);

    const int stretchRow(row + 1);
    m_layout->setRowStretch(stretchRow, 10);

    m_panels.append(panel);
}

dt's avatar
dt committed
236 237 238 239 240 241 242 243
////
// ActiveConfigurationWidget
////

ActiveConfigurationWidget::ActiveConfigurationWidget(QWidget *parent)
    : QWidget(parent)
{
    QGridLayout *grid = new QGridLayout(this);
244
    grid->setMargin(0);
dt's avatar
dt committed
245
    RunConfigurationComboBox *runConfigurationComboBox = new RunConfigurationComboBox(this);
dt's avatar
dt committed
246
    grid->addWidget(new QLabel(tr("Active run configuration")), 0, 0);
dt's avatar
dt committed
247 248 249 250 251 252 253 254
    grid->addWidget(runConfigurationComboBox, 0, 1);

    SessionManager *session = ProjectExplorerPlugin::instance()->session();

    int i = 0;
    foreach(Project *p, session->projects()) {
        ++i;
        BuildConfigurationComboBox *buildConfigurationComboBox = new BuildConfigurationComboBox(p, this);
255
        QLabel *label = new QLabel("Build configuration for <b>" + p->displayName() + "</b>", this);
dt's avatar
dt committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        grid->addWidget(label, i, 0);
        grid->addWidget(buildConfigurationComboBox, i, 1);
        m_buildComboBoxMap.insert(p, qMakePair(buildConfigurationComboBox, label));
    }

    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded(ProjectExplorer::Project*)));

    connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved(ProjectExplorer::Project*)));

};

void ActiveConfigurationWidget::projectAdded(Project *p)
{
    QGridLayout *grid = static_cast<QGridLayout *>(layout());
    BuildConfigurationComboBox *buildConfigurationComboBox = new BuildConfigurationComboBox(p, this);
273
    QLabel *label = new QLabel("Build configuration for <b>" + p->displayName() + "</b>");
dt's avatar
dt committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    grid->addWidget(label);
    grid->addWidget(buildConfigurationComboBox);
    m_buildComboBoxMap.insert(p, qMakePair(buildConfigurationComboBox, label));
}

void ActiveConfigurationWidget::projectRemoved(Project *p)
{
    // Find row

    // TODO also remove the label...
    QPair<BuildConfigurationComboBox *, QLabel *> pair = m_buildComboBoxMap.value(p);;
    delete pair.first;
    delete pair.second;
    m_buildComboBoxMap.remove(p);
}


ActiveConfigurationWidget::~ActiveConfigurationWidget()
{

}

////
// RunConfigurationComboBox
////

RunConfigurationComboBox::RunConfigurationComboBox(QWidget *parent)
    : QComboBox(parent), m_ignoreChange(false)
{
    setSizeAdjustPolicy(QComboBox::AdjustToContents);

    SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();

    // Setup the treewidget
    rebuildTree();

    // Connect
    foreach(Project *p, session->projects()) {
312
        foreach(RunConfiguration *rc, p->runConfigurations()) {
313
            connect(rc, SIGNAL(displayNameChanged()), this, SLOT(rebuildTree()));
dt's avatar
dt committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        }
        connectToProject(p);
    }

    connect(session, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(activeRunConfigurationChanged()));

    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded(ProjectExplorer::Project*)));
    connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved(ProjectExplorer::Project*)));
    connect(this, SIGNAL(activated(int)),
            this, SLOT(activeItemChanged(int)));
}

RunConfigurationComboBox::~RunConfigurationComboBox()
{

}

int RunConfigurationComboBox::convertTreeIndexToInt(int project, int runconfigurationIndex)
{
    ++runconfigurationIndex;
    ++project;
    for(int i=0; i<count(); ++i) {
        if (itemData(i, Qt::UserRole).toInt() == 0) {
            --project;
        } else if (itemData(i, Qt::UserRole).toInt() == 1 && project == 0) {
            --runconfigurationIndex;
        }
        if (runconfigurationIndex == 0) {
            return i;
        }
    }
    return -1;
}

QPair<int, int> RunConfigurationComboBox::convertIntToTreeIndex(int index)
{
    int projectIndex = -1;
    int runConfigIndex = -1;
    for(int i = 0; i <= index; ++i) {
        if (itemData(i, Qt::UserRole).toInt() == 0) {
            ++projectIndex;
            runConfigIndex = -1;
        } else if (itemData(i, Qt::UserRole).toInt() == 1) {
            ++runConfigIndex;
        }
    }
    return qMakePair(projectIndex, runConfigIndex);
}

void RunConfigurationComboBox::activeItemChanged(int index)
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
    SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
    QPair<int, int> pair = convertIntToTreeIndex(index);
    if (pair.first == -1) {
        setCurrentIndex(-1);
    } else {
        if (pair.second == -1)
            pair.second = 0;
        QList<Project *> projects = session->projects();
        if (pair.first < projects.count()) {
            Project *p = projects.at(pair.first);
381
            QList<RunConfiguration *> runconfigurations = p->runConfigurations();
dt's avatar
dt committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
            if (pair.second < runconfigurations.count()) {
                session->setStartupProject(p);
                p->setActiveRunConfiguration(runconfigurations.at(pair.second));
                if (currentIndex() != convertTreeIndexToInt(pair.first, pair.second))
                    setCurrentIndex(convertTreeIndexToInt(pair.first, pair.second));
            }
        }
    }
    m_ignoreChange = false;
}

void RunConfigurationComboBox::activeRunConfigurationChanged()
{
    if (m_ignoreChange)
        return;
    m_ignoreChange = true;
    SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
    Project *startupProject = session->startupProject();
    if (startupProject) {
        int projectIndex = session->projects().indexOf(startupProject);
402
        int runConfigurationIndex = startupProject->runConfigurations().indexOf(startupProject->activeRunConfiguration());
dt's avatar
dt committed
403 404 405 406 407 408 409 410 411
        setCurrentIndex(convertTreeIndexToInt(projectIndex, runConfigurationIndex));
    } else {
        setCurrentIndex(-1);
    }
    m_ignoreChange = false;
}

void RunConfigurationComboBox::addedRunConfiguration(ProjectExplorer::Project *p, const QString &name)
{
412 413
    RunConfiguration *runConfiguration = 0;
    foreach(RunConfiguration *rc, p->runConfigurations()) {
414
        if (rc->displayName() == name) {
dt's avatar
dt committed
415 416 417 418 419
            runConfiguration = rc;
            break;
        }
    }
    if (runConfiguration) {
420
        connect(runConfiguration, SIGNAL(displayNameChanged()),
dt's avatar
dt committed
421 422 423 424 425 426 427
                this, SLOT(rebuildTree()));
    }
    rebuildTree();
}

void RunConfigurationComboBox::removedRunConfiguration(ProjectExplorer::Project *p, const QString &name)
{
428 429
    Q_UNUSED(p)
    Q_UNUSED(name)
dt's avatar
dt committed
430 431 432 433 434 435
    rebuildTree();
}

void RunConfigurationComboBox::projectAdded(ProjectExplorer::Project *p)
{
    rebuildTree();
436
    foreach(RunConfiguration *rc, p->runConfigurations())
437
        connect(rc, SIGNAL(displayNameChanged()), this, SLOT(rebuildTree()));
dt's avatar
dt committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    connectToProject(p);
}

void RunConfigurationComboBox::projectRemoved(ProjectExplorer::Project *p)
{
    rebuildTree();
    disconnectFromProject(p);
}

void RunConfigurationComboBox::connectToProject(ProjectExplorer::Project *p)
{
    connect(p, SIGNAL(activeRunConfigurationChanged()),
            this, SLOT(activeRunConfigurationChanged()));
    connect(p, SIGNAL(addedRunConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(addedRunConfiguration(ProjectExplorer::Project *, QString)));
    connect(p, SIGNAL(removedRunConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(removedRunConfiguration(ProjectExplorer::Project *, QString)));
}

void RunConfigurationComboBox::disconnectFromProject(ProjectExplorer::Project *p)
{
    disconnect(p, SIGNAL(activeRunConfigurationChanged()),
            this, SLOT(activeRunConfigurationChanged()));
    disconnect(p, SIGNAL(addedRunConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(addedRunConfiguration(ProjectExplorer::Project *, QString)));
    disconnect(p, SIGNAL(removedRunConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(removedRunConfiguration(ProjectExplorer::Project *, QString)));
}

void RunConfigurationComboBox::rebuildTree()
{
    m_ignoreChange = true;
    clear();
471

dt's avatar
dt committed
472 473 474
    SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
    Project *startupProject = session->startupProject();
    foreach(Project *p, session->projects()) {
475
        addItem(p->displayName(), QVariant(0));
476
        foreach(RunConfiguration *rc, p->runConfigurations()) {
477
            addItem("  " + rc->displayName(), QVariant(1));
dt's avatar
dt committed
478 479 480 481 482 483 484 485 486 487 488 489 490
            if ((startupProject == p) && (p->activeRunConfiguration() == rc)){
                setCurrentIndex(count() - 1);
            }
        }
    }
    // Select the right index
    m_ignoreChange = false;
}

////
// BuildConfigurationComboBox
////

491

dt's avatar
dt committed
492
BuildConfigurationComboBox::BuildConfigurationComboBox(Project *p, QWidget *parent)
493 494 495 496 497 498 499 500 501 502 503 504
    : QStackedWidget(parent), ignoreIndexChange(false), m_project(p)
{
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_comboBox = new QComboBox(this);
    m_comboBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    addWidget(m_comboBox);

    m_label = new QLabel(this);
    m_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    addWidget(m_label);

    //m_comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
dt's avatar
dt committed
505 506
    foreach(BuildConfiguration *buildConfiguration, p->buildConfigurations())
        m_comboBox->addItem(buildConfiguration->displayName(), QVariant::fromValue(buildConfiguration));
507
    if (p->buildConfigurations().count() == 1) {
508 509 510
        m_label->setText(m_comboBox->itemText(0));
        setCurrentWidget(m_label);
    }
dt's avatar
dt committed
511 512 513

    int index = p->buildConfigurations().indexOf(p->activeBuildConfiguration());
    if (index != -1)
514
        m_comboBox->setCurrentIndex(index);
dt's avatar
dt committed
515

516
    // TODO
Tobias Hunger's avatar
Tobias Hunger committed
517 518
//    connect(p, SIGNAL(buildConfigurationDisplayNameChanged(QString)),
    //            this, SLOT(nameChanged(ProjectExplorer::BuildConfiguration *)));
dt's avatar
dt committed
519 520
    connect(p, SIGNAL(activeBuildConfigurationChanged()),
            this, SLOT(activeConfigurationChanged()));
521 522 523 524
    connect(p, SIGNAL(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
            this, SLOT(addedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
    connect(p, SIGNAL(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)),
            this, SLOT(removedBuildConfiguration(ProjectExplorer::BuildConfiguration*)));
525
    connect(m_comboBox, SIGNAL(activated(int)),
dt's avatar
dt committed
526 527 528 529 530 531 532 533
            this, SLOT(changedIndex(int)));
}

BuildConfigurationComboBox::~BuildConfigurationComboBox()
{

}

dt's avatar
dt committed
534
void BuildConfigurationComboBox::nameChanged(BuildConfiguration *bc)
dt's avatar
dt committed
535
{
Tobias Hunger's avatar
Tobias Hunger committed
536
    const int index(buildConfigurationToIndex(bc));
dt's avatar
dt committed
537 538
    if (index == -1)
        return;
dt's avatar
dt committed
539
    const QString &displayName = bc->displayName();
540 541 542
    m_comboBox->setItemText(index, displayName);
    if (m_comboBox->count() == 1)
        m_label->setText(displayName);
dt's avatar
dt committed
543 544
}

Tobias Hunger's avatar
Tobias Hunger committed
545
int BuildConfigurationComboBox::buildConfigurationToIndex(BuildConfiguration *bc)
dt's avatar
dt committed
546
{
547
    for (int i=0; i < m_comboBox->count(); ++i)
dt's avatar
dt committed
548
        if (m_comboBox->itemData(i).value<BuildConfiguration *>() == bc)
dt's avatar
dt committed
549 550 551 552 553 554
            return i;
    return -1;
}

void BuildConfigurationComboBox::activeConfigurationChanged()
{
Tobias Hunger's avatar
Tobias Hunger committed
555
    const int index(buildConfigurationToIndex(m_project->activeBuildConfiguration()));
dt's avatar
dt committed
556 557 558
    if (index == -1)
        return;
    ignoreIndexChange = true;
559
    m_comboBox->setCurrentIndex(index);
dt's avatar
dt committed
560 561 562
    ignoreIndexChange = false;
}

563
void BuildConfigurationComboBox::addedBuildConfiguration(BuildConfiguration *bc)
dt's avatar
dt committed
564 565
{
    ignoreIndexChange = true;
dt's avatar
dt committed
566
    m_comboBox->addItem(bc->displayName(), QVariant::fromValue(bc));
567 568 569

    if (m_comboBox->count() == 2)
        setCurrentWidget(m_comboBox);
dt's avatar
dt committed
570 571 572
    ignoreIndexChange = false;
}

573
void BuildConfigurationComboBox::removedBuildConfiguration(BuildConfiguration * bc)
dt's avatar
dt committed
574 575
{
    ignoreIndexChange = true;
Tobias Hunger's avatar
Tobias Hunger committed
576 577 578
    const int index(buildConfigurationToIndex(bc));
    if (index == -1)
        return;
579 580 581 582 583
    m_comboBox->removeItem(index);
    if (m_comboBox->count() == 1) {
        m_label->setText(m_comboBox->itemText(0));
        setCurrentWidget(m_label);
    }
dt's avatar
dt committed
584 585 586 587 588 589 590
    ignoreIndexChange = false;
}

void BuildConfigurationComboBox::changedIndex(int newIndex)
{
    if (newIndex == -1)
        return;
dt's avatar
dt committed
591
    m_project->setActiveBuildConfiguration(m_comboBox->itemData(newIndex).value<BuildConfiguration *>());
dt's avatar
dt committed
592
}
593

dt's avatar
dt committed
594
///
595
// ProjectLabel
dt's avatar
dt committed
596 597
///

598 599
ProjectLabel::ProjectLabel(QWidget *parent)
    : QLabel(parent)
dt's avatar
dt committed
600
{
601 602 603 604 605 606 607 608 609 610 611

}

ProjectLabel::~ProjectLabel()
{

}

void ProjectLabel::setProject(ProjectExplorer::Project *p)
{
    if (p)
612
        setText(tr("Edit Project Settings for Project <b>%1</b>").arg(p->displayName()));
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    else
        setText(tr("No Project loaded"));
}

///
// ProjectPushButton
///

ProjectPushButton::ProjectPushButton(QWidget *parent)
    : QPushButton(parent)
{
    setText(tr("Select Project"));
    m_menu = new QMenu(this);
    setMenu(m_menu);

    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

dt's avatar
dt committed
630 631 632
    SessionManager *session = ProjectExplorerPlugin::instance()->session();

    foreach(Project *p, session->projects()) {
633
        QAction *act = m_menu->addAction(p->displayName());
634 635 636
        act->setData(QVariant::fromValue((void *) p));
        connect(act, SIGNAL(triggered()),
                this, SLOT(actionTriggered()));
dt's avatar
dt committed
637
    }
dt's avatar
dt committed
638

639 640
    setEnabled(session->projects().count() > 1);

dt's avatar
dt committed
641 642 643 644
    connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved(ProjectExplorer::Project*)));
    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded(ProjectExplorer::Project*)));
dt's avatar
dt committed
645 646
}

647
ProjectPushButton::~ProjectPushButton()
dt's avatar
dt committed
648 649 650 651
{

}

652
void ProjectPushButton::projectAdded(ProjectExplorer::Project *p)
dt's avatar
dt committed
653
{
654
    QAction *act = m_menu->addAction(p->displayName());
655 656 657 658 659 660 661 662 663
    act->setData(QVariant::fromValue((void *) p));
    connect(act, SIGNAL(triggered()),
                this, SLOT(actionTriggered()));

    // Activate it
    if (m_menu->actions().count() == 1)
        emit projectChanged(p);
    else if (m_menu->actions().count() > 1)
        setEnabled(true);
dt's avatar
dt committed
664 665
}

666
void ProjectPushButton::projectRemoved(ProjectExplorer::Project *p)
dt's avatar
dt committed
667
{
dt's avatar
dt committed
668
    QList<Project *> projects = ProjectExplorerPlugin::instance()->session()->projects();
669 670 671 672 673 674

    bool needToChange = false;
    foreach(QAction *act, m_menu->actions()) {
        if (act->data().value<void *>() == (void *) p) {
            delete act;
            needToChange = true;
dt's avatar
dt committed
675
            break;
676
        }
dt's avatar
dt committed
677 678
    }

dt's avatar
dt committed
679
    // Comboboxes don't emit a signal if the index did't actually change
680 681 682 683 684
    if (m_menu->actions().isEmpty()) {
        emit projectChanged(0);
        setEnabled(false);
    } else if (needToChange) {
        emit projectChanged((ProjectExplorer::Project *) m_menu->actions().first()->data().value<void *>());
dt's avatar
dt committed
685
    }
dt's avatar
dt committed
686 687
}

688
void ProjectPushButton::actionTriggered()
dt's avatar
dt committed
689
{
690 691
    QAction *action = qobject_cast<QAction *>(sender());
    emit projectChanged((ProjectExplorer::Project *) action->data().value<void *>());
dt's avatar
dt committed
692 693 694 695 696 697
}

///
// ProjectWindow
///

dt's avatar
dt committed
698
ProjectWindow::ProjectWindow(QWidget *parent)
699
    : QWidget(parent)
con's avatar
con committed
700
{
701
    ProjectExplorer::SessionManager *session = ProjectExplorerPlugin::instance()->session();
con's avatar
con committed
702

Tobias Hunger's avatar
Tobias Hunger committed
703 704 705 706
    // Setup overall layout:
    QVBoxLayout *viewLayout = new QVBoxLayout(this);
    viewLayout->setMargin(0);
    viewLayout->setSpacing(0);
dt's avatar
dt committed
707

Tobias Hunger's avatar
Tobias Hunger committed
708 709
    // Add toolbar used everywhere
    viewLayout->addWidget(new Utils::StyledBar(this));
710

Tobias Hunger's avatar
Tobias Hunger committed
711 712 713 714
    // Setup our container for the contents:
    m_panelsWidget = new PanelsWidget(this);
    viewLayout->addWidget(m_panelsWidget);

Tobias Hunger's avatar
Tobias Hunger committed
715
    // Run and build configuration selection panel:
Tobias Hunger's avatar
Tobias Hunger committed
716 717
    m_activeConfigurationWidget = new ActiveConfigurationWidget(m_panelsWidget->rootWidget());

Tobias Hunger's avatar
Tobias Hunger committed
718 719
    // Spacer and line panel:
    m_spacerBetween = new OnePixelBlackLine(m_panelsWidget->rootWidget());
Tobias Hunger's avatar
Tobias Hunger committed
720

Tobias Hunger's avatar
Tobias Hunger committed
721
    // Project chooser panel:
Tobias Hunger's avatar
Tobias Hunger committed
722
    m_projectChooser = new QWidget(m_panelsWidget->rootWidget());
dt's avatar
dt committed
723 724
    QHBoxLayout *hbox = new QHBoxLayout(m_projectChooser);
    hbox->setMargin(0);
725 726 727 728 729 730 731 732 733 734 735 736
    ProjectLabel *label = new ProjectLabel(m_projectChooser);
    {
        QFont f = label->font();
        f.setPointSizeF(f.pointSizeF() * 1.4);
        f.setBold(true);
        label->setFont(f);
    }
    hbox->addWidget(label);
    ProjectPushButton *changeProject = new ProjectPushButton(m_projectChooser);
    connect(changeProject, SIGNAL(projectChanged(ProjectExplorer::Project*)),
            label, SLOT(setProject(ProjectExplorer::Project*)));
    hbox->addWidget(changeProject);
dt's avatar
dt committed
737

Tobias Hunger's avatar
Tobias Hunger committed
738
    // no projects label:
739 740 741 742 743 744 745 746 747 748
    m_noprojectLabel = new QLabel(this);
    m_noprojectLabel->setText(tr("No project loaded."));
    {
        QFont f = m_noprojectLabel->font();
        f.setPointSizeF(f.pointSizeF() * 1.4);
        f.setBold(true);
        m_noprojectLabel->setFont(f);
    }
    m_noprojectLabel->setMargin(10);
    m_noprojectLabel->setAlignment(Qt::AlignTop);
Tobias Hunger's avatar
Tobias Hunger committed
749
    viewLayout->addWidget(m_noprojectLabel);
750

Tobias Hunger's avatar
Tobias Hunger committed
751
    // show either panels or no projects label:
752 753 754 755
    bool noProjects = session->projects().isEmpty();
    m_panelsWidget->setVisible(!noProjects);
    m_noprojectLabel->setVisible(noProjects);

Tobias Hunger's avatar
Tobias Hunger committed
756
    // connects:
757
    connect(changeProject, SIGNAL(projectChanged(ProjectExplorer::Project*)),
dt's avatar
dt committed
758 759
            this, SLOT(showProperties(ProjectExplorer::Project*)));

760 761 762 763 764 765 766
    connect(session, SIGNAL(sessionLoaded()), this, SLOT(restoreStatus()));
    connect(session, SIGNAL(aboutToSaveSession()), this, SLOT(saveStatus()));

    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
            this, SLOT(projectAdded()));
    connect(session, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
            this, SLOT(projectRemoved()));
767 768 769

    // Update properties to empty project for now:
    showProperties(0);
con's avatar
con committed
770 771 772 773
}

ProjectWindow::~ProjectWindow()
{
dt's avatar
dt committed
774 775
    qDeleteAll(m_panels);
    m_panels.clear();
con's avatar
con committed
776 777
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791
void ProjectWindow::projectAdded()
{
    m_panelsWidget->setVisible(true);
    m_noprojectLabel->setVisible(false);
}

void ProjectWindow::projectRemoved()
{
    if (ProjectExplorerPlugin::instance()->session()->projects().isEmpty()) {
        m_panelsWidget->setVisible(false);
        m_noprojectLabel->setVisible(true);
    }
}

con's avatar
con committed
792 793
void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
794
    // TODO
con's avatar
con committed
795 796 797 798
}

void ProjectWindow::saveStatus()
{
dt's avatar
dt committed
799
    // TODO
con's avatar
con committed
800 801
}

dt's avatar
dt committed
802
void ProjectWindow::showProperties(Project *project)
con's avatar
con committed
803
{
804
    // Remove all existing panels:
dt's avatar
dt committed
805
    m_panelsWidget->clear();
con's avatar
con committed
806

807
    // delete custom panels:
dt's avatar
dt committed
808 809 810
    qDeleteAll(m_panels);
    m_panels.clear();

811
    // Set up our default panels:
Tobias Hunger's avatar
Tobias Hunger committed
812
    m_panelsWidget->addWidget(tr("Active Build and Run Configurations"), m_activeConfigurationWidget, QIcon());
dt's avatar
dt committed
813
    m_panelsWidget->addWidget(m_spacerBetween);
814
    m_panelsWidget->addWidget(m_projectChooser);
dt's avatar
dt committed
815

816
    // Set up custom panels again:
con's avatar
con committed
817 818 819 820 821
    if (project) {
        QList<IPanelFactory *> pages =
                ExtensionSystem::PluginManager::instance()->getObjects<IPanelFactory>();
        foreach (IPanelFactory *panelFactory, pages) {
            if (panelFactory->supports(project)) {
822
                IPropertiesPanel *panel = panelFactory->createPanel(project);
823
                m_panelsWidget->addWidget(panel->displayName(), panel->widget(), panel->icon());
dt's avatar
dt committed
824
                m_panels.push_back(panel);
con's avatar
con committed
825 826 827 828
            }
        }
    }
}