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
|
/***************************************************************************
* Copyright (C) 2008, 2009, 2010 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 "tikzpreviewcontroller.h"
#ifdef KTIKZ_USE_KDE
#include <KDebug>
#include <KFileDialog>
#include <KMessageBox>
#include <KTempDir>
#include <KIO/Job>
#include <KIO/JobUiDelegate>
#include <KIO/NetAccess>
#else
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QTemporaryFile>
#include <QToolBar>
#include <QToolButton>
#endif
#include <QMenu>
#include <QPointer>
#include <QSettings>
#include "templatewidget.h"
#include "tikzpreview.h"
#include "tikzpreviewgenerator.h"
#include "mainwidget.h"
#include "utils/action.h"
#include "utils/icon.h"
#include "utils/toggleaction.h"
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();
connect(m_tikzPreviewGenerator, SIGNAL(pixmapUpdated(Poppler::Document*)),
m_tikzPreview, SLOT(pixmapUpdated(Poppler::Document*)));
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(shortLogUpdated(QString,bool)),
this, SIGNAL(logUpdated(QString,bool)));
connect(m_templateWidget, SIGNAL(fileNameChanged(QString)),
this, SLOT(setTemplateFileAndRegenerate(QString)));
// connect(m_templateWidget, SIGNAL(replaceTextChanged(QString)),
// this, SLOT(setReplaceTextAndRegenerate(QString)));
createTempDir();
// applySettings(); // must be done after creation of m_tempDir
}
TikzPreviewController::~TikzPreviewController()
{
delete m_tikzPreviewGenerator;
removeTempDir();
}
/***************************************************************************/
void TikzPreviewController::createTempDir()
{
#ifdef KTIKZ_USE_KDE
m_tempDir = new KTempDir();
m_tempDir->setAutoRemove(true);
m_tempTikzFileBaseName = m_tempDir->name() + "temptikzcode";
#else
QDir tempDir(QDir::tempPath() + "/ktikz");
if (!tempDir.exists())
QDir::temp().mkdir("ktikz");
QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + "/ktikz/temptikzcodeXXXXXX.tex");
if (tempFile->open())
{
const QFileInfo tempFileInfo = QFileInfo(*tempFile);
m_tempTikzFileBaseName = tempFileInfo.absolutePath() + '/' + tempFileInfo.completeBaseName();
}
else
qCritical() << "Error: could not create temporary file in" << QDir::toNativeSeparators(QDir::tempPath() + "/ktikz/");
delete tempFile;
// m_tempTikzFileBaseName = QDir::tempPath() + "/ktikz/temptikzcode";
#endif
m_tikzPreviewGenerator->setTikzFileBaseName(m_tempTikzFileBaseName);
}
void TikzPreviewController::removeTempDir()
{
#ifdef KTIKZ_USE_KDE
delete m_tempDir;
#else
// removing all temporary files
qDebug("removing tempfiles");
if (!m_tempTikzFileBaseName.isEmpty() && !cleanUp())
qCritical("Error: removing tempfiles failed");
// remove temp dir if empty
QDir tempDir(QDir::tempPath() + "/ktikz");
if (tempDir.exists())
QDir::temp().rmdir("ktikz");
#endif
}
const QString TikzPreviewController::tempDir() const
{
#ifdef KTIKZ_USE_KDE
return m_tempDir->name();
#else
return QDir::tempPath() + "/ktikz";
#endif
}
/***************************************************************************/
TemplateWidget *TikzPreviewController::templateWidget() const
{
return m_templateWidget;
}
TikzPreview *TikzPreviewController::tikzPreview() const
{
return m_tikzPreview;
}
/***************************************************************************/
void TikzPreviewController::createActions()
{
// File
m_exportAction = new Action(Icon("document-export"), tr("E&xport"), m_parentWidget, "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("image-x-eps"), tr("&Encapsulated PostScript (EPS)"), m_parentWidget, "file_export_eps");
exportEpsAction->setData("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("application-pdf"), tr("&Portable Document Format (PDF)"), m_parentWidget, "file_export_pdf");
exportPdfAction->setData("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);
Action *exportPngAction = new Action(Icon("image-png"), tr("Portable Network &Graphics (PNG)"), m_parentWidget, "file_export_png");
exportPngAction->setData("image/png");
exportPngAction->setStatusTip(tr("Export to PNG"));
exportPngAction->setWhatsThis(tr("<p>Export to PNG.</p>"));
connect(exportPngAction, SIGNAL(triggered()), this, SLOT(exportImage()));
exportMenu->addAction(exportPngAction);
setExportActionsEnabled(false);
// View
m_procStopAction = new Action(Icon("process-stop"), tr("&Stop Process"), m_parentWidget, "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()), m_tikzPreviewGenerator, SLOT(abortProcess()));
m_shellEscapeAction = new ToggleAction(Icon("application-x-executable"), tr("S&hell Escape"), m_parentWidget, "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;
}
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("RunToolBar");
toolBar->addAction(m_procStopAction);
m_shellEscapeButton = new QToolButton(m_parentWidget);
m_shellEscapeButton->setDefaultAction(m_shellEscapeAction);
m_shellEscapeButton->setCheckable(true);
toolBar->addWidget(m_shellEscapeButton);
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);
m_shellEscapeButton->setToolButtonStyle(style);
}
#endif
/***************************************************************************/
#ifdef KTIKZ_USE_KDE
void TikzPreviewController::showJobError(KJob *job)
{
if (job->error() != 0)
{
KIO::JobUiDelegate *ui = static_cast<KIO::Job*>(job)->ui();
if (!ui)
{
kError() << "Saving failed; job->ui() is null.";
return;
}
ui->setWindow(m_parentWidget);
ui->showErrorMessage();
}
}
KUrl TikzPreviewController::getExportUrl(const KUrl &url, const QString &mimeType) const
{
KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(mimeType);
const QString exportUrlExtension = KMimeType::extractKnownExtension(url.path());
const KUrl exportUrl = KUrl(url.url().left(url.url().length()
- (exportUrlExtension.isEmpty() ? 0 : exportUrlExtension.length() + 1)) // the extension is empty when the text/x-pgf mimetype is not correctly installed or when the file does not have a correct extension
+ mimeTypePtr->patterns().at(0).mid(1)); // first extension in the list of possible extensions (without *)
return KFileDialog::getSaveUrl(exportUrl,
mimeTypePtr->patterns().join(" ") + '|'
// + mimeTypePtr->comment() + "\n*|" + i18nc("@item:inlistbox filter", "All files"),
+ mimeTypePtr->comment() + "\n*|" + tr("All files"),
m_parentWidget,
// i18nc("@title:window", "Export Image"),
tr("Export Image"),
KFileDialog::ConfirmOverwrite);
}
#else
QString TikzPreviewController::getExportFileName(const QString &fileName, const QString &mimeType) const
{
QString currentFile;
const QString extension = (mimeType == QLatin1String("image/x-eps")) ? "eps"
: ((mimeType == QLatin1String("application/pdf")) ? "pdf" : "png");
const QString mimeTypeName = (mimeType == QLatin1String("image/x-eps")) ? tr("EPS image")
: ((mimeType == QLatin1String("application/pdf")) ? tr("PDF document") : tr("PNG image"));
if (!fileName.isEmpty())
{
QFileInfo currentFileInfo(fileName);
currentFile = currentFileInfo.absolutePath() + '/' + currentFileInfo.completeBaseName() + '.' + extension;
}
const QString filter = QString("%1 (*.%2);;%3 (*.*)")
.arg(mimeTypeName)
.arg(extension)
.arg(tr("All files"));
return QFileDialog::getSaveFileName(m_parentWidget, tr("Export image"), currentFile, filter);
}
#endif
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;
#ifdef KTIKZ_USE_KDE
const KUrl exportUrl = getExportUrl(m_mainWidget->url(), mimeType);
if (!exportUrl.isValid())
return;
#else
const QString exportFileName = getExportFileName(m_mainWidget->url().path(), mimeType);
if (exportFileName.isEmpty())
return;
QFileInfo exportFileInfo(exportFileName);
if (exportFileInfo.exists())
{
QPointer<QMessageBox> warningBox = new QMessageBox(m_parentWidget);
warningBox->setWindowTitle(QCoreApplication::applicationName());
warningBox->setText(tr("A file named \"%1\" already exists. "
"Are you sure you want to overwrite it?").arg(exportFileInfo.fileName()));
warningBox->setIcon(QMessageBox::Warning);
QPushButton *overwriteButton = warningBox->addButton(tr("&Overwrite", "Do you want to overwrite an existing file - warning box"), QMessageBox::AcceptRole);
QPushButton *cancelButton = warningBox->addButton(tr("&Cancel", "Do you want to overwrite an existing file - warning box"), QMessageBox::RejectRole);
Q_UNUSED(overwriteButton)
warningBox->exec();
if (!warningBox || warningBox->clickedButton() == cancelButton)
{
delete warningBox;
return;
}
else if (!QFile::remove(exportFileName))
{
QMessageBox::critical(m_parentWidget, QCoreApplication::applicationName(),
tr("The file \"%1\" could not be overwritten.").arg(exportFileName));
delete warningBox;
return;
}
delete warningBox;
}
#endif
QString extension;
if (mimeType == QLatin1String("application/pdf"))
{
extension = ".pdf";
}
else if (mimeType == QLatin1String("image/x-eps"))
{
if (!m_tikzPreviewGenerator->generateEpsFile())
return;
extension = ".eps";
}
else if (mimeType == QLatin1String("image/png"))
{
extension = ".png";
tikzImage.save(m_tempTikzFileBaseName + extension);
}
#ifdef KTIKZ_USE_KDE
KIO::Job *job = KIO::file_copy(KUrl::fromPath(m_tempTikzFileBaseName + extension), exportUrl, -1, KIO::Overwrite | KIO::HideProgressInfo);
connect(job, SIGNAL(result(KJob*)), this, SLOT(showJobError(KJob*)));
#else
if (!QFile::copy(m_tempTikzFileBaseName + extension, exportFileName))
QMessageBox::critical(m_parentWidget, QCoreApplication::applicationName(),
tr("The image could not be exported to the file \"%1\".").arg(exportFileName));
#endif
}
/***************************************************************************/
bool TikzPreviewController::setTemplateFile(const QString &path)
{
#ifdef KTIKZ_USE_KDE
const KUrl url(path);
const KUrl localUrl = KUrl::fromPath(m_tempDir->name() + "tikztemplate.tex");
if (url.isValid() && !url.isLocalFile() && KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, m_parentWidget))
{
KIO::Job *job = KIO::file_copy(url, localUrl, -1, KIO::Overwrite | KIO::HideProgressInfo);
if (!KIO::NetAccess::synchronousRun(job, m_parentWidget))
{
// KMessageBox::information(m_parentWidget, i18nc("@info", "Template file could not be copied to a temporary file <filename>%1</filename>.", localUrl.prettyUrl()));
KMessageBox::information(m_parentWidget, tr("Template file could not be copied to a temporary file \"%1\".").arg(localUrl.prettyUrl()));
return false;
}
m_tikzPreviewGenerator->setTemplateFile(localUrl.path());
}
else
#endif
m_tikzPreviewGenerator->setTemplateFile(path);
return true;
}
void TikzPreviewController::setTemplateFileAndRegenerate(const QString &path)
{
if (setTemplateFile(path))
generatePreview(true);
}
void TikzPreviewController::setReplaceTextAndRegenerate(const QString &replace)
{
m_tikzPreviewGenerator->setReplaceText(replace);
generatePreview(true);
}
/***************************************************************************/
QString TikzPreviewController::tikzCode() const
{
return m_mainWidget->tikzCode();
}
QString TikzPreviewController::getLogText()
{
return m_tikzPreviewGenerator->getLogText();
}
void TikzPreviewController::generatePreview()
{
QAction *action = qobject_cast<QAction*>(sender());
bool templateChanged = (action == 0) ? true : false; // XXX dirty hack: the template hasn't changed when the Build button in the app has been pressed (if available), the other cases in which this function is called is when a file is opened, in which case everything should be cleaned up and regenerated
generatePreview(templateChanged);
}
void TikzPreviewController::generatePreview(bool templateChanged)
{
if (templateChanged) // old aux files may contain commands available in the old template, but not anymore in the new template
cleanUp();
// TODO: m_tikzPreviewGenerator->addToTexinputs(QFileInfo(m_mainWidget->url().path()).absolutePath());
// m_tikzPreviewGenerator->setTikzFilePath(m_mainWidget->url().path()); // the directory in which the pgf file is located is added to TEXINPUTS before running latex
m_tikzPreviewGenerator->generatePreview(templateChanged);
}
void TikzPreviewController::regeneratePreview()
{
// m_tikzPreviewGenerator->setTikzFilePath(m_mainWidget->url().path()); // the directory in which the pgf file is located is added to TEXINPUTS before running latex
m_tikzPreviewGenerator->regeneratePreview();
}
void TikzPreviewController::emptyPreview()
{
setExportActionsEnabled(false);
m_tikzPreviewGenerator->abortProcess(); // abort still running processes
m_tikzPreview->emptyPreview();
}
/***************************************************************************/
void TikzPreviewController::applySettings()
{
QSettings settings(ORGNAME, APPNAME);
m_tikzPreviewGenerator->setLatexCommand(settings.value("LatexCommand", "pdflatex").toString());
m_tikzPreviewGenerator->setPdftopsCommand(settings.value("PdftopsCommand", "pdftops").toString());
const bool useShellEscaping = settings.value("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("TemplateFile").toString());
const QString replaceText = settings.value("TemplateReplaceText", "<>").toString();
m_tikzPreviewGenerator->setReplaceText(replaceText);
m_templateWidget->setReplaceText(replaceText);
m_templateWidget->setEditor(settings.value("TemplateEditor", KTIKZ_TEMPLATE_EDITOR_DEFAULT).toString());
}
void TikzPreviewController::setExportActionsEnabled(bool enabled)
{
m_exportAction->setEnabled(enabled);
}
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)
{
#ifndef KTIKZ_USE_KDE
m_shellEscapeButton->setChecked(useShellEscaping);
#endif
QSettings settings(ORGNAME, APPNAME);
settings.setValue("UseShellEscaping", useShellEscaping);
m_tikzPreviewGenerator->setShellEscaping(useShellEscaping);
generatePreview(false);
}
/***************************************************************************/
bool TikzPreviewController::cleanUp()
{
bool success = true;
const QFileInfo tempTikzFileInfo(m_tempTikzFileBaseName + ".tex");
QDir tempTikzDir(tempTikzFileInfo.absolutePath());
QStringList filters;
filters << tempTikzFileInfo.completeBaseName() + ".*";
foreach (const QString &fileName, tempTikzDir.entryList(filters))
success = success && tempTikzDir.remove(fileName);
return success;
}
|