Commit e67d67d0 authored by Liang Qi's avatar Liang Qi Committed by The Qt Project
Browse files

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: default avatarCaroline Chao <caroline.chao@digia.com>
Reviewed-by: default avatarGabriel de Dietrich <gabriel.dedietrich@digia.com>
parent e6ea417c
6.2 5.10 5.11 5.12 5.12.1 5.12.10 5.12.11 5.12.12 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.12.7 5.12.8 5.12.9 5.13 5.13.0 5.13.1 5.13.2 5.14 5.14.0 5.14.1 5.14.2 5.15 5.15.0 5.15.1 5.15.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.9.8 dev old/5.1 old/5.2 wip/calendar wip/tizen v5.15.0-alpha1 v5.14.1 v5.14.0 v5.14.0-rc2 v5.14.0-rc1 v5.14.0-beta3 v5.14.0-beta2 v5.14.0-beta1 v5.14.0-alpha1 v5.13.2 v5.13.1 v5.13.0 v5.13.0-rc3 v5.13.0-rc2 v5.13.0-rc1 v5.13.0-beta4 v5.13.0-beta3 v5.13.0-beta2 v5.13.0-beta1 v5.13.0-alpha1 v5.12.7 v5.12.6 v5.12.5 v5.12.4 v5.12.3 v5.12.2 v5.12.1 v5.12.0 v5.12.0-rc2 v5.12.0-rc1 v5.12.0-beta4 v5.12.0-beta3 v5.12.0-beta2 v5.12.0-beta1 v5.12.0-alpha1 v5.11.3 v5.11.2 v5.11.1 v5.11.0 v5.11.0-rc2 v5.11.0-rc1 v5.11.0-beta4 v5.11.0-beta3 v5.11.0-beta2 v5.11.0-beta1 v5.11.0-alpha1 v5.10.1 v5.10.0 v5.10.0-rc3 v5.10.0-rc2 v5.10.0-rc1 v5.10.0-beta4 v5.10.0-beta3 v5.10.0-beta2 v5.10.0-beta1 v5.10.0-alpha1 v5.9.9 v5.9.8 v5.9.7 v5.9.6 v5.9.5 v5.9.4 v5.9.3 v5.9.2 v5.9.1 v5.9.0 v5.9.0-rc2 v5.9.0-rc1 v5.9.0-beta4 v5.9.0-beta3 v5.9.0-beta2 v5.9.0-beta1 v5.9.0-alpha1 v5.8.0 v5.8.0-rc1 v5.8.0-beta1 v5.8.0-alpha1 v5.7.1 v5.7.0 v5.7.0-rc1 v5.7.0-beta1 v5.7.0-alpha1 v5.6.3 v5.6.2 v5.6.1 v5.6.1-1 v5.6.0 v5.6.0-rc1 v5.6.0-beta1 v5.6.0-alpha1 v5.5.1 v5.5.0 v5.5.0-rc1 v5.5.0-beta1 v5.5.0-alpha1 v5.4.2 v5.4.1 v5.4.0 v5.4.0-rc1 v5.4.0-beta1 v5.4.0-alpha1 v5.3.2 v5.3.1 v5.3.0 v5.3.0-rc1 v5.3.0-beta1 v5.3.0-alpha1 v5.2.1 v5.2.0 v5.2.0-rc1 v5.2.0-beta1 v5.2.0-alpha1 v5.1.1 v5.1.0 v5.1.0-rc2 v5.1.0-rc1
No related merge requests found
Showing with 166 additions and 50 deletions
......@@ -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
}
}
}
}
......@@ -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
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment