Add font size storage/unified handling in Linguists editors
authorErik Larsson <erik@ortogonal.com>
Mon, 16 Dec 2013 12:16:40 +0000 (13:16 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 23 Jan 2014 09:59:56 +0000 (10:59 +0100)
Add a unified font size handling in Linguists editors by assigning
Ctrl-+/Ctrl--/Ctrl-Wheel to inc/dec font size of ALL editors. The
current font size is also stored in the configuration and set at next
startup. This is done to make it simpler to use for translators.
If the configuration does not have a font size set it will set the
default system size.

Change-Id: I941f07807653360f8518938380f2c6a2d6f08b63
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
src/linguist/linguist/mainwindow.cpp
src/linguist/linguist/messageeditor.cpp
src/linguist/linguist/messageeditor.h

index f2897fb..8717bfd 100644 (file)
@@ -2577,6 +2577,9 @@ void MainWindow::readConfig()
     m_ui.actionVisualizeWhitespace->setChecked(
         config.value(settingPath("Options/VisualizeWhitespace"), true).toBool());
 
+    m_messageEditor->setFontSize(
+                config.value(settingPath("Options/EditorFontsize"), font().pointSize()).toReal());
+
     recentFiles().readConfig();
 
     int size = config.beginReadArray(settingPath("OpenedPhraseBooks"));
@@ -2608,6 +2611,8 @@ void MainWindow::writeConfig()
         saveState());
     recentFiles().writeConfig();
 
