Commit 62f19fee authored by hjk's avatar hjk Committed by hjk
Browse files

ProjectExplorer: Use Utils::TreeModel for AddNewModel



Less code.

Change-Id: If02cf7685a1b37be4887ebe21a9c8b55079308f2
Reviewed-by: default avatarDaniel Teske <daniel.teske@theqtcompany.com>
parent 384641d7
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "addnewmodel.h"
#include "projectexplorer.h"
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
AddNewTree::AddNewTree(const QString &displayName)
: m_parent(0),
m_children(QList<AddNewTree *>()),
m_displayName(displayName),
m_node(0),
m_canAdd(true),
m_priority(-1)
{
}
AddNewTree::AddNewTree(FolderNode *node, QList<AddNewTree *> children, const QString &displayName)
: m_parent(0),
m_children(children),
m_displayName(displayName),
m_node(0),
m_canAdd(false),
m_priority(-1)
{
if (node)
m_toolTip = ProjectExplorerPlugin::directoryFor(node);
foreach (AddNewTree *child, m_children)
child->m_parent = this;
}
AddNewTree::AddNewTree(FolderNode *node, QList<AddNewTree *> children, const FolderNode::AddNewInformation &info)
: m_parent(0),
m_children(children),
m_displayName(info.displayName),
m_node(node),
m_canAdd(true),
m_priority(info.priority)
{
if (node)
m_toolTip = ProjectExplorerPlugin::directoryFor(node);
foreach (AddNewTree *child, m_children)
child->m_parent = this;
}
AddNewTree::~AddNewTree()
{
qDeleteAll(m_children);
}
AddNewTree *AddNewTree::parent() const
{
return m_parent;
}
QList<AddNewTree *> AddNewTree::children() const
{
return m_children;
}
bool AddNewTree::canAdd() const
{
return m_canAdd;
}
QString AddNewTree::displayName() const
{
return m_displayName;
}
QString AddNewTree::toolTip() const
{
return m_toolTip;
}
FolderNode *AddNewTree::node() const
{
return m_node;
}
int AddNewTree::priority() const
{
return m_priority;
}
AddNewModel::AddNewModel(AddNewTree *root)
: m_root(root)
{
}
AddNewModel::~AddNewModel()
{
delete m_root;
}
int AddNewModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return m_root->children().size();
AddNewTree *tree = static_cast<AddNewTree *>(parent.internalPointer());
return tree->children().size();
}
int AddNewModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 1;
}
QModelIndex AddNewModel::index(int row, int column, const QModelIndex &parent) const
{
if (column != 0)
return QModelIndex();
if (!parent.isValid()) {
if (row >= 0 && row < m_root->children().size())
return createIndex(row, column, m_root->children().at(row));
return QModelIndex();
}
AddNewTree *tree = static_cast<AddNewTree *>(parent.internalPointer());
if (row >= 0 && row < tree->children().size())
return createIndex(row, column, tree->children().at(row));
return QModelIndex();
}
QModelIndex AddNewModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
AddNewTree *childTree = static_cast<AddNewTree *>(child.internalPointer());
if (childTree == m_root)
return QModelIndex();
AddNewTree *parent = childTree->parent();
if (parent == m_root)
return QModelIndex();
AddNewTree *grandparent = parent->parent();
for (int i = 0; i < grandparent->children().size(); ++i) {
if (grandparent->children().at(i) == parent)
return createIndex(i, 0, parent);
}
return QModelIndex();
}
QVariant AddNewModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
AddNewTree *tree = static_cast<AddNewTree *>(index.internalPointer());
if (role == Qt::DisplayRole)
return tree->displayName();
else if (role == Qt::ToolTipRole)
return tree->toolTip();
return QVariant();
}
Qt::ItemFlags AddNewModel::flags(const QModelIndex &index) const
{
AddNewTree *tree = static_cast<AddNewTree *>(index.internalPointer());
if (tree && tree->canAdd())
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
return Qt::NoItemFlags;
}
FolderNode *AddNewModel::nodeForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return m_root->node();
AddNewTree *tree = static_cast<AddNewTree *>(index.internalPointer());
return tree->node();
}
QModelIndex AddNewModel::indexForTree(AddNewTree *tree) const
{
if (!tree)
return index(0, 0, QModelIndex());
AddNewTree *parent = tree->parent();
if (!parent)
return QModelIndex();
for (int i = 0; i < parent->children().size(); ++i)
if (parent->children().at(i) == tree)
return createIndex(i, 0, tree);
return QModelIndex();
}
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef ADDNEWMODEL_H
#define ADDNEWMODEL_H
#include "projectnodes.h"
#include <QAbstractItemModel>
namespace ProjectExplorer {
class FolderNode;
namespace Internal {
class AddNewTree
{
public:
AddNewTree(const QString &displayName);
AddNewTree(FolderNode *node, QList<AddNewTree *> children, const QString &displayName);
AddNewTree(FolderNode *node, QList<AddNewTree *> children, const FolderNode::AddNewInformation &info);
~AddNewTree();
AddNewTree *parent() const;
QList<AddNewTree *> children() const;
bool canAdd() const;
QString displayName() const;
QString toolTip() const;
FolderNode *node() const;
int priority() const;
private:
AddNewTree *m_parent;
QList<AddNewTree *> m_children;
QString m_displayName;
QString m_toolTip;
FolderNode *m_node;
bool m_canAdd;
int m_priority;
};
class AddNewModel : public QAbstractItemModel
{
public:
AddNewModel(AddNewTree *root);
~AddNewModel();
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
FolderNode *nodeForIndex(const QModelIndex &index) const;
QModelIndex indexForTree(AddNewTree *tree) const;
private:
AddNewTree *m_root;
};
}
}
#endif // ADDNEWMODEL_H
......@@ -31,7 +31,6 @@
#include "jsonsummarypage.h"
#include "jsonwizard.h"
#include "../addnewmodel.h"
#include "../project.h"
#include "../projectexplorerconstants.h"
#include "../projectnodes.h"
......
......@@ -147,7 +147,6 @@ HEADERS += projectexplorer.h \
customparserconfigdialog.h \
ipotentialkit.h \
selectablefilesmodel.h \
addnewmodel.h \
xcodebuildparser.h \
propertiespanel.h \
panelswidget.h \
......@@ -286,7 +285,6 @@ SOURCES += projectexplorer.cpp \
customparserconfigdialog.cpp \
ipotentialkit.cpp \
selectablefilesmodel.cpp \
addnewmodel.cpp \
xcodebuildparser.cpp \
propertiespanel.cpp \
panelswidget.cpp \
......
......@@ -24,7 +24,6 @@ QtcPlugin {
"abi.cpp", "abi.h",
"abiwidget.cpp", "abiwidget.h",
"abstractprocessstep.cpp", "abstractprocessstep.h",
"addnewmodel.cpp", "addnewmodel.h",
"allprojectsfilter.cpp", "allprojectsfilter.h",
"allprojectsfind.cpp", "allprojectsfind.h",
"ansifilterparser.cpp", "ansifilterparser.h",
......
......@@ -31,7 +31,6 @@
#include "projectwizardpage.h"
#include "ui_projectwizardpage.h"
#include "addnewmodel.h"
#include "projectexplorer.h"
#include "session.h"
......@@ -44,6 +43,7 @@
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/treemodel.h>
#include <utils/wizard.h>
#include <vcsbase/vcsbaseconstants.h>
......@@ -61,10 +61,84 @@
*/
using namespace Core;
using namespace Utils;
namespace ProjectExplorer {
namespace Internal {
class AddNewTree : public Utils::TreeItem
{
public:
AddNewTree(const QString &displayName);
AddNewTree(FolderNode *node, QList<AddNewTree *> children, const QString &displayName);
AddNewTree(FolderNode *node, QList<AddNewTree *> children, const FolderNode::AddNewInformation &info);
QVariant data(int column, int role) const;
Qt::ItemFlags flags(int column) const;
QString displayName() const { return m_displayName; }
FolderNode *node() const { return m_node; }
int priority() const { return m_priority; }
private:
QString m_displayName;
QString m_toolTip;
FolderNode *m_node;
bool m_canAdd;
int m_priority;
};
AddNewTree::AddNewTree(const QString &displayName)
: m_displayName(displayName),
m_node(0),
m_canAdd(true),
m_priority(-1)
{
}
// FIXME: potentially merge the following two functions.
// Note the different handling of 'node' and m_canAdd.
AddNewTree::AddNewTree(FolderNode *node, QList<AddNewTree *> children, const QString &displayName)
: m_displayName(displayName),
m_node(0),
m_canAdd(false),
m_priority(-1)
{
if (node)
m_toolTip = ProjectExplorerPlugin::directoryFor(node);
foreach (AddNewTree *child, children)
appendChild(child);
}
AddNewTree::AddNewTree(FolderNode *node, QList<AddNewTree *> children, const FolderNode::AddNewInformation &info)
: m_displayName(info.displayName),
m_node(node),
m_canAdd(true),
m_priority(info.priority)
{
if (node)
m_toolTip = ProjectExplorerPlugin::directoryFor(node);
foreach (AddNewTree *child, children)
appendChild(child);
}
QVariant AddNewTree::data(int, int role) const
{
if (role == Qt::DisplayRole)
return m_displayName;
if (role == Qt::ToolTipRole)
return m_toolTip;
return QVariant();
}
Qt::ItemFlags AddNewTree::flags(int) const
{
if (m_canAdd)
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
return Qt::NoItemFlags;
}
// --------------------------------------------------------------------
// BestNodeSelector:
// --------------------------------------------------------------------
......@@ -268,7 +342,7 @@ ProjectWizardPage::~ProjectWizardPage()
delete m_model;
}
void ProjectWizardPage::setModel(AddNewModel *model)
void ProjectWizardPage::setModel(TreeModel *model)
{
delete m_model;
m_model = model;
......@@ -310,7 +384,7 @@ bool ProjectWizardPage::expandTree(const QModelIndex &root)
void ProjectWizardPage::setBestNode(AddNewTree *tree)
{
QModelIndex index = m_model->indexForTree(tree);
QModelIndex index = m_model->indexFromItem(tree);
m_ui->projectComboBox->setCurrentIndex(index);
while (index.isValid()) {
......@@ -322,7 +396,8 @@ void ProjectWizardPage::setBestNode(AddNewTree *tree)
FolderNode *ProjectWizardPage::currentNode() const
{
QModelIndex index = m_ui->projectComboBox->view()->currentIndex();
return m_model->nodeForIndex(index);
TreeItem *item = m_model->itemFromIndex(index);
return item ? static_cast<AddNewTree *>(item)->node() : 0;
}
void ProjectWizardPage::setAddingSubProject(bool addingSubProject)
......@@ -416,7 +491,8 @@ void ProjectWizardPage::initializeProjectTree(Node *context, const QStringList &
setAdditionalInfo(selector.deployingProjects());
AddNewModel *model = new AddNewModel(tree);
TreeModel *model = new TreeModel;
model->setRootItem(tree);
setModel(model);
setBestNode(selector.bestChoice());
setAddingSubProject(action == AddSubProject);
......
......@@ -44,10 +44,11 @@ class QModelIndex;
QT_END_NAMESPACE
namespace Core { class IVersionControl; }
namespace Utils { class TreeModel; }
namespace ProjectExplorer {
namespace Internal {
class AddNewModel;
class AddNewTree;
namespace Ui { class WizardPage; }
......@@ -92,7 +93,7 @@ private slots:
private:
void setAdditionalInfo(const QString &text);
void setAddingSubProject(bool addingSubProject);
void setModel(AddNewModel *model);
void setModel(Utils::TreeModel *model);
void setBestNode(ProjectExplorer::Internal::AddNewTree *tree);
void setVersionControls(const QStringList &);
void setProjectToolTip(const QString &);
......@@ -100,7 +101,7 @@ private:
Ui::WizardPage *m_ui;
QStringList m_projectToolTips;
AddNewModel *m_model;
Utils::TreeModel *m_model;
QList<Core::IVersionControl*> m_activeVersionControls;
QString m_commonDirectory;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment