projectwindow.cpp 23.7 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>
con's avatar
con committed
47
#include <utils/styledbar.h>
48
#include <utils/stylehelper.h>
con's avatar
con committed
49 50

#include <QtCore/QDebug>
51
#include <QtGui/QApplication>
con's avatar
con committed
52 53
#include <QtGui/QBoxLayout>
#include <QtGui/QComboBox>
54
#include <QtGui/QScrollArea>
con's avatar
con committed
55 56 57
#include <QtGui/QTabWidget>
#include <QtGui/QTreeWidget>
#include <QtGui/QHeaderView>
dt's avatar
dt committed
58
#include <QtGui/QLabel>
dt's avatar
dt committed
59 60
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
61 62
#include <QtGui/QMenu>

con's avatar
con committed
63 64 65 66 67 68 69 70

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
bool debug = false;
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
///
// OnePixelBlackLine
///

class OnePixelBlackLine : public QWidget
{
public:
    OnePixelBlackLine(QWidget *parent)
        : QWidget(parent)
    {
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        setMinimumHeight(1);
        setMaximumHeight(1);
    }
    void paintEvent(QPaintEvent *e)
    {
        QPainter p(this);
        p.fillRect(e->rect(), QBrush(Utils::StyleHelper::borderColor()));
    }
};

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
///
// PanelsWidget::Panel
///

PanelsWidget::Panel::Panel(QWidget * w) :
        spacer(0), nameLabel(0), panelWidget(w), marginLayout(0)
{ }

PanelsWidget::Panel::~Panel()
{
    delete spacer;
    delete nameLabel;
    delete marginLayout;
}

107 108 109 110
///
// PanelsWidget
///

dt's avatar
dt committed
111 112 113
PanelsWidget::PanelsWidget(QWidget *parent)
    : QScrollArea(parent)
{
con's avatar
con committed
114 115 116 117 118 119 120 121 122
    QWidget *topwidget = new QWidget;
    QHBoxLayout *topwidgetLayout = new QHBoxLayout;
    topwidgetLayout->setMargin(0);
    topwidgetLayout->setSpacing(0);
    topwidget->setLayout(topwidgetLayout);

    QWidget *verticalWidget = new QWidget;
    verticalWidget->setMaximumWidth(800);
    m_layout = new QVBoxLayout;
dt's avatar
dt committed
123
    m_layout->addStretch(10);
con's avatar
con committed
124 125 126
    verticalWidget->setLayout(m_layout);
    topwidgetLayout->addWidget(verticalWidget);
    topwidgetLayout->addStretch(10);
dt's avatar
dt committed
127 128

    setFrameStyle(QFrame::NoFrame);
dt's avatar
dt committed
129 130
    setWidgetResizable(true);

con's avatar
con committed
131
    setWidget(topwidget);
dt's avatar
dt committed
132 133 134 135 136 137 138
}

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

dt's avatar
dt committed
139 140
void PanelsWidget::addWidget(QWidget *widget)
{
141
    Panel *p = new Panel(widget);
dt's avatar
dt committed
142
    m_layout->insertWidget(m_layout->count() - 1, widget);
dt's avatar
dt committed
143 144 145
    m_panels.append(p);
}

dt's avatar
dt committed
146 147
void PanelsWidget::addWidget(const QString &name, QWidget *widget)
{
148 149 150 151 152
    Panel *p = new Panel(widget);
    p->spacer = new QSpacerItem(1, 10, QSizePolicy::Fixed, QSizePolicy::Fixed);
    p->nameLabel = new QLabel(this);
    p->nameLabel->setText(name);
    QFont f = p->nameLabel->font();
dt's avatar
dt committed
153
    f.setBold(true);
154
    f.setPointSizeF(f.pointSizeF() * 1.2);
155
    p->nameLabel->setFont(f);
dt's avatar
dt committed
156

157 158
    m_layout->insertSpacerItem(m_layout->count() - 1, p->spacer);
    m_layout->insertWidget(m_layout->count() - 1, p->nameLabel);
dt's avatar
dt committed
159 160
    QHBoxLayout *hboxLayout = new QHBoxLayout();
    hboxLayout->setContentsMargins(20, 0, 0, 0);
161 162
    hboxLayout->addWidget(p->panelWidget);
    p->marginLayout = hboxLayout;
dt's avatar
dt committed
163 164
    m_layout->insertLayout(m_layout->count() -1, hboxLayout);
    m_panels.append(p);
dt's avatar
dt committed
165 166 167 168
}

void PanelsWidget::clear()
{
169 170 171 172 173 174
    foreach(Panel * p, m_panels) {
        if (p->spacer)
            m_layout->removeItem(p->spacer);
        if (p->nameLabel)
            m_layout->removeWidget(p->nameLabel);
        delete p;
dt's avatar
dt committed
175 176 177 178
    }
    m_panels.clear();
}

dt's avatar
dt committed
179 180 181 182 183 184 185 186
////
// ActiveConfigurationWidget
////

ActiveConfigurationWidget::ActiveConfigurationWidget(QWidget *parent)
    : QWidget(parent)
{
    QGridLayout *grid = new QGridLayout(this);
187
    grid->setMargin(0);
dt's avatar
dt committed
188
    RunConfigurationComboBox *runConfigurationComboBox = new RunConfigurationComboBox(this);
dt's avatar
dt committed
189
    grid->addWidget(new QLabel(tr("Active run configuration")), 0, 0);
dt's avatar
dt committed
190 191 192 193 194 195 196 197
    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);
dt's avatar
dt committed
198
        QLabel *label = new QLabel("Build configuration for <b>" + p->name() + "</b>", this);
dt's avatar
dt committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        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);
dt's avatar
dt committed
216
    QLabel *label = new QLabel("Build configuration for <b>" + p->name() + "</b>");
dt's avatar
dt committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    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()) {
255 256
        foreach(RunConfiguration *rc, p->runConfigurations()) {
            connect(rc, SIGNAL(nameChanged()), this, SLOT(rebuildTree()));
dt's avatar
dt committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 312 313 314 315 316 317 318 319 320 321 322 323 324
        }
        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);
325
            QList<RunConfiguration *> runconfigurations = p->runConfigurations();
dt's avatar
dt committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            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);
346
        int runConfigurationIndex = startupProject->runConfigurations().indexOf(startupProject->activeRunConfiguration());
dt's avatar
dt committed
347 348 349 350 351 352 353 354 355
        setCurrentIndex(convertTreeIndexToInt(projectIndex, runConfigurationIndex));
    } else {
        setCurrentIndex(-1);
    }
    m_ignoreChange = false;
}

void RunConfigurationComboBox::addedRunConfiguration(ProjectExplorer::Project *p, const QString &name)
{
356 357
    RunConfiguration *runConfiguration = 0;
    foreach(RunConfiguration *rc, p->runConfigurations()) {
dt's avatar
dt committed
358 359 360 361 362 363
        if (rc->name() == name) {
            runConfiguration = rc;
            break;
        }
    }
    if (runConfiguration) {
364
        connect(runConfiguration, SIGNAL(nameChanged()),
dt's avatar
dt committed
365 366 367 368 369 370 371
                this, SLOT(rebuildTree()));
    }
    rebuildTree();
}

void RunConfigurationComboBox::removedRunConfiguration(ProjectExplorer::Project *p, const QString &name)
{
372 373
    Q_UNUSED(p)
    Q_UNUSED(name)
dt's avatar
dt committed
374 375 376 377 378 379
    rebuildTree();
}

