diff options
Diffstat (limited to 'examples/quick/controls/text/src')
-rw-r--r-- | examples/quick/controls/text/src/documenthandler.cpp | 60 | ||||
-rw-r--r-- | examples/quick/controls/text/src/documenthandler.h | 16 | ||||
-rw-r--r-- | examples/quick/controls/text/src/main.cpp | 14 |
3 files changed, 59 insertions, 31 deletions
diff --git a/examples/quick/controls/text/src/documenthandler.cpp b/examples/quick/controls/text/src/documenthandler.cpp index 48b6d033..e0c9610d 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 @@ -166,13 +173,11 @@ void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format void DocumentHandler::setSelectionStart(int position) { m_selectionStart = position; -// emit selectionStartChanged(); } void DocumentHandler::setSelectionEnd(int position) { m_selectionEnd = position; -// emit selectionEndChanged(); } void DocumentHandler::setAlignment(Qt::Alignment a) @@ -188,10 +193,8 @@ void DocumentHandler::setAlignment(Qt::Alignment a) Qt::Alignment DocumentHandler::alignment() const { -// if (!m_doc || m_doc->isEmpty() || m_cursorPosition < 0) -// return Qt::AlignLeft; QTextCursor cursor = textCursor(); - if (cursor.isNull() || cursor.blockNumber() == 0) + if (cursor.isNull()) return Qt::AlignLeft; return textCursor().blockFormat().alignment(); } @@ -199,7 +202,7 @@ Qt::Alignment DocumentHandler::alignment() const bool DocumentHandler::bold() const { QTextCursor cursor = textCursor(); - if (cursor.isNull() || cursor.blockNumber() == 0) + if (cursor.isNull()) return false; return textCursor().charFormat().fontWeight() == QFont::Bold; } @@ -207,7 +210,7 @@ bool DocumentHandler::bold() const bool DocumentHandler::italic() const { QTextCursor cursor = textCursor(); - if (cursor.isNull() || cursor.blockNumber() == 0) + if (cursor.isNull()) return false; return textCursor().charFormat().fontItalic(); } @@ -215,7 +218,7 @@ bool DocumentHandler::italic() const bool DocumentHandler::underline() const { QTextCursor cursor = textCursor(); - if (cursor.isNull() || cursor.blockNumber() == 0) + if (cursor.isNull()) return false; return textCursor().charFormat().fontUnderline(); } @@ -264,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 c60bc1d5..a759c403 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; diff --git a/examples/quick/controls/text/src/main.cpp b/examples/quick/controls/text/src/main.cpp index 0fc064dc..8d2b8dd0 100644 --- a/examples/quick/controls/text/src/main.cpp +++ b/examples/quick/controls/text/src/main.cpp @@ -49,24 +49,14 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); - QQmlEngine engine; - qmlRegisterType<DocumentHandler>("org.qtproject.example", 1, 0, "DocumentHandler"); - - QQmlComponent component(&engine); - component.loadUrl(QUrl("qrc:/qml/main.qml")); - if ( !component.isReady() ) { - qWarning("%s", qPrintable(component.errorString())); - return -1; - } - QObject *topLevel = component.create(); + QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml")); + QObject *topLevel = engine.rootObjects().value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); if ( !window ) { qWarning("Error: Your root item has to be a Window."); return -1; } - - QObject::connect(&engine, SIGNAL(quit()), &app, SLOT(quit())); window->show(); return app.exec(); } |