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