OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / gitsubmiteditor.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 "gitsubmiteditor.h"
35 #include "gitsubmiteditorwidget.h"
36 #include "gitconstants.h"
37 #include "commitdata.h"
38
39 #include <vcsbase/submitfilemodel.h>
40
41 #include <QtCore/QDebug>
42 #include <QtCore/QStringList>
43
44 namespace Git {
45 namespace Internal {
46
47 enum { FileTypeRole = Qt::UserRole + 1 };
48 enum FileType { StagedFile , UnstagedFile, UntrackedFile };
49
50 /* The problem with git is that no diff can be obtained to for a random
51  * multiselection of staged/unstaged files; it requires the --cached
52  * option for staged files. So, we sort apart the diff file lists
53  * according to a type flag we add to the model. */
54
55 GitSubmitEditor::GitSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
56     VCSBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
57     m_model(0)
58 {
59     connect(this, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotDiffSelected(QStringList)));
60 }
61
62 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
63 {
64     return static_cast<GitSubmitEditorWidget *>(widget());
65 }
66
67 // Utility to add a list of state/file pairs to the model
68 // setting a file type.
69 static void addStateFileListToModel(const QList<CommitData::StateFilePair> &l,
70                                     bool checked, FileType ft,
71                                     VCSBase::SubmitFileModel *model)
72 {
73     typedef QList<CommitData::StateFilePair>::const_iterator ConstIterator;
74     if (!l.empty()) {
75         const ConstIterator cend = l.constEnd();
76         const QVariant fileTypeData(ft);
77         for (ConstIterator it = l.constBegin(); it != cend; ++it)
78             model->addFile(it->second, it->first, checked).front()->setData(fileTypeData, FileTypeRole);
79     }
80 }
81
82 void GitSubmitEditor::setCommitData(const CommitData &d)
83 {
84     submitEditorWidget()->setPanelData(d.panelData);
85     submitEditorWidget()->setPanelInfo(d.panelInfo);
86
87     m_model = new VCSBase::SubmitFileModel(this);
88     addStateFileListToModel(d.stagedFiles,   true,  StagedFile,   m_model);
89     addStateFileListToModel(d.unstagedFiles, false, UnstagedFile, m_model);
90     if (!d.untrackedFiles.empty()) {
91         const QString untrackedSpec = QLatin1String("untracked");
92         const QVariant fileTypeData(UntrackedFile);
93         const QStringList::const_iterator cend = d.untrackedFiles.constEnd();
94         for (QStringList::const_iterator it = d.untrackedFiles.constBegin(); it != cend; ++it)
95             m_model->addFile(*it, untrackedSpec, false).front()->setData(fileTypeData, FileTypeRole);
96     }
97     setFileModel(m_model);
98 }
99
100 void GitSubmitEditor::slotDiffSelected(const QStringList &files)
101 {
102     // Sort it apart into staged/unstaged files
103     QStringList unstagedFiles;
104     QStringList stagedFiles;
105     const int fileColumn = fileNameColumn();
106     const int rowCount = m_model->rowCount();
107     for (int r = 0; r < rowCount; r++) {
108         const QString fileName = m_model->item(r, fileColumn)->text();
109         if (files.contains(fileName)) {
110             const FileType ft = static_cast<FileType>(m_model->item(r, 0)->data(FileTypeRole).toInt());
111             switch (ft) {
112             case StagedFile:
113                 stagedFiles.push_back(fileName);
114                 break;
115             case UnstagedFile:
116                 unstagedFiles.push_back(fileName);
117                 break;
118             case UntrackedFile:
119                 break;
120             }
121         }
122     }
123     if (!unstagedFiles.empty() || !stagedFiles.empty())
124         emit diff(unstagedFiles, stagedFiles);
125 }
126
127 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
128 {
129     return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
130 }
131
132 } // namespace Internal
133 } // namespace Git