Qt for HarmonyOS/qtohosextras doc/Share Data Example - main.cpp
< Qt for HarmonyOS / qtohosextras doc
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QApplication>
#include <QCheckBox>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QFormLayout>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QMimeDatabase>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QScrollBar>
#include <QSharedPointer>
#include <QSpinBox>
#include <QUrl>
#include <QVBoxLayout>
#include <QWidget>
#include <QtOhosExtras>
namespace {
void setupRecord(QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> record, int recordIndex)
{
record->setDescription(QStringLiteral("Record %1 Description").arg(recordIndex));
record->setLabel(QStringLiteral("Record %1 Label").arg(recordIndex));
record->setTitle(QStringLiteral("Record %1 Title").arg(recordIndex));
QVariantMap extraData;
extraData["extraDataIntParam"] = recordIndex;
extraData["extraDataStringParam"] = QStringLiteral("Record %1 String Data").arg(recordIndex);
record->setExtraData(extraData);
}
void trySetupRecordQtThumbnail(QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> record)
{
QFile file(":/qt_logo.png");
if (file.open(QIODevice::ReadOnly)) {
record->setThumbnail(file.readAll());
file.close();
}
}
QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> makeTextRecord(int recordIndex)
{
auto record = QtOhosExtras::ShareKit::createContentRecord(
QMimeDatabase().mimeTypeForName("text/plain"),
QStringLiteral("Record %1 Content").arg(recordIndex));
setupRecord(record, recordIndex);
trySetupRecordQtThumbnail(record);
return record;
}
QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> makeUrlRecord(int recordIndex)
{
auto record = QtOhosExtras::ShareKit::createUrlRecord(QUrl("https://www.example.com"));
setupRecord(record, recordIndex);
trySetupRecordQtThumbnail(record);
return record;
}
QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord> makeFileRecord(
const QFileInfo &fileInfo, int recordIndex)
{
auto record = QtOhosExtras::ShareKit::createFileRecord(fileInfo);
setupRecord(record, recordIndex);
return record;
}
QFrame *createDivider(QWidget *parent, QFrame::Shape shape)
{
auto *divider = new QFrame(parent);
divider->setFrameShape(shape);
divider->setLineWidth(1);
return divider;
}
class DemoWindow : public QWidget
{
public:
DemoWindow(QWidget *parent = nullptr);
private:
QPushButton *createActionPushButton(const QString &label, void (DemoWindow::*actionMemFunc)());
void addTextRecord();
void addFileRecord();
void addUrlRecord();
void removeRecords();
void shareRecords();
void updateRecordsToShareLabel();
void updateControllerOptionsWidgets();
void insertOutputLogText(const QString &text);
void registerOnShareKitEvents();
QLabel *m_recordsToShareLabel;
QCheckBox *m_useControllerOptionsCheckBox;
QCheckBox *m_defaultPreviewModeCheckBox;
QCheckBox *m_singleSelectionModeCheckBox;
QSpinBox *m_anchorXSpinBox;
QSpinBox *m_anchorYSpinBox;
QPlainTextEdit *m_outputLog;
QList<QSharedPointer<QtOhosExtras::ShareKit::QOhosSharedRecord>> m_records;
};
DemoWindow::DemoWindow(QWidget *parent)
: QWidget(parent)
{
m_useControllerOptionsCheckBox = new QCheckBox(QLatin1String("Use Controller Options"), this);
connect(
m_useControllerOptionsCheckBox, &QCheckBox::stateChanged, this,
&DemoWindow::updateControllerOptionsWidgets);
m_defaultPreviewModeCheckBox = new QCheckBox(QLatin1String("Default Preview Mode"), this);
m_singleSelectionModeCheckBox = new QCheckBox(QLatin1String("Single Selection Mode"), this);
m_anchorXSpinBox = new QSpinBox(this);
m_anchorXSpinBox->setRange(0, 250);
m_anchorYSpinBox = new QSpinBox(this);
m_anchorYSpinBox->setRange(0, 250);
auto *controllerOptionsFormLayout = new QFormLayout();
controllerOptionsFormLayout->addRow(
createActionPushButton(QLatin1String("Add Text Record"), &DemoWindow::addTextRecord));
controllerOptionsFormLayout->addRow(
createActionPushButton(QLatin1String("Add File Record"), &DemoWindow::addFileRecord));
controllerOptionsFormLayout->addRow(
createActionPushButton(QLatin1String("Add Url Record"), &DemoWindow::addUrlRecord));
controllerOptionsFormLayout->addRow(
createActionPushButton(QLatin1String("Remove Records"), &DemoWindow::removeRecords));
controllerOptionsFormLayout->addRow(createDivider(this, QFrame::HLine));
controllerOptionsFormLayout->addRow(m_useControllerOptionsCheckBox);
controllerOptionsFormLayout->addRow(m_defaultPreviewModeCheckBox);
controllerOptionsFormLayout->addRow(m_singleSelectionModeCheckBox);
controllerOptionsFormLayout->addRow(QLatin1String("Anchor X"), m_anchorXSpinBox);
controllerOptionsFormLayout->addRow(QLatin1String("Anchor Y"), m_anchorYSpinBox);
m_recordsToShareLabel = new QLabel(QLatin1String(""), this);
m_outputLog = new QPlainTextEdit();
m_outputLog->setReadOnly(true);
auto *summaryLayout = new QVBoxLayout();
summaryLayout->addWidget(m_recordsToShareLabel);
summaryLayout->addWidget(createDivider(this, QFrame::HLine));
summaryLayout->addWidget(m_outputLog);
summaryLayout->addWidget(createDivider(this, QFrame::HLine));
summaryLayout->addWidget(
createActionPushButton(
QLatin1String("Share Records"), &DemoWindow::shareRecords));
auto *mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(controllerOptionsFormLayout);
mainLayout->addWidget(createDivider(this, QFrame::VLine));
mainLayout->addLayout(summaryLayout);
updateRecordsToShareLabel();
updateControllerOptionsWidgets();
registerOnShareKitEvents();
}
QPushButton *DemoWindow::createActionPushButton(const QString &label, void (DemoWindow::*actionMemFunc)())
{
auto *button = new QPushButton(label, this);
connect(button, &QPushButton::clicked, this, actionMemFunc);
return button;
}
void DemoWindow::addTextRecord()
{
m_records.append(makeTextRecord(m_records.size()));
updateRecordsToShareLabel();
}
void DemoWindow::addFileRecord()
{
auto filePath = QFileDialog::getOpenFileName(
nullptr, QLatin1String("Share File"), QDir::homePath());
if (!filePath.isEmpty()) {
m_records.append(makeFileRecord(QFileInfo(filePath), m_records.size()));
updateRecordsToShareLabel();
}
}
void DemoWindow::addUrlRecord()
{
m_records.append(makeUrlRecord(m_records.size()));
updateRecordsToShareLabel();
}
void DemoWindow::removeRecords()
{
m_records.clear();
updateRecordsToShareLabel();
}
void DemoWindow::shareRecords()
{
QSharedPointer<QtOhosExtras::ShareKit::QOhosShareControllerOptions> controllerOptions;
if (m_useControllerOptionsCheckBox->isChecked()) {
controllerOptions = QtOhosExtras::ShareKit::createControllerOptions();
controllerOptions->setSingleSelectionMode(m_singleSelectionModeCheckBox->isChecked());
controllerOptions->setDefaultPreviewMode(m_defaultPreviewModeCheckBox->isChecked());
controllerOptions->setAnchor({m_anchorXSpinBox->value(), m_anchorYSpinBox->value()});
}
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
auto requestId = abilityContext->shareDataWithShareKit(m_records, controllerOptions);
insertOutputLogText("Open ShareKit panel, request id: " + requestId.toHex());
}
void DemoWindow::updateRecordsToShareLabel()
{
m_recordsToShareLabel->setText(
QStringLiteral("Number Of Records To Share: %1").arg(m_records.size()));
}
void DemoWindow::updateControllerOptionsWidgets()
{
bool useControllerOptions = m_useControllerOptionsCheckBox->isChecked();
m_singleSelectionModeCheckBox->setEnabled(useControllerOptions);
m_defaultPreviewModeCheckBox->setEnabled(useControllerOptions);
m_anchorXSpinBox->setEnabled(useControllerOptions);
m_anchorYSpinBox->setEnabled(useControllerOptions);
}
void DemoWindow::insertOutputLogText(const QString &text)
{
auto cursor = m_outputLog->textCursor();
cursor.movePosition(QTextCursor::Start);
cursor.insertText(text + "\n");
m_outputLog->verticalScrollBar()->setValue(0);
}
void DemoWindow::registerOnShareKitEvents()
{
auto abilityContext = QtOhosExtras::QOhosAbilityContext::getDefaultInstance();
QObject::connect(
abilityContext.get(),
&QtOhosExtras::QOhosAbilityContext::shareKitPanelClosed,
this,
[this](auto requestId) {
insertOutputLogText("On ShareKit panel closed, request id: " + requestId.toHex());
});
QObject::connect(
abilityContext.get(),
&QtOhosExtras::QOhosAbilityContext::shareKitCompleted,
this,
[this](auto requestId, auto shareOperationResult) {
insertOutputLogText(
"On ShareKit completed, request id: " + requestId.toHex() + " target ability name: "
+ shareOperationResult->targetAbilityName());
});
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DemoWindow window;
window.setWindowTitle(QLatin1String("Share data example"));
window.setMinimumSize(1000, 800);
window.show();
return app.exec();
}