]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPreambleDialog.C
Lots and lots of little trivial bits.
[lyx.git] / src / frontends / qt2 / QPreambleDialog.C
1 /**
2  * \file QPreambleDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <fstream>
18
19 #include "support/lyxlib.h"
20 #include "support/forkedcall.h"
21 #include "support/filetools.h"
22 #include "gettext.h"
23 #include "LyXView.h"
24 #include "ControlPreamble.h"
25
26 #include "QPreamble.h"
27 #include "QPreambleDialog.h"
28
29 #include <qpushbutton.h>
30 #include <qmultilineedit.h>
31 #include <qinputdialog.h>
32
33 using std::getline;
34
35 QPreambleDialog::QPreambleDialog(QPreamble * form)
36         : QPreambleDialogBase(0, 0, false, 0),
37         form_(form)
38 {
39         connect(okPB, SIGNAL(clicked()),
40                 form, SLOT(slotOK()));
41         connect(applyPB, SIGNAL(clicked()),
42                 form, SLOT(slotApply()));
43         connect(closePB, SIGNAL(clicked()),
44                 form, SLOT(slotClose()));
45 }
46
47
48 void QPreambleDialog::closeEvent(QCloseEvent * e)
49 {
50         form_->slotWMHide();
51         e->accept();
52 }
53
54
55 void QPreambleDialog::change_adaptor()
56 {
57         form_->changed();
58 }
59
60
61 void QPreambleDialog::editClicked()
62 {
63         // find an editor
64         string editor = GetEnv("EDITOR");
65         if (editor.empty()) {
66                 static string lastentry = "";
67                 editor = QInputDialog::getText(
68                         _("Enter editor program"), _("Editor"), QLineEdit::Normal,
69                         lastentry.c_str()).latin1();
70                 if (editor.empty())
71                         return;
72                 lastentry = editor;
73         }
74
75         string const text(preambleLE->text().latin1());
76         string const filename(lyx::tempName("", "preamble"));
77         std::ofstream file(filename.c_str());
78
79         // FIXME ?
80         if (!file)
81                 return;
82
83         file << text;
84
85         file.close();
86
87         editor += " " + filename;
88
89         Forkedcall call;
90
91         // FIXME: make async
92         if (call.startscript(Forkedcall::Wait, editor)) {
93                 lyx::unlink(filename);
94                 return;
95         }
96
97         std::ifstream in(filename.c_str());
98
99         if (!in) {
100                 lyx::unlink(filename);
101                 return;
102         }
103
104         string newtext;
105         string line;
106
107         while (getline(in, line)) {
108                 newtext += line + "\n";
109         }
110
111         in.close();
112         lyx::unlink(filename);
113         preambleLE->setText(newtext.c_str());
114 }