gitcommitdialog.cpp 3.93 KB
Newer Older
Waqar Ahmed's avatar
Waqar Ahmed committed
1 2 3 4 5
/*
    SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/
Waqar Ahmed's avatar
Waqar Ahmed committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#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);
47
        setFixedHeight(le.sizeHint().height() - QFontMetrics(font).descent());
Waqar Ahmed's avatar
Waqar Ahmed committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        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;
88
    vlayout->setContentsMargins(4, 4, 4, 4);
Waqar Ahmed's avatar
Waqar Ahmed committed
89 90 91 92 93 94 95 96 97 98 99
    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
100 101
    m_pe.resize(width, m_pe.height());
    resize(width, fm.averageCharWidth() * 52);
Waqar Ahmed's avatar
Waqar Ahmed committed
102 103 104 105 106 107 108 109 110 111 112 113

    // 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));
            }
        }
    }

114 115 116 117
    m_cbSignOff.setChecked(false);
    m_cbSignOff.setText(i18n("Sign off"));
    vlayout->addWidget(&m_cbSignOff);

Waqar Ahmed's avatar
Waqar Ahmed committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    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();

    vlayout->addLayout(hLayout);
}

QString GitCommitDialog::subject() const
{
    return m_le->toPlainText();
}

QString GitCommitDialog::description() const
{
    return m_pe.toPlainText();
}

142 143 144 145 146
bool GitCommitDialog::signoff() const
{
    return m_cbSignOff.isChecked();
}

Waqar Ahmed's avatar
Waqar Ahmed committed
147 148 149 150 151 152 153 154 155
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)));
    }
}