void RunConfigurationComboBox::projectAdded(ProjectExplorer::Project *p)
{
    rebuildTree();
380 381
    foreach(RunConfiguration *rc, p->runConfigurations())
        connect(rc, SIGNAL(nameChanged()), this, SLOT(rebuildTree()));
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 402 403 404 405 406 407 408 409 410 411 412 413 414
    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();
415

dt's avatar
dt committed
416 417 418 419
    SessionManager *session = ProjectExplorer::ProjectExplorerPlugin::instance()->session();
    Project *startupProject = session->startupProject();
    foreach(Project *p, session->projects()) {
        addItem(p->name(), QVariant(0));
420
        foreach(RunConfiguration *rc, p->runConfigurations()) {
dt's avatar
dt committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434
            addItem("  " + rc->name(), QVariant(1));
            if ((startupProject == p) && (p->activeRunConfiguration() == rc)){
                setCurrentIndex(count() - 1);
            }
        }
    }
    // Select the right index
    m_ignoreChange = false;
}

////
// BuildConfigurationComboBox
////

435

dt's avatar
dt committed
436
BuildConfigurationComboBox::BuildConfigurationComboBox(Project *p, QWidget *parent)
437 438 439 440 441 442 443 444 445 446 447 448
    : 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);
449 450 451
    foreach(const BuildConfiguration *buildConfiguration, p->buildConfigurations())
        m_comboBox->addItem(buildConfiguration->displayName(), buildConfiguration->name());
    if (p->buildConfigurations().count() == 1) {
452 453 454
        m_label->setText(m_comboBox->itemText(0));
        setCurrentWidget(m_label);
    }
dt's avatar
dt committed
455 456 457

    int index = p->buildConfigurations().indexOf(p->activeBuildConfiguration());
    if (index != -1)
458
        m_comboBox->setCurrentIndex(index);
dt's avatar
dt committed
459 460 461 462 463 464 465 466 467

    connect(p, SIGNAL(buildConfigurationDisplayNameChanged(QString)),
            this, SLOT(nameChanged(QString)));
    connect(p, SIGNAL(activeBuildConfigurationChanged()),
            this, SLOT(activeConfigurationChanged()));
    connect(p, SIGNAL(addedBuildConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(addedBuildConfiguration(ProjectExplorer::Project *, QString)));
    connect(p, SIGNAL(removedBuildConfiguration(ProjectExplorer::Project *, QString)),
            this, SLOT(removedBuildConfiguration(ProjectExplorer::Project *, QString)));
468
    connect(m_comboBox, SIGNAL(activated(int)),
dt's avatar
dt committed
469 470 471 472 473 474 475 476 477 478 479 480 481
            this, SLOT(changedIndex(int)));
}

BuildConfigurationComboBox::~BuildConfigurationComboBox()
{

}

void BuildConfigurationComboBox::nameChanged(const QString &buildConfiguration)
{
    int index = nameToIndex(buildConfiguration);
    if (index == -1)
        return;
482
    const QString &displayName = m_project->buildConfiguration(buildConfiguration)->displayName();
483 484 485
    m_comboBox->setItemText(index, displayName);
    if (m_comboBox->count() == 1)
        m_label->setText(displayName);
dt's avatar
dt committed
486 487 488 489
}

int BuildConfigurationComboBox::nameToIndex(const QString &buildConfiguration)
{
490 491
    for (int i=0; i < m_comboBox->count(); ++i)
        if (m_comboBox->itemData(i) == buildConfiguration)
dt's avatar
dt committed
492 493 494 495 496 497
            return i;
    return -1;
}

void BuildConfigurationComboBox::activeConfigurationChanged()
{
498
    int index = nameToIndex(m_project->activeBuildConfiguration()->name());
dt's avatar
dt committed
499 500 501
    if (index == -1)
        return;
    ignoreIndexChange = true;
502
    m_comboBox->setCurrentIndex(index);
dt's avatar
dt committed
503 504 505 506 507 508
    ignoreIndexChange = false;
}

void BuildConfigurationComboBox::addedBuildConfiguration(ProjectExplorer::Project *,const QString &buildConfiguration)
{
    ignoreIndexChange = true;
509
    m_comboBox->addItem(m_project->buildConfiguration(buildConfiguration)->displayName(), buildConfiguration);
510 511 512

    if (m_comboBox->count() == 2)
        setCurrentWidget(m_comboBox);
dt's avatar
dt committed
513 514 515 516 517 518 519
    ignoreIndexChange = false;
}

