2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-01-12 17:44:04 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2009-01-12 17:44:04 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-01-12 17:44:04 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2009-01-12 17:44:04 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-01-12 17:44:04 +01:00
|
|
|
|
|
|
|
|
#include "submitfilemodel.h"
|
|
|
|
|
|
2011-11-10 16:51:50 +01:00
|
|
|
#include <coreplugin/fileiconprovider.h>
|
2013-01-07 22:15:03 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2015-03-11 17:52:26 +01:00
|
|
|
#include <utils/theme/theme.h>
|
2011-11-10 16:51:50 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QStandardItem>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDebug>
|
2009-01-12 17:44:04 +01:00
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
namespace VcsBase {
|
2009-01-12 17:44:04 +01:00
|
|
|
|
2011-10-19 15:49:13 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
// Helpers:
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
2015-03-11 17:52:26 +01:00
|
|
|
enum { stateColumn = 0, fileColumn = 1 };
|
|
|
|
|
|
|
|
|
|
static QBrush fileStatusTextForeground(SubmitFileModel::FileStatusHint statusHint)
|
|
|
|
|
{
|
|
|
|
|
using Utils::Theme;
|
|
|
|
|
Theme::Color statusTextColor = Theme::VcsBase_FileStatusUnknown_TextColor;
|
|
|
|
|
switch (statusHint) {
|
|
|
|
|
case SubmitFileModel::FileStatusUnknown:
|
|
|
|
|
statusTextColor = Theme::VcsBase_FileStatusUnknown_TextColor;
|
|
|
|
|
break;
|
|
|
|
|
case SubmitFileModel::FileAdded:
|
|
|
|
|
statusTextColor = Theme::VcsBase_FileAdded_TextColor;
|
|
|
|
|
break;
|
|
|
|
|
case SubmitFileModel::FileModified:
|
|
|
|
|
statusTextColor = Theme::VcsBase_FileModified_TextColor;
|
|
|
|
|
break;
|
|
|
|
|
case SubmitFileModel::FileDeleted:
|
|
|
|
|
statusTextColor = Theme::VcsBase_FileDeleted_TextColor;
|
|
|
|
|
break;
|
|
|
|
|
case SubmitFileModel::FileRenamed:
|
|
|
|
|
statusTextColor = Theme::VcsBase_FileRenamed_TextColor;
|
|
|
|
|
break;
|
2017-05-17 20:59:48 +02:00
|
|
|
case SubmitFileModel::FileUnmerged:
|
2016-05-19 19:01:25 +02:00
|
|
|
statusTextColor = Theme::VcsBase_FileUnmerged_TextColor;
|
|
|
|
|
break;
|
2015-03-11 17:52:26 +01:00
|
|
|
}
|
|
|
|
|
return QBrush(Utils::creatorTheme()->color(statusTextColor));
|
|
|
|
|
}
|
2013-01-02 22:19:04 +02:00
|
|
|
|
2015-03-09 17:10:25 +01:00
|
|
|
static QList<QStandardItem *> createFileRow(const QString &repositoryRoot,
|
|
|
|
|
const QString &fileName,
|
|
|
|
|
const QString &status,
|
2015-03-11 17:52:26 +01:00
|
|
|
SubmitFileModel::FileStatusHint statusHint,
|
2015-03-09 17:10:25 +01:00
|
|
|
CheckMode checked,
|
|
|
|
|
const QVariant &v)
|
2011-10-19 15:49:13 +00:00
|
|
|
{
|
2015-01-22 13:54:05 +01:00
|
|
|
auto statusItem = new QStandardItem(status);
|
2013-06-14 17:51:53 +03:00
|
|
|
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
|
if (checked != Uncheckable) {
|
|
|
|
|
flags |= Qt::ItemIsUserCheckable;
|
2012-12-25 19:38:07 +02:00
|
|
|
statusItem->setCheckState(checked == Checked ? Qt::Checked : Qt::Unchecked);
|
2013-06-14 17:51:53 +03:00
|
|
|
}
|
|
|
|
|
statusItem->setFlags(flags);
|
2011-10-19 15:49:13 +00:00
|
|
|
statusItem->setData(v);
|
2015-01-22 13:54:05 +01:00
|
|
|
auto fileItem = new QStandardItem(fileName);
|
2011-10-19 15:49:13 +00:00
|
|
|
fileItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
|
2015-03-09 17:10:25 +01:00
|
|
|
// For some reason, Windows (at least) requires a valid (existing) file path to the icon, so
|
|
|
|
|
// the repository root is needed here.
|
|
|
|
|
// Note: for "overlaid" icons in Core::FileIconProvider a valid file path is not required
|
|
|
|
|
const QFileInfo fi(repositoryRoot + QLatin1Char('/') + fileName);
|
|
|
|
|
fileItem->setIcon(Core::FileIconProvider::icon(fi));
|
2017-05-17 21:10:14 +02:00
|
|
|
const QList<QStandardItem *> row{statusItem, fileItem};
|
2015-03-11 17:52:26 +01:00
|
|
|
if (statusHint != SubmitFileModel::FileStatusUnknown) {
|
|
|
|
|
const QBrush textForeground = fileStatusTextForeground(statusHint);
|
2017-05-18 21:20:08 +02:00
|
|
|
for (QStandardItem *item : row)
|
2015-03-11 17:52:26 +01:00
|
|
|
item->setForeground(textForeground);
|
|
|
|
|
}
|
2011-10-19 15:49:13 +00:00
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
// SubmitFileModel:
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
2011-03-28 14:19:17 +02:00
|
|
|
/*!
|
2012-01-07 12:31:48 +01:00
|
|
|
\class VcsBase::SubmitFileModel
|
2011-03-28 14:19:17 +02:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The SubmitFileModel class is a 2-column (checkable, state, file name)
|
|
|
|
|
model to be used to list the files in the submit editor.
|
|
|
|
|
|
|
|
|
|
Provides header items and a convenience function to add files.
|
2011-03-28 14:19:17 +02:00
|
|
|
*/
|
|
|
|
|
|
2009-01-12 17:44:04 +01:00
|
|
|
SubmitFileModel::SubmitFileModel(QObject *parent) :
|
|
|
|
|
QStandardItemModel(0, 2, parent)
|
|
|
|
|
{
|
|
|
|
|
// setColumnCount(2);
|
2017-05-17 21:10:14 +02:00
|
|
|
setHorizontalHeaderLabels({tr("State"), tr("File")});
|
2009-01-12 17:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-09 17:10:25 +01:00
|
|
|
const QString &SubmitFileModel::repositoryRoot() const
|
|
|
|
|
{
|
|
|
|
|
return m_repositoryRoot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SubmitFileModel::setRepositoryRoot(const QString &repoRoot)
|
|
|
|
|
{
|
|
|
|
|
m_repositoryRoot = repoRoot;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 19:38:07 +02:00
|
|
|
QList<QStandardItem *> SubmitFileModel::addFile(const QString &fileName, const QString &status, CheckMode checkMode,
|
2011-10-19 15:49:13 +00:00
|
|
|
const QVariant &v)
|
2009-01-13 10:06:06 +01:00
|
|
|
{
|
2015-03-11 17:52:26 +01:00
|
|
|
const FileStatusHint statusHint =
|
|
|
|
|
m_fileStatusQualifier ? m_fileStatusQualifier(status, v) : FileStatusUnknown;
|
|
|
|
|
const QList<QStandardItem *> row =
|
|
|
|
|
createFileRow(m_repositoryRoot, fileName, status, statusHint, checkMode, v);
|
2009-01-12 17:44:04 +01:00
|
|
|
appendRow(row);
|
|
|
|
|
return row;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-19 14:58:12 +00:00
|
|
|
QString SubmitFileModel::state(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= rowCount())
|
|
|
|
|
return QString();
|
|
|
|
|
return item(row)->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SubmitFileModel::file(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= rowCount())
|
|
|
|
|
return QString();
|
2013-01-02 22:19:04 +02:00
|
|
|
return item(row, fileColumn)->text();
|
2011-10-19 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-14 17:51:53 +03:00
|
|
|
bool SubmitFileModel::isCheckable(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= rowCount())
|
|
|
|
|
return false;
|
|
|
|
|
return item(row)->isCheckable();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-19 14:58:12 +00:00
|
|
|
bool SubmitFileModel::checked(int row) const
|
|
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= rowCount())
|
|
|
|
|
return false;
|
|
|
|
|
return (item(row)->checkState() == Qt::Checked);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 18:27:38 +02:00
|
|
|
void SubmitFileModel::setChecked(int row, bool check)
|
|
|
|
|
{
|
|
|
|
|
if (row >= 0 || row < rowCount())
|
|
|
|
|
item(row)->setCheckState(check ? Qt::Checked : Qt::Unchecked);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-02 22:19:04 +02:00
|
|
|
void SubmitFileModel::setAllChecked(bool check)
|
|
|
|
|
{
|
|
|
|
|
int rows = rowCount();
|
2015-02-04 22:55:49 +02:00
|
|
|
for (int row = 0; row < rows; ++row) {
|
|
|
|
|
QStandardItem *i = item(row);
|
|
|
|
|
if (i->isCheckable())
|
|
|
|
|
i->setCheckState(check ? Qt::Checked : Qt::Unchecked);
|
|
|
|
|
}
|
2013-01-02 22:19:04 +02:00
|
|
|
}
|
|
|
|
|
|
2012-01-19 15:33:20 +01:00
|
|
|
QVariant SubmitFileModel::extraData(int row) const
|
2011-10-19 15:49:13 +00:00
|
|
|
{
|
|
|
|
|
if (row < 0 || row >= rowCount())
|
|
|
|
|
return false;
|
|
|
|
|
return item(row)->data();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-19 14:58:12 +00:00
|
|
|
bool SubmitFileModel::hasCheckedFiles() const
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < rowCount(); ++i) {
|
|
|
|
|
if (checked(i))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-02 22:19:04 +02:00
|
|
|
unsigned int SubmitFileModel::filterFiles(const QStringList &filter)
|
2009-01-12 17:44:04 +01:00
|
|
|
{
|
2013-01-02 22:19:04 +02:00
|
|
|
unsigned int rc = 0;
|
2009-01-12 17:44:04 +01:00
|
|
|
for (int r = rowCount() - 1; r >= 0; r--)
|
2013-01-02 22:19:04 +02:00
|
|
|
if (!filter.contains(file(r))) {
|
|
|
|
|
removeRow(r);
|
|
|
|
|
rc++;
|
|
|
|
|
}
|
2009-01-12 17:44:04 +01:00
|
|
|
return rc;
|
|
|
|
|
}
|
2011-11-10 16:51:50 +01:00
|
|
|
|
2013-01-02 23:21:30 +02:00
|
|
|
/*! Updates user selections from \a source model.
|
|
|
|
|
*
|
2013-09-10 15:46:58 +02:00
|
|
|
* Assumes that both models are sorted with the same order, and there
|
2013-01-02 23:21:30 +02:00
|
|
|
* are no duplicate entries.
|
|
|
|
|
*/
|
|
|
|
|
void SubmitFileModel::updateSelections(SubmitFileModel *source)
|
|
|
|
|
{
|
2013-01-07 22:15:03 +02:00
|
|
|
QTC_ASSERT(source, return);
|
2013-01-02 23:21:30 +02:00
|
|
|
int rows = rowCount();
|
|
|
|
|
int sourceRows = source->rowCount();
|
|
|
|
|
int lastMatched = 0;
|
|
|
|
|
for (int i = 0; i < rows; ++i) {
|
|
|
|
|
// Since both models are sorted with the same order, there is no need
|
|
|
|
|
// to test rows earlier than latest match found
|
|
|
|
|
for (int j = lastMatched; j < sourceRows; ++j) {
|
|
|
|
|
if (file(i) == source->file(j) && state(i) == source->state(j)) {
|
2013-06-14 17:51:53 +03:00
|
|
|
if (isCheckable(i) && source->isCheckable(j))
|
|
|
|
|
setChecked(i, source->checked(j));
|
2013-01-02 23:21:30 +02:00
|
|
|
lastMatched = j + 1; // No duplicates, start on next entry
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:52:26 +01:00
|
|
|
const SubmitFileModel::FileStatusQualifier &SubmitFileModel::fileStatusQualifier() const
|
|
|
|
|
{
|
|
|
|
|
return m_fileStatusQualifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SubmitFileModel::setFileStatusQualifier(FileStatusQualifier &&func)
|
|
|
|
|
{
|
|
|
|
|
const int topLevelRowCount = rowCount();
|
|
|
|
|
const int topLevelColCount = columnCount();
|
|
|
|
|
for (int row = 0; row < topLevelRowCount; ++row) {
|
|
|
|
|
const QStandardItem *statusItem = item(row, stateColumn);
|
|
|
|
|
const FileStatusHint statusHint =
|
|
|
|
|
func ? func(statusItem->text(), statusItem->data()) : FileStatusUnknown;
|
|
|
|
|
const QBrush textForeground = fileStatusTextForeground(statusHint);
|
|
|
|
|
for (int col = 0; col < topLevelColCount; ++col)
|
|
|
|
|
item(row, col)->setForeground(textForeground);
|
|
|
|
|
}
|
|
|
|
|
m_fileStatusQualifier = func;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-07 12:31:48 +01:00
|
|
|
} // namespace VcsBase
|