projectwindow.cpp 12.8 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"
35
#include "environment.h"
con's avatar
con committed
36 37 38 39
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "iprojectproperties.h"
#include "session.h"
Tobias Hunger's avatar
Tobias Hunger committed
40
#include "target.h"
con's avatar
con committed
41
#include "projecttreewidget.h"
dt's avatar
dt committed
42
#include "runconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
43 44 45
#include "buildsettingspropertiespage.h"
#include "runsettingspropertiespage.h"
#include "targetsettingspanel.h"
con's avatar
con committed
46 47 48 49

#include <coreplugin/minisplitter.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
50
#include <coreplugin/ifile.h>
51
#include <extensionsystem/pluginmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
52
#include <utils/qtcassert.h>
con's avatar
con committed
53
#include <utils/styledbar.h>
54
#include <utils/stylehelper.h>
con's avatar
con committed
55

56
#include <QtGui/QApplication>
con's avatar
con committed
57 58
#include <QtGui/QBoxLayout>
#include <QtGui/QComboBox>
59
#include <QtGui/QScrollArea>
dt's avatar
dt committed
60
#include <QtGui/QLabel>
dt's avatar
dt committed
61 62
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>
63 64
#include <QtGui/QMenu>

con's avatar
con committed
65 66 67 68
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
Tobias Hunger's avatar
Tobias Hunger committed
69
const int ICON_SIZE(64);
Tobias Hunger's avatar
Tobias Hunger committed
70 71 72 73

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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

bool skipPanelFactory(Project *project, IPanelFactory *factory)
{
    bool simplifyTargets(project->supportedTargetIds().count() <= 1);
    if (simplifyTargets) {
        // Do not show the targets list:
        if (factory->id() == QLatin1String(TARGETSETTINGS_PANEL_ID))
            return true;
        // Skip build settigns if none are available anyway:
        if (project->activeTarget() &&
            !project->activeTarget()->buildConfigurationFactory() &&
            factory->id() == QLatin1String(BUILDSETTINGS_PANEL_ID))
            return true;
    } else {
        // Skip panels embedded into the targets panel:
        if (factory->id() == QLatin1String(BUILDSETTINGS_PANEL_ID) ||
            factory->id() == QLatin1String(RUNSETTINGS_PANEL_ID))
            return true;
    }
    return false;
con's avatar
con committed
94 95
}

Tobias Hunger's avatar
Tobias Hunger committed
96 97
} // namespace

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
///
// 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
114
        Q_UNUSED(e);
115
        QPainter p(this);
116 117 118
        QColor fillColor = Utils::StyleHelper::mergedColors(
                palette().button().color(), Qt::black, 80);
        p.fillRect(contentsRect(), fillColor);
119 120 121
    }
};

Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
122 123 124 125 126 127 128 129 130 131 132 133
class RootWidget : public QWidget
{
public:
    RootWidget(QWidget *parent) : QWidget(parent) {}
    void paintEvent(QPaintEvent *);
};

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

    QPainter painter(this);
134 135 136 137 138 139
    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
140
    painter.drawLine(rect().topRight(), rect().bottomRight());
141
    painter.setPen(dark);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
142 143 144
    painter.drawLine(rect().topRight() - QPoint(1,0), rect().bottomRight() - QPoint(1,0));
}

145 146 147 148
///
// PanelsWidget
///

Tobias Hunger's avatar
Tobias Hunger committed
149 150
PanelsWidget::PanelsWidget(QWidget *parent) :
    QScrollArea(parent),
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
151
    m_root(new RootWidget(this))
