summaryrefslogtreecommitdiff
path: root/src/tikzit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tikzit.cpp')
-rw-r--r--src/tikzit.cpp277
1 files changed, 203 insertions, 74 deletions
diff --git a/src/tikzit.cpp b/src/tikzit.cpp
index c9286c9..8569817 100644
--- a/src/tikzit.cpp
+++ b/src/tikzit.cpp
@@ -19,6 +19,8 @@
#include "tikzit.h"
#include "tikzassembler.h"
#include "tikzstyles.h"
+#include "previewwindow.h"
+#include "latexprocess.h"
#include <QFile>
#include <QFileDialog>
@@ -28,6 +30,7 @@
#include <QRegularExpression>
#include <QVersionNumber>
#include <QNetworkAccessManager>
+#include <QColorDialog>
// application-level instance of Tikzit
Tikzit *tikzit;
@@ -35,7 +38,7 @@ Tikzit *tikzit;
// font to use for node labels
QFont Tikzit::LABEL_FONT("Courrier", 9);
-Tikzit::Tikzit() : _styleFile("[no styles]"), _activeWindow(0)
+Tikzit::Tikzit() : _styleFile("[no styles]"), _activeWindow(nullptr)
{
}
@@ -43,54 +46,7 @@ void Tikzit::init()
{
QSettings settings("tikzit", "tikzit");
- // 19 standard xcolor colours
- _colNames <<
- "black" <<
- "darkgray" <<
- "gray" <<
- "lightgray" <<
- "white" <<
-
- "red" <<
- "orange" <<
- "yellow" <<
- "green" <<
- "blue" <<
- "purple" <<
-
- "brown" <<
- "olive" <<
- "lime" <<
- "cyan" <<
- "teal" <<
-
- "magenta" <<
- "violet" <<
- "pink";
-
- _cols <<
- QColor::fromRgbF(0,0,0) <<
- QColor::fromRgbF(0.25,0.25,0.25) <<
- QColor::fromRgbF(0.5,0.5,0.5) <<
- QColor::fromRgbF(0.75,0.75,0.75) <<
- QColor::fromRgbF(1,1,1) <<
-
- QColor::fromRgbF(1,0,0) <<
- QColor::fromRgbF(1,0.5,0) <<
- QColor::fromRgbF(1,1,0) <<
- QColor::fromRgbF(0,1,0) <<
- QColor::fromRgbF(0,0,1) <<
- QColor::fromRgbF(0.75,0,0.25) <<
-
- QColor::fromRgbF(0.75,0.5,0.25) <<
- QColor::fromRgbF(0.5,0.5,0) <<
- QColor::fromRgbF(0.75,1,0) <<
- QColor::fromRgbF(0,1,1) <<
- QColor::fromRgbF(0,0.5,0.5) <<
-
- QColor::fromRgbF(1,0,1) <<
- QColor::fromRgbF(0.5,0,0.5) <<
- QColor::fromRgbF(1,0.75,0.75);
+ initColors();
_mainMenu = new MainMenu();
QMainWindow *dummy = new QMainWindow();
@@ -106,12 +62,14 @@ void Tikzit::init()
_windows << new MainWindow();
_windows[0]->show();
+ _styleFile = "";
+ _styleFilePath = "";
QString styleFile = settings.value("previous-tikzstyles-file").toString();
if (!styleFile.isEmpty()) loadStyles(styleFile);
QVariant check = settings.value("check-for-updates");
if (check.isNull()) {
- int resp = QMessageBox::question(0,
+ int resp = QMessageBox::question(nullptr,
tr("Check for updates"),
tr("Would you like TikZiT to check for updates automatically?"
" (You can always change this later in the Help menu.)"),
@@ -124,8 +82,11 @@ void Tikzit::init()
setCheckForUpdates(check.toBool());
if (check.toBool()) {
- checkForUpdates();
+ checkForUpdates(false);
}
+
+ _preview = new PreviewWindow();
+ _latex = nullptr;
}
//QMenuBar *Tikzit::mainMenu() const
@@ -190,7 +151,7 @@ void Tikzit::newTikzStyles()
if (dialog.exec() && !dialog.selectedFiles().isEmpty()) {
QString fileName = dialog.selectedFiles()[0];
- TikzStyles *st = new TikzStyles;
+ TikzStyles *st = new TikzStyles(this);
if (st->saveStyles(fileName)) {
QFileInfo fi(fileName);
@@ -198,14 +159,14 @@ void Tikzit::newTikzStyles()
_styleFilePath = fi.absoluteFilePath();
settings.setValue("previous-tikzstyles-file", fileName);
settings.setValue("previous-tikzstyles-path", fi.absolutePath());
- delete _styles;
+ _styles->deleteLater();
_styles = st;
foreach (MainWindow *w, _windows) {
w->tikzScene()->reloadStyles();
}
} else {
- QMessageBox::warning(0,
+ QMessageBox::warning(nullptr,
"Could not write to style file.",
"Could not write to: '" + fileName + "'. Check file permissions or choose a new location.");
}
@@ -244,7 +205,7 @@ void Tikzit::removeWindow(MainWindow *w)
_windows.removeAll(w);
if (_activeWindow == w) {
if (_windows.isEmpty()) {
- _activeWindow = 0;
+ _activeWindow = nullptr;
// TODO: check if we should quit when last window closed
quit();
} else _activeWindow = _windows[0];
@@ -254,7 +215,7 @@ void Tikzit::removeWindow(MainWindow *w)
void Tikzit::open()
{
QSettings settings("tikzit", "tikzit");
- QString fileName = QFileDialog::getOpenFileName(0,
+ QString fileName = QFileDialog::getOpenFileName(nullptr,
tr("Open File"),
settings.value("previous-file-path").toString(),
tr("TiKZ Files (*.tikz)"),
@@ -269,23 +230,35 @@ void Tikzit::open(QString fileName)
if (!fileName.isEmpty()) {
if (_windows.size() == 1 &&
_windows[0]->tikzDocument()->isClean() &&
- _windows[0]->tikzDocument()->shortName().isEmpty())
- {
+ _windows[0]->tikzDocument()->shortName().isEmpty())
+ {
_windows[0]->open(fileName);
_windows[0]->show();
- }
- else {
- MainWindow *w = new MainWindow();
- w->show();
- w->open(fileName);
- _windows << w;
+ }
+ else
+ {
+ bool found = false;
+ foreach (MainWindow *w, _windows) {
+ if (w->tikzDocument()->fileName() == fileName) {
+ w->raise();
+ w->activateWindow();
+ found = true;
+ }
+ }
+
+ if (!found) {
+ MainWindow *w = new MainWindow();
+ _windows << w;
+ w->show();
+ w->open(fileName);
+ }
}
}
}
void Tikzit::openTikzStyles() {
QSettings settings("tikzit", "tikzit");
- QString fileName = QFileDialog::getOpenFileName(0,
+ QString fileName = QFileDialog::getOpenFileName(nullptr,
tr("Open File"),
settings.value("previous-tikzstyles-path").toString(),
tr("TiKZ Style Files (*.tikzstyles)"),
@@ -310,7 +283,7 @@ bool Tikzit::loadStyles(QString fileName)
if (st->loadStyles(fileName)) {
_styleFile = fi.fileName();
_styleFilePath = fi.absoluteFilePath();
- delete _styles;
+ _styles->deleteLater();
_styles = st;
foreach (MainWindow *w, _windows) {
@@ -318,7 +291,7 @@ bool Tikzit::loadStyles(QString fileName)
}
return true;
} else {
- QMessageBox::warning(0,
+ QMessageBox::warning(nullptr,
"Bad style file.",
"Bad style file: '" + fileName + "'. Check the file is properly formatted and try to load it again.");
return false;
@@ -326,7 +299,8 @@ bool Tikzit::loadStyles(QString fileName)
} else {
//settings.setValue("previous-tikzstyles-file", "");
- QMessageBox::warning(0, "Style file not found.", "Could not open style file: '" + fileName + "'.");
+ QMessageBox::warning(nullptr,
+ "Style file not found.", "Could not open style file: '" + fileName + "'.");
return false;
}
}
@@ -346,6 +320,20 @@ QString Tikzit::styleFilePath() const
return _styleFilePath;
}
+void Tikzit::updateRecentFiles()
+{
+ foreach (MainWindow *w, _windows) {
+ w->menu()->updateRecentFiles();
+ }
+}
+
+void Tikzit::clearRecentFiles()
+{
+ QSettings settings("tikzit", "tikzit");
+ settings.setValue("recent-files", QStringList());
+ updateRecentFiles();
+}
+
void Tikzit::setCheckForUpdates(bool check)
{
QSettings settings("tikzit", "tikzit");
@@ -357,16 +345,32 @@ void Tikzit::setCheckForUpdates(bool check)
}
}
-void Tikzit::checkForUpdates()
+void Tikzit::checkForUpdates(bool manual)
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
- connect(manager, SIGNAL(finished(QNetworkReply*)),
- this, SLOT(updateReply(QNetworkReply*)));
+
+ if (manual) {
+ connect(manager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(updateManual(QNetworkReply*)));
+ } else {
+ connect(manager, SIGNAL(finished(QNetworkReply*)),
+ this, SLOT(updateAuto(QNetworkReply*)));
+ }
manager->get(QNetworkRequest(QUrl("https://tikzit.github.io/latest-version.txt")));
}
-void Tikzit::updateReply(QNetworkReply *reply)
+void Tikzit::updateAuto(QNetworkReply *reply)
+{
+ updateReply(reply, false);
+}
+
+void Tikzit::updateManual(QNetworkReply *reply)
+{
+ updateReply(reply, true);
+}
+
+void Tikzit::updateReply(QNetworkReply *reply, bool manual)
{
if (!reply->isReadable()) return;
@@ -395,7 +399,7 @@ void Tikzit::updateReply(QNetworkReply *reply)
QString::number(latest.minorVersion()) + "." +
QString::number(latest.microVersion());
if (rcLatest != 1000) strLatest += "-rc" + QString::number(rcLatest);
- QMessageBox::information(0,
+ QMessageBox::information(nullptr,
tr("Update available"),
"<p><b>A new version of TikZiT is available!</b></p>"
"<p><i>current version: " TIKZIT_VERSION "<br />"
@@ -404,13 +408,138 @@ void Tikzit::updateReply(QNetworkReply *reply)
"<a href=\"https://tikzit.github.io\">tikzit.github.io</a>.</p>");
}
} else {
- QMessageBox::warning(0,
+ // don't complain of invalid response for auto update check
+ if (manual) {
+ QMessageBox::warning(nullptr,
tr("Invalid response"),
"<p>Got invalid version response from "
"<a href=\"https://tikzit.github.io\">tikzit.github.io</a>.</p>");
+ }
+ }
+}
+
+void Tikzit::makePreview()
+{
+ if (activeWindow()) {
+ LatexProcess *oldProc = _latex;
+ _latex = new LatexProcess(_preview, this);
+ if (oldProc != nullptr) {
+ oldProc->kill();
+ oldProc->deleteLater();
+ }
+
+ connect(_latex, SIGNAL(previewFinished()), this, SLOT(cleanupLatex()));
+
+ if (activeWindow()->tikzDocument()->isEmpty()) {
+ _latex->makePreview("\\begin{tikzpicture}\n"
+ " \\node [style=none] (0) at (0,0) {};\n"
+ "\\end{tikzpicture}\n");
+ } else {
+ _latex->makePreview(activeWindow()->tikzSource());
+ }
+ _preview->show();
+ _preview->raise();
}
}
+void Tikzit::cleanupLatex()
+{
+ LatexProcess *oldProc = _latex;
+ _latex = nullptr;
+ if (oldProc != nullptr) {
+ oldProc->deleteLater();
+ }
+}
+
+void Tikzit::initColors()
+{
+ // 19 standard xcolor colours
+ _colNames <<
+ "black" <<
+ "darkgray" <<
+ "gray" <<
+ "lightgray" <<
+ "white" <<
+
+ "red" <<
+ "orange" <<
+ "yellow" <<
+ "green" <<
+ "blue" <<
+ "purple" <<
+
+ "brown" <<
+ "olive" <<
+ "lime" <<
+ "cyan" <<
+ "teal" <<
+
+ "magenta" <<
+ "violet" <<
+ "pink";
+
+ _cols <<
+ QColor::fromRgbF(0,0,0) <<
+ QColor::fromRgbF(0.25,0.25,0.25) <<
+ QColor::fromRgbF(0.5,0.5,0.5) <<
+ QColor::fromRgbF(0.75,0.75,0.75) <<
+ QColor::fromRgbF(1,1,1) <<
+
+ QColor::fromRgbF(1,0,0) <<
+ QColor::fromRgbF(1,0.5,0) <<
+ QColor::fromRgbF(1,1,0) <<
+ QColor::fromRgbF(0,1,0) <<
+ QColor::fromRgbF(0,0,1) <<
+ QColor::fromRgbF(0.75,0,0.25) <<
+
+ QColor::fromRgbF(0.75,0.5,0.25) <<
+ QColor::fromRgbF(0.5,0.5,0) <<
+ QColor::fromRgbF(0.75,1,0) <<
+ QColor::fromRgbF(0,1,1) <<
+ QColor::fromRgbF(0,0.5,0.5) <<
+
+ QColor::fromRgbF(1,0,1) <<
+ QColor::fromRgbF(0.5,0,0.5) <<
+ QColor::fromRgbF(1,0.75,0.75);
+
+ for (int i = 0; i < 48; ++i) {
+ QColorDialog::setStandardColor(i, QColor(Qt::white));
+ }
+
+ // grayscale in column 1
+ int pos = 0;
+ for (int i=0; i < 5; ++i) {
+ QColorDialog::setStandardColor(pos, _cols[i]);
+ pos += 1;
+ }
+
+ // rainbow in column 2
+ pos = 6;
+ for (int i=5; i < 11; ++i) {
+ QColorDialog::setStandardColor(pos, _cols[i]);
+ pos += 1;
+ }
+
+ // brown/green/teal spectrum in column 3
+ pos = 12;
+ for (int i=11; i < 16; ++i) {
+ QColorDialog::setStandardColor(pos, _cols[i]);
+ pos += 1;
+ }
+
+ // pinks in column 4
+ pos = 18;
+ for (int i=16; i < 19; ++i) {
+ QColorDialog::setStandardColor(pos, _cols[i]);
+ pos += 1;
+ }
+}
+
+PreviewWindow *Tikzit::previewWindow() const
+{
+ return _preview;
+}
+
//StylePalette *Tikzit::stylePalette() const
//{
// return _stylePalette;