projectwindow.cpp 15.5 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 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
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "projectwindow.h"
hjk's avatar
hjk committed
34

con's avatar
con committed
35 36
#include "doubletabwidget.h"

hjk's avatar
hjk committed
37
#include "project.h"
con's avatar
con committed
38 39 40 41
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "session.h"
#include "projecttreewidget.h"
42
#include "iprojectproperties.h"
Tobias Hunger's avatar
Tobias Hunger committed
43
#include "targetsettingspanel.h"
44
#include "target.h"
con's avatar
con committed
45 46

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

52
#include <QtGui/QApplication>
53
#include <QtGui/QGridLayout>
dt's avatar
dt committed
54
#include <QtGui/QLabel>
dt's avatar
dt committed
55
#include <QtGui/QPainter>
56
#include <QtGui/QStackedWidget>
57

con's avatar
con committed
58 59 60 61
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

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

const int ABOVE_HEADING_MARGIN(10);
const int ABOVE_CONTENTS_MARGIN(4);
const int BELOW_CONTENTS_MARGIN(16);
Tobias Hunger's avatar
Tobias Hunger committed
67

68
} // anonymous namespace
Tobias Hunger's avatar
Tobias Hunger committed
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
///
// 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
86
        Q_UNUSED(e);
87
        QPainter p(this);
88 89 90
        QColor fillColor = Utils::StyleHelper::mergedColors(
                palette().button().color(), Qt::black, 80);
        p.fillRect(contentsRect(), fillColor);
91 92 93
    }
};

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
94 95 96 97 98 99 100 101 102 103 104 105
class RootWidget : public QWidget
{
public:
    RootWidget(QWidget *parent) : QWidget(parent) {}
    void paintEvent(QPaintEvent *);
};

void RootWidget::paintEvent(QPaintEvent *e)
{
    QWidget::paintEvent(e);

    QPainter painter(this);
106 107 108 109 110 111
    QColor light = Utils::StyleHelper::mergedColors(
            palette().button().color(), Qt::white, 30);
    QColor dark = Utils::StyleHelper::mergedColors(
            palette().button().color(), Qt::black, 85);

    painter.setPen(light);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
112
    painter.drawLine(rect().topRight(), rect().bottomRight());
113
    painter.setPen(dark);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
114 115 116
    painter.drawLine(rect().topRight() - QPoint(1,0), rect().bottomRight() - QPoint(1,0));
}

117 118 119 120
///
// PanelsWidget
///

Tobias Hunger's avatar
Tobias Hunger committed
121 122
PanelsWidget::PanelsWidget(QWidget *parent) :
    QScrollArea(parent),
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
123
    m_root(new RootWidget(this))
dt's avatar
dt committed
124
{
125
    // We want a 900px wide widget with and the scrollbar at the
Tobias Hunger's avatar
Tobias Hunger committed
126
    // side of the screen.
127
    m_root->setFixedWidth(900);
128
    m_root->setContentsMargins(0, 0, 40, 0);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
129 130
    
    QPalette pal = m_root->palette();
131 132 133
    QColor background = Utils::StyleHelper::mergedColors(
            palette().window().color(), Qt::white, 85);
    pal.setColor(QPalette::All, QPalette::Window, background.darker(102));
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
134
    setPalette(pal);
135 136
    pal.setColor(QPalette::All, QPalette::Window, background);
    m_root->setPalette(pal);
Tobias Hunger's avatar
Tobias Hunger committed
137
    // The layout holding the individual panels:
Tobias Hunger's avatar
Tobias Hunger committed
138 139 140
    m_layout = new QGridLayout(m_root);
    m_layout->setColumnMinimumWidth(0, ICON_SIZE + 4);
    m_layout->setSpacing(0);
Tobias Hunger's avatar
Tobias Hunger committed
141

142
    setWidget(m_root);
dt's avatar
dt committed
143
    setFrameStyle(QFrame::NoFrame);
dt's avatar
dt committed
144
    setWidgetResizable(true);
145
    setFocusPolicy(Qt::NoFocus);
dt's avatar
dt committed
146 147 148 149
}

PanelsWidget::~PanelsWidget()
{
con's avatar
con committed
150
    qDeleteAll(m_panels);
dt's avatar
dt committed
151 152
}

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

Tobias Hunger's avatar
Tobias Hunger committed
170 171
    const int headerRow(m_layout->rowCount() - 1);

Tobias Hunger's avatar
Tobias Hunger committed
172
    // icon:
con's avatar
con committed
173 174 175 176
    if (!panel->icon().isNull()) {
        QLabel *iconLabel = new QLabel(m_root);
        iconLabel->setPixmap(panel->icon().pixmap(ICON_SIZE, ICON_SIZE));
        iconLabel->setContentsMargins(0, ABOVE_HEADING_MARGIN, 0, 0);
con's avatar
con committed
177
        m_layout->addWidget(iconLabel, headerRow, 0, 3, 1, Qt::AlignTop | Qt::AlignHCenter);
Tobias Hunger's avatar
Tobias Hunger committed
178 179 180
    }

    // name:
con's avatar
con committed
181 182
    QLabel *nameLabel = new QLabel(m_root);
    nameLabel->setText(panel->displayName());
183 184 185
    QPalette palette = nameLabel->palette();
    palette.setBrush(QPalette::All, QPalette::Foreground, QColor(0, 0, 0, 110));
    nameLabel->setPalette(palette);
con's avatar
con committed
186 187
    nameLabel->setContentsMargins(0, ABOVE_HEADING_MARGIN, 0, 0);
    QFont f = nameLabel->font();
dt's avatar
dt committed
188
    f.setBold(true);
con's avatar
con committed
189
    f.setPointSizeF(f.pointSizeF() * 1.6);
con's avatar
con committed
190
    nameLabel->setFont(f);
con's avatar
con committed
191
    m_layout->addWidget(nameLabel, headerRow, 1, 1, 1, Qt::AlignVCenter | Qt::AlignLeft);
Tobias Hunger's avatar
Tobias Hunger committed
192 193 194

    // line:
    const int lineRow(headerRow + 1);
con's avatar
con committed
195 196
    QWidget *line = new OnePixelBlackLine(m_root);
    m_layout->addWidget(line, lineRow, 1, 1, -1, Qt::AlignTop);
Tobias Hunger's avatar
Tobias Hunger committed
197

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

203
void PanelsWidget::addPanelWidget(PropertiesPanel *panel, int row)
Tobias Hunger's avatar
Tobias Hunger committed
204
{
con's avatar
con committed
205
    QWidget *widget = panel->widget();
206
    widget->setContentsMargins(Constants::PANEL_LEFT_MARGIN,
Tobias Hunger's avatar
Tobias Hunger committed
207 208
                               ABOVE_CONTENTS_MARGIN, 0,
                               BELOW_CONTENTS_MARGIN);
con's avatar
con committed
209 210
    widget->setParent(m_root);
    m_layout->addWidget(widget, row, 0, 1, 2);
Tobias Hunger's avatar
Tobias Hunger committed
211 212 213 214

    m_panels.append(panel);
}

dt's avatar
dt committed
215 216 217 218
///
// ProjectWindow
///

dt's avatar
dt committed
219
ProjectWindow::ProjectWindow(QWidget *parent)
con's avatar
con committed
220
    : QWidget(parent),
221 222
      m_currentWidget(0),
      m_previousTargetSubIndex(-1)
con's avatar
con committed
223
{
224
    ProjectExplorer::SessionManager *session = ProjectExplorerPlugin::instance()->session();
con's avatar
con committed
225

Tobias Hunger's avatar
Tobias Hunger committed
226 227 228 229
    // Setup overall layout:
    QVBoxLayout *viewLayout = new QVBoxLayout(this);
    viewLayout->setMargin(0);
    viewLayout->setSpacing(0);
dt's avatar
dt committed
230

con's avatar
con committed
231 232
    m_tabWidget = new DoubleTabWidget(this);
    viewLayout->addWidget(m_tabWidget);
233

Tobias Hunger's avatar
Tobias Hunger committed
234
    // Setup our container for the contents:
con's avatar
con committed
235 236
    m_centralWidget = new QStackedWidget(this);
    viewLayout->addWidget(m_centralWidget);
Tobias Hunger's avatar
Tobias Hunger committed
237 238

    // connects:
con's avatar
con committed
239 240
    connect(m_tabWidget, SIGNAL(currentIndexChanged(int,int)),
            this, SLOT(showProperties(int,int)));
dt's avatar
dt committed
241

242 243 244 245
    connect(session, SIGNAL(sessionLoaded()), this, SLOT(restoreStatus()));
    connect(session, SIGNAL(aboutToSaveSession()), this, SLOT(saveStatus()));

    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
246
            this, SLOT(registerProject(ProjectExplorer::Project*)));
con's avatar
con committed
247
    connect(session, SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)),
248
            this, SLOT(deregisterProject(ProjectExplorer::Project*)));
249

250 251 252
    connect(session, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(startupProjectChanged(ProjectExplorer::Project *)));

253
    // Update properties to empty project for now:
con's avatar
con committed
254
    showProperties(-1, -1);
con's avatar
con committed
255 256 257 258 259 260
}

ProjectWindow::~ProjectWindow()
{
}

