Commit c7142aff authored by Tobias Hunger's avatar Tobias Hunger

Git: Only allow commit if author information is valid

Check author information to be valid before enableing
the commit button.

Task-number: QTCREATORBUG-2410
parent 685ae6bc
...@@ -139,7 +139,6 @@ struct SubmitEditorWidgetPrivate ...@@ -139,7 +139,6 @@ struct SubmitEditorWidgetPrivate
Ui::SubmitEditorWidget m_ui; Ui::SubmitEditorWidget m_ui;
bool m_filesSelected; bool m_filesSelected;
bool m_filesChecked;
int m_fileNameColumn; int m_fileNameColumn;
int m_activatedRow; int m_activatedRow;
bool m_emptyFileListEnabled; bool m_emptyFileListEnabled;
...@@ -148,16 +147,18 @@ struct SubmitEditorWidgetPrivate ...@@ -148,16 +147,18 @@ struct SubmitEditorWidgetPrivate
QVBoxLayout *m_fieldLayout; QVBoxLayout *m_fieldLayout;
QList<SubmitFieldWidget *> m_fieldWidgets; QList<SubmitFieldWidget *> m_fieldWidgets;
int m_lineWidth; int m_lineWidth;
bool m_commitEnabled;
}; };
SubmitEditorWidgetPrivate::SubmitEditorWidgetPrivate() : SubmitEditorWidgetPrivate::SubmitEditorWidgetPrivate() :
m_filesSelected(false), m_filesSelected(false),
m_filesChecked(false),
m_fileNameColumn(1), m_fileNameColumn(1),
m_activatedRow(-1), m_activatedRow(-1),
m_emptyFileListEnabled(false), m_emptyFileListEnabled(false),
m_fieldLayout(0), m_fieldLayout(0),
m_lineWidth(defaultLineWidth) m_lineWidth(defaultLineWidth),
m_commitEnabled(false)
{ {
} }
...@@ -209,10 +210,10 @@ void SubmitEditorWidget::registerActions(QAction *editorUndoAction, QAction *edi ...@@ -209,10 +210,10 @@ void SubmitEditorWidget::registerActions(QAction *editorUndoAction, QAction *edi
int count = 0; int count = 0;
if (const QAbstractItemModel *model = m_d->m_ui.fileView->model()) if (const QAbstractItemModel *model = m_d->m_ui.fileView->model())
count = model->rowCount(); count = model->rowCount();
qDebug() << Q_FUNC_INFO << submitAction << count << "items" << m_d->m_filesChecked; qDebug() << Q_FUNC_INFO << submitAction << count << "items";
} }
submitAction->setEnabled(m_d->m_filesChecked); m_d->m_commitEnabled = !canSubmit();
connect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool))); connect(this, SIGNAL(submitActionEnabledChanged(bool)), submitAction, SLOT(setEnabled(bool)));
// Wire setText via QActionSetTextSlotHelper. // Wire setText via QActionSetTextSlotHelper.
QActionSetTextSlotHelper *actionSlotHelper = submitAction->findChild<QActionSetTextSlotHelper *>(); QActionSetTextSlotHelper *actionSlotHelper = submitAction->findChild<QActionSetTextSlotHelper *>();
if (!actionSlotHelper) if (!actionSlotHelper)
...@@ -243,7 +244,7 @@ void SubmitEditorWidget::unregisterActions(QAction *editorUndoAction, QAction * ...@@ -243,7 +244,7 @@ void SubmitEditorWidget::unregisterActions(QAction *editorUndoAction, QAction *
} }
if (submitAction) { if (submitAction) {
disconnect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool))); disconnect(this, SIGNAL(submitActionEnabledChanged(bool)), submitAction, SLOT(setEnabled(bool)));
// Just deactivate the QActionSetTextSlotHelper on the action // Just deactivate the QActionSetTextSlotHelper on the action
disconnect(this, SIGNAL(submitActionTextChanged(QString)), 0, 0); disconnect(this, SIGNAL(submitActionTextChanged(QString)), 0, 0);
} }
...@@ -448,18 +449,20 @@ void SubmitEditorWidget::updateActions() ...@@ -448,18 +449,20 @@ void SubmitEditorWidget::updateActions()
void SubmitEditorWidget::updateSubmitAction() void SubmitEditorWidget::updateSubmitAction()
{ {
const unsigned checkedCount = checkedFilesCount(); const unsigned checkedCount = checkedFilesCount();
const bool newFilesCheckedState = m_d->m_emptyFileListEnabled || checkedCount > 0; const bool newCommitState = canSubmit();
// Emit signal to update action // Emit signal to update action
if (m_d->m_filesChecked != newFilesCheckedState) { if (m_d->m_commitEnabled != newCommitState) {
m_d->m_filesChecked = newFilesCheckedState; m_d->m_commitEnabled = newCommitState;
emit fileCheckStateChanged(m_d->m_filesChecked); emit submitActionEnabledChanged(m_d->m_commitEnabled);
} }
if (m_d->m_ui.fileView && m_d->m_ui.fileView->model()) {
// Update button text. // Update button text.
const int fileCount = m_d->m_ui.fileView->model()->rowCount(); const int fileCount = m_d->m_ui.fileView->model()->rowCount();
const QString msg = checkedCount ? const QString msg = checkedCount ?
tr("Commit %1/%n Files", 0, fileCount).arg(checkedCount) : tr("Commit %1/%n Files", 0, fileCount).arg(checkedCount) :
tr("Commit"); tr("Commit");
emit submitActionTextChanged(msg); emit submitActionTextChanged(msg);
}
} }
// Enable diff depending on selected files // Enable diff depending on selected files
...@@ -509,6 +512,12 @@ void SubmitEditorWidget::insertTopWidget(QWidget *w) ...@@ -509,6 +512,12 @@ void SubmitEditorWidget::insertTopWidget(QWidget *w)
m_d->m_ui.vboxLayout->insertWidget(0, w); m_d->m_ui.vboxLayout->insertWidget(0, w);
} }
bool SubmitEditorWidget::canSubmit() const
{
const unsigned checkedCount = checkedFilesCount();
return m_d->m_emptyFileListEnabled || checkedCount > 0;
}
void SubmitEditorWidget::addSubmitFieldWidget(SubmitFieldWidget *f) void SubmitEditorWidget::addSubmitFieldWidget(SubmitFieldWidget *f)
{ {
if (!m_d->m_fieldLayout) { if (!m_d->m_fieldLayout) {
......
...@@ -129,8 +129,8 @@ public: ...@@ -129,8 +129,8 @@ public:
signals: signals:
void diffSelected(const QStringList &); void diffSelected(const QStringList &);
void fileSelectionChanged(bool someFileSelected); void fileSelectionChanged(bool someFileSelected);
void fileCheckStateChanged(bool someFileChecked);
void submitActionTextChanged(const QString &); void submitActionTextChanged(const QString &);
void submitActionEnabledChanged(const bool);
public slots: public slots:
void checkAll(); void checkAll();
...@@ -139,13 +139,16 @@ public slots: ...@@ -139,13 +139,16 @@ public slots:
protected: protected:
virtual void changeEvent(QEvent *e); virtual void changeEvent(QEvent *e);
void insertTopWidget(QWidget *w); void insertTopWidget(QWidget *w);
virtual bool canSubmit() const;
protected slots:
void updateSubmitAction();
private slots: private slots:
void triggerDiffSelected(); void triggerDiffSelected();
void diffActivated(const QModelIndex &index); void diffActivated(const QModelIndex &index);
void diffActivatedDelayed(); void diffActivatedDelayed();
void updateActions(); void updateActions();
void updateSubmitAction();
void updateDiffAction(); void updateDiffAction();
void editorCustomContextMenuRequested(const QPoint &); void editorCustomContextMenuRequested(const QPoint &);
void fileListCustomContextMenuRequested(const QPoint & pos); void fileListCustomContextMenuRequested(const QPoint & pos);
......
...@@ -34,10 +34,13 @@ ...@@ -34,10 +34,13 @@
#include <texteditor/fontsettings.h> #include <texteditor/fontsettings.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <QtGui/QLineEdit>
#include <QtGui/QRegExpValidator>
#include <QtGui/QSyntaxHighlighter> #include <QtGui/QSyntaxHighlighter>
#include <QtGui/QTextEdit> #include <QtGui/QTextEdit>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QRegExp> #include <QtCore/QRegExp>
namespace Git { namespace Git {
...@@ -116,11 +119,19 @@ GitSubmitEditorWidget::GitSubmitEditorWidget(QWidget *parent) : ...@@ -116,11 +119,19 @@ GitSubmitEditorWidget::GitSubmitEditorWidget(QWidget *parent) :
m_gitSubmitPanelUi.setupUi(m_gitSubmitPanel); m_gitSubmitPanelUi.setupUi(m_gitSubmitPanel);
insertTopWidget(m_gitSubmitPanel); insertTopWidget(m_gitSubmitPanel);
new GitSubmitHighlighter(descriptionEdit()); new GitSubmitHighlighter(descriptionEdit());
m_emailValidator = new QRegExpValidator(QRegExp(QLatin1String("[^@ ]+@[^@ ]+\\.[a-zA-Z]+")), this);
m_gitSubmitPanelUi.emailLineEdit->setValidator(m_emailValidator);
connect(m_gitSubmitPanelUi.authorLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(authorInformationChanged()));
connect(m_gitSubmitPanelUi.emailLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(authorInformationChanged()));
} }
void GitSubmitEditorWidget::setPanelInfo(const GitSubmitEditorPanelInfo &info) void GitSubmitEditorWidget::setPanelInfo(const GitSubmitEditorPanelInfo &info)
{ {
m_gitSubmitPanelUi.repositoryLabel->setText(info.repository); m_gitSubmitPanelUi.repositoryLabel->setText(QDir::toNativeSeparators(info.repository));
m_gitSubmitPanelUi.branchLabel->setText(info.branch); m_gitSubmitPanelUi.branchLabel->setText(info.branch);
} }
...@@ -136,6 +147,32 @@ void GitSubmitEditorWidget::setPanelData(const GitSubmitEditorPanelData &data) ...@@ -136,6 +147,32 @@ void GitSubmitEditorWidget::setPanelData(const GitSubmitEditorPanelData &data)
{ {
m_gitSubmitPanelUi.authorLineEdit->setText(data.author); m_gitSubmitPanelUi.authorLineEdit->setText(data.author);
m_gitSubmitPanelUi.emailLineEdit->setText(data.email); m_gitSubmitPanelUi.emailLineEdit->setText(data.email);
authorInformationChanged();
}
bool GitSubmitEditorWidget::canSubmit() const
{
if (m_gitSubmitPanelUi.authorLineEdit->text().isEmpty()
|| !emailIsValid())
return false;
return SubmitEditorWidget::canSubmit();
}
void GitSubmitEditorWidget::authorInformationChanged()
{
m_gitSubmitPanelUi.invalidAuthorLabel->
setVisible(m_gitSubmitPanelUi.authorLineEdit->text().isEmpty());
m_gitSubmitPanelUi.invalidEmailLabel->
setVisible(!emailIsValid());
updateSubmitAction();
}
bool GitSubmitEditorWidget::emailIsValid() const
{
int pos = m_gitSubmitPanelUi.emailLineEdit->cursorPosition();
return m_emailValidator->validate(m_gitSubmitPanelUi.emailLineEdit->text(), pos)
== QValidator::Acceptable;
} }
} // namespace Internal } // namespace Internal
......
...@@ -31,8 +31,13 @@ ...@@ -31,8 +31,13 @@
#define GITSUBMITEDITORWIDGET_H #define GITSUBMITEDITORWIDGET_H
#include "ui_gitsubmitpanel.h" #include "ui_gitsubmitpanel.h"
#include <utils/submiteditorwidget.h> #include <utils/submiteditorwidget.h>
QT_BEGIN_NAMESPACE
class QValidator;
QT_END_NAMESPACE
namespace Git { namespace Git {
namespace Internal { namespace Internal {
...@@ -49,6 +54,7 @@ struct GitSubmitEditorPanelData; ...@@ -49,6 +54,7 @@ struct GitSubmitEditorPanelData;
class GitSubmitEditorWidget : public Utils::SubmitEditorWidget class GitSubmitEditorWidget : public Utils::SubmitEditorWidget
{ {
Q_OBJECT
public: public:
explicit GitSubmitEditorWidget(QWidget *parent = 0); explicit GitSubmitEditorWidget(QWidget *parent = 0);
...@@ -59,9 +65,18 @@ public: ...@@ -59,9 +65,18 @@ public:
void setPanelInfo(const GitSubmitEditorPanelInfo &info); void setPanelInfo(const GitSubmitEditorPanelInfo &info);
protected:
bool canSubmit() const;
private slots:
void authorInformationChanged();
private: private:
bool emailIsValid() const;
QWidget *m_gitSubmitPanel; QWidget *m_gitSubmitPanel;
Ui::GitSubmitPanel m_gitSubmitPanelUi; Ui::GitSubmitPanel m_gitSubmitPanelUi;
QValidator *m_emailValidator;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>374</width> <width>364</width>
<height>229</height> <height>172</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
...@@ -59,9 +59,7 @@ ...@@ -59,9 +59,7 @@
<property name="title"> <property name="title">
<string>Commit Information</string> <string>Commit Information</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QGridLayout" name="gridLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="authorLabel"> <widget class="QLabel" name="authorLabel">
<property name="text"> <property name="text">
...@@ -70,38 +68,87 @@ ...@@ -70,38 +68,87 @@
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="authorLineEdit"/> <widget class="QLineEdit" name="authorLineEdit">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
</widget>
</item> </item>
<item row="1" column="0"> <item row="0" column="2">
<widget class="QLabel" name="emailLabel"> <widget class="QLabel" name="invalidAuthorLabel">
<property name="minimumSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="text"> <property name="text">
<string>Email:</string> <string/>
</property>
<property name="pixmap">
<pixmap resource="../projectexplorer/projectexplorer.qrc">:/projectexplorer/images/compile_error.png</pixmap>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="0" column="3">
<widget class="QLineEdit" name="emailLineEdit"/>
</item>
</layout>
</item>
<item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="sizeType">
<enum>QSizePolicy::MinimumExpanding</enum>
</property>
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>161</width> <width>5</width>
<height>20</height> <height>20</height>
</size> </size>
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0">
<widget class="QLabel" name="emailLabel">
<property name="text">
<string>Email:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="emailLineEdit">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="invalidEmailLabel">
<property name="minimumSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../projectexplorer/projectexplorer.qrc">:/projectexplorer/images/compile_error.png</pixmap>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
</layout> </layout>
</widget> </widget>
<resources/> <resources>
<include location="../projectexplorer/projectexplorer.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>
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