Commit 01084aaa authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Tobias Hunger

VCS: Use SubmitFileModel in SubmitEditorWidget

Much cleaner

Change-Id: I090e5d04e5afae1023fa58eca1ef416ddc069165
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 34db64c7
......@@ -131,10 +131,9 @@ void GitSubmitEditor::slotDiffSelected(const QStringList &files)
QStringList unmergedFiles;
QStringList unstagedFiles;
QStringList stagedFiles;
const int fileColumn = fileNameColumn();
const int rowCount = m_model->rowCount();
for (int r = 0; r < rowCount; r++) {
const QString fileName = m_model->item(r, fileColumn)->text();
const QString fileName = m_model->file(r);
if (files.contains(fileName)) {
const FileStates state = static_cast<FileStates>(m_model->extraData(r).toInt());
if (state & UnmergedFile)
......
......@@ -114,7 +114,7 @@ bool PerforceSubmitEditor::parseText(QString text)
void PerforceSubmitEditor::restrictToProjectFiles(const QStringList &knownProjectFiles)
{
m_fileModel->filter(knownProjectFiles, fileNameColumn());
m_fileModel->filterFiles(knownProjectFiles);
}
void PerforceSubmitEditor::updateFields()
......
......@@ -29,6 +29,7 @@
#include "submiteditorwidget.h"
#include "submitfieldwidget.h"
#include "submitfilemodel.h"
#include "ui_submiteditorwidget.h"
#include <QDebug>
......@@ -46,8 +47,6 @@
enum { debug = 0 };
enum { defaultLineWidth = 72 };
enum { checkableColumn = 0 };
/*!
\class VcsBase::SubmitEditorWidget
......@@ -55,8 +54,8 @@ enum { checkableColumn = 0 };
checkable list of modified files in a list window.
The user can delete files from the list by unchecking them or diff the selection
by doubleclicking. A list model which contains the file in a column
specified by fileNameColumn should be set using setFileModel().
by doubleclicking. A list model which contains state and file columns should be
set using setFileModel().
Additionally, standard creator actions can be registered:
Undo/redo will be set up to work with the description editor.
......@@ -117,28 +116,6 @@ public slots:
};
// Helpers to retrieve model data
static inline bool listModelChecked(const QAbstractItemModel *model, int row, int column = 0)
{
const QModelIndex checkableIndex = model->index(row, column, QModelIndex());
return model->data(checkableIndex, Qt::CheckStateRole).toInt() == Qt::Checked;
}
static void setListModelChecked(QAbstractItemModel *model, bool checked, int column = 0)
{
const QVariant data = QVariant(int(checked ? Qt::Checked : Qt::Unchecked));
const int count = model->rowCount();
for (int i = 0; i < count; i++) {
const QModelIndex checkableIndex = model->index(i, column, QModelIndex());
model->setData(checkableIndex, data, Qt::CheckStateRole);
}
}
static inline QString listModelText(const QAbstractItemModel *model, int row, int column)
{
const QModelIndex index = model->index(row, column, QModelIndex());
return model->data(index, Qt::DisplayRole).toString();
}
// Convenience to extract a list of selected indexes
QList<int> selectedRows(const QAbstractItemView *view)
{
......@@ -163,7 +140,6 @@ struct SubmitEditorWidgetPrivate
Ui::SubmitEditorWidget m_ui;
bool m_filesSelected;
int m_fileNameColumn;
int m_activatedRow;
bool m_emptyFileListEnabled;
......@@ -180,7 +156,6 @@ struct SubmitEditorWidgetPrivate
SubmitEditorWidgetPrivate::SubmitEditorWidgetPrivate() :
m_filesSelected(false),
m_fileNameColumn(1),
m_activatedRow(-1),
m_emptyFileListEnabled(false),
m_fieldLayout(0),
......@@ -242,9 +217,8 @@ void SubmitEditorWidget::registerActions(QAction *editorUndoAction, QAction *edi
if (submitAction) {
if (debug) {
int count = 0;
if (const QAbstractItemModel *model = d->m_ui.fileView->model())
count = model->rowCount();
const SubmitFileModel *model = fileModel();
int count = model ? model->rowCount() : 0;
qDebug() << Q_FUNC_INFO << submitAction << count << "items";
}
d->m_commitEnabled = !canSubmit();
......@@ -385,16 +359,6 @@ void SubmitEditorWidget::setDescriptionMandatory(bool v)
d->m_descriptionMandatory = v;
}
int SubmitEditorWidget::fileNameColumn() const
{
return d->m_fileNameColumn;
}
void SubmitEditorWidget::setFileNameColumn(int c)
{
d->m_fileNameColumn = c;
}
QAbstractItemView::SelectionMode SubmitEditorWidget::fileListSelectionMode() const
{
return d->m_ui.fileView->selectionMode();
......@@ -405,7 +369,7 @@ void SubmitEditorWidget::setFileListSelectionMode(QAbstractItemView::SelectionMo
d->m_ui.fileView->setSelectionMode(sm);
}
void SubmitEditorWidget::setFileModel(QAbstractItemModel *model)
void SubmitEditorWidget::setFileModel(SubmitFileModel *model)
{
d->m_ui.fileView->clearSelection(); // trigger the change signals
......@@ -434,9 +398,9 @@ void SubmitEditorWidget::setFileModel(QAbstractItemModel *model)
updateActions();
}
QAbstractItemModel *SubmitEditorWidget::fileModel() const
SubmitFileModel *SubmitEditorWidget::fileModel() const
{
return d->m_ui.fileView->model();
return static_cast<SubmitFileModel *>(d->m_ui.fileView->model());
}
QStringList SubmitEditorWidget::selectedFiles() const
......@@ -446,23 +410,23 @@ QStringList SubmitEditorWidget::selectedFiles() const
return QStringList();
QStringList rc;
const QAbstractItemModel *model = d->m_ui.fileView->model();
const SubmitFileModel *model = fileModel();
const int count = selection.size();
for (int i = 0; i < count; i++)
rc.push_back(listModelText(model, selection.at(i), fileNameColumn()));
rc.push_back(model->file(selection.at(i)));
return rc;
}
QStringList SubmitEditorWidget::checkedFiles() const
{
QStringList rc;
const QAbstractItemModel *model = d->m_ui.fileView->model();
const SubmitFileModel *model = fileModel();
if (!model)
return rc;
const int count = model->rowCount();
for (int i = 0; i < count; i++)
if (listModelChecked(model, i, checkableColumn))
rc.push_back(listModelText(model, i, fileNameColumn()));
if (model->checked(i))
rc.push_back(model->file(i));
return rc;
}
......@@ -480,7 +444,7 @@ void SubmitEditorWidget::triggerDiffSelected()
void SubmitEditorWidget::diffActivatedDelayed()
{
const QStringList files = QStringList(listModelText(d->m_ui.fileView->model(), d->m_activatedRow, fileNameColumn()));
const QStringList files = QStringList(fileModel()->file(d->m_activatedRow));
emit diffSelected(files);
}
......@@ -554,10 +518,10 @@ bool SubmitEditorWidget::hasSelection() const
int SubmitEditorWidget::checkedFilesCount() const
{
int checkedCount = 0;
if (const QAbstractItemModel *model = d->m_ui.fileView->model()) {
if (const SubmitFileModel *model = fileModel()) {
const int count = model->rowCount();
for (int i = 0; i < count; ++i)
if (listModelChecked(model, i, checkableColumn))
if (model->checked(i))
++checkedCount;
}
return checkedCount;
......@@ -662,24 +626,20 @@ void SubmitEditorWidget::checkAllToggled()
{
if (d->m_ignoreChange)
return;
if (d->m_ui.checkAllCheckBox->checkState() == Qt::Checked
|| d->m_ui.checkAllCheckBox->checkState() == Qt::PartiallyChecked) {
setListModelChecked(d->m_ui.fileView->model(), true, checkableColumn);
} else {
setListModelChecked(d->m_ui.fileView->model(), false, checkableColumn);
}
Qt::CheckState checkState = d->m_ui.checkAllCheckBox->checkState();
fileModel()->setAllChecked(checkState == Qt::Checked || checkState == Qt::PartiallyChecked);
// Reset that again, so that the user can't do it
d->m_ui.checkAllCheckBox->setTristate(false);
}
void SubmitEditorWidget::checkAll()
{
setListModelChecked(d->m_ui.fileView->model(), true, checkableColumn);
fileModel()->setAllChecked(true);
}
void SubmitEditorWidget::uncheckAll()
{
setListModelChecked(d->m_ui.fileView->model(), false, checkableColumn);
fileModel()->setAllChecked(false);
}
void SubmitEditorWidget::fileListCustomContextMenuRequested(const QPoint & pos)
......
......@@ -38,23 +38,20 @@
#include <QAbstractItemView>
QT_BEGIN_NAMESPACE
class QListWidgetItem;
class QAction;
class QAbstractItemModel;
class QModelIndex;
class QLineEdit;
QT_END_NAMESPACE
namespace VcsBase {
class SubmitFieldWidget;
struct SubmitEditorWidgetPrivate;
class SubmitFileModel;
class VCSBASE_EXPORT SubmitEditorWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString descriptionText READ descriptionText WRITE setDescriptionText DESIGNABLE true)
Q_PROPERTY(int fileNameColumn READ fileNameColumn WRITE setFileNameColumn DESIGNABLE false)
Q_PROPERTY(QAbstractItemView::SelectionMode fileListSelectionMode READ fileListSelectionMode WRITE setFileListSelectionMode DESIGNABLE true)
Q_PROPERTY(bool lineWrap READ lineWrap WRITE setLineWrap DESIGNABLE true)
Q_PROPERTY(int lineWrapWidth READ lineWrapWidth WRITE setLineWrapWidth DESIGNABLE true)
......@@ -80,9 +77,6 @@ public:
bool isEmptyFileListEnabled() const;
void setEmptyFileListEnabled(bool e);
int fileNameColumn() const;
void setFileNameColumn(int c);
bool lineWrap() const;
void setLineWrap(bool);
......@@ -95,8 +89,8 @@ public:
QAbstractItemView::SelectionMode fileListSelectionMode() const;
void setFileListSelectionMode(QAbstractItemView::SelectionMode sm);
void setFileModel(QAbstractItemModel *model);
QAbstractItemModel *fileModel() const;
void setFileModel(SubmitFileModel *model);
SubmitFileModel *fileModel() const;
// Files to be included in submit
QStringList checkedFiles() const;
......
......@@ -42,6 +42,8 @@ namespace VcsBase {
// Helpers:
// --------------------------------------------------------------------------
enum { fileColumn = 1 };
static QList<QStandardItem *> createFileRow(const QString &fileName, const QString &status,
CheckMode checked, const QVariant &v)
{
......@@ -87,15 +89,6 @@ QList<QStandardItem *> SubmitFileModel::addFile(const QString &fileName, const Q
return row;
}
QList<QStandardItem *> SubmitFileModel::rowAt(int row) const
{
const int colCount = columnCount();
QList<QStandardItem *> rc;
for (int c = 0; c < colCount; c++)
rc.push_back(item(row, c));
return rc;
}
QString SubmitFileModel::state(int row) const
{
if (row < 0 || row >= rowCount())
......@@ -107,7 +100,7 @@ QString SubmitFileModel::file(int row) const
{
if (row < 0 || row >= rowCount())
return QString();
return item(row, 1)->text();
return item(row, fileColumn)->text();
}
bool SubmitFileModel::checked(int row) const
......@@ -123,6 +116,13 @@ void SubmitFileModel::setChecked(int row, bool check)
item(row)->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
void SubmitFileModel::setAllChecked(bool check)
{
int rows = rowCount();
for (int row = 0; row < rows; ++row)
item(row)->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
QVariant SubmitFileModel::extraData(int row) const
{
if (row < 0 || row >= rowCount())
......@@ -139,25 +139,14 @@ bool SubmitFileModel::hasCheckedFiles() const
return false;
}
QList<QStandardItem *> SubmitFileModel::findRow(const QString &text, int column) const
{
// Single item
const QList<QStandardItem *> items = findItems(text, Qt::MatchExactly, column);
if (items.empty())
return items;
// Compile row
return rowAt(items.front()->row());
}
unsigned SubmitFileModel::filter(const QStringList &filter, int column)
unsigned int SubmitFileModel::filterFiles(const QStringList &filter)
{
unsigned rc = 0;
unsigned int rc = 0;
for (int r = rowCount() - 1; r >= 0; r--)
if (const QStandardItem *i = item(r, column))
if (!filter.contains(i->text())) {
qDeleteAll(takeRow(r));
rc++;
}
if (!filter.contains(file(r))) {
removeRow(r);
rc++;
}
return rc;
}
......
......@@ -53,23 +53,18 @@ public:
QList<QStandardItem *> addFile(const QString &fileName, const QString &status = QString(),
CheckMode checkMode = Checked, const QVariant &data = QVariant());
// Find convenience that returns the whole row (as opposed to QStandardItemModel::find).
QList<QStandardItem *> findRow(const QString &text, int column = 0) const;
// Convenience to obtain a row
QList<QStandardItem *> rowAt(int row) const;
QString state(int row) const;
QString file(int row) const;
bool checked(int row) const;
void setChecked(int row, bool check);
void setAllChecked(bool check);
QVariant extraData(int row) const;
bool hasCheckedFiles() const;
// Filter for entries contained in the filter list. Returns the
// number of deleted entries.
unsigned filter(const QStringList &filter, int column);
unsigned int filterFiles(const QStringList &filter);
};
} // namespace VcsBase
......
......@@ -34,6 +34,7 @@
#include "submiteditorfile.h"
#include "submiteditorwidget.h"
#include "submitfieldwidget.h"
#include "submitfilemodel.h"
#include "vcsbaseoutputwindow.h"
#include "vcsplugin.h"
......@@ -318,16 +319,6 @@ void VcsBaseSubmitEditor::unregisterActions(QAction *editorUndoAction, QAction
d->m_diffAction = d->m_submitAction = 0;
}
int VcsBaseSubmitEditor::fileNameColumn() const
{
return d->m_widget->fileNameColumn();
}
void VcsBaseSubmitEditor::setFileNameColumn(int c)
{
d->m_widget->setFileNameColumn(c);
}
QAbstractItemView::SelectionMode VcsBaseSubmitEditor::fileListSelectionMode() const
{
return d->m_widget->fileListSelectionMode();
......@@ -486,7 +477,7 @@ QStringList VcsBaseSubmitEditor::checkedFiles() const
return d->m_widget->checkedFiles();
}
void VcsBaseSubmitEditor::setFileModel(QAbstractItemModel *m, const QString &repositoryDirectory)
void VcsBaseSubmitEditor::setFileModel(SubmitFileModel *m, const QString &repositoryDirectory)
{
d->m_widget->setFileModel(m);
......@@ -495,8 +486,7 @@ void VcsBaseSubmitEditor::setFileModel(QAbstractItemModel *m, const QString &rep
// Iterate over the files and get interesting symbols
for (int row = 0; row < m->rowCount(); ++row) {
const QString fileName = m->data(m->index(row, d->m_widget->fileNameColumn())).toString();
const QFileInfo fileInfo(repositoryDirectory, fileName);
const QFileInfo fileInfo(repositoryDirectory, m->file(row));
// Add file name
uniqueSymbols.insert(fileInfo.fileName());
......@@ -539,7 +529,7 @@ void VcsBaseSubmitEditor::setFileModel(QAbstractItemModel *m, const QString &rep
}
}
QAbstractItemModel *VcsBaseSubmitEditor::fileModel() const
SubmitFileModel *VcsBaseSubmitEditor::fileModel() const
{
return d->m_widget->fileModel();
}
......
......@@ -38,7 +38,6 @@
QT_BEGIN_NAMESPACE
class QIcon;
class QAbstractItemModel;
class QAction;
QT_END_NAMESPACE
......@@ -48,6 +47,7 @@ namespace Internal {
}
struct VcsBaseSubmitEditorPrivate;
class SubmitEditorWidget;
class SubmitFileModel;
class VCSBASE_EXPORT VcsBaseSubmitEditorParameters
{
......@@ -61,7 +61,6 @@ public:
class VCSBASE_EXPORT VcsBaseSubmitEditor : public Core::IEditor
{
Q_OBJECT
Q_PROPERTY(int fileNameColumn READ fileNameColumn WRITE setFileNameColumn DESIGNABLE false)
Q_PROPERTY(QAbstractItemView::SelectionMode fileListSelectionMode READ fileListSelectionMode WRITE setFileListSelectionMode DESIGNABLE true)
Q_PROPERTY(bool lineWrap READ lineWrap WRITE setLineWrap DESIGNABLE true)
Q_PROPERTY(int lineWrapWidth READ lineWrapWidth WRITE setLineWrapWidth DESIGNABLE true)
......@@ -94,9 +93,6 @@ public:
bool forcePrompt = false,
bool canCommitOnFailure = true) const;
int fileNameColumn() const;
void setFileNameColumn(int c);
QAbstractItemView::SelectionMode fileListSelectionMode() const;
void setFileListSelectionMode(QAbstractItemView::SelectionMode sm);
......@@ -131,8 +127,8 @@ public:
QStringList checkedFiles() const;
void setFileModel(QAbstractItemModel *m, const QString &repositoryDirectory = QString());
QAbstractItemModel *fileModel() const;
void setFileModel(SubmitFileModel *m, const QString &repositoryDirectory = QString());
SubmitFileModel *fileModel() const;
virtual void updateFileModel() { }
// Utilities returning some predefined icons for actions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment