1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "gitsubmiteditor.h"
34 #include "gitsubmiteditorwidget.h"
35 #include "gitconstants.h"
36 #include "commitdata.h"
38 #include <vcsbase/submitfilemodel.h>
40 #include <QtCore/QDebug>
41 #include <QtCore/QStringList>
46 enum { FileTypeRole = Qt::UserRole + 1 };
47 enum FileType { StagedFile , UnstagedFile, UntrackedFile };
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. */
54 GitSubmitEditor::GitSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
55 VCSBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
58 connect(this, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotDiffSelected(QStringList)));
61 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
63 return static_cast<GitSubmitEditorWidget *>(widget());
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)
72 typedef QList<CommitData::StateFilePair>::const_iterator ConstIterator;
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);
81 void GitSubmitEditor::setCommitData(const CommitData &d)
83 submitEditorWidget()->setPanelData(d.panelData);
84 submitEditorWidget()->setPanelInfo(d.panelInfo);
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);
96 setFileModel(m_model);
99 void GitSubmitEditor::slotDiffSelected(const QStringList &files)
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());
112 stagedFiles.push_back(fileName);
115 unstagedFiles.push_back(fileName);
122 if (!unstagedFiles.empty() || !stagedFiles.empty())
123 emit diff(unstagedFiles, stagedFiles);
126 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
128 return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
131 } // namespace Internal