File: templatewidget.cpp

package info (click to toggle)
ktikz 0.10-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,676 kB
  • ctags: 879
  • sloc: cpp: 7,513; xml: 416; perl: 394; sh: 162; makefile: 28
file content (174 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download | duplicates (2)
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
 *   Copyright (C) 2008 by Glad Deschrijver                                *
 *     <glad.deschrijver@gmail.com>                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "templatewidget.h"

#include <QApplication>
#include <QComboBox>
#include <QCompleter>
#include <QDirModel>
#include <QLineEdit>
#include <QFileDialog>
#include <QKeyEvent>
#include <QProcess>
#include <QSettings>

#include "utils/filedialog.h"
#include "utils/icon.h"
#include "utils/lineedit.h"
#include "utils/url.h"

TemplateWidget::TemplateWidget(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
	ui.templateCombo->setLineEdit(new LineEdit(this));
	ui.templateCombo->setMinimumContentsLength(20);
	ui.templateChooseButton->setIcon(Icon("document-open"));
	ui.templateReloadButton->setIcon(Icon("view-refresh"));

	QCompleter *completer = new QCompleter(this);
	completer->setModel(new QDirModel(completer));
	completer->setCompletionMode(QCompleter::PopupCompletion);
	ui.templateCombo->setCompleter(completer);

	connect(ui.templateChooseButton, SIGNAL(clicked()),
	        this, SLOT(setTemplateFile()));
	connect(ui.templateEditButton, SIGNAL(clicked()),
	        this, SLOT(editTemplateFile()));
	connect(ui.templateReloadButton, SIGNAL(clicked()),
	        this, SLOT(reloadTemplateFile()));
	connect(ui.templateCombo->lineEdit(), SIGNAL(textChanged(QString)),
	        this, SIGNAL(fileNameChanged(QString)));

	readRecentTemplates();
}

TemplateWidget::~TemplateWidget()
{
	saveRecentTemplates();
}

void TemplateWidget::readRecentTemplates()
{
	QSettings settings(ORGNAME, APPNAME);
	ui.templateCombo->setMaxCount(settings.value("TemplateRecentNumber", 10).toInt());
	const QStringList templateRecentList = settings.value("TemplateRecent").toStringList();
	ui.templateCombo->addItems(templateRecentList);
	const int index = templateRecentList.indexOf(settings.value("TemplateFile").toString());
	ui.templateCombo->setCurrentIndex((index >= 0) ? index : 0);
}

void TemplateWidget::saveRecentTemplates()
{
	QSettings settings(ORGNAME, APPNAME);
	QStringList recentTemplates;
	for (int i = 0; i < ui.templateCombo->count(); ++i)
		recentTemplates << ui.templateCombo->itemText(i);
	settings.setValue("TemplateRecent", recentTemplates);
	settings.setValue("TemplateFile", ui.templateCombo->lineEdit()->text());
}

void TemplateWidget::setFileName(const QString &fileName)
{
	disconnect(ui.templateCombo->lineEdit(), SIGNAL(textChanged(QString)),
	        this, SIGNAL(fileNameChanged(QString)));
	const int index = ui.templateCombo->findText(fileName);
	if (index >= 0) // then remove item in order to re-add it at the top
		ui.templateCombo->removeItem(index);
	ui.templateCombo->insertItem(0, fileName);
	ui.templateCombo->lineEdit()->setText("");
	connect(ui.templateCombo->lineEdit(), SIGNAL(textChanged(QString)),
	        this, SIGNAL(fileNameChanged(QString)));
	ui.templateCombo->setCurrentIndex(0);
}

void TemplateWidget::setReplaceText(const QString &replace)
{
	QString replaceText = replace;
	replaceText.replace('&', QLatin1String("&amp;"));
	replaceText.replace('<', QLatin1String("&lt;"));
	replaceText.replace('>', QLatin1String("&gt;"));
	const QString templateDescription(tr("<p>The template contains the code "
	    "of a complete LaTeX document in which the TikZ picture will be "
	    "included and which will be typesetted to produce the preview "
	    "image.  The string %1 in the template will be replaced by the "
	    "TikZ code.</p>").arg(replaceText));
	ui.templateCombo->setWhatsThis(tr("<p>Give the file name of the LaTeX "
	    "template.  If this input field is empty or contains an invalid "
	    "file name, an internal default template will be used.</p>")
	    + templateDescription);
	ui.templateLabel->setWhatsThis(ui.templateCombo->whatsThis());
	ui.templateEditButton->setWhatsThis(tr("<p>Edit this template with "
	    "an external editor specified in the \"Configure\" dialog.</p>")
	    + templateDescription);
}

void TemplateWidget::setEditor(const QString &editor)
{
	m_editor = editor;
}

QString TemplateWidget::fileName() const
{
	return ui.templateCombo->currentText();
}

void TemplateWidget::setTemplateFile()
{
	QString currentFileName = ui.templateCombo->currentText();
#ifdef KTIKZ_TEMPLATES_INSTALL_DIR
	if (currentFileName.isEmpty() && QFileInfo(KTIKZ_TEMPLATES_INSTALL_DIR).isDir())
		currentFileName = KTIKZ_TEMPLATES_INSTALL_DIR;
#endif
	const Url url = FileDialog::getOpenUrl(this,
	    tr("Select a template file"), Url(currentFileName),
	    QString("*.pgs *.tex|%1\n*|%2")
	    .arg(tr("%1 template files").arg(APPNAME))
	    .arg(tr("All files")));
	if (url.isValid())
		setFileName(url.pathOrUrl());
}

void TemplateWidget::editTemplateFile()
{
	QApplication::setOverrideCursor(Qt::WaitCursor);

	QStringList editorArguments;
	editorArguments << ui.templateCombo->currentText();

	QProcess process;
	process.startDetached(m_editor, editorArguments);

	QApplication::restoreOverrideCursor();

//	reloadTemplateFile();
}

void TemplateWidget::reloadTemplateFile()
{
	setFileName(fileName());
}

void TemplateWidget::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Return)
		setFileName(ui.templateCombo->currentText());
	if (event->key() == Qt::Key_Escape || event->key() == Qt::Key_Return)
		emit focusEditor();
	QWidget::keyPressEvent(event);
}