File: maketemplatedialog.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (61 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download
1
2
3
4
5
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "maketemplatedialog.h"
#include "ui_maketemplatedialog.h"
#include "smallUsefulFunctions.h"

MakeTemplateDialog::MakeTemplateDialog(QString templateDir, QString editorFilename, QWidget *parent) :
	QDialog(parent),
	ui(new Ui::MakeTemplateDialog),
	m_templateDir(templateDir),
	m_editorFilename(editorFilename)
{
	ui->setupUi(this);

	connect(ui->buttonBox, SIGNAL(accepted()), SLOT(tryAccept()));
	connect(ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));

	ui->leAuthor->setText(getUserName());
	ui->leVersion->setText("1.0");
	ui->cbLicense->clearEditText();
}

MakeTemplateDialog::~MakeTemplateDialog()
{
	delete ui;
}

void MakeTemplateDialog::tryAccept()
{
	QString fn = ui->leName->text();
	QString invalidChars = "\\/:*?\"<>|"; // for windows, OSX and linux is less restrictive but we use this to guarantee compatibility
	foreach (const QChar &c, invalidChars)
		fn.remove(c);
	if (fn.length() > 80) {
		fn.remove(80);
	}
	fn.prepend("template_");
	QString ext = QFileInfo(m_editorFilename).completeSuffix();
	if (ext.isEmpty()) {
		ext = "tex";
	}
	fn.append("." + ext);
	m_suggestedFile = QFileInfo(QDir(m_templateDir), fn);
	if (m_suggestedFile.exists()) {
		bool overwrite = txsConfirmWarning(tr("A template with the given name already exists.\nDo you want to overwrite it?") + "\n" + m_suggestedFile.canonicalFilePath());
		if (!overwrite)
			return;
	}
	accept();
}

QString MakeTemplateDialog::generateMetaData()
{
	QString s = "{\n";
	s += formatJsonStringParam("Name", ui->leName->text(), 13) + ",\n";
	s += formatJsonStringParam("Author", ui->leAuthor->text(), 13) + ",\n";
	s += formatJsonStringParam("Date", QDate::currentDate().toString(Qt::ISODate), 13) + ",\n";
	s += formatJsonStringParam("Version", ui->leVersion->text(), 13) + ",\n";
	s += formatJsonStringParam("Description", ui->leDescription->toPlainText(), 13) + ",\n";
	s += formatJsonStringParam("License", ui->cbLicense->currentText(), 13) + "\n"; // last entry does not have colon
	s += "}";
	return s;
}