dt's avatar
dt committed
152
{
153
    // We want a 900px wide widget with and the scrollbar at the
Tobias Hunger's avatar
Tobias Hunger committed
154
    // side of the screen.
155
    m_root->setFixedWidth(900);
156
    m_root->setContentsMargins(0, 0, 40, 0);
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
157 158
    
    QPalette pal = m_root->palette();
159 160 161
    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
162
    setPalette(pal);
163 164
    pal.setColor(QPalette::All, QPalette::Window, background);
    m_root->setPalette(pal);
Tobias Hunger's avatar
Tobias Hunger committed
165
    // The layout holding the individual panels:
Tobias Hunger's avatar
Tobias Hunger committed
166 167 168
    m_layout = new QGridLayout(m_root);
    m_layout->setColumnMinimumWidth(0, ICON_SIZE + 4);
    m_layout->setSpacing(0);
Tobias Hunger's avatar
Tobias Hunger committed
169

170
    setWidget(m_root);
dt's avatar
dt committed
171
    setFrameStyle(QFrame::NoFrame);
dt's avatar
dt committed
172
    setWidgetResizable(true);
dt's avatar
dt committed
173 174 175 176
}

PanelsWidget::~PanelsWidget()
{
con's avatar
con committed
177
    qDeleteAll(m_panels);
dt's avatar
dt committed
178 179
}

Tobias Hunger's avatar
Tobias Hunger committed
180 181 182 183 184
/*
 * Add a widget with heading information into the grid
 * layout of the PanelsWidget.
 *
 *     ...
Tobias Hunger's avatar
Tobias Hunger committed
185
 * +--------+-------------------------------------------+ ABOVE_HEADING_MARGIN
Tobias Hunger's avatar
Tobias Hunger committed
186 187
 * | icon   | name                                      |
 * +        +-------------------------------------------+
Tobias Hunger's avatar
Tobias Hunger committed
188 189 190 191
 * |        | line                                      |
 * +--------+-------------------------------------------+ ABOVE_CONTENTS_MARGIN
 * |          widget (with contentsmargins adjusted!)   |
 * +--------+-------------------------------------------+ BELOW_CONTENTS_MARGIN
Tobias Hunger's avatar
Tobias Hunger committed
192
 */
con's avatar
con committed
193
void PanelsWidget::addPropertiesPanel(IPropertiesPanel *panel)
dt's avatar
dt committed
194
{
con's avatar
con committed
195
    QTC_ASSERT(panel, return);
Tobias Hunger's avatar
Tobias Hunger committed
196

Tobias Hunger's avatar
Tobias Hunger committed
197 198 199
    const int headerRow(m_layout->rowCount() - 1);
    m_layout->setRowStretch(headerRow, 0);

Tobias Hunger's avatar
Tobias Hunger committed
200
    // icon:
con's avatar
con committed
201 202 203 204
    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
205
        m_layout->addWidget(iconLabel, headerRow, 0, 3, 1, Qt::AlignTop | Qt::AlignHCenter);
Tobias Hunger's avatar
Tobias Hunger committed
206 207 208
    }

    // name:
con's avatar
con committed
209 210
    QLabel *nameLabel = new QLabel(m_root);
    nameLabel->setText(panel->displayName());
211 212 213
    QPalette palette = nameLabel->palette();
    palette.setBrush(QPalette::All, QPalette::Foreground, QColor(0, 0, 0, 110));
    nameLabel->setPalette(palette);
con's avatar
con committed
214 215
    nameLabel->setContentsMargins(0, ABOVE_HEADING_MARGIN, 0, 0);
    QFont f = nameLabel->font();
dt's avatar
dt committed
216
    f.setBold(true);
con's avatar
con committed
217
    f.setPointSizeF(f.pointSizeF() * 1.6);
con's avatar
con committed
218
    nameLabel->setFont(f);
con's avatar
con committed
219
    m_layout->addWidget(nameLabel, headerRow, 1, 1, 1, Qt::AlignVCenter | Qt::AlignLeft);
Tobias Hunger's avatar
Tobias Hunger committed
220 221 222

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

Tobias Hunger's avatar
Tobias Hunger committed
226
    // add the widget:
Tobias Hunger's avatar
Tobias Hunger committed
227
    const int widgetRow(lineRow + 1);
con's avatar
con committed
228
    addPanelWidget(panel, widgetRow);
