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
|
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tikzgraphassembler.h"
#include <QDebug>
#include <QFile>
#include <QList>
#include <QSettings>
#include <QMessageBox>
#include <QFileDialog>
int MainWindow::_numWindows = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
_numWindows++;
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
_graph = new Graph(this);
tikzScene = new TikzScene(_graph, this);
ui->tikzView->setScene(tikzScene);
_fileName = "";
_pristine = true;
// initially, the source view should be collapsed
QList<int> sz = ui->splitter->sizes();
sz[0] = sz[0] + sz[1];
sz[1] = 0;
ui->splitter->setSizes(sz);
}
MainWindow::~MainWindow()
{
//qDebug() << "~MainWindow";
}
void MainWindow::open(QString fileName)
{
_fileName = fileName;
_pristine = false;
QFile file(fileName);
QFileInfo fi(file);
QSettings settings("tikzit", "tikzit");
settings.setValue("previous-file-path", fi.absolutePath());
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"),
tr("Could not open file"));
return;
}
QTextStream in(&file);
QString tikz = in.readAll();
file.close();
ui->tikzSource->setText(tikz);
Graph *newGraph = new Graph(this);
TikzGraphAssembler ass(newGraph);
if (ass.parse(tikz)) {
statusBar()->showMessage("TiKZ parsed successfully", 2000);
tikzScene->setGraph(newGraph);
delete _graph;
_graph = newGraph;
} else {
statusBar()->showMessage("Cannot read TiKZ source");
delete newGraph;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//qDebug() << "got close event";
QMainWindow::closeEvent(event);
}
void MainWindow::on_actionOpen_triggered()
{
QSettings settings("tikzit", "tikzit");
QString fileName = QFileDialog::getOpenFileName(
this,
tr("Open File"),
settings.value("previous-file-path").toString(),
tr("TiKZ Files (*.tikz)"));
if (!fileName.isEmpty()) {
if (_pristine) {
open(fileName);
} else {
MainWindow *w = new MainWindow();
w->show();
w->open(fileName);
}
}
}
|