OSDN Git Service

Update license.
[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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "gitsubmiteditor.h"
34 #include "gitsubmiteditorwidget.h"
35 #include "gitconstants.h"
36 #include "commitdata.h"
37
38 #include <vcsbase/submitfilemodel.h>
39
40 #include <QtCore/QDebug>
41 #include <QtCore/QStringList>
42
43 namespace Git {
44 namespace Internal {
45
46 enum { FileTypeRole = Qt::UserRole + 1 };
47 enum FileType { StagedFile , UnstagedFile, UntrackedFile };
48
49 /* The problem with git is that no diff can be obtained to for a random
50  * multiselection of staged/unstaged files; it requires the --cached
51  * option for staged files. So, we sort apart the diff file lists
52  * according to a type flag we add to the model. */
53
54 GitSubmitEditor::GitSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
55     VCSBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
56     m_model(0)
57 {
58     connect(this, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotDiffSelected(QStringList)));
59 }
60
61 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
62 {
63     return static_cast<GitSubmitEditorWidget *>(widget());
64 }
65
66 // Utility to add a list of state/file pairs to the model
67 // setting a file type.
68 static void addStateFileListToModel(const QList<CommitData::StateFilePair> &l,
69                                     bool checked, FileType ft,
70                                     VCSBase::SubmitFileModel *model)
71 {
72     typedef QList<CommitData::StateFilePair>::const_iterator ConstIterator;
73     if (!l.empty()) {
74         const ConstIterator cend = l.constEnd();
75         const QVariant fileTypeData(ft);
76         for (ConstIterator it = l.constBegin(); it != cend; ++it)
77             model->addFile(it->second, it->first, checked).front()->setData(fileTypeData, FileTypeRole);
78     }
79 }
80
81 void GitSubmitEditor::setCommitData(const CommitData &d)
82 {
83     submitEditorWidget()->setPanelData(d.panelData);
84     submitEditorWidget()->setPanelInfo(d.panelInfo);
85
86     m_model = new VCSBase::SubmitFileModel(this);
87     addStateFileListToModel(d.stagedFiles,   true,  StagedFile,   m_model);
88     addStateFileListToModel(d.unstagedFiles, false, UnstagedFile, m_model);
89     if (!d.untrackedFiles.empty()) {
90         const QString untrackedSpec = QLatin1String("untracked");
91         const QVariant fileTypeData(UntrackedFile);
92         const QStringList::const_iterator cend = d.untrackedFiles.constEnd();
93         for (QStringList::const_iterator it = d.untrackedFiles.constBegin(); it != cend; ++it)
94             m_model->addFile(*it, untrackedSpec, false).front()->setData(fileTypeData, FileTypeRole);
95     }
96     setFileModel(m_model);
97 }
98
99 void GitSubmitEditor::slotDiffSelected(const QStringList &files)
100 {
101     // Sort it apart into staged/unstaged files
102     QStringList unstagedFiles;
103     QStringList stagedFiles;
104     const int fileColumn = fileNameColumn();
105     const int rowCount = m_model->rowCount();
106     for (int r = 0; r < rowCount; r++) {
107         const QString fileName = m_model->item(r, fileColumn)->text();
108         if (files.contains(fileName)) {
109             const FileType ft = static_cast<FileType>(m_model->item(r, 0)->data(FileTypeRole).toInt());
110             switch (ft) {
111             case StagedFile:
112                 stagedFiles.push_back(fileName);
113                 break;
114             case UnstagedFile:
115                 unstagedFiles.push_back(fileName);
116                 break;
117             case UntrackedFile:
118                 break;
119             }
120         }
121     }
122     if (!unstagedFiles.empty() || !stagedFiles.empty())
123         emit diff(unstagedFiles, stagedFiles);
124 }
125
126 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
127 {
128     return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
129 }
130
131 } // namespace Internal
132 } // namespace Git