]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QDocument.C
Overhaul the branches code.
[features.git] / src / frontends / qt2 / QDocument.C
1 /**
2  * \file QDocument.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "debug.h"
14 #include "qt_helpers.h"
15
16 #include "ControlDocument.h"
17 #include "QDocument.h"
18 #include "QDocumentDialog.h"
19
20 #include "bufferparams.h"
21 #include "language.h"
22 #include "helper_funcs.h" // getSecond()
23 #include "frnt_lang.h"
24 #include "lyxrc.h" // defaultUnit
25 #include "tex-strings.h" // tex_graphics
26 #include "support/tostr.h"
27 #include "support/lstrings.h"
28 #include "lyxtextclasslist.h"
29 #include "floatplacement.h"
30
31 #include <qpushbutton.h>
32 #include <qmultilineedit.h>
33 #include <qradiobutton.h>
34 #include <qcheckbox.h>
35 #include <qslider.h>
36 #include <qlineedit.h>
37 #include "lengthcombo.h"
38
39
40 using lyx::support::bformat;
41 using lyx::support::getVectorFromString;
42
43 using std::vector;
44 using std::string;
45
46
47 typedef Qt2CB<ControlDocument, Qt2DB<QDocumentDialog> > base_class;
48
49
50 namespace {
51
52 char const * encodings[] = { "LaTeX default", "latin1", "latin2",
53         "latin3", "latin4", "latin5", "latin9",
54         "koi8-r", "koi8-u", "cp866", "cp1251",
55         "iso88595", "pt154", 0
56 };
57
58 }
59
60
61 QDocument::QDocument()
62         : base_class(_("LyX: Document Settings")),
63                 lang_(getSecond(frnt::getLanguageData(false)))
64 {}
65
66
67 void QDocument::build_dialog()
68 {
69         dialog_.reset(new QDocumentDialog(this));
70
71         // biblio
72         dialog_->biblioModule->citeStyleCO->insertItem(qt_("Author-year"));
73         dialog_->biblioModule->citeStyleCO->insertItem(qt_("Numerical"));
74         dialog_->biblioModule->citeStyleCO->setCurrentItem(0);
75
76         // language & quotes
77         vector<frnt::LanguagePair> const langs = frnt::getLanguageData(false);
78         vector<frnt::LanguagePair>::const_iterator lit  = langs.begin();
79         vector<frnt::LanguagePair>::const_iterator lend = langs.end();
80         for (; lit != lend; ++lit) {
81                 dialog_->langModule->languageCO->insertItem(
82                         toqstr(lit->first));
83         }
84
85         int i = 0;
86         while (encodings[i]) {
87                 dialog_->langModule->encodingCO->insertItem(qt_(encodings[i++]));
88         }
89
90         dialog_->langModule->quoteStyleCO->insertItem(qt_("``text''"));
91         dialog_->langModule->quoteStyleCO->insertItem(qt_("''text''"));
92         dialog_->langModule->quoteStyleCO->insertItem(qt_(",,text``"));
93         dialog_->langModule->quoteStyleCO->insertItem(qt_(",,text''"));
94         dialog_->langModule->quoteStyleCO->insertItem(qt_("<<text>>"));
95         dialog_->langModule->quoteStyleCO->insertItem(qt_(">>text<<"));
96
97         // packages
98         for (int n = 0; tex_graphics[n][0]; ++n) {
99                 QString enc = tex_graphics[n];
100                 dialog_->latexModule->psdriverCO->insertItem(enc);
101         }
102
103         // paper
104         QComboBox * cb = dialog_->pageLayoutModule->papersizeCO;
105         cb->insertItem(qt_("Default"));
106         cb->insertItem(qt_("Custom"));
107         cb->insertItem(qt_("US letter"));
108         cb->insertItem(qt_("US legal"));
109         cb->insertItem(qt_("US executive"));
110         cb->insertItem(qt_("A3"));
111         cb->insertItem(qt_("A4"));
112         cb->insertItem(qt_("A5"));
113         cb->insertItem(qt_("B3"));
114         cb->insertItem(qt_("B4"));
115         cb->insertItem(qt_("B5"));
116         // remove the %-items from the unit choice
117         dialog_->pageLayoutModule->paperwidthUnitCO->noPercents();
118         dialog_->pageLayoutModule->paperheightUnitCO->noPercents();
119
120         // layout
121         for (LyXTextClassList::const_iterator cit = textclasslist.begin();
122              cit != textclasslist.end(); ++cit) {
123                 if (cit->isTeXClassAvailable()) {
124                         dialog_->latexModule->classCO->insertItem(toqstr(cit->description()));
125                 } else {
126                         string item =
127                                 bformat(_("Unavailable: %1$s"), cit->description());
128                         dialog_->latexModule->classCO->insertItem(toqstr(item));
129                 }
130         }
131
132         for (int n = 0; tex_fonts[n][0]; ++n) {
133                 QString font = tex_fonts[n];
134                 dialog_->textLayoutModule->fontsCO->insertItem(font);
135         }
136
137         dialog_->textLayoutModule->fontsizeCO->insertItem(qt_("default"));
138         dialog_->textLayoutModule->fontsizeCO->insertItem(qt_("10"));
139         dialog_->textLayoutModule->fontsizeCO->insertItem(qt_("11"));
140         dialog_->textLayoutModule->fontsizeCO->insertItem(qt_("12"));
141
142         dialog_->textLayoutModule->skipCO->insertItem(qt_("SmallSkip"));
143         dialog_->textLayoutModule->skipCO->insertItem(qt_("MedSkip"));
144         dialog_->textLayoutModule->skipCO->insertItem(qt_("BigSkip"));
145         dialog_->textLayoutModule->skipCO->insertItem(qt_("Length"));
146         // remove the %-items from the unit choice
147         dialog_->textLayoutModule->skipLengthCO->noPercents();
148
149         dialog_->pageLayoutModule->pagestyleCO->insertItem(qt_("default"));
150         dialog_->pageLayoutModule->pagestyleCO->insertItem(qt_("empty"));
151         dialog_->pageLayoutModule->pagestyleCO->insertItem(qt_("plain"));
152         dialog_->pageLayoutModule->pagestyleCO->insertItem(qt_("headings"));
153         dialog_->pageLayoutModule->pagestyleCO->insertItem(qt_("fancy"));
154
155         dialog_->textLayoutModule->lspacingCO->insertItem(
156                 qt_("Single"), Spacing::Single);
157         dialog_->textLayoutModule->lspacingCO->insertItem(
158                 qt_("OneHalf"), Spacing::Onehalf);
159         dialog_->textLayoutModule->lspacingCO->insertItem(
160                 qt_("Double"), Spacing::Double);
161         dialog_->textLayoutModule->lspacingCO->insertItem(
162                 qt_("Custom"), Spacing::Other);
163
164         // margins
165         dialog_->setMargins(0);
166
167         // Manage the restore, ok, apply, restore and cancel/close buttons
168         bcview().setOK(dialog_->okPB);
169         bcview().setApply(dialog_->applyPB);
170         bcview().setCancel(dialog_->closePB);
171         bcview().setRestore(dialog_->restorePB);
172 }
173
174
175 void QDocument::showPreamble()
176 {
177         dialog_->showPreamble();
178 }
179
180
181 void QDocument::apply()
182 {
183         BufferParams & params = controller().params();
184
185         // preamble
186         params.preamble =
187                 fromqstr(dialog_->preambleModule->preambleMLE->text());
188
189         // biblio
190         params.use_natbib =
191                 dialog_->biblioModule->natbibCB->isChecked();
192         params.use_numerical_citations  =
193                 dialog_->biblioModule->citeStyleCO->currentItem();
194
195         // language & quotes
196         if (dialog_->langModule->singleQuoteRB->isChecked())
197                 params.quotes_times = InsetQuotes::SingleQ;
198         else
199                 params.quotes_times = InsetQuotes::DoubleQ;
200
201         if (dialog_->langModule->defaultencodingCB->isChecked()) {
202                 params.inputenc = "auto";
203         } else {
204                 int i = dialog_->langModule->encodingCO->currentItem();
205                 if (i == 0) {
206                         params.inputenc = "default";
207                 } else {
208                         params.inputenc = encodings[i];
209                 }
210         }
211
212         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
213         switch (dialog_->langModule->quoteStyleCO->currentItem()) {
214         case 0:
215                 lga = InsetQuotes::EnglishQ;
216                 break;
217         case 1:
218                 lga = InsetQuotes::SwedishQ;
219                 break;
220         case 2:
221                 lga = InsetQuotes::GermanQ;
222                 break;
223         case 3:
224                 lga = InsetQuotes::PolishQ;
225                 break;
226         case 4:
227                 lga = InsetQuotes::FrenchQ;
228                 break;
229         case 5:
230                 lga = InsetQuotes::DanishQ;
231                 break;
232         }
233         params.quotes_language = lga;
234
235         int const pos = dialog_->langModule->languageCO->currentItem();
236         params.language = languages.getLanguage(lang_[pos]);
237
238         // numbering
239         params.tocdepth = dialog_->numberingModule->tocSL->value();
240         params.secnumdepth = dialog_->numberingModule->depthSL->value();
241
242         // bullets
243         params.user_defined_bullet(0) = dialog_->bulletsModule->getBullet(0);
244         params.user_defined_bullet(1) = dialog_->bulletsModule->getBullet(1);
245         params.user_defined_bullet(2) = dialog_->bulletsModule->getBullet(2);
246         params.user_defined_bullet(3) = dialog_->bulletsModule->getBullet(3);
247
248         // packages
249         params.graphicsDriver =
250                 fromqstr(dialog_->latexModule->psdriverCO->currentText());
251
252         if (dialog_->mathsModule->amsautoCB->isChecked()) {
253                 params.use_amsmath = BufferParams::AMS_AUTO;
254         } else {
255                 if (dialog_->mathsModule->amsCB->isChecked())
256                         params.use_amsmath = BufferParams::AMS_ON;
257                 else
258                         params.use_amsmath = BufferParams::AMS_OFF;
259         }
260
261         // layout
262         params.textclass =
263                 dialog_->latexModule->classCO->currentItem();
264
265         params.fonts =
266                 fromqstr(dialog_->textLayoutModule->fontsCO->currentText());
267
268         params.fontsize =
269                 fromqstr(dialog_->textLayoutModule->fontsizeCO->currentText());
270
271         params.pagestyle =
272                 fromqstr(dialog_->pageLayoutModule->pagestyleCO->currentText());
273
274         switch (dialog_->textLayoutModule->lspacingCO->currentItem()) {
275         case 0:
276                 params.spacing().set(Spacing::Single);
277                 break;
278         case 1:
279                 params.spacing().set(Spacing::Onehalf);
280                 break;
281         case 2:
282                 params.spacing().set(Spacing::Double);
283                 break;
284         case 3:
285                 params.spacing().set(Spacing::Other,
286                                    dialog_->textLayoutModule->
287                                    lspacingLE->text().toFloat()
288                                    );
289                 break;
290         }
291
292         if (dialog_->textLayoutModule->twoColumnCB->isChecked())
293                 params.columns = 2;
294         else
295                 params.columns = 1;
296
297         if (dialog_->textLayoutModule->indentRB->isChecked())
298                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
299         else
300                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
301
302         switch (dialog_->textLayoutModule->skipCO->currentItem()) {
303         case 0:
304                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
305                 break;
306         case 1:
307                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
308                 break;
309         case 2:
310                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
311                 break;
312         case 3:
313         {
314                 LyXLength::UNIT unit =
315                         dialog_->textLayoutModule->skipLengthCO->
316                         currentLengthItem();
317                 double length =
318                         dialog_->textLayoutModule->skipLE->text().toDouble();
319                 VSpace vs = VSpace(LyXGlueLength(LyXLength(length,unit)));
320                 params.setDefSkip(vs);
321                 break;
322         }
323         default:
324                 // DocumentDefskipCB assures that this never happens
325                 // so Assert then !!!  - jbl
326                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
327                 break;
328         }
329
330         params.options =
331                 fromqstr(dialog_->latexModule->optionsLE->text());
332
333         params.float_placement = dialog_->floatModule->get();
334
335         // paper
336         params.papersize2 = VMARGIN_PAPER_TYPE(
337                 dialog_->pageLayoutModule->papersizeCO->currentItem());
338
339         // custom, A3, B3 and B4 paper sizes need geometry
340         int psize = dialog_->pageLayoutModule->papersizeCO->currentItem();
341         bool geom_papersize = (psize == 1 || psize == 5 || psize == 8 || psize == 9);
342
343         params.paperwidth = widgetsToLength(dialog_->pageLayoutModule->paperwidthLE,
344                 dialog_->pageLayoutModule->paperwidthUnitCO);
345
346         params.paperheight = widgetsToLength(dialog_->pageLayoutModule->paperheightLE,
347                 dialog_->pageLayoutModule->paperheightUnitCO);
348
349         if (dialog_->pageLayoutModule->facingPagesCB->isChecked())
350                 params.sides = LyXTextClass::TwoSides;
351         else
352                 params.sides = LyXTextClass::OneSide;
353
354         if (dialog_->pageLayoutModule->landscapeRB->isChecked())
355                 params.orientation = ORIENTATION_LANDSCAPE;
356         else
357                 params.orientation = ORIENTATION_PORTRAIT;
358
359         // margins
360         params.use_geometry =
361                 (dialog_->marginsModule->marginCO->currentItem() == 1
362                 || geom_papersize);
363
364         int margin = dialog_->marginsModule->marginCO->currentItem();
365         if (margin > 0) {
366                 margin = margin - 1;
367         }
368         params.paperpackage = PAPER_PACKAGES(margin);
369
370         // set params.papersize from params.papersize2
371         // and params.paperpackage
372         params.setPaperStuff();
373
374         MarginsModuleBase const * m(dialog_->marginsModule);
375
376         params.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
377
378         params.topmargin = widgetsToLength(m->topLE, m->topUnit);
379
380         params.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
381
382         params.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
383
384         params.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
385
386         params.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
387
388         params.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
389
390         params.branchlist() = branchlist_;
391 }
392
393
394 namespace {
395
396 /** Return the position of val in the vector if found.
397     If not found, return 0.
398  */
399 template<class A>
400 typename std::vector<A>::size_type
401 findPos(std::vector<A> const & vec, A const & val)
402 {
403         typename std::vector<A>::const_iterator it =
404                 std::find(vec.begin(), vec.end(), val);
405         if (it == vec.end())
406                 return 0;
407         return std::distance(vec.begin(), it);
408 }
409
410 } // namespace anom
411
412
413 void QDocument::update_contents()
414 {
415         if (!dialog_.get())
416                 return;
417
418         BufferParams const & params = controller().params();
419
420         // set the default unit
421         // FIXME: move to controller
422         LyXLength::UNIT defaultUnit = LyXLength::CM;
423         switch (lyxrc.default_papersize) {
424                 case PAPER_DEFAULT: break;
425
426                 case PAPER_USLETTER:
427                 case PAPER_LEGALPAPER:
428                 case PAPER_EXECUTIVEPAPER:
429                         defaultUnit = LyXLength::IN;
430                         break;
431
432                 case PAPER_A3PAPER:
433                 case PAPER_A4PAPER:
434                 case PAPER_A5PAPER:
435                 case PAPER_B5PAPER:
436                         defaultUnit = LyXLength::CM;
437                         break;
438         }
439
440         // preamble
441         QString preamble = toqstr(params.preamble);
442         dialog_->preambleModule->preambleMLE->setText(preamble);
443
444         // biblio
445         dialog_->biblioModule->natbibCB->setChecked(
446                 params.use_natbib);
447         dialog_->biblioModule->citeStyleCO->setCurrentItem(
448                 params.use_numerical_citations ? 1 : 0);
449
450         // language & quotes
451         dialog_->langModule->singleQuoteRB->setChecked(
452                 params.quotes_times == InsetQuotes::SingleQ);
453         dialog_->langModule->doubleQuoteRB->setChecked(
454                 params.quotes_times == InsetQuotes::DoubleQ);
455
456         int const pos = int(findPos(lang_,
457                                     params.language->lang()));
458         dialog_->langModule->languageCO->setCurrentItem(pos);
459
460         dialog_->langModule->quoteStyleCO->setCurrentItem(
461                 params.quotes_language);
462
463         dialog_->langModule->defaultencodingCB->setChecked(true);
464
465         if (params.inputenc != "auto") {
466                 dialog_->langModule->defaultencodingCB->setChecked(false);
467                 if (params.inputenc == "default") {
468                         dialog_->langModule->encodingCO->setCurrentItem(0);
469                 } else {
470                         int i = 0;
471                         while (encodings[i]) {
472                                 if (encodings[i] == params.inputenc)
473                                         dialog_->langModule->encodingCO->setCurrentItem(i);
474                                 ++i;
475                         }
476                 }
477         }
478
479         // numbering
480         dialog_->numberingModule->tocSL->setValue(params.tocdepth);
481         dialog_->numberingModule->depthSL->setValue(params.secnumdepth);
482         dialog_->updateNumbering();
483
484         // bullets
485         dialog_->bulletsModule->setBullet(0,params.user_defined_bullet(0));
486         dialog_->bulletsModule->setBullet(1,params.user_defined_bullet(1));
487         dialog_->bulletsModule->setBullet(2,params.user_defined_bullet(2));
488         dialog_->bulletsModule->setBullet(3,params.user_defined_bullet(3));
489
490         // packages
491         QString text = toqstr(params.graphicsDriver);
492         int nitem = dialog_->latexModule->psdriverCO->count();
493         for (int n = 0; n < nitem ; ++n) {
494                 QString enc = tex_graphics[n];
495                 if (enc == text) {
496                         dialog_->latexModule->psdriverCO->setCurrentItem(n);
497                 }
498         }
499
500
501         dialog_->mathsModule->amsCB->setChecked(
502                 params.use_amsmath == BufferParams::AMS_ON);
503         dialog_->mathsModule->amsautoCB->setChecked(
504                 params.use_amsmath == BufferParams::AMS_AUTO);
505
506         switch (params.spacing().getSpace()) {
507                 case Spacing::Other: nitem = 3; break;
508                 case Spacing::Double: nitem = 2; break;
509                 case Spacing::Onehalf: nitem = 1; break;
510                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
511         }
512
513
514         // layout
515         dialog_->latexModule->classCO->setCurrentItem(params.textclass);
516
517         dialog_->updateFontsize(controller().textClass().opt_fontsize(),
518                                 params.fontsize);
519
520         dialog_->updatePagestyle(controller().textClass().opt_pagestyle(),
521                                  params.pagestyle);
522
523         for (int n = 0; tex_fonts[n][0]; ++n) {
524                 if (tex_fonts[n] == params.fonts) {
525                         dialog_->textLayoutModule->fontsCO->setCurrentItem(n);
526                         break;
527                 }
528         }
529
530         dialog_->textLayoutModule->lspacingCO->setCurrentItem(nitem);
531         if (params.spacing().getSpace() == Spacing::Other) {
532                 dialog_->textLayoutModule->lspacingLE->setText(
533                         toqstr(tostr(params.spacing().getValue())));
534                 dialog_->setLSpacing(3);
535         }
536
537         if (params.paragraph_separation
538             == BufferParams::PARSEP_INDENT) {
539                 dialog_->textLayoutModule->indentRB->setChecked(true);
540         } else {
541                 dialog_->textLayoutModule->skipRB->setChecked(true);
542         }
543
544         int skip = 0;
545         switch (params.getDefSkip().kind()) {
546         case VSpace::SMALLSKIP:
547                 skip = 0;
548                 break;
549         case VSpace::MEDSKIP:
550                 skip = 1;
551                 break;
552         case VSpace::BIGSKIP:
553                 skip = 2;
554                 break;
555         case VSpace::LENGTH:
556         {
557                 skip = 3;
558                 string const length = params.getDefSkip().asLyXCommand();
559                 dialog_->textLayoutModule->skipLengthCO->setCurrentItem(LyXLength(length).unit());
560                 dialog_->textLayoutModule->skipLE->setText(toqstr(tostr(LyXLength(length).value())));
561                 break;
562         }
563         default:
564                 skip = 0;
565                 break;
566         }
567         dialog_->textLayoutModule->skipCO->setCurrentItem(skip);
568         dialog_->setSkip(skip);
569
570         dialog_->textLayoutModule->twoColumnCB->setChecked(
571                 params.columns == 2);
572
573         if (!params.options.empty()) {
574                 dialog_->latexModule->optionsLE->setText(
575                         toqstr(params.options));
576         } else {
577                 dialog_->latexModule->optionsLE->setText("");
578         }
579
580         dialog_->floatModule->set(params.float_placement);
581
582         // paper
583         int const psize = params.papersize2;
584         dialog_->pageLayoutModule->papersizeCO->setCurrentItem(psize);
585         dialog_->setMargins(psize);
586         dialog_->setCustomPapersize(psize);
587
588         bool const landscape =
589                 params.orientation == ORIENTATION_LANDSCAPE;
590         dialog_->pageLayoutModule->landscapeRB->setChecked(landscape);
591         dialog_->pageLayoutModule->portraitRB->setChecked(!landscape);
592
593         dialog_->pageLayoutModule->facingPagesCB->setChecked(
594                 params.sides == LyXTextClass::TwoSides);
595
596
597
598         lengthToWidgets(dialog_->pageLayoutModule->paperwidthLE,
599                 dialog_->pageLayoutModule->paperwidthUnitCO, params.paperwidth, defaultUnit);
600
601         lengthToWidgets(dialog_->pageLayoutModule->paperheightLE,
602                 dialog_->pageLayoutModule->paperheightUnitCO, params.paperheight, defaultUnit);
603
604         // margins
605
606         MarginsModuleBase * m = dialog_->marginsModule;
607
608         int item = params.paperpackage;
609         if (params.use_geometry) {
610                 item = 1;
611         } else if (item > 0) {
612                 item = item + 1;
613         }
614         m->marginCO->setCurrentItem(item);
615         dialog_->setCustomMargins(item);
616
617         lengthToWidgets(m->topLE, m->topUnit,
618                 params.topmargin, defaultUnit);
619
620         lengthToWidgets(m->bottomLE, m->bottomUnit,
621                 params.bottommargin, defaultUnit);
622
623         lengthToWidgets(m->innerLE, m->innerUnit,
624                 params.leftmargin, defaultUnit);
625
626         lengthToWidgets(m->outerLE, m->outerUnit,
627                 params.rightmargin, defaultUnit);
628
629         lengthToWidgets(m->headheightLE, m->headheightUnit,
630                 params.headheight, defaultUnit);
631
632         lengthToWidgets(m->headsepLE, m->headsepUnit,
633                 params.headsep, defaultUnit);
634
635         lengthToWidgets(m->footskipLE, m->footskipUnit,
636                 params.footskip, defaultUnit);
637
638         // branches
639         branchlist_ = params.branchlist();
640         dialog_->updateBranchView();
641 }
642
643
644 void QDocument::saveDocDefault()
645 {
646         // we have to apply the params first
647         apply();
648         controller().saveAsDefault();
649 }
650
651
652 void QDocument::useClassDefaults()
653 {
654         BufferParams & params = controller().params();
655
656         params.textclass = dialog_->latexModule->classCO->currentItem();
657         params.useClassDefaults();
658         update_contents();
659 }