OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / gitorious / gitoriousrepositorywizardpage.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "gitoriousrepositorywizardpage.h"
35 #include "gitoriousprojectwizardpage.h"
36 #include "gitoriousprojectwidget.h"
37 #include "gitorious.h"
38 #include "ui_gitoriousrepositorywizardpage.h"
39
40 #include <utils/qtcassert.h>
41
42 #include <QtCore/QDebug>
43
44 #include <QtGui/QStandardItemModel>
45 #include <QtGui/QStandardItem>
46 #include <QtGui/QItemSelectionModel>
47 #include <QtGui/QSortFilterProxyModel>
48
49 enum { TypeRole = Qt::UserRole + 1};
50 enum { HeaderType, RepositoryType };
51
52 enum { debug = 0 };
53
54 namespace Gitorious {
55 namespace Internal {
56
57 // A filter model that returns true for the parent (category) nodes
58 // (which by default do not match the search string and are thus collapsed).
59 class RepositoryFilterModel : public QSortFilterProxyModel {
60 public:
61     explicit RepositoryFilterModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
62 protected:
63     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;
64 };
65
66 bool RepositoryFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
67 {
68     if (!source_parent.isValid())
69         return true; // Always true for parents.
70     return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
71 }
72
73 // ----------- GitoriousRepositoryWizardPage
74 enum { RepositoryColumn, OwnerColumn, DescriptionColumn, ColumnCount };
75
76 GitoriousRepositoryWizardPage::GitoriousRepositoryWizardPage(const GitoriousProjectWizardPage *projectPage,
77                                                              QWidget *parent) :
78     QWizardPage(parent),
79     ui(new Ui::GitoriousRepositoryWizardPage),
80     m_projectPage(projectPage),
81     m_model(new QStandardItemModel(0, ColumnCount)),
82     m_filterModel(new RepositoryFilterModel),
83     m_valid(false)
84 {
85     QStringList headers;
86     headers << tr("Name") << tr("Owner") << tr("Description");
87     m_model->setHorizontalHeaderLabels(headers);
88     // Filter on all columns
89     m_filterModel->setSourceModel(m_model);
90     m_filterModel->setFilterKeyColumn(-1);
91     m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
92     m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
93
94     ui->setupUi(this);
95     // Filter
96     connect(ui->filterLineEdit, SIGNAL(filterChanged(QString)), m_filterModel, SLOT(setFilterFixedString(QString)));
97     // Tree view
98     ui->repositoryTreeView->setModel(m_filterModel);
99     ui->repositoryTreeView->setUniformRowHeights(true);
100     ui->repositoryTreeView->setAlternatingRowColors(true);
101     ui->repositoryTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
102     connect(ui->repositoryTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
103             this, SLOT(slotCurrentChanged(QModelIndex,QModelIndex)));
104
105     setTitle(tr("Repository"));
106 }
107
108 GitoriousRepositoryWizardPage::~GitoriousRepositoryWizardPage()
109 {
110     delete ui;
111 }
112
113 bool gitRepoLessThanByType(const GitoriousRepository &r1, const GitoriousRepository &r2)
114 {
115     return r1.type < r2.type;
116 }
117
118 static inline QList<QStandardItem *> headerEntry(const QString &h)
119 {
120     QStandardItem *nameItem = new QStandardItem(h);
121     nameItem->setFlags(Qt::ItemIsEnabled);
122     nameItem->setData(QVariant(HeaderType), TypeRole);
123     QStandardItem *ownerItem = new QStandardItem;
124     ownerItem->setFlags(Qt::ItemIsEnabled);
125     ownerItem->setData(QVariant(HeaderType), TypeRole);
126     QStandardItem *descriptionItem = new QStandardItem;
127     descriptionItem->setFlags(Qt::ItemIsEnabled);
128     descriptionItem->setData(QVariant(HeaderType), TypeRole);
129     QList<QStandardItem *> rc;
130     rc << nameItem << ownerItem << descriptionItem;
131     return rc;
132 }
133
134 static inline QList<QStandardItem *> repositoryEntry(const GitoriousRepository &r)
135 {
136     QStandardItem *nameItem = new QStandardItem(r.name);
137     nameItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
138     nameItem->setData(QVariant(RepositoryType), TypeRole);
139     QStandardItem *ownerItem = new QStandardItem(r.owner);
140     ownerItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
141     ownerItem->setData(QVariant(RepositoryType), TypeRole);
142     QStandardItem *descriptionItem = new QStandardItem;
143     descriptionItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
144     descriptionItem->setData(QVariant(RepositoryType), TypeRole);
145     QList<QStandardItem *> rc;
146     rc << nameItem << ownerItem << descriptionItem;
147     GitoriousProjectWidget::setDescription(r.description, DescriptionColumn, &rc);
148     return rc;
149 }
150
151 void GitoriousRepositoryWizardPage::initializePage()
152 {
153     // Populate the model
154     ui->repositoryTreeView->selectionModel()->clearSelection();
155     if (const int oldRowCount = m_model->rowCount())
156         m_model->removeRows(0, oldRowCount);
157     ui->filterLineEdit->clear();
158     // fill model
159     const QSharedPointer<GitoriousProject> proj = m_projectPage->project();
160     setSubTitle(tr("Choose a repository of the project '%1'.").arg(proj->name));
161     // Create a hierarchical list by repository type, sort by type
162     QList<GitoriousRepository> repositories = proj->repositories;
163     QStandardItem *firstEntry = 0;
164     if (!repositories.empty()) {
165         int lastRepoType = -1;
166         QStandardItem *header = 0;
167         qStableSort(repositories.begin(), repositories.end(), gitRepoLessThanByType);
168         const QString types[GitoriousRepository::PersonalRepository + 1] =
169             { tr("Mainline Repositories"), tr("Clones"), tr("Baseline Repositories"), tr("Shared Project Repositories"), tr("Personal Repositories") };
170         foreach(const GitoriousRepository &r, repositories) {
171             // New Header?
172             if (r.type != lastRepoType || !header) {
173                 lastRepoType = r.type;
174                 const QList<QStandardItem *> headerRow = headerEntry(types[r.type]);
175                 m_model->appendRow(headerRow);
176                 header = headerRow.front();
177             }
178             // Repository row
179             const QList<QStandardItem *> row = repositoryEntry(r);
180             header->appendRow(row);
181             if (!firstEntry)
182                 firstEntry = row.front();
183         }
184     }
185     ui->repositoryTreeView->expandAll();
186     for (int r = 0; r < ColumnCount; r++)
187         ui->repositoryTreeView->resizeColumnToContents(r);
188     // Select first
189     if (firstEntry) {
190         const QModelIndex filterIndex = m_filterModel->mapFromSource(m_model->indexFromItem(firstEntry));
191         ui->repositoryTreeView->selectionModel()->setCurrentIndex(filterIndex, QItemSelectionModel::Select|QItemSelectionModel::Current|QItemSelectionModel::Rows);
192     }
193     ui->repositoryTreeView->setFocus();
194 }
195
196 QStandardItem *GitoriousRepositoryWizardPage::currentItem0() const
197 {
198     return item0FromIndex(ui->repositoryTreeView->selectionModel()->currentIndex());
199 }
200
201 QStandardItem *GitoriousRepositoryWizardPage::item0FromIndex(const QModelIndex &filterIndex) const
202 {
203     if (filterIndex.isValid()) {
204         const QModelIndex sourceIndex = m_filterModel->mapToSource(filterIndex);
205         if (sourceIndex.column() == 0)
206             return m_model->itemFromIndex(sourceIndex);
207         const QModelIndex sibling0 = sourceIndex.sibling(sourceIndex.row(), 0);
208         return m_model->itemFromIndex(sibling0);
209     }
210     return 0;
211 }
212
213 void GitoriousRepositoryWizardPage::slotCurrentChanged(const QModelIndex &current, const QModelIndex & /*previous */)
214 {
215     const QStandardItem *item = item0FromIndex(current);
216     const bool isValid = item && item->data(TypeRole).toInt() == RepositoryType;
217     if (isValid != m_valid) {
218         m_valid = isValid;
219         emit completeChanged();
220     }
221 }
222
223 QString GitoriousRepositoryWizardPage::repositoryName() const
224 {
225     if (const QStandardItem *item = currentItem0())
226         if (item->data(TypeRole).toInt() == RepositoryType)
227             return item->text();
228     return QString();
229 }
230
231 QUrl GitoriousRepositoryWizardPage::repositoryURL() const
232 {
233     // Find by name (as we sorted the the repositories)
234     const QString repoName = repositoryName();
235     foreach (const GitoriousRepository &r, m_projectPage->project()->repositories)
236         if (r.name == repoName)
237             return r.cloneUrl;
238     return QUrl();
239 }
240
241 bool GitoriousRepositoryWizardPage::isComplete() const
242 {
243     return m_valid;
244 }
245
246 void GitoriousRepositoryWizardPage::changeEvent(QEvent *e)
247 {
248     QWizardPage::changeEvent(e);
249     switch (e->type()) {
250     case QEvent::LanguageChange:
251         ui->retranslateUi(this);
252         break;
253     default:
254         break;
255     }
256 }
257
258 } // namespace Internal
259 } // namespace Gitorious