]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
remove preamble dialog from the qt frontend
[lyx.git] / src / frontends / controllers / ControlDocument.C
1 // -*- C++ -*-
2 /**
3  * \file ControlDocument.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Edwin Leuven
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "BufferView.h"
19 #include "ControlDocument.h"
20 #include "ViewBase.h"
21
22 #include "gettext.h"
23 #include "lyxfind.h"
24
25 #include "buffer.h"
26 #include "language.h"
27 #include "lyx_main.h"
28 #include "lyxtextclass.h"
29 #include "lyxtextclasslist.h"
30 #include "CutAndPaste.h"
31
32 #include "frontends/LyXView.h"
33 #include "frontends/Alert.h"
34
35 #include "support/lstrings.h"
36 #include "support/filetools.h"
37
38 #include "BoostFormat.h"
39
40 using std::endl;
41
42
43 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
44         : ControlDialogBD(lv, d), bp_(0)
45 {
46 }
47
48 ControlDocument::~ControlDocument()
49 {}
50
51
52 BufferParams & ControlDocument::params()
53 {
54         lyx::Assert(bp_.get());
55         return *bp_;
56 }
57
58
59 LyXTextClass ControlDocument::textClass()
60 {
61         return textclasslist[bp_->textclass];
62 }
63
64
65 void ControlDocument::apply()
66 {
67         if (!bufferIsAvailable())
68                 return;
69
70         setLanguage();
71
72         classApply();
73
74         view().apply();
75         buffer()->params = *bp_;
76
77         lv_.view()->redoCurrentBuffer();
78
79         buffer()->markDirty();
80
81         lv_.message(_("Document settings applied"));
82 }
83
84
85 void ControlDocument::setParams()
86 {
87         if (!bp_.get())
88                 bp_.reset(new BufferParams());
89
90         /// Set the buffer parameters
91         *bp_ = buffer()->params;
92 }
93
94
95 void ControlDocument::setLanguage()
96 {
97         Language const * oldL = buffer()->params.language;
98         Language const * newL = bp_->language;
99
100         if (oldL != newL
101             && oldL->RightToLeft() == newL->RightToLeft()
102             && !lv_.buffer()->isMultiLingual())
103                 lv_.buffer()->changeLanguage(oldL, newL);
104 }
105
106
107 void ControlDocument::classApply()
108 {
109         BufferParams & params = buffer()->params;
110         lyx::textclass_type const old_class = params.textclass;
111         lyx::textclass_type const new_class = bp_->textclass;
112
113         // exit if nothing changes or if unable to load the new class
114         if (new_class == old_class || !loadTextclass(new_class))
115                 return;
116
117         // successfully loaded
118         view().apply();
119         buffer()->params = *bp_;
120
121         lv_.message(_("Converting document to new document class..."));
122         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
123                 old_class, new_class,
124                 &*(lv_.buffer()->paragraphs.begin()),
125                 lv_.buffer()->params);
126         if (ret) {
127                 string s;
128                 if (ret == 1) {
129                         s = _("One paragraph couldn't be converted");
130                 } else {
131 #if USE_BOOST_FORMAT
132                         boost::format fmt(_("%1$s paragraphs couldn't be converted"));
133                         fmt % ret;
134                         s = fmt.str();
135 #else
136                         s += tostr(ret);
137                         s += _(" paragraphs couldn't be converted");
138 #endif
139                 }
140                 Alert::alert(_("Conversion Errors!"),s,
141                              _("into chosen document class"));
142         }
143 }
144
145
146 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
147 {
148         bool const success = textclasslist[tc].load();
149         if (!success) {
150                 // problem changing class
151                 // -- warn user (to retain old style)
152                 Alert::alert(_("Conversion Errors!"),
153                              _("Errors loading new document class."),
154                              _("Reverting to original document class."));
155         }
156         return success;
157 }
158
159
160 void ControlDocument::saveAsDefault()
161 {
162         if (!Alert::askQuestion(_("Do you want to save the current settings"),
163                                 _("for the document layout as default?"),
164                                 _("(they will be valid for any new document)")))
165                 return;
166
167         lv_.buffer()->params.preamble = bp_->preamble;
168
169         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
170                                      "defaults.lyx");
171         Buffer defaults(fname);
172         defaults.params = params();
173
174         // add an empty paragraph. Is this enough?
175         Paragraph * par = new Paragraph;
176         par->layout(params().getLyXTextClass().defaultLayout());
177         defaults.paragraphs.set(par);
178
179         defaults.writeFile(defaults.fileName());
180
181 }