projectwindow.cpp 13.6 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) 2010 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

con's avatar
con committed
32 33
#include "doubletabwidget.h"

hjk's avatar
hjk committed
34
#include "project.h"
con's avatar
con committed
35 36 37 38
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "session.h"
#include "projecttreewidget.h"
39
#include "iprojectproperties.h"
Tobias Hunger's avatar
Tobias Hunger committed
40
#include "targetsettingspanel.h"
con's avatar
con committed
41 42

#include <coreplugin/icore.h>
43
#include <coreplugin/ifile.h>
44
#include <extensionsystem/pluginmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
45
#include <utils/qtcassert.h>
46
#include <utils/stylehelper.h>
con's avatar
con committed
47

48
#include <QtGui/QApplication>
49
#include <QtGui/QGridLayout>
dt's avatar
dt committed
50
#include <QtGui/QLabel>
dt's avatar
dt committed
51
#include <QtGui/QPainter>
52
#include <QtGui/QStackedWidget>
53

con's avatar
con committed
54 55 56 57
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
Tobias Hunger's avatar
Tobias Hunger committed
58
const int ICON_SIZE(64);
Tobias Hunger's avatar
Tobias Hunger committed
59 60 61 62

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
63

64
} // anonymous namespace
Tobias Hunger's avatar
Tobias Hunger committed
65

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

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
90 91 92 93 94 95 96 97 98 99 100 101
class RootWidget : public QWidget
{
public:
    RootWidget(QWidget *parent) : QWidget(parent) {}
    void paintEvent(QPaintEvent *);
};

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

    QPainter painter(this);
102 103 104 105 106 107
    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
108
    painter.drawLine(rect().topRight(), rect().bottomRight());
109
    painter.setPen(dark);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
110 111 112
    painter.drawLine(rect().topRight() - QPoint(1,0), rect().bottomRight() - QPoint(1,0));
}

113 114 115 116
///
// PanelsWidget
///

Tobias Hunger's avatar
Tobias Hunger committed
117 118
PanelsWidget::PanelsWidget(QWidget *parent) :
    QScrollArea(parent),
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
119
    m_root(new RootWidget(this))
dt's avatar
dt committed
120
{
121
    // We want a 900px wide widget with and the scrollbar at the
Tobias Hunger's avatar
Tobias Hunger committed
122
    // side of the screen.
123
    m_root->setFixedWidth(900);
124
    m_root->setContentsMargins(0, 0, 40, 0);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
125 126
    
    QPalette pal = m_root->palette();
127 128 129
    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
130
    setPalette(pal);
131 132
    pal.setColor(QPalette::All, QPalette::Window, background);
    m_root->setPalette(pal);
Tobias Hunger's avatar
Tobias Hunger committed
133
    // The layout holding the individual panels:
Tobias Hunger's avatar
Tobias Hunger committed
134 135 136
    m_layout = new QGridLayout(m_root);
    m_layout->setColumnMinimumWidth(0, ICON_SIZE + 4);
    m_layout->setSpacing(0);
Tobias Hunger's avatar
Tobias Hunger committed
137

138
    setWidget(m_root);
dt's avatar
dt committed
139
    setFrameStyle(QFrame::NoFrame);
dt's avatar
dt committed
140
    setWidgetResizable(true);
dt's avatar
dt committed
141 142 143 144
}

PanelsWidget::~PanelsWidget()
{
con's avatar
con committed
145
    qDeleteAll(m_panels);
dt's avatar
dt committed
146 147
}

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

Tobias Hunger's avatar
Tobias Hunger committed
165 166 167
    const int headerRow(m_layout->rowCount() - 1);
    m_layout->setRowStretch(headerRow, 0);

Tobias Hunger's avatar
Tobias Hunger committed
168
    // icon:
con's avatar
con committed
169 170 171 172
    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
173
        m_layout->addWidget(iconLabel, headerRow, 0, 3, 1, Qt::AlignTop | Qt::AlignHCenter);
Tobias Hunger's avatar
Tobias Hunger committed
174 175 176
    }

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

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

Tobias Hunger's avatar
Tobias Hunger committed
194
    // add the widget:
Tobias Hunger's avatar
Tobias Hunger committed
195
    const int widgetRow(lineRow + 1);
con's avatar
con committed
196
    addPanelWidget(panel, widgetRow);
dt's avatar
dt committed
197 198
}

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

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

    m_panels.append(panel);
}

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

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

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

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

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

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

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

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

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

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

ProjectWindow::~ProjectWindow()
{
}

260
void ProjectWindow::aboutToShutdown()
261
{
con's avatar
con committed
262 263 264
    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);
265 266
}

