]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrefs.cpp
Silence a warning.
[lyx.git] / src / frontends / qt4 / GuiPrefs.cpp
1 /**
2  * \file GuiPrefs.cpp
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 #include "GuiPrefs.h"
14 #include "ControlPrefs.h"
15
16 #include "qt_helpers.h"
17 #include "GuiApplication.h"
18
19 #include "ConverterCache.h"
20 #include "Session.h"
21 #include "debug.h"
22 #include "Color.h"
23 #include "Font.h"
24 #include "PanelStack.h"
25 #include "GuiFontExample.h"
26 #include "gettext.h"
27
28 #include "support/lstrings.h"
29 #include "support/os.h"
30
31 #include "frontend_helpers.h"
32
33 #include "frontends/alert.h"
34 #include "frontends/Application.h"
35
36 #include <QCheckBox>
37 #include <QColorDialog>
38 #include <QFontDatabase>
39 #include <QLineEdit>
40 #include <QPushButton>
41 #include <QSpinBox>
42 #include <QString>
43 #include <QValidator>
44 #include <QCloseEvent>
45
46 #include <iomanip>
47 #include <sstream>
48 #include <algorithm>
49 #include <utility>
50
51 using namespace Ui;
52
53 using lyx::support::compare_ascii_no_case;
54 using lyx::support::os::external_path;
55 using lyx::support::os::external_path_list;
56 using lyx::support::os::internal_path;
57 using lyx::support::os::internal_path_list;
58
59 using std::endl;
60 using std::string;
61 using std::pair;
62 using std::vector;
63
64
65 namespace lyx {
66 namespace frontend {
67
68 /////////////////////////////////////////////////////////////////////
69 //
70 // Helpers
71 //
72 /////////////////////////////////////////////////////////////////////
73
74 template<class A>
75 static size_t findPos_helper(std::vector<A> const & vec, A const & val)
76 {
77         typedef typename std::vector<A>::const_iterator Cit;
78
79         Cit it = std::find(vec.begin(), vec.end(), val);
80         if (it == vec.end())
81                 return 0;
82         return std::distance(vec.begin(), it);
83 }
84
85
86 static std::pair<string, string> parseFontName(string const & name)
87 {
88         string::size_type const idx = name.find('[');
89         if (idx == string::npos || idx == 0)
90                 return make_pair(name, string());
91         return make_pair(name.substr(0, idx - 1),
92                          name.substr(idx + 1, name.size() - idx - 2));
93 }
94
95
96 static void setComboxFont(QComboBox * cb, string const & family,
97         string const & foundry)
98 {
99         QString fontname = toqstr(family);
100         if (!foundry.empty())
101                 fontname += " [" + toqstr(foundry) + ']';
102
103         for (int i = 0; i < cb->count(); ++i) {
104                 if (cb->itemText(i) == fontname) {
105                         cb->setCurrentIndex(i);
106                         return;
107                 }
108         }
109
110         // Try matching without foundry name
111
112         // We count in reverse in order to prefer the Xft foundry
113         for (int i = cb->count(); --i >= 0;) {
114                 pair<string, string> tmp = parseFontName(fromqstr(cb->itemText(i)));
115                 if (compare_ascii_no_case(tmp.first, family) == 0) {
116                         cb->setCurrentIndex(i);
117                         return;
118                 }
119         }
120
121         // family alone can contain e.g. "Helvetica [Adobe]"
122         pair<string, string> tmpfam = parseFontName(family);
123
124         // We count in reverse in order to prefer the Xft foundry
125         for (int i = cb->count() - 1; i >= 0; --i) {
126                 pair<string, string> tmp = parseFontName(fromqstr(cb->itemText(i)));
127                 if (compare_ascii_no_case(tmp.first, tmpfam.first) == 0) {
128                         cb->setCurrentIndex(i);
129                         return;
130                 }
131         }
132
133         // Bleh, default fonts, and the names couldn't be found. Hack
134         // for bug 1063.
135
136         QFont font;
137         font.setKerning(false);
138
139         if (family == theApp()->romanFontName()) {
140                 font.setStyleHint(QFont::Serif);
141                 font.setFamily(family.c_str());
142         } else if (family == theApp()->sansFontName()) {
143                 font.setStyleHint(QFont::SansSerif);
144                 font.setFamily(family.c_str());
145         } else if (family == theApp()->typewriterFontName()) {
146                 font.setStyleHint(QFont::TypeWriter);
147                 font.setFamily(family.c_str());
148         } else {
149                 lyxerr << "FAILED to find the default font: '"
150                        << foundry << "', '" << family << '\''<< endl;
151                 return;
152         }
153
154         QFontInfo info(font);
155         pair<string, string> tmp = parseFontName(fromqstr(info.family()));
156         string const & default_font_name = tmp.first;
157         lyxerr << "Apparent font is " << default_font_name << endl;
158
159         for (int i = 0; i < cb->count(); ++i) {
160                 lyxerr << "Looking at " << fromqstr(cb->itemText(i)) << endl;
161                 if (compare_ascii_no_case(fromqstr(cb->itemText(i)),
162                                     default_font_name) == 0) {
163                         cb->setCurrentIndex(i);
164                         return;
165                 }
166         }
167
168         lyxerr << "FAILED to find the font: '"
169                << foundry << "', '" << family << '\'' <<endl;
170 }
171
172
173 /////////////////////////////////////////////////////////////////////
174 //
175 // PrefPlaintext
176 //
177 /////////////////////////////////////////////////////////////////////
178
179 PrefPlaintext::PrefPlaintext(QWidget * parent)
180         : PrefModule(_("Plain text"), 0, parent)
181 {
182         setupUi(this);
183         connect(plaintextLinelengthSB, SIGNAL(valueChanged(int)),
184                 this, SIGNAL(changed()));
185         connect(plaintextRoffED, SIGNAL(textChanged(const QString&)),
186                 this, SIGNAL(changed()));
187 }
188
189
190 void PrefPlaintext::apply(LyXRC & rc) const
191 {
192         rc.plaintext_linelen = plaintextLinelengthSB->value();
193         rc.plaintext_roff_command = fromqstr(plaintextRoffED->text());
194 }
195
196
197 void PrefPlaintext::update(LyXRC const & rc)
198 {
199         plaintextLinelengthSB->setValue(rc.plaintext_linelen);
200         plaintextRoffED->setText(toqstr(rc.plaintext_roff_command));
201 }
202
203
204 /////////////////////////////////////////////////////////////////////
205 //
206 // PrefDate
207 //
208 /////////////////////////////////////////////////////////////////////
209
210 PrefDate::PrefDate(QWidget * parent)
211         : PrefModule(_("Date format"), 0, parent)
212 {
213         setupUi(this);
214         connect(DateED, SIGNAL(textChanged(const QString &)),
215                 this, SIGNAL(changed()));
216 }
217
218
219 void PrefDate::apply(LyXRC & rc) const
220 {
221         rc.date_insert_format = fromqstr(DateED->text());
222 }
223
224
225 void PrefDate::update(LyXRC const & rc)
226 {
227         DateED->setText(toqstr(rc.date_insert_format));
228 }
229
230
231 /////////////////////////////////////////////////////////////////////
232 //
233 // PrefKeyboard
234 //
235 /////////////////////////////////////////////////////////////////////
236
237 PrefKeyboard::PrefKeyboard(GuiPrefsDialog * form, QWidget * parent)
238         : PrefModule(_("Keyboard"), form, parent)
239 {
240         setupUi(this);
241
242         connect(keymapCB, SIGNAL(clicked()),
243                 this, SIGNAL(changed()));
244         connect(firstKeymapED, SIGNAL(textChanged(const QString&)),
245                 this, SIGNAL(changed()));
246         connect(secondKeymapED, SIGNAL(textChanged(const QString&)),
247                 this, SIGNAL(changed()));
248 }
249
250
251 void PrefKeyboard::apply(LyXRC & rc) const
252 {
253         // FIXME: can derive CB from the two EDs
254         rc.use_kbmap = keymapCB->isChecked();
255         rc.primary_kbmap = internal_path(fromqstr(firstKeymapED->text()));
256         rc.secondary_kbmap = internal_path(fromqstr(secondKeymapED->text()));
257 }
258
259
260 void PrefKeyboard::update(LyXRC const & rc)
261 {
262         // FIXME: can derive CB from the two EDs
263         keymapCB->setChecked(rc.use_kbmap);
264         firstKeymapED->setText(toqstr(external_path(rc.primary_kbmap)));
265         secondKeymapED->setText(toqstr(external_path(rc.secondary_kbmap)));
266 }
267
268
269 QString PrefKeyboard::testKeymap(QString keymap)
270 {
271         return toqstr(form_->controller().browsekbmap(from_utf8(internal_path(fromqstr(keymap)))));
272 }
273
274
275 void PrefKeyboard::on_firstKeymapPB_clicked(bool)
276 {
277         QString const file = testKeymap(firstKeymapED->text());
278         if (!file.isEmpty())
279                 firstKeymapED->setText(file);
280 }
281
282
283 void PrefKeyboard::on_secondKeymapPB_clicked(bool)
284 {
285         QString const file = testKeymap(secondKeymapED->text());
286         if (!file.isEmpty())
287                 secondKeymapED->setText(file);
288 }
289
290
291 void PrefKeyboard::on_keymapCB_toggled(bool keymap)
292 {
293         firstKeymapLA->setEnabled(keymap);
294         secondKeymapLA->setEnabled(keymap);
295         firstKeymapED->setEnabled(keymap);
296         secondKeymapED->setEnabled(keymap);
297         firstKeymapPB->setEnabled(keymap);
298         secondKeymapPB->setEnabled(keymap);
299 }
300
301
302 /////////////////////////////////////////////////////////////////////
303 //
304 // PrefLatex
305 //
306 /////////////////////////////////////////////////////////////////////
307
308 PrefLatex::PrefLatex(GuiPrefsDialog * form, QWidget * parent)
309         : PrefModule(_("LaTeX"), form, parent)
310 {
311         setupUi(this);
312         connect(latexEncodingED, SIGNAL(textChanged(const QString&)),
313                 this, SIGNAL(changed()));
314         connect(latexChecktexED, SIGNAL(textChanged(const QString&)),
315                 this, SIGNAL(changed()));
316         connect(latexBibtexED, SIGNAL(textChanged(const QString&)),
317                 this, SIGNAL(changed()));
318         connect(latexIndexED, SIGNAL(textChanged(const QString&)),
319                 this, SIGNAL(changed()));
320         connect(latexAutoresetCB, SIGNAL(clicked()),
321                 this, SIGNAL(changed()));
322         connect(latexDviPaperED, SIGNAL(textChanged(const QString&)),
323                 this, SIGNAL(changed()));
324         connect(latexPaperSizeCO, SIGNAL(activated(int)),
325                 this, SIGNAL(changed()));
326
327 #if defined(__CYGWIN__) || defined(_WIN32)
328         pathCB->setVisible(true);
329         connect(pathCB, SIGNAL(clicked()), 
330                 this, SIGNAL(changed()));
331 #else
332         pathCB->setVisible(false);
333 #endif
334 }
335
336
337 void PrefLatex::apply(LyXRC & rc) const
338 {
339         rc.fontenc = fromqstr(latexEncodingED->text());
340         rc.chktex_command = fromqstr(latexChecktexED->text());
341         rc.bibtex_command = fromqstr(latexBibtexED->text());
342         rc.index_command = fromqstr(latexIndexED->text());
343         rc.auto_reset_options = latexAutoresetCB->isChecked();
344         rc.view_dvi_paper_option = fromqstr(latexDviPaperED->text());
345         rc.default_papersize =
346                 form_->controller().toPaperSize(latexPaperSizeCO->currentIndex());
347 #if defined(__CYGWIN__) || defined(_WIN32)
348         rc.windows_style_tex_paths = pathCB->isChecked();
349 #endif
350 }
351
352
353 void PrefLatex::update(LyXRC const & rc)
354 {
355         latexEncodingED->setText(toqstr(rc.fontenc));
356         latexChecktexED->setText(toqstr(rc.chktex_command));
357         latexBibtexED->setText(toqstr(rc.bibtex_command));
358         latexIndexED->setText(toqstr(rc.index_command));
359         latexAutoresetCB->setChecked(rc.auto_reset_options);
360         latexDviPaperED->setText(toqstr(rc.view_dvi_paper_option));
361         latexPaperSizeCO->setCurrentIndex(
362                 form_->controller().fromPaperSize(rc.default_papersize));
363 #if defined(__CYGWIN__) || defined(_WIN32)
364         pathCB->setChecked(rc.windows_style_tex_paths);
365 #endif
366 }
367
368
369 /////////////////////////////////////////////////////////////////////
370 //
371 // PrefScreenFonts
372 //
373 /////////////////////////////////////////////////////////////////////
374
375 PrefScreenFonts::PrefScreenFonts(GuiPrefsDialog * form, QWidget * parent)
376         : PrefModule(_("Screen fonts"), form, parent)
377 {
378         setupUi(this);
379
380         connect(screenRomanCO, SIGNAL(activated(const QString&)),
381                 this, SLOT(select_roman(const QString&)));
382         connect(screenSansCO, SIGNAL(activated(const QString&)),
383                 this, SLOT(select_sans(const QString&)));
384         connect(screenTypewriterCO, SIGNAL(activated(const QString&)),
385                 this, SLOT(select_typewriter(const QString&)));
386
387         QFontDatabase fontdb;
388         QStringList families(fontdb.families());
389         for (QStringList::Iterator it = families.begin(); it != families.end(); ++it) {
390                 screenRomanCO->addItem(*it);
391                 screenSansCO->addItem(*it);
392                 screenTypewriterCO->addItem(*it);
393         }
394         connect(screenRomanCO, SIGNAL(activated(const QString&)),
395                 this, SIGNAL(changed()));
396         connect(screenSansCO, SIGNAL(activated(const QString&)),
397                 this, SIGNAL(changed()));
398         connect(screenTypewriterCO, SIGNAL(activated(const QString&)),
399                 this, SIGNAL(changed()));
400         connect(screenZoomSB, SIGNAL(valueChanged(int)),
401                 this, SIGNAL(changed()));
402         connect(screenDpiSB, SIGNAL(valueChanged(int)),
403                 this, SIGNAL(changed()));
404         connect(screenTinyED, SIGNAL(textChanged(const QString&)),
405                 this, SIGNAL(changed()));
406         connect(screenSmallestED, SIGNAL(textChanged(const QString&)),
407                 this, SIGNAL(changed()));
408         connect(screenSmallerED, SIGNAL(textChanged(const QString&)),
409                 this, SIGNAL(changed()));
410         connect(screenSmallED, SIGNAL(textChanged(const QString&)),
411                 this, SIGNAL(changed()));
412         connect(screenNormalED, SIGNAL(textChanged(const QString&)),
413                 this, SIGNAL(changed()));
414         connect(screenLargeED, SIGNAL(textChanged(const QString&)),
415                 this, SIGNAL(changed()));
416         connect(screenLargerED, SIGNAL(textChanged(const QString&)),
417                 this, SIGNAL(changed()));
418         connect(screenLargestED, SIGNAL(textChanged(const QString&)),
419                 this, SIGNAL(changed()));
420         connect(screenHugeED, SIGNAL(textChanged(const QString&)),
421                 this, SIGNAL(changed()));
422         connect(screenHugerED, SIGNAL(textChanged(const QString&)),
423                 this, SIGNAL(changed()));
424
425         screenTinyED->setValidator(new QDoubleValidator(screenTinyED));
426         screenSmallestED->setValidator(new QDoubleValidator(screenSmallestED));
427         screenSmallerED->setValidator(new QDoubleValidator(screenSmallerED));
428         screenSmallED->setValidator(new QDoubleValidator(screenSmallED));
429         screenNormalED->setValidator(new QDoubleValidator(screenNormalED));
430         screenLargeED->setValidator(new QDoubleValidator(screenLargeED));
431         screenLargerED->setValidator(new QDoubleValidator(screenLargerED));
432         screenLargestED->setValidator(new QDoubleValidator(screenLargestED));
433         screenHugeED->setValidator(new QDoubleValidator(screenHugeED));
434         screenHugerED->setValidator(new QDoubleValidator(screenHugerED));
435 }
436
437
438 void PrefScreenFonts::apply(LyXRC & rc) const
439 {
440         LyXRC const oldrc(rc);
441
442         boost::tie(rc.roman_font_name, rc.roman_font_foundry)
443                 = parseFontName(fromqstr(screenRomanCO->currentText()));
444         boost::tie(rc.sans_font_name, rc.sans_font_foundry) =
445                 parseFontName(fromqstr(screenSansCO->currentText()));
446         boost::tie(rc.typewriter_font_name, rc.typewriter_font_foundry) =
447                 parseFontName(fromqstr(screenTypewriterCO->currentText()));
448
449         rc.zoom = screenZoomSB->value();
450         rc.dpi = screenDpiSB->value();
451         rc.font_sizes[Font::SIZE_TINY] = fromqstr(screenTinyED->text());
452         rc.font_sizes[Font::SIZE_SCRIPT] = fromqstr(screenSmallestED->text());
453         rc.font_sizes[Font::SIZE_FOOTNOTE] = fromqstr(screenSmallerED->text());
454         rc.font_sizes[Font::SIZE_SMALL] = fromqstr(screenSmallED->text());
455         rc.font_sizes[Font::SIZE_NORMAL] = fromqstr(screenNormalED->text());
456         rc.font_sizes[Font::SIZE_LARGE] = fromqstr(screenLargeED->text());
457         rc.font_sizes[Font::SIZE_LARGER] = fromqstr(screenLargerED->text());
458         rc.font_sizes[Font::SIZE_LARGEST] = fromqstr(screenLargestED->text());
459         rc.font_sizes[Font::SIZE_HUGE] = fromqstr(screenHugeED->text());
460         rc.font_sizes[Font::SIZE_HUGER] = fromqstr(screenHugerED->text());
461
462         if (rc.font_sizes != oldrc.font_sizes
463                 || rc.roman_font_name != oldrc.roman_font_name
464                 || rc.sans_font_name != oldrc.sans_font_name
465                 || rc.typewriter_font_name != oldrc.typewriter_font_name
466                 || rc.zoom != oldrc.zoom || rc.dpi != oldrc.dpi) {
467                 form_->controller().updateScreenFonts();
468         }
469 }
470
471
472 void PrefScreenFonts::update(LyXRC const & rc)
473 {
474         setComboxFont(screenRomanCO, rc.roman_font_name,
475                         rc.roman_font_foundry);
476         setComboxFont(screenSansCO, rc.sans_font_name,
477                         rc.sans_font_foundry);
478         setComboxFont(screenTypewriterCO, rc.typewriter_font_name,
479                         rc.typewriter_font_foundry);
480
481         select_roman(screenRomanCO->currentText());
482         select_sans(screenSansCO->currentText());
483         select_typewriter(screenTypewriterCO->currentText());
484
485         screenZoomSB->setValue(rc.zoom);
486         screenDpiSB->setValue(rc.dpi);
487         screenTinyED->setText(toqstr(rc.font_sizes[Font::SIZE_TINY]));
488         screenSmallestED->setText(toqstr(rc.font_sizes[Font::SIZE_SCRIPT]));
489         screenSmallerED->setText(toqstr(rc.font_sizes[Font::SIZE_FOOTNOTE]));
490         screenSmallED->setText(toqstr(rc.font_sizes[Font::SIZE_SMALL]));
491         screenNormalED->setText(toqstr(rc.font_sizes[Font::SIZE_NORMAL]));
492         screenLargeED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGE]));
493         screenLargerED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGER]));
494         screenLargestED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGEST]));
495         screenHugeED->setText(toqstr(rc.font_sizes[Font::SIZE_HUGE]));
496         screenHugerED->setText(toqstr(rc.font_sizes[Font::SIZE_HUGER]));
497 }
498
499 void PrefScreenFonts::select_roman(const QString& name)
500 {
501         screenRomanFE->set(QFont(name), name);
502 }
503
504
505 void PrefScreenFonts::select_sans(const QString& name)
506 {
507         screenSansFE->set(QFont(name), name);
508 }
509
510
511 void PrefScreenFonts::select_typewriter(const QString& name)
512 {
513         screenTypewriterFE->set(QFont(name), name);
514 }
515
516
517 /////////////////////////////////////////////////////////////////////
518 //
519 // PrefColors
520 //
521 /////////////////////////////////////////////////////////////////////
522
523 namespace {
524
525 struct ColorSorter
526 {
527         bool operator()(Color::color const & lhs, Color::color const & rhs) const {
528                 return lcolor.getGUIName(lhs) < lcolor.getGUIName(rhs);
529         }
530 };
531
532 } // namespace anon
533
534 PrefColors::PrefColors(GuiPrefsDialog * form, QWidget * parent)
535         : PrefModule( _("Colors"), form, parent)
536 {
537         setupUi(this);
538
539         // FIXME: all of this initialization should be put into the controller.
540         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg113301.html
541         // for some discussion of why that is not trivial.
542         QPixmap icon(32, 32);
543         for (int i = 0; i < Color::ignore; ++i) {
544                 Color::color lc = static_cast<Color::color>(i);
545                 if (lc == Color::none
546                         || lc == Color::black
547                         || lc == Color::white
548                         || lc == Color::red
549                         || lc == Color::green
550                         || lc == Color::blue
551                         || lc == Color::cyan
552                         || lc == Color::magenta
553                         || lc == Color::yellow
554                         || lc == Color::inherit
555                         || lc == Color::ignore) continue;
556
557                 lcolors_.push_back(lc);
558         }
559         std::sort(lcolors_.begin(), lcolors_.end(), ColorSorter());
560         vector<Color_color>::const_iterator cit = lcolors_.begin();
561         vector<Color_color>::const_iterator const end = lcolors_.end();
562         for (; cit != end; ++cit) {
563                         (void) new QListWidgetItem(QIcon(icon),
564                         toqstr(lcolor.getGUIName(*cit)), lyxObjectsLW);
565         }
566         curcolors_.resize(lcolors_.size());
567         newcolors_.resize(lcolors_.size());
568         // End initialization
569
570         connect(colorChangePB, SIGNAL(clicked()),
571                 this, SLOT(change_color()));
572         connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()),
573                 this, SLOT(change_lyxObjects_selection()));
574         connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)),
575                 this, SLOT(change_color()));
576 }
577
578
579 void PrefColors::apply(LyXRC & /*rc*/) const
580 {
581         for (unsigned int i = 0; i < lcolors_.size(); ++i)
582                 if (curcolors_[i] != newcolors_[i])
583                         form_->controller().setColor(lcolors_[i], fromqstr(newcolors_[i]));
584 }
585
586
587 void PrefColors::update(LyXRC const & /*rc*/)
588 {
589         for (unsigned int i = 0; i < lcolors_.size(); ++i) {
590                 QColor color = QColor(guiApp->colorCache().get(lcolors_[i]));
591                 QPixmap coloritem(32, 32);
592                 coloritem.fill(color);
593                 lyxObjectsLW->item(i)->setIcon(QIcon(coloritem));
594                 newcolors_[i] = curcolors_[i] = color.name();
595         }
596         change_lyxObjects_selection();
597 }
598
599 void PrefColors::change_color()
600 {
601         int const row = lyxObjectsLW->currentRow();
602
603         // just to be sure
604         if (row < 0)
605                 return;
606
607         QString const color = newcolors_[row];
608         QColor c = QColorDialog::getColor(QColor(color), qApp->focusWidget());
609
610         if (c.isValid() && c.name() != color) {
611                 newcolors_[row] = c.name();
612                 QPixmap coloritem(32, 32);
613                 coloritem.fill(c);
614                 lyxObjectsLW->currentItem()->setIcon(QIcon(coloritem));
615                 // emit signal
616                 changed();
617         }
618 }
619
620 void PrefColors::change_lyxObjects_selection()
621 {
622         colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0);
623 }
624
625
626 /////////////////////////////////////////////////////////////////////
627 //
628 // PrefDisplay
629 //
630 /////////////////////////////////////////////////////////////////////
631
632 PrefDisplay::PrefDisplay(QWidget * parent)
633         : PrefModule(_("Graphics"), 0, parent)
634 {
635         setupUi(this);
636         connect(instantPreviewCO, SIGNAL(activated(int)),
637                 this, SIGNAL(changed()));
638         connect(displayGraphicsCO, SIGNAL(activated(int)),
639                 this, SIGNAL(changed()));
640 }
641
642
643 void PrefDisplay::apply(LyXRC & rc) const
644 {
645         switch (instantPreviewCO->currentIndex()) {
646                 case 0: rc.preview = LyXRC::PREVIEW_OFF; break;
647                 case 1: rc.preview = LyXRC::PREVIEW_NO_MATH; break;
648                 case 2: rc.preview = LyXRC::PREVIEW_ON; break;
649         }
650
651         lyx::graphics::DisplayType dtype;
652         switch (displayGraphicsCO->currentIndex()) {
653                 case 3: dtype = lyx::graphics::NoDisplay; break;
654                 case 2: dtype = lyx::graphics::ColorDisplay; break;
655                 case 1: dtype = lyx::graphics::GrayscaleDisplay;        break;
656                 case 0: dtype = lyx::graphics::MonochromeDisplay; break;
657                 default: dtype = lyx::graphics::GrayscaleDisplay;
658         }
659         rc.display_graphics = dtype;
660
661         // FIXME!! The graphics cache no longer has a changeDisplay method.
662 #if 0
663         if (old_value != rc.display_graphics) {
664                 lyx::graphics::GCache & gc = lyx::graphics::GCache::get();
665                 gc.changeDisplay();
666         }
667 #endif
668 }
669
670
671 void PrefDisplay::update(LyXRC const & rc)
672 {
673         switch (rc.preview) {
674         case LyXRC::PREVIEW_OFF:
675                 instantPreviewCO->setCurrentIndex(0);
676                 break;
677         case LyXRC::PREVIEW_NO_MATH :
678                 instantPreviewCO->setCurrentIndex(1);
679                 break;
680         case LyXRC::PREVIEW_ON :
681                 instantPreviewCO->setCurrentIndex(2);
682                 break;
683         }
684
685         int item = 2;
686         switch (rc.display_graphics) {
687                 case lyx::graphics::NoDisplay:          item = 3; break;
688                 case lyx::graphics::ColorDisplay:       item = 2; break;
689                 case lyx::graphics::GrayscaleDisplay:   item = 1; break;
690                 case lyx::graphics::MonochromeDisplay:  item = 0; break;
691                 default: break;
692         }
693         displayGraphicsCO->setCurrentIndex(item);
694 }
695
696
697 /////////////////////////////////////////////////////////////////////
698 //
699 // PrefPaths
700 //
701 /////////////////////////////////////////////////////////////////////
702
703 PrefPaths::PrefPaths(GuiPrefsDialog * form, QWidget * parent)
704         : PrefModule(_("Paths"), form, parent)
705 {
706         setupUi(this);
707         connect(templateDirPB, SIGNAL(clicked()), this, SLOT(select_templatedir()));
708         connect(tempDirPB, SIGNAL(clicked()), this, SLOT(select_tempdir()));
709         connect(backupDirPB, SIGNAL(clicked()), this, SLOT(select_backupdir()));
710         connect(workingDirPB, SIGNAL(clicked()), this, SLOT(select_workingdir()));
711         connect(lyxserverDirPB, SIGNAL(clicked()), this, SLOT(select_lyxpipe()));
712         connect(workingDirED, SIGNAL(textChanged(const QString&)),
713                 this, SIGNAL(changed()));
714         connect(templateDirED, SIGNAL(textChanged(const QString&)),
715                 this, SIGNAL(changed()));
716         connect(backupDirED, SIGNAL(textChanged(const QString&)),
717                 this, SIGNAL(changed()));
718         connect(tempDirED, SIGNAL(textChanged(const QString&)),
719                 this, SIGNAL(changed()));
720         connect(lyxserverDirED, SIGNAL(textChanged(const QString&)),
721                 this, SIGNAL(changed()));
722         connect(pathPrefixED, SIGNAL(textChanged(const QString&)),
723                 this, SIGNAL(changed()));
724 }
725
726
727 void PrefPaths::apply(LyXRC & rc) const
728 {
729         rc.document_path = internal_path(fromqstr(workingDirED->text()));
730         rc.template_path = internal_path(fromqstr(templateDirED->text()));
731         rc.backupdir_path = internal_path(fromqstr(backupDirED->text()));
732         rc.tempdir_path = internal_path(fromqstr(tempDirED->text()));
733         rc.path_prefix = internal_path_list(fromqstr(pathPrefixED->text()));
734         // FIXME: should be a checkbox only
735         rc.lyxpipes = internal_path(fromqstr(lyxserverDirED->text()));
736 }
737
738
739 void PrefPaths::update(LyXRC const & rc)
740 {
741         workingDirED->setText(toqstr(external_path(rc.document_path)));
742         templateDirED->setText(toqstr(external_path(rc.template_path)));
743         backupDirED->setText(toqstr(external_path(rc.backupdir_path)));
744         tempDirED->setText(toqstr(external_path(rc.tempdir_path)));
745         pathPrefixED->setText(toqstr(external_path_list(rc.path_prefix)));
746         // FIXME: should be a checkbox only
747         lyxserverDirED->setText(toqstr(external_path(rc.lyxpipes)));
748 }
749
750
751 void PrefPaths::select_templatedir()
752 {
753         docstring file(form_->controller().browsedir(
754                 from_utf8(internal_path(fromqstr(templateDirED->text()))),
755                 _("Select a document templates directory")));
756         if (!file.empty())
757                 templateDirED->setText(toqstr(file));
758 }
759
760
761 void PrefPaths::select_tempdir()
762 {
763         docstring file(form_->controller().browsedir(
764                 from_utf8(internal_path(fromqstr(tempDirED->text()))),
765                 _("Select a temporary directory")));
766         if (!file.empty())
767                 tempDirED->setText(toqstr(file));
768 }
769
770
771 void PrefPaths::select_backupdir()
772 {
773         docstring file(form_->controller().browsedir(
774                 from_utf8(internal_path(fromqstr(backupDirED->text()))),
775                 _("Select a backups directory")));
776         if (!file.empty())
777                 backupDirED->setText(toqstr(file));
778 }
779
780
781 void PrefPaths::select_workingdir()
782 {
783         docstring file(form_->controller().browsedir(
784                 from_utf8(internal_path(fromqstr(workingDirED->text()))),
785                 _("Select a document directory")));
786         if (!file.empty())
787                 workingDirED->setText(toqstr(file));
788 }
789
790
791 void PrefPaths::select_lyxpipe()
792 {
793         docstring file(form_->controller().browse(
794                 from_utf8(internal_path(fromqstr(lyxserverDirED->text()))),
795                 _("Give a filename for the LyX server pipe")));
796         if (!file.empty())
797                 lyxserverDirED->setText(toqstr(file));
798 }
799
800
801 /////////////////////////////////////////////////////////////////////
802 //
803 // PrefSpellchecker
804 //
805 /////////////////////////////////////////////////////////////////////
806
807 PrefSpellchecker::PrefSpellchecker(GuiPrefsDialog * form, QWidget * parent)
808         : PrefModule(_("Spellchecker"), form, parent)
809 {
810         setupUi(this);
811
812         connect(persDictionaryPB, SIGNAL(clicked()), this, SLOT(select_dict()));
813 #if defined (USE_ISPELL)
814         connect(spellCommandCO, SIGNAL(activated(int)),
815                 this, SIGNAL(changed()));
816 #else
817         spellCommandCO->setEnabled(false);
818 #endif
819         connect(altLanguageED, SIGNAL(textChanged(const QString&)),
820                 this, SIGNAL(changed()));
821         connect(escapeCharactersED, SIGNAL(textChanged(const QString&)),
822                 this, SIGNAL(changed()));
823         connect(persDictionaryED, SIGNAL(textChanged(const QString&)),
824                 this, SIGNAL(changed()));
825         connect(compoundWordCB, SIGNAL(clicked()),
826                 this, SIGNAL(changed()));
827         connect(inputEncodingCB, SIGNAL(clicked()),
828                 this, SIGNAL(changed()));
829
830         spellCommandCO->addItem(qt_("ispell"));
831         spellCommandCO->addItem(qt_("aspell"));
832         spellCommandCO->addItem(qt_("hspell"));
833 #ifdef USE_PSPELL
834         spellCommandCO->addItem(qt_("pspell (library)"));
835 #else
836 #ifdef USE_ASPELL
837         spellCommandCO->addItem(qt_("aspell (library)"));
838 #endif
839 #endif
840 }
841
842
843 void PrefSpellchecker::apply(LyXRC & rc) const
844 {
845         switch (spellCommandCO->currentIndex()) {
846                 case 0:
847                 case 1:
848                 case 2:
849                         rc.use_spell_lib = false;
850                         rc.isp_command = fromqstr(spellCommandCO->currentText());
851                         break;
852                 case 3:
853                         rc.use_spell_lib = true;
854                         break;
855         }
856
857         // FIXME: remove isp_use_alt_lang
858         rc.isp_alt_lang = fromqstr(altLanguageED->text());
859         rc.isp_use_alt_lang = !rc.isp_alt_lang.empty();
860         // FIXME: remove isp_use_esc_chars
861         rc.isp_esc_chars = fromqstr(escapeCharactersED->text());
862         rc.isp_use_esc_chars = !rc.isp_esc_chars.empty();
863         // FIXME: remove isp_use_pers_dict
864         rc.isp_pers_dict = internal_path(fromqstr(persDictionaryED->text()));
865         rc.isp_use_pers_dict = !rc.isp_pers_dict.empty();
866         rc.isp_accept_compound = compoundWordCB->isChecked();
867         rc.isp_use_input_encoding = inputEncodingCB->isChecked();
868 }
869
870
871 void PrefSpellchecker::update(LyXRC const & rc)
872 {
873         spellCommandCO->setCurrentIndex(0);
874
875         if (rc.isp_command == "ispell") {
876                 spellCommandCO->setCurrentIndex(0);
877         } else if (rc.isp_command == "aspell") {
878                 spellCommandCO->setCurrentIndex(1);
879         } else if (rc.isp_command == "hspell") {
880                 spellCommandCO->setCurrentIndex(2);
881         }
882
883         if (rc.use_spell_lib) {
884 #if defined(USE_ASPELL) || defined(USE_PSPELL)
885                 spellCommandCO->setCurrentIndex(3);
886 #endif
887         }
888
889         // FIXME: remove isp_use_alt_lang
890         altLanguageED->setText(toqstr(rc.isp_alt_lang));
891         // FIXME: remove isp_use_esc_chars
892         escapeCharactersED->setText(toqstr(rc.isp_esc_chars));
893         // FIXME: remove isp_use_pers_dict
894         persDictionaryED->setText(toqstr(external_path(rc.isp_pers_dict)));
895         compoundWordCB->setChecked(rc.isp_accept_compound);
896         inputEncodingCB->setChecked(rc.isp_use_input_encoding);
897 }
898
899
900 void PrefSpellchecker::select_dict()
901 {
902         docstring file(form_->controller().browsedict(
903                 from_utf8(internal_path(fromqstr(persDictionaryED->text())))));
904         if (!file.empty())
905                 persDictionaryED->setText(toqstr(file));
906 }
907
908
909
910 /////////////////////////////////////////////////////////////////////
911 //
912 // PrefConverters
913 //
914 /////////////////////////////////////////////////////////////////////
915
916
917 PrefConverters::PrefConverters(GuiPrefsDialog * form, QWidget * parent)
918         : PrefModule(_("Converters"), form, parent)
919 {
920         setupUi(this);
921
922         connect(converterNewPB, SIGNAL(clicked()),
923                 this, SLOT(update_converter()));
924         connect(converterRemovePB, SIGNAL(clicked()),
925                 this, SLOT(remove_converter()));
926         connect(converterModifyPB, SIGNAL(clicked()),
927                 this, SLOT(update_converter()));
928         connect(convertersLW, SIGNAL(currentRowChanged(int)),
929                 this, SLOT(switch_converter()));
930         connect(converterFromCO, SIGNAL(activated(const QString&)),
931                 this, SLOT(converter_changed()));
932         connect(converterToCO, SIGNAL(activated(const QString&)),
933                 this, SLOT(converter_changed()));
934         connect(converterED, SIGNAL(textEdited(const QString&)),
935                 this, SLOT(converter_changed()));
936         connect(converterFlagED, SIGNAL(textEdited(const QString&)),
937                 this, SLOT(converter_changed()));
938         connect(converterNewPB, SIGNAL(clicked()),
939                 this, SIGNAL(changed()));
940         connect(converterRemovePB, SIGNAL(clicked()),
941                 this, SIGNAL(changed()));
942         connect(converterModifyPB, SIGNAL(clicked()),
943                 this, SIGNAL(changed()));
944         connect(maxAgeLE, SIGNAL(textEdited(const QString&)),
945                 this, SIGNAL(changed()));
946
947         maxAgeLE->setValidator(new QDoubleValidator(maxAgeLE));
948         //converterDefGB->setFocusProxy(convertersLW);
949 }
950
951
952 void PrefConverters::apply(LyXRC & rc) const
953 {
954         rc.use_converter_cache = cacheCB->isChecked();
955         rc.converter_cache_maxage = int(maxAgeLE->text().toDouble() * 86400.0);
956 }
957
958
959 void PrefConverters::update(LyXRC const & rc)
960 {
961         cacheCB->setChecked(rc.use_converter_cache);
962         QString max_age;
963         max_age.setNum(double(rc.converter_cache_maxage) / 86400.0, 'g', 6);
964         maxAgeLE->setText(max_age);
965         updateGui();
966 }
967
968
969 void PrefConverters::updateGui()
970 {
971         form_->formats().sort();
972         form_->converters().update(form_->formats());
973         // save current selection
974         QString current = converterFromCO->currentText()
975                 + " -> " + converterToCO->currentText();
976
977         converterFromCO->clear();
978         converterToCO->clear();
979
980         Formats::const_iterator cit = form_->formats().begin();
981         Formats::const_iterator end = form_->formats().end();
982         for (; cit != end; ++cit) {
983                 converterFromCO->addItem(toqstr(cit->prettyname()));
984                 converterToCO->addItem(toqstr(cit->prettyname()));
985         }
986
987         // currentRowChanged(int) is also triggered when updating the listwidget
988         // block signals to avoid unnecessary calls to switch_converter()
989         convertersLW->blockSignals(true);
990         convertersLW->clear();
991
992         Converters::const_iterator ccit = form_->converters().begin();
993         Converters::const_iterator cend = form_->converters().end();
994         for (; ccit != cend; ++ccit) {
995                 std::string const name =
996                         ccit->From->prettyname() + " -> " + ccit->To->prettyname();
997                 int type = form_->converters().getNumber(ccit->From->name(), ccit->To->name());
998                 new QListWidgetItem(toqstr(name), convertersLW, type);
999         }
1000         convertersLW->sortItems(Qt::AscendingOrder);
1001         convertersLW->blockSignals(false);
1002
1003         // restore selection
1004         if (!current.isEmpty()) {
1005                 QList<QListWidgetItem *> const item =
1006                         convertersLW->findItems(current, Qt::MatchExactly);
1007                 if (!item.isEmpty())
1008                         convertersLW->setCurrentItem(item.at(0));
1009         }
1010
1011         // select first element if restoring failed
1012         if (convertersLW->currentRow() == -1)
1013                 convertersLW->setCurrentRow(0);
1014
1015         updateButtons();
1016 }
1017
1018
1019 void PrefConverters::switch_converter()
1020 {
1021         int const cnr = convertersLW->currentItem()->type();
1022         Converter const & c(form_->converters().get(cnr));
1023         converterFromCO->setCurrentIndex(form_->formats().getNumber(c.from));
1024         converterToCO->setCurrentIndex(form_->formats().getNumber(c.to));
1025         converterED->setText(toqstr(c.command));
1026         converterFlagED->setText(toqstr(c.flags));
1027
1028         updateButtons();
1029 }
1030
1031
1032 void PrefConverters::converter_changed()
1033 {
1034         updateButtons();
1035 }
1036
1037
1038 void PrefConverters::updateButtons()
1039 {
1040         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1041         Format const & to = form_->formats().get(converterToCO->currentIndex());
1042         int const sel = form_->converters().getNumber(from.name(), to.name());
1043         bool const known = !(sel < 0);
1044         bool const valid = !(converterED->text().isEmpty()
1045                 || from.name() == to.name());
1046
1047         int const cnr = convertersLW->currentItem()->type();
1048         Converter const & c(form_->converters().get(cnr));
1049         string const old_command = c.command;
1050         string const old_flag = c.flags;
1051         string const new_command(fromqstr(converterED->text()));
1052         string const new_flag(fromqstr(converterFlagED->text()));
1053
1054         bool modified = ((old_command != new_command) || (old_flag != new_flag));
1055
1056         converterModifyPB->setEnabled(valid && known && modified);
1057         converterNewPB->setEnabled(valid && !known);
1058         converterRemovePB->setEnabled(known);
1059
1060         maxAgeLE->setEnabled(cacheCB->isChecked());
1061         maxAgeLA->setEnabled(cacheCB->isChecked());
1062 }
1063
1064
1065 // FIXME: user must
1066 // specify unique from/to or it doesn't appear. This is really bad UI
1067 // this is why we can use the same function for both new and modify
1068 void PrefConverters::update_converter()
1069 {
1070         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1071         Format const & to = form_->formats().get(converterToCO->currentIndex());
1072         string const flags = fromqstr(converterFlagED->text());
1073         string const command = fromqstr(converterED->text());
1074
1075         Converter const * old =
1076                 form_->converters().getConverter(from.name(), to.name());
1077         form_->converters().add(from.name(), to.name(), command, flags);
1078
1079         if (!old)
1080                 form_->converters().updateLast(form_->formats());
1081
1082         updateGui();
1083
1084         // Remove all files created by this converter from the cache, since
1085         // the modified converter might create different files.
1086         ConverterCache::get().remove_all(from.name(), to.name());
1087 }
1088
1089
1090 void PrefConverters::remove_converter()
1091 {
1092         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1093         Format const & to = form_->formats().get(converterToCO->currentIndex());
1094         form_->converters().erase(from.name(), to.name());
1095
1096         updateGui();
1097
1098         // Remove all files created by this converter from the cache, since
1099         // a possible new converter might create different files.
1100         ConverterCache::get().remove_all(from.name(), to.name());
1101 }
1102
1103
1104 void PrefConverters::on_cacheCB_stateChanged(int state)
1105 {
1106         maxAgeLE->setEnabled(state == Qt::Checked);
1107         maxAgeLA->setEnabled(state == Qt::Checked);
1108         changed();
1109 }
1110
1111
1112 /////////////////////////////////////////////////////////////////////
1113 //
1114 // PrefFileformats
1115 //
1116 /////////////////////////////////////////////////////////////////////
1117 FormatValidator::FormatValidator(QWidget * parent, Formats const & f) 
1118         : QValidator(parent), formats_(f)
1119 {
1120 }
1121
1122
1123 void FormatValidator::fixup(QString & input) const
1124 {
1125         Formats::const_iterator cit = formats_.begin();
1126         Formats::const_iterator end = formats_.end();
1127         for (; cit != end; ++cit) {
1128                 string const name = str(cit);
1129                 if (distance(formats_.begin(), cit) == nr()) {
1130                         input = toqstr(name);
1131                         return;
1132
1133                 }
1134         }
1135 }
1136
1137
1138 QValidator::State FormatValidator::validate(QString & input, int & /*pos*/) const
1139 {
1140         Formats::const_iterator cit = formats_.begin();
1141         Formats::const_iterator end = formats_.end();
1142         bool unknown = true;
1143         for (; unknown && cit != end; ++cit) {
1144                 string const name = str(cit);
1145                 if (distance(formats_.begin(), cit) != nr())
1146                         unknown = toqstr(name) != input;
1147         }
1148
1149         if (unknown && !input.isEmpty()) 
1150                 return QValidator::Acceptable;
1151         else
1152                 return QValidator::Intermediate;
1153 }
1154
1155
1156 int FormatValidator::nr() const
1157 {
1158         QComboBox * p = qobject_cast<QComboBox *>(parent());
1159         return p->itemData(p->currentIndex()).toInt();
1160 }
1161
1162
1163 FormatNameValidator::FormatNameValidator(QWidget * parent, Formats const & f) 
1164         : FormatValidator(parent, f)
1165 {
1166 }
1167
1168 std::string FormatNameValidator::str(Formats::const_iterator it) const
1169 {
1170         return it->name();
1171 }
1172
1173
1174 FormatPrettynameValidator::FormatPrettynameValidator(QWidget * parent, Formats const & f) 
1175         : FormatValidator(parent, f)
1176 {
1177 }
1178
1179
1180 std::string FormatPrettynameValidator::str(Formats::const_iterator it) const
1181 {
1182         return it->prettyname();
1183 }
1184
1185
1186 PrefFileformats::PrefFileformats(GuiPrefsDialog * form, QWidget * parent)
1187         : PrefModule(_("File formats"), form, parent)
1188 {
1189         setupUi(this);
1190         formatED->setValidator(new FormatNameValidator(formatsCB, form_->formats()));
1191         formatsCB->setValidator(new FormatPrettynameValidator(formatsCB, form_->formats()));
1192
1193         connect(documentCB, SIGNAL(clicked()),
1194                 this, SLOT(setFlags()));
1195         connect(vectorCB, SIGNAL(clicked()),
1196                 this, SLOT(setFlags()));
1197         connect(formatsCB->lineEdit(), SIGNAL(editingFinished()),
1198                 this, SLOT(updatePrettyname()));
1199         connect(formatsCB->lineEdit(), SIGNAL(textEdited(const QString &)),
1200                 this, SIGNAL(changed()));
1201 }
1202
1203
1204 void PrefFileformats::apply(LyXRC & /*rc*/) const
1205 {
1206 }
1207
1208
1209 void PrefFileformats::update(LyXRC const & /*rc*/)
1210 {
1211         updateView();
1212 }
1213
1214
1215 void PrefFileformats::updateView()
1216 {
1217         QString const current = formatsCB->currentText();
1218
1219         // update combobox with formats
1220         formatsCB->blockSignals(true);
1221         formatsCB->clear();
1222         form_->formats().sort();
1223         Formats::const_iterator cit = form_->formats().begin();
1224         Formats::const_iterator end = form_->formats().end();
1225         for (; cit != end; ++cit)
1226                 formatsCB->addItem(toqstr(cit->prettyname()),
1227                                                         QVariant(form_->formats().getNumber(cit->name())) );
1228
1229         // restore selection
1230         int const item = formatsCB->findText(current, Qt::MatchExactly);
1231         formatsCB->setCurrentIndex(item < 0 ? 0 : item);
1232         on_formatsCB_currentIndexChanged(item < 0 ? 0 : item);
1233         formatsCB->blockSignals(false);
1234 }
1235
1236
1237 void PrefFileformats::on_formatsCB_currentIndexChanged(int i)
1238 {
1239         int const nr = formatsCB->itemData(i).toInt();
1240         Format const f = form_->formats().get(nr);
1241
1242         formatED->setText(toqstr(f.name()));
1243         copierED->setText(toqstr(form_->movers().command(f.name())));
1244         extensionED->setText(toqstr(f.extension()));
1245         shortcutED->setText(toqstr(f.shortcut()));
1246         viewerED->setText(toqstr(f.viewer()));
1247         editorED->setText(toqstr(f.editor()));
1248         documentCB->setChecked((f.documentFormat()));
1249         vectorCB->setChecked((f.vectorFormat()));
1250 }
1251
1252
1253 void PrefFileformats::setFlags()
1254 {
1255         int flags = Format::none;
1256         if (documentCB->isChecked())
1257                 flags |= Format::document;
1258         if (vectorCB->isChecked())
1259                 flags |= Format::vector;
1260         currentFormat().setFlags(flags);
1261         changed();
1262 }
1263
1264
1265 void PrefFileformats::on_copierED_textEdited(const QString & s)
1266 {
1267         string const fmt = fromqstr(formatED->text());
1268         form_->movers().set(fmt, fromqstr(s));
1269         changed();
1270 }
1271
1272
1273 void PrefFileformats::on_extensionED_textEdited(const QString & s)
1274 {
1275         currentFormat().setExtension(fromqstr(s));
1276         changed();
1277 }
1278
1279 void PrefFileformats::on_viewerED_textEdited(const QString & s)
1280 {
1281         currentFormat().setViewer(fromqstr(s));
1282         changed();
1283 }
1284
1285
1286 void PrefFileformats::on_editorED_textEdited(const QString & s)
1287 {
1288         currentFormat().setEditor(fromqstr(s));
1289         changed();
1290 }
1291
1292
1293 void PrefFileformats::on_shortcutED_textEdited(const QString & s)
1294 {
1295         currentFormat().setShortcut(fromqstr(s));
1296         changed();
1297 }
1298
1299
1300 void PrefFileformats::on_formatED_editingFinished()
1301 {
1302         string const newname = fromqstr(formatED->displayText());
1303         if (newname == currentFormat().name())
1304                 return;
1305
1306         currentFormat().setName(newname);
1307         changed();
1308 }
1309
1310
1311 void PrefFileformats::on_formatED_textChanged(const QString &)
1312 {
1313         QString t = formatED->text();
1314         int p = 0;
1315         bool valid = formatED->validator()->validate(t, p) == QValidator::Acceptable;
1316         setValid(formatLA, valid);
1317 }
1318
1319
1320 void PrefFileformats::on_formatsCB_editTextChanged(const QString &)
1321 {
1322         QString t = formatsCB->currentText();
1323         int p = 0;
1324         bool valid = formatsCB->validator()->validate(t, p) == QValidator::Acceptable;
1325         setValid(formatsLA, valid);
1326 }
1327
1328
1329 void PrefFileformats::updatePrettyname()
1330 {
1331         string const newname = fromqstr(formatsCB->currentText());
1332         if (newname == currentFormat().prettyname())
1333                 return;
1334
1335         currentFormat().setPrettyname(newname);
1336         formatsChanged();
1337         updateView();
1338         changed();
1339 }
1340
1341
1342 Format & PrefFileformats::currentFormat()
1343 {
1344         int const i = formatsCB->currentIndex();
1345         int const nr = formatsCB->itemData(i).toInt();
1346         return form_->formats().get(nr);
1347 }
1348
1349
1350 void PrefFileformats::on_formatNewPB_clicked()
1351 {
1352         form_->formats().add("", "", "", "", "", "", Format::none);
1353         updateView();
1354         formatsCB->setCurrentIndex(0);
1355         formatsCB->setFocus(Qt::OtherFocusReason);
1356 }
1357
1358
1359 void PrefFileformats::on_formatRemovePB_clicked()
1360 {
1361         int const i = formatsCB->currentIndex();
1362         int const nr = formatsCB->itemData(i).toInt();
1363         string const current_text = form_->formats().get(nr).name();
1364         if (form_->converters().formatIsUsed(current_text)) {
1365                 Alert::error(_("Format in use"),
1366                              _("Cannot remove a Format used by a Converter. "
1367                                             "Remove the converter first."));
1368                 return;
1369         }
1370
1371         form_->formats().erase(current_text);
1372         formatsChanged();
1373         updateView();
1374         on_formatsCB_editTextChanged(formatsCB->currentText());
1375         changed();
1376 }
1377
1378
1379 /////////////////////////////////////////////////////////////////////
1380 //
1381 // PrefLanguage
1382 //
1383 /////////////////////////////////////////////////////////////////////
1384
1385 PrefLanguage::PrefLanguage(QWidget * parent)
1386         : PrefModule(_("Language"), 0, parent)
1387 {
1388         setupUi(this);
1389
1390         connect(rtlCB, SIGNAL(clicked()),
1391                 this, SIGNAL(changed()));
1392         connect(markForeignCB, SIGNAL(clicked()),
1393                 this, SIGNAL(changed()));
1394         connect(autoBeginCB, SIGNAL(clicked()),
1395                 this, SIGNAL(changed()));
1396         connect(autoEndCB, SIGNAL(clicked()),
1397                 this, SIGNAL(changed()));
1398         connect(useBabelCB, SIGNAL(clicked()),
1399                 this, SIGNAL(changed()));
1400         connect(globalCB, SIGNAL(clicked()),
1401                 this, SIGNAL(changed()));
1402         connect(languagePackageED, SIGNAL(textChanged(const QString&)),
1403                 this, SIGNAL(changed()));
1404         connect(startCommandED, SIGNAL(textChanged(const QString&)),
1405                 this, SIGNAL(changed()));
1406         connect(endCommandED, SIGNAL(textChanged(const QString&)),
1407                 this, SIGNAL(changed()));
1408         connect(defaultLanguageCO, SIGNAL(activated(int)),
1409                 this, SIGNAL(changed()));
1410
1411         defaultLanguageCO->clear();
1412
1413         // store the lang identifiers for later
1414         std::vector<LanguagePair> const langs = frontend::getLanguageData(false);
1415         std::vector<LanguagePair>::const_iterator lit  = langs.begin();
1416         std::vector<LanguagePair>::const_iterator lend = langs.end();
1417         lang_.clear();
1418         for (; lit != lend; ++lit) {
1419                 defaultLanguageCO->addItem(toqstr(lit->first));
1420                 lang_.push_back(lit->second);
1421         }
1422 }
1423
1424
1425 void PrefLanguage::apply(LyXRC & rc) const
1426 {
1427         // FIXME: remove rtl_support bool
1428         rc.rtl_support = rtlCB->isChecked();
1429         rc.mark_foreign_language = markForeignCB->isChecked();
1430         rc.language_auto_begin = autoBeginCB->isChecked();
1431         rc.language_auto_end = autoEndCB->isChecked();
1432         rc.language_use_babel = useBabelCB->isChecked();
1433         rc.language_global_options = globalCB->isChecked();
1434         rc.language_package = fromqstr(languagePackageED->text());
1435         rc.language_command_begin = fromqstr(startCommandED->text());
1436         rc.language_command_end = fromqstr(endCommandED->text());
1437         rc.default_language = lang_[defaultLanguageCO->currentIndex()];
1438 }
1439
1440
1441 void PrefLanguage::update(LyXRC const & rc)
1442 {
1443         // FIXME: remove rtl_support bool
1444         rtlCB->setChecked(rc.rtl_support);
1445         markForeignCB->setChecked(rc.mark_foreign_language);
1446         autoBeginCB->setChecked(rc.language_auto_begin);
1447         autoEndCB->setChecked(rc.language_auto_end);
1448         useBabelCB->setChecked(rc.language_use_babel);
1449         globalCB->setChecked(rc.language_global_options);
1450         languagePackageED->setText(toqstr(rc.language_package));
1451         startCommandED->setText(toqstr(rc.language_command_begin));
1452         endCommandED->setText(toqstr(rc.language_command_end));
1453
1454         int const pos = int(findPos_helper(lang_, rc.default_language));
1455         defaultLanguageCO->setCurrentIndex(pos);
1456 }
1457
1458
1459 /////////////////////////////////////////////////////////////////////
1460 //
1461 // PrefPrinter
1462 //
1463 /////////////////////////////////////////////////////////////////////
1464
1465 PrefPrinter::PrefPrinter(QWidget * parent)
1466         : PrefModule(_("Printer"), 0, parent)
1467 {
1468         setupUi(this);
1469
1470         connect(printerAdaptCB, SIGNAL(clicked()),
1471                 this, SIGNAL(changed()));
1472         connect(printerCommandED, SIGNAL(textChanged(const QString&)),
1473                 this, SIGNAL(changed()));
1474         connect(printerNameED, SIGNAL(textChanged(const QString&)),
1475                 this, SIGNAL(changed()));
1476         connect(printerPageRangeED, SIGNAL(textChanged(const QString&)),
1477                 this, SIGNAL(changed()));
1478         connect(printerCopiesED, SIGNAL(textChanged(const QString&)),
1479                 this, SIGNAL(changed()));
1480         connect(printerReverseED, SIGNAL(textChanged(const QString&)),
1481                 this, SIGNAL(changed()));
1482         connect(printerToPrinterED, SIGNAL(textChanged(const QString&)),
1483                 this, SIGNAL(changed()));
1484         connect(printerExtensionED, SIGNAL(textChanged(const QString&)),
1485                 this, SIGNAL(changed()));
1486         connect(printerSpoolCommandED, SIGNAL(textChanged(const QString&)),
1487                 this, SIGNAL(changed()));
1488         connect(printerPaperTypeED, SIGNAL(textChanged(const QString&)),
1489                 this, SIGNAL(changed()));
1490         connect(printerEvenED, SIGNAL(textChanged(const QString&)),
1491                 this, SIGNAL(changed()));
1492         connect(printerOddED, SIGNAL(textChanged(const QString&)),
1493                 this, SIGNAL(changed()));
1494         connect(printerCollatedED, SIGNAL(textChanged(const QString&)),
1495                 this, SIGNAL(changed()));
1496         connect(printerLandscapeED, SIGNAL(textChanged(const QString&)),
1497                 this, SIGNAL(changed()));
1498         connect(printerToFileED, SIGNAL(textChanged(const QString&)),
1499                 this, SIGNAL(changed()));
1500         connect(printerExtraED, SIGNAL(textChanged(const QString&)),
1501                 this, SIGNAL(changed()));
1502         connect(printerSpoolPrefixED, SIGNAL(textChanged(const QString&)),
1503                 this, SIGNAL(changed()));
1504         connect(printerPaperSizeED, SIGNAL(textChanged(const QString&)),
1505                 this, SIGNAL(changed()));
1506 }
1507
1508
1509 void PrefPrinter::apply(LyXRC & rc) const
1510 {
1511         rc.print_adapt_output = printerAdaptCB->isChecked();
1512         rc.print_command = fromqstr(printerCommandED->text());
1513         rc.printer = fromqstr(printerNameED->text());
1514
1515         rc.print_pagerange_flag = fromqstr(printerPageRangeED->text());
1516         rc.print_copies_flag = fromqstr(printerCopiesED->text());
1517         rc.print_reverse_flag = fromqstr(printerReverseED->text());
1518         rc.print_to_printer = fromqstr(printerToPrinterED->text());
1519         rc.print_file_extension = fromqstr(printerExtensionED->text());
1520         rc.print_spool_command = fromqstr(printerSpoolCommandED->text());
1521         rc.print_paper_flag = fromqstr(printerPaperTypeED->text());
1522         rc.print_evenpage_flag = fromqstr(printerEvenED->text());
1523         rc.print_oddpage_flag = fromqstr(printerOddED->text());
1524         rc.print_collcopies_flag = fromqstr(printerCollatedED->text());
1525         rc.print_landscape_flag = fromqstr(printerLandscapeED->text());
1526         rc.print_to_file = internal_path(fromqstr(printerToFileED->text()));
1527         rc.print_extra_options = fromqstr(printerExtraED->text());
1528         rc.print_spool_printerprefix = fromqstr(printerSpoolPrefixED->text());
1529         rc.print_paper_dimension_flag = fromqstr(printerPaperSizeED->text());
1530 }
1531
1532
1533 void PrefPrinter::update(LyXRC const & rc)
1534 {
1535         printerAdaptCB->setChecked(rc.print_adapt_output);
1536         printerCommandED->setText(toqstr(rc.print_command));
1537         printerNameED->setText(toqstr(rc.printer));
1538
1539         printerPageRangeED->setText(toqstr(rc.print_pagerange_flag));
1540         printerCopiesED->setText(toqstr(rc.print_copies_flag));
1541         printerReverseED->setText(toqstr(rc.print_reverse_flag));
1542         printerToPrinterED->setText(toqstr(rc.print_to_printer));
1543         printerExtensionED->setText(toqstr(rc.print_file_extension));
1544         printerSpoolCommandED->setText(toqstr(rc.print_spool_command));
1545         printerPaperTypeED->setText(toqstr(rc.print_paper_flag));
1546         printerEvenED->setText(toqstr(rc.print_evenpage_flag));
1547         printerOddED->setText(toqstr(rc.print_oddpage_flag));
1548         printerCollatedED->setText(toqstr(rc.print_collcopies_flag));
1549         printerLandscapeED->setText(toqstr(rc.print_landscape_flag));
1550         printerToFileED->setText(toqstr(external_path(rc.print_to_file)));
1551         printerExtraED->setText(toqstr(rc.print_extra_options));
1552         printerSpoolPrefixED->setText(toqstr(rc.print_spool_printerprefix));
1553         printerPaperSizeED->setText(toqstr(rc.print_paper_dimension_flag));
1554 }
1555
1556
1557 /////////////////////////////////////////////////////////////////////
1558 //
1559 // PrefUserInterface
1560 //
1561 /////////////////////////////////////////////////////////////////////
1562
1563 PrefUserInterface::PrefUserInterface(GuiPrefsDialog * form, QWidget * parent)
1564         : PrefModule(_("User interface"), form, parent)
1565 {
1566         setupUi(this);
1567
1568         connect(autoSaveCB, SIGNAL(toggled(bool)),
1569                 autoSaveSB, SLOT(setEnabled(bool)));
1570         connect(autoSaveCB, SIGNAL(toggled(bool)),
1571                 TextLabel1, SLOT(setEnabled(bool)));
1572         connect(uiFilePB, SIGNAL(clicked()),
1573                 this, SLOT(select_ui()));
1574         connect(bindFilePB, SIGNAL(clicked()),
1575                 this, SLOT(select_bind()));
1576         connect(uiFileED, SIGNAL(textChanged(const QString &)),
1577                 this, SIGNAL(changed()));
1578         connect(bindFileED, SIGNAL(textChanged(const QString &)),
1579                 this, SIGNAL(changed()));
1580         connect(restoreCursorCB, SIGNAL(clicked()),
1581                 this, SIGNAL(changed()));
1582         connect(loadSessionCB, SIGNAL(clicked()),
1583                 this, SIGNAL(changed()));
1584         connect(loadWindowSizeCB, SIGNAL(clicked()),
1585                 this, SIGNAL(changed()));
1586         connect(loadWindowLocationCB, SIGNAL(clicked()),
1587                 this, SIGNAL(changed()));
1588         connect(windowWidthSB, SIGNAL(valueChanged(int)),
1589                 this, SIGNAL(changed()));
1590         connect(windowHeightSB, SIGNAL(valueChanged(int)),
1591                 this, SIGNAL(changed()));
1592         connect(cursorFollowsCB, SIGNAL(clicked()),
1593                 this, SIGNAL(changed()));
1594         connect(autoSaveSB, SIGNAL(valueChanged(int)),
1595                 this, SIGNAL(changed()));
1596         connect(autoSaveCB, SIGNAL(clicked()),
1597                 this, SIGNAL(changed()));
1598         connect(lastfilesSB, SIGNAL(valueChanged(int)),
1599                 this, SIGNAL(changed()));
1600         lastfilesSB->setMaximum(maxlastfiles);
1601 }
1602
1603
1604 void PrefUserInterface::apply(LyXRC & rc) const
1605 {
1606         rc.ui_file = internal_path(fromqstr(uiFileED->text()));
1607         rc.bind_file = internal_path(fromqstr(bindFileED->text()));
1608         rc.use_lastfilepos = restoreCursorCB->isChecked();
1609         rc.load_session = loadSessionCB->isChecked();
1610         if (loadWindowSizeCB->isChecked()) {
1611                 rc.geometry_width = 0;
1612                 rc.geometry_height = 0;
1613         } else {
1614                 rc.geometry_width = windowWidthSB->value();
1615                 rc.geometry_height = windowHeightSB->value();
1616         }
1617         rc.geometry_xysaved = loadWindowLocationCB->isChecked();
1618         rc.cursor_follows_scrollbar = cursorFollowsCB->isChecked();
1619         rc.autosave = autoSaveSB->value() * 60;
1620         rc.make_backup = autoSaveCB->isChecked();
1621         rc.num_lastfiles = lastfilesSB->value();
1622 }
1623
1624
1625 void PrefUserInterface::update(LyXRC const & rc)
1626 {
1627         uiFileED->setText(toqstr(external_path(rc.ui_file)));
1628         bindFileED->setText(toqstr(external_path(rc.bind_file)));
1629         restoreCursorCB->setChecked(rc.use_lastfilepos);
1630         loadSessionCB->setChecked(rc.load_session);
1631         bool loadWindowSize = rc.geometry_width == 0 && rc.geometry_height == 0;
1632         loadWindowSizeCB->setChecked(loadWindowSize);
1633         if (!loadWindowSize) {
1634                 windowWidthSB->setValue(rc.geometry_width);
1635                 windowHeightSB->setValue(rc.geometry_height);
1636         }
1637         loadWindowLocationCB->setChecked(rc.geometry_xysaved);
1638         cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
1639         // convert to minutes
1640         int mins(rc.autosave / 60);
1641         if (rc.autosave && !mins)
1642                 mins = 1;
1643         autoSaveSB->setValue(mins);
1644         autoSaveCB->setChecked(rc.make_backup);
1645         lastfilesSB->setValue(rc.num_lastfiles);
1646 }
1647
1648
1649 void PrefUserInterface::select_ui()
1650 {
1651         docstring const name =
1652                 from_utf8(internal_path(fromqstr(uiFileED->text())));
1653         docstring file = form_->controller().browseUI(name);
1654         if (!file.empty())
1655                 uiFileED->setText(toqstr(file));
1656 }
1657
1658
1659 void PrefUserInterface::select_bind()
1660 {
1661         docstring const name =
1662                 from_utf8(internal_path(fromqstr(bindFileED->text())));
1663         docstring file = form_->controller().browsebind(name);
1664         if (!file.empty())
1665                 bindFileED->setText(toqstr(file));
1666 }
1667
1668
1669 void PrefUserInterface::on_loadWindowSizeCB_toggled(bool loadwindowsize)
1670 {
1671         windowWidthLA->setDisabled(loadwindowsize);
1672         windowHeightLA->setDisabled(loadwindowsize);
1673         windowWidthSB->setDisabled(loadwindowsize);
1674         windowHeightSB->setDisabled(loadwindowsize);
1675 }
1676
1677
1678 PrefIdentity::PrefIdentity(QWidget * parent)
1679         : PrefModule(_("Identity"), 0, parent)
1680 {
1681         setupUi(this);
1682
1683         connect(nameED, SIGNAL(textChanged(const QString&)),
1684                 this, SIGNAL(changed()));
1685         connect(emailED, SIGNAL(textChanged(const QString&)),
1686                 this, SIGNAL(changed()));
1687 }
1688
1689
1690 void PrefIdentity::apply(LyXRC & rc) const
1691 {
1692         rc.user_name = fromqstr(nameED->text());
1693         rc.user_email = fromqstr(emailED->text());
1694 }
1695
1696
1697 void PrefIdentity::update(LyXRC const & rc)
1698 {
1699         nameED->setText(toqstr(rc.user_name));
1700         emailED->setText(toqstr(rc.user_email));
1701 }
1702
1703
1704
1705 /////////////////////////////////////////////////////////////////////
1706 //
1707 // GuiPrefsDialog
1708 //
1709 /////////////////////////////////////////////////////////////////////
1710
1711 GuiPrefsDialog::GuiPrefsDialog(LyXView & lv)
1712         : GuiDialog(lv, "prefs")
1713 {
1714         setupUi(this);
1715         setViewTitle(_("Preferences"));
1716         setController(new ControlPrefs(*this));
1717
1718         QDialog::setModal(false);
1719
1720         connect(savePB, SIGNAL(clicked()), this, SLOT(slotOK()));
1721         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
1722         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
1723         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
1724
1725         add(new PrefUserInterface(this));
1726         add(new PrefScreenFonts(this));
1727         add(new PrefColors(this));
1728         add(new PrefDisplay);
1729         add(new PrefKeyboard(this));
1730
1731         add(new PrefPaths(this));
1732
1733         add(new PrefIdentity);
1734
1735         add(new PrefLanguage);
1736         add(new PrefSpellchecker(this));
1737
1738         add(new PrefPrinter);
1739         add(new PrefDate);
1740         add(new PrefPlaintext);
1741         add(new PrefLatex(this));
1742
1743         PrefConverters * converters = new PrefConverters(this);
1744         PrefFileformats * formats = new PrefFileformats(this);
1745         connect(formats, SIGNAL(formatsChanged()),
1746                         converters, SLOT(updateGui()));
1747         add(converters);
1748         add(formats);
1749
1750         prefsPS->setCurrentPanel(_("User interface"));
1751 // FIXME: hack to work around resizing bug in Qt >= 4.2
1752 // bug verified with Qt 4.2.{0-3} (JSpitzm)
1753 #if QT_VERSION >= 0x040200
1754         prefsPS->updateGeometry();
1755 #endif
1756
1757         bc().setPolicy(ButtonPolicy::PreferencesPolicy);
1758         bc().setOK(savePB);
1759         bc().setApply(applyPB);
1760         bc().setCancel(closePB);
1761         bc().setRestore(restorePB);
1762 }
1763
1764
1765 ControlPrefs & GuiPrefsDialog::controller()
1766 {
1767         return static_cast<ControlPrefs &>(GuiDialog::controller());
1768 }
1769
1770
1771 void GuiPrefsDialog::add(PrefModule * module)
1772 {
1773         BOOST_ASSERT(module);
1774         prefsPS->addPanel(module, module->title());
1775         connect(module, SIGNAL(changed()), this, SLOT(change_adaptor()));
1776         modules_.push_back(module);
1777 }
1778
1779
1780 void GuiPrefsDialog::closeEvent(QCloseEvent * e)
1781 {
1782         slotClose();
1783         e->accept();
1784 }
1785
1786
1787 void GuiPrefsDialog::change_adaptor()
1788 {
1789         changed();
1790 }
1791
1792
1793 void GuiPrefsDialog::apply(LyXRC & rc) const
1794 {
1795         size_t end = modules_.size();
1796         for (size_t i = 0; i != end; ++i)
1797                 modules_[i]->apply(rc);
1798 }
1799
1800
1801 void GuiPrefsDialog::updateRc(LyXRC const & rc)
1802 {
1803         size_t const end = modules_.size();
1804         for (size_t i = 0; i != end; ++i)
1805                 modules_[i]->update(rc);
1806 }
1807
1808
1809 Converters & GuiPrefsDialog::converters()
1810 {
1811         return controller().converters();
1812 }
1813
1814
1815 Formats & GuiPrefsDialog::formats()
1816 {
1817         return controller().formats();
1818 }
1819
1820
1821 Movers & GuiPrefsDialog::movers()
1822 {
1823         return controller().movers();
1824 }
1825
1826
1827 void GuiPrefsDialog::applyView()
1828 {
1829         apply(controller().rc());
1830 }
1831
1832 void GuiPrefsDialog::updateContents()
1833 {
1834         updateRc(controller().rc());
1835 }
1836
1837 } // namespace frontend
1838 } // namespace lyx
1839
1840 #include "GuiPrefs_moc.cpp"