gitoriousrepositorywizardpage.cpp 9.61 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32 33 34 35

#include "gitoriousrepositorywizardpage.h"
#include "gitoriousprojectwizardpage.h"
#include "gitoriousprojectwidget.h"
#include "gitorious.h"
#include "ui_gitoriousrepositorywizardpage.h"

36
#include <QDebug>
37

38 39 40 41
#include <QStandardItemModel>
#include <QStandardItem>
#include <QItemSelectionModel>
#include <QSortFilterProxyModel>
42 43 44 45 46 47 48 49 50

enum { TypeRole = Qt::UserRole + 1};
enum { HeaderType, RepositoryType };

enum { debug = 0 };

namespace Gitorious {
namespace Internal {

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// A filter model that returns true for the parent (category) nodes
// (which by default do not match the search string and are thus collapsed).
class RepositoryFilterModel : public QSortFilterProxyModel {
public:
    explicit RepositoryFilterModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
protected:
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
};

bool RepositoryFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    if (!source_parent.isValid())
        return true; // Always true for parents.
    return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}

// ----------- GitoriousRepositoryWizardPage
68 69 70 71 72 73 74 75
enum { RepositoryColumn, OwnerColumn, DescriptionColumn, ColumnCount };

GitoriousRepositoryWizardPage::GitoriousRepositoryWizardPage(const GitoriousProjectWizardPage *projectPage,
                                                             QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::GitoriousRepositoryWizardPage),
    m_projectPage(projectPage),
    m_model(new QStandardItemModel(0, ColumnCount)),
76
    m_filterModel(new RepositoryFilterModel),
77 78 79 80
    m_valid(false)
{
    QStringList headers;
    headers << tr("Name") << tr("Owner") << tr("Description");
81
    m_model->setHorizontalHeaderLabels(headers);
82 83 84 85 86
    // Filter on all columns
    m_filterModel->setSourceModel(m_model);
    m_filterModel->setFilterKeyColumn(-1);
    m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
87 88

    ui->setupUi(this);
89
    // Filter
90
    connect(ui->filterLineEdit, SIGNAL(filterChanged(QString)), m_filterModel, SLOT(setFilterFixedString(QString)));
91 92
    // Tree view
    ui->repositoryTreeView->setModel(m_filterModel);
93 94 95 96 97
    ui->repositoryTreeView->setUniformRowHeights(true);
    ui->repositoryTreeView->setAlternatingRowColors(true);
    ui->repositoryTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
    connect(ui->repositoryTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(slotCurrentChanged(QModelIndex,QModelIndex)));
98 99

    setTitle(tr("Repository"));
100 101 102 103 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
}

GitoriousRepositoryWizardPage::~GitoriousRepositoryWizardPage()
{
    delete ui;
}

bool gitRepoLessThanByType(const GitoriousRepository &r1, const GitoriousRepository &r2)
{
    return r1.type < r2.type;
}

static inline QList<QStandardItem *> headerEntry(const QString &h)
{
    QStandardItem *nameItem = new QStandardItem(h);
    nameItem->setFlags(Qt::ItemIsEnabled);
    nameItem->setData(QVariant(HeaderType), TypeRole);
    QStandardItem *ownerItem = new QStandardItem;
    ownerItem->setFlags(Qt::ItemIsEnabled);
    ownerItem->setData(QVariant(HeaderType), TypeRole);
    QStandardItem *descriptionItem = new QStandardItem;
    descriptionItem->setFlags(Qt::ItemIsEnabled);
    descriptionItem->setData(QVariant(HeaderType), TypeRole);
    QList<QStandardItem *> rc;
    rc << nameItem << ownerItem << descriptionItem;
    return rc;
}

static inline QList<QStandardItem *> repositoryEntry(const GitoriousRepository &r)
{
    QStandardItem *nameItem = new QStandardItem(r.name);
    nameItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    nameItem->setData(QVariant(RepositoryType), TypeRole);
    QStandardItem *ownerItem = new QStandardItem(r.owner);
    ownerItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    ownerItem->setData(QVariant(RepositoryType), TypeRole);
    QStandardItem *descriptionItem = new QStandardItem;
    descriptionItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    descriptionItem->setData(QVariant(RepositoryType), TypeRole);
    QList<QStandardItem *> rc;
    rc << nameItem << ownerItem << descriptionItem;
    GitoriousProjectWidget::setDescription(r.description, DescriptionColumn, &rc);
    return rc;
}

void GitoriousRepositoryWizardPage::initializePage()
{
    // Populate the model
    ui->repositoryTreeView->selectionModel()->clearSelection();
    if (const int oldRowCount = m_model->rowCount())
        m_model->removeRows(0, oldRowCount);
151
    ui->filterLineEdit->clear();
152 153 154 155 156 157 158 159 160 161 162 163
    // fill model
    const QSharedPointer<GitoriousProject> proj = m_projectPage->project();
    setSubTitle(tr("Choose a repository of the project '%1'.").arg(proj->name));
    // Create a hierarchical list by repository type, sort by type
    QList<GitoriousRepository> repositories = proj->repositories;
    QStandardItem *firstEntry = 0;
    if (!repositories.empty()) {
        int lastRepoType = -1;
        QStandardItem *header = 0;
        qStableSort(repositories.begin(), repositories.end(), gitRepoLessThanByType);
        const QString types[GitoriousRepository::PersonalRepository + 1] =
            { tr("Mainline Repositories"), tr("Clones"), tr("Baseline Repositories"), tr("Shared Project Repositories"), tr("Personal Repositories") };
164
        foreach (const GitoriousRepository &r, repositories) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            // New Header?
            if (r.type != lastRepoType || !header) {
                lastRepoType = r.type;
                const QList<QStandardItem *> headerRow = headerEntry(types[r.type]);
                m_model->appendRow(headerRow);
                header = headerRow.front();
            }
            // Repository row
            const QList<QStandardItem *> row = repositoryEntry(r);
            header->appendRow(row);
            if (!firstEntry)
                firstEntry = row.front();
        }
    }
    ui->repositoryTreeView->expandAll();
    for (int r = 0; r < ColumnCount; r++)
        ui->repositoryTreeView->resizeColumnToContents(r);
    // Select first
    if (firstEntry) {
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        const QModelIndex filterIndex = m_filterModel->mapFromSource(m_model->indexFromItem(firstEntry));
        ui->repositoryTreeView->selectionModel()->setCurrentIndex(filterIndex, QItemSelectionModel::Select|QItemSelectionModel::Current|QItemSelectionModel::Rows);
    }
    ui->repositoryTreeView->setFocus();
}

