1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "datamodel.h"
#include "spreadrole.h"
bool DataModel::empty() const
{
return m_keys.empty();
}
std::pair<SpreadKey, SpreadKey> DataModel::clearHighlight()
{
SpreadKey top_left{INT_MAX, INT_MAX};
SpreadKey bottom_right{-1, -1};
for (auto it = m_cells.begin(); it != m_cells.end();) {
it.value().set(spread::Role::Hightlight, false);
if (it.key().first < top_left.first)
top_left.first = it.key().first;
if (it.key().second < top_left.second)
top_left.second = it.key().second;
if (bottom_right.first < it.key().first)
bottom_right.first = it.key().first;
if (bottom_right.second < it.key().second)
bottom_right.second = it.key().second;
if (it.value().isNull()) {
m_keys.remove(it.value().id);
auto cit = spread::make_const(m_cells, it);
it = m_cells.erase(cit);
} else {
++it;
}
}
return std::make_pair(top_left, bottom_right);
}
bool DataModel::setHighlight(const SpreadKey &key, bool highlight)
{
if (auto it = m_cells.find(key); it != m_cells.end()) {
it.value().set(spread::Role::Hightlight, highlight);
if (it.value().isNull()) {
m_keys.remove(it.value().id);
auto cit = spread::make_const(m_cells, it);
m_cells.erase(cit);
}
return true;
}
if (highlight) {
SpreadCell cell;
cell.set(spread::Role::Hightlight, true);
cell.id = ++lastId;
m_cells.insert(key, cell);
m_keys.insert(cell.id, key);
return true;
}
// we skipped false highlight for non-existing cell
// because we don't store cells with only false hightlight data
// to save memory
return false;
}
QVariant DataModel::getData(int id, int role) const
{
auto it_key = m_keys.find(id);
if (it_key == m_keys.end())
return QVariant{};
const SpreadKey &key = it_key.value();
auto it_cell = m_cells.find(key);
if (it_cell == m_cells.end())
return QVariant{};
return it_cell.value().get(role);
}
QVariant DataModel::getData(const SpreadKey &key, int role) const
{
auto it = m_cells.find(key);
return it == m_cells.end() ? QVariant{} : it.value().get(role);
}
bool DataModel::setData(const SpreadKey &key, const QVariant &value, int role)
{
// special roles
switch (role) {
case spread::Role::Hightlight:
return setHighlight(key, value.toBool());
default: break;
}
// no special handling for the role
if (auto it = m_cells.find(key); it != m_cells.end()) {
it.value().set(role, value);
if (it.value().isNull()) {
clearData(key);
return true;
}
} else {
SpreadCell cell;
cell.set(role, value);
cell.id = ++lastId;
if (!cell.isNull()) {
m_cells.insert(key, cell);
m_keys.insert(cell.id, key);
}
}
return true;
}
bool DataModel::clearData(const SpreadKey &key)
{
auto find_key = [&key](const auto &i) { return i == key; };
auto it = std::find_if(m_keys.cbegin(), m_keys.cend(), find_key);
if (it == m_keys.cend())
return 0;
m_keys.erase(it);
return m_cells.remove(key) > 0;
}
void DataModel::shiftColumns(int from, int count)
{
if (count > 0) {
// the reason for reverse iteration is because of the coverage of
// the updated keys (bigger keys) and existing keys (next keys)
QMapIterator i(m_cells);
i.toBack();
while (i.hasPrevious()) {
i.previous();
if (i.key().second >= from) {
SpreadKey key = i.key();
SpreadCell cell = i.value();
m_cells.remove(key);
key.second += count;
m_cells.insert(key, cell);
}
}
} else if (count < 0) {
// the reason for normal iteration is because of the coverage of
// the updated keys (smaller keys) and existing keys (previous keys)
for (auto it = m_cells.begin(); it != m_cells.end(); ++it) {
if (it.key().second >= from) {
SpreadKey key = it.key();
SpreadCell cell = it.value();
m_cells.remove(key);
key.second += count;
m_cells.insert(key, cell);
}
}
}
if (count != 0) {
for (auto it = m_keys.begin(); it != m_keys.end(); ++it) {
SpreadKey &key = it.value();
if (key.second >= from)
key.second += count;
}
}
}
void DataModel::removeColumnCells(int column)
{
for (auto it = m_cells.begin(); it != m_cells.end(); ) {
if (it.key().second == column) {
auto cit = spread::make_const(m_cells, it);
it = m_cells.erase(cit);
} else {
++it;
}
}
for (auto it = m_keys.begin(); it != m_keys.end(); ) {
if (it.value().second == column) {
auto cit = spread::make_const(m_keys, it);
it = m_keys.erase(cit);
} else {
++it;
}
}
}
void DataModel::shiftRows(int from, int count)
{
if (count > 0) {
// the reason for reverse iteration is because of the coverage of
// the updated keys (bigger keys) and existing keys (next keys)
QMapIterator i(m_cells);
i.toBack();
while (i.hasPrevious()) {
i.previous();
if (i.key().first < from)
break;
SpreadKey key = i.key();
SpreadCell cell = i.value();
m_cells.remove(key);
key.first += count;
m_cells.insert(key, cell);
}
} else if (count < 0) {
// the reason for normal iteration is because of the coverage of
// the updated keys (smaller keys) and existing keys (previous keys)
for (auto it = m_cells.begin(); it != m_cells.end(); ++it) {
if (it.key().first >= from) {
SpreadKey key = it.key();
SpreadCell cell = it.value();
m_cells.remove(key);
key.first += count;
m_cells.insert(key, cell);
}
}
}
if (count != 0) {
for (auto it = m_keys.begin(); it != m_keys.end(); ++it) {
SpreadKey &key = it.value();
if (key.first >= from)
key.first += count;
}
}
}
void DataModel::removeRowCells(int row)
{
for (auto it = m_cells.begin(); it != m_cells.end(); ) {
if (it.key().first == row) {
auto cit = spread::make_const(m_cells, it);
it = m_cells.erase(cit);
} else {
++it;
}
}
for (auto it = m_keys.begin(); it != m_keys.end(); ) {
if (it.value().first == row) {
auto cit = spread::make_const(m_keys, it);
it = m_keys.erase(cit);
} else {
++it;
}
}
}
int DataModel::createId(const SpreadKey &key)
{
auto find_key = [&key](const auto &elem) { return key == elem; };
auto it = std::find_if(m_keys.begin(), m_keys.end(), find_key);
if (it != m_keys.end())
return it.key();
const int id = ++lastId;
m_keys.insert(id, key);
return id;
}
int DataModel::getId(const SpreadKey &key) const
{
auto find_key = [&key](const auto &elem) { return key == elem; };
auto it = std::find_if(m_keys.begin(), m_keys.end(), find_key);
if (it == m_keys.end())
return 0;
return it.key();
}
SpreadKey DataModel::getKey(int id) const
{
auto it = m_keys.find(id);
return it == m_keys.end() ? SpreadKey{-1, -1} : it.value();
}
|