projecttreewidget.cpp 15 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 "projecttreewidget.h"
hjk's avatar
hjk committed
34

con's avatar
con committed
35
#include "projectexplorer.h"
36
#include "projectnodes.h"
37 38
#include "project.h"
#include "session.h"
con's avatar
con committed
39 40 41
#include "projectexplorerconstants.h"
#include "projectmodels.h"

42
#include <coreplugin/coreconstants.h>
con's avatar
con committed
43 44
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
dt's avatar
dt committed
45 46
#include <coreplugin/icontext.h>

hjk's avatar
hjk committed
47
#include <utils/qtcassert.h>
48
#include <utils/navigationtreeview.h>
hjk's avatar
hjk committed
49 50

#include <QtCore/QDebug>
51
#include <QtCore/QSettings>
con's avatar
con committed
52 53

#include <QtGui/QHeaderView>
54
#include <QtGui/QTreeView>
con's avatar
con committed
55 56 57
#include <QtGui/QVBoxLayout>
#include <QtGui/QToolButton>
#include <QtGui/QFocusEvent>
58
#include <QtGui/QAction>
59
#include <QtGui/QPalette>
60
#include <QtGui/QMenu>
con's avatar
con committed
61 62 63 64 65 66 67 68

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;

namespace {
    bool debug = false;
}

69
class ProjectTreeView : public Utils::NavigationTreeView
con's avatar
con committed
70 71 72 73 74 75 76
{
public:
    ProjectTreeView()
    {
        setEditTriggers(QAbstractItemView::EditKeyPressed);
        setContextMenuPolicy(Qt::CustomContextMenu);
//        setExpandsOnDoubleClick(false);
77 78
        m_context = new Core::IContext(this);
        m_context->setContext(Core::Context(Constants::C_PROJECT_TREE));
79
        m_context->setWidget(this);
hjk's avatar
hjk committed
80
        Core::ICore::addContextObject(m_context);
dt's avatar
dt committed
81 82 83
    }
    ~ProjectTreeView()
    {
hjk's avatar
hjk committed
84
        Core::ICore::removeContextObject(m_context);
dt's avatar
dt committed
85
        delete m_context;
con's avatar
con committed
86
    }
dt's avatar
dt committed
87 88

private:
89
    Core::IContext *m_context;
con's avatar
con committed
90 91 92 93 94 95 96
};

/*!
  /class ProjectTreeWidget

  Shows the projects in form of a tree.
  */
hjk's avatar
hjk committed
97
ProjectTreeWidget::ProjectTreeWidget(QWidget *parent)
con's avatar
con committed
98 99 100 101 102
        : QWidget(parent),
          m_explorer(ProjectExplorerPlugin::instance()),
          m_view(0),
          m_model(0),
          m_filterProjectsAction(0),
103
          m_autoSync(false),
104
          m_autoExpand(true)
con's avatar
con committed
105 106
{
    m_model = new FlatModel(m_explorer->session()->sessionNode(), this);
107 108 109
    Project *pro = m_explorer->session()->startupProject();
    if (pro)
        m_model->setStartupProject(pro->rootProjectNode());
110 111 112 113 114 115 116
    NodesWatcher *watcher = new NodesWatcher(this);
    m_explorer->session()->sessionNode()->registerWatcher(watcher);

    connect(watcher, SIGNAL(foldersAboutToBeRemoved(FolderNode *, const QList<FolderNode*> &)),
            this, SLOT(foldersAboutToBeRemoved(FolderNode *, const QList<FolderNode*> &)));
    connect(watcher, SIGNAL(filesAboutToBeRemoved(FolderNode *, const QList<FileNode*> &)),
            this, SLOT(filesAboutToBeRemoved(FolderNode *, const QList<FileNode*> &)));
con's avatar
con committed
117 118 119 120 121 122 123 124 125 126 127

    m_view = new ProjectTreeView;
    m_view->setModel(m_model);
    setFocusProxy(m_view);
    initView();

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_view);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

Leena Miettinen's avatar
Leena Miettinen committed
128
    m_filterProjectsAction = new QAction(tr("Simplify Tree"), this);
con's avatar
con committed
129 130 131 132
    m_filterProjectsAction->setCheckable(true);
    m_filterProjectsAction->setChecked(false); // default is the traditional complex tree
    connect(m_filterProjectsAction, SIGNAL(toggled(bool)), this, SLOT(setProjectFilter(bool)));

Leena Miettinen's avatar
Leena Miettinen committed
133
    m_filterGeneratedFilesAction = new QAction(tr("Hide Generated Files"), this);
