Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
// Copyright (C) 2024 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#include "spreadmimedataprovider.h"
|
|
|
|
|
|
|
|
#include <QMimeData>
|
2024-07-23 12:05:51 +00:00
|
|
|
#ifndef QT_NO_CLIPBOARD
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QClipboard>
|
2024-07-23 12:05:51 +00:00
|
|
|
#endif
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
static inline constexpr auto MIMETYPE_SPREADMODEL = "application/x-qtexamplespreadmodel";
|
|
|
|
static inline constexpr auto MIMETYPE_SELECTEDDATA = "model/data";
|
|
|
|
static inline constexpr auto MIMETYPE_TEXT = "text/plain";
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData *SpreadMimeDataProvider::saveToMimeData() const
|
|
|
|
{
|
|
|
|
if (m_data.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
QDataStream stream{&data, QDataStream::WriteOnly};
|
|
|
|
for (auto it = m_data.begin(); it != m_data.end(); ++it) {
|
|
|
|
const QPoint &cell = it->first;
|
|
|
|
const QMap<int, QVariant> &item_data = it->second;
|
|
|
|
stream << cell.x() << cell.y() << item_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData *mime_data = new QMimeData{};
|
|
|
|
mime_data->setData(MIMETYPE_SPREADMODEL, QByteArray{});
|
|
|
|
mime_data->setData(MIMETYPE_SELECTEDDATA, data);
|
|
|
|
return mime_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpreadMimeDataProvider::loadFromMimeData(const QMimeData *mimeData)
|
|
|
|
{
|
|
|
|
if (!mimeData)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mimeData->hasFormat(MIMETYPE_SPREADMODEL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QByteArray data = mimeData->data(MIMETYPE_SELECTEDDATA);
|
|
|
|
QDataStream stream{&data, QDataStream::ReadOnly};
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
QPoint cell;
|
|
|
|
QMap<int, QVariant> item_data;
|
|
|
|
stream >> cell.rx() >> cell.ry() >> item_data;
|
|
|
|
m_data.push_back(std::make_pair(cell, item_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpreadMimeDataProvider::saveToClipboard()
|
|
|
|
{
|
2024-07-23 12:05:51 +00:00
|
|
|
#ifdef QT_NO_CLIPBOARD
|
|
|
|
qWarning() << "Clipboard is not supported";
|
|
|
|
return false;
|
|
|
|
#else
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
QMimeData *mime_data = saveToMimeData();
|
|
|
|
if (!mime_data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QGuiApplication::clipboard()->setMimeData(mime_data);
|
|
|
|
return true;
|
2024-07-23 12:05:51 +00:00
|
|
|
#endif
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpreadMimeDataProvider::loadFromClipboard()
|
|
|
|
{
|
2024-07-23 12:05:51 +00:00
|
|
|
#ifdef QT_NO_CLIPBOARD
|
|
|
|
qWarning() << "Clipboard is not supported";
|
|
|
|
return false;
|
|
|
|
#else
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
const QMimeData *mime_data = QGuiApplication::clipboard()->mimeData();
|
|
|
|
if (!mime_data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return loadFromMimeData(mime_data);
|
2024-07-23 12:05:51 +00:00
|
|
|
#endif
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpreadMimeDataProvider::saveDataToModel(int index,
|
|
|
|
const QModelIndex &modelIndex,
|
|
|
|
QAbstractItemModel *model) const
|
|
|
|
{
|
|
|
|
const QMap<int, QVariant> &item_data = m_data.at(index).second;
|
2024-08-12 13:51:15 +00:00
|
|
|
return item_data.isEmpty() ? model->clearItemData(modelIndex)
|
|
|
|
: model->setItemData(modelIndex, item_data);
|
Add the Spreadsheet example
The example demonstrates a Spreadsheet that provides adding, editing,
and deleting data, and also the ability to write formulas for numeric
data. Also, it's possible to select cells, rows, and columns for
deleting them or their data, copying or cutting the data, and dragging
them to other places. The user can hide columns or rows, and also show
them again. Thanks to the reordering API, columns and rows can be
reordered and also can be reset to the default order.
There is a SpreadModel class which handles the entered data. It only
stores the data of the cells that is provided by the user. It means
that it does not create any empty data structure for empty cells, in
order to reduce memory usage.
Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: I1d9cc5b4b8d902257e9ed508d4a712b0574490f3
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
2024-05-27 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpreadMimeDataProvider::loadDataFromModel(const QPoint &cell,
|
|
|
|
const QModelIndex &index,
|
|
|
|
const QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
const QMap<int, QVariant> &item_data = model->itemData(index);
|
|
|
|
m_data.push_back(std::make_pair(cell, item_data));
|
|
|
|
}
|