diff options
Diffstat (limited to 'examples')
58 files changed, 1267 insertions, 162 deletions
diff --git a/examples/quickcontrols2/chattutorial/chapter1-settingup/main.qml b/examples/quickcontrols2/chattutorial/chapter1-settingup/main.qml index ff65c53c..4fc97a49 100644 --- a/examples/quickcontrols2/chattutorial/chapter1-settingup/main.qml +++ b/examples/quickcontrols2/chattutorial/chapter1-settingup/main.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ApplicationWindow { width: 540 diff --git a/examples/quickcontrols2/chattutorial/chapter2-lists/main.qml b/examples/quickcontrols2/chattutorial/chapter2-lists/main.qml index a8d9f478..3a1d4cc9 100644 --- a/examples/quickcontrols2/chattutorial/chapter2-lists/main.qml +++ b/examples/quickcontrols2/chattutorial/chapter2-lists/main.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ApplicationWindow { width: 540 diff --git a/examples/quickcontrols2/chattutorial/chapter3-navigation/ContactPage.qml b/examples/quickcontrols2/chattutorial/chapter3-navigation/ContactPage.qml index 8beb689f..e45e5b61 100644 --- a/examples/quickcontrols2/chattutorial/chapter3-navigation/ContactPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter3-navigation/ContactPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Page { id: root diff --git a/examples/quickcontrols2/chattutorial/chapter3-navigation/ConversationPage.qml b/examples/quickcontrols2/chattutorial/chapter3-navigation/ConversationPage.qml index 9590669a..0e0e07fb 100644 --- a/examples/quickcontrols2/chattutorial/chapter3-navigation/ConversationPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter3-navigation/ConversationPage.qml @@ -40,7 +40,7 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Page { id: root diff --git a/examples/quickcontrols2/chattutorial/chapter3-navigation/main.qml b/examples/quickcontrols2/chattutorial/chapter3-navigation/main.qml index 5bc9e95d..25d50fb7 100644 --- a/examples/quickcontrols2/chattutorial/chapter3-navigation/main.qml +++ b/examples/quickcontrols2/chattutorial/chapter3-navigation/main.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ApplicationWindow { id: window diff --git a/examples/quickcontrols2/chattutorial/chapter4-models/ContactPage.qml b/examples/quickcontrols2/chattutorial/chapter4-models/ContactPage.qml index a6491495..4a914b32 100644 --- a/examples/quickcontrols2/chattutorial/chapter4-models/ContactPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter4-models/ContactPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 import io.qt.examples.chattutorial 1.0 diff --git a/examples/quickcontrols2/chattutorial/chapter4-models/ConversationPage.qml b/examples/quickcontrols2/chattutorial/chapter4-models/ConversationPage.qml index 5c91a206..183d5a32 100644 --- a/examples/quickcontrols2/chattutorial/chapter4-models/ConversationPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter4-models/ConversationPage.qml @@ -40,7 +40,7 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 import io.qt.examples.chattutorial 1.0 diff --git a/examples/quickcontrols2/chattutorial/chapter4-models/main.qml b/examples/quickcontrols2/chattutorial/chapter4-models/main.qml index 5bc9e95d..25d50fb7 100644 --- a/examples/quickcontrols2/chattutorial/chapter4-models/main.qml +++ b/examples/quickcontrols2/chattutorial/chapter4-models/main.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ApplicationWindow { id: window diff --git a/examples/quickcontrols2/chattutorial/chapter5-styling/+material/ChatToolBar.qml b/examples/quickcontrols2/chattutorial/chapter5-styling/+material/ChatToolBar.qml index b0f42969..0557de7f 100644 --- a/examples/quickcontrols2/chattutorial/chapter5-styling/+material/ChatToolBar.qml +++ b/examples/quickcontrols2/chattutorial/chapter5-styling/+material/ChatToolBar.qml @@ -38,8 +38,8 @@ ** ****************************************************************************/ -import QtQuick.Controls 2.0 -import QtQuick.Controls.Material 2.0 +import QtQuick.Controls 2.1 +import QtQuick.Controls.Material 2.1 ToolBar { Material.theme: Material.Dark diff --git a/examples/quickcontrols2/chattutorial/chapter5-styling/ChatToolBar.qml b/examples/quickcontrols2/chattutorial/chapter5-styling/ChatToolBar.qml index e400e029..072ae241 100644 --- a/examples/quickcontrols2/chattutorial/chapter5-styling/ChatToolBar.qml +++ b/examples/quickcontrols2/chattutorial/chapter5-styling/ChatToolBar.qml @@ -38,7 +38,7 @@ ** ****************************************************************************/ -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ToolBar { } diff --git a/examples/quickcontrols2/chattutorial/chapter5-styling/ContactPage.qml b/examples/quickcontrols2/chattutorial/chapter5-styling/ContactPage.qml index b94ef0b3..9afc2d8c 100644 --- a/examples/quickcontrols2/chattutorial/chapter5-styling/ContactPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter5-styling/ContactPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 import io.qt.examples.chattutorial 1.0 diff --git a/examples/quickcontrols2/chattutorial/chapter5-styling/ConversationPage.qml b/examples/quickcontrols2/chattutorial/chapter5-styling/ConversationPage.qml index afe8b572..a217bc0a 100644 --- a/examples/quickcontrols2/chattutorial/chapter5-styling/ConversationPage.qml +++ b/examples/quickcontrols2/chattutorial/chapter5-styling/ConversationPage.qml @@ -40,7 +40,7 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 import io.qt.examples.chattutorial 1.0 diff --git a/examples/quickcontrols2/chattutorial/chapter5-styling/main.qml b/examples/quickcontrols2/chattutorial/chapter5-styling/main.qml index 5bc9e95d..25d50fb7 100644 --- a/examples/quickcontrols2/chattutorial/chapter5-styling/main.qml +++ b/examples/quickcontrols2/chattutorial/chapter5-styling/main.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 ApplicationWindow { id: window diff --git a/examples/quickcontrols2/chattutorial/doc/src/qtquickcontrols2-chattutorial.qdoc b/examples/quickcontrols2/chattutorial/doc/src/qtquickcontrols2-chattutorial.qdoc index fe339133..2450d495 100644 --- a/examples/quickcontrols2/chattutorial/doc/src/qtquickcontrols2-chattutorial.qdoc +++ b/examples/quickcontrols2/chattutorial/doc/src/qtquickcontrols2-chattutorial.qdoc @@ -82,7 +82,7 @@ Once we've set up things in C++, we can move on to the user interface in QML. \quotefromfile chattutorial/chapter1-settingup/main.qml \skipto import -\printuntil import QtQuick.Controls 2.0 +\printuntil import QtQuick.Controls 2.1 First, we import the \l {Qt Quick} module. This gives us access to graphical primitives such as \l Item, \l Rectangle, \l Text, and so @@ -727,7 +727,7 @@ their text color to something lighter. The simplest way of doing so is to import the Material style directly and use the Material attached property: \code - import QtQuick.Controls.Material 2.0 + import QtQuick.Controls.Material 2.1 // ... diff --git a/examples/quickcontrols2/gallery/gallery.pro b/examples/quickcontrols2/gallery/gallery.pro index cb0736fd..fbfc3df8 100644 --- a/examples/quickcontrols2/gallery/gallery.pro +++ b/examples/quickcontrols2/gallery/gallery.pro @@ -5,12 +5,11 @@ QT += quick quickcontrols2 SOURCES += \ gallery.cpp -OTHER_FILES += \ - gallery.qml \ - pages/*.qml - RESOURCES += \ - gallery.qrc + gallery.qml \ + qtquickcontrols2.conf \ + $$files(images/*.png) \ + $$files(pages/*.qml) target.path = $$[QT_INSTALL_EXAMPLES]/quickcontrols2/gallery INSTALLS += target diff --git a/examples/quickcontrols2/gallery/gallery.qml b/examples/quickcontrols2/gallery/gallery.qml index cc458087..753a8c00 100644 --- a/examples/quickcontrols2/gallery/gallery.qml +++ b/examples/quickcontrols2/gallery/gallery.qml @@ -40,9 +40,9 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 -import QtQuick.Controls 2.0 -import QtQuick.Controls.Material 2.0 -import QtQuick.Controls.Universal 2.0 +import QtQuick.Controls 2.1 +import QtQuick.Controls.Material 2.1 +import QtQuick.Controls.Universal 2.1 import Qt.labs.settings 1.0 ApplicationWindow { @@ -100,7 +100,7 @@ ApplicationWindow { MenuItem { text: "Settings" - onTriggered: settingsPopup.open() + onTriggered: settingsDialog.open() } MenuItem { text: "About" @@ -207,15 +207,25 @@ ApplicationWindow { } } - Popup { - id: settingsPopup - x: (window.width - width) / 2 - y: window.height / 6 - width: Math.min(window.width, window.height) / 3 * 2 + Dialog { + id: settingsDialog + x: Math.round((window.width - width) / 2) + y: Math.round(window.height / 6) + width: Math.round(Math.min(window.width, window.height) / 3 * 2) height: settingsColumn.implicitHeight + topPadding + bottomPadding modal: true focus: true + standardButtons: Dialog.Ok | Dialog.Cancel + onAccepted: { + settings.style = styleBox.displayText + settingsDialog.close() + } + onRejected: { + styleBox.currentIndex = styleBox.styleIndex + settingsDialog.close() + } + contentItem: ColumnLayout { id: settingsColumn spacing: 20 @@ -254,45 +264,10 @@ ApplicationWindow { Layout.fillWidth: true Layout.fillHeight: true } - - RowLayout { - spacing: 10 - - Button { - id: okButton - text: "Ok" - onClicked: { - settings.style = styleBox.displayText - settingsPopup.close() - } - - Material.foreground: Material.primary - Material.background: "transparent" - Material.elevation: 0 - - Layout.preferredWidth: 0 - Layout.fillWidth: true - } - - Button { - id: cancelButton - text: "Cancel" - onClicked: { - styleBox.currentIndex = styleBox.styleIndex - settingsPopup.close() - } - - Material.background: "transparent" - Material.elevation: 0 - - Layout.preferredWidth: 0 - Layout.fillWidth: true - } - } } } - Popup { + Dialog { id: aboutDialog modal: true focus: true diff --git a/examples/quickcontrols2/gallery/gallery.qrc b/examples/quickcontrols2/gallery/gallery.qrc deleted file mode 100644 index 01c61324..00000000 --- a/examples/quickcontrols2/gallery/gallery.qrc +++ /dev/null @@ -1,61 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>gallery.qml</file> - <file>qtquickcontrols2.conf</file> - <file>images/arrow.png</file> - <file>images/arrow@2x.png</file> - <file>images/arrow@3x.png</file> - <file>images/arrow@4x.png</file> - <file>images/arrows.png</file> - <file>images/arrows@2x.png</file> - <file>images/arrows@3x.png</file> - <file>images/arrows@4x.png</file> - <file>images/drawer.png</file> - <file>images/drawer@2x.png</file> - <file>images/drawer@3x.png</file> - <file>images/drawer@4x.png</file> - <file>images/menu.png</file> - <file>images/menu@2x.png</file> - <file>images/menu@3x.png</file> - <file>images/menu@4x.png</file> - <file>images/+material/drawer.png</file> - <file>images/+material/drawer@2x.png</file> - <file>images/+material/drawer@3x.png</file> - <file>images/+material/drawer@4x.png</file> - <file>images/+material/menu.png</file> - <file>images/+material/menu@2x.png</file> - <file>images/+material/menu@3x.png</file> - <file>images/+material/menu@4x.png</file> - <file>images/qt-logo.png</file> - <file>images/qt-logo@2x.png</file> - <file>images/qt-logo@3x.png</file> - <file>images/qt-logo@4x.png</file> - <file>pages/BusyIndicatorPage.qml</file> - <file>pages/ButtonPage.qml</file> - <file>pages/CheckBoxPage.qml</file> - <file>pages/ComboBoxPage.qml</file> - <file>pages/DialPage.qml</file> - <file>pages/DrawerPage.qml</file> - <file>pages/FramePage.qml</file> - <file>pages/GroupBoxPage.qml</file> - <file>pages/MenuPage.qml</file> - <file>pages/PageIndicatorPage.qml</file> - <file>pages/PopupPage.qml</file> - <file>pages/ProgressBarPage.qml</file> - <file>pages/RadioButtonPage.qml</file> - <file>pages/RangeSliderPage.qml</file> - <file>pages/ScrollBarPage.qml</file> - <file>pages/ScrollIndicatorPage.qml</file> - <file>pages/SliderPage.qml</file> - <file>pages/SpinBoxPage.qml</file> - <file>pages/StackViewPage.qml</file> - <file>pages/SwipeViewPage.qml</file> - <file>pages/SwitchPage.qml</file> - <file>pages/TabBarPage.qml</file> - <file>pages/TextAreaPage.qml</file> - <file>pages/TextFieldPage.qml</file> - <file>pages/ToolTipPage.qml</file> - <file>pages/TumblerPage.qml</file> - <file>pages/DelegatePage.qml</file> - </qresource> -</RCC> diff --git a/examples/quickcontrols2/gallery/pages/BusyIndicatorPage.qml b/examples/quickcontrols2/gallery/pages/BusyIndicatorPage.qml index ec73ac36..908d8f6d 100644 --- a/examples/quickcontrols2/gallery/pages/BusyIndicatorPage.qml +++ b/examples/quickcontrols2/gallery/pages/BusyIndicatorPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/ButtonPage.qml b/examples/quickcontrols2/gallery/pages/ButtonPage.qml index da817eba..181cc2fc 100644 --- a/examples/quickcontrols2/gallery/pages/ButtonPage.qml +++ b/examples/quickcontrols2/gallery/pages/ButtonPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/CheckBoxPage.qml b/examples/quickcontrols2/gallery/pages/CheckBoxPage.qml index 3e08555b..3793cc1f 100644 --- a/examples/quickcontrols2/gallery/pages/CheckBoxPage.qml +++ b/examples/quickcontrols2/gallery/pages/CheckBoxPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/ComboBoxPage.qml b/examples/quickcontrols2/gallery/pages/ComboBoxPage.qml index 58c5b97d..bf2b70d8 100644 --- a/examples/quickcontrols2/gallery/pages/ComboBoxPage.qml +++ b/examples/quickcontrols2/gallery/pages/ComboBoxPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/DelegatePage.qml b/examples/quickcontrols2/gallery/pages/DelegatePage.qml index 3d175fd8..525487d1 100644 --- a/examples/quickcontrols2/gallery/pages/DelegatePage.qml +++ b/examples/quickcontrols2/gallery/pages/DelegatePage.qml @@ -40,7 +40,7 @@ import QtQuick 2.6 import QtQuick.Layouts 1.1 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { padding: 0 @@ -70,17 +70,17 @@ Pane { text: labelText width: parent.width - onClicked: if (swipe.complete) view.model.remove(ourIndex) - Component { id: removeComponent Rectangle { - color: swipeDelegate.swipe.complete && swipeDelegate.pressed ? "#333" : "#444" + color: SwipeDelegate.pressed ? "#333" : "#444" width: parent.width height: parent.height clip: true + SwipeDelegate.onClicked: view.model.remove(ourIndex) + Label { font.pixelSize: swipeDelegate.font.pixelSize text: "Remove" diff --git a/examples/quickcontrols2/gallery/pages/DialPage.qml b/examples/quickcontrols2/gallery/pages/DialPage.qml index bac6fb47..2901cb4d 100644 --- a/examples/quickcontrols2/gallery/pages/DialPage.qml +++ b/examples/quickcontrols2/gallery/pages/DialPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/DrawerPage.qml b/examples/quickcontrols2/gallery/pages/DrawerPage.qml index f4a7d272..1764bbce 100644 --- a/examples/quickcontrols2/gallery/pages/DrawerPage.qml +++ b/examples/quickcontrols2/gallery/pages/DrawerPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/FramePage.qml b/examples/quickcontrols2/gallery/pages/FramePage.qml index 1979007c..579e7e2d 100644 --- a/examples/quickcontrols2/gallery/pages/FramePage.qml +++ b/examples/quickcontrols2/gallery/pages/FramePage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/GroupBoxPage.qml b/examples/quickcontrols2/gallery/pages/GroupBoxPage.qml index 194c14b4..52676721 100644 --- a/examples/quickcontrols2/gallery/pages/GroupBoxPage.qml +++ b/examples/quickcontrols2/gallery/pages/GroupBoxPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/MenuPage.qml b/examples/quickcontrols2/gallery/pages/MenuPage.qml index ec1c88cd..18321038 100644 --- a/examples/quickcontrols2/gallery/pages/MenuPage.qml +++ b/examples/quickcontrols2/gallery/pages/MenuPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/PageIndicatorPage.qml b/examples/quickcontrols2/gallery/pages/PageIndicatorPage.qml index 8dfd9725..8f17a302 100644 --- a/examples/quickcontrols2/gallery/pages/PageIndicatorPage.qml +++ b/examples/quickcontrols2/gallery/pages/PageIndicatorPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/PopupPage.qml b/examples/quickcontrols2/gallery/pages/PopupPage.qml index 5a70c617..432ff8d8 100644 --- a/examples/quickcontrols2/gallery/pages/PopupPage.qml +++ b/examples/quickcontrols2/gallery/pages/PopupPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane @@ -62,7 +62,7 @@ Pane { anchors.horizontalCenter: parent.horizontalCenter width: Math.max(implicitWidth, Math.min(implicitWidth * 2, pane.availableWidth / 3)) - onClicked: settingsPopup.open() + onClicked: settingsDialog.open() } } } diff --git a/examples/quickcontrols2/gallery/pages/ProgressBarPage.qml b/examples/quickcontrols2/gallery/pages/ProgressBarPage.qml index 901cddd8..85024c07 100644 --- a/examples/quickcontrols2/gallery/pages/ProgressBarPage.qml +++ b/examples/quickcontrols2/gallery/pages/ProgressBarPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/RadioButtonPage.qml b/examples/quickcontrols2/gallery/pages/RadioButtonPage.qml index c57c7baf..e6f178a8 100644 --- a/examples/quickcontrols2/gallery/pages/RadioButtonPage.qml +++ b/examples/quickcontrols2/gallery/pages/RadioButtonPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/RangeSliderPage.qml b/examples/quickcontrols2/gallery/pages/RangeSliderPage.qml index 5a73c09d..df354fd4 100644 --- a/examples/quickcontrols2/gallery/pages/RangeSliderPage.qml +++ b/examples/quickcontrols2/gallery/pages/RangeSliderPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/ScrollBarPage.qml b/examples/quickcontrols2/gallery/pages/ScrollBarPage.qml index f8f617d7..1c026270 100644 --- a/examples/quickcontrols2/gallery/pages/ScrollBarPage.qml +++ b/examples/quickcontrols2/gallery/pages/ScrollBarPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/ScrollIndicatorPage.qml b/examples/quickcontrols2/gallery/pages/ScrollIndicatorPage.qml index d76d2de3..db48ca3b 100644 --- a/examples/quickcontrols2/gallery/pages/ScrollIndicatorPage.qml +++ b/examples/quickcontrols2/gallery/pages/ScrollIndicatorPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/SliderPage.qml b/examples/quickcontrols2/gallery/pages/SliderPage.qml index 43da4209..886af3df 100644 --- a/examples/quickcontrols2/gallery/pages/SliderPage.qml +++ b/examples/quickcontrols2/gallery/pages/SliderPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/SpinBoxPage.qml b/examples/quickcontrols2/gallery/pages/SpinBoxPage.qml index febc869a..b8b42b49 100644 --- a/examples/quickcontrols2/gallery/pages/SpinBoxPage.qml +++ b/examples/quickcontrols2/gallery/pages/SpinBoxPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/StackViewPage.qml b/examples/quickcontrols2/gallery/pages/StackViewPage.qml index 4e44c3c8..d99a4b93 100644 --- a/examples/quickcontrols2/gallery/pages/StackViewPage.qml +++ b/examples/quickcontrols2/gallery/pages/StackViewPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 StackView { id: stackView diff --git a/examples/quickcontrols2/gallery/pages/SwipeViewPage.qml b/examples/quickcontrols2/gallery/pages/SwipeViewPage.qml index d0b0486c..cfa32b41 100644 --- a/examples/quickcontrols2/gallery/pages/SwipeViewPage.qml +++ b/examples/quickcontrols2/gallery/pages/SwipeViewPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/SwitchPage.qml b/examples/quickcontrols2/gallery/pages/SwitchPage.qml index f69a1409..24fe5478 100644 --- a/examples/quickcontrols2/gallery/pages/SwitchPage.qml +++ b/examples/quickcontrols2/gallery/pages/SwitchPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/TabBarPage.qml b/examples/quickcontrols2/gallery/pages/TabBarPage.qml index 1b5bdb9b..78dfc6e9 100644 --- a/examples/quickcontrols2/gallery/pages/TabBarPage.qml +++ b/examples/quickcontrols2/gallery/pages/TabBarPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Page { id: page diff --git a/examples/quickcontrols2/gallery/pages/TextAreaPage.qml b/examples/quickcontrols2/gallery/pages/TextAreaPage.qml index 733ac24b..696b1983 100644 --- a/examples/quickcontrols2/gallery/pages/TextAreaPage.qml +++ b/examples/quickcontrols2/gallery/pages/TextAreaPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Flickable { id: flickable diff --git a/examples/quickcontrols2/gallery/pages/TextFieldPage.qml b/examples/quickcontrols2/gallery/pages/TextFieldPage.qml index cbae30cd..d41a17a1 100644 --- a/examples/quickcontrols2/gallery/pages/TextFieldPage.qml +++ b/examples/quickcontrols2/gallery/pages/TextFieldPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/ToolTipPage.qml b/examples/quickcontrols2/gallery/pages/ToolTipPage.qml index 06a5fbf9..f5df5623 100644 --- a/examples/quickcontrols2/gallery/pages/ToolTipPage.qml +++ b/examples/quickcontrols2/gallery/pages/ToolTipPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane diff --git a/examples/quickcontrols2/gallery/pages/TumblerPage.qml b/examples/quickcontrols2/gallery/pages/TumblerPage.qml index ff295b43..2e248bfc 100644 --- a/examples/quickcontrols2/gallery/pages/TumblerPage.qml +++ b/examples/quickcontrols2/gallery/pages/TumblerPage.qml @@ -39,7 +39,7 @@ ****************************************************************************/ import QtQuick 2.6 -import QtQuick.Controls 2.0 +import QtQuick.Controls 2.1 Pane { id: pane @@ -57,7 +57,6 @@ Pane { Tumbler { model: 10 - visibleItemCount: 5 anchors.horizontalCenter: parent.horizontalCenter } } diff --git a/examples/quickcontrols2/gallery/qtquickcontrols2.conf b/examples/quickcontrols2/gallery/qtquickcontrols2.conf index ce348f2e..da1a8f41 100644 --- a/examples/quickcontrols2/gallery/qtquickcontrols2.conf +++ b/examples/quickcontrols2/gallery/qtquickcontrols2.conf @@ -1,8 +1,8 @@ [Material] Primary=#41cd52 Accent=#41cd52 -Theme=Light +Theme=System [Universal] Accent=#41cd52 -Theme=Light +Theme=System diff --git a/examples/quickcontrols2/quickcontrols2.pro b/examples/quickcontrols2/quickcontrols2.pro index 4f0f2fcc..6d732b17 100644 --- a/examples/quickcontrols2/quickcontrols2.pro +++ b/examples/quickcontrols2/quickcontrols2.pro @@ -1,4 +1,5 @@ TEMPLATE = subdirs SUBDIRS += \ gallery \ - chattutorial + chattutorial \ + texteditor diff --git a/examples/quickcontrols2/texteditor/doc/images/qtquickcontrols2-texteditor-desktop.png b/examples/quickcontrols2/texteditor/doc/images/qtquickcontrols2-texteditor-desktop.png Binary files differnew file mode 100644 index 00000000..4b4d7f79 --- /dev/null +++ b/examples/quickcontrols2/texteditor/doc/images/qtquickcontrols2-texteditor-desktop.png diff --git a/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc b/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc new file mode 100644 index 00000000..60ff0567 --- /dev/null +++ b/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example texteditor + \title Qt Quick Controls 2 - Text Editor + \ingroup qtquickcontrols2-examples + \brief A QML app using Qt Quick Controls 2 and a C++ class to + provide a fully-functional rich-text editor application. + + \image qtquickcontrols2-texteditor-desktop.png + + The \e {Text Editor Example} presents a sample HTML file using the TextArea + control, preserving the HTML formatting. It uses a C++ class to handle the + document by providing options to open, format, and edit. The application + also lets you open and edit plain text files. + + The C++ class, DocumentHandler, extends QObject and is registered + as a QML type under the namespace \c {io.qt.examples.texteditor 1.0}. + + The following snippets show how the type is registered under + a namespace and later imported by \e main.qml. + + QML type registration: + + \code + #include <QtQml/qqml.h> + ... + qmlRegisterType<DocumentHandler>("io.qt.examples.texteditor", 1, 0, "DocumentHandler"); + ... + \endcode + + QML namespace import: + + \code + import io.qt.examples.texteditor 1.0 + \endcode + + For more information about registering C++ classes as QML types, see + \l {Defining QML Types from C++}. + + \include examples-run.qdocinc +*/ diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp new file mode 100644 index 00000000..45c0f4cb --- /dev/null +++ b/examples/quickcontrols2/texteditor/documenthandler.cpp @@ -0,0 +1,362 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "documenthandler.h" + +#include <QFile> +#include <QFileInfo> +#include <QQmlFile> +#include <QQuickTextDocument> +#include <QTextCharFormat> +#include <QTextCodec> +#include <QTextDocument> + +DocumentHandler::DocumentHandler(QObject *parent) + : QObject(parent) + , m_document(nullptr) + , m_cursorPosition(-1) + , m_selectionStart(0) + , m_selectionEnd(0) +{ +} + +QQuickTextDocument *DocumentHandler::document() const +{ + return m_document; +} + +void DocumentHandler::setDocument(QQuickTextDocument *document) +{ + if (document == m_document) + return; + + m_document = document; + emit documentChanged(); +} + +int DocumentHandler::cursorPosition() const +{ + return m_cursorPosition; +} + +void DocumentHandler::setCursorPosition(int position) +{ + if (position == m_cursorPosition) + return; + + m_cursorPosition = position; + reset(); + emit cursorPositionChanged(); +} + +int DocumentHandler::selectionStart() const +{ + return m_selectionStart; +} + +void DocumentHandler::setSelectionStart(int position) +{ + if (position == m_selectionStart) + return; + + m_selectionStart = position; + emit selectionStartChanged(); +} + +int DocumentHandler::selectionEnd() const +{ + return m_selectionEnd; +} + +void DocumentHandler::setSelectionEnd(int position) +{ + if (position == m_selectionEnd) + return; + + m_selectionEnd = 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(); + if (cursor.isNull()) + return QColor(Qt::black); + QTextCharFormat format = cursor.charFormat(); + return format.foreground().color(); +} + +void DocumentHandler::setTextColor(const QColor &color) +{ + QTextCharFormat format; + format.setForeground(QBrush(color)); + mergeFormatOnWordOrSelection(format); + emit textColorChanged(); +} + +Qt::Alignment DocumentHandler::alignment() const +{ + QTextCursor cursor = textCursor(); + if (cursor.isNull()) + return Qt::AlignLeft; + return textCursor().blockFormat().alignment(); +} + +void DocumentHandler::setAlignment(Qt::Alignment alignment) +{ + QTextBlockFormat format; + format.setAlignment(alignment); + QTextCursor cursor = textCursor(); + cursor.mergeBlockFormat(format); + 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); + const QString fileName = QFileInfo(filePath).fileName(); + if (fileName.isEmpty()) + return QStringLiteral("untitled.txt"); + return fileName; +} + +QString DocumentHandler::fileType() const +{ + return QFileInfo(fileName()).suffix(); +} + +QUrl DocumentHandler::fileUrl() const +{ + return m_fileUrl; +} + +void DocumentHandler::load(const QUrl &fileUrl) +{ + if (fileUrl == m_fileUrl) + return; + + QString fileName = QQmlFile::urlToLocalFileOrQrc(fileUrl); + if (QFile::exists(fileName)) { + QFile file(fileName); + if (file.open(QFile::ReadOnly)) { + QByteArray data = file.readAll(); + QTextCodec *codec = QTextCodec::codecForHtml(data); + if (QTextDocument *doc = textDocument()) + doc->setModified(false); + + emit loaded(codec->toUnicode(data)); + reset(); + } + } + + m_fileUrl = fileUrl; + emit fileUrlChanged(); +} + +void DocumentHandler::saveAs(const QUrl &fileUrl) +{ + QTextDocument *doc = textDocument(); + if (!doc) + return; + + const QString filePath = fileUrl.toLocalFile(); + const bool isHtml = QFileInfo(filePath).suffix().contains(QLatin1String("htm")); + QFile file(filePath); + if (!file.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) { + emit error(tr("Cannot save: ") + file.errorString()); + return; + } + file.write((isHtml ? doc->toHtml() : doc->toPlainText()).toUtf8()); + file.close(); + + if (fileUrl == m_fileUrl) + return; + + m_fileUrl = fileUrl; + emit fileUrlChanged(); +} + +void DocumentHandler::reset() +{ + emit fontFamilyChanged(); + emit alignmentChanged(); + emit boldChanged(); + emit italicChanged(); + emit underlineChanged(); + emit fontSizeChanged(); + emit textColorChanged(); +} + +QTextCursor DocumentHandler::textCursor() const +{ + QTextDocument *doc = textDocument(); + if (!doc) + return QTextCursor(); + + QTextCursor cursor = QTextCursor(doc); + if (m_selectionStart != m_selectionEnd) { + cursor.setPosition(m_selectionStart); + cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor); + } else { + cursor.setPosition(m_cursorPosition); + } + return cursor; +} + +QTextDocument *DocumentHandler::textDocument() const +{ + if (!m_document) + return nullptr; + + return m_document->textDocument(); +} + +void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) +{ + QTextCursor cursor = textCursor(); + if (!cursor.hasSelection()) + cursor.select(QTextCursor::WordUnderCursor); + cursor.mergeCharFormat(format); +} diff --git a/examples/quickcontrols2/texteditor/documenthandler.h b/examples/quickcontrols2/texteditor/documenthandler.h new file mode 100644 index 00000000..dd3c3988 --- /dev/null +++ b/examples/quickcontrols2/texteditor/documenthandler.h @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DOCUMENTHANDLER_H +#define DOCUMENTHANDLER_H + +#include <QFont> +#include <QObject> +#include <QTextCursor> +#include <QUrl> + +QT_BEGIN_NAMESPACE +class QTextDocument; +class QQuickTextDocument; +QT_END_NAMESPACE + +class DocumentHandler : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged) + Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) + Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged) + Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged) + + 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) + Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) + Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) + + Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) + + Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged) + Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged) + Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged) + +public: + explicit DocumentHandler(QObject *parent = nullptr); + + QQuickTextDocument *document() const; + void setDocument(QQuickTextDocument *document); + + int cursorPosition() const; + void setCursorPosition(int position); + + int selectionStart() const; + void setSelectionStart(int position); + + int selectionEnd() const; + void setSelectionEnd(int position); + + QString fontFamily() const; + void setFontFamily(const QString &family); + + QColor textColor() const; + void setTextColor(const QColor &color); + + Qt::Alignment alignment() const; + void setAlignment(Qt::Alignment alignment); + + bool bold() const; + void setBold(bool bold); + + bool italic() const; + void setItalic(bool italic); + + bool underline() const; + void setUnderline(bool underline); + + int fontSize() const; + void setFontSize(int size); + + QString fileName() const; + QString fileType() const; + QUrl fileUrl() const; + +public Q_SLOTS: + void load(const QUrl &fileUrl); + void saveAs(const QUrl &fileUrl); + +Q_SIGNALS: + void documentChanged(); + void cursorPositionChanged(); + void selectionStartChanged(); + void selectionEndChanged(); + + void fontFamilyChanged(); + void textColorChanged(); + void alignmentChanged(); + + void boldChanged(); + void italicChanged(); + void underlineChanged(); + + void fontSizeChanged(); + + void textChanged(); + void fileUrlChanged(); + + void loaded(const QString &text); + void error(const QString &message); + +private: + void reset(); + QTextCursor textCursor() const; + QTextDocument *textDocument() const; + void mergeFormatOnWordOrSelection(const QTextCharFormat &format); + + QQuickTextDocument *m_document; + + int m_cursorPosition; + int m_selectionStart; + int m_selectionEnd; + + QFont m_font; + int m_fontSize; + QUrl m_fileUrl; +}; + +#endif // DOCUMENTHANDLER_H diff --git a/examples/quickcontrols2/texteditor/fonts/fontello.ttf b/examples/quickcontrols2/texteditor/fonts/fontello.ttf Binary files differnew file mode 100644 index 00000000..d2914494 --- /dev/null +++ b/examples/quickcontrols2/texteditor/fonts/fontello.ttf diff --git a/examples/quickcontrols2/texteditor/images/qt-logo.png b/examples/quickcontrols2/texteditor/images/qt-logo.png Binary files differnew file mode 100644 index 00000000..e16b4182 --- /dev/null +++ b/examples/quickcontrols2/texteditor/images/qt-logo.png diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml new file mode 100644 index 00000000..66bea215 --- /dev/null +++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml @@ -0,0 +1,440 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.8 +import QtQuick.Controls 2.1 +import QtQuick.Window 2.0 +import QtQuick.Controls.Material 2.1 +import Qt.labs.platform 1.0 + +import io.qt.examples.texteditor 1.0 + +ApplicationWindow { + id: window + width: 1024 + height: 600 + visible: true + title: document.fileName + " - Text Editor Example" + + Component.onCompleted: { + x = Screen.width / 2 - width / 2 + y = Screen.height / 2 - height / 2 + } + + Shortcut { + sequence: StandardKey.Open + onActivated: openDialog.open() + } + Shortcut { + sequence: StandardKey.SaveAs + onActivated: saveDialog.open() + } + Shortcut { + sequence: StandardKey.Quit + onActivated: Qt.quit() + } + Shortcut { + sequence: StandardKey.Copy + onActivated: textArea.copy() + } + Shortcut { + sequence: StandardKey.Cut + onActivated: textArea.cut() + } + Shortcut { + sequence: StandardKey.Paste + onActivated: textArea.paste() + } + Shortcut { + sequence: StandardKey.Bold + onActivated: document.bold = !document.bold + } + Shortcut { + sequence: StandardKey.Italic + onActivated: document.italic = !document.italic + } + Shortcut { + sequence: StandardKey.Underline + onActivated: document.underline = !document.underline + } + + MenuBar { + Menu { + title: qsTr("File") + + MenuItem { + text: qsTr("Open") + onTriggered: openDialog.open() + } + MenuItem { + text: qsTr("Save As...") + onTriggered: saveDialog.open() + } + MenuItem { + text: qsTr("Quit") + onTriggered: Qt.quit() + } + } + + Menu { + title: qsTr("Edit") + + MenuItem { + text: qsTr("Copy") + enabled: textArea.selectedText + onTriggered: textArea.copy() + } + MenuItem { + text: qsTr("Cut") + enabled: textArea.selectedText + onTriggered: textArea.cut() + } + MenuItem { + text: qsTr("Paste") + enabled: textArea.canPaste + onTriggered: textArea.paste() + } + } + + Menu { + title: qsTr("Format") + + MenuItem { + text: qsTr("Bold") + checkable: true + checked: document.bold + onTriggered: document.bold = !document.bold + } + MenuItem { + text: qsTr("Italic") + checkable: true + checked: document.italic + onTriggered: document.italic = !document.italic + } + MenuItem { + text: qsTr("Underline") + checkable: true + checked: document.underline + onTriggered: document.underline = !document.underline + } + } + } + + FileDialog { + id: openDialog + fileMode: FileDialog.OpenFile + selectedNameFilter: nameFilters[1] + nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"] + onAccepted: document.load(file) + } + + FileDialog { + id: saveDialog + fileMode: FileDialog.SaveFile + defaultSuffix: document.fileType + nameFilters: openDialog.nameFilters + selectedNameFilter: document.fileType === "txt" ? nameFilters[0] : nameFilters[1] + onAccepted: document.saveAs(file) + } + + FontDialog { + id: fontDialog + onAccepted: { + document.fontFamily = font.family; + document.fontSize = font.pointSize; + } + } + + ColorDialog { + id: colorDialog + currentColor: "black" + } + + MessageDialog { + id: errorDialog + } + + header: ToolBar { + leftPadding: 8 + + Flow { + id: flow + width: parent.width + + Row { + id: fileRow + ToolButton { + id: openButton + text: "\uF115" + font.family: "fontello" + onClicked: openDialog.open() + } + ToolSeparator { + contentItem.visible: fileRow.y === editRow.y + } + } + + Row { + id: editRow + ToolButton { + id: copyButton + text: "\uF0C5" + font.family: "fontello" + focusPolicy: Qt.TabFocus + enabled: textArea.selectedText + onClicked: textArea.copy() + } + ToolButton { + id: cutButton + text: "\uE802" + font.family: "fontello" + focusPolicy: Qt.TabFocus + enabled: textArea.selectedText + onClicked: textArea.cut() + } + ToolButton { + id: pasteButton + text: "\uF0EA" + font.family: "fontello" + focusPolicy: Qt.TabFocus + enabled: textArea.canPaste + onClicked: textArea.paste() + } + ToolSeparator { + contentItem.visible: editRow.y === formatRow.y + } + } + + Row { + id: formatRow + ToolButton { + id: boldButton + text: "\uE800" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.bold + onClicked: document.bold = !document.bold + } + ToolButton { + id: italicButton + text: "\uE801" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.italic + onClicked: document.italic = !document.italic + } + ToolButton { + id: underlineButton + text: "\uF0CD" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.underline + onClicked: document.underline = !document.underline + } + ToolButton { + id: fontFamilyToolButton + text: qsTr("\uE808") + font.family: "fontello" + font.bold: document.bold + font.italic: document.italic + font.underline: document.underline + onClicked: { + fontDialog.currentFont.family = document.fontFamily; + fontDialog.currentFont.pointSize = document.fontSize; + fontDialog.open(); + } + } + ToolButton { + id: textColorButton + text: "\uF1FC" + font.family: "fontello" + focusPolicy: Qt.TabFocus + onClicked: colorDialog.open() + + Rectangle { + width: aFontMetrics.width + 3 + height: 2 + color: document.textColor + parent: textColorButton.contentItem + anchors.horizontalCenter: parent.horizontalCenter + anchors.baseline: parent.baseline + anchors.baselineOffset: 6 + + TextMetrics { + id: aFontMetrics + font: textColorButton.font + text: textColorButton.text + } + } + } + ToolSeparator { + contentItem.visible: formatRow.y === alignRow.y + } + } + + Row { + id: alignRow + ToolButton { + id: alignLeftButton + text: "\uE803" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.alignment == Qt.AlignLeft + onClicked: document.alignment = Qt.AlignLeft + } + ToolButton { + id: alignCenterButton + text: "\uE804" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.alignment == Qt.AlignHCenter + onClicked: document.alignment = Qt.AlignHCenter + } + ToolButton { + id: alignRightButton + text: "\uE805" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.alignment == Qt.AlignRight + onClicked: document.alignment = Qt.AlignRight + } + ToolButton { + id: alignJustifyButton + text: "\uE806" + font.family: "fontello" + focusPolicy: Qt.TabFocus + checkable: true + checked: document.alignment == Qt.AlignJustify + onClicked: document.alignment = Qt.AlignJustify + } + } + } + } + + DocumentHandler { + id: document + document: textArea.textDocument + cursorPosition: textArea.cursorPosition + selectionStart: textArea.selectionStart + selectionEnd: textArea.selectionEnd + textColor: colorDialog.color + // TODO: if we don't do this, e.g. the bold button won't be checked + // when it should be (the title is bold) + Component.onCompleted: document.load("qrc:/texteditor.html") + onLoaded: { + textArea.text = text + } + onError: { + errorDialog.text = message + errorDialog.visible = true + } + } + + Flickable { + id: flickable + flickableDirection: Flickable.VerticalFlick + anchors.fill: parent + + TextArea.flickable: TextArea { + id: textArea + textFormat: Qt.RichText + wrapMode: TextArea.Wrap + focus: true + selectByMouse: true + persistentSelection: true + // Different styles have different padding and background + // decorations, but since this editor is almost taking up the + // entire window, we don't need them. + leftPadding: 6 + rightPadding: 6 + topPadding: 0 + bottomPadding: 0 + background: null + + MouseArea { + acceptedButtons: Qt.RightButton + anchors.fill: parent + onClicked: contextMenu.open() + } + + onLinkActivated: Qt.openUrlExternally(link) + } + + ScrollBar.vertical: ScrollBar {} + } + + Menu { + id: contextMenu + + MenuItem { + text: qsTr("Copy") + enabled: textArea.selectedText + onTriggered: textArea.copy() + } + MenuItem { + text: qsTr("Cut") + enabled: textArea.selectedText + onTriggered: textArea.cut() + } + MenuItem { + text: qsTr("Paste") + enabled: textArea.canPaste + onTriggered: textArea.paste() + } + + MenuSeparator {} + + MenuItem { + text: qsTr("Font...") + onTriggered: fontDialog.open() + } + + MenuItem { + text: qsTr("Color...") + onTriggered: colorDialog.open() + } + } +} diff --git a/examples/quickcontrols2/texteditor/qtquickcontrols2.conf b/examples/quickcontrols2/texteditor/qtquickcontrols2.conf new file mode 100644 index 00000000..496037be --- /dev/null +++ b/examples/quickcontrols2/texteditor/qtquickcontrols2.conf @@ -0,0 +1,10 @@ +[Controls] +Style=Material + +[Material] +Primary=BlueGrey +Accent=Blue +Theme=System + +[Universal] +Theme=System diff --git a/examples/quickcontrols2/texteditor/texteditor.cpp b/examples/quickcontrols2/texteditor/texteditor.cpp new file mode 100644 index 00000000..9af4cc3b --- /dev/null +++ b/examples/quickcontrols2/texteditor/texteditor.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include <QFontDatabase> +#include <QDebug> +#include <QQmlApplicationEngine> +#include <QQmlContext> +#include <QQuickStyle> +#include <QFileSelector> + +#include "documenthandler.h" + +int main(int argc, char *argv[]) +{ + QGuiApplication::setApplicationName("Text Editor"); + QGuiApplication::setOrganizationName("QtProject"); + QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + + QApplication app(argc, argv); + + QFontDatabase fontDatabase; + if (fontDatabase.addApplicationFont(":/fonts/fontello.ttf") == -1) + qWarning() << "Failed to load fontello.ttf"; + + qmlRegisterType<DocumentHandler>("io.qt.examples.texteditor", 1, 0, "DocumentHandler"); + + QQmlApplicationEngine engine; + engine.load(QUrl("qrc:/qml/texteditor.qml")); + if (engine.rootObjects().isEmpty()) + return -1; + + return app.exec(); +} diff --git a/examples/quickcontrols2/texteditor/texteditor.html b/examples/quickcontrols2/texteditor/texteditor.html new file mode 100644 index 00000000..1310da42 --- /dev/null +++ b/examples/quickcontrols2/texteditor/texteditor.html @@ -0,0 +1,44 @@ +<html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <meta name="qrichtext" content="1"> + <title>Text Editor Example</title> + <style type="text/css"> + p, + body, <!-- specifiying font-size for body seems to be necessary to avoid bullet points being incorrectly positioned --> + li { + white-space: pre-wrap; + font-size:11pt; + } + </style> +</head> +<body> + <p align="center"> + <img src="qrc:/images/qt-logo.png" /> + </p> + <h2 align="center"> + Qt Quick Controls 2 + </h2> + <p align="center"> + This example demonstrates a modern rich text editor. The UI uses Qt Labs Platforms to provide native menus and dialogs. + </p> + <br /> + <br /> + <br /> + + <p> + Below you'll find a list of the native controls used in this application. + </p> + + <ul> + <!-- TODO: update the links before the 5.8 release --> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-menu.html">Menu</a> - provides a QML API for native platform menu popups.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-menubar.html">MenuBar</a> - provides a QML API for native platform menubars.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-menuitem.html">MenuItem</a> - provides a QML API for native platform menu items.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-filedialog.html">FileDialog</a> - provides a QML API for native platform file dialogs.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-fontdialog.html">FontDialog</a> - provides a QML API for native platform font dialogs.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-colordialog.html">ColorDialog</a> - provides a QML API for native platform color dialogs.</li> + <li><a href="http://doc-snapshots.qt.io/qt5-dev/qml-qt-labs-platform-messagedialog.html">MessageDialog</a> - provides a QML API for native platform message dialogs.</li> + </ul> +</body> +</html> diff --git a/examples/quickcontrols2/texteditor/texteditor.pro b/examples/quickcontrols2/texteditor/texteditor.pro new file mode 100644 index 00000000..ed645ca7 --- /dev/null +++ b/examples/quickcontrols2/texteditor/texteditor.pro @@ -0,0 +1,19 @@ +TEMPLATE = app +TARGET = texteditor +QT += quick quickcontrols2 widgets + +HEADERS += \ + documenthandler.h + +SOURCES += \ + texteditor.cpp \ + documenthandler.cpp + +OTHER_FILES += \ + qml/*.qml + +RESOURCES += \ + texteditor.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/quickcontrols2/texteditor +INSTALLS += target diff --git a/examples/quickcontrols2/texteditor/texteditor.qrc b/examples/quickcontrols2/texteditor/texteditor.qrc new file mode 100644 index 00000000..92f317fa --- /dev/null +++ b/examples/quickcontrols2/texteditor/texteditor.qrc @@ -0,0 +1,9 @@ +<RCC> + <qresource prefix="/"> + <file>qtquickcontrols2.conf</file> + <file>images/qt-logo.png</file> + <file>fonts/fontello.ttf</file> + <file>qml/texteditor.qml</file> + <file>texteditor.html</file> + </qresource> +</RCC> |