+    config.setValue(settingPath("Options/EditorFontsize"), m_messageEditor->fontSize());
+
     config.beginWriteArray(settingPath("OpenedPhraseBooks"),
         m_phraseBooks.size());
     for (int i = 0; i < m_phraseBooks.size(); ++i) {
@@ -2711,11 +2716,26 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
         e->acceptProposedAction();
         return true;
     } else if (event->type() == QEvent::KeyPress) {
-        if (static_cast<QKeyEvent *>(event)->key() == Qt::Key_Escape) {
+        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
+        if (ke->key() == Qt::Key_Escape) {
             if (object == m_messageEditor)
                 m_messageView->setFocus();
             else if (object == m_messagesDock)
                 m_contextView->setFocus();
+        } else if ((ke->key() == Qt::Key_Plus || ke->key() == Qt::Key_Equal)
+                   && (ke->modifiers() & Qt::ControlModifier)) {
+            m_messageEditor->increaseFontSize();
+        } else if (ke->key() == Qt::Key_Minus
+                   && (ke->modifiers() & Qt::ControlModifier)) {
+            m_messageEditor->decreaseFontSize();
+        }
+    } else if (event->type() == QEvent::Wheel) {
+        QWheelEvent *we = static_cast<QWheelEvent *>(event);
+        if (we->modifiers() & Qt::ControlModifier) {
+            if (we->delta() > 0)
+                m_messageEditor->increaseFontSize();
+            else
+                m_messageEditor->decreaseFontSize();
         }
     }
     return false;
index 4ca5f3c..8df290a 100644 (file)
@@ -91,6 +91,7 @@ MessageEditor::MessageEditor(MultiDataModel *dataModel, QMainWindow *parent)
       m_currentModel(-1),
       m_currentNumerus(-1),
       m_lengthVariants(false),
+      m_fontSize(font().pointSize()),
       m_undoAvail(false),
       m_redoAvail(false),
       m_cutAvail(false),
@@ -200,7 +201,7 @@ void MessageEditor::messageModelAppended()
     m_editors.append(MessageEditorData());
     MessageEditorData &ed = m_editors.last();
     ed.pluralEditMode = false;
-    ed.fontSize = font().pointSize();
+    ed.fontSize = m_fontSize;
     ed.container = new QWidget;
     if (model > 0) {
         ed.container->setPalette(paletteForModel(model));
@@ -292,12 +293,14 @@ void MessageEditor::addPluralForm(int model, const QString &label, bool writable
 
 void MessageEditor::editorCreated(QTextEdit *te)
 {
+    QFont font;
+    font.setPointSize(static_cast<int>(m_fontSize));
+
     FormMultiWidget *snd = static_cast<FormMultiWidget *>(sender());
     for (int model = 0; ; ++model) {
         MessageEditorData med = m_editors.at(model);
+        med.transCommentText->getEditor()->setFont(font);
         if (med.transTexts.contains(snd)) {
-            QFont font;
-            font.setPointSize(static_cast<int>(med.fontSize));
             te->setFont(font);
 
             te->installEventFilter(this);
@@ -504,33 +507,6 @@ MessageEditorData *MessageEditor::modelForWidget(const QObject *o)
     return 0;
 }
 
-static bool applyFont(MessageEditorData *med)
-{
-    QFont font;
-    font.setPointSize(static_cast<int>(med->fontSize));
-    for (int i = 0; i < med->transTexts.count(); ++i)
-        foreach (QTextEdit *te, med->transTexts[i]->getEditors())
-            te->setFont(font);
-    med->transCommentText->getEditor()->setFont(font);
-    return true;
-}
-
-static bool incFont(MessageEditorData *med)
-{
-    if (!med || med->fontSize >= 32)
-        return true;
-    med->fontSize *= 1.2;
-    return applyFont(med);
-}
-
-static bool decFont(MessageEditorData *med)
-{
-    if (!med || med->fontSize <= 8)
-        return true;
-    med->fontSize /= 1.2;
-    return applyFont(med);
-}
-
 bool MessageEditor::eventFilter(QObject *o, QEvent *e)
 {
     // handle copying from the source
@@ -555,25 +531,12 @@ bool MessageEditor::eventFilter(QObject *o, QEvent *e)
             }
         }
     } else if (e->type() == QEvent::KeyPress) {
+        // Ctrl-Tab is still passed through to the textedit and causes a tab to be inserted.
         QKeyEvent *ke = static_cast<QKeyEvent *>(e);
-        if (ke->modifiers() & Qt::ControlModifier) {
-            if (ke->key() == Qt::Key_Plus || ke->key() == Qt::Key_Equal)
-                return incFont(modelForWidget(o));
-            if (ke->key() == Qt::Key_Minus)
-                return decFont(modelForWidget(o));
-        } else {
-            // Ctrl-Tab is still passed through to the textedit and causes a tab to be inserted.
-            if (ke->key() == Qt::Key_Tab) {
-                focusNextChild();
-                return true;
-            }
-        }
-    } else if (e->type() == QEvent::Wheel) {
-        QWheelEvent *we = static_cast<QWheelEvent *>(e);
-        if (we->modifiers() & Qt::ControlModifier) {
-            if (we->delta() > 0)
-                return incFont(modelForWidget(o));
-            return decFont(modelForWidget(o));
+        if (ke->key() == Qt::Key_Tab &&
+            !(ke->modifiers() & Qt::ControlModifier)) {
+            focusNextChild();
+            return true;
         }
     } else if (e->type() == QEvent::FocusIn) {
         QWidget *widget = static_cast<QWidget *>(o);
@@ -940,4 +903,51 @@ void MessageEditor::setVisualizeWhitespace(bool value)
     }
 }
 
+void MessageEditor::setFontSize(const float fontSize)
+{
+    if (m_fontSize != fontSize) {
+        m_fontSize = fontSize;
+        applyFontSize();
+    }
+}
+
+float MessageEditor::fontSize()
+{
+    return m_fontSize;
+}
+
+void MessageEditor::applyFontSize()
+{
+    QFont font;
+    font.setPointSize(static_cast<int>(m_fontSize));
+
+    m_source->getEditor()->setFont(font);
+    m_pluralSource->getEditor()->setFont(font);
+    m_commentText->getEditor()->setFont(font);
+
+    foreach (MessageEditorData med, m_editors) {
+        for (int i = 0; i < med.transTexts.count(); ++i)
+            foreach (QTextEdit *te, med.transTexts[i]->getEditors())
+                te->setFont(font);
+        med.transCommentText->getEditor()->setFont(font);
+    }
+}
+
+void MessageEditor::increaseFontSize()
+{
+    if (m_fontSize >= 32)
+        return;
+
+    m_fontSize *= 1.2;
+    applyFontSize();
+}
+
+void MessageEditor::decreaseFontSize()
+{
+    if (m_fontSize > 8) {
+        m_fontSize /= 1.2;
+        applyFontSize();
+    }
+}
+
 QT_END_NAMESPACE
index 8af58c4..e966d49 100644 (file)
@@ -88,6 +88,10 @@ public:
     void setUnfinishedEditorFocus();
     bool focusNextUnfinished();
     void setVisualizeWhitespace(bool value);
+    void setFontSize(const float fontSize);
+    float fontSize();
+    void increaseFontSize();
+    void decreaseFontSize();
 
 signals:
     void translationChanged(const QStringList &translations);
@@ -158,6 +162,7 @@ private:
     void addPluralForm(int model, const QString &label, bool writable);
     void fixTabOrder();
     QPalette paletteForModel(int model) const;
+    void applyFontSize();
 
     MultiDataModel *m_dataModel;
 
@@ -166,6 +171,7 @@ private:
     int m_currentNumerus;
 
     bool m_lengthVariants;
+    float m_fontSize;
 
     bool m_undoAvail;
     bool m_redoAvail;