mirror of
https://github.com/Murmele/Gittyup.git
synced 2024-09-11 02:35:41 +03:00
add possibility to import and export templates
This commit is contained in:
parent
e013ff2c94
commit
ba9d605734
@ -9,6 +9,14 @@
|
||||
#include <QVBoxLayout>
|
||||
#include <QSpacerItem>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFileDialog>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
namespace {
|
||||
const QString kTemplateFileExtension =
|
||||
QStringLiteral(".GittyupCommitMessageTemplate");
|
||||
}
|
||||
|
||||
TemplateDialog::TemplateDialog(QList<TemplateButton::Template> &templates,
|
||||
QWidget *parent)
|
||||
@ -81,11 +89,16 @@ TemplateDialog::TemplateDialog(QList<TemplateButton::Template> &templates,
|
||||
hBox->addLayout(vBox2);
|
||||
hBox->addLayout(vBox3);
|
||||
|
||||
// Import, export, ok, cancel
|
||||
auto importButton = new QPushButton(tr("Import"), this);
|
||||
auto exportButton = new QPushButton(tr("Export"), this);
|
||||
spacer =
|
||||
new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||
mButtonBox = new QDialogButtonBox(
|
||||
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
|
||||
hBox2 = new QHBoxLayout();
|
||||
hBox2->addWidget(importButton);
|
||||
hBox2->addWidget(exportButton);
|
||||
hBox2->addItem(spacer);
|
||||
hBox2->addWidget(mButtonBox);
|
||||
|
||||
@ -114,6 +127,8 @@ TemplateDialog::TemplateDialog(QList<TemplateButton::Template> &templates,
|
||||
connect(mName, &QLineEdit::textChanged, this, &TemplateDialog::checkName);
|
||||
connect(mTemplateList, &QListWidget::currentRowChanged, this,
|
||||
&TemplateDialog::showTemplate);
|
||||
connect(importButton, &QPushButton::pressed, [this] { importTemplates(); });
|
||||
connect(exportButton, &QPushButton::pressed, [this] { exportTemplates(); });
|
||||
}
|
||||
|
||||
void TemplateDialog::addTemplate() {
|
||||
@ -141,7 +156,7 @@ void TemplateDialog::addTemplate() {
|
||||
|
||||
void TemplateDialog::removeTemplate() {
|
||||
QListWidgetItem *itm = mTemplateList->currentItem();
|
||||
if (!itm) // not item selected
|
||||
if (!itm) // no item selected
|
||||
return;
|
||||
|
||||
mSupress = true;
|
||||
@ -220,6 +235,82 @@ void TemplateDialog::moveTemplateDown() {
|
||||
showTemplate(mTemplateList->currentRow());
|
||||
}
|
||||
|
||||
void TemplateDialog::importTemplates(QString filename) {
|
||||
if (filename.isEmpty()) {
|
||||
filename = QFileDialog::getOpenFileName(
|
||||
this, tr("Open File"), "/home",
|
||||
tr("Gittyup Templates (*%1)").arg(kTemplateFileExtension));
|
||||
}
|
||||
|
||||
mNew.clear();
|
||||
mTemplateList->clear();
|
||||
|
||||
QFile file(filename);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
while (!file.atEnd()) {
|
||||
QString line = file.readLine();
|
||||
line.remove(line.length() - 1, 1);
|
||||
const int index = line.indexOf(QStringLiteral(":"));
|
||||
if (index == -1)
|
||||
continue;
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
const QString name = line.sliced(0, index);
|
||||
if (index + 1 >= line.length())
|
||||
continue;
|
||||
QString value = line.sliced(index + 1);
|
||||
#else
|
||||
const auto list = line.split(QStringLiteral(":"));
|
||||
if (list.length() < 2)
|
||||
continue;
|
||||
const QString name = list.at(0);
|
||||
QString value;
|
||||
for (int i = 1; i < list.length() - 1; i++)
|
||||
value += QStringLiteral("%1:").arg(list.at(i));
|
||||
value += list.last();
|
||||
#endif
|
||||
value = value.replace(QStringLiteral("\\n"), QStringLiteral("\n"));
|
||||
value = value.replace(QStringLiteral("\\t"), QStringLiteral("\t"));
|
||||
TemplateButton::Template t;
|
||||
t.name = name;
|
||||
t.value = value;
|
||||
mNew.append(t);
|
||||
}
|
||||
}
|
||||
if (mNew.count() > 0) {
|
||||
for (const auto &t : mNew)
|
||||
mTemplateList->addItem(t.name);
|
||||
showTemplate(0);
|
||||
} else {
|
||||
mName->setText(QStringLiteral(""));
|
||||
mTemplate->setText(QStringLiteral(""));
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateDialog::exportTemplates(QString filename) {
|
||||
if (filename.isEmpty()) {
|
||||
filename = QFileDialog::getSaveFileName(
|
||||
this, tr("Save Templates"),
|
||||
QStringLiteral("/home/%1%2")
|
||||
.arg("GittyupTemplates", kTemplateFileExtension),
|
||||
tr("Gittyup Templates (*%1)").arg(kTemplateFileExtension));
|
||||
}
|
||||
|
||||
QString templatesStr;
|
||||
for (const auto tmpl : mNew) {
|
||||
QString name = tmpl.name;
|
||||
QString value = tmpl.value;
|
||||
value = value.replace(QStringLiteral("\n"), QStringLiteral("\\n"));
|
||||
value = value.replace(QStringLiteral("\t"), QStringLiteral("\\t"));
|
||||
templatesStr += QStringLiteral("%1:%2\n").arg(name, value);
|
||||
}
|
||||
|
||||
QFile file(filename);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
QTextStream stream(&file);
|
||||
stream << templatesStr;
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateDialog::applyTemplates() {
|
||||
mTemplates = mNew;
|
||||
accept();
|
||||
|
@ -14,7 +14,8 @@ class QDialogButtonBox;
|
||||
class TemplateDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
public:
|
||||
TemplateDialog(QList<TemplateButton::Template> &templates, QWidget *parent);
|
||||
TemplateDialog(QList<TemplateButton::Template> &templates,
|
||||
QWidget *parent = nullptr);
|
||||
|
||||
private:
|
||||
void addTemplate();
|
||||
@ -25,6 +26,8 @@ private:
|
||||
bool uniqueName(QString name);
|
||||
void checkName(QString name);
|
||||
void showTemplate(int idx);
|
||||
void importTemplates(QString filename = QStringLiteral(""));
|
||||
void exportTemplates(QString filename = QStringLiteral(""));
|
||||
|
||||
QPushButton *mUp; // moving template up
|
||||
QPushButton *mDown; // moving template down
|
||||
@ -40,6 +43,8 @@ private:
|
||||
QList<TemplateButton::Template> mNew;
|
||||
|
||||
bool mSupress{false};
|
||||
|
||||
friend class TestCommitMessageTemplate;
|
||||
};
|
||||
|
||||
#endif // TEMPLATEDIALOG_H
|
||||
|
@ -111,6 +111,7 @@ test(NAME amend)
|
||||
test(NAME SshConfig)
|
||||
test(NAME fileContextMenu NO_WIN32_OFFSCREEN)
|
||||
test(NAME Setting)
|
||||
test(NAME commitMessageTemplate)
|
||||
|
||||
option(GITTYUP_CI_TESTS "Run tests that change global settings" OFF)
|
||||
if(GITTYUP_CI_TESTS)
|
||||
|
200
test/commitMessageTemplate.cpp
Normal file
200
test/commitMessageTemplate.cpp
Normal file
@ -0,0 +1,200 @@
|
||||
#include "Test.h"
|
||||
|
||||
#include "ui/TemplateButton.h"
|
||||
#include "ui/TemplateDialog.h"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QTextEdit>
|
||||
#include <QListWidget>
|
||||
|
||||
class TestCommitMessageTemplate : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testImportExport();
|
||||
void testMoveUp();
|
||||
void testMoveDown();
|
||||
void testRemove();
|
||||
void testRemoveNoItemAvailable();
|
||||
};
|
||||
|
||||
void TestCommitMessageTemplate::testImportExport() {
|
||||
QList<TemplateButton::Template> templates;
|
||||
QTemporaryFile f;
|
||||
QVERIFY(f.open());
|
||||
QVERIFY(!f.fileName().isEmpty());
|
||||
|
||||
{
|
||||
TemplateDialog d(templates);
|
||||
|
||||
d.mName->setText(QStringLiteral("Template1"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate1:"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template2"));
|
||||
d.mTemplate->setPlainText(
|
||||
QStringLiteral("ContentTemplate2: %|\nfiles: ${files:3}\t"));
|
||||
d.addTemplate();
|
||||
d.exportTemplates(f.fileName());
|
||||
}
|
||||
|
||||
{
|
||||
TemplateDialog d(templates);
|
||||
QCOMPARE(d.mTemplates.length(), 0);
|
||||
QCOMPARE(d.mTemplateList->count(), 0);
|
||||
d.importTemplates(f.fileName());
|
||||
d.applyTemplates();
|
||||
QCOMPARE(d.mTemplates.length(), 2);
|
||||
QCOMPARE(d.mTemplates.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplates.at(0).value, QStringLiteral("ContentTemplate1:"));
|
||||
QCOMPARE(d.mTemplates.at(1).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplates.at(1).value,
|
||||
QStringLiteral("ContentTemplate2: %|\nfiles: ${files:3}\t"));
|
||||
|
||||
QCOMPARE(d.mTemplateList->count(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void TestCommitMessageTemplate::testMoveUp() {
|
||||
QList<TemplateButton::Template> templates;
|
||||
TemplateDialog d(templates);
|
||||
|
||||
d.mName->setText(QStringLiteral("Template1"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate1:"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template2"));
|
||||
d.mTemplate->setPlainText(
|
||||
QStringLiteral("ContentTemplate2: %|\nfiles: ${files:3}\t"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template3"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate3:"));
|
||||
d.addTemplate();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template3"));
|
||||
|
||||
d.mTemplateList->setCurrentRow(1);
|
||||
d.moveTemplateUp();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template3"));
|
||||
|
||||
d.mTemplateList->setCurrentRow(0);
|
||||
d.moveTemplateUp();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template3"));
|
||||
}
|
||||
|
||||
void TestCommitMessageTemplate::testMoveDown() {
|
||||
QList<TemplateButton::Template> templates;
|
||||
TemplateDialog d(templates);
|
||||
|
||||
d.mName->setText(QStringLiteral("Template1"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate1:"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template2"));
|
||||
d.mTemplate->setPlainText(
|
||||
QStringLiteral("ContentTemplate2: %|\nfiles: ${files:3}\t"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template3"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate3:"));
|
||||
d.addTemplate();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template3"));
|
||||
|
||||
d.mTemplateList->setCurrentRow(1);
|
||||
d.moveTemplateDown();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template2"));
|
||||
|
||||
d.mTemplateList->setCurrentRow(2);
|
||||
d.moveTemplateDown();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 3);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mNew.at(2).name, QStringLiteral("Template2"));
|
||||
QCOMPARE(d.mTemplateList->count(), 3);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->item(2)->text(), QStringLiteral("Template2"));
|
||||
}
|
||||
|
||||
void TestCommitMessageTemplate::testRemove() {
|
||||
QList<TemplateButton::Template> templates;
|
||||
TemplateDialog d(templates);
|
||||
|
||||
d.mName->setText(QStringLiteral("Template1"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate1:"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template2"));
|
||||
d.mTemplate->setPlainText(
|
||||
QStringLiteral("ContentTemplate2: %|\nfiles: ${files:3}\t"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mName->setText(QStringLiteral("Template3"));
|
||||
d.mTemplate->setPlainText(QStringLiteral("ContentTemplate3:"));
|
||||
d.addTemplate();
|
||||
|
||||
d.mTemplateList->setCurrentRow(1);
|
||||
d.removeTemplate();
|
||||
|
||||
QCOMPARE(d.mNew.count(), 2);
|
||||
QCOMPARE(d.mNew.at(0).name, QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mNew.at(1).name, QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplateList->count(), 2);
|
||||
QCOMPARE(d.mTemplateList->item(0)->text(), QStringLiteral("Template1"));
|
||||
QCOMPARE(d.mTemplateList->item(1)->text(), QStringLiteral("Template3"));
|
||||
|
||||
QCOMPARE(d.mName->text(), QStringLiteral("Template3"));
|
||||
QCOMPARE(d.mTemplate->toPlainText(), QStringLiteral("ContentTemplate3:"));
|
||||
}
|
||||
|
||||
void TestCommitMessageTemplate::testRemoveNoItemAvailable() {
|
||||
QList<TemplateButton::Template> templates;
|
||||
TemplateDialog d(templates);
|
||||
d.removeTemplate();
|
||||
|
||||
// Should not crash!
|
||||
}
|
||||
|
||||
TEST_MAIN(TestCommitMessageTemplate)
|
||||
#include "commitMessageTemplate.moc"
|
Loading…
Reference in New Issue
Block a user