| 1 |
/* poppler-optcontent.cc: qt interface to poppler |
| 2 |
* |
| 3 |
* Copyright (C) 2007, Brad Hards <[email protected]> |
| 4 |
* Copyright (C) 2008, 2014, Pino Toscano <[email protected]> |
| 5 |
* Copyright (C) 2008, Carlos Garcia Campos <[email protected]> |
| 6 |
* Copyright (C) 2015-2019, 2022, 2025, Albert Astals Cid <[email protected]> |
| 7 |
* Copyright (C) 2017, Hubert Figuière <[email protected]> |
| 8 |
* Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich |
| 9 |
* Copyright (C) 2018 Adam Reichold <[email protected]> |
| 10 |
* Copyright (C) 2019, 2020 Oliver Sander <[email protected]> |
| 11 |
* Copyright (C) 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 12 |
* |
| 13 |
* This program is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU General Public License as published by |
| 15 |
* the Free Software Foundation; either version 2, or (at your option) |
| 16 |
* any later version. |
| 17 |
* |
| 18 |
* This program is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU General Public License |
| 24 |
* along with this program; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "poppler-optcontent.h" |
| 29 |
|
| 30 |
#include "poppler-optcontent-private.h" |
| 31 |
|
| 32 |
#include "poppler-private.h" |
| 33 |
#include "poppler-link-private.h" |
| 34 |
|
| 35 |
#include <QtCore/QDebug> |
| 36 |
#include <QtCore/QtAlgorithms> |
| 37 |
|
| 38 |
#include "poppler/OptionalContent.h" |
| 39 |
#include "poppler/Link.h" |
| 40 |
|
| 41 |
namespace Poppler { |
| 42 |
|
| 43 |
RadioButtonGroup::RadioButtonGroup(OptContentModelPrivate *ocModel, Array *rbarray) |
| 44 |
{ |
| 45 |
itemsInGroup.reserve(rbarray->getLength()); |
| 46 |
for (int i = 0; i < rbarray->getLength(); ++i) { |
| 47 |
const Object &ref = rbarray->getNF(i); |
| 48 |
if (!ref.isRef()) { |
| 49 |
qDebug() << "expected ref, but got:" << ref.getType(); |
| 50 |
} |
| 51 |
OptContentItem *item = ocModel->itemFromRef(QString::number(ref.getRefNum())); |
| 52 |
itemsInGroup.append(item); |
| 53 |
} |
| 54 |
for (OptContentItem *item : std::as_const(itemsInGroup)) { |
| 55 |
item->appendRBGroup(this); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
RadioButtonGroup::~RadioButtonGroup() = default; |
| 60 |
|
| 61 |
QSet<OptContentItem *> RadioButtonGroup::setItemOn(OptContentItem *itemToSetOn) |
| 62 |
{ |
| 63 |
QSet<OptContentItem *> changedItems; |
| 64 |
for (OptContentItem *thisItem : std::as_const(itemsInGroup)) { |
| 65 |
if (thisItem != itemToSetOn) { |
| 66 |
QSet<OptContentItem *> newChangedItems; |
| 67 |
thisItem->setState(OptContentItem::Off, false /*obeyRadioGroups*/, newChangedItems); |
| 68 |
changedItems += newChangedItems; |
| 69 |
} |
| 70 |
} |
| 71 |
return changedItems; |
| 72 |
} |
| 73 |
|
| 74 |
OptContentItem::OptContentItem(OptionalContentGroup *group) |
| 75 |
{ |
| 76 |
m_group = group; |
| 77 |
m_parent = nullptr; |
| 78 |
m_name = UnicodeParsedString(group->getName()); |
| 79 |
if (group->getState() == OptionalContentGroup::On) { |
| 80 |
m_state = OptContentItem::On; |
| 81 |
} else { |
| 82 |
m_state = OptContentItem::Off; |
| 83 |
} |
| 84 |
m_stateBackup = m_state; |
| 85 |
m_enabled = true; |
| 86 |
} |
| 87 |
|
| 88 |
OptContentItem::OptContentItem(const QString &label) |
| 89 |
{ |
| 90 |
m_parent = nullptr; |
| 91 |
m_name = label; |
| 92 |
m_group = nullptr; |
| 93 |
m_state = OptContentItem::HeadingOnly; |
| 94 |
m_stateBackup = m_state; |
| 95 |
m_enabled = true; |
| 96 |
} |
| 97 |
|
| 98 |
OptContentItem::OptContentItem() : m_parent(nullptr), m_enabled(true) { } |
| 99 |
|
| 100 |
OptContentItem::~OptContentItem() = default; |
| 101 |
|
| 102 |
void OptContentItem::appendRBGroup(RadioButtonGroup *rbgroup) |
| 103 |
{ |
| 104 |
m_rbGroups.append(rbgroup); |
| 105 |
} |
| 106 |
|
| 107 |
void OptContentItem::setState(ItemState state, bool obeyRadioGroups, QSet<OptContentItem *> &changedItems) |
| 108 |
{ |
| 109 |
if (state == m_state) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
m_state = state; |
| 114 |
m_stateBackup = m_state; |
| 115 |
changedItems.insert(this); |
| 116 |
QSet<OptContentItem *> empty; |
| 117 |
Q_FOREACH (OptContentItem *child, m_children) { |
| 118 |
ItemState oldState = child->m_stateBackup; |
| 119 |
child->setState(state == OptContentItem::On ? child->m_stateBackup : OptContentItem::Off, true /*obeyRadioGroups*/, empty); |
| 120 |
child->m_enabled = state == OptContentItem::On; |
| 121 |
child->m_stateBackup = oldState; |
| 122 |
} |
| 123 |
if (!m_group) { |
| 124 |
return; |
| 125 |
} |
| 126 |
if (state == OptContentItem::On) { |
| 127 |
m_group->setState(OptionalContentGroup::On); |
| 128 |
if (obeyRadioGroups) { |
| 129 |
for (RadioButtonGroup *rbgroup : std::as_const(m_rbGroups)) { |
| 130 |
changedItems += rbgroup->setItemOn(this); |
| 131 |
} |
| 132 |
} |
| 133 |
} else if (state == OptContentItem::Off) { |
| 134 |
m_group->setState(OptionalContentGroup::Off); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void OptContentItem::addChild(OptContentItem *child) |
| 139 |
{ |
| 140 |
m_children += child; |
| 141 |
child->setParent(this); |
| 142 |
} |
| 143 |
|
| 144 |
QSet<OptContentItem *> OptContentItem::recurseListChildren(bool includeMe) const |
| 145 |
{ |
| 146 |
QSet<OptContentItem *> ret; |
| 147 |
if (includeMe) { |
| 148 |
ret.insert(const_cast<OptContentItem *>(this)); |
| 149 |
} |
| 150 |
Q_FOREACH (OptContentItem *child, m_children) { |
| 151 |
ret += child->recurseListChildren(true); |
| 152 |
} |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
OptContentModelPrivate::OptContentModelPrivate(OptContentModel *qq, const OCGs *optContent) : q(qq) |
| 157 |
{ |
| 158 |
m_rootNode = new OptContentItem(); |
| 159 |
const auto &ocgs = optContent->getOCGs(); |
| 160 |
|
| 161 |
for (const auto &ocg : ocgs) { |
| 162 |
OptContentItem *node = new OptContentItem(ocg.second.get()); |
| 163 |
m_optContentItems.insert(QString::number(ocg.first.num), node); |
| 164 |
} |
| 165 |
|
| 166 |
if (optContent->getOrderArray() == nullptr) { |
| 167 |
// no Order array, so drop them all at the top level |
| 168 |
QMapIterator<QString, OptContentItem *> i(m_optContentItems); |
| 169 |
while (i.hasNext()) { |
| 170 |
i.next(); |
| 171 |
addChild(m_rootNode, i.value()); |
| 172 |
} |
| 173 |
} else { |
| 174 |
parseOrderArray(m_rootNode, optContent->getOrderArray()); |
| 175 |
} |
| 176 |
|
| 177 |
parseRBGroupsArray(optContent->getRBGroupsArray()); |
| 178 |
} |
| 179 |
|
| 180 |
OptContentModelPrivate::~OptContentModelPrivate() |
| 181 |
{ |
| 182 |
qDeleteAll(m_optContentItems); |
| 183 |
qDeleteAll(m_rbgroups); |
| 184 |
qDeleteAll(m_headerOptContentItems); |
| 185 |
delete m_rootNode; |
| 186 |
} |
| 187 |
|
| 188 |
void OptContentModelPrivate::parseOrderArray(OptContentItem *parentNode, const Array *orderArray) |
| 189 |
{ |
| 190 |
OptContentItem *lastItem = parentNode; |
| 191 |
for (int i = 0; i < orderArray->getLength(); ++i) { |
| 192 |
Object orderItem = orderArray->get(i); |
| 193 |
if (orderItem.isDict()) { |
| 194 |
const Object &item = orderArray->getNF(i); |
| 195 |
if (item.isRef()) { |
| 196 |
OptContentItem *ocItem = m_optContentItems.value(QString::number(item.getRefNum())); |
| 197 |
if (ocItem) { |
| 198 |
addChild(parentNode, ocItem); |
| 199 |
lastItem = ocItem; |
| 200 |
} else { |
| 201 |
qDebug() << "could not find group for object" << item.getRefNum(); |
| 202 |
} |
| 203 |
} |
| 204 |
} else if ((orderItem.isArray()) && (orderItem.arrayGetLength() > 0)) { |
| 205 |
parseOrderArray(lastItem, orderItem.getArray()); |
| 206 |
} else if (orderItem.isString()) { |
| 207 |
const GooString *label = orderItem.getString(); |
| 208 |
OptContentItem *header = new OptContentItem(UnicodeParsedString(label)); |
| 209 |
m_headerOptContentItems.append(header); |
| 210 |
addChild(parentNode, header); |
| 211 |
parentNode = header; |
| 212 |
lastItem = header; |
| 213 |
} else { |
| 214 |
qDebug() << "something unexpected"; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void OptContentModelPrivate::parseRBGroupsArray(const Array *rBGroupArray) |
| 220 |
{ |
| 221 |
if (!rBGroupArray) { |
| 222 |
return; |
| 223 |
} |
| 224 |
// This is an array of array(s) |
| 225 |
for (int i = 0; i < rBGroupArray->getLength(); ++i) { |
| 226 |
Object rbObj = rBGroupArray->get(i); |
| 227 |
if (!rbObj.isArray()) { |
| 228 |
qDebug() << "expected inner array, got:" << rbObj.getType(); |
| 229 |
return; |
| 230 |
} |
| 231 |
Array *rbarray = rbObj.getArray(); |
| 232 |
RadioButtonGroup *rbg = new RadioButtonGroup(this, rbarray); |
| 233 |
m_rbgroups.append(rbg); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
OptContentModel::OptContentModel(const OCGs *optContent, QObject *parent) : QAbstractItemModel(parent) |
| 238 |
{ |
| 239 |
d = new OptContentModelPrivate(this, optContent); |
| 240 |
} |
| 241 |
|
| 242 |
OptContentModel::~OptContentModel() |
| 243 |
{ |
| 244 |
delete d; |
| 245 |
} |
| 246 |
|
| 247 |
void OptContentModelPrivate::setRootNode(OptContentItem *node) |
| 248 |
{ |
| 249 |
q->beginResetModel(); |
| 250 |
delete m_rootNode; |
| 251 |
m_rootNode = node; |
| 252 |
q->endResetModel(); |
| 253 |
} |
| 254 |
|
| 255 |
QModelIndex OptContentModel::index(int row, int column, const QModelIndex &parent) const |
| 256 |
{ |
| 257 |
if (row < 0 || column != 0) { |
| 258 |
return QModelIndex(); |
| 259 |
} |
| 260 |
|
| 261 |
OptContentItem *parentNode = d->nodeFromIndex(parent); |
| 262 |
if (row < parentNode->childList().count()) { |
| 263 |
return createIndex(row, column, parentNode->childList().at(row)); |
| 264 |
} |
| 265 |
return QModelIndex(); |
| 266 |
} |
| 267 |
|
| 268 |
QModelIndex OptContentModel::parent(const QModelIndex &child) const |
| 269 |
{ |
| 270 |
OptContentItem *childNode = d->nodeFromIndex(child); |
| 271 |
if (!childNode) { |
| 272 |
return QModelIndex(); |
| 273 |
} |
| 274 |
return d->indexFromItem(childNode->parent(), child.column()); |
| 275 |
} |
| 276 |
|
| 277 |
QModelIndex OptContentModelPrivate::indexFromItem(OptContentItem *node, int column) const |
| 278 |
{ |
| 279 |
if (!node) { |
| 280 |
return QModelIndex(); |
| 281 |
} |
| 282 |
OptContentItem *parentNode = node->parent(); |
| 283 |
if (!parentNode) { |
| 284 |
return QModelIndex(); |
| 285 |
} |
| 286 |
const int row = parentNode->childList().indexOf(node); |
| 287 |
return q->createIndex(row, column, node); |
| 288 |
} |
| 289 |
|
| 290 |
int OptContentModel::rowCount(const QModelIndex &parent) const |
| 291 |
{ |
| 292 |
OptContentItem *parentNode = d->nodeFromIndex(parent); |
| 293 |
if (!parentNode) { |
| 294 |
return 0; |
| 295 |
} else { |
| 296 |
return parentNode->childList().count(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
int OptContentModel::columnCount(const QModelIndex & /*parent*/) const |
| 301 |
{ |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
QVariant OptContentModel::data(const QModelIndex &index, int role) const |
| 306 |
{ |
| 307 |
OptContentItem *node = d->nodeFromIndex(index, true); |
| 308 |
if (!node) { |
| 309 |
return QVariant(); |
| 310 |
} |
| 311 |
|
| 312 |
switch (role) { |
| 313 |
case Qt::DisplayRole: |
| 314 |
return node->name(); |
| 315 |
break; |
| 316 |
case Qt::EditRole: |
| 317 |
if (node->state() == OptContentItem::On) { |
| 318 |
return true; |
| 319 |
} else if (node->state() == OptContentItem::Off) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
break; |
| 323 |
case Qt::CheckStateRole: |
| 324 |
if (node->state() == OptContentItem::On) { |
| 325 |
return Qt::Checked; |
| 326 |
} else if (node->state() == OptContentItem::Off) { |
| 327 |
return Qt::Unchecked; |
| 328 |
} |
| 329 |
break; |
| 330 |
} |
| 331 |
|
| 332 |
return QVariant(); |
| 333 |
} |
| 334 |
|
| 335 |
bool OptContentModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 336 |
{ |
| 337 |
OptContentItem *node = d->nodeFromIndex(index, true); |
| 338 |
if (!node) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
switch (role) { |
| 343 |
case Qt::CheckStateRole: { |
| 344 |
const bool newvalue = value.toBool(); |
| 345 |
QSet<OptContentItem *> changedItems; |
| 346 |
node->setState(newvalue ? OptContentItem::On : OptContentItem::Off, true /*obeyRadioGroups*/, changedItems); |
| 347 |
|
| 348 |
if (!changedItems.isEmpty()) { |
| 349 |
changedItems += node->recurseListChildren(false); |
| 350 |
QModelIndexList indexes; |
| 351 |
Q_FOREACH (OptContentItem *item, changedItems) { |
| 352 |
indexes.append(d->indexFromItem(item, 0)); |
| 353 |
} |
| 354 |
std::stable_sort(indexes.begin(), indexes.end()); // NOLINT(modernize-use-ranges) QModelIndexList doesn't support ranges requirements |
| 355 |
Q_FOREACH (const QModelIndex &changedIndex, indexes) { |
| 356 |
Q_EMIT dataChanged(changedIndex, changedIndex); |
| 357 |
} |
| 358 |
return true; |
| 359 |
} |
| 360 |
break; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
Qt::ItemFlags OptContentModel::flags(const QModelIndex &index) const |
| 368 |
{ |
| 369 |
OptContentItem *node = d->nodeFromIndex(index); |
| 370 |
Qt::ItemFlags itemFlags = Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; |
| 371 |
if (node->isEnabled()) { |
| 372 |
itemFlags |= Qt::ItemIsEnabled; |
| 373 |
} |
| 374 |
return itemFlags; |
| 375 |
} |
| 376 |
|
| 377 |
QVariant OptContentModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 378 |
{ |
| 379 |
return QAbstractItemModel::headerData(section, orientation, role); |
| 380 |
} |
| 381 |
|
| 382 |
void OptContentModel::applyLink(LinkOCGState *link) |
| 383 |
{ |
| 384 |
LinkOCGStatePrivate *linkPrivate = link->d_func(); |
| 385 |
|
| 386 |
QSet<OptContentItem *> changedItems; |
| 387 |
|
| 388 |
const std::vector<::LinkOCGState::StateList> &statesList = linkPrivate->stateList; |
| 389 |
for (const ::LinkOCGState::StateList &stateList : statesList) { |
| 390 |
const std::vector<Ref> &refsList = stateList.list; |
| 391 |
for (const Ref &ref : refsList) { |
| 392 |
OptContentItem *item = d->itemFromRef(QString::number(ref.num)); |
| 393 |
|
| 394 |
if (stateList.st == ::LinkOCGState::On) { |
| 395 |
item->setState(OptContentItem::On, linkPrivate->preserveRB, changedItems); |
| 396 |
} else if (stateList.st == ::LinkOCGState::Off) { |
| 397 |
item->setState(OptContentItem::Off, linkPrivate->preserveRB, changedItems); |
| 398 |
} else { |
| 399 |
OptContentItem::ItemState newState = item->state() == OptContentItem::On ? OptContentItem::Off : OptContentItem::On; |
| 400 |
item->setState(newState, linkPrivate->preserveRB, changedItems); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (!changedItems.isEmpty()) { |
| 406 |
QSet<OptContentItem *> aux; |
| 407 |
Q_FOREACH (OptContentItem *item, aux) { |
| 408 |
changedItems += item->recurseListChildren(false); |
| 409 |
} |
| 410 |
|
| 411 |
QModelIndexList indexes; |
| 412 |
Q_FOREACH (OptContentItem *item, changedItems) { |
| 413 |
indexes.append(d->indexFromItem(item, 0)); |
| 414 |
} |
| 415 |
std::stable_sort(indexes.begin(), indexes.end()); // NOLINT(modernize-use-ranges) Qt5 QModelIndexList doesn't support ranges requirements |
| 416 |
Q_FOREACH (const QModelIndex &changedIndex, indexes) { |
| 417 |
Q_EMIT dataChanged(changedIndex, changedIndex); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
void OptContentModelPrivate::addChild(OptContentItem *parent, OptContentItem *child) |
| 423 |
{ |
| 424 |
parent->addChild(child); |
| 425 |
} |
| 426 |
|
| 427 |
OptContentItem *OptContentModelPrivate::itemFromRef(const QString &ref) const |
| 428 |
{ |
| 429 |
return m_optContentItems.value(ref); |
| 430 |
} |
| 431 |
|
| 432 |
OptContentItem *OptContentModelPrivate::nodeFromIndex(const QModelIndex &index, bool canBeNull) const |
| 433 |
{ |
| 434 |
if (index.isValid()) { |
| 435 |
return static_cast<OptContentItem *>(index.internalPointer()); |
| 436 |
} else { |
| 437 |
return canBeNull ? nullptr : m_rootNode; |
| 438 |
} |
| 439 |
} |
| 440 |
} |