]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPrefs.C
better selection and scrolling behaviour
[lyx.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(_("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(lit->first.c_str());
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 = langmod->languagePackageED->text().latin1();
110         rc.language_command_begin = langmod->startCommandED->text().latin1();
111         rc.language_command_end = langmod->endCommandED->text().latin1();
112         rc.default_language = lang_[langmod->defaultLanguageCO->currentItem()];
113
114         QPrefUIModule * uimod(dialog_->uiModule);
115
116         rc.ui_file = uimod->uiFileED->text().latin1();
117         rc.bind_file = uimod->bindFileED->text().latin1();
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 = keymod->firstKeymapED->text().latin1();
130         rc.secondary_kbmap = keymod->secondKeymapED->text().latin1();
131
132
133         QPrefAsciiModule * ascmod(dialog_->asciiModule);
134
135         rc.ascii_linelen = ascmod->asciiLinelengthSB->value();
136         rc.ascii_roff_command = ascmod->asciiRoffED->text().latin1();
137
138
139         QPrefDateModule * datemod(dialog_->dateModule);
140
141         rc.date_insert_format = datemod->DateED->text().latin1();
142
143
144         QPrefLatexModule * latexmod(dialog_->latexModule);
145
146         rc.fontenc = latexmod->latexEncodingED->text().latin1();
147         rc.chktex_command = latexmod->latexChecktexED->text().latin1();
148         rc.auto_reset_options = latexmod->latexAutoresetCB->isChecked();
149         rc.view_dvi_paper_option = latexmod->latexDviPaperED->text().latin1();
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 = pathsmod->workingDirED->text().latin1();
181         rc.template_path = pathsmod->templateDirED->text().latin1();
182         rc.backupdir_path = pathsmod->backupDirED->text().latin1();
183         rc.use_tempdir = pathsmod->tempDirCB->isChecked();
184         rc.tempdir_path = pathsmod->tempDirED->text().latin1();
185         // FIXME: should be a checkbox only
186         rc.lyxpipes = pathsmod->lyxserverDirED->text().latin1();
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 = spellmod->altLanguageED->text().latin1();
198         rc.isp_use_alt_lang = !rc.isp_alt_lang.empty();
199         // FIXME: remove isp_use_esc_chars
200         rc.isp_esc_chars = spellmod->escapeCharactersED->text().latin1();
201         rc.isp_use_esc_chars = !rc.isp_esc_chars.empty();
202         // FIXME: remove isp_use_pers_dict
203         rc.isp_pers_dict = spellmod->persDictionaryED->text().latin1();
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 = printmod->printerCommandED->text().latin1();
213         rc.printer = printmod->printerNameED->text().latin1();
214
215         rc.print_pagerange_flag = printmod->printerPageRangeED->text().latin1();
216         rc.print_copies_flag = printmod->printerCopiesED->text().latin1();
217         rc.print_reverse_flag = printmod->printerReverseED->text().latin1();
218         rc.print_to_printer = printmod->printerToPrinterED->text().latin1();
219         rc.print_file_extension = printmod->printerExtensionED->text().latin1();
220         rc.print_file_extension = printmod->printerSpoolCommandED->text().latin1();
221         rc.print_paper_flag = printmod->printerPaperTypeED->text().latin1();
222         rc.print_evenpage_flag = printmod->printerEvenED->text().latin1();
223         rc.print_oddpage_flag = printmod->printerOddED->text().latin1();
224         rc.print_collcopies_flag = printmod->printerCollatedED->text().latin1();
225         rc.print_landscape_flag = printmod->printerLandscapeED->text().latin1();
226         rc.print_to_file = printmod->printerToFileED->text().latin1();
227         rc.print_extra_options = printmod->printerExtraED->text().latin1();
228         rc.print_spool_printerprefix = printmod->printerSpoolPrefixED->text().latin1();
229         rc.print_paper_dimension_flag = printmod->printerPaperSizeED->text().latin1();
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(fontmod->screenRomanCO->currentText().latin1());
238         boost::tie(rc.sans_font_name, rc.sans_font_foundry) =
239                 parseFontName(fontmod->screenSansCO->currentText().latin1());
240         boost::tie(rc.typewriter_font_name, rc.typewriter_font_foundry) =
241                 parseFontName(fontmod->screenTypewriterCO->currentText().latin1());
242
243         rc.zoom = fontmod->screenZoomSB->value();
244         rc.dpi = fontmod->screenDpiSB->value();
245         rc.font_sizes[LyXFont::SIZE_TINY] = strToDbl(fontmod->screenTinyED->text().latin1());
246         rc.font_sizes[LyXFont::SIZE_SCRIPT] = strToDbl(fontmod->screenSmallestED->text().latin1());
247         rc.font_sizes[LyXFont::SIZE_FOOTNOTE] = strToDbl(fontmod->screenSmallerED->text().latin1());
248         rc.font_sizes[LyXFont::SIZE_SMALL] = strToDbl(fontmod->screenSmallED->text().latin1());
249         rc.font_sizes[LyXFont::SIZE_NORMAL] = strToDbl(fontmod->screenNormalED->text().latin1());
250         rc.font_sizes[LyXFont::SIZE_LARGE] = strToDbl(fontmod->screenLargeED->text().latin1());
251         rc.font_sizes[LyXFont::SIZE_LARGER] = strToDbl(fontmod->screenLargerED->text().latin1());
252         rc.font_sizes[LyXFont::SIZE_LARGEST] = strToDbl(fontmod->screenLargestED->text().latin1());
253         rc.font_sizes[LyXFont::SIZE_HUGE] = strToDbl(fontmod->screenHugeED->text().latin1());
254         rc.font_sizes[LyXFont::SIZE_HUGER] = strToDbl(fontmod->screenHugerED->text().latin1());
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().setConverters(converters_);
265         controller().setFormats(formats_);
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,
309                    string const & family, string const & foundry)
310 {
311
312         string const name = makeFontName(family, foundry);
313         for (int i = 0; i < cb->count(); ++i) {
314                 if (compare_no_case(cb->text(i).latin1(), name) == 0) {
315                         cb->setCurrentItem(i);
316                         return;
317                 }
318         }
319
320         // Try matching without foundary name
321         for (int i = cb->count() - 1; i >= 0; --i) {
322                 // We count in reverse in order to prefer the Xft foundry
323                 pair<string, string> tmp = parseFontName(cb->text(i).latin1());
324                 if (compare_no_case(tmp.first, family) == 0) {
325                         cb->setCurrentItem(i);
326                         return;
327                 }
328         }
329 }
330
331 }
332
333 void QPrefs::update_contents()
334 {
335         LyXRC const & rc(controller().rc());
336
337         QPrefLanguageModule * langmod(dialog_->languageModule);
338
339         // FIXME: remove rtl_support bool
340         langmod->rtlCB->setChecked(rc.rtl_support);
341         langmod->markForeignCB->setChecked(rc.mark_foreign_language);
342         langmod->autoBeginCB->setChecked(rc.language_auto_begin);
343         langmod->autoEndCB->setChecked(rc.language_auto_end);
344         langmod->useBabelCB->setChecked(rc.language_use_babel);
345         langmod->globalCB->setChecked(rc.language_global_options);
346         langmod->languagePackageED->setText(rc.language_package.c_str());
347         langmod->startCommandED->setText(rc.language_command_begin.c_str());
348         langmod->endCommandED->setText(rc.language_command_end.c_str());
349
350         int const pos = int(findPos(lang_, rc.default_language));
351         langmod->defaultLanguageCO->setCurrentItem(pos);
352
353         QPrefUIModule * uimod(dialog_->uiModule);
354
355         uimod->uiFileED->setText(rc.ui_file.c_str());
356         uimod->bindFileED->setText(rc.bind_file.c_str());
357         uimod->cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
358         uimod->wheelMouseSB->setValue(rc.wheel_jump);
359         // convert to minutes
360         int mins(rc.autosave / 60);
361         if (rc.autosave && !mins)
362                 mins = 1;
363         uimod->autoSaveSB->setValue(mins);
364         uimod->autoSaveCB->setChecked(rc.make_backup);
365         uimod->lastfilesSB->setValue(rc.num_lastfiles);
366
367
368         QPrefKeyboardModule * keymod(dialog_->keyboardModule);
369
370         // FIXME: can derive CB from the two EDs
371         keymod->keymapCB->setChecked(rc.use_kbmap);
372         // no idea why we need these. Fscking Qt.
373         keymod->firstKeymapED->setEnabled(rc.use_kbmap);
374         keymod->firstKeymapPB->setEnabled(rc.use_kbmap);
375         keymod->firstKeymapLA->setEnabled(rc.use_kbmap);
376         keymod->secondKeymapED->setEnabled(rc.use_kbmap);
377         keymod->secondKeymapPB->setEnabled(rc.use_kbmap);
378         keymod->secondKeymapLA->setEnabled(rc.use_kbmap);
379         keymod->firstKeymapED->setText(rc.primary_kbmap.c_str());
380         keymod->secondKeymapED->setText(rc.secondary_kbmap.c_str());
381
382
383         QPrefAsciiModule * ascmod(dialog_->asciiModule);
384
385         ascmod->asciiLinelengthSB->setValue(rc.ascii_linelen);
386         ascmod->asciiRoffED->setText(rc.ascii_roff_command.c_str());
387
388
389         QPrefDateModule * datemod(dialog_->dateModule);
390
391         datemod->DateED->setText(rc.date_insert_format.c_str());
392
393
394         QPrefLatexModule * latexmod(dialog_->latexModule);
395
396         latexmod->latexEncodingED->setText(rc.fontenc.c_str());
397         latexmod->latexChecktexED->setText(rc.chktex_command.c_str());
398         latexmod->latexAutoresetCB->setChecked(rc.auto_reset_options);
399         latexmod->latexDviPaperED->setText(rc.view_dvi_paper_option.c_str());
400         latexmod->latexPaperSizeCO->setCurrentItem(rc.default_papersize);
401
402
403         QPrefDisplayModule * displaymod(dialog_->displayModule);
404
405         displaymod->previewCB->setChecked(rc.preview);
406
407         int item = 2;
408
409         switch (rc.display_graphics) {
410                 case grfx::NoDisplay:           item = 3; break;
411                 case grfx::ColorDisplay:        item = 2; break;
412                 case grfx::GrayscaleDisplay:    item = 1; break;
413                 case grfx::MonochromeDisplay:   item = 0; break;
414         }
415         displaymod->displayGraphicsCO->setCurrentItem(item);
416
417
418         QPrefPathsModule * pathsmod(dialog_->pathsModule);
419
420         pathsmod->workingDirED->setText(rc.document_path.c_str());
421         pathsmod->templateDirED->setText(rc.template_path.c_str());
422         pathsmod->backupDirED->setText(rc.backupdir_path.c_str());
423         pathsmod->tempDirCB->setChecked(rc.use_tempdir);
424         pathsmod->tempDirED->setText(rc.tempdir_path.c_str());
425         // FIXME: should be a checkbox only
426         pathsmod->lyxserverDirED->setText(rc.lyxpipes.c_str());
427
428
429         QPrefSpellcheckerModule * spellmod(dialog_->spellcheckerModule);
430
431         item = (rc.isp_command == "ispell") ? 0 : 1;
432         spellmod->spellCommandCO->setCurrentItem(item);
433         // FIXME: remove isp_use_alt_lang
434         spellmod->altLanguageED->setText(rc.isp_alt_lang.c_str());
435         // FIXME: remove isp_use_esc_chars
436         spellmod->escapeCharactersED->setText(rc.isp_esc_chars.c_str());
437         // FIXME: remove isp_use_pers_dict
438         spellmod->persDictionaryED->setText(rc.isp_pers_dict.c_str());
439         spellmod->compoundWordCB->setChecked(rc.isp_accept_compound);
440         spellmod->inputEncodingCB->setChecked(rc.isp_use_input_encoding);
441
442
443         QPrefPrinterModule * printmod(dialog_->printerModule);
444
445         printmod->printerAdaptCB->setChecked(rc.print_adapt_output);
446         printmod->printerCommandED->setText(rc.print_command.c_str());
447         printmod->printerNameED->setText(rc.printer.c_str());
448
449         printmod->printerPageRangeED->setText(rc.print_pagerange_flag.c_str());
450         printmod->printerCopiesED->setText(rc.print_copies_flag.c_str());
451         printmod->printerReverseED->setText(rc.print_reverse_flag.c_str());
452         printmod->printerToPrinterED->setText(rc.print_to_printer.c_str());
453         printmod->printerExtensionED->setText(rc.print_file_extension.c_str());
454         printmod->printerSpoolCommandED->setText(rc.print_file_extension.c_str());
455         printmod->printerPaperTypeED->setText(rc.print_paper_flag.c_str());
456         printmod->printerEvenED->setText(rc.print_evenpage_flag.c_str());
457         printmod->printerOddED->setText(rc.print_oddpage_flag.c_str());
458         printmod->printerCollatedED->setText(rc.print_collcopies_flag.c_str());
459         printmod->printerLandscapeED->setText(rc.print_landscape_flag.c_str());
460         printmod->printerToFileED->setText(rc.print_to_file.c_str());
461         printmod->printerExtraED->setText(rc.print_extra_options.c_str());
462         printmod->printerSpoolPrefixED->setText(rc.print_spool_printerprefix.c_str());
463         printmod->printerPaperSizeED->setText(rc.print_paper_dimension_flag.c_str());
464
465
466         QPrefScreenFontsModule * fontmod(dialog_->screenfontsModule);
467
468         setComboxFont(fontmod->screenRomanCO,
469                       rc.roman_font_name, rc.roman_font_foundry);
470         setComboxFont(fontmod->screenSansCO,
471                       rc.sans_font_name, rc.sans_font_foundry);
472         setComboxFont(fontmod->screenTypewriterCO,
473                       rc.typewriter_font_name, rc.typewriter_font_foundry);
474
475         dialog_->select_roman(fontmod->screenRomanCO->currentText());
476         dialog_->select_sans(fontmod->screenSansCO->currentText());
477         dialog_->select_typewriter(fontmod->screenTypewriterCO->currentText());
478
479         fontmod->screenZoomSB->setValue(rc.zoom);
480         fontmod->screenDpiSB->setValue(int(rc.dpi));
481         fontmod->screenTinyED->setText(tostr(rc.font_sizes[LyXFont::SIZE_TINY]).c_str());
482         fontmod->screenSmallestED->setText(tostr(rc.font_sizes[LyXFont::SIZE_SCRIPT]).c_str());
483         fontmod->screenSmallerED->setText(tostr(rc.font_sizes[LyXFont::SIZE_FOOTNOTE]).c_str());
484         fontmod->screenSmallED->setText(tostr(rc.font_sizes[LyXFont::SIZE_SMALL]).c_str());
485         fontmod->screenNormalED->setText(tostr(rc.font_sizes[LyXFont::SIZE_NORMAL]).c_str());
486         fontmod->screenLargeED->setText(tostr(rc.font_sizes[LyXFont::SIZE_LARGE]).c_str());
487         fontmod->screenLargerED->setText(tostr(rc.font_sizes[LyXFont::SIZE_LARGER]).c_str());
488         fontmod->screenLargestED->setText(tostr(rc.font_sizes[LyXFont::SIZE_LARGEST]).c_str());
489         fontmod->screenHugeED->setText(tostr(rc.font_sizes[LyXFont::SIZE_HUGE]).c_str());
490         fontmod->screenHugerED->setText(tostr(rc.font_sizes[LyXFont::SIZE_HUGER]).c_str());
491
492         formats_ = formats;
493
494         dialog_->updateFormats();
495
496         converters_ = converters;
497
498         dialog_->updateConverters();
499 }