diff options
author | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2021-09-23 17:45:44 +0200 |
---|---|---|
committer | Oliver Eftevaag <oliver.eftevaag@qt.io> | 2021-09-29 23:16:33 +0200 |
commit | cba55a2f08b59a378e721077d03e871f291301fb (patch) | |
tree | 0d4e80d11a2bc85e70d9914abd5d42720ef47325 /examples/quickcontrols2/texteditor/documenthandler.cpp | |
parent | a55816d076bd06320c6e372005aa1b7509a3d7eb (diff) |
TextEditor: Clean up example and use quick dialogs
The text editor example is dependent on Qt.labs for its dialogs and
native menus.
The long term goal is to remove the Qt.labs dependency, and replace it
with components from QtQuick.
Currently we have a file and a font dialog, that should be used instead
of the Qt.labs dialogs.
I'm also replacing some of the properties in the DocumentHandler, since
I don't think it makes sense for the font to be limited to only a few
font weights and styles (which can be selected through the font dialog).
Selecting the most common font weight and style is still possible
using the bold and italic shortcuts and toolbar buttons.
Some more features includes:
* An additional toolbar button, which can be used to strikeout the
selected font. This required adding an additional glyph to
fontello.ttf.
* When coupled with the fix for QTBUG-96934, the font for the current
selection should automatically update the listviews in the font dialog
to select the correct family, style, sizes and effects for what is
selected in the document handler.
Fixes: QTBUG-95976
Pick-to: 6.2
Change-Id: I2907a2270e2a139b1ccb3fcb3ce3a788306a42e3
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'examples/quickcontrols2/texteditor/documenthandler.cpp')
-rw-r--r-- | examples/quickcontrols2/texteditor/documenthandler.cpp | 125 |
1 files changed, 24 insertions, 101 deletions
diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp index e82fbb8c55..d904e6f637 100644 --- a/examples/quickcontrols2/texteditor/documenthandler.cpp +++ b/examples/quickcontrols2/texteditor/documenthandler.cpp @@ -82,7 +82,7 @@ void DocumentHandler::setDocument(QQuickTextDocument *document) return; if (m_document) - m_document->textDocument()->disconnect(this); + disconnect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged); m_document = document; if (m_document) connect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged); @@ -132,23 +132,6 @@ void DocumentHandler::setSelectionEnd(int position) emit selectionEndChanged(); } -QString DocumentHandler::fontFamily() const -{ - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return QString(); - QTextCharFormat format = cursor.charFormat(); - return format.font().family(); -} - -void DocumentHandler::setFontFamily(const QString &family) -{ - QTextCharFormat format; - format.setFontFamily(family); - mergeFormatOnWordOrSelection(format); - emit fontFamilyChanged(); -} - QColor DocumentHandler::textColor() const { QTextCursor cursor = textCursor(); @@ -183,84 +166,6 @@ void DocumentHandler::setAlignment(Qt::Alignment alignment) emit alignmentChanged(); } -bool DocumentHandler::bold() const -{ - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return false; - return textCursor().charFormat().fontWeight() == QFont::Bold; -} - -void DocumentHandler::setBold(bool bold) -{ - QTextCharFormat format; - format.setFontWeight(bold ? QFont::Bold : QFont::Normal); - mergeFormatOnWordOrSelection(format); - emit boldChanged(); -} - -bool DocumentHandler::italic() const -{ - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return false; - return textCursor().charFormat().fontItalic(); -} - -void DocumentHandler::setItalic(bool italic) -{ - QTextCharFormat format; - format.setFontItalic(italic); - mergeFormatOnWordOrSelection(format); - emit italicChanged(); -} - -bool DocumentHandler::underline() const -{ - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return false; - return textCursor().charFormat().fontUnderline(); -} - -void DocumentHandler::setUnderline(bool underline) -{ - QTextCharFormat format; - format.setFontUnderline(underline); - mergeFormatOnWordOrSelection(format); - emit underlineChanged(); -} - -int DocumentHandler::fontSize() const -{ - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return 0; - QTextCharFormat format = cursor.charFormat(); - return format.font().pointSize(); -} - -void DocumentHandler::setFontSize(int size) -{ - if (size <= 0) - return; - - QTextCursor cursor = textCursor(); - if (cursor.isNull()) - return; - - if (!cursor.hasSelection()) - cursor.select(QTextCursor::WordUnderCursor); - - if (cursor.charFormat().property(QTextFormat::FontPointSize).toInt() == size) - return; - - QTextCharFormat format; - format.setFontPointSize(size); - mergeFormatOnWordOrSelection(format); - emit fontSizeChanged(); -} - QString DocumentHandler::fileName() const { const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl); @@ -348,13 +253,9 @@ void DocumentHandler::saveAs(const QUrl &fileUrl) void DocumentHandler::reset() { - emit fontFamilyChanged(); emit alignmentChanged(); - emit boldChanged(); - emit italicChanged(); - emit underlineChanged(); - emit fontSizeChanged(); emit textColorChanged(); + emit fontChanged(); } QTextCursor DocumentHandler::textCursor() const @@ -400,4 +301,26 @@ void DocumentHandler::setModified(bool m) m_document->textDocument()->setModified(m); } +QFont DocumentHandler::font() const +{ + QTextCursor cursor = textCursor(); + if (cursor.isNull()) + return m_document->textDocument()->defaultFont(); + QTextCharFormat format = cursor.charFormat(); + return format.font(); +} + +void DocumentHandler::setFont(const QFont & font){ + + QTextCursor cursor = textCursor(); + if (!cursor.isNull() && cursor.charFormat().font() == font) + return; + + QTextCharFormat format; + format.setFont(font); + mergeFormatOnWordOrSelection(format); + + emit fontChanged(); +} + #include "moc_documenthandler.cpp" |