summaryrefslogtreecommitdiff
path: root/src/data/tikzdocument.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/tikzdocument.cpp')
-rw-r--r--src/data/tikzdocument.cpp62
1 files changed, 41 insertions, 21 deletions
diff --git a/src/data/tikzdocument.cpp b/src/data/tikzdocument.cpp
index 24a793b..1099779 100644
--- a/src/data/tikzdocument.cpp
+++ b/src/data/tikzdocument.cpp
@@ -34,16 +34,10 @@ TikzDocument::TikzDocument(QObject *parent) : QObject(parent)
_parseSuccess = true;
_fileName = "";
_shortName = "";
- _undoStack = new QUndoStack();
+ _undoStack = new QUndoStack(this);
_undoStack->setClean();
}
-TikzDocument::~TikzDocument()
-{
- delete _graph;
- delete _undoStack;
-}
-
QUndoStack *TikzDocument::undoStack() const
{
return _undoStack;
@@ -75,15 +69,18 @@ void TikzDocument::open(QString fileName)
return;
}
+ addToRecentFiles();
+
QTextStream in(&file);
_tikz = in.readAll();
file.close();
+ Graph *oldGraph = _graph;
Graph *newGraph = new Graph(this);
TikzAssembler ass(newGraph);
if (ass.parse(_tikz)) {
- delete _graph;
_graph = newGraph;
+ oldGraph->deleteLater();
foreach (Node *n, _graph->nodes()) n->attachStyle();
foreach (Edge *e, _graph->edges()) {
e->attachStyle();
@@ -93,7 +90,7 @@ void TikzDocument::open(QString fileName)
refreshTikz();
setClean();
} else {
- delete newGraph;
+ newGraph->deleteLater();
_parseSuccess = false;
}
}
@@ -103,10 +100,10 @@ bool TikzDocument::save() {
return saveAs();
} else {
MainWindow *win = tikzit->activeWindow();
- if (win != 0 && !win->tikzScene()->enabled()) {
+ if (win != nullptr && !win->tikzScene()->enabled()) {
win->tikzScene()->parseTikz(win->tikzSource());
if (!win->tikzScene()->enabled()) {
- auto resp = QMessageBox::question(0,
+ auto resp = QMessageBox::question(nullptr,
tr("Tikz failed to parse"),
tr("Cannot save file with invalid TiKZ source. Revert changes and save?"));
if (resp == QMessageBox::Yes) win->tikzScene()->setEnabled(true);
@@ -128,7 +125,8 @@ bool TikzDocument::save() {
setClean();
return true;
} else {
- QMessageBox::warning(0, "Save Failed", "Could not open file: '" + _fileName + "' for writing.");
+ QMessageBox::warning(nullptr,
+ "Save Failed", "Could not open file: '" + _fileName + "' for writing.");
}
}
@@ -145,6 +143,34 @@ void TikzDocument::setClean()
_undoStack->setClean();
}
+QString TikzDocument::fileName() const
+{
+ return _fileName;
+}
+
+bool TikzDocument::isEmpty()
+{
+ return _graph->nodes().isEmpty();
+}
+
+void TikzDocument::addToRecentFiles()
+{
+ QSettings settings("tikzit", "tikzit");
+ if (!_fileName.isEmpty()) {
+ QStringList recentFiles = settings.value("recent-files").toStringList();
+
+ // if the file is in the list already, shift it to the top. Otherwise, add it.
+ recentFiles.removeAll(_fileName);
+ recentFiles.prepend(_fileName);
+
+ // keep max 10 files
+ while (recentFiles.size() > 10) recentFiles.removeLast();
+
+ settings.setValue("recent-files", recentFiles);
+ tikzit->updateRecentFiles();
+ }
+}
+
void TikzDocument::setGraph(Graph *graph)
{
_graph = graph;
@@ -153,10 +179,10 @@ void TikzDocument::setGraph(Graph *graph)
bool TikzDocument::saveAs() {
MainWindow *win = tikzit->activeWindow();
- if (win != 0 && !win->tikzScene()->enabled()) {
+ if (win != nullptr && !win->tikzScene()->enabled()) {
win->tikzScene()->parseTikz(win->tikzSource());
if (!win->tikzScene()->enabled()) {
- auto resp = QMessageBox::question(0,
+ auto resp = QMessageBox::question(nullptr,
tr("Tikz failed to parse"),
tr("Cannot save file with invalid TiKZ source. Revert changes and save?"));
if (resp == QMessageBox::Yes) win->tikzScene()->setEnabled(true);
@@ -175,19 +201,13 @@ bool TikzDocument::saveAs() {
dialog.setDirectory(settings.value("previous-file-path").toString());
dialog.setOption(QFileDialog::DontUseNativeDialog);
-// QString fileName = QFileDialog::getSaveFileName(tikzit->activeWindow(),
-// tr("Save File As"),
-// settings.value("previous-file-path").toString(),
-// tr("TiKZ Files (*.tikz)"),
-// nullptr,
-// QFileDialog::DontUseNativeDialog);
-
if (dialog.exec() && !dialog.selectedFiles().isEmpty()) {
QString fileName = dialog.selectedFiles()[0];
_fileName = fileName;
if (save()) {
// clean state might not change, so update title bar manually
tikzit->activeWindow()->updateFileName();
+ addToRecentFiles();
return true;
}
}