267
void ProjectWindow::registerProject(ProjectExplorer::Project *project)
268
{
269
    if (!project || m_tabIndexToProject.contains(project))
Tobias Hunger's avatar
Tobias Hunger committed
270 271
        return;

272 273 274 275 276 277 278 279 280 281
    // 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
282
    QStringList subtabs;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    if (project->supportedTargetIds().count() <= 1) {
        // Show the target specific pages directly
        QList<ITargetPanelFactory *> factories =
                ExtensionSystem::PluginManager::instance()->getObjects<ITargetPanelFactory>();

        foreach (ITargetPanelFactory *factory, factories) {
            if (factory->supports(project->activeTarget()))
                subtabs << factory->displayName();
        }
    } else {
        // Use the Targets page
        subtabs << QCoreApplication::translate("TargetSettingsPanelFactory", "Targets");
    }

    // Add the project specific pages

    QList<IProjectPanelFactory *> factories = ExtensionSystem::PluginManager::instance()->getObjects<IProjectPanelFactory>();
    foreach (IProjectPanelFactory *panelFactory, factories) {
        if (panelFactory->supports(project))
            subtabs << panelFactory->displayName();
303
    }
Tobias Hunger's avatar
Tobias Hunger committed
304

con's avatar
con committed
305 306
    m_tabIndexToProject.insert(index, project);
    m_tabWidget->insertTab(index, project->displayName(), subtabs);
307 308 309

    connect(project, SIGNAL(supportedTargetIdsChanged()),
            this, SLOT(refreshProject()));
con's avatar
con committed
310 311
}

312
void ProjectWindow::deregisterProject(ProjectExplorer::Project *project)
con's avatar
con committed
313 314
{
    int index = m_tabIndexToProject.indexOf(project);
315 316
    if (index < 0)
        return;
317 318 319 320

    disconnect(project, SIGNAL(supportedTargetIdsChanged()),
               this, SLOT(refreshProject()));

con's avatar
con committed
321 322
    m_tabIndexToProject.removeAt(index);
    m_tabWidget->removeTab(index);
323 324
}

con's avatar
con committed
325 326
void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
327
    // TODO
con's avatar
con committed
328 329 330 331
}

void ProjectWindow::saveStatus()
{
dt's avatar
dt committed
332
    // TODO
con's avatar
con committed
333 334
}

335 336 337 338 339 340
void ProjectWindow::refreshProject()
{
    Project *project = qobject_cast<ProjectExplorer::Project *>(sender());
    if (!m_tabIndexToProject.contains(project))
        return;

341 342
    // TODO this changes the subindex
    int index = m_tabWidget->currentIndex();
343 344
    deregisterProject(project);
    registerProject(project);
345 346 347 348 349 350
    m_tabWidget->setCurrentIndex(index);
}

void ProjectWindow::startupProjectChanged(ProjectExplorer::Project *p)
{
    int index = m_tabIndexToProject.indexOf(p);
351 352
    if (index != -1)
        m_tabWidget->setCurrentIndex(index);
353 354
}

con's avatar
con committed
355
void ProjectWindow::showProperties(int index, int subIndex)
con's avatar
con committed
356
{
357 358
    if (index < 0 || index >= m_tabIndexToProject.count()) {
        removeCurrentWidget();
con's avatar
con committed
359
        return;
360
    }
361

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

364
    // Set up custom panels again:
Tobias Hunger's avatar
Tobias Hunger committed
365
    int pos = 0;
366
    IPanelFactory *fac = 0;
367 368 369 370 371
    // remember previous sub index state of target settings page
    if (TargetSettingsPanelWidget *previousPanelWidget
            = qobject_cast<TargetSettingsPanelWidget*>(m_currentWidget)) {
        m_previousTargetSubIndex = previousPanelWidget->currentSubIndex();
    }
372 373 374
    if (project->supportedTargetIds().count() > 1) {
        if (subIndex == 0) {
            // Targets page
Tobias Hunger's avatar
Tobias Hunger committed
375
            removeCurrentWidget();
376 377 378 379
            TargetSettingsPanelWidget *panelWidget = new TargetSettingsPanelWidget(project);
            if (m_previousTargetSubIndex >= 0)
                panelWidget->setCurrentSubIndex(m_previousTargetSubIndex);
            m_currentWidget = panelWidget;
Tobias Hunger's avatar
Tobias Hunger committed
380 381
            m_centralWidget->addWidget(m_currentWidget);
            m_centralWidget->setCurrentWidget(m_currentWidget);
con's avatar
con committed
382
        }
Tobias Hunger's avatar
Tobias Hunger committed
383
        ++pos;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    } else {
        // No Targets page, target specific pages are first in the list
        foreach (ITargetPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<ITargetPanelFactory>()) {
            if (panelFactory->supports(project->activeTarget())) {
                if (subIndex == pos) {
                    fac = panelFactory;
                    break;
                }
                ++pos;
            }
        }
    }

    if (!fac) {
        foreach (IProjectPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<IProjectPanelFactory>()) {
            if (panelFactory->supports(project)) {
                if (subIndex == pos) {
                    fac = panelFactory;
                    break;
                }
                ++pos;
            }
        }
con's avatar
con committed
407
    }
408 409 410 411

    if (fac) {
        removeCurrentWidget();

412
        IPropertiesPanel *panel = 0;
413 414 415 416 417 418 419 420 421 422 423 424 425 426
        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);

    }

427
    ProjectExplorerPlugin::instance()->session()->setStartupProject(project);
Tobias Hunger's avatar
Tobias Hunger committed
428 429 430 431 432 433
}

void ProjectWindow::removeCurrentWidget()
{
    if (m_currentWidget) {
        m_centralWidget->removeWidget(m_currentWidget);
434
        if (m_currentWidget) {
Tobias Hunger's avatar
Tobias Hunger committed
435 436
            delete m_currentWidget;
            m_currentWidget = 0;
con's avatar
con committed
437 438 439
        }
    }
}