]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QDocumentDialog.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[lyx.git] / src / frontends / qt4 / QDocumentDialog.C
1 /**
2  * \file QDocumentDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QDocument.h"
15 #include "QDocumentDialog.h"
16
17 #include <QCloseEvent>
18
19 #include "floatplacement.h"
20 #include "lengthcombo.h"
21 #include "validators.h"
22 #include "panelstack.h"
23 #include "Qt2BC.h"
24 #include "checkedwidgets.h"
25 #include "qt_helpers.h"
26
27 #include "bufferparams.h"
28 #include "floatplacement.h"
29 #include "gettext.h"
30 #include "helper_funcs.h" // getSecond()
31 #include "language.h"
32 #include "lyxrc.h" // defaultUnit
33 #include "lyxtextclasslist.h"
34 #include "tex-strings.h" // tex_graphics
35 #include "Spacing.h"
36
37 #include "controllers/ControlDocument.h"
38 #include "controllers/frnt_lang.h"
39
40 #include "support/lstrings.h"
41
42 #include "controllers/ControlDocument.h"
43 #include "controllers/frnt_lang.h"
44
45
46 using lyx::support::token;
47 using lyx::support::bformat;
48 using lyx::support::getVectorFromString;
49
50 using std::distance;
51 using std::vector;
52 using std::string;
53
54
55 namespace lyx {
56 namespace frontend {
57
58
59 namespace {
60
61 char const * encodings[] = { "LaTeX default", "latin1", "latin2",
62         "latin3", "latin4", "latin5", "latin9",
63         "koi8-r", "koi8-u", "cp866", "cp1251",
64         "iso88595", "pt154", 0
65 };
66
67 }
68
69 QDocumentDialog::QDocumentDialog(QDocument * form)
70         : form_(form),
71         lang_(getSecond(getLanguageData(false)))
72 {
73         setupUi(this);
74
75         connect(okPB, SIGNAL(clicked()),
76                 form, SLOT(slotOK()));
77         connect(applyPB, SIGNAL(clicked()),
78                 form, SLOT(slotApply()));
79         connect(closePB, SIGNAL(clicked()),
80                 form, SLOT(slotClose()));
81         connect(restorePB, SIGNAL(clicked()),
82                 form, SLOT(slotRestore()));
83
84
85         connect(savePB, SIGNAL( clicked() ), 
86                 this, SLOT( saveDefaultClicked() ) );
87         connect(defaultPB, SIGNAL( clicked() ), 
88                 this, SLOT( useDefaultsClicked() ) );
89
90         // Manage the restore, ok, apply, restore and cancel/close buttons
91         form_->bcview().setOK(okPB);
92         form_->bcview().setApply(applyPB);
93         form_->bcview().setCancel(closePB);
94         form_->bcview().setRestore(restorePB);
95
96
97         textLayoutModule = new UiWidget<Ui::TextLayoutUi>;
98         // text layout
99         connect(textLayoutModule->fontsCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
100         connect(textLayoutModule->fontsizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
101         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
102         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)), this, SLOT(setLSpacing(int)));
103         connect(textLayoutModule->lspacingLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
104         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
105         connect(textLayoutModule->indentRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
106         connect(textLayoutModule->skipCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
107         connect(textLayoutModule->skipLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
108         connect(textLayoutModule->skipLengthCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
109         connect(textLayoutModule->skipCO, SIGNAL(activated(int)), this, SLOT(setSkip(int)));
110         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)), this, SLOT(enableSkip(bool)));
111         connect(textLayoutModule->twoColumnCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
112         textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
113                 textLayoutModule->lspacingLE));
114         textLayoutModule->skipLE->setValidator(unsignedLengthValidator(
115                 textLayoutModule->skipLE));
116
117         for (int n = 0; tex_fonts[n][0]; ++n) {
118                 QString font = tex_fonts[n];
119                 textLayoutModule->fontsCO->insertItem(font);
120         }
121
122         textLayoutModule->fontsizeCO->insertItem(qt_("default"));
123         textLayoutModule->fontsizeCO->insertItem(qt_("10"));
124         textLayoutModule->fontsizeCO->insertItem(qt_("11"));
125         textLayoutModule->fontsizeCO->insertItem(qt_("12"));
126
127         textLayoutModule->skipCO->insertItem(qt_("SmallSkip"));
128         textLayoutModule->skipCO->insertItem(qt_("MedSkip"));
129         textLayoutModule->skipCO->insertItem(qt_("BigSkip"));
130         textLayoutModule->skipCO->insertItem(qt_("Length"));
131         // remove the %-items from the unit choice
132         textLayoutModule->skipLengthCO->noPercents();
133         textLayoutModule->lspacingCO->insertItem(
134                 qt_("Single"), Spacing::Single);
135         textLayoutModule->lspacingCO->insertItem(
136                 qt_("OneHalf"), Spacing::Onehalf);
137         textLayoutModule->lspacingCO->insertItem(
138                 qt_("Double"), Spacing::Double);
139         textLayoutModule->lspacingCO->insertItem(
140                 qt_("Custom"), Spacing::Other);
141
142         // initialize the length validator
143         addCheckedLineEdit(form_->bcview(), textLayoutModule->skipLE);
144
145
146
147
148         pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
149         // page layout
150         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)), this, SLOT(setCustomPapersize(int)));
151         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)), this, SLOT(setCustomPapersize(int)));
152         connect(pageLayoutModule->portraitRB, SIGNAL(toggled(bool)), this, SLOT(portraitChanged()));
153         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
154         connect(pageLayoutModule->paperheightLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
155         connect(pageLayoutModule->paperwidthLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
156         connect(pageLayoutModule->paperwidthUnitCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
157         connect(pageLayoutModule->paperheightUnitCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
158         connect(pageLayoutModule->portraitRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
159         connect(pageLayoutModule->landscapeRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
160         connect(pageLayoutModule->facingPagesCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
161         connect(pageLayoutModule->pagestyleCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
162         pageLayoutModule->pagestyleCO->insertItem(qt_("default"));
163         pageLayoutModule->pagestyleCO->insertItem(qt_("empty"));
164         pageLayoutModule->pagestyleCO->insertItem(qt_("plain"));
165         pageLayoutModule->pagestyleCO->insertItem(qt_("headings"));
166         pageLayoutModule->pagestyleCO->insertItem(qt_("fancy"));
167         addCheckedLineEdit(form_->bcview(), pageLayoutModule->paperheightLE,
168                 pageLayoutModule->paperheightL);
169         addCheckedLineEdit(form_->bcview(), pageLayoutModule->paperwidthLE,
170                 pageLayoutModule->paperwidthL);
171
172         // paper
173         QComboBox * cb = pageLayoutModule->papersizeCO;
174         cb->insertItem(qt_("Default"));
175         cb->insertItem(qt_("Custom"));
176         cb->insertItem(qt_("US letter"));
177         cb->insertItem(qt_("US legal"));
178         cb->insertItem(qt_("US executive"));
179         cb->insertItem(qt_("A3"));
180         cb->insertItem(qt_("A4"));
181         cb->insertItem(qt_("A5"));
182         cb->insertItem(qt_("B3"));
183         cb->insertItem(qt_("B4"));
184         cb->insertItem(qt_("B5"));
185         // remove the %-items from the unit choice
186         pageLayoutModule->paperwidthUnitCO->noPercents();
187         pageLayoutModule->paperheightUnitCO->noPercents();
188         pageLayoutModule->paperheightLE->setValidator(unsignedLengthValidator(
189                 pageLayoutModule->paperheightLE));
190         pageLayoutModule->paperwidthLE->setValidator(unsignedLengthValidator(
191                 pageLayoutModule->paperwidthLE));
192
193
194
195
196         marginsModule = new UiWidget<Ui::MarginsUi>;
197         // margins
198         connect(marginsModule->marginCB, SIGNAL(toggled(bool)), this, SLOT(setCustomMargins(bool)));
199         connect(marginsModule->marginCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
200         connect(marginsModule->topLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
201         connect(marginsModule->topUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
202         connect(marginsModule->bottomLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
203         connect(marginsModule->bottomUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
204         connect(marginsModule->innerLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
205         connect(marginsModule->innerUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
206         connect(marginsModule->outerLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
207         connect(marginsModule->outerUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
208         connect(marginsModule->headheightLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
209         connect(marginsModule->headheightUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
210         connect(marginsModule->headsepLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
211         connect(marginsModule->headsepUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
212         connect(marginsModule->footskipLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
213         connect(marginsModule->footskipUnit, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
214         marginsModule->topLE->setValidator(unsignedLengthValidator(
215                 marginsModule->topLE));
216         marginsModule->bottomLE->setValidator(unsignedLengthValidator(
217                 marginsModule->bottomLE));
218         marginsModule->innerLE->setValidator(unsignedLengthValidator(
219                 marginsModule->innerLE));
220         marginsModule->outerLE->setValidator(unsignedLengthValidator(
221                 marginsModule->outerLE));
222         marginsModule->headsepLE->setValidator(unsignedLengthValidator(
223                 marginsModule->headsepLE));
224         marginsModule->headheightLE->setValidator(unsignedLengthValidator(
225                 marginsModule->headheightLE));
226         marginsModule->footskipLE->setValidator(unsignedLengthValidator(
227                 marginsModule->footskipLE));
228
229         addCheckedLineEdit(form_->bcview(), marginsModule->topLE,
230                 marginsModule->topL);
231         addCheckedLineEdit(form_->bcview(), marginsModule->bottomLE,
232                 marginsModule->bottomL);
233         addCheckedLineEdit(form_->bcview(), marginsModule->innerLE,
234                 marginsModule->innerL);
235         addCheckedLineEdit(form_->bcview(), marginsModule->outerLE,
236                 marginsModule->outerL);
237         addCheckedLineEdit(form_->bcview(), marginsModule->headsepLE,
238                 marginsModule->headsepL);
239         addCheckedLineEdit(form_->bcview(), marginsModule->headheightLE,
240                 marginsModule->headheightL);
241         addCheckedLineEdit(form_->bcview(), marginsModule->footskipLE,
242                 marginsModule->footskipL);
243
244
245
246
247
248         langModule = new UiWidget<Ui::LanguageUi>;
249     connect( langModule->defaultencodingCB, SIGNAL( toggled(bool) ), langModule->encodingL, SLOT( setDisabled(bool) ) );
250     connect( langModule->defaultencodingCB, SIGNAL( toggled(bool) ), langModule->encodingCO, SLOT( setDisabled(bool) ) );
251         // language & quote
252         connect(langModule->languageCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
253         connect(langModule->defaultencodingCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
254         connect(langModule->encodingCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
255         connect(langModule->quoteStyleCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
256         // language & quotes
257         vector<LanguagePair> const langs = getLanguageData(false);
258         vector<LanguagePair>::const_iterator lit  = langs.begin();
259         vector<LanguagePair>::const_iterator lend = langs.end();
260         for (; lit != lend; ++lit) {
261                 langModule->languageCO->insertItem(
262                         toqstr(lit->first));
263         }
264
265         int k = 0;
266         while (encodings[k]) {
267                 langModule->encodingCO->insertItem(qt_(encodings[k++]));
268         }
269
270         langModule->quoteStyleCO->insertItem(qt_("``text''"));
271         langModule->quoteStyleCO->insertItem(qt_("''text''"));
272         langModule->quoteStyleCO->insertItem(qt_(",,text``"));
273         langModule->quoteStyleCO->insertItem(qt_(",,text''"));
274         langModule->quoteStyleCO->insertItem(qt_("<<text>>"));
275         langModule->quoteStyleCO->insertItem(qt_(">>text<<"));
276
277
278
279         numberingModule = new UiWidget<Ui::NumberingUi>;
280         // numbering
281         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)), this, SLOT(change_adaptor()));
282         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)), this, SLOT(change_adaptor()));
283         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)), this, SLOT(updateNumbering()));
284         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)), this, SLOT(updateNumbering()));
285         numberingModule->tocTW->setColumnCount(3);
286         numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
287         numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
288         numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
289
290
291         biblioModule = new UiWidget<Ui::BiblioUi>;
292         connect( biblioModule->citeNatbibRB, SIGNAL( toggled(bool) ), biblioModule->citationStyleL, SLOT( setEnabled(bool) ) );
293         connect( biblioModule->citeNatbibRB, SIGNAL( toggled(bool) ), biblioModule->citeStyleCO, SLOT( setEnabled(bool) ) );
294         // biblio
295         connect(biblioModule->citeDefaultRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
296         connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
297         connect(biblioModule->citeStyleCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
298         connect(biblioModule->citeJurabibRB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
299         connect(biblioModule->bibtopicCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
300         // biblio
301         biblioModule->citeStyleCO->insertItem(qt_("Author-year"));
302         biblioModule->citeStyleCO->insertItem(qt_("Numerical"));
303         biblioModule->citeStyleCO->setCurrentItem(0);
304
305
306
307         mathsModule = new UiWidget<Ui::MathsUi>;
308     connect( mathsModule->amsautoCB, SIGNAL( toggled(bool) ), mathsModule->amsCB, SLOT( setDisabled(bool) ) );
309         // maths
310         connect(mathsModule->amsCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
311         connect(mathsModule->amsautoCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
312
313
314         latexModule = new UiWidget<Ui::LaTeXUi>;
315         // latex class
316         connect(latexModule->classCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
317         connect(latexModule->optionsLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
318         connect(latexModule->psdriverCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
319         connect(latexModule->classCO, SIGNAL(activated(int)), this, SLOT(classChanged()));
320         // packages
321         for (int n = 0; tex_graphics[n][0]; ++n) {
322                 QString enc = tex_graphics[n];
323                 latexModule->psdriverCO->insertItem(enc);
324         }
325         // latex
326         for (LyXTextClassList::const_iterator cit = textclasslist.begin();
327              cit != textclasslist.end(); ++cit) {
328                 if (cit->isTeXClassAvailable()) {
329                         latexModule->classCO->insertItem(toqstr(cit->description()));
330                 } else {
331                         string item =
332                                 bformat(_("Unavailable: %1$s"), cit->description());
333                         latexModule->classCO->insertItem(toqstr(item));
334                 }
335         }
336
337
338
339         branchesModule = new QBranches;
340         connect(branchesModule, SIGNAL(changed()), this, SLOT(change_adaptor()));
341
342
343         preambleModule = new UiWidget<Ui::PreambleUi>;
344         // preamble
345         connect(preambleModule->preambleTE, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
346
347
348         bulletsModule = new BulletsModule;
349         // bullets
350         connect(bulletsModule, SIGNAL(changed()), this, SLOT(change_adaptor()));
351
352
353         floatModule = new FloatPlacement;
354         // float
355         connect(floatModule, SIGNAL(changed()), this, SLOT(change_adaptor()));
356
357         docPS->addPanel(latexModule, _("Document Class"));
358         docPS->addPanel(textLayoutModule, _("Text Layout"));
359         docPS->addPanel(pageLayoutModule, _("Page Layout"));
360         docPS->addPanel(marginsModule, _("Page Margins"));
361         docPS->addPanel(langModule, _("Language"));
362         docPS->addPanel(numberingModule, _("Numbering & TOC"));
363         docPS->addPanel(biblioModule, _("Bibliography"));
364         docPS->addPanel(mathsModule, _("Math Options"));
365         docPS->addPanel(floatModule, _("Float Placement"));
366         docPS->addPanel(bulletsModule, _("Bullets"));
367         docPS->addPanel(branchesModule, _("Branches"));
368         docPS->addPanel(preambleModule, _("LaTeX Preamble"));
369         docPS->setCurrentPanel(_("Document Class"));
370 }
371
372
373 QDocumentDialog::~QDocumentDialog()
374 {
375 }
376
377
378 void QDocumentDialog::showPreamble()
379 {
380         docPS->setCurrentPanel(_("LaTeX Preamble"));
381 }
382
383
384 void QDocumentDialog::saveDefaultClicked()
385 {
386         form_->saveDocDefault();
387 }
388
389
390 void QDocumentDialog::useDefaultsClicked()
391 {
392         form_->useClassDefaults();
393 }
394
395
396 void QDocumentDialog::change_adaptor()
397 {
398         form_->changed();
399 }
400
401
402 void QDocumentDialog::closeEvent(QCloseEvent * e)
403 {
404         form_->slotWMHide();
405         e->accept();
406 }
407
408
409 void QDocumentDialog::setLSpacing(int item)
410 {
411         textLayoutModule->lspacingLE->setEnabled(item == 3);
412 }
413
414
415 void QDocumentDialog::setSkip(int item)
416 {
417         bool const enable = (item == 3);
418         textLayoutModule->skipLE->setEnabled(enable);
419         textLayoutModule->skipLengthCO->setEnabled(enable);
420 }
421
422
423 void QDocumentDialog::enableSkip(bool skip)
424 {
425         textLayoutModule->skipCO->setEnabled(skip);
426         textLayoutModule->skipLE->setEnabled(skip);
427         textLayoutModule->skipLengthCO->setEnabled(skip);
428         if (skip)
429                 setSkip(textLayoutModule->skipCO->currentItem());
430 }
431
432 void QDocumentDialog::portraitChanged()
433 {
434         setMargins(pageLayoutModule->papersizeCO->currentItem());
435 }
436
437 void QDocumentDialog::setMargins(bool custom)
438 {
439         marginsModule->marginCB->setChecked(custom);
440         setCustomMargins(custom);
441 }
442
443
444 void QDocumentDialog::setCustomPapersize(int papersize)
445 {
446         bool const custom = (papersize == 1);
447
448         pageLayoutModule->paperwidthL->setEnabled(custom);
449         pageLayoutModule->paperwidthLE->setEnabled(custom);
450         pageLayoutModule->paperwidthUnitCO->setEnabled(custom);
451         pageLayoutModule->paperheightL->setEnabled(custom);
452         pageLayoutModule->paperheightLE->setEnabled(custom);
453         pageLayoutModule->paperheightLE->setFocus();
454         pageLayoutModule->paperheightUnitCO->setEnabled(custom);
455 }
456
457
458 void QDocumentDialog::setCustomMargins(bool custom)
459 {
460         marginsModule->topL->setEnabled(!custom);
461         marginsModule->topLE->setEnabled(!custom);
462         marginsModule->topUnit->setEnabled(!custom);
463
464         marginsModule->bottomL->setEnabled(!custom);
465         marginsModule->bottomLE->setEnabled(!custom);
466         marginsModule->bottomUnit->setEnabled(!custom);
467
468         marginsModule->innerL->setEnabled(!custom);
469         marginsModule->innerLE->setEnabled(!custom);
470         marginsModule->innerUnit->setEnabled(!custom);
471
472         marginsModule->outerL->setEnabled(!custom);
473         marginsModule->outerLE->setEnabled(!custom);
474         marginsModule->outerUnit->setEnabled(!custom);
475
476         marginsModule->headheightL->setEnabled(!custom);
477         marginsModule->headheightLE->setEnabled(!custom);
478         marginsModule->headheightUnit->setEnabled(!custom);
479
480         marginsModule->headsepL->setEnabled(!custom);
481         marginsModule->headsepLE->setEnabled(!custom);
482         marginsModule->headsepUnit->setEnabled(!custom);
483
484         marginsModule->footskipL->setEnabled(!custom);
485         marginsModule->footskipLE->setEnabled(!custom);
486         marginsModule->footskipUnit->setEnabled(!custom);
487 }
488
489
490 void QDocumentDialog::updateFontsize(string const & items, string const & sel)
491 {
492         textLayoutModule->fontsizeCO->clear();
493         textLayoutModule->fontsizeCO->insertItem("default");
494
495         for (int n = 0; !token(items,'|',n).empty(); ++n)
496                 textLayoutModule->fontsizeCO->
497                         insertItem(toqstr(token(items,'|',n)));
498
499         for (int n = 0; n<textLayoutModule->fontsizeCO->count(); ++n) {
500                 if (fromqstr(textLayoutModule->fontsizeCO->text(n)) == sel) {
501                         textLayoutModule->fontsizeCO->setCurrentItem(n);
502                         break;
503                 }
504         }
505 }
506
507
508 void QDocumentDialog::updatePagestyle(string const & items, string const & sel)
509 {
510         pageLayoutModule->pagestyleCO->clear();
511         pageLayoutModule->pagestyleCO->insertItem("default");
512
513         for (int n=0; !token(items,'|',n).empty(); ++n)
514                 pageLayoutModule->pagestyleCO->
515                         insertItem(toqstr(token(items,'|',n)));
516
517         for (int n = 0; n<pageLayoutModule->pagestyleCO->count(); ++n) {
518                 if (fromqstr(pageLayoutModule->pagestyleCO->text(n))==sel) {
519                         pageLayoutModule->pagestyleCO->setCurrentItem(n);
520                         break;
521                 }
522         }
523 }
524
525
526 void QDocumentDialog::classChanged()
527 {
528         ControlDocument & cntrl = form_->controller();
529         BufferParams & params = cntrl.params();
530
531         lyx::textclass_type const tc = latexModule->classCO->currentItem();
532
533         if (form_->controller().loadTextclass(tc)) {
534                 params.textclass = tc;
535                 if (lyxrc.auto_reset_options)
536                         params.useClassDefaults();
537                 form_->update_contents();
538         } else {
539                 latexModule->classCO->setCurrentItem(params.textclass);
540         }
541 }
542
543
544 void QDocumentDialog::updateNumbering()
545 {
546         LyXTextClass const & tclass =
547                 form_->controller().params().getLyXTextClass();
548
549         numberingModule->tocTW->setUpdatesEnabled(false);
550         numberingModule->tocTW->clear();
551
552         int const depth = numberingModule->depthSL->value();
553         int const toc = numberingModule->tocSL->value();
554         QString const no = qt_("No");
555         QString const yes = qt_("Yes");
556         LyXTextClass::const_iterator end = tclass.end();
557         LyXTextClass::const_iterator cit = tclass.begin();
558         QTreeWidgetItem * item = 0;
559         for ( ; cit != end ; ++cit) {
560                 int const toclevel = (*cit)->toclevel;
561                 if (toclevel != LyXLayout::NOT_IN_TOC) {
562                         item = new QTreeWidgetItem(numberingModule->tocTW);
563                         item->setText(0, qt_((*cit)->name()));
564                         item->setText(1, (toclevel <= depth) ? yes : no);
565                         item->setText(2, (toclevel <= toc) ? yes : no);
566                 }
567         }
568
569         numberingModule->tocTW->setUpdatesEnabled(true);
570         numberingModule->tocTW->update();
571 }
572
573 void QDocumentDialog::apply(BufferParams & params)
574 {
575         // preamble
576         params.preamble =
577                 fromqstr(preambleModule->preambleTE->document()->toPlainText());
578
579         // biblio
580         params.cite_engine = biblio::ENGINE_BASIC;
581
582         if (biblioModule->citeNatbibRB->isChecked()) {
583                 bool const use_numerical_citations =
584                         biblioModule->citeStyleCO->currentItem();
585                 if (use_numerical_citations)
586                         params.cite_engine = biblio::ENGINE_NATBIB_NUMERICAL;
587                 else
588                         params.cite_engine = biblio::ENGINE_NATBIB_AUTHORYEAR;
589
590         } else if (biblioModule->citeJurabibRB->isChecked())
591                 params.cite_engine = biblio::ENGINE_JURABIB;
592
593         params.use_bibtopic =
594                 biblioModule->bibtopicCB->isChecked();
595
596         // language & quotes
597         if (langModule->defaultencodingCB->isChecked()) {
598                 params.inputenc = "auto";
599         } else {
600                 int i = langModule->encodingCO->currentItem();
601                 if (i == 0) {
602                         params.inputenc = "default";
603                 } else {
604                         params.inputenc = encodings[i];
605                 }
606         }
607
608         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
609         switch (langModule->quoteStyleCO->currentItem()) {
610         case 0:
611                 lga = InsetQuotes::EnglishQ;
612                 break;
613         case 1:
614                 lga = InsetQuotes::SwedishQ;
615                 break;
616         case 2:
617                 lga = InsetQuotes::GermanQ;
618                 break;
619         case 3:
620                 lga = InsetQuotes::PolishQ;
621                 break;
622         case 4:
623                 lga = InsetQuotes::FrenchQ;
624                 break;
625         case 5:
626                 lga = InsetQuotes::DanishQ;
627                 break;
628         }
629         params.quotes_language = lga;
630
631         int const pos = langModule->languageCO->currentItem();
632         params.language = languages.getLanguage(lang_[pos]);
633
634         // numbering
635         if (params.getLyXTextClass().hasTocLevels()) {
636                 params.tocdepth = numberingModule->tocSL->value();
637                 params.secnumdepth = numberingModule->depthSL->value();
638         }
639
640         // bullets
641         params.user_defined_bullet(0) = bulletsModule->getBullet(0);
642         params.user_defined_bullet(1) = bulletsModule->getBullet(1);
643         params.user_defined_bullet(2) = bulletsModule->getBullet(2);
644         params.user_defined_bullet(3) = bulletsModule->getBullet(3);
645
646         // packages
647         params.graphicsDriver =
648                 fromqstr(latexModule->psdriverCO->currentText());
649
650         if (mathsModule->amsautoCB->isChecked()) {
651                 params.use_amsmath = BufferParams::AMS_AUTO;
652         } else {
653                 if (mathsModule->amsCB->isChecked())
654                         params.use_amsmath = BufferParams::AMS_ON;
655                 else
656                         params.use_amsmath = BufferParams::AMS_OFF;
657         }
658
659         // layout
660         params.textclass =
661                 latexModule->classCO->currentItem();
662
663         params.fonts =
664                 fromqstr(textLayoutModule->fontsCO->currentText());
665
666         params.fontsize =
667                 fromqstr(textLayoutModule->fontsizeCO->currentText());
668
669         params.pagestyle =
670                 fromqstr(pageLayoutModule->pagestyleCO->currentText());
671
672         switch (textLayoutModule->lspacingCO->currentItem()) {
673         case 0:
674                 params.spacing().set(Spacing::Single);
675                 break;
676         case 1:
677                 params.spacing().set(Spacing::Onehalf);
678                 break;
679         case 2:
680                 params.spacing().set(Spacing::Double);
681                 break;
682         case 3:
683                 params.spacing().set(Spacing::Other,
684                         fromqstr(textLayoutModule->lspacingLE->text()));
685                 break;
686         }
687
688         if (textLayoutModule->twoColumnCB->isChecked())
689                 params.columns = 2;
690         else
691                 params.columns = 1;
692
693         if (textLayoutModule->indentRB->isChecked())
694                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
695         else
696                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
697
698         switch (textLayoutModule->skipCO->currentItem()) {
699         case 0:
700                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
701                 break;
702         case 1:
703                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
704                 break;
705         case 2:
706                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
707                 break;
708         case 3:
709         {
710                 VSpace vs = VSpace(
711                         widgetsToLength(textLayoutModule->skipLE,
712                                 textLayoutModule->skipLengthCO)
713                         );
714                 params.setDefSkip(vs);
715                 break;
716         }
717         default:
718                 // DocumentDefskipCB assures that this never happens
719                 // so Assert then !!!  - jbl
720                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
721                 break;
722         }
723
724         params.options =
725                 fromqstr(latexModule->optionsLE->text());
726
727         params.float_placement = floatModule->get();
728
729         // paper
730         params.papersize = PAPER_SIZE(
731                 pageLayoutModule->papersizeCO->currentItem());
732
733         // custom, A3, B3 and B4 paper sizes need geometry
734         int psize = pageLayoutModule->papersizeCO->currentItem();
735         bool geom_papersize = (psize == 1 || psize == 5 || psize == 8 || psize == 9);
736
737         params.paperwidth = widgetsToLength(pageLayoutModule->paperwidthLE,
738                 pageLayoutModule->paperwidthUnitCO);
739
740         params.paperheight = widgetsToLength(pageLayoutModule->paperheightLE,
741                 pageLayoutModule->paperheightUnitCO);
742
743         if (pageLayoutModule->facingPagesCB->isChecked())
744                 params.sides = LyXTextClass::TwoSides;
745         else
746                 params.sides = LyXTextClass::OneSide;
747
748         if (pageLayoutModule->landscapeRB->isChecked())
749                 params.orientation = ORIENTATION_LANDSCAPE;
750         else
751                 params.orientation = ORIENTATION_PORTRAIT;
752
753         // margins
754         params.use_geometry =
755                 (!marginsModule->marginCB->isChecked()
756                 || geom_papersize);
757
758         Ui::MarginsUi const * m(marginsModule);
759
760         params.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
761
762         params.topmargin = widgetsToLength(m->topLE, m->topUnit);
763
764         params.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
765
766         params.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
767
768         params.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
769
770         params.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
771
772         params.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
773
774         branchesModule->apply(params);
775 }
776
777 namespace {
778
779 /** Return the position of val in the vector if found.
780     If not found, return 0.
781  */
782 template<class A>
783 typename std::vector<A>::size_type
784 findPos(std::vector<A> const & vec, A const & val)
785 {
786         typename std::vector<A>::const_iterator it =
787                 std::find(vec.begin(), vec.end(), val);
788         if (it == vec.end())
789                 return 0;
790         return distance(vec.begin(), it);
791 }
792
793 } // namespace anom
794
795
796 void QDocumentDialog::update(BufferParams const & params)
797 {
798         // set the default unit
799         // FIXME: move to controller
800         LyXLength::UNIT defaultUnit = LyXLength::CM;
801         switch (lyxrc.default_papersize) {
802                 case PAPER_DEFAULT: break;
803
804                 case PAPER_USLETTER:
805                 case PAPER_USLEGAL:
806                 case PAPER_USEXECUTIVE:
807                         defaultUnit = LyXLength::IN;
808                         break;
809
810                 case PAPER_A3:
811                 case PAPER_A4:
812                 case PAPER_A5:
813                 case PAPER_B3:
814                 case PAPER_B4:
815                 case PAPER_B5:
816                         defaultUnit = LyXLength::CM;
817                         break;
818                 case PAPER_CUSTOM:
819                         break;
820         }
821
822         // preamble
823         QString preamble = toqstr(params.preamble);
824         preambleModule->preambleTE->document()->setPlainText(preamble);
825
826         // biblio
827         biblioModule->citeDefaultRB->setChecked(
828                 params.cite_engine == biblio::ENGINE_BASIC);
829
830         biblioModule->citeNatbibRB->setChecked(
831                 params.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL ||
832                 params.cite_engine == biblio::ENGINE_NATBIB_AUTHORYEAR);
833
834         biblioModule->citeStyleCO->setCurrentItem(
835                 params.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL);
836
837         biblioModule->citeJurabibRB->setChecked(
838                 params.cite_engine == biblio::ENGINE_JURABIB);
839
840         biblioModule->bibtopicCB->setChecked(
841                 params.use_bibtopic);
842
843         // language & quotes
844         int const pos = int(findPos(lang_,
845                                     params.language->lang()));
846         langModule->languageCO->setCurrentItem(pos);
847
848         langModule->quoteStyleCO->setCurrentItem(
849                 params.quotes_language);
850
851         langModule->defaultencodingCB->setChecked(true);
852
853         if (params.inputenc != "auto") {
854                 langModule->defaultencodingCB->setChecked(false);
855                 if (params.inputenc == "default") {
856                         langModule->encodingCO->setCurrentItem(0);
857                 } else {
858                         int i = 0;
859                         while (encodings[i]) {
860                                 if (encodings[i] == params.inputenc)
861                                         langModule->encodingCO->setCurrentItem(i);
862                                 ++i;
863                         }
864                 }
865         }
866
867         // numbering
868         int const min_toclevel = form_->controller().textClass().min_toclevel();
869         int const max_toclevel = form_->controller().textClass().max_toclevel();
870         if (form_->controller().textClass().hasTocLevels()) {
871                 numberingModule->setEnabled(true);
872                 numberingModule->depthSL->setMinValue(min_toclevel - 1);
873                 numberingModule->depthSL->setMaxValue(max_toclevel);
874                 numberingModule->depthSL->setValue(params.secnumdepth);
875                 numberingModule->tocSL->setMinValue(min_toclevel - 1);
876                 numberingModule->tocSL->setMaxValue(max_toclevel);
877                 numberingModule->tocSL->setValue(params.tocdepth);
878                 updateNumbering();
879         } else {
880                 numberingModule->setEnabled(false);
881                 numberingModule->tocTW->clear();
882         }
883
884         // bullets
885         bulletsModule->setBullet(0,params.user_defined_bullet(0));
886         bulletsModule->setBullet(1,params.user_defined_bullet(1));
887         bulletsModule->setBullet(2,params.user_defined_bullet(2));
888         bulletsModule->setBullet(3,params.user_defined_bullet(3));
889
890         // packages
891         QString text = toqstr(params.graphicsDriver);
892         int nitem = latexModule->psdriverCO->count();
893         for (int n = 0; n < nitem ; ++n) {
894                 QString enc = tex_graphics[n];
895                 if (enc == text) {
896                         latexModule->psdriverCO->setCurrentItem(n);
897                 }
898         }
899
900
901         mathsModule->amsCB->setChecked(
902                 params.use_amsmath == BufferParams::AMS_ON);
903         mathsModule->amsautoCB->setChecked(
904                 params.use_amsmath == BufferParams::AMS_AUTO);
905
906         switch (params.spacing().getSpace()) {
907                 case Spacing::Other: nitem = 3; break;
908                 case Spacing::Double: nitem = 2; break;
909                 case Spacing::Onehalf: nitem = 1; break;
910                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
911         }
912
913         // layout
914         latexModule->classCO->setCurrentItem(params.textclass);
915
916         updateFontsize(form_->controller().textClass().opt_fontsize(),
917                                 params.fontsize);
918
919         updatePagestyle(form_->controller().textClass().opt_pagestyle(),
920                                  params.pagestyle);
921
922         for (int n = 0; tex_fonts[n][0]; ++n) {
923                 if (tex_fonts[n] == params.fonts) {
924                         textLayoutModule->fontsCO->setCurrentItem(n);
925                         break;
926                 }
927         }
928
929         textLayoutModule->lspacingCO->setCurrentItem(nitem);
930         if (params.spacing().getSpace() == Spacing::Other) {
931                 textLayoutModule->lspacingLE->setText(
932                         toqstr(params.spacing().getValueAsString()));
933         }
934         setLSpacing(nitem);
935
936         if (params.paragraph_separation
937             == BufferParams::PARSEP_INDENT) {
938                 textLayoutModule->indentRB->setChecked(true);
939         } else {
940                 textLayoutModule->skipRB->setChecked(true);
941         }
942
943         int skip = 0;
944         switch (params.getDefSkip().kind()) {
945         case VSpace::SMALLSKIP:
946                 skip = 0;
947                 break;
948         case VSpace::MEDSKIP:
949                 skip = 1;
950                 break;
951         case VSpace::BIGSKIP:
952                 skip = 2;
953                 break;
954         case VSpace::LENGTH:
955         {
956                 skip = 3;
957                 string const length = params.getDefSkip().asLyXCommand();
958                 lengthToWidgets(textLayoutModule->skipLE,
959                         textLayoutModule->skipLengthCO,
960                         length, defaultUnit);
961                 break;
962         }
963         default:
964                 skip = 0;
965                 break;
966         }
967         textLayoutModule->skipCO->setCurrentItem(skip);
968         setSkip(skip);
969
970         textLayoutModule->twoColumnCB->setChecked(
971                 params.columns == 2);
972
973         if (!params.options.empty()) {
974                 latexModule->optionsLE->setText(
975                         toqstr(params.options));
976         } else {
977                 latexModule->optionsLE->setText("");
978         }
979
980         floatModule->set(params.float_placement);
981
982         // paper
983         int const psize = params.papersize;
984         pageLayoutModule->papersizeCO->setCurrentItem(psize);
985         setCustomPapersize(psize);
986
987         bool const landscape =
988                 params.orientation == ORIENTATION_LANDSCAPE;
989         pageLayoutModule->landscapeRB->setChecked(landscape);
990         pageLayoutModule->portraitRB->setChecked(!landscape);
991
992         pageLayoutModule->facingPagesCB->setChecked(
993                 params.sides == LyXTextClass::TwoSides);
994
995
996         lengthToWidgets(pageLayoutModule->paperwidthLE,
997                 pageLayoutModule->paperwidthUnitCO, params.paperwidth, defaultUnit);
998
999         lengthToWidgets(pageLayoutModule->paperheightLE,
1000                 pageLayoutModule->paperheightUnitCO, params.paperheight, defaultUnit);
1001
1002         // margins
1003         Ui::MarginsUi * m = marginsModule;
1004
1005         setMargins(!params.use_geometry);
1006
1007         lengthToWidgets(m->topLE, m->topUnit,
1008                 params.topmargin, defaultUnit);
1009
1010         lengthToWidgets(m->bottomLE, m->bottomUnit,
1011                 params.bottommargin, defaultUnit);
1012
1013         lengthToWidgets(m->innerLE, m->innerUnit,
1014                 params.leftmargin, defaultUnit);
1015
1016         lengthToWidgets(m->outerLE, m->outerUnit,
1017                 params.rightmargin, defaultUnit);
1018
1019         lengthToWidgets(m->headheightLE, m->headheightUnit,
1020                 params.headheight, defaultUnit);
1021
1022         lengthToWidgets(m->headsepLE, m->headsepUnit,
1023                 params.headsep, defaultUnit);
1024
1025         lengthToWidgets(m->footskipLE, m->footskipUnit,
1026                 params.footskip, defaultUnit);
1027
1028         branchesModule->update(params);
1029 }
1030
1031
1032
1033
1034 } // namespace frontend
1035 } // namespace lyx