void BuildConfigurationComboBox::removedBuildConfiguration(ProjectExplorer::Project *, const QString &buildConfiguration)
{
    ignoreIndexChange = true;
    int index = nameToIndex(buildConfiguration);
520 521 522 523 524
    m_comboBox->removeItem(index);
    if (m_comboBox->count() == 1) {
        m_label->setText(m_comboBox->itemText(0));
        setCurrentWidget(m_label);
    }
dt's avatar
dt committed
525 526 527 528 529 530 531
    ignoreIndexChange = false;
}

void BuildConfigurationComboBox::changedIndex(int newIndex)
{
    if (newIndex == -1)
        return;
532 533
    m_project->setActiveBuildConfiguration(
            m_project->buildConfiguration(m_comboBox->itemData(newIndex).toString()));
dt's avatar
dt committed
534
}
535

dt's avatar
dt committed
536
///
537
// ProjectLabel
dt's avatar
dt committed
538 539
///

540 541
ProjectLabel::ProjectLabel(QWidget *parent)
    : QLabel(parent)
dt's avatar
dt committed
542
{
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

}

ProjectLabel::~ProjectLabel()
{

}

void ProjectLabel::setProject(ProjectExplorer::Project *p)
{
    if (p)
        setText(tr("Edit Project Settings for Project <b>%1</b>").arg(p->name()));
    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
573 574 575
    SessionManager *session = ProjectExplorerPlugin::instance()->session();

    foreach(Project *p, session->projects()) {
576 577 578 579
        QAction *act = m_menu->addAction(p->name());
        act->setData(QVariant::fromValue((void *) p));
        connect(act, SIGNAL(triggered()),
                this, SLOT(actionTriggered()));
dt's avatar
dt committed
580
    }
dt's avatar
dt committed
581

582 583
    setEnabled(session->projects().count() > 1);

dt's avatar
dt committed
584 585 586 587
    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
588 589
}

590
ProjectPushButton::~ProjectPushButton()
dt's avatar
dt committed
591 592 593 594
{

}

595
void ProjectPushButton::projectAdded(ProjectExplorer::Project *p)
dt's avatar
dt committed
596
{
597 598 599 600 601 602 603 604 605 606
    QAction *act = m_menu->addAction(p->name());
    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
607 608
}

609
void ProjectPushButton::projectRemoved(ProjectExplorer::Project *p)
dt's avatar
dt committed
610
{
dt's avatar
dt committed
611
    QList<Project *> projects = ProjectExplorerPlugin::instance()->session()->projects();
612 613 614 615 616 617

    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
618
            break;
619
        }
dt's avatar
dt committed
620 621
    }

dt's avatar
dt committed
622
    // Comboboxes don't emit a signal if the index did't actually change
623 624 625 626 627
    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
628
    }
dt's avatar
dt committed
629 630
}

631
void ProjectPushButton::actionTriggered()
dt's avatar
dt committed
632
{
633 634
    QAction *action = qobject_cast<QAction *>(sender());
    emit projectChanged((ProjectExplorer::Project *) action->data().value<void *>());
dt's avatar
dt committed
635 636 637 638 639 640
}

///
// ProjectWindow
///

dt's avatar
dt committed
641
ProjectWindow::ProjectWindow(QWidget *parent)
642
    : QWidget(parent)
