projectwindow.cpp 11 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
26
** contact the sales department at http://www.qtsoftware.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"
con's avatar
con committed
33
34
35
36
37
38
39
40
41
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "iprojectproperties.h"
#include "session.h"
#include "projecttreewidget.h"

#include <coreplugin/minisplitter.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
42
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
43
44

#include <QtCore/QDebug>
45
#include <QtGui/QApplication>
con's avatar
con committed
46
47
#include <QtGui/QBoxLayout>
#include <QtGui/QComboBox>
48
#include <QtGui/QScrollArea>
con's avatar
con committed
49
50
51
52
53
54
55
56
57
58
59
60
#include <QtGui/QTabWidget>
#include <QtGui/QToolBar>
#include <QtGui/QTreeWidget>
#include <QtGui/QHeaderView>

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
bool debug = false;
}

dt's avatar
dt committed
61
62
ProjectWindow::ProjectWindow(QWidget *parent)
    : QWidget(parent), m_currentItemChanged(false)
con's avatar
con committed
63
64
65
66
{
    setWindowTitle(tr("Project Explorer"));
    setWindowIcon(QIcon(":/projectexplorer/images/projectexplorer.png"));

67
68
    m_projectExplorer = ProjectExplorerPlugin::instance();
    m_session = m_projectExplorer->session();
con's avatar
con committed
69
70

    m_treeWidget = new QTreeWidget(this);
dt's avatar
dt committed
71
72
    m_treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    m_treeWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
con's avatar
con committed
73
74
75
76
77
78
79
80
81
82
    m_treeWidget->setFrameStyle(QFrame::NoFrame);
    m_treeWidget->setRootIsDecorated(false);
    m_treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
    m_treeWidget->setHeaderLabels(QStringList()
                                  << tr("Projects")
                                  << tr("Startup")
                                  << tr("Path")
        );

    connect(m_treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
dt's avatar
dt committed
83
            this, SLOT(handleItem(QTreeWidgetItem*, int)));
con's avatar
con committed
84
85
86
87
88
89
90
91
    connect(m_treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem *)),
            this, SLOT(handleCurrentItemChanged(QTreeWidgetItem*)));

    QWidget *panelsWidget = new QWidget;
    m_panelsTabWidget = new QTabWidget;
    m_panelsTabWidget->setDocumentMode(true);
    QVBoxLayout *panelsLayout = new QVBoxLayout(panelsWidget);

92
93
94
95
96
97
98
99
100
    QWidget *marginWidget = new QWidget;
    QVBoxLayout *marginLayout = new QVBoxLayout(marginWidget);
    marginLayout->setContentsMargins(0, panelsLayout->margin(), 0, 0);
    marginLayout->addWidget(m_panelsTabWidget);

    QScrollArea *scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    scrollArea->setFrameStyle(QFrame::NoFrame);
    scrollArea->setWidget(marginWidget);
con's avatar
con committed
101
    panelsLayout->setSpacing(0);
102
103
    panelsLayout->setMargin(0);
    panelsLayout->addWidget(scrollArea);
con's avatar
con committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

    QWidget *dummy = new QWidget;
    QVBoxLayout *dummyLayout = new QVBoxLayout(dummy);
    dummyLayout->setMargin(0);
    dummyLayout->setSpacing(0);
    dummyLayout->addWidget(new QToolBar(dummy));
    dummyLayout->addWidget(m_treeWidget);

    QSplitter *splitter = new Core::MiniSplitter;
    splitter->setOrientation(Qt::Vertical);
    splitter->addWidget(dummy);
    splitter->addWidget(panelsWidget);



    // make sure that the tree treewidget has same size policy as qtabwidget
    m_treeWidget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    const int treeWidgetMinSize = m_treeWidget->minimumSizeHint().height();
    splitter->setSizes(QList<int>() << treeWidgetMinSize << splitter->height() - treeWidgetMinSize);

    QVBoxLayout *topLayout = new QVBoxLayout(this);
    topLayout->setMargin(0);
    topLayout->setSpacing(0);
    topLayout->addWidget(splitter);

