]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiDocument.cpp
Fix readability
[lyx.git] / src / frontends / qt / GuiDocument.cpp
index 1948bb6287eca30b03dcddf3be516f862c17b301..b16e8923bc8ef36a7d3f336f32e21f4889a6802a 100644 (file)
@@ -21,7 +21,6 @@
 #include "GuiIndices.h"
 #include "GuiView.h"
 #include "GuiSelectionManager.h"
-#include "LaTeXHighlighter.h"
 #include "Validator.h"
 
 #include "LayoutFile.h"
@@ -475,12 +474,12 @@ void ModuleSelectionManager::updateDelPB()
 /////////////////////////////////////////////////////////////////////
 
 PreambleModule::PreambleModule(QWidget * parent)
-       : UiWidget<Ui::PreambleUi>(parent), current_id_(nullptr)
+       : UiWidget<Ui::PreambleUi>(parent), current_id_(nullptr),
+         highlighter_(new LaTeXHighlighter(preambleTE->document(), true))
 {
        // This is not a memory leak. The object will be destroyed
        // with this.
        // @ is letter in the LyX user preamble
-       (void) new LaTeXHighlighter(preambleTE->document(), true);
        preambleTE->setFont(guiApp->typewriterSystemFont());
        preambleTE->setWordWrapMode(QTextOption::NoWrap);
        setFocusProxy(preambleTE);
@@ -507,14 +506,22 @@ PreambleModule::PreambleModule(QWidget * parent)
 bool PreambleModule::eventFilter(QObject * sender, QEvent * event)
 {
        if (sender == findLE) {
-               if (event->type()==QEvent::KeyPress) {
-               QKeyEvent * key = static_cast<QKeyEvent *>(event);
-               if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
-                   findText();
+               if (event->type() == QEvent::KeyPress) {
+                       QKeyEvent * key = static_cast<QKeyEvent *>(event);
+                       if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
+                               findText();
                                // Return true to filter out the event
                                return true;
-               }
-           }
+                       }
+               }
+       }
+       if (event->type() == QEvent::ApplicationPaletteChange) {
+               // mode switch: colors need to be updated
+               // and the highlighting redone
+               if (highlighter_) {
+                       highlighter_->setupColors();
+                       highlighter_->rehighlight();
+               }
        }
        return QWidget::eventFilter(sender, event);
 }
@@ -1265,7 +1272,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
        // margins
        marginsModule = new UiWidget<Ui::MarginsUi>(this);
-       connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
+       connect(marginsModule->marginCB, SIGNAL(clicked(bool)),
                this, SLOT(setCustomMargins(bool)));
        connect(marginsModule->marginCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
@@ -1775,8 +1782,11 @@ GuiDocument::GuiDocument(GuiView & lv)
                pdfSupportModule->subjectLE));
        pdfSupportModule->keywordsLE->setValidator(new NoNewLineValidator(
                pdfSupportModule->keywordsLE));
-       (void) new LaTeXHighlighter(pdfSupportModule->optionsTE->document(), true, true);
-       (void) new LaTeXHighlighter(pdfSupportModule->metadataTE->document(), true, true);
+
+       pdf_options_highlighter_ = new LaTeXHighlighter(
+                               pdfSupportModule->optionsTE->document(), true, true);
+       pdf_metadata_highlighter_ = new LaTeXHighlighter(
+                               pdfSupportModule->metadataTE->document(), true, true);
 
        for (int i = 0; backref_opts[i][0]; ++i)
                pdfSupportModule->backrefCO->addItem(qt_(backref_opts_gui[i]));
@@ -1833,6 +1843,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        docPS->addPanel(outputModule, N_("Output"));
        docPS->addPanel(preambleModule, N_("LaTeX Preamble"));
        docPS->setCurrentPanel("Document Class");
+
+       // Filter out (dark/light) mode changes
+       installEventFilter(this);
 }
 
 
@@ -2205,38 +2218,95 @@ void GuiDocument::setColSep()
 }
 
 
