Files
qt-creator/src/libs/utils/submiteditorwidget.cpp

587 lines
19 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include "submiteditorwidget.h"
#include "submitfieldwidget.h"
2008-12-02 12:01:29 +01:00
#include "ui_submiteditorwidget.h"
#include <QtCore/QDebug>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
#include <QtCore/QScopedPointer>
2008-12-02 12:01:29 +01:00
#include <QtGui/QPushButton>
#include <QtGui/QMenu>
#include <QtGui/QHBoxLayout>
#include <QtGui/QToolButton>
#include <QtGui/QSpacerItem>
2008-12-02 14:09:21 +01:00
enum { debug = 0 };
enum { defaultLineWidth = 72 };
2008-12-02 12:01:29 +01:00
enum { checkableColumn = 0 };
2008-12-02 12:01:29 +01:00
namespace Utils {
// QActionPushButton: A push button tied to an action
// (similar to a QToolButton)
2008-12-09 11:07:24 +01:00
class QActionPushButton : public QPushButton
{
Q_OBJECT
public:
explicit QActionPushButton(QAction *a);
private slots:
void actionChanged();
};
QActionPushButton::QActionPushButton(QAction *a) :
QPushButton(a->icon(), a->text())
{
connect(a, SIGNAL(changed()), this, SLOT(actionChanged()));
connect(this, SIGNAL(clicked()), a, SLOT(trigger()));
setEnabled(a->isEnabled());
}
void QActionPushButton::actionChanged()
{
if (const QAction *a = qobject_cast<QAction*>(sender())) {
setEnabled(a->isEnabled());
setText(a->text());
}
}
// A helper parented on a QAction,
// making QAction::setText() a slot (which it currently is not).
class QActionSetTextSlotHelper : public QObject
{
Q_OBJECT
public:
explicit QActionSetTextSlotHelper(QAction *a) : QObject(a) {}
public slots:
void setText(const QString &t) {
if (QAction *action = qobject_cast<QAction *>(parent()))
action->setText(t);
}
};
// 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)
{
const QModelIndexList indexList = view->selectionModel()->selectedRows(0);
if (indexList.empty())
return QList<int>();
QList<int> rc;
const QModelIndexList::const_iterator cend = indexList.constEnd();
for (QModelIndexList::const_iterator it = indexList.constBegin(); it != cend; ++it)
rc.push_back(it->row());
return rc;
}
// ----------- SubmitEditorWidgetPrivate
2008-12-02 14:09:21 +01:00
struct SubmitEditorWidgetPrivate
{
// A pair of position/action to extend context menus
typedef QPair<int, QPointer<QAction> > AdditionalContextMenuAction;
2008-12-02 12:01:29 +01:00
SubmitEditorWidgetPrivate();
Ui::SubmitEditorWidget m_ui;
bool m_filesSelected;
bool m_filesChecked;
int m_fileNameColumn;
int m_activatedRow;
QList<AdditionalContextMenuAction> descriptionEditContextMenuActions;
QVBoxLayout *m_fieldLayout;
QList<SubmitFieldWidget *> m_fieldWidgets;
int m_lineWidth;
2008-12-02 12:01:29 +01:00
};
SubmitEditorWidgetPrivate::SubmitEditorWidgetPrivate() :
m_filesSelected(false),
m_filesChecked(false),
m_fileNameColumn(1),
m_activatedRow(-1),
m_fieldLayout(0),
m_lineWidth(defaultLineWidth)
2008-12-02 12:01:29 +01:00
{
}
SubmitEditorWidget::SubmitEditorWidget(QWidget *parent) :
QWidget(parent),
m_d(new SubmitEditorWidgetPrivate)
{
m_d->m_ui.setupUi(this);
m_d->m_ui.description->setContextMenuPolicy(Qt::CustomContextMenu);
m_d->m_ui.description->setLineWrapMode(QTextEdit::NoWrap);
m_d->m_ui.description->setWordWrapMode(QTextOption::WordWrap);
connect(m_d->m_ui.description, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(editorCustomContextMenuRequested(QPoint)));
2008-12-02 12:01:29 +01:00
// File List
m_d->m_ui.fileView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_d->m_ui.fileView, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(fileListCustomContextMenuRequested(QPoint)));
m_d->m_ui.fileView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_d->m_ui.fileView->setRootIsDecorated(false);
connect(m_d->m_ui.fileView, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(diffActivated(QModelIndex)));
2008-12-02 12:01:29 +01:00
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(m_d->m_ui.description);
}
SubmitEditorWidget::~SubmitEditorWidget()
{
delete m_d;
}
void SubmitEditorWidget::registerActions(QAction *editorUndoAction, QAction *editorRedoAction,
2008-12-02 12:01:29 +01:00
QAction *submitAction, QAction *diffAction)
{
if (editorUndoAction) {
editorUndoAction->setEnabled(m_d->m_ui.description->document()->isUndoAvailable());
connect(m_d->m_ui.description, SIGNAL(undoAvailable(bool)), editorUndoAction, SLOT(setEnabled(bool)));
connect(editorUndoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(undo()));
}
if (editorRedoAction) {
editorRedoAction->setEnabled(m_d->m_ui.description->document()->isRedoAvailable());
connect(m_d->m_ui.description, SIGNAL(redoAvailable(bool)), editorRedoAction, SLOT(setEnabled(bool)));
connect(editorRedoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(redo()));
}
if (submitAction) {
if (debug) {
int count = 0;
if (const QAbstractItemModel *model = m_d->m_ui.fileView->model())
count = model->rowCount();
qDebug() << Q_FUNC_INFO << submitAction << count << "items" << m_d->m_filesChecked;
}
2008-12-02 12:01:29 +01:00
submitAction->setEnabled(m_d->m_filesChecked);
connect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool)));
// Wire setText via QActionSetTextSlotHelper.
QActionSetTextSlotHelper *actionSlotHelper = submitAction->findChild<QActionSetTextSlotHelper *>();
if (!actionSlotHelper)
actionSlotHelper = new QActionSetTextSlotHelper(submitAction);
connect(this, SIGNAL(submitActionTextChanged(QString)), actionSlotHelper, SLOT(setText(QString)));
m_d->m_ui.buttonLayout->addWidget(new QActionPushButton(submitAction));
2008-12-02 12:01:29 +01:00
}
if (diffAction) {
if (debug)
qDebug() << diffAction << m_d->m_filesSelected;
diffAction->setEnabled(m_d->m_filesSelected);
connect(this, SIGNAL(fileSelectionChanged(bool)), diffAction, SLOT(setEnabled(bool)));
connect(diffAction, SIGNAL(triggered()), this, SLOT(triggerDiffSelected()));
m_d->m_ui.buttonLayout->addWidget(new QActionPushButton(diffAction));
2008-12-02 12:01:29 +01:00
}
}
void SubmitEditorWidget::unregisterActions(QAction *editorUndoAction, QAction *editorRedoAction,
QAction *submitAction, QAction *diffAction)
{
if (editorUndoAction) {
disconnect(m_d->m_ui.description, SIGNAL(undoAvailableChanged(bool)), editorUndoAction, SLOT(setEnabled(bool)));
disconnect(editorUndoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(undo()));
}
if (editorRedoAction) {
disconnect(m_d->m_ui.description, SIGNAL(redoAvailableChanged(bool)), editorRedoAction, SLOT(setEnabled(bool)));
disconnect(editorRedoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(redo()));
}
if (submitAction) {
2008-12-02 12:01:29 +01:00
disconnect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool)));
// Just deactivate the QActionSetTextSlotHelper on the action
disconnect(this, SIGNAL(submitActionTextChanged(QString)), 0, 0);
}
2008-12-02 12:01:29 +01:00
if (diffAction) {
disconnect(this, SIGNAL(fileSelectionChanged(bool)), diffAction, SLOT(setEnabled(bool)));
disconnect(diffAction, SIGNAL(triggered()), this, SLOT(triggerDiffSelected()));
}
}
// Make sure we have one terminating NL. Do not trim front as leading space might be
// required for some formattings.
static inline QString trimMessageText(QString t)
2008-12-02 12:01:29 +01:00
{
if (t.isEmpty())
return t;
// Trim back of string.
const int last = t.size() - 1;
int lastWordCharacter = last;
for ( ; lastWordCharacter >= 0 && t.at(lastWordCharacter).isSpace() ; lastWordCharacter--) ;
if (lastWordCharacter != last)
t.truncate(lastWordCharacter + 1);
t += QLatin1Char('\n');
return t;
}
// Extract the wrapped text from a text edit, which performs
// the wrapping only optically.
static QString wrappedText(const QTextEdit *e)
{
const QChar newLine = QLatin1Char('\n');
QString rc;
QTextCursor cursor(e->document());
cursor.movePosition(QTextCursor::Start);
while (!cursor.atEnd()) {
cursor.select(QTextCursor::LineUnderCursor);
rc += cursor.selectedText();
rc += newLine;
cursor.movePosition(QTextCursor::EndOfLine); // Mac needs it
cursor.movePosition(QTextCursor::Right);
}
return rc;
2008-12-02 12:01:29 +01:00
}
QString SubmitEditorWidget::descriptionText() const
{
QString rc = trimMessageText(lineWrap() ? wrappedText(m_d->m_ui.description) : m_d->m_ui.description->toPlainText());
// append field entries
foreach(const SubmitFieldWidget *fw, m_d->m_fieldWidgets)
rc += fw->fieldValues();
return rc;
2008-12-02 12:01:29 +01:00
}
void SubmitEditorWidget::setDescriptionText(const QString &text)
{
m_d->m_ui.description->setPlainText(text);
}
bool SubmitEditorWidget::lineWrap() const
{
return m_d->m_ui.description->lineWrapMode() != QTextEdit::NoWrap;
}
void SubmitEditorWidget::setLineWrap(bool v)
{
if (debug)
qDebug() << Q_FUNC_INFO << v;
if (v) {
m_d->m_ui.description->setLineWrapColumnOrWidth(m_d->m_lineWidth);
m_d->m_ui.description->setLineWrapMode(QTextEdit::FixedColumnWidth);
} else {
m_d->m_ui.description->setLineWrapMode(QTextEdit::NoWrap);
}
}
int SubmitEditorWidget::lineWrapWidth() const
{
return m_d->m_lineWidth;
}
void SubmitEditorWidget::setLineWrapWidth(int v)
{
if (debug)
qDebug() << Q_FUNC_INFO << v << lineWrap();
if (m_d->m_lineWidth == v)
return;
m_d->m_lineWidth = v;
if (lineWrap())
m_d->m_ui.description->setLineWrapColumnOrWidth(v);
}
int SubmitEditorWidget::fileNameColumn() const
2008-12-02 12:01:29 +01:00
{
return m_d->m_fileNameColumn;
2008-12-02 12:01:29 +01:00
}
void SubmitEditorWidget::setFileNameColumn(int c)
2008-12-02 12:01:29 +01:00
{
m_d->m_fileNameColumn = c;
2008-12-02 12:01:29 +01:00
}
QAbstractItemView::SelectionMode SubmitEditorWidget::fileListSelectionMode() const
{
return m_d->m_ui.fileView->selectionMode();
}
void SubmitEditorWidget::setFileListSelectionMode(QAbstractItemView::SelectionMode sm)
{
m_d->m_ui.fileView->setSelectionMode(sm);
}
void SubmitEditorWidget::setFileModel(QAbstractItemModel *model)
2008-12-02 12:01:29 +01:00
{
m_d->m_ui.fileView->clearSelection(); // trigger the change signals
2008-12-02 12:01:29 +01:00
m_d->m_ui.fileView->setModel(model);
2008-12-02 12:01:29 +01:00
if (model->rowCount()) {
const int columnCount = model->columnCount();
for (int c = 0; c < columnCount; c++)
m_d->m_ui.fileView->resizeColumnToContents(c);
2008-12-02 12:01:29 +01:00
}
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(updateSubmitAction()));
connect(model, SIGNAL(modelReset()),
this, SLOT(updateSubmitAction()));
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(updateSubmitAction()));
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(updateSubmitAction()));
connect(m_d->m_ui.fileView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this, SLOT(updateDiffAction()));
updateActions();
2008-12-02 12:01:29 +01:00
}
QAbstractItemModel *SubmitEditorWidget::fileModel() const
2008-12-02 12:01:29 +01:00
{
return m_d->m_ui.fileView->model();
2008-12-02 12:01:29 +01:00
}
QStringList SubmitEditorWidget::selectedFiles() const
{
const QList<int> selection = selectedRows(m_d->m_ui.fileView);
if (selection.empty())
return QStringList();
2008-12-02 12:01:29 +01:00
QStringList rc;
const QAbstractItemModel *model = m_d->m_ui.fileView->model();
const int count = selection.size();
for (int i = 0; i < count; i++)
rc.push_back(listModelText(model, selection.at(i), fileNameColumn()));
2008-12-02 12:01:29 +01:00
return rc;
}
QStringList SubmitEditorWidget::checkedFiles() const
{
QStringList rc;
const QAbstractItemModel *model = m_d->m_ui.fileView->model();
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()));
2008-12-02 12:01:29 +01:00
return rc;
}
QTextEdit *SubmitEditorWidget::descriptionEdit() const
2008-12-02 12:01:29 +01:00
{
return m_d->m_ui.description;
}
void SubmitEditorWidget::triggerDiffSelected()
{
const QStringList sel = selectedFiles();
if (!sel.empty())
emit diffSelected(sel);
}
void SubmitEditorWidget::diffActivatedDelayed()
{
const QStringList files = QStringList(listModelText(m_d->m_ui.fileView->model(), m_d->m_activatedRow, fileNameColumn()));
emit diffSelected(files);
}
void SubmitEditorWidget::diffActivated(const QModelIndex &index)
{
// We need to delay the signal, otherwise, the diff editor will not
// be in the foreground.
m_d->m_activatedRow = index.row();
QTimer::singleShot(0, this, SLOT(diffActivatedDelayed()));
}
void SubmitEditorWidget::updateActions()
{
updateSubmitAction();
updateDiffAction();
}
// Enable submit depending on having checked files
void SubmitEditorWidget::updateSubmitAction()
{
const unsigned checkedCount = checkedFilesCount();
const bool newFilesCheckedState = checkedCount;
// Emit signal to update action
if (m_d->m_filesChecked != newFilesCheckedState) {
m_d->m_filesChecked = newFilesCheckedState;
emit fileCheckStateChanged(m_d->m_filesChecked);
2008-12-02 12:01:29 +01:00
}
// Update button text.
const int fileCount = m_d->m_ui.fileView->model()->rowCount();
const QString msg = checkedCount ?
tr("Commit %1/%2 Files").arg(checkedCount).arg(fileCount) :
tr("Commit");
emit submitActionTextChanged(msg);
2008-12-02 12:01:29 +01:00
}
// Enable diff depending on selected files
void SubmitEditorWidget::updateDiffAction()
2008-12-02 12:01:29 +01:00
{
const bool filesSelected = hasSelection();
if (m_d->m_filesSelected != filesSelected) {
m_d->m_filesSelected = filesSelected;
2008-12-02 12:01:29 +01:00
emit fileSelectionChanged(m_d->m_filesSelected);
}
}
bool SubmitEditorWidget::hasSelection() const
{
// Not present until model is set
if (const QItemSelectionModel *sm = m_d->m_ui.fileView->selectionModel())
return sm->hasSelection();
return false;
}
unsigned SubmitEditorWidget::checkedFilesCount() const
{
unsigned checkedCount = 0;
if (const QAbstractItemModel *model = m_d->m_ui.fileView->model()) {
const int count = model->rowCount();
for (int i = 0; i < count; i++)
if (listModelChecked(model, i, checkableColumn))
checkedCount++;
}
return checkedCount;
}
2008-12-02 12:01:29 +01:00
void SubmitEditorWidget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
2008-12-09 11:07:24 +01:00
switch (e->type()) {
2008-12-02 12:01:29 +01:00
case QEvent::LanguageChange:
m_d->m_ui.retranslateUi(this);
break;
default:
break;
}
}
void SubmitEditorWidget::insertTopWidget(QWidget *w)
{
m_d->m_ui.vboxLayout->insertWidget(0, w);
}
void SubmitEditorWidget::addSubmitFieldWidget(SubmitFieldWidget *f)
{
if (!m_d->m_fieldLayout) {
// VBox with horizontal, expanding spacer
m_d->m_fieldLayout = new QVBoxLayout;
QHBoxLayout *outerLayout = new QHBoxLayout;
outerLayout->addLayout(m_d->m_fieldLayout);
outerLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
QBoxLayout *descrLayout = qobject_cast<QBoxLayout*>(m_d->m_ui.descriptionBox->layout());
Q_ASSERT(descrLayout);
descrLayout->addLayout(outerLayout);
}
m_d->m_fieldLayout->addWidget(f);
m_d->m_fieldWidgets.push_back(f);
}
QList<SubmitFieldWidget *> SubmitEditorWidget::submitFieldWidgets() const
{
return m_d->m_fieldWidgets;
}
void SubmitEditorWidget::addDescriptionEditContextMenuAction(QAction *a)
{
m_d->descriptionEditContextMenuActions.push_back(SubmitEditorWidgetPrivate::AdditionalContextMenuAction(-1, a));
}
void SubmitEditorWidget::insertDescriptionEditContextMenuAction(int pos, QAction *a)
{
m_d->descriptionEditContextMenuActions.push_back(SubmitEditorWidgetPrivate::AdditionalContextMenuAction(pos, a));
}
void SubmitEditorWidget::editorCustomContextMenuRequested(const QPoint &pos)
{
QScopedPointer<QMenu> menu(m_d->m_ui.description->createStandardContextMenu());
// Extend
foreach (const SubmitEditorWidgetPrivate::AdditionalContextMenuAction &a, m_d->descriptionEditContextMenuActions) {
if (a.second) {
if (a.first >= 0) {
menu->insertAction(menu->actions().at(a.first), a.second);
} else {
menu->addAction(a.second);
}
}
}
menu->exec(m_d->m_ui.description->mapToGlobal(pos));
}
void SubmitEditorWidget::checkAll()
{
setListModelChecked(m_d->m_ui.fileView->model(), true, checkableColumn);
}
void SubmitEditorWidget::uncheckAll()
{
setListModelChecked(m_d->m_ui.fileView->model(), false, checkableColumn);
}
void SubmitEditorWidget::fileListCustomContextMenuRequested(const QPoint & pos)
{
// Execute menu offering to check/uncheck all
QMenu menu;
QAction *checkAllAction = menu.addAction(tr("Check All"));
QAction *uncheckAllAction = menu.addAction(tr("Uncheck All"));
QAction *action = menu.exec(m_d->m_ui.fileView->mapToGlobal(pos));
if (action == checkAllAction) {
checkAll();
return;
}
if (action == uncheckAllAction) {
uncheckAll();
return;
}
}
2008-12-02 12:01:29 +01:00
} // namespace Utils
#include "submiteditorwidget.moc"