con's avatar
con committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    m_filterGeneratedFilesAction->setCheckable(true);
    m_filterGeneratedFilesAction->setChecked(true);
    connect(m_filterGeneratedFilesAction, SIGNAL(toggled(bool)), this, SLOT(setGeneratedFilesFilter(bool)));

    // connections
    connect(m_model, SIGNAL(modelReset()),
            this, SLOT(initView()));
    connect(m_view, SIGNAL(activated(const QModelIndex&)),
            this, SLOT(openItem(const QModelIndex&)));
    connect(m_view->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
            this, SLOT(handleCurrentItemChange(const QModelIndex&)));
    connect(m_view, SIGNAL(customContextMenuRequested(const QPoint&)),
            this, SLOT(showContextMenu(const QPoint&)));
    connect(m_explorer->session(), SIGNAL(singleProjectAdded(ProjectExplorer::Project *)),
            this, SLOT(handleProjectAdded(ProjectExplorer::Project *)));
    connect(m_explorer->session(), SIGNAL(startupProjectChanged(ProjectExplorer::Project *)),
            this, SLOT(startupProjectChanged(ProjectExplorer::Project *)));

152
    connect(m_explorer->session(), SIGNAL(aboutToLoadSession(QString)),
153 154 155 156 157 158
            this, SLOT(disableAutoExpand()));
    connect(m_explorer->session(), SIGNAL(sessionLoaded()),
            this, SLOT(loadExpandData()));
    connect(m_explorer->session(), SIGNAL(aboutToSaveSession()),
            this, SLOT(saveExpandData()));

159
    m_toggleSync = new QToolButton;
160
    m_toggleSync->setIcon(QIcon(QLatin1String(Core::Constants::ICON_LINK)));
161 162 163 164 165
    m_toggleSync->setCheckable(true);
    m_toggleSync->setChecked(autoSynchronization());
    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
    connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));

166
    setAutoSynchronization(true);
167 168
}

169 170 171 172 173 174 175 176
void ProjectTreeWidget::disableAutoExpand()
{
    m_autoExpand = false;
}

void ProjectTreeWidget::loadExpandData()
{
    m_autoExpand = true;
177
    QStringList data = m_explorer->session()->value(QLatin1String("ProjectTree.ExpandData")).toStringList();
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    recursiveLoadExpandData(m_view->rootIndex(), data.toSet());
}

void ProjectTreeWidget::recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data)
{
    if (data.contains(m_model->nodeForIndex(index)->path())) {
        m_view->expand(index);
        int count = m_model->rowCount(index);
        for (int i = 0; i < count; ++i)
            recursiveLoadExpandData(index.child(i, 0), data);
    }
}

void ProjectTreeWidget::saveExpandData()
{
    QStringList data;
    recursiveSaveExpandData(m_view->rootIndex(), &data);
    // TODO if there are multiple ProjectTreeWidgets, the last one saves the data
196
    m_explorer->session()->setValue(QLatin1String("ProjectTree.ExpandData"), data);
197 198 199 200 201 202 203 204 205 206 207 208 209
}

void ProjectTreeWidget::recursiveSaveExpandData(const QModelIndex &index, QStringList *data)
{
    Q_ASSERT(data);
    if (m_view->isExpanded(index)) {
        data->append(m_model->nodeForIndex(index)->path());
        int count = m_model->rowCount(index);
        for (int i = 0; i < count; ++i)
            recursiveSaveExpandData(index.child(i, 0), data);
    }
}

210 211 212 213 214 215
void ProjectTreeWidget::foldersAboutToBeRemoved(FolderNode *, const QList<FolderNode*> &list)
{
    Node *n = m_explorer->currentNode();
    while(n) {
        if (FolderNode *fn = qobject_cast<FolderNode *>(n)) {
            if (list.contains(fn)) {
216 217 218 219 220
                ProjectNode *pn = n->projectNode();
                // Make sure the node we are switching too isn't going to be removed also
                while (list.contains(pn))
                    pn = pn->parentFolderNode()->projectNode();
                m_explorer->setCurrentNode(pn);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                break;
            }
        }
        n = n->parentFolderNode();
    }
}

void ProjectTreeWidget::filesAboutToBeRemoved(FolderNode *, const QList<FileNode*> &list)
{
    if (FileNode *fileNode = qobject_cast<FileNode *>(m_explorer->currentNode())) {
        if (list.contains(fileNode)) {
            m_explorer->setCurrentNode(fileNode->projectNode());
        }
    }
}

237 238 239
QToolButton *ProjectTreeWidget::toggleSync()
{
    return m_toggleSync;
con's avatar
con committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

void ProjectTreeWidget::toggleAutoSynchronization()
{
    setAutoSynchronization(!m_autoSync);
}

bool ProjectTreeWidget::autoSynchronization() const
{
    return m_autoSync;
}

void ProjectTreeWidget::setAutoSynchronization(bool sync, bool syncNow)
{
254
    m_toggleSync->setChecked(sync);
con's avatar
con committed
255 256 257 258 259 260 261 262 263 264 265
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (debug)
        qDebug() << (m_autoSync ? "Enabling auto synchronization" : "Disabling auto synchronization");
    if (m_autoSync) {
        connect(m_explorer, SIGNAL(currentNodeChanged(ProjectExplorer::Node*, ProjectExplorer::Project*)),
                this, SLOT(setCurrentItem(ProjectExplorer::Node*, ProjectExplorer::Project*)));
        if (syncNow)
266
            setCurrentItem(m_explorer->currentNode(), ProjectExplorerPlugin::currentProject());
con's avatar
con committed
267 268 269 270 271 272
    } else {
        disconnect(m_explorer, SIGNAL(currentNodeChanged(ProjectExplorer::Node*, ProjectExplorer::Project*)),
                this, SLOT(setCurrentItem(ProjectExplorer::Node*, ProjectExplorer::Project*)));
    }
}

273 274 275 276 277
void ProjectTreeWidget::collapseAll()
{
    m_view->collapseAll();
}

con's avatar
con committed
278 279
void ProjectTreeWidget::editCurrentItem()
{
280 281
    if (m_view->selectionModel()->currentIndex().isValid())
        m_view->edit(m_view->selectionModel()->currentIndex());
con's avatar
con committed
282 283 284 285 286
}

void ProjectTreeWidget::setCurrentItem(Node *node, Project *project)
{
    if (debug)
287 288
        qDebug() << "ProjectTreeWidget::setCurrentItem(" << (project ? project->displayName() : QLatin1String("0"))
                 << ", " <<  (node ? node->path() : QLatin1String("0")) << ")";
289

con's avatar
con committed
290 291 292 293 294 295
    if (!project) {
        return;
    }

    const QModelIndex mainIndex = m_model->indexForNode(node);

296 297
    if (mainIndex.isValid() && mainIndex != m_view->selectionModel()->currentIndex()) {
        m_view->setCurrentIndex(mainIndex);
con's avatar
con committed
298
        m_view->scrollTo(mainIndex);
299 300 301 302
    } else {
        if (debug)
            qDebug() << "clear selection";
        m_view->clearSelection();
con's avatar
con committed
303
    }
304

con's avatar
con committed
305 306 307 308 309
}

void ProjectTreeWidget::handleCurrentItemChange(const QModelIndex &current)
{
    Node *node = m_model->nodeForIndex(current);
dt's avatar
dt committed
310
    // node might be 0. that's okay
con's avatar
con committed
311 312 313 314 315 316 317 318 319 320
    bool autoSync = autoSynchronization();
    setAutoSynchronization(false);
    m_explorer->setCurrentNode(node);
    setAutoSynchronization(autoSync, false);
}

void ProjectTreeWidget::showContextMenu(const QPoint &pos)
{
    QModelIndex index = m_view->indexAt(pos);
    Node *node = m_model->nodeForIndex(index);
321
    m_explorer->showContextMenu(this, m_view->mapToGlobal(pos), node);
con's avatar
con committed
322 323 324 325 326 327
}

void ProjectTreeWidget::handleProjectAdded(ProjectExplorer::Project *project)
{
    Node *node = project->rootProjectNode();
    QModelIndex idx = m_model->indexForNode(node);
328 329
    if (m_autoExpand) // disabled while session restoring
        m_view->setExpanded(idx, true);
330
    m_view->setCurrentIndex(idx);
con's avatar
con committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
}

void ProjectTreeWidget::startupProjectChanged(ProjectExplorer::Project *project)
{
    if (project) {
        ProjectNode *node = project->rootProjectNode();
        m_model->setStartupProject(node);
    } else {
        m_model->setStartupProject(0);
    }
}

void ProjectTreeWidget::initView()
{
    QModelIndex sessionIndex = m_model->index(0, 0);

    // hide root folder
    m_view->setRootIndex(sessionIndex);

    while (m_model->canFetchMore(sessionIndex))
        m_model->fetchMore(sessionIndex);

    // expand top level projects
hjk's avatar
hjk committed
354
    for (int i = 0; i < m_model->rowCount(sessionIndex); ++i)
con's avatar
con committed
355 356
        m_view->expand(m_model->index(i, 0, sessionIndex));

357
    setCurrentItem(m_explorer->currentNode(), ProjectExplorerPlugin::currentProject());
con's avatar
con committed
358 359 360 361 362 363
}

void ProjectTreeWidget::openItem(const QModelIndex &mainIndex)
{
    Node *node = m_model->nodeForIndex(mainIndex);
    if (node->nodeType() == FileNodeType) {
364
        Core::EditorManager *editorManager = Core::EditorManager::instance();
365
        editorManager->openEditor(node->path(), Core::Id(), Core::EditorManager::ModeSwitch);
con's avatar
con committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    }
}

void ProjectTreeWidget::setProjectFilter(bool filter)
{
    m_model->setProjectFilterEnabled(filter);
    m_filterProjectsAction->setChecked(filter);
}

void ProjectTreeWidget::setGeneratedFilesFilter(bool filter)
{
    m_model->setGeneratedFilesFilterEnabled(filter);
    m_filterGeneratedFilesAction->setChecked(filter);
}

381 382 383 384 385 386 387 388 389 390 391
bool ProjectTreeWidget::generatedFilesFilter()
{
    return m_model->generatedFilesFilterEnabled();
}

bool ProjectTreeWidget::projectFilter()
{
    return m_model->projectFilterEnabled();
}


hjk's avatar
hjk committed
392
ProjectTreeWidgetFactory::ProjectTreeWidgetFactory()
con's avatar
con committed
393 394 395 396 397 398 399
{
}

ProjectTreeWidgetFactory::~ProjectTreeWidgetFactory()
{
}

400
QString ProjectTreeWidgetFactory::displayName() const
con's avatar
con committed
401 402 403 404
{
    return tr("Projects");
}

405 406 407 408 409
int ProjectTreeWidgetFactory::priority() const
{
    return 100;
}

410
Core::Id ProjectTreeWidgetFactory::id() const
411
{
412
    return Core::Id("Projects");
413 414 415
}

QKeySequence ProjectTreeWidgetFactory::activationSequence() const
con's avatar
con committed
416 417 418 419 420 421 422
{
    return QKeySequence(Qt::ALT + Qt::Key_X);
}

Core::NavigationView ProjectTreeWidgetFactory::createWidget()
{
    Core::NavigationView n;
hjk's avatar
hjk committed
423
    ProjectTreeWidget *ptw = new ProjectTreeWidget;
con's avatar
con committed
424 425 426
    n.widget = ptw;

    QToolButton *filter = new QToolButton;
427
    filter->setIcon(QIcon(QLatin1String(Core::Constants::ICON_FILTER)));
Leena Miettinen's avatar
Leena Miettinen committed
428
    filter->setToolTip(tr("Filter Tree"));
con's avatar
con committed
429 430 431 432 433 434
    filter->setPopupMode(QToolButton::InstantPopup);
    QMenu *filterMenu = new QMenu(filter);
    filterMenu->addAction(ptw->m_filterProjectsAction);
    filterMenu->addAction(ptw->m_filterGeneratedFilesAction);
    filter->setMenu(filterMenu);

Roberto Raggi's avatar
Roberto Raggi committed
435
    n.dockToolBarWidgets << filter << ptw->toggleSync();
con's avatar
con committed
436 437 438
    return n;
}

439 440 441 442
void ProjectTreeWidgetFactory::saveSettings(int position, QWidget *widget)
{
    ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
    Q_ASSERT(ptw);
hjk's avatar
hjk committed
443
    QSettings *settings = Core::ICore::settings();
444 445 446 447
    const QString baseKey = QLatin1String("ProjectTreeWidget.") + QString::number(position);
    settings->setValue(baseKey + QLatin1String(".ProjectFilter"), ptw->projectFilter());
    settings->setValue(baseKey + QLatin1String(".GeneratedFilter"), ptw->generatedFilesFilter());
    settings->setValue(baseKey + QLatin1String(".SyncWithEditor"), ptw->autoSynchronization());
448 449 450 451 452 453
}

void ProjectTreeWidgetFactory::restoreSettings(int position, QWidget *widget)
{
    ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
    Q_ASSERT(ptw);
hjk's avatar
hjk committed
454
    QSettings *settings = Core::ICore::settings();
455 456 457 458
    const QString baseKey = QLatin1String("ProjectTreeWidget.") + QString::number(position);
    ptw->setProjectFilter(settings->value(baseKey + QLatin1String(".ProjectFilter"), false).toBool());
    ptw->setGeneratedFilesFilter(settings->value(baseKey + QLatin1String(".GeneratedFilter"), true).toBool());
    ptw->setAutoSynchronization(settings->value(baseKey +  QLatin1String(".SyncWithEditor"), true).toBool());
459
}