File: tikzpreviewcontroller.cpp

package info (click to toggle)
ktikz 0.13.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,136 kB
  • sloc: cpp: 10,491; xml: 701; sh: 131; makefile: 19
file content (556 lines) | stat: -rw-r--r-- 21,609 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/***************************************************************************
 *   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014                      *
 *     by Glad Deschrijver <glad.deschrijver@gmail.com>                    *
 *   Copyright (C) 2016 by G. Prudhomme                                    *
 *     <gprud@users.noreply.github.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 "tikzpreviewcontroller.h"

#ifndef KTIKZ_USE_KDE
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets/QToolBar>
#else
#include <QtGui/QToolBar>
#endif
#endif

#include <QtCore/QSettings>
#include <QtCore/QTimer>
#include <QtCore/QPointer>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtPrintSupport/QPrintDialog>
#include <QtPrintSupport/QPrinter>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QMenu>
#include <QtWidgets/QPushButton>
#else
#include <QtGui/QDialogButtonBox>
#include <QtGui/QMenu>
#include <QtGui/QPrintDialog>
#include <QtGui/QPrinter>
#include <QtGui/QPushButton>
// #include <KConfigGroup> // JC
#endif

#include "templatewidget.h"
#include "tikzpreview.h"
#include "mainwidget.h"
#include "utils/action.h"
#include "utils/file.h"
#include "utils/filedialog.h"
#include "utils/icon.h"
#include "utils/messagebox.h"
#include "utils/printpreviewdialog.h"
#include "utils/standardaction.h"
#include "utils/tempdir.h"
#include "utils/toggleaction.h"

static const int s_minUpdateInterval = 1000; // 1 sec

TikzPreviewController::TikzPreviewController(MainWidget *mainWidget)
{
	m_mainWidget = mainWidget;
	m_parentWidget = m_mainWidget->widget();

	m_templateWidget = new TemplateWidget(m_parentWidget);

	m_tikzPreview = new TikzPreview(m_parentWidget);
	m_tikzPreviewGenerator = new TikzPreviewGenerator(this);

	createActions();

	qRegisterMetaType<QList<qreal> >("QList<qreal>");
	connect(m_tikzPreviewGenerator, SIGNAL(pixmapUpdated(Poppler::Document*,QList<qreal>)),
	        m_tikzPreview, SLOT(pixmapUpdated(Poppler::Document*,QList<qreal>)));
	connect(m_tikzPreviewGenerator, SIGNAL(showErrorMessage(QString)),
	        m_tikzPreview, SLOT(showErrorMessage(QString)));
	connect(m_tikzPreviewGenerator, SIGNAL(setExportActionsEnabled(bool)),
	        this, SLOT(setExportActionsEnabled(bool)));
	connect(m_tikzPreviewGenerator, SIGNAL(updateLog(QString,bool)),
	        this, SIGNAL(updateLog(QString,bool)));
	connect(m_tikzPreviewGenerator, SIGNAL(appendLog(QString,bool)),
	        this, SIGNAL(appendLog(QString,bool)));
	connect(m_templateWidget, SIGNAL(fileNameChanged(QString)),
	        this, SLOT(setTemplateFileAndRegenerate(QString)));
	connect(m_tikzPreview, SIGNAL(showMouseCoordinates(qreal,qreal,int,int)),
	        this, SIGNAL(showMouseCoordinates(qreal,qreal,int,int)));

	m_regenerateTimer = new QTimer(this);
	m_regenerateTimer->setSingleShot(true);
	connect(m_regenerateTimer, SIGNAL(timeout()),
	        this, SLOT(regeneratePreview()));

	m_tempDir = new TempDir();
	m_tikzPreviewGenerator->setTikzFileBaseName(tempFileBaseName());
#ifdef KTIKZ_USE_KDE
	File::setMainWidget(m_parentWidget);
	File::setTempDir(m_tempDir->path()); // this must happen before any object of type File is constructed
#endif
}

TikzPreviewController::~TikzPreviewController()
{
	delete m_tikzPreviewGenerator;
	delete m_tempDir;
}

/***************************************************************************/

const TextCodecProfile *TikzPreviewController::textCodecProfile() const {
	return (TextCodecProfile*) this->m_mainWidget;
}


/***************************************************************************/

const QString TikzPreviewController::tempDir() const
{
	return m_tempDir->path();
}

const QString TikzPreviewController::tempFileBaseName() const
{
	return m_tempDir->path() + QLatin1String("/temptikzcode");
}

const QString TikzPreviewController::tempDirLocation() const
{
	return m_tempDir->location();
}

/***************************************************************************/

TemplateWidget *TikzPreviewController::templateWidget() const
{
	return m_templateWidget;
}

TikzPreview *TikzPreviewController::tikzPreview() const
{
	return m_tikzPreview;
}

/***************************************************************************/

void TikzPreviewController::createActions()
{
	// File
	m_exportAction = new Action(Icon(QLatin1String("document-export")), tr("E&xport"), m_parentWidget, QLatin1String("file_export_as"));
	m_exportAction->setStatusTip(tr("Export image to various formats"));
	m_exportAction->setWhatsThis(tr("<p>Export image to various formats.</p>"));
	QMenu *exportMenu = new QMenu(m_parentWidget);
	m_exportAction->setMenu(exportMenu);

	Action *exportEpsAction = new Action(Icon(QLatin1String("image-x-eps")), tr("&Encapsulated PostScript (EPS)"), exportMenu, QLatin1String("file_export_eps"));
	exportEpsAction->setData(QLatin1String("image/x-eps"));
	exportEpsAction->setStatusTip(tr("Export to EPS"));
	exportEpsAction->setWhatsThis(tr("<p>Export to EPS.</p>"));
	connect(exportEpsAction, SIGNAL(triggered()), this, SLOT(exportImage()));
	exportMenu->addAction(exportEpsAction);

	Action *exportPdfAction = new Action(Icon(QLatin1String("application-pdf")), tr("&Portable Document Format (PDF)"), exportMenu, QLatin1String("file_export_pdf"));
	exportPdfAction->setData(QLatin1String("application/pdf"));
	exportPdfAction->setStatusTip(tr("Export to PDF"));
	exportPdfAction->setWhatsThis(tr("<p>Export to PDF.</p>"));
	connect(exportPdfAction, SIGNAL(triggered()), this, SLOT(exportImage()));
	exportMenu->addAction(exportPdfAction);

	QStringList mimeTypes;
	QStringList mimeTypeNames;
	mimeTypes << QLatin1String("png") << QLatin1String("jpeg") << QLatin1String("tiff") << QLatin1String("bmp");
	mimeTypeNames << tr("Portable Network &Graphics") << tr("&Joint Photographic Experts Group Format") << tr("&Tagged Image File Format") << tr("&Windows Bitmap");
	for (int i = 0; i < mimeTypes.size(); ++i)
	{
		Action *exportImageAction = new Action(Icon(QLatin1String("image-") + mimeTypes.at(i)), mimeTypeNames.at(i) + QLatin1String(" (") + mimeTypes.at(i).toUpper() + QLatin1Char(')'), exportMenu, QLatin1String("file_export_") + mimeTypes.at(i));
		exportImageAction->setData(QLatin1String("image/") + mimeTypes.at(i));
		exportImageAction->setStatusTip(tr("Export to %1").arg(mimeTypes.at(i).toUpper()));
		exportImageAction->setWhatsThis(tr("<p>Export to %1.</p>").arg(mimeTypes.at(i).toUpper()));
		connect(exportImageAction, SIGNAL(triggered()), this, SLOT(exportImage()));
		exportMenu->addAction(exportImageAction);
	}

#ifndef KTIKZ_KPART // don't have two "Print" actions in the kpart
	m_printPreviewAction = StandardAction::printPreview(this, SLOT(printPreviewImage()), this);
	m_printPreviewAction->setStatusTip(tr("Print preview image"));
	m_printPreviewAction->setWhatsThis(tr("<p>Show print preview of the preview image.</p>"));

	m_printAction = StandardAction::print(this, SLOT(printImage()), this);
	m_printAction->setStatusTip(tr("Print image"));
	m_printAction->setWhatsThis(tr("<p>Print the preview image.</p>"));
#endif

	setExportActionsEnabled(false);

	// View
	m_procStopAction = new Action(Icon(QLatin1String("process-stop")), tr("&Stop Process"), m_parentWidget, QLatin1String("stop_process"));
	m_procStopAction->setShortcut(tr("Escape", "View|Stop Process"));
	m_procStopAction->setStatusTip(tr("Abort current process"));
	m_procStopAction->setWhatsThis(tr("<p>Abort the execution of the currently running process.</p>"));
	m_procStopAction->setEnabled(false);
	connect(m_procStopAction, SIGNAL(triggered()), this, SLOT(abortProcess()));

	m_shellEscapeAction = new ToggleAction(Icon(QLatin1String("application-x-executable")), tr("S&hell Escape"), m_parentWidget, QLatin1String("shell_escape"));
	m_shellEscapeAction->setStatusTip(tr("Enable the \\write18{shell-command} feature"));
	m_shellEscapeAction->setWhatsThis(tr("<p>Enable LaTeX to run shell commands, this is needed when you want to plot functions using gnuplot within TikZ."
	                                     "</p><p><strong>Warning:</strong> Enabling this may cause malicious software to be run on your computer! Check the LaTeX code to see which commands are executed.</p>"));
	connect(m_shellEscapeAction, SIGNAL(toggled(bool)), this, SLOT(toggleShellEscaping(bool)));

	connect(m_tikzPreviewGenerator, SIGNAL(processRunning(bool)),
	        this, SLOT(setProcessRunning(bool)));
}

