From e67d67d0ad0c5c4549047f13be1999c65a9efb42 Mon Sep 17 00:00:00 2001
From: Liang Qi <liang.qi@digia.com>
Date: Tue, 4 Jun 2013 13:36:18 +0200
Subject: [PATCH] Examples: several fixes for text

* Cut/Copy/Paste works
* The initial status for b/i/u and alignment works
* Font size and family works
* Text color works

Task-number: QTBUG-31482
Change-Id: I299931dede9defbb1d3eb927f867e77d20ded5ae
Reviewed-by: Caroline Chao <caroline.chao@digia.com>
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
---
 examples/quick/controls/text/qml/main.qml     | 152 +++++++++++++-----
 .../controls/text/src/documenthandler.cpp     |  48 +++++-
 .../quick/controls/text/src/documenthandler.h |  16 +-
 3 files changed, 166 insertions(+), 50 deletions(-)

diff --git a/examples/quick/controls/text/qml/main.qml b/examples/quick/controls/text/qml/main.qml
index ec6bff7b0..d8f175ed1 100644
--- a/examples/quick/controls/text/qml/main.qml
+++ b/examples/quick/controls/text/qml/main.qml
@@ -85,32 +85,34 @@ ApplicationWindow {
     }
 
     Action {
-        id: cut
+        id: cutAction
         text: "Cut"
         shortcut: "ctrl+x"
         iconSource: "images/editcut.png"
         iconName: "edit-cut"
+        onTriggered: textArea.cut()
     }
 
     Action {
-        id: copy
+        id: copyAction
         text: "Copy"
         shortcut: "Ctrl+C"
         iconSource: "images/editcopy.png"
         iconName: "edit-copy"
-        onTriggered: console.log("Ctrl C pressed - in action...")
+        onTriggered: textArea.copy()
     }
 
     Action {
-        id: paste
+        id: pasteAction
         text: "Paste"
         shortcut: "ctrl+v"
         iconSource: "qrc:images/editpaste.png"
         iconName: "edit-paste"
+        onTriggered: textArea.paste()
     }
 
     Action {
-        id: alignLeft
+        id: alignLeftAction
         text: "&Left"
         iconSource: "images/textleft.png"
         iconName: "format-justify-left"
@@ -120,16 +122,16 @@ ApplicationWindow {
         checked: document.alignment == Qt.AlignLeft
     }
     Action {
-        id: alignCenter
+        id: alignCenterAction
         text: "C&enter"
         iconSource: "images/textcenter.png"
         iconName: "format-justify-center"
-        onTriggered: document.alignment = Qt.AlignCenter
+        onTriggered: document.alignment = Qt.AlignHCenter
         checkable: true
-        checked: document.alignment == Qt.AlignCenter
+        checked: document.alignment == Qt.AlignHCenter
     }
     Action {
-        id: alignRight
+        id: alignRightAction
         text: "&Right"
         iconSource: "images/textright.png"
         iconName: "format-justify-right"
@@ -138,7 +140,7 @@ ApplicationWindow {
         checked: document.alignment == Qt.AlignRight
     }
     Action {
-        id: alignJustify
+        id: alignJustifyAction
         text: "&Justify"
         iconSource: "images/textjustify.png"
         iconName: "format-justify-fill"
@@ -148,7 +150,7 @@ ApplicationWindow {
     }
 
     Action {
-        id: bold
+        id: boldAction
         text: "&Bold"
         iconSource: "images/textbold.png"
         iconName: "format-text-bold"
@@ -156,8 +158,9 @@ ApplicationWindow {
         checkable: true
         checked: document.bold
     }
+
     Action {
-        id: italic
+        id: italicAction
         text: "&Italic"
         iconSource: "images/textitalic.png"
         iconName: "format-text-italic"
@@ -166,7 +169,7 @@ ApplicationWindow {
         checked: document.italic
     }
     Action {
-        id: underline
+        id: underlineAction
         text: "&Underline"
         iconSource: "images/textunder.png"
         iconName: "format-text-underline"
@@ -176,41 +179,55 @@ ApplicationWindow {
     }
 
     FileDialog {
-        id: file
+        id: fileDialog
         nameFilters: ["Text files (*.txt)", "HTML files (*.html)"]
         onAccepted: document.fileUrl = fileUrl
     }
 
+    ColorDialog {
+        id: colorDialog
+        color: "black"
+        onAccepted: document.textColor = color
+    }
+
     Action {
-        id: fileOpen
+        id: fileOpenAction
         iconSource: "images/fileopen.png"
         iconName: "document-open"
         text: "Open"
-        onTriggered: file.open()
+        onTriggered: fileDialog.open()
     }
 
     menuBar: MenuBar {
         Menu {
             title: "&File"
-            MenuItem { action: fileOpen }
+            MenuItem { action: fileOpenAction }
             MenuItem { text: "Quit"; onTriggered: Qt.quit() }
         }
         Menu {
             title: "&Edit"
-            MenuItem { action: copy }
-            MenuItem { action: cut }
-            MenuItem { action: paste }
+            MenuItem { action: copyAction }
+            MenuItem { action: cutAction }
+            MenuItem { action: pasteAction }
         }
         Menu {
             title: "F&ormat"
-            MenuItem { action: bold }
-            MenuItem { action: italic }
-            MenuItem { action: underline }
+            MenuItem { action: boldAction }
+            MenuItem { action: italicAction }
+            MenuItem { action: underlineAction }
+            MenuSeparator {}
+            MenuItem { action: alignLeftAction }
+            MenuItem { action: alignCenterAction }
+            MenuItem { action: alignRightAction }
+            MenuItem { action: alignJustifyAction }
             MenuSeparator {}
-            MenuItem { action: alignLeft }
-            MenuItem { action: alignCenter }
-            MenuItem { action: alignRight }
-            MenuItem { action: alignJustify }
+            MenuItem {
+                text: "&Color ..."
+                onTriggered: {
+                    colorDialog.color = document.textColor
+                    colorDialog.open()
+                }
+            }
         }
         Menu {
             title: "&Help"
@@ -224,26 +241,71 @@ ApplicationWindow {
         RowLayout {
             anchors.fill: parent
             spacing: 0
-            ToolButton { action: fileOpen }
+            ToolButton { action: fileOpenAction }
 
             ToolBarSeparator {}
 
-            ToolButton { action: copy }
-            ToolButton { action: cut }
-            ToolButton { action: paste }
+            ToolButton { action: copyAction }
+            ToolButton { action: cutAction }
+            ToolButton { action: pasteAction }
 
             ToolBarSeparator {}
 
-            ToolButton { action: bold }
-            ToolButton { action: italic }
-            ToolButton { action: underline }
+            ToolButton { action: boldAction }
+            ToolButton { action: italicAction }
+            ToolButton { action: underlineAction }
 
             ToolBarSeparator {}
 
-            ToolButton { action: alignLeft }
-            ToolButton { action: alignCenter }
-            ToolButton { action: alignRight }
-            ToolButton { action: alignJustify }
+            ToolButton { action: alignLeftAction }
+            ToolButton { action: alignCenterAction }
+            ToolButton { action: alignRightAction }
+            ToolButton { action: alignJustifyAction }
+
+            ToolBarSeparator {}
+
+            ToolButton {
+                id: colorButton
+                property var color : document.textColor
+                Rectangle {
+                    id: colorRect
+                    anchors.fill: parent
+                    anchors.margins: 8
+                    color: Qt.darker(document.textColor, colorButton.pressed ? 1.4 : 1)
+                    border.width: 1
+                    border.color: Qt.darker(colorRect.color, 2)
+                }
+                onClicked: {
+                    colorDialog.color = document.textColor
+                    colorDialog.open()
+                }
+            }
+            Item { Layout.fillWidth: true }
+        }
+    }
+
+    ToolBar {
+        id: secondaryToolBar
+        width: parent.width
+
+        RowLayout {
+            anchors.fill: parent
+            ComboBox {
+                id: fontFamilyComboBox
+                implicitWidth: 150
+                model: Qt.fontFamilies()
+                property bool special : false
+                onCurrentTextChanged: {
+                    if (special == false || currentIndex != 0)
+                        document.fontFamily = currentText
+                }
+            }
+            SpinBox {
+                id: fontSizeSpinBox
+                implicitWidth: 50
+                value: 0
+                onValueChanged: document.fontSize = value
+            }
             Item { Layout.fillWidth: true }
         }
     }
@@ -253,7 +315,7 @@ ApplicationWindow {
         id: textArea
         frameVisible: false
         width: parent.width
-        anchors.top: parent.top
+        anchors.top: secondaryToolBar.bottom
         anchors.bottom: parent.bottom
         text: document.text
         textFormat: Qt.RichText
@@ -266,5 +328,17 @@ ApplicationWindow {
         cursorPosition: textArea.cursorPosition
         selectionStart: textArea.selectionStart
         selectionEnd: textArea.selectionEnd
+        Component.onCompleted: document.fileUrl = "qrc:/example.html"
+        onFontSizeChanged: fontSizeSpinBox.value = document.fontSize
+        onFontFamilyChanged: {
+            var index = Qt.fontFamilies().indexOf(document.fontFamily)
+            if (index == -1) {
+                fontFamilyComboBox.currentIndex = 0
+                fontFamilyComboBox.special = true
+            } else {
+                fontFamilyComboBox.currentIndex = index
+                fontFamilyComboBox.special = false
+            }
+        }
     }
 }
diff --git a/examples/quick/controls/text/src/documenthandler.cpp b/examples/quick/controls/text/src/documenthandler.cpp
index 81eb744b6..e0c9610d0 100644
--- a/examples/quick/controls/text/src/documenthandler.cpp
+++ b/examples/quick/controls/text/src/documenthandler.cpp
@@ -52,7 +52,6 @@ DocumentHandler::DocumentHandler()
     , m_selectionStart(0)
     , m_selectionEnd(0)
 {
-    setFileUrl(QUrl("qrc:/example.html"));
 }
 
 void DocumentHandler::setTarget(QQuickItem *target)
@@ -91,6 +90,8 @@ void DocumentHandler::setFileUrl(const QUrl &arg)
 
                 emit textChanged();
                 emit documentTitleChanged();
+
+                reset();
             }
         }
         emit fileUrlChanged();
@@ -135,12 +136,18 @@ void DocumentHandler::setCursorPosition(int position)
 
     m_cursorPosition = position;
 
-    emit currentFontChanged();
+    reset();
+}
+
+void DocumentHandler::reset()
+{
+    emit fontFamilyChanged();
     emit alignmentChanged();
     emit boldChanged();
     emit italicChanged();
     emit underlineChanged();
     emit fontSizeChanged();
+    emit textColorChanged();
 }
 
 QTextCursor DocumentHandler::textCursor() const
@@ -260,13 +267,44 @@ void DocumentHandler::setFontSize(int arg)
     emit fontSizeChanged();
 }
 
-QFont DocumentHandler::currentFont() const
+QColor DocumentHandler::textColor() const
+{
+    QTextCursor cursor = textCursor();
+    if (cursor.isNull())
+        return QColor(Qt::black);
+    QTextCharFormat format = cursor.charFormat();
+    return format.foreground().color();
+}
+
+void DocumentHandler::setTextColor(const QColor &c)
+{
+    QTextCursor cursor = textCursor();
+    if (cursor.isNull())
+        return;
+    QTextCharFormat format;
+    format.setForeground(QBrush(c));
+    mergeFormatOnWordOrSelection(format);
+    emit textColorChanged();
+}
+
+QString DocumentHandler::fontFamily() const
 {
     QTextCursor cursor = textCursor();
     if (cursor.isNull())
-        return QFont();
+        return QString();
     QTextCharFormat format = cursor.charFormat();
-    return format.font();
+    return format.font().family();
+}
+
+void DocumentHandler::setFontFamily(const QString &arg)
+{
+    QTextCursor cursor = textCursor();
+    if (cursor.isNull())
+        return;
+    QTextCharFormat format;
+    format.setFontFamily(arg);
+    mergeFormatOnWordOrSelection(format);
+    emit fontFamilyChanged();
 }
 
 QStringList DocumentHandler::defaultFontSizes() const
diff --git a/examples/quick/controls/text/src/documenthandler.h b/examples/quick/controls/text/src/documenthandler.h
index c60bc1d51..a759c4033 100644
--- a/examples/quick/controls/text/src/documenthandler.h
+++ b/examples/quick/controls/text/src/documenthandler.h
@@ -63,7 +63,8 @@ class DocumentHandler : public QObject
     Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
     Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
 
-    Q_PROPERTY(QFont currentFont READ currentFont NOTIFY currentFontChanged)
+    Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
+    Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
 
     Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
@@ -93,7 +94,9 @@ public:
     int selectionStart() const { return m_selectionStart; }
     int selectionEnd() const { return m_selectionEnd; }
 
-    QFont currentFont() const;
+    QString fontFamily() const;
+
+    QColor textColor() const;
 
     Qt::Alignment alignment() const;
     void setAlignment(Qt::Alignment a);
@@ -114,6 +117,8 @@ public Q_SLOTS:
     void setItalic(bool arg);
     void setUnderline(bool arg);
     void setFontSize(int arg);
+    void setTextColor(const QColor &arg);
+    void setFontFamily(const QString &arg);
 
     void setFileUrl(const QUrl &arg);
     void setText(const QString &arg);
@@ -126,7 +131,8 @@ Q_SIGNALS:
     void selectionStartChanged();
     void selectionEndChanged();
 
-    void currentFontChanged();
+    void fontFamilyChanged();
+    void textColorChanged();
     void alignmentChanged();
 
     void boldChanged();
@@ -142,6 +148,7 @@ Q_SIGNALS:
     void documentTitleChanged();
 
 private:
+    void reset();
     QTextCursor textCursor() const;
     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
 
@@ -153,9 +160,6 @@ private:
     int m_selectionEnd;
 
     QFont m_font;
-    bool m_bold;
-    bool m_italic;
-    bool m_underline;
     int m_fontSize;
     QUrl m_fileUrl;
     QString m_text;
-- 
GitLab