diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2021-10-20 15:11:12 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2021-11-03 11:40:59 +0100 |
commit | dbf7048ae265f43dc8b097dd55e6de21e53ed645 (patch) | |
tree | 2609d813e5a802f31bdb397ec9b0fc9cc7668918 /examples | |
parent | ec9e2c047992ca7f12883de5128fa0e4e84733bf (diff) |
Add calendar types
Formerly Qt.labs.calendar, then QtQuick.Calendar in the marketplace,
this now returns as part of the controls module.
[ChangeLog] Added types from Qt.labs.calendar/QtQuick.Calendar.
Change-Id: I8a06c08e6520be1ba8f33c73ba174785724e99c2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'examples')
16 files changed, 984 insertions, 0 deletions
diff --git a/examples/quickcontrols2/CMakeLists.txt b/examples/quickcontrols2/CMakeLists.txt index 7ca535e9ef..1f760a1bb2 100644 --- a/examples/quickcontrols2/CMakeLists.txt +++ b/examples/quickcontrols2/CMakeLists.txt @@ -2,6 +2,9 @@ add_subdirectory(gallery) add_subdirectory(chattutorial) +if (QT_FEATURE_quicktemplates2_calendar AND TARGET Qt::Sql) + add_subdirectory(eventcalendar) +endif() add_subdirectory(texteditor) add_subdirectory(contactlist) add_subdirectory(sidepanel) diff --git a/examples/quickcontrols2/eventcalendar/CMakeLists.txt b/examples/quickcontrols2/eventcalendar/CMakeLists.txt new file mode 100644 index 0000000000..a5f417677d --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/CMakeLists.txt @@ -0,0 +1,54 @@ +# Generated from eventcalendar.pro. + +cmake_minimum_required(VERSION 3.16) +project(eventcalendar LANGUAGES CXX) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOMOC ON) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quickcontrols2/eventcalendar") + +find_package(Qt6 COMPONENTS Core) +find_package(Qt6 COMPONENTS Gui) +find_package(Qt6 COMPONENTS Sql) +find_package(Qt6 COMPONENTS Qml) + +qt_add_executable(eventcalendar + event.h + eventcalendar.cpp + eventmodel.cpp eventmodel.h + sqleventdatabase.cpp sqleventdatabase.h +) +set_target_properties(eventcalendar PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) +target_link_libraries(eventcalendar PUBLIC + Qt::Core + Qt::Gui + Qt::Qml + Qt::Sql +) + +qt_add_qml_module(eventcalendar + URI App + VERSION 1.0 + QML_FILES + MonthGridDelegate.qml + eventcalendar.qml + RESOURCES + qtquickcontrols2.conf + NO_RESOURCE_TARGET_PATH +) + +install(TARGETS eventcalendar + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) + diff --git a/examples/quickcontrols2/eventcalendar/MonthGridDelegate.qml b/examples/quickcontrols2/eventcalendar/MonthGridDelegate.qml new file mode 100644 index 0000000000..b42d74f92b --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/MonthGridDelegate.qml @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts +import QtQuick.Controls.Material + +import App + +ColumnLayout { + id: root + + required property EventDatabase eventDatabase + + required property bool today + required property int year + required property int month + required property int day + + required property int visibleMonth + + Material.theme: today ? Material.Dark : undefined + + Label { + id: dayText + horizontalAlignment: Text.AlignHCenter + topPadding: 4 + opacity: month === root.visibleMonth ? 1 : 0 + text: day + + Layout.fillWidth: true + + Rectangle { + width: height + height: Math.max(dayText.implicitWidth, dayText.implicitHeight) + radius: width / 2 + color: Material.primary + anchors.centerIn: dayText + anchors.verticalCenterOffset: dayText.height - dayText.baselineOffset + z: -1 + visible: root.today + } + } + + ListView { + spacing: 1 + clip: true + + Layout.fillWidth: true + Layout.fillHeight: true + Layout.topMargin: 4 + + delegate: ItemDelegate { + id: itemDelegate + width: parent.width + text: name + font.pixelSize: Qt.application.font.pixelSize * 0.8 + leftPadding: 4 + rightPadding: 4 + topPadding: 4 + bottomPadding: 4 + + required property string name + + Material.theme: Material.Dark + + background: Rectangle { + color: itemDelegate.Material.primary + radius: 3 + } + } + model: EventModel { + eventDatabase: root.eventDatabase + date: new Date(root.year, root.month, root.day) + } + } +} diff --git a/examples/quickcontrols2/eventcalendar/doc/images/qtquickcalendar-eventcalendar.png b/examples/quickcontrols2/eventcalendar/doc/images/qtquickcalendar-eventcalendar.png Binary files differnew file mode 100644 index 0000000000..35d905be6d --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/doc/images/qtquickcalendar-eventcalendar.png diff --git a/examples/quickcontrols2/eventcalendar/doc/src/qtquickcontrols2-eventcalendar.qdoc b/examples/quickcontrols2/eventcalendar/doc/src/qtquickcontrols2-eventcalendar.qdoc new file mode 100644 index 0000000000..1fe63d2978 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/doc/src/qtquickcontrols2-eventcalendar.qdoc @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://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 https://www.qt.io/terms-conditions. For further +** information use the contact form at https://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: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example eventcalendar + \keyword Qt Quick Controls - Event Calendar + \title Qt Quick Controls - Event Calendar + \keyword Qt Quick Controls - Event Calendar + \ingroup qtquickcontrols2-examples + \brief A simple event calendar. + + \image qtquickcontrols2-gallery-menu.png + + The Event Calendar example is a simple application that shows how to use + \l DayOfWeekRow, \l WeekNumberColumn, and \l MonthGrid to create a calendar. + It uses an in-memory SQL database to populate the calendar with events. + The example uses the \l {Material Style}{Material style}. + + \include examples-run.qdocinc +*/ diff --git a/examples/quickcontrols2/eventcalendar/event.h b/examples/quickcontrols2/eventcalendar/event.h new file mode 100644 index 0000000000..77696b0a4c --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/event.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 EVENT_H +#define EVENT_H + +#include <QAbstractListModel> +#include <QDateTime> + +struct Event { + QString name; + QDateTime startDate; + QDateTime endDate; +}; + +#endif // EVENT_H diff --git a/examples/quickcontrols2/eventcalendar/eventcalendar.cpp b/examples/quickcontrols2/eventcalendar/eventcalendar.cpp new file mode 100644 index 0000000000..d7c045f366 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/eventcalendar.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 <QGuiApplication> +#include <QQmlApplicationEngine> +#include <QIcon> + +#include "sqleventdatabase.h" + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QIcon::setThemeName("eventcalendar"); + + QQmlApplicationEngine engine; + SqlEventDatabase eventDatabase; + engine.setInitialProperties({{ "eventDatabase", QVariant::fromValue(&eventDatabase) }}); + + const QUrl url(QStringLiteral("qrc:/eventcalendar.qml")); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, + &app, [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/examples/quickcontrols2/eventcalendar/eventcalendar.pro b/examples/quickcontrols2/eventcalendar/eventcalendar.pro new file mode 100644 index 0000000000..742c5d09e8 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/eventcalendar.pro @@ -0,0 +1,25 @@ +TEMPLATE = app +TARGET = eventcalendar +QT += sql qml + +CONFIG += qmltypes +QML_IMPORT_NAME = App +QML_IMPORT_MAJOR_VERSION = 1 + +HEADERS += \ + event.h \ + eventmodel.h \ + sqleventdatabase.h + +SOURCES += \ + eventcalendar.cpp \ + eventmodel.cpp \ + sqleventdatabase.cpp + +RESOURCES += \ + eventcalendar.qml \ + MonthGridDelegate.qml \ + qtquickcontrols2.conf + +target.path = $$[QT_INSTALL_EXAMPLES]/quickcalendar/eventcalendar +INSTALLS += target diff --git a/examples/quickcontrols2/eventcalendar/eventcalendar.qml b/examples/quickcontrols2/eventcalendar/eventcalendar.qml new file mode 100644 index 0000000000..9e2cce1823 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/eventcalendar.qml @@ -0,0 +1,160 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Controls.Material +import QtQuick.Layouts + +import App + +ApplicationWindow { + id: window + width: 800 + height: 600 + title: qsTr("Event Calendar") + visible: true + + required property EventDatabase eventDatabase + + readonly property date currentDate: new Date() + + header: ToolBar { + Label { + text: window.currentDate.toLocaleString(locale, "MMMM yyyy") + font.pixelSize: Qt.application.font.pixelSize * 1.25 + anchors.centerIn: parent + } + } + + GridLayout { + anchors.fill: parent + columns: 2 + + DayOfWeekRow { + id: dayOfWeekRow + locale: grid.locale + font.bold: false + delegate: Label { + text: model.shortName + font: dayOfWeekRow.font + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + } + + Layout.column: 1 + Layout.fillWidth: true + } + + WeekNumberColumn { + month: grid.month + year: grid.year + locale: grid.locale + font.bold: false + + Layout.fillHeight: true + } + + MonthGrid { + id: grid + month: window.currentDate.getMonth() + year: window.currentDate.getFullYear() + spacing: 0 + + readonly property int gridLineThickness: 1 + + Layout.fillWidth: true + Layout.fillHeight: true + + delegate: MonthGridDelegate { + id: gridDelegate + visibleMonth: grid.month + eventDatabase: window.eventDatabase + } + + background: Item { + x: grid.leftPadding + y: grid.topPadding + width: grid.availableWidth + height: grid.availableHeight + + // Vertical lines + Row { + spacing: (parent.width - (grid.gridLineThickness * rowRepeater.model)) / rowRepeater.model + + Repeater { + id: rowRepeater + model: 7 + delegate: Rectangle { + width: 1 + height: grid.height + color: "#ccc" + } + } + } + + // Horizontal lines + Column { + spacing: (parent.height - (grid.gridLineThickness * columnRepeater.model)) / columnRepeater.model + + Repeater { + id: columnRepeater + model: 6 + delegate: Rectangle { + width: grid.width + height: 1 + color: "#ccc" + } + } + } + } + } + } +} diff --git a/examples/quickcontrols2/eventcalendar/eventmodel.cpp b/examples/quickcontrols2/eventcalendar/eventmodel.cpp new file mode 100644 index 0000000000..48fdc24065 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/eventmodel.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 "eventmodel.h" + +#include "sqleventdatabase.h" + +EventModel::EventModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +SqlEventDatabase *EventModel::eventDatabase() +{ + return m_eventDatabase; +} + +void EventModel::setEventDatabase(SqlEventDatabase *eventDatabase) +{ + if (eventDatabase == m_eventDatabase) + return; + + m_eventDatabase = eventDatabase; + repopulate(); + emit eventDatabaseChanged(); +} + +QDate EventModel::date() const +{ + return m_date; +} + +void EventModel::setDate(const QDate &date) +{ + if (date == m_date) + return; + + m_date = date; + repopulate(); + emit dateChanged(); +} + +int EventModel::rowCount(const QModelIndex &) const +{ + return m_events.size(); +} + +QVariant EventModel::data(const QModelIndex &index, int role) const +{ + if (!checkIndex(index, CheckIndexOption::IndexIsValid)) + return QVariant(); + + switch (role) { + case NameRole: return m_events.at(index.row()).name; + case StartDateRole: return m_events.at(index.row()).startDate; + case EndDateRole: return m_events.at(index.row()).endDate; + default: return QVariant(); + } +} + +QHash<int, QByteArray> EventModel::roleNames() const +{ + static const QHash<int, QByteArray> roles { + { NameRole, "name" }, + { StartDateRole, "startDate" }, + { EndDateRole, "endDate" } + }; + return roles; +} + +bool EventModel::isValid() const +{ + return m_eventDatabase && !m_date.isNull(); +} + +void EventModel::repopulate() +{ + beginResetModel(); + + if (!m_eventDatabase || m_date.isNull()) { + m_events.clear(); + return; + } + + m_events = m_eventDatabase->eventsForDate(m_date); + + endResetModel(); +} diff --git a/examples/quickcontrols2/eventcalendar/eventmodel.h b/examples/quickcontrols2/eventcalendar/eventmodel.h new file mode 100644 index 0000000000..ab948c7d4e --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/eventmodel.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 CONTACTMODEL_H +#define CONTACTMODEL_H + +#include <QAbstractListModel> +#include <QDate> +#include <QtQml> + +#include "event.h" + +class SqlEventDatabase; + +class EventModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(SqlEventDatabase *eventDatabase READ eventDatabase WRITE setEventDatabase NOTIFY eventDatabaseChanged) + Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY dateChanged) + QML_ELEMENT + Q_MOC_INCLUDE("sqleventdatabase.h") + +public: + EventModel(QObject *parent = nullptr); + + enum ContactRole { + NameRole = Qt::UserRole, + StartDateRole, + EndDateRole + }; + Q_ENUM(ContactRole) + + SqlEventDatabase *eventDatabase(); + void setEventDatabase(SqlEventDatabase *eventDatabase); + + QDate date() const; + void setDate(const QDate &date); + + int rowCount(const QModelIndex & = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + +signals: + void eventDatabaseChanged(); + void dateChanged(); + +private: + bool isValid() const; + void repopulate(); + + SqlEventDatabase *m_eventDatabase = nullptr; + QDate m_date; + QVector<Event> m_events; +}; + +#endif // CONTACTMODEL_H diff --git a/examples/quickcontrols2/eventcalendar/qmldir b/examples/quickcontrols2/eventcalendar/qmldir new file mode 100644 index 0000000000..6b7f86bfac --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/qmldir @@ -0,0 +1 @@ +module App diff --git a/examples/quickcontrols2/eventcalendar/qtquickcontrols2.conf b/examples/quickcontrols2/eventcalendar/qtquickcontrols2.conf new file mode 100644 index 0000000000..b6c7c87ea1 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/qtquickcontrols2.conf @@ -0,0 +1,2 @@ +[Controls] +Style=Material diff --git a/examples/quickcontrols2/eventcalendar/sqleventdatabase.cpp b/examples/quickcontrols2/eventcalendar/sqleventdatabase.cpp new file mode 100644 index 0000000000..3ad0621c18 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/sqleventdatabase.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 "sqleventdatabase.h" + +#include <QDebug> +#include <QFileInfo> +#include <QSqlError> +#include <QSqlQuery> + +SqlEventDatabase::SqlEventDatabase() +{ + createConnection(); +} + +QVector<Event> SqlEventDatabase::eventsForDate(const QDate &date) +{ + const QString queryStr = QString::fromLatin1("SELECT * FROM Event WHERE '%1' >= startDate AND '%1' <= endDate").arg(date.toString("yyyy-MM-dd")); + QSqlQuery query(queryStr); + if (!query.exec()) { + qWarning() << "SQL query failed"; + return {}; + } + + QVector<Event> events; + while (query.next()) { + Event event; + event.name = query.value("name").toString(); + + QDateTime startDate; + startDate.setDate(query.value("startDate").toDate()); + startDate.setTime(QTime(0, 0).addSecs(query.value("startTime").toInt())); + event.startDate = startDate; + + QDateTime endDate; + endDate.setDate(query.value("endDate").toDate()); + endDate.setTime(QTime(0, 0).addSecs(query.value("endTime").toInt())); + event.endDate = endDate; + + events.append(event); + } + return events; +} + +/* + Defines a helper function to open a connection to an + in-memory SQLITE database and to create a table. +*/ +void SqlEventDatabase::createConnection() +{ + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); + db.setDatabaseName(":memory:"); + if (!db.open()) { + qFatal("Cannot open database"); + return; + } + + QSqlQuery query; + const QString year = QDate::currentDate().toString("yyyy"); + const QString month = QDate::currentDate().toString("MM"); + // Keep the example up-to-date by making the events fall in the current year and month. + // We store the time as seconds because it's easier to query. + query.exec("create table Event (name TEXT, startDate DATE, startTime INT, endDate DATE, endTime INT)"); + query.exec(QString::fromLatin1("insert into Event values('Grocery shopping', '%1-%2-01', 36000, '%1-%2-01', 39600)").arg(year, month)); + query.exec(QString::fromLatin1("insert into Event values('Ice skating', '%1-%2-01', 57600, '%1-%2-01', 61200)").arg(year, month)); + query.exec(QString::fromLatin1("insert into Event values('Doctor''s appointment', '%1-%2-15', 57600, '%1-%2-15', 63000)").arg(year, month)); + query.exec(QString::fromLatin1("insert into Event values('Conference', '%1-%2-24', 32400, '%1-%2-28', 61200)").arg(year, month)); + + return; +} diff --git a/examples/quickcontrols2/eventcalendar/sqleventdatabase.h b/examples/quickcontrols2/eventcalendar/sqleventdatabase.h new file mode 100644 index 0000000000..81f770ef84 --- /dev/null +++ b/examples/quickcontrols2/eventcalendar/sqleventdatabase.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2021 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 SQLEVENTDATABASE_H +#define SQLEVENTDATABASE_H + +#include <QObject> +#include <QtQml> +#include <QVector> + +#include "event.h" + +class SqlEventDatabase : public QObject +{ + Q_OBJECT + QML_NAMED_ELEMENT(EventDatabase) + QML_UNCREATABLE("EventDatabase should not be created in QML") + +public: + SqlEventDatabase(); + + QVector<Event> eventsForDate(const QDate &date); + +private: + static void createConnection(); +}; + +#endif // SQLEVENTDATABASE_H diff --git a/examples/quickcontrols2/quickcontrols2.pro b/examples/quickcontrols2/quickcontrols2.pro index a9784d8620..b7fc57bca6 100644 --- a/examples/quickcontrols2/quickcontrols2.pro +++ b/examples/quickcontrols2/quickcontrols2.pro @@ -10,4 +10,5 @@ SUBDIRS += \ imagine/automotive \ imagine/musicplayer +qtHaveModule(sql): SUBDIRS += eventcalendar qtHaveModule(widgets): SUBDIRS += flatstyle |