3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
17 #include "support/lstrings.h"
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 "ui/QPrefIdentityModule.h"
41 #include "frnt_lang.h"
42 #include "helper_funcs.h"
43 #include "qt_helpers.h"
46 #include <boost/tuple/tuple.hpp>
48 #include <qpushbutton.h>
49 #include <qcheckbox.h>
50 #include <qlineedit.h>
52 #include <qcombobox.h>
55 #include "qcoloritem.h"
59 using std::ostringstream;
64 typedef Qt2CB<ControlPrefs, Qt2DB<QPrefsDialog> > base_class;
68 : base_class(qt_("LyX: Preferences"))
73 void QPrefs::build_dialog()
75 dialog_.reset(new QPrefsDialog(this));
77 bc().setOK(dialog_->savePB);
78 bc().setApply(dialog_->applyPB);
79 bc().setCancel(dialog_->closePB);
80 bc().setRestore(dialog_->restorePB);
82 QPrefLanguageModule * langmod(dialog_->languageModule);
84 langmod->defaultLanguageCO->clear();
85 // store the lang identifiers for later
86 vector<frnt::LanguagePair> const langs = frnt::getLanguageData(false);
87 lang_ = getSecond(langs);
89 vector<frnt::LanguagePair>::const_iterator lit = langs.begin();
90 vector<frnt::LanguagePair>::const_iterator lend = langs.end();
91 for (; lit != lend; ++lit) {
92 langmod->defaultLanguageCO->insertItem(toqstr(lit->first));
99 LyXRC & rc(controller().rc());
101 QPrefLanguageModule * langmod(dialog_->languageModule);
103 // FIXME: remove rtl_support bool
104 rc.rtl_support = langmod->rtlCB->isChecked();
105 rc.mark_foreign_language = langmod->markForeignCB->isChecked();
106 rc.language_auto_begin = langmod->autoBeginCB->isChecked();
107 rc.language_auto_end = langmod->autoEndCB->isChecked();
108 rc.language_use_babel = langmod->useBabelCB->isChecked();
109 rc.language_global_options = langmod->globalCB->isChecked();
110 rc.language_package = fromqstr(langmod->languagePackageED->text());
111 rc.language_command_begin = fromqstr(langmod->startCommandED->text());
112 rc.language_command_end = fromqstr(langmod->endCommandED->text());
113 rc.default_language = lang_[langmod->defaultLanguageCO->currentItem()];
115 QPrefUIModule * uimod(dialog_->uiModule);
117 rc.ui_file = fromqstr(uimod->uiFileED->text());
118 rc.bind_file = fromqstr(uimod->bindFileED->text());
119 rc.cursor_follows_scrollbar = uimod->cursorFollowsCB->isChecked();
120 rc.wheel_jump = uimod->wheelMouseSB->value();
121 rc.autosave = uimod->autoSaveSB->value() * 60;
122 rc.make_backup = uimod->autoSaveCB->isChecked();
123 rc.num_lastfiles = uimod->lastfilesSB->value();
126 QPrefKeyboardModule * keymod(dialog_->keyboardModule);
128 // FIXME: can derive CB from the two EDs
129 rc.use_kbmap = keymod->keymapCB->isChecked();
130 rc.primary_kbmap = fromqstr(keymod->firstKeymapED->text());
131 rc.secondary_kbmap = fromqstr(keymod->secondKeymapED->text());
134 QPrefAsciiModule * ascmod(dialog_->asciiModule);
136 rc.ascii_linelen = ascmod->asciiLinelengthSB->value();
137 rc.ascii_roff_command = fromqstr(ascmod->asciiRoffED->text());
140 QPrefDateModule * datemod(dialog_->dateModule);
142 rc.date_insert_format = fromqstr(datemod->DateED->text());
145 QPrefLatexModule * latexmod(dialog_->latexModule);
147 rc.fontenc = fromqstr(latexmod->latexEncodingED->text());
148 rc.chktex_command = fromqstr(latexmod->latexChecktexED->text());
149 rc.auto_reset_options = latexmod->latexAutoresetCB->isChecked();
150 rc.view_dvi_paper_option = fromqstr(latexmod->latexDviPaperED->text());
151 rc.default_papersize =
152 static_cast<BufferParams::PAPER_SIZE>(latexmod->latexPaperSizeCO->currentItem());
155 QPrefDisplayModule * displaymod(dialog_->displayModule);
157 rc.preview = displaymod->previewCB->isChecked();
159 grfx::DisplayType dtype(grfx::ColorDisplay);
161 switch (displaymod->displayGraphicsCO->currentItem()) {
162 case 3: dtype = grfx::NoDisplay; break;
163 case 2: dtype = grfx::ColorDisplay; break;
164 case 1: dtype = grfx::GrayscaleDisplay; break;
165 case 0: dtype = grfx::MonochromeDisplay; break;
167 rc.display_graphics = dtype;
170 #warning FIXME!! The graphics cache no longer has a changeDisplay method.
173 if (old_value != rc.display_graphics) {
174 grfx::GCache & gc = grfx::GCache::get();
179 QPrefPathsModule * pathsmod(dialog_->pathsModule);
181 rc.document_path = fromqstr(pathsmod->workingDirED->text());
182 rc.template_path = fromqstr(pathsmod->templateDirED->text());
183 rc.backupdir_path = fromqstr(pathsmod->backupDirED->text());
184 rc.use_tempdir = pathsmod->tempDirCB->isChecked();
185 rc.tempdir_path = fromqstr(pathsmod->tempDirED->text());
186 // FIXME: should be a checkbox only
187 rc.lyxpipes = fromqstr(pathsmod->lyxserverDirED->text());
190 QPrefSpellcheckerModule * spellmod(dialog_->spellcheckerModule);
192 if (spellmod->spellCommandCO->currentItem() == 0)
193 rc.isp_command = "ispell";
195 rc.isp_command = "aspell";
197 // FIXME: remove isp_use_alt_lang
198 rc.isp_alt_lang = fromqstr(spellmod->altLanguageED->text());
199 rc.isp_use_alt_lang = !rc.isp_alt_lang.empty();
200 // FIXME: remove isp_use_esc_chars
201 rc.isp_esc_chars = fromqstr(spellmod->escapeCharactersED->text());
202 rc.isp_use_esc_chars = !rc.isp_esc_chars.empty();
203 // FIXME: remove isp_use_pers_dict
204 rc.isp_pers_dict = fromqstr(spellmod->persDictionaryED->text());
205 rc.isp_use_pers_dict = !rc.isp_pers_dict.empty();
206 rc.isp_accept_compound = spellmod->compoundWordCB->isChecked();
207 rc.isp_use_input_encoding = spellmod->inputEncodingCB->isChecked();
210 QPrefPrinterModule * printmod(dialog_->printerModule);
212 rc.print_adapt_output = printmod->printerAdaptCB->isChecked();
213 rc.print_command = fromqstr(printmod->printerCommandED->text());
214 rc.printer = fromqstr(printmod->printerNameED->text());
216 rc.print_pagerange_flag = fromqstr(printmod->printerPageRangeED->text());
217 rc.print_copies_flag = fromqstr(printmod->printerCopiesED->text());
218 rc.print_reverse_flag = fromqstr(printmod->printerReverseED->text());
219 rc.print_to_printer = fromqstr(printmod->printerToPrinterED->text());
220 rc.print_file_extension = fromqstr(printmod->printerExtensionED->text());
221 rc.print_file_extension = fromqstr(printmod->printerSpoolCommandED->text());
222 rc.print_paper_flag = fromqstr(printmod->printerPaperTypeED->text());
223 rc.print_evenpage_flag = fromqstr(printmod->printerEvenED->text());
224 rc.print_oddpage_flag = fromqstr(printmod->printerOddED->text());
225 rc.print_collcopies_flag = fromqstr(printmod->printerCollatedED->text());
226 rc.print_landscape_flag = fromqstr(printmod->printerLandscapeED->text());
227 rc.print_to_file = fromqstr(printmod->printerToFileED->text());
228 rc.print_extra_options = fromqstr(printmod->printerExtraED->text());
229 rc.print_spool_printerprefix = fromqstr(printmod->printerSpoolPrefixED->text());
230 rc.print_paper_dimension_flag = fromqstr(printmod->printerPaperSizeED->text());
233 QPrefIdentityModule * idmod(dialog_->identityModule);
234 rc.user_name = fromqstr(idmod->nameED->text());
235 rc.user_email = fromqstr(idmod->emailED->text());
237 QPrefScreenFontsModule * fontmod(dialog_->screenfontsModule);
239 LyXRC const oldrc(rc);
241 boost::tie(rc.roman_font_name, rc.roman_font_foundry)
242 = parseFontName(fromqstr(fontmod->screenRomanCO->currentText()));
243 boost::tie(rc.sans_font_name, rc.sans_font_foundry) =
244 parseFontName(fromqstr(fontmod->screenSansCO->currentText()));
245 boost::tie(rc.typewriter_font_name, rc.typewriter_font_foundry) =
246 parseFontName(fromqstr(fontmod->screenTypewriterCO->currentText()));
248 rc.zoom = fontmod->screenZoomSB->value();
249 rc.dpi = fontmod->screenDpiSB->value();
250 rc.font_sizes[LyXFont::SIZE_TINY] = strToDbl(fromqstr(fontmod->screenTinyED->text()));
251 rc.font_sizes[LyXFont::SIZE_SCRIPT] = strToDbl(fromqstr(fontmod->screenSmallestED->text()));
252 rc.font_sizes[LyXFont::SIZE_FOOTNOTE] = strToDbl(fromqstr(fontmod->screenSmallerED->text()));
253 rc.font_sizes[LyXFont::SIZE_SMALL] = strToDbl(fromqstr(fontmod->screenSmallED->text()));
254 rc.font_sizes[LyXFont::SIZE_NORMAL] = strToDbl(fromqstr(fontmod->screenNormalED->text()));
255 rc.font_sizes[LyXFont::SIZE_LARGE] = strToDbl(fromqstr(fontmod->screenLargeED->text()));
256 rc.font_sizes[LyXFont::SIZE_LARGER] = strToDbl(fromqstr(fontmod->screenLargerED->text()));
257 rc.font_sizes[LyXFont::SIZE_LARGEST] = strToDbl(fromqstr(fontmod->screenLargestED->text()));
258 rc.font_sizes[LyXFont::SIZE_HUGE] = strToDbl(fromqstr(fontmod->screenHugeED->text()));
259 rc.font_sizes[LyXFont::SIZE_HUGER] = strToDbl(fromqstr(fontmod->screenHugerED->text()));
261 if (rc.font_sizes != oldrc.font_sizes
262 || rc.roman_font_name != oldrc.roman_font_name
263 || rc.sans_font_name != oldrc.sans_font_name
264 || rc.typewriter_font_name != oldrc.typewriter_font_name
265 || rc.zoom != oldrc.zoom || rc.dpi != oldrc.dpi) {
266 controller().updateScreenFonts();
269 controller().setFormats(formats_);
270 controller().setConverters(converters_);
272 QPrefColorsModule * colmod(dialog_->colorsModule);
276 for (i = 0; i < colmod->lyxObjectsLB->count(); ++i) {
277 QListBoxItem * ib(colmod->lyxObjectsLB->item(i));
278 QColorItem * ci(static_cast<QColorItem*>(ib));
282 ostr << '#' << std::setbase(16) << setfill('0')
283 << setw(2) << ci->color().red()
284 << setw(2) << ci->color().green()
285 << setw(2) << ci->color().blue();
287 string newhex(STRCONV(ostr.str()));
289 LColor::color col(dialog_->colors_[i]);
291 // FIXME: dubious, but it's what xforms does
292 if (lcolor.getX11Name(col) != newhex) {
293 controller().setColor(col, newhex);
299 // FIXME: move to helper_funcs.h
303 typename std::vector<A>::size_type
304 findPos(std::vector<A> const & vec, A const & val)
306 typename std::vector<A>::const_iterator it =
307 std::find(vec.begin(), vec.end(), val);
310 return std::distance(vec.begin(), it);
313 void setComboxFont(QComboBox * cb, string const & family,
314 string const & foundry, QFont::StyleHint hint)
316 string const name = makeFontName(family, foundry);
317 for (int i = 0; i < cb->count(); ++i) {
318 if (fromqstr(cb->text(i)) == name) {
319 cb->setCurrentItem(i);
324 // Try matching without foundary name
326 // We count in reverse in order to prefer the Xft foundry
327 for (int i = cb->count() - 1; i >= 0; --i) {
328 pair<string, string> tmp = parseFontName(fromqstr(cb->text(i)));
329 if (compare_no_case(tmp.first, family) == 0) {
330 cb->setCurrentItem(i);
337 font.setStyleHint(hint);
340 for (int i = cb->count() - 1; i >= 0; --i) {
341 if (cb->text(i) == fi.family()) {
342 cb->setCurrentItem(i);
350 void QPrefs::update_contents()
352 LyXRC const & rc(controller().rc());
354 QPrefLanguageModule * langmod(dialog_->languageModule);
356 // FIXME: remove rtl_support bool
357 langmod->rtlCB->setChecked(rc.rtl_support);
358 langmod->markForeignCB->setChecked(rc.mark_foreign_language);
359 langmod->autoBeginCB->setChecked(rc.language_auto_begin);
360 langmod->autoEndCB->setChecked(rc.language_auto_end);
361 langmod->useBabelCB->setChecked(rc.language_use_babel);
362 langmod->globalCB->setChecked(rc.language_global_options);
363 langmod->languagePackageED->setText(toqstr(rc.language_package));
364 langmod->startCommandED->setText(toqstr(rc.language_command_begin));
365 langmod->endCommandED->setText(toqstr(rc.language_command_end));
367 int const pos = int(findPos(lang_, rc.default_language));
368 langmod->defaultLanguageCO->setCurrentItem(pos);
370 QPrefUIModule * uimod(dialog_->uiModule);
372 uimod->uiFileED->setText(toqstr(rc.ui_file));
373 uimod->bindFileED->setText(toqstr(rc.bind_file));
374 uimod->cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
375 uimod->wheelMouseSB->setValue(rc.wheel_jump);
376 // convert to minutes
377 int mins(rc.autosave / 60);
378 if (rc.autosave && !mins)
380 uimod->autoSaveSB->setValue(mins);
381 uimod->autoSaveCB->setChecked(rc.make_backup);
382 uimod->lastfilesSB->setValue(rc.num_lastfiles);
385 QPrefIdentityModule * idmod(dialog_->identityModule);
386 idmod->nameED->setText(toqstr(rc.user_name));
387 idmod->emailED->setText(toqstr(rc.user_email));
390 QPrefKeyboardModule * keymod(dialog_->keyboardModule);
392 // FIXME: can derive CB from the two EDs
393 keymod->keymapCB->setChecked(rc.use_kbmap);
394 // no idea why we need these. Fscking Qt.
395 keymod->firstKeymapED->setEnabled(rc.use_kbmap);
396 keymod->firstKeymapPB->setEnabled(rc.use_kbmap);
397 keymod->firstKeymapLA->setEnabled(rc.use_kbmap);
398 keymod->secondKeymapED->setEnabled(rc.use_kbmap);
399 keymod->secondKeymapPB->setEnabled(rc.use_kbmap);
400 keymod->secondKeymapLA->setEnabled(rc.use_kbmap);
401 keymod->firstKeymapED->setText(toqstr(rc.primary_kbmap));
402 keymod->secondKeymapED->setText(toqstr(rc.secondary_kbmap));
405 QPrefAsciiModule * ascmod(dialog_->asciiModule);
407 ascmod->asciiLinelengthSB->setValue(rc.ascii_linelen);
408 ascmod->asciiRoffED->setText(toqstr(rc.ascii_roff_command));
411 QPrefDateModule * datemod(dialog_->dateModule);
413 datemod->DateED->setText(toqstr(rc.date_insert_format));
416 QPrefLatexModule * latexmod(dialog_->latexModule);
418 latexmod->latexEncodingED->setText(toqstr(rc.fontenc));
419 latexmod->latexChecktexED->setText(toqstr(rc.chktex_command));
420 latexmod->latexAutoresetCB->setChecked(rc.auto_reset_options);
421 latexmod->latexDviPaperED->setText(toqstr(rc.view_dvi_paper_option));
422 latexmod->latexPaperSizeCO->setCurrentItem(rc.default_papersize);
425 QPrefDisplayModule * displaymod(dialog_->displayModule);
427 displaymod->previewCB->setChecked(rc.preview);
431 switch (rc.display_graphics) {
432 case grfx::NoDisplay: item = 3; break;
433 case grfx::ColorDisplay: item = 2; break;
434 case grfx::GrayscaleDisplay: item = 1; break;
435 case grfx::MonochromeDisplay: item = 0; break;
438 displaymod->displayGraphicsCO->setCurrentItem(item);
441 QPrefPathsModule * pathsmod(dialog_->pathsModule);
443 pathsmod->workingDirED->setText(toqstr(rc.document_path));
444 pathsmod->templateDirED->setText(toqstr(rc.template_path));
445 pathsmod->backupDirED->setText(toqstr(rc.backupdir_path));
446 pathsmod->tempDirCB->setChecked(rc.use_tempdir);
447 pathsmod->tempDirED->setText(toqstr(rc.tempdir_path));
448 // FIXME: should be a checkbox only
449 pathsmod->lyxserverDirED->setText(toqstr(rc.lyxpipes));
452 QPrefSpellcheckerModule * spellmod(dialog_->spellcheckerModule);
454 item = (rc.isp_command == "ispell") ? 0 : 1;
455 spellmod->spellCommandCO->setCurrentItem(item);
456 // FIXME: remove isp_use_alt_lang
457 spellmod->altLanguageED->setText(toqstr(rc.isp_alt_lang));
458 // FIXME: remove isp_use_esc_chars
459 spellmod->escapeCharactersED->setText(toqstr(rc.isp_esc_chars));
460 // FIXME: remove isp_use_pers_dict
461 spellmod->persDictionaryED->setText(toqstr(rc.isp_pers_dict));
462 spellmod->compoundWordCB->setChecked(rc.isp_accept_compound);
463 spellmod->inputEncodingCB->setChecked(rc.isp_use_input_encoding);
466 QPrefPrinterModule * printmod(dialog_->printerModule);
468 printmod->printerAdaptCB->setChecked(rc.print_adapt_output);
469 printmod->printerCommandED->setText(toqstr(rc.print_command));
470 printmod->printerNameED->setText(toqstr(rc.printer));
472 printmod->printerPageRangeED->setText(toqstr(rc.print_pagerange_flag));
473 printmod->printerCopiesED->setText(toqstr(rc.print_copies_flag));
474 printmod->printerReverseED->setText(toqstr(rc.print_reverse_flag));
475 printmod->printerToPrinterED->setText(toqstr(rc.print_to_printer));
476 printmod->printerExtensionED->setText(toqstr(rc.print_file_extension));
477 printmod->printerSpoolCommandED->setText(toqstr(rc.print_file_extension));
478 printmod->printerPaperTypeED->setText(toqstr(rc.print_paper_flag));
479 printmod->printerEvenED->setText(toqstr(rc.print_evenpage_flag));
480 printmod->printerOddED->setText(toqstr(rc.print_oddpage_flag));
481 printmod->printerCollatedED->setText(toqstr(rc.print_collcopies_flag));
482 printmod->printerLandscapeED->setText(toqstr(rc.print_landscape_flag));
483 printmod->printerToFileED->setText(toqstr(rc.print_to_file));
484 printmod->printerExtraED->setText(toqstr(rc.print_extra_options));
485 printmod->printerSpoolPrefixED->setText(toqstr(rc.print_spool_printerprefix));
486 printmod->printerPaperSizeED->setText(toqstr(rc.print_paper_dimension_flag));
489 QPrefScreenFontsModule * fontmod(dialog_->screenfontsModule);
491 setComboxFont(fontmod->screenRomanCO, rc.roman_font_name,
492 rc.roman_font_foundry, QFont::Serif);
493 setComboxFont(fontmod->screenSansCO, rc.sans_font_name,
494 rc.sans_font_foundry, QFont::SansSerif);
495 setComboxFont(fontmod->screenTypewriterCO, rc.typewriter_font_name,
496 rc.typewriter_font_foundry, QFont::TypeWriter);
498 dialog_->select_roman(fontmod->screenRomanCO->currentText());
499 dialog_->select_sans(fontmod->screenSansCO->currentText());
500 dialog_->select_typewriter(fontmod->screenTypewriterCO->currentText());
502 fontmod->screenZoomSB->setValue(rc.zoom);
503 fontmod->screenDpiSB->setValue(int(rc.dpi));
504 fontmod->screenTinyED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_TINY])));
505 fontmod->screenSmallestED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_SCRIPT])));
506 fontmod->screenSmallerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_FOOTNOTE])));
507 fontmod->screenSmallED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_SMALL])));
508 fontmod->screenNormalED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_NORMAL])));
509 fontmod->screenLargeED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGE])));
510 fontmod->screenLargerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGER])));
511 fontmod->screenLargestED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_LARGEST])));
512 fontmod->screenHugeED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_HUGE])));
513 fontmod->screenHugerED->setText(toqstr(tostr(rc.font_sizes[LyXFont::SIZE_HUGER])));
517 dialog_->updateFormats();
519 converters_ = converters;
521 dialog_->updateConverters();