QStandardItem *GitoriousRepositoryWizardPage::currentItem0() const
{
    return item0FromIndex(ui->repositoryTreeView->selectionModel()->currentIndex());
}

QStandardItem *GitoriousRepositoryWizardPage::item0FromIndex(const QModelIndex &filterIndex) const
{
    if (filterIndex.isValid()) {
        const QModelIndex sourceIndex = m_filterModel->mapToSource(filterIndex);
        if (sourceIndex.column() == 0)
            return m_model->itemFromIndex(sourceIndex);
        const QModelIndex sibling0 = sourceIndex.sibling(sourceIndex.row(), 0);
        return m_model->itemFromIndex(sibling0);
203
    }
204
    return 0;
205 206 207 208
}

void GitoriousRepositoryWizardPage::slotCurrentChanged(const QModelIndex &current, const QModelIndex & /*previous */)
{
209
    const QStandardItem *item = item0FromIndex(current);
210 211 212 213 214 215 216 217 218
    const bool isValid = item && item->data(TypeRole).toInt() == RepositoryType;
    if (isValid != m_valid) {
        m_valid = isValid;
        emit completeChanged();
    }
}

QString GitoriousRepositoryWizardPage::repositoryName() const
{
219 220 221
    if (const QStandardItem *item = currentItem0())
        if (item->data(TypeRole).toInt() == RepositoryType)
            return item->text();
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    return QString();
}

QUrl GitoriousRepositoryWizardPage::repositoryURL() const
{
    // Find by name (as we sorted the the repositories)
    const QString repoName = repositoryName();
    foreach (const GitoriousRepository &r, m_projectPage->project()->repositories)
        if (r.name == repoName)
            return r.cloneUrl;
    return QUrl();
}

bool GitoriousRepositoryWizardPage::isComplete() const
{
    return m_valid;
}

void GitoriousRepositoryWizardPage::changeEvent(QEvent *e)
{
    QWizardPage::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

} // namespace Internal
} // namespace Gitorious