dt's avatar
dt committed
129
130
131
132
133
134
135
136
    connect(m_session, SIGNAL(sessionLoaded()), this, SLOT(restoreStatus()));
    connect(m_session, SIGNAL(aboutToSaveSession()), this, SLOT(saveStatus()));

    connect(m_session, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)), this, SLOT(updateTreeWidgetStatupProjectChanged(ProjectExplorer::Project*)));
    connect(m_session, SIGNAL(projectAdded(ProjectExplorer::Project*)), this, SLOT(updateTreeWidgetProjectAdded(ProjectExplorer::Project*)));
    connect(m_session, SIGNAL(projectRemoved(ProjectExplorer::Project*)), this, SLOT(updateTreeWidgetProjectRemoved(ProjectExplorer::Project*)));
    connect(m_session, SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)), this, SLOT(updateTreeWidgetAboutToRemoveProject(ProjectExplorer::Project*)));

con's avatar
con committed
137
138
139
140
141
142
143
144
}

ProjectWindow::~ProjectWindow()
{
}

void ProjectWindow::restoreStatus()
{
dt's avatar
dt committed
145
146
147
148
    if (!m_treeWidget->currentItem() && m_treeWidget->topLevelItemCount()) {
        m_treeWidget->setCurrentItem(m_treeWidget->topLevelItem(0), 0, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
    }

con's avatar
con committed
149
150
151
152
153
154
    const QVariant lastPanel = m_session->value(QLatin1String("ProjectWindow/Panel"));
    if (lastPanel.isValid()) {
        const int index = lastPanel.toInt();
        if (index < m_panelsTabWidget->count())
            m_panelsTabWidget->setCurrentIndex(index);
    }
dt's avatar
dt committed
155
156
157

    if ((m_panelsTabWidget->currentIndex() == -1) && m_panelsTabWidget->count())
        m_panelsTabWidget->setCurrentIndex(0);
con's avatar
con committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
}

void ProjectWindow::saveStatus()
{
    m_session->setValue(QLatin1String("ProjectWindow/Panel"), m_panelsTabWidget->currentIndex());
}

void ProjectWindow::showProperties(ProjectExplorer::Project *project, const QModelIndex & /* subIndex */)
{
    if (debug)
        qDebug() << "ProjectWindow - showProperties called";

    // Remove the tabs from the tab widget first
    while (m_panelsTabWidget->count() > 0)
        m_panelsTabWidget->removeTab(0);

    while (m_panels.count()) {
        PropertiesPanel *panel = m_panels.at(0);
        m_panels.removeOne(panel);
        delete panel;
    }

    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();

                m_panels.append(panel);
                m_panelsTabWidget->addTab(panel->widget(), panel->name());
            }
        }
    }
}

dt's avatar
dt committed
196
void ProjectWindow::updateTreeWidgetStatupProjectChanged(ProjectExplorer::Project *startupProject)
con's avatar
con committed
197
{
dt's avatar
dt committed
198
199
200
    int count = m_treeWidget->topLevelItemCount();
    for (int i = 0; i < count; ++i) {
        QTreeWidgetItem *item = m_treeWidget->topLevelItem(i);
201
        if (Project *project = findProject(item->data(2, Qt::UserRole).toString())) {
dt's avatar
dt committed
202
203
204
205
206
            bool checked = (startupProject == project);
            if (item->checkState(1) != (checked ? Qt::Checked : Qt::Unchecked))
                item->setCheckState(1, checked ? Qt::Checked : Qt::Unchecked);
        } else {
            item->setCheckState(1, Qt::Unchecked);
207
208
        }
    }
dt's avatar
dt committed
209
}
con's avatar
con committed
210

