1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "gitsubmiteditor.h"
35 #include "gitsubmiteditorwidget.h"
36 #include "gitconstants.h"
37 #include "commitdata.h"
39 #include <vcsbase/submitfilemodel.h>
41 #include <QtCore/QDebug>
42 #include <QtCore/QStringList>
47 enum { FileTypeRole = Qt::UserRole + 1 };
48 enum FileType { StagedFile , UnstagedFile, UntrackedFile };
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. */
55 GitSubmitEditor::GitSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
56 VCSBaseSubmitEditor(parameters, new GitSubmitEditorWidget(parent)),
59 connect(this, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotDiffSelected(QStringList)));
62 GitSubmitEditorWidget *GitSubmitEditor::submitEditorWidget()
64 return static_cast<GitSubmitEditorWidget *>(widget());
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)
73 typedef QList<CommitData::StateFilePair>::const_iterator ConstIterator;
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);
82 void GitSubmitEditor::setCommitData(const CommitData &d)
84 submitEditorWidget()->setPanelData(d.panelData);
85 submitEditorWidget()->setPanelInfo(d.panelInfo);
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);
97 setFileModel(m_model);
100 void GitSubmitEditor::slotDiffSelected(const QStringList &files)
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());
113 stagedFiles.push_back(fileName);
116 unstagedFiles.push_back(fileName);
123 if (!unstagedFiles.empty() || !stagedFiles.empty())
124 emit diff(unstagedFiles, stagedFiles);
127 GitSubmitEditorPanelData GitSubmitEditor::panelData() const
129 return const_cast<GitSubmitEditor*>(this)->submitEditorWidget()->panelData();
132 } // namespace Internal