]> git.lyx.org Git - lyx.git/blob - src/lyxled/LayoutEditor.cpp
simplify Lexer use a bit
[lyx.git] / src / lyxled / LayoutEditor.cpp
1
2 #include "LayoutEditor.h"
3
4 #include "ui_MainWindowUi.h"
5
6 #include <QDebug>
7 #include <QFile>
8 #include <QList>
9 #include <QStandardItem>
10 #include <QStandardItemModel>
11 #include <QString>
12 #include <QTreeView>
13
14
15 namespace lyx {
16
17 /////////////////////////////////////////////////////////////////////////
18 //
19 // LayoutTree
20 //
21 /////////////////////////////////////////////////////////////////////////
22
23 LayoutTree::LayoutTree(QWidget * parent) 
24         : QTreeView(parent)
25 {}
26
27
28 /////////////////////////////////////////////////////////////////////////
29 //
30 // LayoutEditor
31 //
32 /////////////////////////////////////////////////////////////////////////
33
34 LayoutEditor::LayoutEditor(QWidget * parent) 
35         : QWidget(parent)
36 {}
37
38
39 /////////////////////////////////////////////////////////////////////////
40 //
41 // MainWindow
42 //
43 /////////////////////////////////////////////////////////////////////////
44
45 MainWindow::MainWindow()
46 {
47         ui_ = new Ui::MainWindow;
48         ui_->setupUi(this);
49
50         model_ = new QStandardItemModel(this);
51         view_ = new LayoutTree(this);
52         view_->setModel(model_);
53         //setCentralWidget(view_);
54
55         ui_->dockLayoutTree->setWidget(view_);
56 }
57
58
59 MainWindow::~MainWindow()
60 {
61         delete ui_;
62 }
63
64 static bool isInsensitivelyEqual(QString const & s1, QString const & s2)
65 {
66         return s1.compare(s2, Qt::CaseInsensitive) == 0;
67 }
68
69 void MainWindow::loadLayoutFile(QString const & fileName)
70 {
71         loadLayoutFile(fileName, model_->invisibleRootItem());
72         view_->expandAll();
73 }
74
75
76 void MainWindow::loadLayoutFile(QString const & fileName,
77         QStandardItem * parent)
78 {
79         QFile file(fileName);
80 #if 0
81         file.open(QIODevice::ReadOnly);
82         QString contents = file.readAll();
83         file.close();
84         qDebug() << "contents: " << contents;
85 #endif
86
87         file.open(QIODevice::ReadOnly);
88
89         QTextStream ts(&file);
90         while (!ts.atEnd()) {
91                 QList<QStandardItem *> row;
92                 QString code;
93                 ts >> code;
94                 //qDebug() << "CODE: " << code;
95                 if (code.startsWith('#')) {
96                         QString line = code + ' ' + ts.readLine();
97                         //row.append(new QStandardItem("Comment"));
98                         //row.append(new QStandardItem(code + ' ' + ts.readLine()));
99                         //parent->appendRow(row);
100                 } else if (isInsensitivelyEqual(code, "Input")) {
101                         QString inputFile;
102                         ts >> inputFile;
103                         QStandardItem * item = new QStandardItem(inputFile);
104                         row.append(item);
105                         parent->appendRow(row);
106                         inputFile = fileName.left(fileName.lastIndexOf('/')) + '/' + inputFile;
107                         qDebug() << "INPUT: " << inputFile;
108                         loadLayoutFile(inputFile, item);
109                 } else if (isInsensitivelyEqual(code, "Style")) {
110                         QString style;
111                         ts >> style;
112                         //while (!ts.atEnd() && !isInsensitivelyEqual(code, "EndStyle"))
113                         //      ts >> code;
114                         QStandardItem * item = new QStandardItem(style);
115                         row.append(item);
116                         parent->appendRow(row);
117                 } else {
118                         //row.append(new QStandardItem(code));
119                         //parent->appendRow(row);
120                 }
121         }
122         
123         file.close();
124 }
125
126 } // namespace lyx