#ifndef KTIKZ_USE_KDE
QAction *TikzPreviewController::exportAction()
{
	return m_exportAction;
}

QAction *TikzPreviewController::printPreviewAction()
{
	return m_printPreviewAction;
}

QAction *TikzPreviewController::printAction()
{
	return m_printAction;
}

QMenu *TikzPreviewController::menu()
{
	QMenu *viewMenu = new QMenu(tr("&View"), m_parentWidget);
	viewMenu->addActions(m_tikzPreview->actions());
	viewMenu->addSeparator();
	viewMenu->addAction(m_procStopAction);
	viewMenu->addAction(m_shellEscapeAction);
	return viewMenu;
}

QList<QToolBar*> TikzPreviewController::toolBars()
{
	QToolBar *toolBar = new QToolBar(tr("Run"), m_parentWidget);
	toolBar->setObjectName(QLatin1String("RunToolBar"));
	toolBar->addAction(m_procStopAction);
	toolBar->addAction(m_shellEscapeAction);

	m_toolBars << m_tikzPreview->toolBar() << toolBar;

	return m_toolBars;
}

void TikzPreviewController::setToolBarStyle(const Qt::ToolButtonStyle &style)
{
	for (int i = 0; i < m_toolBars.size(); ++i)
		m_toolBars.at(i)->setToolButtonStyle(style);
}
#endif

/***************************************************************************/

