]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QPrefs.C
bugs 854, bug 855
[features.git] / src / frontends / qt2 / QPrefs.C
1 /**
2  * \file QPrefs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "support/lstrings.h"
18 #include "Lsstream.h"
19 #include <iomanip>
20
21 #include "ControlPrefs.h"
22 #include "QPrefsDialog.h"
23 #include "ui/QPrefAsciiModule.h"
24 #include "ui/QPrefDateModule.h"
25 #include "ui/QPrefKeyboardModule.h"
26 #include "ui/QPrefLatexModule.h"
27 #include "ui/QPrefScreenFontsModule.h"
28 #include "ui/QPrefColorsModule.h"
29 #include "ui/QPrefDisplayModule.h"
30 #include "ui/QPrefPathsModule.h"
31 #include "ui/QPrefSpellcheckerModule.h"
32 #include "ui/QPrefConvertersModule.h"
33 #include "ui/QPrefFileformatsModule.h"
34 #include "ui/QPrefLanguageModule.h"
35 #include "ui/QPrefPrinterModule.h"
36 #include "ui/QPrefUIModule.h"
37 #include "QPrefs.h"
38 #include "Qt2BC.h"
39 #include "lyxrc.h"
40 #include "frnt_lang.h"
41 #include "helper_funcs.h"
42 #include "qt_helpers.h"
43 #include "debug.h"
44
45 #include <boost/tuple/tuple.hpp>
46
47 #include <qpushbutton.h>
48 #include <qcheckbox.h>
49 #include <qlineedit.h>
50 #include <qspinbox.h>
51 #include <qcombobox.h>
52 #include <qlistbox.h>
53 #include <qlabel.h>
54 #include "qcoloritem.h"
55
56 using std::vector;
57 using std::pair;
58 using std::ostringstream;
59 using std::setfill;
60 using std::setw;
61 using std::endl;
62
63 typedef Qt2CB<ControlPrefs, Qt2DB<QPrefsDialog> > base_class;
64
65
66 QPrefs::QPrefs()
67         : base_class(qt_("LyX: Preferences"))
68 {
69 }
70
71
72 void QPrefs::build_dialog()
73 {
74         dialog_.reset(new QPrefsDialog(this));
75
76         bc().setOK(dialog_->savePB);
77         bc().setApply(dialog_->applyPB);
78         bc().setCancel(dialog_->closePB);
79         bc().setRestore(dialog_->restorePB);
80
81         QPrefLanguageModule * langmod(dialog_->languageModule);
82
83         langmod->defaultLanguageCO->clear();
84         // store the lang identifiers for later
85         vector<frnt::LanguagePair> const langs = frnt::getLanguageData(false);
86         lang_ = getSecond(langs);
87
88         vector<frnt::LanguagePair>::const_iterator lit  = langs.begin();
89         vector<frnt::LanguagePair>::const_iterator lend = langs.end();
90         for (; lit != lend; ++lit) {
91                 langmod->defaultLanguageCO->insertItem(toqstr(lit->first));
92         }
93 }
94
95
96 void QPrefs::apply()
97 {
98         LyXRC & rc(controller().rc());
99
100         QPrefLanguageModule * langmod(dialog_->languageModule);
101
102         // FIXME: remove rtl_support bool
103         rc.rtl_support = langmod->rtlCB->isChecked();
104         rc.mark_foreign_language = langmod->markForeignCB->isChecked();
105         rc.language_auto_begin = langmod->autoBeginCB->isChecked();
106         rc.language_auto_end = langmod->autoEndCB->isChecked();
107         rc.language_use_babel = langmod->useBabelCB->isChecked();
108         rc.language_global_options = langmod->globalCB->isChecked();
109         rc.language_package = fromqstr(langmod->languagePackageED->text());
110         rc.language_command_begin = fromqstr(langmod->startCommandED->text());
111         rc.language_command_end = fromqstr(langmod->endCommandED->text());
112         rc.default_language = lang_[langmod->defaultLanguageCO->currentItem()];
113
114         QPrefUIModule * uimod(dialog_->uiModule);
115
116         rc.ui_file = fromqstr(uimod->uiFileED->text());
117         rc.bind_file = fromqstr(uimod->bindFileED->text());
118         rc.cursor_follows_scrollbar = uimod->cursorFollowsCB->isChecked();
119         rc.wheel_jump = uimod->wheelMouseSB->value();
120         rc.autosave = uimod->autoSaveSB->value() * 60;
121         rc.make_backup = uimod->autoSaveCB->isChecked();
122         rc.num_lastfiles = uimod->lastfilesSB->value();
123
124
125         QPrefKeyboardModule * keymod(dialog_->keyboardModule);
126
127         // FIXME: can derive CB from the two EDs
128         rc.use_kbmap = keymod->keymapCB->isChecked();
129         rc.primary_kbmap = fromqstr(keymod->firstKeymapED->text());
130         rc.secondary_kbmap = fromqstr(keymod->secondKeymapED->text());
131
132
133         QPrefAsciiModule * ascmod(dialog_->asciiModule);
134
135         rc.ascii_linelen = ascmod->asciiLinelengthSB->value();
136         rc.ascii_roff_command = fromqstr(ascmod->asciiRoffED->text());
137
138
139         QPrefDateModule * datemod(dialog_->dateModule);
140
141         rc.date_insert_format = fromqstr(datemod->DateED->text());
142
143
144         QPrefLatexModule * latexmod(dialog_->latexModule);
145
146         rc.fontenc = fromqstr(latexmod->latexEncodingED->text());
147         rc.chktex_command = fromqstr(latexmod->latexChecktexED->text());
148         rc.auto_reset_options = latexmod->latexAutoresetCB->isChecked();
149         rc.view_dvi_paper_option = fromqstr(latexmod->latexDviPaperED->text());
150         rc.default_papersize =
151                 static_cast<BufferParams::PAPER_SIZE>(latexmod->latexPaperSizeCO->currentItem());
152
153
154         QPrefDisplayModule * displaymod(dialog_->displayModule);
155
156         rc.preview = displaymod->previewCB->isChecked();
157
158         grfx::DisplayType dtype(grfx::ColorDisplay);
159
160         switch (displaymod->displayGraphicsCO->currentItem()) {
161                 case 3: dtype = grfx::NoDisplay; break;
162                 case 2: dtype = grfx::ColorDisplay; break;
163                 case 1: dtype = grfx::GrayscaleDisplay; break;
164                 case 0: dtype = grfx::MonochromeDisplay; break;
165         }
166         rc.display_graphics = dtype;
167
168 #ifdef WITH_WARNINGS
169 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
170 #endif
171 #if 0
172         if (old_value != rc.display_graphics) {
173                 grfx::GCache & gc = grfx::GCache::get();
174                 gc.changeDisplay();
175         }
176 #endif
177
178         QPrefPathsModule * pathsmod(dialog_->pathsModule);
179
180         rc.document_path = fromqstr(pathsmod->workingDirED->text());
181         rc.template_path = fromqstr(pathsmod->templateDirED->text());
182         rc.backupdir_path = fromqstr(pathsmod->backupDirED->text());
183         rc.use_tempdir = pathsmod->tempDirCB->isChecked();
184         rc.tempdir_path = fromqstr(pathsmod->tempDirED->text());
185         // FIXME: should be a checkbox only
186         rc.lyxpipes = fromqstr(pathsmod->lyxserverDirED->text());
187
188
189         QPrefSpellcheckerModule * spellmod(dialog_->spellcheckerModule);
190
191         if (spellmod->spellCommandCO->currentItem() == 0)
192                 rc.isp_command = "ispell";
193         else
194                 rc.isp_command = "aspell";
195
196         // FIXME: remove isp_use_alt_lang
197         rc.isp_alt_lang = fromqstr(spellmod->altLanguageED->text());
198         rc.isp_use_alt_lang = !rc.isp_alt_lang.empty();
199         // FIXME: remove isp_use_esc_chars
200         rc.isp_esc_chars = fromqstr(spellmod->escapeCharactersED->text());
201         rc.isp_use_esc_chars = !rc.isp_esc_chars.empty();
202         // FIXME: remove isp_use_pers_dict
203         rc.isp_pers_dict = fromqstr(spellmod->persDictionaryED->text());
204         rc.isp_use_pers_dict = !rc.isp_pers_dict.empty();
205         rc.isp_accept_compound = spellmod->compoundWordCB->isChecked();
206         rc.isp_use_input_encoding = spellmod->inputEncodingCB->isChecked();
207
208
209         QPrefPrinterModule * printmod(dialog_->printerModule);
210
211         rc.print_adapt_output = printmod->printerAdaptCB->isChecked();
212         rc.print_command = fromqstr(printmod->printerCommandED->text());
213         rc.printer = fromqstr(printmod->printerNameED->text());
214
215         rc.print_pagerange_flag = fromqstr(printmod->printerPageRangeED->text());
216         rc.print_copies_flag = fromqstr(printmod->printerCopiesED->text());
217         rc.print_reverse_flag = fromqstr(printmod->printerReverseED->text());
218         rc.print_to_printer = fromqstr(printmod->printerToPrinterED->text());
219         rc.print_file_extension = fromqstr(printmod->printerExtensionED->text());
220         rc.print_file_extension = fromqstr(printmod->printerSpoolCommandED->text());
221         rc.print_paper_flag = fromqstr(printmod->printerPaperTypeED->text());
222         rc.print_evenpage_flag = fromqstr(printmod->printerEvenED->text());
223         rc.print_oddpage_flag = fromqstr(printmod->printerOddED->text());
224         rc.print_collcopies_flag = fromqstr(printmod->printerCollatedED->text());
225         rc.print_landscape_flag = fromqstr(printmod->printerLandscapeED->text());
226         rc.print_to_file = fromqstr(printmod->printerToFileED->text());
227         rc.print_extra_options = fromqstr(printmod->printerExtraED->text());
228         rc.print_spool_printerprefix = fromqstr(printmod->printerSpoolPrefixED->text());
229         rc.print_paper_dimension_flag = fromqstr(printmod->printerPaperSizeED->text());
230
231
232         QPrefScreenFontsModule * fontmod(dialog_->screenfontsModule);
233
234         LyXRC const oldrc(rc);
235
236         boost::tie(rc.roman_font_name, rc.roman_font_foundry)
237                 = parseFontName(fromqstr(fontmod->screenRomanCO->currentText()));
238         boost::tie(rc.sans_font_name, rc.sans_font_foundry) =
239                 parseFontName(fromqstr(fontmod->screenSansCO->currentText()));
240         boost::tie(rc.typewriter_font_name, rc.typewriter_font_foundry) =
241                 parseFontName(fromqstr(fontmod->screenTypewriterCO->currentText()));
242
243         rc.zoom = fontmod->screenZoomSB->value();
244         rc.dpi = fontmod->screenDpiSB->value();
245         rc.font_sizes[LyXFont::SIZE_TINY] = strToDbl(fromqstr(fontmod->screenTinyED->text()));
246         rc.font_sizes[LyXFont::SIZE_SCRIPT] = strToDbl(fromqstr(fontmod->screenSmallestED->text()));
247         rc.font_sizes[LyXFont::SIZE_FOOTNOTE] = strToDbl(fromqstr(fontmod->screenSmallerED->text()));
248         rc.font_sizes[LyXFont::SIZE_SMALL] = strToDbl(fromqstr(fontmod->screenSmallED->text()));
249         rc.font_sizes[LyXFont::SIZE_NORMAL] = strToDbl(fromqstr(fontmod->screenNormalED->text()));
250         rc.font_sizes[LyXFont::SIZE_LARGE] = strToDbl(fromqstr(fontmod->screenLargeED->text()));
251         rc.font_sizes[LyXFont::SIZE_LARGER] = strToDbl(fromqstr(fontmod->screenLargerED->text()));
252         rc.font_sizes[LyXFont::SIZE_LARGEST] = strToDbl(fromqstr(fontmod->screenLargestED->text()));
253         rc.font_sizes[LyXFont::SIZE_HUGE] = strToDbl(fromqstr(fontmod->screenHugeED->text()));
254         rc.font_sizes[LyXFont::SIZE_HUGER] = strToDbl(fromqstr(fontmod->screenHugerED->text()));
255
256         if (rc.font_sizes != oldrc.font_sizes
257                 || rc.roman_font_name != oldrc.roman_font_name
258                 || rc.sans_font_name != oldrc.sans_font_name
259                 || rc.typewriter_font_name != oldrc.typewriter_font_name
260                 || rc.zoom != oldrc.zoom || rc.dpi != oldrc.dpi) {
261                 controller().updateScreenFonts();
262         }
263
264         controller().setFormats(formats_);
265         controller().setConverters(converters_);
266
267         QPrefColorsModule * colmod(dialog_->colorsModule);
268
269         unsigned int i;
270
271         for (i = 0; i < colmod->lyxObjectsLB->count(); ++i) {
272                 QListBoxItem * ib(colmod->lyxObjectsLB->item(i));
273                 QColorItem * ci(static_cast<QColorItem*>(ib));
274
275                 ostringstream ostr;
276
277                 ostr << '#' << std::setbase(16) << setfill('0')
278                         << setw(2) << ci->color().red()
279                         << setw(2) << ci->color().green()
280                         << setw(2) << ci->color().blue();
281
282                 string newhex(STRCONV(ostr.str()));
283
284                 LColor::color col(dialog_->colors_[i]);
285
286                 // FIXME: dubious, but it's what xforms does
287                 if (lcolor.getX11Name(col) != newhex) {
288                         controller().setColor(col, newhex);
289                 }
290         }
291 }
292
293
294 // FIXME: move to helper_funcs.h
295 namespace {
296
297 template<class A>
298 typename std::vector<A>::size_type
299 findPos(std::vector<A> const & vec, A const & val)
300 {
301         typename std::vector<A>::const_iterator it =
302                 std::find(vec.begin(), vec.end(), val);
303         if (it == vec.end())
304                 return 0;
305         return std::distance(vec.begin(), it);
306 }
307
308 void setComboxFont(QComboBox * cb, string const & family,
309                 string const & foundry, QFont::StyleHint hint)
310 {
311         string const name = makeFontName(family, foundry);
312         for (int i = 0; i < cb->count(); ++i) {
313                 if (fromqstr(cb->text(i)) == name) {
314                         cb->setCurrentItem(i);
315                         return;
316                 }
317         }
318
319         // Try matching without foundary name
320
321         // We count in reverse in order to prefer the Xft foundry
322         for (int i = cb->count() - 1; i >= 0; --i) {
323                 pair<string, string> tmp = parseFontName(fromqstr(cb->text(i)));
324                 if (compare_no_case(tmp.first, family) == 0) {
325                         cb->setCurrentItem(i);
326                         return;
327                 }
328         }
329
330         // Try the hint
331         QFont font;
332         font.setStyleHint(hint);
333         QFontInfo fi(font);
334
335         for (int i = cb->count() - 1; i >= 0; --i) {
336                 if (cb->text(i) == fi.family()) {
337                         cb->setCurrentItem(i);
338                         return;
339                 }
340         }
341 }
342
343 }
344
345 void QPrefs::update_contents()
346 {
347         LyXRC const & rc(controller().rc());
348
349         QPrefLanguageModule * langmod(dialog_->languageModule);
350
351         // FIXME: remove rtl_support bool
352         langmod->rtlCB->setChecked(rc.rtl_support);
353         langmod->markForeignCB->setChecked(rc.mark_foreign_language);
354         langmod->autoBeginCB->setChecked(rc.language_auto_begin);
355         langmod->autoEndCB->setChecked(rc.language_auto_end);
356         langmod->useBabelCB->setChecked(rc.language_use_babel);
357         langmod->globalCB->setChecked(rc.language_global_options);
358         langmod->languagePackageED->setText(toqstr(rc.language_package));
359         langmod->startCommandED->setText(toqstr(rc.language_command_begin));
360         langmod->endCommandED->setText(toqstr(rc.language_command_end));
361
362         int const pos = int(findPos(lang_, rc.default_language));
363         langmod->defaultLanguageCO->setCurrentItem(pos);
364
365         QPrefUIModule * uimod(dialog_->uiModule);
366
367         uimod->uiFileED->setText(toqstr(rc.ui_file));
368         uimod->bindFileED->setText(toqstr(rc.bind_file));
369         uimod->cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
370         uimod->wheelMouseSB->setValue(rc.wheel_jump);
371         // convert to minutes
372         int mins(rc.autosave / 60);
373         if (rc.autosave && !mins)
374                 mins = 1;
375         uimod->autoSaveSB->setValue(mins);
376         uimod->autoSaveCB->setChecked(rc.make_backup);
377         uimod->lastfilesSB->setValue(rc.num_lastfiles);
378
379
380         QPrefKeyboardModule * keymod(dialog_->keyboardModule);
381
382         // FIXME: can derive CB from the two EDs
383         keymod->keymapCB->setChecked(rc.use_kbmap);
384         // no idea why we need these. Fscking Qt.
385         keymod->firstKeymapED->setEnabled(rc.use_kbmap);
386         keymod->firstKeymapPB->setEnabled(rc.use_kbmap);
387         keymod->firstKeymapLA->setEnabled(rc.use_kbmap);
388         keymod->secondKeymapED->setEnabled(rc.use_kbmap);
389         keymod->secondKeymapPB->setEnabled(rc.use_kbmap);
390         keymod->secondKeymapLA->setEnabled(rc.use_kbmap);
391         keymod->firstKeymapED->setText(toqstr(rc.primary_kbmap));
392         keymod->secondKeymapED->setText(toqstr(rc.secondary_kbmap));
393
394
395         QPrefAsciiModule * ascmod(dialog_->asciiModule);
396
397         ascmod->asciiLinelengthSB->setValue(rc.ascii_linelen);
398         ascmod->asciiRoffED->setText(toqstr(rc.ascii_roff_command));
399
400
401         QPrefDateModule * datemod(dialog_->dateModule);
402
403         datemod->DateED->setText(toqstr(rc.date_insert_format));
404
405
406         QPrefLatexModule * latexmod(dialog_->latexModule);
407
408         latexmod->latexEncodingED->setText(toqstr(rc.fontenc));
409         latexmod->latexChecktexED->setText(toqstr(rc.chktex_command));
410         latexmod->latexAutoresetCB->setChecked(rc.auto_reset_options);
411         latexmod->latexDviPaperED->setText(toqstr(rc.view_dvi_paper_option));
412         latexmod->latexPaperSizeCO->setCurrentItem(rc.default_papersize);
413
414
415         QPrefDisplayModule * displaymod(dialog_->displayModule);
416
417         displaymod->previewCB->setChecked(rc.preview);
418
419         int item = 2;
420
421         switch (rc.display_graphics) {
422                 case grfx::NoDisplay:           item = 3; break;
423                 case grfx::ColorDisplay:        item = 2; break;
424                 case grfx::GrayscaleDisplay:    item = 1; break;
425                 case grfx::MonochromeDisplay:   item = 0; break;
426                 default: break;
427         }
428         displaymod->displayGraphicsCO->setCurrentItem(item);
429
430
431         QPrefPathsModule * pathsmod(dialog_->pathsModule);
432
433         pathsmod->workingDirED->setText(toqstr(rc.document_path));
434         pathsmod->templateDirED->setText(toqstr(rc.template_path));
435         pathsmod->backupDirED->setText(toqstr(rc.backupdir_path));
436         pathsmod->tempDirCB->setChecked(rc.use_tempdir);
437         pathsmod->tempDirED->setText(toqstr(rc.tempdir_path));
438         // FIXME: should be a checkbox only
439         pathsmod->lyxserverDirED->setText(toqstr(rc.lyxpipes));
440
441
442         QPrefSpellcheckerModule * spellmod(dialog_->spellcheckerModule);
443
444         item = (rc.isp_command == "ispell") ? 0 : 1;
445         spellmod->spellCommandCO->setCurrentItem(item);
446         // FIXME: remove isp_use_alt_lang
447         spellmod->altLanguageED->setText(toqstr(rc.isp_alt_lang));
448         // FIXME: remove isp_use_esc_chars
449         spellmod->escapeCharactersED->setText(toqstr(rc.isp_esc_chars));
450         // FIXME: remove isp_use_pers_dict
451         spellmod->persDictionaryED->setText(toqstr(rc.isp_pers_dict));
452         spellmod->compoundWordCB->setChecked(rc.isp_accept_compound);
453         spellmod->inputEncodingCB->setChecked(rc.isp_use_input_encoding);
454
455
456         QPrefPrinterModule * printmod(dialog_->printerModule);
457
458         printmod->printerAdaptCB->setChecked(rc.print_adapt_output);
459         printmod->printerCommandED->setText(toqstr(rc.print_command));
460         printmod->printerNameED->setText(toqstr(rc.printer));
461
462         printmod->printerPageRangeED->setText(toqstr(rc.print_pagerange_flag));
463         printmod->printerCopiesED->setText(toqstr(rc.print_copies_flag));
464         printmod->printerReverseED->setText(toqstr(rc.print_reverse_flag));
465         printmod->printerToPrinterED->setText(toqstr(rc.print_to_printer));
466         printmod->printerExtensionED->setText(toqstr(rc.print_file_extension));
467         printmod->printerSpoolCommandED->setText(toqstr(rc.print_file_extension));
468         printmod->printerPaperTypeED->setText(toqstr(rc.print_paper_flag));
469         printmod->printerEvenED->setText(toqstr(rc.print_evenpage_flag));
470         printmod->printerOddED->setText(toqstr(rc.print_oddpage_flag));
471         printmod->printerCollatedED->setText(toqstr(rc.print_collcopies_flag));
472         printmod->printerLandscapeED->setText(toqstr(rc.print_landscape_flag));
473         printmod->printerToFileED->setText(toqstr(rc.print_to_file));
474         printmod->printerExtraED->setText(toqstr(rc.print_extra_options));
475         printmod->printerSpoolPrefixED->setText(toqstr(rc.print_spool_printerprefix));
476         printmod->printerPaperSizeED->setText(toqstr(rc.print_paper_dimension_flag));
477
478
479         QPrefScreenFontsModule * fontmod(dialog_->screenfontsModule);
480
481         setComboxFont(fontmod->screenRomanCO, rc.roman_font_name,
482                         rc.roman_font_foundry, QFont::Serif);
483         setComboxFont(fontmod->screenSansCO, rc.sans_font_name,
484                         rc.sans_font_foundry, QFont::SansSerif);
485         setComboxFont(fontmod->screenTypewriterCO, rc.typewriter_font_name,
486                         rc.typewriter_font_foundry, QFont::TypeWriter);
487
488         dialog_->select_roman(fontmod->screenRomanCO->currentText());
489         dialog_->select_sans(fontmod->screenSansCO->currentText());
490         dialog_->select_typewriter(fontmod->screenTypewriterCO->currentText());
491
492         fontmod->screenZoomSB->setValue(rc.zoom);
493         fontmod->screenDpiSB->setValue(int(rc.dpi));
494         fontmod->screenTinyED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_TINY])));
495         fontmod->screenSmallestED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_SCRIPT])));
496         fontmod->screenSmallerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_FOOTNOTE])));
497         fontmod->screenSmallED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_SMALL])));
498         fontmod->screenNormalED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_NORMAL])));
499         fontmod->screenLargeED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGE])));
500         fontmod->screenLargerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGER])));
501         fontmod->screenLargestED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGEST])));
502         fontmod->screenHugeED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_HUGE])));
503         fontmod->screenHugerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_HUGER])));
504
505         formats_ = formats;
506
507         dialog_->updateFormats();
508
509         converters_ = converters;
510
511         dialog_->updateConverters();
512 }