-void GuiDocument::setCustomMargins(bool custom)
-{
-       marginsModule->topL->setEnabled(!custom);
-       marginsModule->topLE->setEnabled(!custom);
-       marginsModule->topUnit->setEnabled(!custom);
-
-       marginsModule->bottomL->setEnabled(!custom);
-       marginsModule->bottomLE->setEnabled(!custom);
-       marginsModule->bottomUnit->setEnabled(!custom);
-
-       marginsModule->innerL->setEnabled(!custom);
-       marginsModule->innerLE->setEnabled(!custom);
-       marginsModule->innerUnit->setEnabled(!custom);
-
-       marginsModule->outerL->setEnabled(!custom);
-       marginsModule->outerLE->setEnabled(!custom);
-       marginsModule->outerUnit->setEnabled(!custom);
-
-       marginsModule->headheightL->setEnabled(!custom);
-       marginsModule->headheightLE->setEnabled(!custom);
-       marginsModule->headheightUnit->setEnabled(!custom);
-
-       marginsModule->headsepL->setEnabled(!custom);
-       marginsModule->headsepLE->setEnabled(!custom);
-       marginsModule->headsepUnit->setEnabled(!custom);
-
-       marginsModule->footskipL->setEnabled(!custom);
-       marginsModule->footskipLE->setEnabled(!custom);
-       marginsModule->footskipUnit->setEnabled(!custom);
-
-       bool const enableColSep = !custom &&
-                       textLayoutModule->twoColumnCB->checkState() == Qt::Checked;
+void GuiDocument::setCustomMargins(bool cb_checked)
+{
+       bool const custom_margins = !cb_checked;
+       if (custom_margins) {
+               Length::UNIT const default_unit = Length::defaultUnit();
+               // fill with chached values
+               lengthToWidgets(marginsModule->topLE,
+                               marginsModule->topUnit,
+                               tmp_topmargin_, default_unit);
+               lengthToWidgets(marginsModule->bottomLE,
+                               marginsModule->bottomUnit,
+                               tmp_bottommargin_, default_unit);
+               lengthToWidgets(marginsModule->innerLE,
+                               marginsModule->innerUnit,
+                               tmp_leftmargin_, default_unit);
+               lengthToWidgets(marginsModule->outerLE,
+                               marginsModule->outerUnit,
+                               tmp_rightmargin_, default_unit);
+               lengthToWidgets(marginsModule->headheightLE,
+                               marginsModule->headheightUnit,
+                               tmp_headheight_, default_unit);
+               lengthToWidgets(marginsModule->headsepLE,
+                               marginsModule->headsepUnit,
+                               tmp_headsep_, default_unit);
+               lengthToWidgets(marginsModule->footskipLE,
+                               marginsModule->footskipUnit,
+                               tmp_footskip_, default_unit);
+               lengthToWidgets(marginsModule->columnsepLE,
+                               marginsModule->columnsepUnit,
+                               tmp_columnsep_, default_unit);
+       } else { // default margins
+               // Cache current values
+               tmp_leftmargin_ = widgetsToLength(marginsModule->innerLE,
+                                                 marginsModule->innerUnit);
+               tmp_topmargin_ = widgetsToLength(marginsModule->topLE,
+                                                marginsModule->topUnit);
+               tmp_rightmargin_ = widgetsToLength(marginsModule->outerLE,
+                                                  marginsModule->outerUnit);
+               tmp_bottommargin_ = widgetsToLength(marginsModule->bottomLE,
+                                                   marginsModule->bottomUnit);
+               tmp_headheight_ = widgetsToLength(marginsModule->headheightLE,
+                                                 marginsModule->headheightUnit);
+               tmp_headsep_ = widgetsToLength(marginsModule->headsepLE,
+                                              marginsModule->headsepUnit);
+               tmp_footskip_ = widgetsToLength(marginsModule->footskipLE,
+                                               marginsModule->footskipUnit);
+               tmp_columnsep_ = widgetsToLength(marginsModule->columnsepLE,
+                                                marginsModule->columnsepUnit);
+               // clear widgets
+               marginsModule->topLE->clear();
+               marginsModule->bottomLE->clear();
+               marginsModule->innerLE->clear();
+               marginsModule->outerLE->clear();
+               marginsModule->headheightLE->clear();
+               marginsModule->headsepLE->clear();
+               marginsModule->footskipLE->clear();
+               marginsModule->columnsepLE->clear();
+       }
+
+       marginsModule->topL->setEnabled(custom_margins);
+       marginsModule->topLE->setEnabled(custom_margins);
+       marginsModule->topUnit->setEnabled(custom_margins);
+
+       marginsModule->bottomL->setEnabled(custom_margins);
+       marginsModule->bottomLE->setEnabled(custom_margins);
+       marginsModule->bottomUnit->setEnabled(custom_margins);
+
+       marginsModule->innerL->setEnabled(custom_margins);
+       marginsModule->innerLE->setEnabled(custom_margins);
+       marginsModule->innerUnit->setEnabled(custom_margins);
+
+       marginsModule->outerL->setEnabled(custom_margins);
+       marginsModule->outerLE->setEnabled(custom_margins);
+       marginsModule->outerUnit->setEnabled(custom_margins);
+
+       marginsModule->headheightL->setEnabled(custom_margins);
+       marginsModule->headheightLE->setEnabled(custom_margins);
+       marginsModule->headheightUnit->setEnabled(custom_margins);
+
+       marginsModule->headsepL->setEnabled(custom_margins);
+       marginsModule->headsepLE->setEnabled(custom_margins);
+       marginsModule->headsepUnit->setEnabled(custom_margins);
+
+       marginsModule->footskipL->setEnabled(custom_margins);
+       marginsModule->footskipLE->setEnabled(custom_margins);
+       marginsModule->footskipUnit->setEnabled(custom_margins);
+
+       bool const enableColSep = custom_margins &&
+                       textLayoutModule->twoColumnCB->isChecked();
        marginsModule->columnsepL->setEnabled(enableColSep);
        marginsModule->columnsepLE->setEnabled(enableColSep);
        marginsModule->columnsepUnit->setEnabled(enableColSep);
@@ -3976,14 +4046,16 @@ void GuiDocument::applyView()
 
        Ui::MarginsUi const * m = marginsModule;
 
-       bp_.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
-       bp_.topmargin = widgetsToLength(m->topLE, m->topUnit);
-       bp_.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
-       bp_.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
-       bp_.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
-       bp_.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
-       bp_.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
-       bp_.columnsep = widgetsToLength(m->columnsepLE, m->columnsepUnit);
+       if (bp_.use_geometry) {
+               bp_.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
+               bp_.topmargin = widgetsToLength(m->topLE, m->topUnit);
+               bp_.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
+               bp_.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
+               bp_.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
+               bp_.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
+               bp_.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
+               bp_.columnsep = widgetsToLength(m->columnsepLE, m->columnsepUnit);
+       }
 
        // branches
        branchesModule->apply(bp_);
@@ -4573,32 +4645,34 @@ void GuiDocument::paramsToDialog()
        // margins
        Ui::MarginsUi * m = marginsModule;
 
-       setMargins();
+       tmp_leftmargin_ = bp_.leftmargin;
+       tmp_topmargin_ = bp_.topmargin;
+       tmp_rightmargin_ = bp_.rightmargin;
+       tmp_bottommargin_ = bp_.bottommargin;
+       tmp_headheight_ = bp_.headheight;
+       tmp_headsep_ = bp_.headsep;
+       tmp_footskip_ = bp_.footskip;
+       tmp_columnsep_ = bp_.columnsep;
 
        lengthToWidgets(m->topLE, m->topUnit,
                bp_.topmargin, default_unit);
-
        lengthToWidgets(m->bottomLE, m->bottomUnit,
                bp_.bottommargin, default_unit);
-
        lengthToWidgets(m->innerLE, m->innerUnit,
                bp_.leftmargin, default_unit);
-
        lengthToWidgets(m->outerLE, m->outerUnit,
                bp_.rightmargin, default_unit);
-
        lengthToWidgets(m->headheightLE, m->headheightUnit,
                bp_.headheight, default_unit);
-
        lengthToWidgets(m->headsepLE, m->headsepUnit,
                bp_.headsep, default_unit);
-
        lengthToWidgets(m->footskipLE, m->footskipUnit,
                bp_.footskip, default_unit);
-
        lengthToWidgets(m->columnsepLE, m->columnsepUnit,
                bp_.columnsep, default_unit);
 
+       setMargins();
+
        // branches
        updateUnknownBranches();
        branchesModule->update(bp_);
@@ -5384,6 +5458,24 @@ void GuiDocument::setOutputSync(bool on)
 }
 
 
+bool GuiDocument::eventFilter(QObject * sender, QEvent * event)
+{
+       if (event->type() == QEvent::ApplicationPaletteChange) {
+               // mode switch: colors need to be updated
+               // and the highlighting redone
+               if (pdf_options_highlighter_) {
+                       pdf_options_highlighter_->setupColors();
+                       pdf_options_highlighter_->rehighlight();
+               }
+               if (pdf_metadata_highlighter_) {
+                       pdf_metadata_highlighter_->setupColors();
+                       pdf_metadata_highlighter_->rehighlight();
+               }
+       }
+       return QWidget::eventFilter(sender, event);
+}
+
+
 } // namespace frontend
 } // namespace lyx