Url TikzPreviewController::getExportUrl(const Url &url, const QString &mimeType) const
{
	QString currentFile;
	const QString extension = mimeType == QLatin1String("image/x-eps") ? QLatin1String("eps")
	                          : (mimeType == QLatin1String("application/pdf") ? QLatin1String("pdf") : mimeType.mid(6));
	if (!url.isEmpty())
	{
		const QFileInfo currentFileInfo(url.path());
		currentFile = currentFileInfo.absolutePath();
		if (!currentFile.endsWith(QLatin1Char('/')))
			currentFile += QLatin1Char('/');
		currentFile += currentFileInfo.completeBaseName()
		               + (m_tikzPreview->numberOfPages() > 1 && mimeType != QLatin1String("application/pdf") ? QLatin1String("_") + QString::number(m_tikzPreview->currentPage() + 1) : QString())
		               + QLatin1Char('.') + extension;
	}
	return FileDialog::getSaveUrl(m_parentWidget, tr("Export image"), Url(currentFile), mimeType);
}

void TikzPreviewController::exportImage()
{
	QAction *action = qobject_cast<QAction*>(sender());
	const QString mimeType = action->data().toString();

	const QPixmap tikzImage = m_tikzPreview->pixmap();
	if (tikzImage.isNull())
		return;

	const Url exportUrl = getExportUrl(m_mainWidget->url(), mimeType);
	if (!exportUrl.isValid())
		return;

	QString extension;
	if (mimeType == QLatin1String("application/pdf"))
	{
		extension = QLatin1String(".pdf");
	}
	else if (mimeType == QLatin1String("image/x-eps"))
	{
		if (!m_tikzPreviewGenerator->generateEpsFile(m_tikzPreview->currentPage()))
		{
			MessageBox::error(m_parentWidget, tr("Export failed."), QCoreApplication::applicationName());
			return;
		}
		extension = QLatin1String(".eps");
	}
	else
	{
		extension = QLatin1Char('.') + mimeType.mid(6);
		if (!tikzImage.save(tempFileBaseName() + extension))
		{
			MessageBox::error(m_parentWidget, tr("Export failed."), QCoreApplication::applicationName());
			return;
		}
	}

	if (!File::copy(Url(tempFileBaseName() + extension), exportUrl))
		MessageBox::error(m_parentWidget,
		                  tr("The image could not be exported to the file \"%1\".").arg(exportUrl.path()),
		                  QCoreApplication::applicationName());
}

/***************************************************************************/

void TikzPreviewController::printImage(QPrinter *printer)
{
	// get page range
	int startPage, endPage;
	if (printer->printRange() == QPrinter::PageRange)
	{
		startPage = printer->fromPage() - 1;
		endPage = printer->toPage() - 1;
	}
	else if (printer->printRange() == QPrinter::CurrentPage)
	{
		startPage = m_tikzPreview->currentPage();
		endPage = m_tikzPreview->currentPage();
	}
	else
	{
		startPage = 0;
		endPage = m_tikzPreview->numberOfPages() - 1;
	}

	// print
	QPainter painter;
	painter.begin(printer);
//	painter.drawPixmap(0, 0, m_tikzPreview->pixmap());
	for (int i = startPage; i <= endPage; ++i)
	{
		if (i != startPage)
			printer->newPage();
		const QImage image = m_tikzPreview->renderToImage(printer->physicalDpiX(), printer->physicalDpiY(), i);
		if (!image.isNull())
		{
			const double scaleFactor = qMin(double(painter.window().width()) / image.width(), double(painter.window().height()) / image.height());
			painter.drawImage(QRect(0, 0, image.width() * scaleFactor, image.height() * scaleFactor), image, image.rect());
		}
	}
	painter.end();
}

void TikzPreviewController::printPreviewImage()
{
	QPrinter printer;

	// choose printer
	QPointer<QPrintDialog> printDialog = new QPrintDialog(&printer, m_parentWidget);
	printDialog->setWindowTitle(tr("Print preview of image"));
	printDialog->setOptions(printDialog->options() | QAbstractPrintDialog::PrintPageRange | QAbstractPrintDialog::PrintCurrentPage);
	printDialog->setMinMax(0, m_tikzPreview->numberOfPages());
	QList<QDialogButtonBox*> dialogButtonBoxes = printDialog->findChildren<QDialogButtonBox*>();
	QPushButton *printButton = dialogButtonBoxes.at(0)->button(QDialogButtonBox::Ok);
	printButton->setText(tr("Print &preview"));
	if (printDialog->exec() != QDialog::Accepted)
	{
		delete printDialog;
		return;
	}
	delete printDialog;

	// show print preview
	PrintPreviewDialog preview(&printer);
	connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printImage(QPrinter*)));
	preview.exec();
}