dt's avatar
dt committed
211
212
213
214
void ProjectWindow::updateTreeWidgetProjectAdded(ProjectExplorer::Project *projectAdded)
{
    int position = m_session->projects().indexOf(projectAdded);
    const QFileInfo fileInfo(projectAdded->file()->fileName());
con's avatar
con committed
215

dt's avatar
dt committed
216
    QTreeWidgetItem *item = new QTreeWidgetItem();
217
    item->setText(0, projectAdded->name());
dt's avatar
dt committed
218
    item->setIcon(0, Core::FileIconProvider::instance()->icon(fileInfo));
219
220
    item->setData(2, Qt::UserRole, fileInfo.filePath());
    item->setText(2, QDir::toNativeSeparators(fileInfo.filePath()));
con's avatar
con committed
221

dt's avatar
dt committed
222
223
224
225
    if (projectAdded->isApplication()) {
        bool checked = (m_session->startupProject() == projectAdded);
        item->setCheckState(1, checked ? Qt::Checked : Qt::Unchecked);
    }
con's avatar
con committed
226

dt's avatar
dt committed
227
228
    m_treeWidget->insertTopLevelItem(position, item);
}
con's avatar
con committed
229

dt's avatar
dt committed
230
231
232
233
void ProjectWindow::updateTreeWidgetAboutToRemoveProject(ProjectExplorer::Project *projectRemoved) {
    int count = m_treeWidget->topLevelItemCount();
    for (int i = 0; i < count; ++i) {
        QTreeWidgetItem *item = m_treeWidget->topLevelItem(i);
234
        if (item->data(2, Qt::UserRole).toString() == QFileInfo(projectRemoved->file()->fileName()).filePath()) {
dt's avatar
dt committed
235
236
237
238
            if (m_treeWidget->currentItem() == item) {
                    m_treeWidget->setCurrentItem(0);
            }
        }
con's avatar
con committed
239
240
241
    }
}

dt's avatar
dt committed
242
243
244
245
246
void ProjectWindow::updateTreeWidgetProjectRemoved(ProjectExplorer::Project *projectRemoved)
{    
    int count = m_treeWidget->topLevelItemCount();
    for (int i = 0; i < count; ++i) {
        QTreeWidgetItem *item = m_treeWidget->topLevelItem(i);
247
        if (item->data(2, Qt::UserRole).toString() == QFileInfo(projectRemoved->file()->fileName()).filePath()) {
dt's avatar
dt committed
248
249
250
251
252
253
            QTreeWidgetItem *it = m_treeWidget->takeTopLevelItem(i);
            delete it;
            break;
        }
    }
}
con's avatar
con committed
254
255
256
257

Project *ProjectWindow::findProject(const QString &path) const
{
    QList<Project*> projects = m_session->projects();
hjk's avatar
hjk committed
258
    foreach (Project* project, projects)
dt's avatar
dt committed
259
        if (QFileInfo(project->file()->fileName()).filePath() == path)
con's avatar
con committed
260
261
            return project;
    return 0;
hjk's avatar
hjk committed
262
}
con's avatar
con committed
263
264
265
266


void ProjectWindow::handleCurrentItemChanged(QTreeWidgetItem *current)
{
dt's avatar
dt committed
267
268
269
    if (m_currentItemChanged)
        return;
    m_currentItemChanged = true;
con's avatar
con committed
270
    if (current) {
271
        QString path = current->data(2, Qt::UserRole).toString();
con's avatar
con committed
272
273
274
        if (Project *project = findProject(path)) {
            m_projectExplorer->setCurrentFile(project, path);
            showProperties(project, QModelIndex());
dt's avatar
dt committed
275
            m_currentItemChanged = false;
con's avatar
con committed
276
277
278
            return;
        }
    }
dt's avatar
dt committed
279
280
    showProperties(0, QModelIndex());
    m_currentItemChanged = false;
con's avatar
con committed
281
282
283
284
285
}


void ProjectWindow::handleItem(QTreeWidgetItem *item, int column)
{
dt's avatar
dt committed
286

con's avatar
con committed
287
288
289
    if (!item || column != 1) // startup project
        return;

290
    const QString path = item->data(2, Qt::UserRole).toString();
con's avatar
con committed
291
292
    Project *project = findProject(path);
    if (project && project->isApplication()) {
dt's avatar
dt committed
293
294
295
296
297
        if (!(item->checkState(1) == Qt::Checked)) { // is now unchecked
            if (m_session->startupProject() == project) {
                item->setCheckState(1, Qt::Checked); // uncheck not supported
            }
        } else { // is now checked
con's avatar
con committed
298
299
300
301
            m_session->setStartupProject(project);
        }
    }
}