con's avatar
con committed
643
{
644
    ProjectExplorer::SessionManager *session = ProjectExplorerPlugin::instance()->session();
con's avatar
con committed
645

dt's avatar
dt committed
646
    m_panelsWidget = new PanelsWidget(this);
con's avatar
con committed
647

648
    // active configuration widget:
dt's avatar
dt committed
649 650
    m_activeConfigurationWidget = new ActiveConfigurationWidget(m_panelsWidget);

651 652
    // spacer with line:
    m_spacerBetween = new QWidget(m_panelsWidget);
653 654 655 656 657 658 659
    QVBoxLayout *vbox = new QVBoxLayout(m_spacerBetween);
    vbox->setMargin(0);
    m_spacerBetween->setLayout(vbox);
    vbox->addSpacerItem(new QSpacerItem(10, 15, QSizePolicy::Fixed, QSizePolicy::Fixed));
    vbox->addWidget(new OnePixelBlackLine(m_spacerBetween));
    vbox->addSpacerItem(new QSpacerItem(10, 15, QSizePolicy::Fixed, QSizePolicy::Fixed));

660
    // project chooser:
dt's avatar
dt committed
661 662 663
    m_projectChooser = new QWidget(m_panelsWidget);
    QHBoxLayout *hbox = new QHBoxLayout(m_projectChooser);
    hbox->setMargin(0);
664 665 666 667 668 669 670 671 672 673 674 675
    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
676

677
    // Overall layout:
dt's avatar
dt committed
678 679
    QVBoxLayout *topLevelLayout = new QVBoxLayout(this);
    topLevelLayout->setMargin(0);
dt's avatar
dt committed
680
    topLevelLayout->setSpacing(0);
681
    topLevelLayout->addWidget(new Utils::StyledBar(this));
dt's avatar
dt committed
682

dt's avatar
dt committed
683
    topLevelLayout->addWidget(m_panelsWidget);
con's avatar
con committed
684

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    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);
    topLevelLayout->addWidget(m_noprojectLabel);

    bool noProjects = session->projects().isEmpty();
    m_panelsWidget->setVisible(!noProjects);
    m_noprojectLabel->setVisible(noProjects);

701
    connect(changeProject, SIGNAL(projectChanged(ProjectExplorer::Project*)),
dt's avatar
dt committed
702 703
            this, SLOT(showProperties(ProjectExplorer::Project*)));

704 705 706 707 708 709 710
    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()));
711 712 713

    // Update properties to empty project for now:
    showProperties(0);
con's avatar
con committed
714 715 716 717
}

ProjectWindow::~ProjectWindow()
{
dt's avatar
dt committed
718 719
    qDeleteAll(m_panels);
    m_panels.clear();
con's avatar
con committed
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735
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
736 737
void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
738
    // TODO
con's avatar
con committed
739 740 741 742
}

void ProjectWindow::saveStatus()
{
dt's avatar
dt committed
743
    // TODO
con's avatar
con committed
744 745
}

dt's avatar
dt committed
746
void ProjectWindow::showProperties(Project *project)
con's avatar
con committed
747 748 749 750
{
    if (debug)
        qDebug() << "ProjectWindow - showProperties called";

751
    // Remove all existing panels:
dt's avatar
dt committed
752
    m_panelsWidget->clear();
con's avatar
con committed
753

754
    // delete custom panels:
dt's avatar
dt committed
755 756 757
    qDeleteAll(m_panels);
    m_panels.clear();

758
    // Set up our default panels again:
759
    m_panelsWidget->addWidget(tr("Active Build and Run Configurations"), m_activeConfigurationWidget);
dt's avatar
dt committed
760
    m_panelsWidget->addWidget(m_spacerBetween);
761
    m_panelsWidget->addWidget(m_projectChooser);
dt's avatar
dt committed
762

763
    // Set up custom panels again:
con's avatar
con committed
764 765 766 767 768 769 770 771
    if (project) {
        QList<IPanelFactory *> pages =
                ExtensionSystem::PluginManager::instance()->getObjects<IPanelFactory>();
        foreach (IPanelFactory *panelFactory, pages) {
            if (panelFactory->supports(project)) {
                PropertiesPanel *panel = panelFactory->createPanel(project);
                if (debug)
                  qDebug() << "ProjectWindow - setting up project properties tab " << panel->name();
dt's avatar
dt committed
772
                m_panelsWidget->addWidget(panel->name(), panel->widget());
dt's avatar
dt committed
773
                m_panels.push_back(panel);
con's avatar
con committed
774 775 776 777
            }
        }
    }
}