void TikzPreviewController::printImage()
{
	QPrinter printer(QPrinter::PrinterResolution);

	// choose printer
	QPointer<QPrintDialog> printDialog = new QPrintDialog(&printer, m_parentWidget);
	printDialog->setWindowTitle(tr("Print image"));
	printDialog->setOptions(printDialog->options() | QAbstractPrintDialog::PrintPageRange | QAbstractPrintDialog::PrintCurrentPage);
	printDialog->setMinMax(0, m_tikzPreview->numberOfPages());
	if (printDialog->exec() != QDialog::Accepted)
	{
		delete printDialog;
		return;
	}
	delete printDialog;

	printImage(&printer);
}

/***************************************************************************/

bool TikzPreviewController::setTemplateFile(const QString &path)
{
	File templateFile(path, File::ReadOnly);
	if (templateFile.file()->exists()) // use local copy of template file if template file is remote
		m_tikzPreviewGenerator->setTemplateFile(templateFile.file()->fileName());
	else
		m_tikzPreviewGenerator->setTemplateFile(QString());
	return true;
}

void TikzPreviewController::setTemplateFileAndRegenerate(const QString &path)
{
	if (setTemplateFile(path))
		generatePreview(TikzPreviewGenerator::ReloadTemplate);
}

void TikzPreviewController::setReplaceTextAndRegenerate(const QString &replace)
{
	m_tikzPreviewGenerator->setReplaceText(replace);
	generatePreview(TikzPreviewGenerator::ReloadTemplate);
}

/***************************************************************************/

QString TikzPreviewController::tikzCode() const
{
	return m_mainWidget->tikzCode();
}

QString TikzPreviewController::getLogText()
{
	return m_tikzPreviewGenerator->getLogText();
}

void TikzPreviewController::generatePreview()
{
	generatePreview(TikzPreviewGenerator::ReloadTemplate);
}

void TikzPreviewController::generatePreview(TikzPreviewGenerator::TemplateStatus templateStatus)
{
	if (templateStatus == TikzPreviewGenerator::ReloadTemplate) // old aux files may contain commands available in the old template, but not anymore in the new template
		m_tempDir->cleanUp();

	// the directory in which the pgf file is located is added to TEXINPUTS (and the directory of the old pgf file is removed) before running latex
	const QString currentFileName = m_mainWidget->url().path();
	if (!m_currentFileName.isEmpty() && currentFileName != m_currentFileName)
		m_tikzPreviewGenerator->removeFromLatexSearchPath(QFileInfo(m_currentFileName).absolutePath());
	m_currentFileName = currentFileName;
	if (!currentFileName.isEmpty())
		m_tikzPreviewGenerator->addToLatexSearchPath(QFileInfo(currentFileName).absolutePath());

	m_tikzPreviewGenerator->abortProcess(); // abort still running process before starting a new one (without this, if a process hangs, all subsequently started processes are executed one after the other when the user aborts the hanging process)
	m_tikzPreviewGenerator->generatePreview(templateStatus);
}

void TikzPreviewController::regeneratePreview()
{
	generatePreview(TikzPreviewGenerator::DontReloadTemplate);
}

void TikzPreviewController::regeneratePreviewAfterDelay()
{
	if (tikzCode().isEmpty())
	{
		m_tikzPreview->pixmapUpdated(0); // clean up error messages in preview
		Q_EMIT updateLog(QString(), false); // clean up error messages in log panel
	}
	// Each start cancels the previous one, this means that timeout() is only
	// fired when there have been no changes in the text editor for the last
	// s_minUpdateInterval msecs. This ensures that the preview is not
	// regenerated on every character that is added/changed/removed.
	m_regenerateTimer->start(s_minUpdateInterval);
}

void TikzPreviewController::emptyPreview()
{
	setExportActionsEnabled(false);
	m_tikzPreviewGenerator->abortProcess(); // abort still running processes
	m_tikzPreview->emptyPreview();
}

void TikzPreviewController::abortProcess()
{
	// We cannot connect the triggered signal of m_procStopAction directly
	// to m_tikzPreviewGenerator->abortProcess() because then the latter would
	// be run in the same thread as the process which must be aborted, so the
	// abortion would be executed after the process finishes.  So we must abort
	// the process in the main thread by calling m_tikzPreviewGenerator->abortProcess()
	// as a regular (non-slot) function.
	m_tikzPreviewGenerator->abortProcess();
}

/***************************************************************************/

void TikzPreviewController::applySettings()
{
	QSettings settings(QString::fromLocal8Bit(ORGNAME), QString::fromLocal8Bit(APPNAME));
	m_tikzPreviewGenerator->setLatexCommand(settings.value(QLatin1String("LatexCommand"), QLatin1String("pdflatex")).toString());
	m_tikzPreviewGenerator->setPdftopsCommand(settings.value(QLatin1String("PdftopsCommand"), QLatin1String("pdftops")).toString());
	const bool useShellEscaping = settings.value(QLatin1String("UseShellEscaping"), false).toBool();

	disconnect(m_shellEscapeAction, SIGNAL(toggled(bool)), this, SLOT(toggleShellEscaping(bool)));
	m_shellEscapeAction->setChecked(useShellEscaping);
	m_tikzPreviewGenerator->setShellEscaping(useShellEscaping);
	connect(m_shellEscapeAction, SIGNAL(toggled(bool)), this, SLOT(toggleShellEscaping(bool)));

	setTemplateFile(settings.value(QLatin1String("TemplateFile")).toString());
	const QString replaceText = settings.value(QLatin1String("TemplateReplaceText"), QLatin1String("<>")).toString();
	m_tikzPreviewGenerator->setReplaceText(replaceText);
	m_templateWidget->setReplaceText(replaceText);
	m_templateWidget->setEditor(settings.value(QLatin1String("TemplateEditor"), QLatin1String("")).toString());

	settings.beginGroup(QLatin1String("Preview"));
	m_tikzPreview->setShowCoordinates(settings.value(QLatin1String("ShowCoordinates"), true).toBool());
	m_tikzPreview->setCoordinatePrecision(settings.value(QLatin1String("ShowCoordinatesPrecision"), -1).toInt());
	m_tikzPreview->setBackgroundColor(settings.value(QLatin1String("PreviewBackgroundColor"), QColor(0, 0, 0)).value<QColor>());
	settings.endGroup();
}

void TikzPreviewController::setExportActionsEnabled(bool enabled)
{
	m_exportAction->setEnabled(enabled);
#ifndef KTIKZ_KPART
	m_printPreviewAction->setEnabled(enabled);
	m_printAction->setEnabled(enabled);
#endif
}

void TikzPreviewController::setProcessRunning(bool isRunning)
{
	m_procStopAction->setEnabled(isRunning);
	if (isRunning)
		QApplication::setOverrideCursor(Qt::BusyCursor);
	else
		QApplication::restoreOverrideCursor();
	m_tikzPreview->setProcessRunning(isRunning);
}

void TikzPreviewController::toggleShellEscaping(bool useShellEscaping)
{
	QSettings settings(QString::fromLocal8Bit(ORGNAME), QString::fromLocal8Bit(APPNAME));
	settings.setValue(QLatin1String("UseShellEscaping"), useShellEscaping);

	m_tikzPreviewGenerator->setShellEscaping(useShellEscaping);
	generatePreview(TikzPreviewGenerator::DontReloadTemplate);
}