]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QDocumentDialog.C
Qt4 compilation speedup patch by Bo Peng and me.
[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         // FIXME: disable until we fix browsebox in BulletsModule.[Ch]
350         bulletsModule->setEnabled(false);
351         // bullets
352         connect(bulletsModule, SIGNAL(changed()), this, SLOT(change_adaptor()));
353
354
355         floatModule = new FloatPlacement;
356         // float
357         connect(floatModule, SIGNAL(changed()), this, SLOT(change_adaptor()));
358
359         docPS->addPanel(latexModule, _("Document Class"));
360         docPS->addPanel(textLayoutModule, _("Text Layout"));
361         docPS->addPanel(pageLayoutModule, _("Page Layout"));
362         docPS->addPanel(marginsModule, _("Page Margins"));
363         docPS->addPanel(langModule, _("Language"));
364         docPS->addPanel(numberingModule, _("Numbering & TOC"));
365         docPS->addPanel(biblioModule, _("Bibliography"));
366         docPS->addPanel(mathsModule, _("Math Options"));
367         docPS->addPanel(floatModule, _("Float Placement"));
368         docPS->addPanel(bulletsModule, _("Bullets"));
369         docPS->addPanel(branchesModule, _("Branches"));
370         docPS->addPanel(preambleModule, _("LaTeX Preamble"));
371         docPS->setCurrentPanel(_("Document Class"));
372 }
373
374
375 QDocumentDialog::~QDocumentDialog()
376 {
377 }
378
379
380 void QDocumentDialog::showPreamble()
381 {
382         docPS->setCurrentPanel(_("LaTeX Preamble"));
383 }
384
385
386 void QDocumentDialog::saveDefaultClicked()
387 {
388         form_->saveDocDefault();
389 }
390
391
392 void QDocumentDialog::useDefaultsClicked()
393 {
394         form_->useClassDefaults();
395 }
396
397
398 void QDocumentDialog::change_adaptor()
399 {
400         form_->changed();
401 }
402
403
404 void QDocumentDialog::closeEvent(QCloseEvent * e)
405 {
406         form_->slotWMHide();
407         e->accept();
408 }
409
410
411 void QDocumentDialog::setLSpacing(int item)
412 {
413         textLayoutModule->lspacingLE->setEnabled(item == 3);
414 }
415
416
417 void QDocumentDialog::setSkip(int item)
418 {
419         bool const enable = (item == 3);
420         textLayoutModule->skipLE->setEnabled(enable);
421         textLayoutModule->skipLengthCO->setEnabled(enable);
422 }
423
424
425 void QDocumentDialog::enableSkip(bool skip)
426 {
427         textLayoutModule->skipCO->setEnabled(skip);
428         textLayoutModule->skipLE->setEnabled(skip);
429         textLayoutModule->skipLengthCO->setEnabled(skip);
430         if (skip)
431                 setSkip(textLayoutModule->skipCO->currentItem());
432 }
433
434 void QDocumentDialog::portraitChanged()
435 {
436         setMargins(pageLayoutModule->papersizeCO->currentItem());
437 }
438
439 void QDocumentDialog::setMargins(bool custom)
440 {
441         marginsModule->marginCB->setChecked(custom);
442         setCustomMargins(custom);
443 }
444
445
446 void QDocumentDialog::setCustomPapersize(int papersize)
447 {
448         bool const custom = (papersize == 1);
449
450         pageLayoutModule->paperwidthL->setEnabled(custom);
451         pageLayoutModule->paperwidthLE->setEnabled(custom);
452         pageLayoutModule->paperwidthUnitCO->setEnabled(custom);
453         pageLayoutModule->paperheightL->setEnabled(custom);
454         pageLayoutModule->paperheightLE->setEnabled(custom);
455         pageLayoutModule->paperheightLE->setFocus();
456         pageLayoutModule->paperheightUnitCO->setEnabled(custom);
457 }
458
459
460 void QDocumentDialog::setCustomMargins(bool custom)
461 {
462         marginsModule->topL->setEnabled(!custom);
463         marginsModule->topLE->setEnabled(!custom);
464         marginsModule->topUnit->setEnabled(!custom);
465
466         marginsModule->bottomL->setEnabled(!custom);
467         marginsModule->bottomLE->setEnabled(!custom);
468         marginsModule->bottomUnit->setEnabled(!custom);
469
470         marginsModule->innerL->setEnabled(!custom);
471         marginsModule->innerLE->setEnabled(!custom);
472         marginsModule->innerUnit->setEnabled(!custom);
473
474         marginsModule->outerL->setEnabled(!custom);
475         marginsModule->outerLE->setEnabled(!custom);
476         marginsModule->outerUnit->setEnabled(!custom);
477
478         marginsModule->headheightL->setEnabled(!custom);
479         marginsModule->headheightLE->setEnabled(!custom);
480         marginsModule->headheightUnit->setEnabled(!custom);
481
482         marginsModule->headsepL->setEnabled(!custom);
483         marginsModule->headsepLE->setEnabled(!custom);
484         marginsModule->headsepUnit->setEnabled(!custom);
485
486         marginsModule->footskipL->setEnabled(!custom);
487         marginsModule->footskipLE->setEnabled(!custom);
488         marginsModule->footskipUnit->setEnabled(!custom);
489 }
490
491
492 void QDocumentDialog::updateFontsize(string const & items, string const & sel)
493 {
494         textLayoutModule->fontsizeCO->clear();
495         textLayoutModule->fontsizeCO->insertItem("default");
496
497         for (int n = 0; !token(items,'|',n).empty(); ++n)
498                 textLayoutModule->fontsizeCO->
499                         insertItem(toqstr(token(items,'|',n)));
500
501         for (int n = 0; n<textLayoutModule->fontsizeCO->count(); ++n) {
502                 if (fromqstr(textLayoutModule->fontsizeCO->text(n)) == sel) {
503                         textLayoutModule->fontsizeCO->setCurrentItem(n);
504                         break;
505                 }
506         }
507 }
508
509
510 void QDocumentDialog::updatePagestyle(string const & items, string const & sel)
511 {
512         pageLayoutModule->pagestyleCO->clear();
513         pageLayoutModule->pagestyleCO->insertItem("default");
514
515         for (int n=0; !token(items,'|',n).empty(); ++n)
516                 pageLayoutModule->pagestyleCO->
517                         insertItem(toqstr(token(items,'|',n)));
518
519         for (int n = 0; n<pageLayoutModule->pagestyleCO->count(); ++n) {
520                 if (fromqstr(pageLayoutModule->pagestyleCO->text(n))==sel) {
521                         pageLayoutModule->pagestyleCO->setCurrentItem(n);
522                         break;
523                 }
524         }
525 }
526
527
528 void QDocumentDialog::classChanged()
529 {
530         ControlDocument & cntrl = form_->controller();
531         BufferParams & params = cntrl.params();
532
533         lyx::textclass_type const tc = latexModule->classCO->currentItem();
534
535         if (form_->controller().loadTextclass(tc)) {
536                 params.textclass = tc;
537                 if (lyxrc.auto_reset_options)
538                         params.useClassDefaults();
539                 form_->update_contents();
540         } else {
541                 latexModule->classCO->setCurrentItem(params.textclass);
542         }
543 }
544
545
546 void QDocumentDialog::updateNumbering()
547 {
548         LyXTextClass const & tclass =
549                 form_->controller().params().getLyXTextClass();
550
551         numberingModule->tocTW->setUpdatesEnabled(false);
552         numberingModule->tocTW->clear();
553
554         int const depth = numberingModule->depthSL->value();
555         int const toc = numberingModule->tocSL->value();
556         QString const no = qt_("No");
557         QString const yes = qt_("Yes");
558         LyXTextClass::const_iterator end = tclass.end();
559         LyXTextClass::const_iterator cit = tclass.begin();
560         QTreeWidgetItem * item = 0;
561         for ( ; cit != end ; ++cit) {
562                 int const toclevel = (*cit)->toclevel;
563                 if (toclevel != LyXLayout::NOT_IN_TOC) {
564                         item = new QTreeWidgetItem(numberingModule->tocTW);
565                         item->setText(0, qt_((*cit)->name()));
566                         item->setText(1, (toclevel <= depth) ? yes : no);
567                         item->setText(2, (toclevel <= toc) ? yes : no);
568                 }
569         }
570
571         numberingModule->tocTW->setUpdatesEnabled(true);
572         numberingModule->tocTW->update();
573 }
574
575 void QDocumentDialog::apply(BufferParams & params)
576 {
577         // preamble
578         params.preamble =
579                 fromqstr(preambleModule->preambleTE->document()->toPlainText());
580
581         // biblio
582         params.cite_engine = biblio::ENGINE_BASIC;
583
584         if (biblioModule->citeNatbibRB->isChecked()) {
585                 bool const use_numerical_citations =
586                         biblioModule->citeStyleCO->currentItem();
587                 if (use_numerical_citations)
588                         params.cite_engine = biblio::ENGINE_NATBIB_NUMERICAL;
589                 else
590                         params.cite_engine = biblio::ENGINE_NATBIB_AUTHORYEAR;
591
592         } else if (biblioModule->citeJurabibRB->isChecked())
593                 params.cite_engine = biblio::ENGINE_JURABIB;
594
595         params.use_bibtopic =
596                 biblioModule->bibtopicCB->isChecked();
597
598         // language & quotes
599         if (langModule->defaultencodingCB->isChecked()) {
600                 params.inputenc = "auto";
601         } else {
602                 int i = langModule->encodingCO->currentItem();
603                 if (i == 0) {
604                         params.inputenc = "default";
605                 } else {
606                         params.inputenc = encodings[i];
607                 }
608         }
609
610         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
611         switch (langModule->quoteStyleCO->currentItem()) {
612         case 0:
613                 lga = InsetQuotes::EnglishQ;
614                 break;
615         case 1:
616                 lga = InsetQuotes::SwedishQ;
617                 break;
618         case 2:
619                 lga = InsetQuotes::GermanQ;
620                 break;
621         case 3:
622                 lga = InsetQuotes::PolishQ;
623                 break;
624         case 4:
625                 lga = InsetQuotes::FrenchQ;
626                 break;
627         case 5:
628                 lga = InsetQuotes::DanishQ;
629                 break;
630         }
631         params.quotes_language = lga;
632
633         int const pos = langModule->languageCO->currentItem();
634         params.language = languages.getLanguage(lang_[pos]);
635
636         // numbering
637         if (params.getLyXTextClass().hasTocLevels()) {
638                 params.tocdepth = numberingModule->tocSL->value();
639                 params.secnumdepth = numberingModule->depthSL->value();
640         }
641
642         // bullets
643         params.user_defined_bullet(0) = bulletsModule->getBullet(0);
644         params.user_defined_bullet(1) = bulletsModule->getBullet(1);
645         params.user_defined_bullet(2) = bulletsModule->getBullet(2);
646         params.user_defined_bullet(3) = bulletsModule->getBullet(3);
647
648         // packages
649         params.graphicsDriver =
650                 fromqstr(latexModule->psdriverCO->currentText());
651
652         if (mathsModule->amsautoCB->isChecked()) {
653                 params.use_amsmath = BufferParams::AMS_AUTO;
654         } else {
655                 if (mathsModule->amsCB->isChecked())
656                         params.use_amsmath = BufferParams::AMS_ON;
657                 else
658                         params.use_amsmath = BufferParams::AMS_OFF;
659         }
660
661         // layout
662         params.textclass =
663                 latexModule->classCO->currentItem();
664
665         params.fonts =
666                 fromqstr(textLayoutModule->fontsCO->currentText());
667
668         params.fontsize =
669                 fromqstr(textLayoutModule->fontsizeCO->currentText());
670
671         params.pagestyle =
672                 fromqstr(pageLayoutModule->pagestyleCO->currentText());
673
674         switch (textLayoutModule->lspacingCO->currentItem()) {
675         case 0:
676                 params.spacing().set(Spacing::Single);
677                 break;
678         case 1:
679                 params.spacing().set(Spacing::Onehalf);
680                 break;
681         case 2:
682                 params.spacing().set(Spacing::Double);
683                 break;
684         case 3:
685                 params.spacing().set(Spacing::Other,
686                         fromqstr(textLayoutModule->lspacingLE->text()));
687                 break;
688         }
689
690         if (textLayoutModule->twoColumnCB->isChecked())
691                 params.columns = 2;
692         else
693                 params.columns = 1;
694
695         if (textLayoutModule->indentRB->isChecked())
696                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
697         else
698                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
699
700         switch (textLayoutModule->skipCO->currentItem()) {
701         case 0:
702                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
703                 break;
704         case 1:
705                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
706                 break;
707         case 2:
708                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
709                 break;
710         case 3:
711         {
712                 VSpace vs = VSpace(
713                         widgetsToLength(textLayoutModule->skipLE,
714                                 textLayoutModule->skipLengthCO)
715                         );
716                 params.setDefSkip(vs);
717                 break;
718         }
719         default:
720                 // DocumentDefskipCB assures that this never happens
721                 // so Assert then !!!  - jbl
722                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
723                 break;
724         }
725
726         params.options =
727                 fromqstr(latexModule->optionsLE->text());
728
729         params.float_placement = floatModule->get();
730
731         // paper
732         params.papersize = PAPER_SIZE(
733                 pageLayoutModule->papersizeCO->currentItem());
734
735         // custom, A3, B3 and B4 paper sizes need geometry
736         int psize = pageLayoutModule->papersizeCO->currentItem();
737         bool geom_papersize = (psize == 1 || psize == 5 || psize == 8 || psize == 9);
738
739         params.paperwidth = widgetsToLength(pageLayoutModule->paperwidthLE,
740                 pageLayoutModule->paperwidthUnitCO);
741
742         params.paperheight = widgetsToLength(pageLayoutModule->paperheightLE,
743                 pageLayoutModule->paperheightUnitCO);
744
745         if (pageLayoutModule->facingPagesCB->isChecked())
746                 params.sides = LyXTextClass::TwoSides;
747         else
748                 params.sides = LyXTextClass::OneSide;
749
750         if (pageLayoutModule->landscapeRB->isChecked())
751                 params.orientation = ORIENTATION_LANDSCAPE;
752         else
753                 params.orientation = ORIENTATION_PORTRAIT;
754
755         // margins
756         params.use_geometry =
757                 (!marginsModule->marginCB->isChecked()
758                 || geom_papersize);
759
760         Ui::MarginsUi const * m(marginsModule);
761
762         params.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
763
764         params.topmargin = widgetsToLength(m->topLE, m->topUnit);
765
766         params.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
767
768         params.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
769
770         params.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
771
772         params.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
773
774         params.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
775
776         branchesModule->apply(params);
777 }
778
779 namespace {
780
781 /** Return the position of val in the vector if found.
782     If not found, return 0.
783  */
784 template<class A>
785 typename std::vector<A>::size_type
786 findPos(std::vector<A> const & vec, A const & val)
787 {
788         typename std::vector<A>::const_iterator it =
789                 std::find(vec.begin(), vec.end(), val);
790         if (it == vec.end())
791                 return 0;
792         return distance(vec.begin(), it);
793 }
794
795 } // namespace anom
796
797
798 void QDocumentDialog::update(BufferParams const & params)
799 {
800         // set the default unit
801         // FIXME: move to controller
802         LyXLength::UNIT defaultUnit = LyXLength::CM;
803         switch (lyxrc.default_papersize) {
804                 case PAPER_DEFAULT: break;
805
806                 case PAPER_USLETTER:
807                 case PAPER_USLEGAL:
808                 case PAPER_USEXECUTIVE:
809                         defaultUnit = LyXLength::IN;
810                         break;
811
812                 case PAPER_A3:
813                 case PAPER_A4:
814                 case PAPER_A5:
815                 case PAPER_B3:
816                 case PAPER_B4:
817                 case PAPER_B5:
818                         defaultUnit = LyXLength::CM;
819                         break;
820                 case PAPER_CUSTOM:
821                         break;
822         }
823
824         // preamble
825         QString preamble = toqstr(params.preamble);
826         preambleModule->preambleTE->document()->setPlainText(preamble);
827
828         // biblio
829         biblioModule->citeDefaultRB->setChecked(
830                 params.cite_engine == biblio::ENGINE_BASIC);
831
832         biblioModule->citeNatbibRB->setChecked(
833                 params.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL ||
834                 params.cite_engine == biblio::ENGINE_NATBIB_AUTHORYEAR);
835
836         biblioModule->citeStyleCO->setCurrentItem(
837                 params.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL);
838
839         biblioModule->citeJurabibRB->setChecked(
840                 params.cite_engine == biblio::ENGINE_JURABIB);
841
842         biblioModule->bibtopicCB->setChecked(
843                 params.use_bibtopic);
844
845         // language & quotes
846         int const pos = int(findPos(lang_,
847                                     params.language->lang()));
848         langModule->languageCO->setCurrentItem(pos);
849
850         langModule->quoteStyleCO->setCurrentItem(
851                 params.quotes_language);
852
853         langModule->defaultencodingCB->setChecked(true);
854
855         if (params.inputenc != "auto") {
856                 langModule->defaultencodingCB->setChecked(false);
857                 if (params.inputenc == "default") {
858                         langModule->encodingCO->setCurrentItem(0);
859                 } else {
860                         int i = 0;
861                         while (encodings[i]) {
862                                 if (encodings[i] == params.inputenc)
863                                         langModule->encodingCO->setCurrentItem(i);
864                                 ++i;
865                         }
866                 }
867         }
868
869         // numbering
870         int const min_toclevel = form_->controller().textClass().min_toclevel();
871         int const max_toclevel = form_->controller().textClass().max_toclevel();
872         if (form_->controller().textClass().hasTocLevels()) {
873                 numberingModule->setEnabled(true);
874                 numberingModule->depthSL->setMinValue(min_toclevel - 1);
875                 numberingModule->depthSL->setMaxValue(max_toclevel);
876                 numberingModule->depthSL->setValue(params.secnumdepth);
877                 numberingModule->tocSL->setMinValue(min_toclevel - 1);
878                 numberingModule->tocSL->setMaxValue(max_toclevel);
879                 numberingModule->tocSL->setValue(params.tocdepth);
880                 updateNumbering();
881         } else {
882                 numberingModule->setEnabled(false);
883                 numberingModule->tocTW->clear();
884         }
885
886         // bullets
887         bulletsModule->setBullet(0,params.user_defined_bullet(0));
888         bulletsModule->setBullet(1,params.user_defined_bullet(1));
889         bulletsModule->setBullet(2,params.user_defined_bullet(2));
890         bulletsModule->setBullet(3,params.user_defined_bullet(3));
891
892         // packages
893         QString text = toqstr(params.graphicsDriver);
894         int nitem = latexModule->psdriverCO->count();
895         for (int n = 0; n < nitem ; ++n) {
896                 QString enc = tex_graphics[n];
897                 if (enc == text) {
898                         latexModule->psdriverCO->setCurrentItem(n);
899                 }
900         }
901
902
903         mathsModule->amsCB->setChecked(
904                 params.use_amsmath == BufferParams::AMS_ON);
905         mathsModule->amsautoCB->setChecked(
906                 params.use_amsmath == BufferParams::AMS_AUTO);
907
908         switch (params.spacing().getSpace()) {
909                 case Spacing::Other: nitem = 3; break;
910                 case Spacing::Double: nitem = 2; break;
911                 case Spacing::Onehalf: nitem = 1; break;
912                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
913         }
914
915         // layout
916         latexModule->classCO->setCurrentItem(params.textclass);
917
918         updateFontsize(form_->controller().textClass().opt_fontsize(),
919                                 params.fontsize);
920
921         updatePagestyle(form_->controller().textClass().opt_pagestyle(),
922                                  params.pagestyle);
923
924         for (int n = 0; tex_fonts[n][0]; ++n) {
925                 if (tex_fonts[n] == params.fonts) {
926                         textLayoutModule->fontsCO->setCurrentItem(n);
927                         break;
928                 }
929         }
930
931         textLayoutModule->lspacingCO->setCurrentItem(nitem);
932         if (params.spacing().getSpace() == Spacing::Other) {
933                 textLayoutModule->lspacingLE->setText(
934                         toqstr(params.spacing().getValueAsString()));
935         }
936         setLSpacing(nitem);
937
938         if (params.paragraph_separation
939             == BufferParams::PARSEP_INDENT) {
940                 textLayoutModule->indentRB->setChecked(true);
941         } else {
942                 textLayoutModule->skipRB->setChecked(true);
943         }
944
945         int skip = 0;
946         switch (params.getDefSkip().kind()) {
947         case VSpace::SMALLSKIP:
948                 skip = 0;
949                 break;
950         case VSpace::MEDSKIP:
951                 skip = 1;
952                 break;
953         case VSpace::BIGSKIP:
954                 skip = 2;
955                 break;
956         case VSpace::LENGTH:
957         {
958                 skip = 3;
959                 string const length = params.getDefSkip().asLyXCommand();
960                 lengthToWidgets(textLayoutModule->skipLE,
961                         textLayoutModule->skipLengthCO,
962                         length, defaultUnit);
963                 break;
964         }
965         default:
966                 skip = 0;
967                 break;
968         }
969         textLayoutModule->skipCO->setCurrentItem(skip);
970         setSkip(skip);
971
972         textLayoutModule->twoColumnCB->setChecked(
973                 params.columns == 2);
974
975         if (!params.options.empty()) {
976                 latexModule->optionsLE->setText(
977                         toqstr(params.options));
978         } else {
979                 latexModule->optionsLE->setText("");
980         }
981
982         floatModule->set(params.float_placement);
983
984         // paper
985         int const psize = params.papersize;
986         pageLayoutModule->papersizeCO->setCurrentItem(psize);
987         setCustomPapersize(psize);
988
989         bool const landscape =
990                 params.orientation == ORIENTATION_LANDSCAPE;
991         pageLayoutModule->landscapeRB->setChecked(landscape);
992         pageLayoutModule->portraitRB->setChecked(!landscape);
993
994         pageLayoutModule->facingPagesCB->setChecked(
995                 params.sides == LyXTextClass::TwoSides);
996
997
998         lengthToWidgets(pageLayoutModule->paperwidthLE,
999                 pageLayoutModule->paperwidthUnitCO, params.paperwidth, defaultUnit);
1000
1001         lengthToWidgets(pageLayoutModule->paperheightLE,
1002                 pageLayoutModule->paperheightUnitCO, params.paperheight, defaultUnit);
1003
1004         // margins
1005         Ui::MarginsUi * m = marginsModule;
1006
1007         setMargins(!params.use_geometry);
1008
1009         lengthToWidgets(m->topLE, m->topUnit,
1010                 params.topmargin, defaultUnit);
1011
1012         lengthToWidgets(m->bottomLE, m->bottomUnit,
1013                 params.bottommargin, defaultUnit);
1014
1015         lengthToWidgets(m->innerLE, m->innerUnit,
1016                 params.leftmargin, defaultUnit);
1017
1018         lengthToWidgets(m->outerLE, m->outerUnit,
1019                 params.rightmargin, defaultUnit);
1020
1021         lengthToWidgets(m->headheightLE, m->headheightUnit,
1022                 params.headheight, defaultUnit);
1023
1024         lengthToWidgets(m->headsepLE, m->headsepUnit,
1025                 params.headsep, defaultUnit);
1026
1027         lengthToWidgets(m->footskipLE, m->footskipUnit,
1028                 params.footskip, defaultUnit);
1029
1030         branchesModule->update(params);
1031 }
1032
1033
1034
1035
1036 } // namespace frontend
1037 } // namespace lyx
1038
1039 #include "QDocumentDialog_moc.cpp"