261 262 263 264 265 266
void ProjectWindow::extensionsInitialized()
{
    foreach (ITargetFactory *fac, ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>())
        connect(fac, SIGNAL(supportedTargetIdsChanged()),
                this, SLOT(targetFactoriesChanged()));

267 268 269 270 271 272
    QList<IProjectPanelFactory *> list = ExtensionSystem::PluginManager::instance()->getObjects<IProjectPanelFactory>();
    qSort(list.begin(), list.end(), &IPanelFactory::prioritySort);
    foreach (IProjectPanelFactory *fac, list)
        connect (fac, SIGNAL(projectUpdated(ProjectExplorer::Project *)),
                 this, SLOT(projectUpdated(ProjectExplorer::Project *)));

273 274
}

275
void ProjectWindow::aboutToShutdown()
276
{
con's avatar
con committed
277 278 279
    showProperties(-1, -1); // TODO that's a bit stupid, but otherwise stuff is still
                             // connected to the session
    disconnect(ProjectExplorerPlugin::instance()->session(), 0, this, 0);
280 281
}

282 283 284 285 286 287 288 289 290
void ProjectWindow::projectUpdated(Project *p)
{
    // Called after a project was configured
    int index = m_tabWidget->currentIndex();
    deregisterProject(p);
    registerProject(p);
    m_tabWidget->setCurrentIndex(index);
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
void ProjectWindow::targetFactoriesChanged()
{
    bool changed = false;
    int index = m_tabWidget->currentIndex();
    QList<Project *> projects = m_tabIndexToProject;
    foreach (ProjectExplorer::Project *project, projects) {
        if (m_usesTargetPage.value(project) != useTargetPage(project)) {
            changed = true;
            deregisterProject(project);
            registerProject(project);
        }
    }
    if (changed)
        m_tabWidget->setCurrentIndex(index);
}

bool ProjectWindow::useTargetPage(ProjectExplorer::Project *project)
{
309 310
    if (project->targets().isEmpty())
        return false;
311 312 313 314 315 316 317 318 319 320 321
    if (project->targets().size() > 1)
        return true;
    QStringList tmp;
    foreach (ITargetFactory *fac, ExtensionSystem::PluginManager::instance()->getObjects<ITargetFactory>()) {
        tmp.append(fac->supportedTargetIds(project));
        if (tmp.size() > 1)
            return true;
    }
    return false;
}

322
void ProjectWindow::registerProject(ProjectExplorer::Project *project)
323
{
324
    if (!project || m_tabIndexToProject.contains(project))
Tobias Hunger's avatar
Tobias Hunger committed
325 326
        return;

327 328 329 330 331 332 333 334 335 336
    // find index to insert:
    int index = -1;
    for (int i = 0; i <= m_tabIndexToProject.count(); ++i) {
        if (i == m_tabIndexToProject.count() ||
            m_tabIndexToProject.at(i)->displayName() > project->displayName()) {
            index = i;
            break;
        }
    }

con's avatar
con committed
337
    QStringList subtabs;
338 339 340 341 342

    bool usesTargetPage = useTargetPage(project);
    m_usesTargetPage.insert(project, usesTargetPage);

    if (!usesTargetPage){
343
        // Show the target specific pages directly
344 345 346 347 348
        if (project->activeTarget()) {
            QList<ITargetPanelFactory *> factories =
                    ExtensionSystem::PluginManager::instance()->getObjects<ITargetPanelFactory>();

            qSort(factories.begin(), factories.end(), &IPanelFactory::prioritySort);
349

350 351 352
            foreach (ITargetPanelFactory *factory, factories)
                if (factory->supports(project->activeTarget()))
                    subtabs << factory->displayName();
353 354 355 356 357 358 359 360
        }
    } else {
        // Use the Targets page
        subtabs << QCoreApplication::translate("TargetSettingsPanelFactory", "Targets");
    }

    // Add the project specific pages
    QList<IProjectPanelFactory *> factories = ExtensionSystem::PluginManager::instance()->getObjects<IProjectPanelFactory>();
361
    qSort(factories.begin(), factories.end(), &IPanelFactory::prioritySort);
362 363 364
    foreach (IProjectPanelFactory *panelFactory, factories) {
        if (panelFactory->supports(project))
            subtabs << panelFactory->displayName();
365
    }
Tobias Hunger's avatar
Tobias Hunger committed
366

con's avatar
con committed
367
    m_tabIndexToProject.insert(index, project);
dt's avatar
dt committed
368
    m_tabWidget->insertTab(index, project->displayName(), project->file()->fileName(), subtabs);
con's avatar
con committed
369 370
}

371
void ProjectWindow::deregisterProject(ProjectExplorer::Project *project)
con's avatar
con committed
372 373
{
    int index = m_tabIndexToProject.indexOf(project);
374 375
    if (index < 0)
        return;
376

con's avatar
con committed
377 378
    m_tabIndexToProject.removeAt(index);
    m_tabWidget->removeTab(index);
379 380
}

con's avatar
con committed
381 382
void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
383
    // TODO
con's avatar
con committed
384 385 386 387
}