dt's avatar
dt committed
229 230
}

con's avatar
con committed
231
void PanelsWidget::addPanelWidget(IPropertiesPanel *panel, int row)
Tobias Hunger's avatar
Tobias Hunger committed
232
{
con's avatar
con committed
233
    QWidget *widget = panel->widget();
Tobias Hunger's avatar
Tobias Hunger committed
234 235 236 237 238
    int leftMargin = (panel->flags() & IPropertiesPanel::NoLeftMargin)
                     ? 0 : Constants::PANEL_LEFT_MARGIN;
    widget->setContentsMargins(leftMargin,
                               ABOVE_CONTENTS_MARGIN, 0,
                               BELOW_CONTENTS_MARGIN);
con's avatar
con committed
239 240
    widget->setParent(m_root);
    m_layout->addWidget(widget, row, 0, 1, 2);
Tobias Hunger's avatar
Tobias Hunger committed
241 242 243 244 245 246 247

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

    m_panels.append(panel);
}

dt's avatar
dt committed
248 249 250 251
///
// ProjectWindow
///

dt's avatar
dt committed
252
ProjectWindow::ProjectWindow(QWidget *parent)
con's avatar
con committed
253
    : QWidget(parent),
Tobias Hunger's avatar
Tobias Hunger committed
254 255
    m_currentWidget(0),
    m_currentPanel(0)
con's avatar
con committed
256
{
257
    ProjectExplorer::SessionManager *session = ProjectExplorerPlugin::instance()->session();
con's avatar
con committed
258

Tobias Hunger's avatar
Tobias Hunger committed
259 260 261 262
    // Setup overall layout:
    QVBoxLayout *viewLayout = new QVBoxLayout(this);
    viewLayout->setMargin(0);
    viewLayout->setSpacing(0);
dt's avatar
dt committed
263

con's avatar
con committed
264 265
    m_tabWidget = new DoubleTabWidget(this);
    viewLayout->addWidget(m_tabWidget);
266

Tobias Hunger's avatar
Tobias Hunger committed
267
    // Setup our container for the contents:
con's avatar
con committed
268 269
    m_centralWidget = new QStackedWidget(this);
    viewLayout->addWidget(m_centralWidget);
Tobias Hunger's avatar
Tobias Hunger committed
270 271

    // connects:
con's avatar
con committed
272 273
    connect(m_tabWidget, SIGNAL(currentIndexChanged(int,int)),
            this, SLOT(showProperties(int,int)));
dt's avatar
dt committed
274

275 276 277 278
    connect(session, SIGNAL(sessionLoaded()), this, SLOT(restoreStatus()));
    connect(session, SIGNAL(aboutToSaveSession()), this, SLOT(saveStatus()));

    connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
279
            this, SLOT(registerProject(ProjectExplorer::Project*)));
con's avatar
con committed
280
    connect(session, SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)),
281
            this, SLOT(deregisterProject(ProjectExplorer::Project*)));
282

283 284 285
    connect(session, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
            this, SLOT(startupProjectChanged(ProjectExplorer::Project *)));

286
    // Update properties to empty project for now:
con's avatar
con committed
287
    showProperties(-1, -1);
con's avatar
con committed
288 289 290 291 292 293
}

ProjectWindow::~ProjectWindow()
{
}

con's avatar
con committed
294
void ProjectWindow::shutdown()
295
{
con's avatar
con committed
296 297 298
    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);
299 300
}

