Commit 0cee605a authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Implement a new commit dialog

This commit is being written from this new dialog ;)

The new dialog has the following features:
- Highlights if the subject > 52 or description lines > 72
- Improved sizing, the plaintextedit is now fixed to 72 chars length
- Shows the number of chars near the top right border of the upper line
  edit for commit-subject
parent 0b8d346a
......@@ -62,6 +62,7 @@ target_sources(
branchesdialogmodel.cpp
gitwidget.cpp
gitstatusmodel.cpp
gitcommitdialog.cpp
tools/kateprojectcodeanalysistoolcppcheck.cpp
tools/kateprojectcodeanalysistoolflake8.cpp
......
#include "gitcommitdialog.h"
#include <QDebug>
#include <QSyntaxHighlighter>
#include <QVBoxLayout>
#include <KLocalizedString>
class BadLengthHighlighter : public QSyntaxHighlighter
{
public:
explicit BadLengthHighlighter(QTextDocument *doc, int badLen)
: QSyntaxHighlighter(doc)
, badLength(badLen)
{
}
void highlightBlock(const QString &text) override
{
if (text.length() < badLength) {
return;
}
setFormat(badLength, text.length() - badLength, Qt::red);
}
private:
int badLength = 0;
};
class SingleLineEdit : public QPlainTextEdit
{
public:
explicit SingleLineEdit(const QFont &font, QWidget *parent = nullptr)
: QPlainTextEdit(parent)
, m_hl(new BadLengthHighlighter(document(), 52))
{
// create a temporary line edit to figure out the correct size
QLineEdit le;
le.setFont(font);
le.setText(QStringLiteral("TEMP"));
setFont(font);
setFixedHeight(le.sizeHint().height());
setLineWrapMode(QPlainTextEdit::NoWrap);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void setText(const QString &text)
{
setPlainText(text);
}
int textLength()
{
return toPlainText().length();
}
private:
BadLengthHighlighter *m_hl;
};
GitCommitDialog::GitCommitDialog(const QString &lastCommit, const QFont &font, QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f)
, m_le(new SingleLineEdit(font))
, m_hl(new BadLengthHighlighter(m_pe.document(), 72))
{
setWindowTitle(i18n("Commit Changes"));
ok.setText(i18n("Commit"));
cancel.setText(i18n("Cancel"));
m_le->setPlaceholderText(i18n("Write commit message..."));
m_le->setFont(font);
QFontMetrics fm(font);
const int width = fm.averageCharWidth() * 72;
m_leLen.setText(QStringLiteral("0 / 52"));
m_pe.setPlaceholderText(i18n("Extended commit description..."));
m_pe.setFont(font);
QVBoxLayout *vlayout = new QVBoxLayout;
vlayout->setContentsMargins(0, 0, 0, 0);
setLayout(vlayout);
QHBoxLayout *hLayoutLine = new QHBoxLayout;
hLayoutLine->addStretch();
hLayoutLine->addWidget(&m_leLen);
vlayout->addLayout(hLayoutLine);
vlayout->addWidget(m_le);
vlayout->addWidget(&m_pe);
// set 72 chars wide plain text edit
m_pe.setFixedWidth(width);
this->setFixedHeight(fm.averageCharWidth() * 52);
// restore last message ?
if (!lastCommit.isEmpty()) {
auto msgs = lastCommit.split(QStringLiteral("[[\n\n]]"));
if (!msgs.isEmpty()) {
m_le->setText(msgs.at(0));
if (msgs.length() > 1) {
m_pe.setPlainText(msgs.at(1));
}
}
}
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch();
hLayout->addWidget(&ok);
hLayout->addWidget(&cancel);
connect(&ok, &QPushButton::clicked, this, &QDialog::accept);
connect(&cancel, &QPushButton::clicked, this, &QDialog::reject);
connect(m_le, &QPlainTextEdit::textChanged, this, &GitCommitDialog::updateLineSizeLabel);
updateLineSizeLabel();
// m_cbSeventyTwoLimit.setText(i18n("Automatically break description into 72 or less character lines"));
// m_cbSeventyTwoLimit.setChecked(true);
// vlayout->addWidget(&m_cbSeventyTwoLimit);
vlayout->addLayout(hLayout);
}
QString GitCommitDialog::subject() const
{
return m_le->toPlainText();
}
QString GitCommitDialog::description() const
{
return m_pe.toPlainText();
}
void GitCommitDialog::updateLineSizeLabel()
{
int len = m_le->textLength();
if (len < 52) {
m_leLen.setText(QStringLiteral("%1 / 52").arg(QString::number(len)));
} else {
m_leLen.setText(QStringLiteral("<span style=\"color:red;\">%1</span> / 52").arg(QString::number(len)));
}
}
#ifndef GITCOMMITDIALOG_H
#define GITCOMMITDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <memory>
class SingleLineEdit;
class BadLengthHighlighter;
class QFont;
class GitCommitDialog : public QDialog
{
Q_OBJECT
public:
explicit GitCommitDialog(const QString &lastCommit, const QFont &font, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
QString subject() const;
QString description() const;
private:
Q_SLOT void updateLineSizeLabel();
SingleLineEdit *m_le;
QPlainTextEdit m_pe;
QPushButton ok;
QPushButton cancel;
QLabel m_leLen;
QLabel m_peLen;
BadLengthHighlighter *m_hl;
};
#endif // GITCOMMITDIALOG_H
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "gitwidget.h"
#include "gitcommitdialog.h"
#include "gitstatusmodel.h"
#include "kateproject.h"
......@@ -360,56 +361,17 @@ void GitWidget::opencommitChangesDialog()
font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
}
QDialog dialog;
GitCommitDialog dialog(m_commitMessage, font);
dialog.setWindowFlags(Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
dialog.setWindowTitle(i18n("Commit Changes"));
QVBoxLayout vlayout;
QHBoxLayout hLayout;
dialog.setLayout(&vlayout);
QLineEdit le;
le.setPlaceholderText(i18n("Write commit message..."));
le.setFont(font);
QPlainTextEdit pe;
pe.setPlaceholderText(i18n("Extended commit description..."));
pe.setFont(font);
// restore last message ?
if (!m_commitMessage.isEmpty()) {
auto msgs = m_commitMessage.split(QStringLiteral("\n\n"));
if (!msgs.isEmpty()) {
le.setText(msgs.at(0));
if (msgs.length() > 1) {
pe.setPlainText(msgs.at(1));
}
}
}
vlayout.addWidget(&le);
vlayout.addWidget(&pe);
QPushButton ok(i18n("Ok"));
QPushButton cancel(i18n("Cancel"));
hLayout.addStretch();
hLayout.addWidget(&ok);
hLayout.addWidget(&cancel);
connect(&ok, &QPushButton::clicked, &dialog, &QDialog::accept);
connect(&cancel, &QPushButton::clicked, &dialog, &QDialog::reject);
vlayout.addLayout(&hLayout);
dialog.open();
int res = dialog.exec();
if (res == QDialog::Accepted) {
if (le.text().isEmpty()) {
if (dialog.subject().isEmpty()) {
return sendMessage(i18n("Commit message cannot be empty."), true);
}
commitChanges(le.text(), pe.toPlainText());
commitChanges(dialog.subject(), dialog.description());
} else {
m_commitMessage = le.text() + QStringLiteral("\n\n") + pe.toPlainText();
m_commitMessage = dialog.subject() + QStringLiteral("[[\n\n]]") + dialog.description();
}
}
......
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