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