301
void ProjectWindow::registerProject(ProjectExplorer::Project *project)
302
{
303
    if (!project || m_tabIndexToProject.contains(project))
Tobias Hunger's avatar
Tobias Hunger committed
304 305
        return;

306 307 308 309 310 311 312 313 314 315
    // 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
316 317
    QStringList subtabs;
    foreach (IPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<IPanelFactory>()) {
Tobias Hunger's avatar
Tobias Hunger committed
318 319 320
        if (skipPanelFactory(project, panelFactory))
            continue;
        subtabs << panelFactory->displayName();
321
    }
Tobias Hunger's avatar
Tobias Hunger committed
322

con's avatar
con committed
323 324
    m_tabIndexToProject.insert(index, project);
    m_tabWidget->insertTab(index, project->displayName(), subtabs);
325 326 327

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

330
void ProjectWindow::deregisterProject(ProjectExplorer::Project *project)
con's avatar
con committed
331 332
{
    int index = m_tabIndexToProject.indexOf(project);
333 334
    if (index < 0)
        return;
335 336 337 338

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

con's avatar
con committed
339 340
    m_tabIndexToProject.removeAt(index);
    m_tabWidget->removeTab(index);
341 342
}

con's avatar
con committed
343 344
void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
345
    // TODO
con's avatar
con committed
346 347 348 349
}

void ProjectWindow::saveStatus()
{
dt's avatar
dt committed
350
    // TODO
con's avatar
con committed
351 352
}

353 354 355 356 357 358
void ProjectWindow::refreshProject()
{
    Project *project = qobject_cast<ProjectExplorer::Project *>(sender());
    if (!m_tabIndexToProject.contains(project))
        return;

359 360
    // TODO this changes the subindex
    int index = m_tabWidget->currentIndex();
361 362
    deregisterProject(project);
    registerProject(project);
363 364 365 366 367 368 369
    m_tabWidget->setCurrentIndex(index);
}

void ProjectWindow::startupProjectChanged(ProjectExplorer::Project *p)
{
    int index = m_tabIndexToProject.indexOf(p);
    m_tabWidget->setCurrentIndex(index);
370 371
}

con's avatar
con committed
372
void ProjectWindow::showProperties(int index, int subIndex)
con's avatar
con committed
373
{
374
    if (index < 0 || index >= m_tabIndexToProject.count())
con's avatar
con committed
375
        return;
376

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

379
    // Set up custom panels again:
Tobias Hunger's avatar
Tobias Hunger committed
380 381 382 383 384 385 386 387 388 389 390 391
    int pos = 0;
    foreach (IPanelFactory *panelFactory, ExtensionSystem::PluginManager::instance()->getObjects<IPanelFactory>()) {
        if (skipPanelFactory(project, panelFactory))
            continue;
        if (pos == subIndex) {
            removeCurrentWidget();
            IPropertiesPanel *panel(panelFactory->createPanel(project));
            if (panel->flags() & IPropertiesPanel::NoAutomaticStyle) {
                m_currentWidget = panel->widget();
                m_currentPanel = panel;
            } else {
                PanelsWidget *panelsWidget = new PanelsWidget(m_centralWidget);
con's avatar
con committed
392
                panelsWidget->addPropertiesPanel(panel);
Tobias Hunger's avatar
Tobias Hunger committed
393
                m_currentWidget = panelsWidget;
con's avatar
con committed
394
            }
Tobias Hunger's avatar
Tobias Hunger committed
395 396 397
            m_centralWidget->addWidget(m_currentWidget);
            m_centralWidget->setCurrentWidget(m_currentWidget);
            break;
con's avatar
con committed
398
        }
Tobias Hunger's avatar
Tobias Hunger committed
399
        ++pos;
con's avatar
con committed
400
    }
401
    ProjectExplorerPlugin::instance()->session()->setStartupProject(project);
Tobias Hunger's avatar
Tobias Hunger committed
402 403 404 405 406 407 408 409 410 411 412 413 414
}

void ProjectWindow::removeCurrentWidget()
{
    if (m_currentWidget) {
        m_centralWidget->removeWidget(m_currentWidget);
        if (m_currentPanel) {
            delete m_currentPanel;
            m_currentPanel = 0;
            m_currentWidget = 0; // is deleted by the panel
        } else if (m_currentWidget) {
            delete m_currentWidget;
            m_currentWidget = 0;
con's avatar
con committed
415 416 417
        }
    }
}