void ProjectWindow::saveStatus()
{
dt's avatar
dt committed
388
    // TODO
con's avatar
con committed
389 390
}

391 392 393
void ProjectWindow::startupProjectChanged(ProjectExplorer::Project *p)
{
    int index = m_tabIndexToProject.indexOf(p);
394 395
    if (index != -1)
        m_tabWidget->setCurrentIndex(index);
396 397
}

con's avatar
con committed
398
void ProjectWindow::showProperties(int index, int subIndex)
con's avatar
con committed
399
{
400 401
    if (index < 0 || index >= m_tabIndexToProject.count()) {
        removeCurrentWidget();
con's avatar
con committed
402
        return;
403
    }
404

con's avatar
con committed
405
    Project *project = m_tabIndexToProject.at(index);
dt's avatar
dt committed
406

407
    // Set up custom panels again:
Tobias Hunger's avatar
Tobias Hunger committed
408
    int pos = 0;
409
    IPanelFactory *fac = 0;
410 411 412 413 414
    // remember previous sub index state of target settings page
    if (TargetSettingsPanelWidget *previousPanelWidget
            = qobject_cast<TargetSettingsPanelWidget*>(m_currentWidget)) {
        m_previousTargetSubIndex = previousPanelWidget->currentSubIndex();
    }
415 416

    if (m_usesTargetPage.value(project)) {
417 418
        if (subIndex == 0) {
            // Targets page
Tobias Hunger's avatar
Tobias Hunger committed
419
            removeCurrentWidget();
420 421 422 423
            TargetSettingsPanelWidget *panelWidget = new TargetSettingsPanelWidget(project);
            if (m_previousTargetSubIndex >= 0)
                panelWidget->setCurrentSubIndex(m_previousTargetSubIndex);
            m_currentWidget = panelWidget;
Tobias Hunger's avatar
Tobias Hunger committed
424 425
            m_centralWidget->addWidget(m_currentWidget);
            m_centralWidget->setCurrentWidget(m_currentWidget);
con's avatar
con committed
426
        }
Tobias Hunger's avatar
Tobias Hunger committed
427
        ++pos;
428
    } else if (project->activeTarget()) {
429
        // No Targets page, target specific pages are first in the list
430 431 432
        QList<ITargetPanelFactory *> factories = ExtensionSystem::PluginManager::instance()->getObjects<ITargetPanelFactory>();
        qSort(factories.begin(), factories.end(), &ITargetPanelFactory::prioritySort);
        foreach (ITargetPanelFactory *panelFactory, factories) {
433 434 435 436 437 438 439 440 441 442 443
            if (panelFactory->supports(project->activeTarget())) {
                if (subIndex == pos) {
                    fac = panelFactory;
                    break;
                }
                ++pos;
            }
        }
    }

    if (!fac) {
444 445 446
        QList<IProjectPanelFactory *> factories = ExtensionSystem::PluginManager::instance()->getObjects<IProjectPanelFactory>();
        qSort(factories.begin(), factories.end(), &IPanelFactory::prioritySort);
        foreach (IProjectPanelFactory *panelFactory, factories) {
447 448 449 450 451 452 453 454
            if (panelFactory->supports(project)) {
                if (subIndex == pos) {
                    fac = panelFactory;
                    break;
                }
                ++pos;
            }
        }
con's avatar
con committed
455
    }
456 457 458 459

    if (fac) {
        removeCurrentWidget();

460
        PropertiesPanel *panel = 0;
461 462 463 464 465 466 467 468 469 470 471 472 473 474
        if (ITargetPanelFactory *ipf = qobject_cast<ITargetPanelFactory *>(fac))
            panel = ipf->createPanel(project->activeTarget());
        else if (IProjectPanelFactory *ipf = qobject_cast<IProjectPanelFactory *>(fac))
            panel = ipf->createPanel(project);
        Q_ASSERT(panel);

        PanelsWidget *panelsWidget = new PanelsWidget(m_centralWidget);
        panelsWidget->addPropertiesPanel(panel);
        m_currentWidget = panelsWidget;
        m_centralWidget->addWidget(m_currentWidget);
        m_centralWidget->setCurrentWidget(m_currentWidget);

    }

475
    ProjectExplorerPlugin::instance()->session()->setStartupProject(project);
Tobias Hunger's avatar
Tobias Hunger committed
476 477 478 479 480 481
}

void ProjectWindow::removeCurrentWidget()
{
    if (m_currentWidget) {
        m_centralWidget->removeWidget(m_currentWidget);
482
        if (m_currentWidget) {
Tobias Hunger's avatar
Tobias Hunger committed
483 484
            delete m_currentWidget;
            m_currentWidget = 0;
con's avatar
con committed
485 486 487
        }
    }
}