]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPreambleDialog.C
Some string(widget->text()) fixes. Weirdness
[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 "Lsstream.h"
18 #include "support/lyxlib.h"
19 #include "support/forkedcall.h"
20 #include "support/filetools.h"
21 #include "gettext.h"
22 #include "LyXView.h"
23 #include "ControlPreamble.h"
24
25 #include "QPreamble.h"
26 #include "QPreambleDialog.h"
27
28 #include <qpushbutton.h>
29 #include <qmultilineedit.h>
30 #include <qinputdialog.h>
31
32 #include <fstream>
33
34 using std::getline;
35
36 QPreambleDialog::QPreambleDialog(QPreamble * form)
37         : QPreambleDialogBase(0, 0, false, 0),
38         form_(form)
39 {
40         connect(okPB, SIGNAL(clicked()),
41                 form, SLOT(slotOK()));
42         connect(applyPB, SIGNAL(clicked()),
43                 form, SLOT(slotApply()));
44         connect(closePB, SIGNAL(clicked()),
45                 form, SLOT(slotClose()));
46 }
47
48
49 void QPreambleDialog::closeEvent(QCloseEvent * e)
50 {
51         form_->slotWMHide();
52         e->accept();
53 }
54
55
56 void QPreambleDialog::change_adaptor()
57 {
58         form_->changed();
59 }
60
61
62 void QPreambleDialog::editClicked()
63 {
64         // find an editor
65         string editor = GetEnv("EDITOR");
66         if (editor.empty()) {
67                 static string lastentry;
68                 editor = QInputDialog::getText(
69                         _("Enter editor program"), _("Editor"), QLineEdit::Normal,
70                         lastentry.c_str()).latin1();
71                 if (editor.empty())
72                         return;
73                 lastentry = editor;
74         }
75
76         string const text(preambleLE->text().latin1());
77         string const filename(lyx::tempName("", "preamble"));
78         std::ofstream file(filename.c_str());
79
80         // FIXME ?
81         if (!file)
82                 return;
83
84         file << text;
85
86         file.close();
87
88         editor += ' ' + filename;
89
90         Forkedcall call;
91
92         // FIXME: make async
93         if (call.startscript(Forkedcall::Wait, editor)) {
94                 lyx::unlink(filename);
95                 return;
96         }
97
98         std::ifstream in(filename.c_str());
99
100         if (!in) {
101                 lyx::unlink(filename);
102                 return;
103         }
104
105         ostringstream newtext;
106         newtext << in.rdbuf();
107
108         // close the files before we delete the file
109         in.close();
110
111         lyx::unlink(filename);
112         preambleLE->setText(newtext.str().c_str());
113 }