PDF4QT/Pdf4QtDocPageOrganizer/pageitemmodel.h

173 lines
6.0 KiB
C
Raw Normal View History

2021-07-06 18:35:29 +02:00
// Copyright (C) 2021 Jakub Melka
//
// This file is part of Pdf4Qt.
//
// Pdf4Qt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// with the written consent of the copyright owner, any later version.
//
// Pdf4Qt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
#ifndef PDFDOCPAGEORGANIZER_PAGEITEMMODEL_H
#define PDFDOCPAGEORGANIZER_PAGEITEMMODEL_H
#include "pdfdocument.h"
#include "pdfutils.h"
2021-07-22 20:05:16 +02:00
#include <QImage>
2021-07-06 18:35:29 +02:00
#include <QAbstractItemModel>
namespace pdfdocpage
{
2021-07-16 18:43:34 +02:00
enum PageType
{
PT_DocumentPage,
PT_Image,
PT_Empty
};
2021-07-06 18:35:29 +02:00
struct PageGroupItem
{
QString groupName;
QString pagesCaption;
QStringList tags;
struct GroupItem
{
2021-07-09 19:50:00 +02:00
auto operator<=>(const GroupItem&) const = default;
2021-07-16 18:43:34 +02:00
int documentIndex = -1;
pdf::PDFInteger pageIndex = -1;
pdf::PDFInteger imageIndex = -1;
2021-07-06 18:35:29 +02:00
QSizeF rotatedPageDimensionsMM;
2021-07-15 19:44:44 +02:00
pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None;
2021-07-16 18:43:34 +02:00
PageType pageType = PT_DocumentPage;
2021-07-06 18:35:29 +02:00
};
std::vector<GroupItem> groups;
2021-07-08 13:48:27 +02:00
2021-07-09 19:50:00 +02:00
auto operator<=>(const PageGroupItem&) const = default;
2021-07-08 13:48:27 +02:00
bool isGrouped() const { return groups.size() > 1; }
2021-07-09 19:50:00 +02:00
std::set<int> getDocumentIndices() const;
2021-07-15 19:44:44 +02:00
void rotateLeft();
void rotateRight();
2021-07-06 18:35:29 +02:00
};
struct DocumentItem
{
QString fileName;
pdf::PDFDocument document;
};
2021-07-22 20:05:16 +02:00
struct ImageItem
{
QImage image;
QByteArray imageData;
};
2021-07-06 18:35:29 +02:00
class PageItemModel : public QAbstractItemModel
{
Q_OBJECT
2021-07-11 17:17:13 +02:00
private:
using BaseClass = QAbstractItemModel;
2021-07-06 18:35:29 +02:00
public:
explicit PageItemModel(QObject* parent);
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
virtual QModelIndex parent(const QModelIndex& index) const override;
virtual int rowCount(const QModelIndex& parent) const override;
virtual int columnCount(const QModelIndex& parent) const override;
virtual QVariant data(const QModelIndex& index, int role) const override;
2021-07-11 17:17:13 +02:00
virtual QStringList mimeTypes() const override;
virtual QMimeData* mimeData(const QModelIndexList& indexes) const override;
virtual bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const override;
virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
virtual Qt::DropActions supportedDropActions() const override;
virtual Qt::DropActions supportedDragActions() const override;
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
2021-07-06 18:35:29 +02:00
2021-07-11 18:18:34 +02:00
/// Clear all data and undo/redo
void clear();
2021-07-06 18:35:29 +02:00
/// Adds document to the model, inserts one single page group containing
/// the whole document. Returns index of a newly added document. If document
/// cannot be added (for example, it already exists), -1 is returned.
/// \param fileName File name
/// \param document Document
/// \returns Identifier of the document (internal index)
int addDocument(QString fileName, pdf::PDFDocument document);
2021-07-22 20:05:16 +02:00
/// Adds image to the model, inserts one single page containing
/// the image. Returns index of a newly added image. If image
/// cannot be read from the file, -1 is returned.
/// \param fileName Image file
/// \param index Index, where image is inserted
/// \returns Identifier of the image (internal index)
int insertImage(QString fileName, const QModelIndex& index);
2021-07-07 18:30:03 +02:00
/// Returns item at a given index. If item doesn't exist,
/// then nullptr is returned.
/// \param index Index
const PageGroupItem* getItem(const QModelIndex& index) const;
2021-07-09 19:50:00 +02:00
/// Returns item at a given index. If item doesn't exist,
/// then nullptr is returned.
/// \param index Index
PageGroupItem* getItem(const QModelIndex& index);
2021-07-08 13:48:27 +02:00
/// Returns true, if grouped item exists in the indices
bool isGrouped(const QModelIndexList& indices) const;
2021-07-09 19:50:00 +02:00
/// Returns true, if trash bin is empty
bool isTrashBinEmpty() const { return m_trashBin.empty(); }
QItemSelection getSelectionEven() const;
QItemSelection getSelectionOdd() const;
QItemSelection getSelectionPortrait() const;
QItemSelection getSelectionLandscape() const;
2021-07-09 19:50:00 +02:00
void group(const QModelIndexList& list);
void ungroup(const QModelIndexList& list);
QModelIndexList restoreRemovedItems();
QModelIndexList cloneSelection(const QModelIndexList& list);
void removeSelection(const QModelIndexList& list);
2021-07-16 18:43:34 +02:00
void insertEmptyPage(const QModelIndexList& list);
2021-07-15 19:44:44 +02:00
void rotateLeft(const QModelIndexList& list);
void rotateRight(const QModelIndexList& list);
2021-07-11 17:17:13 +02:00
static QString getMimeDataType() { return QLatin1String("application/pagemodel.PDF4QtDocPageOrganizer"); }
2021-07-06 18:35:29 +02:00
private:
void createDocumentGroup(int index);
QString getGroupNameFromDocument(int index) const;
2021-07-09 19:50:00 +02:00
void updateItemCaptionAndTags(PageGroupItem& item) const;
2021-07-16 18:43:34 +02:00
void insertEmptyPage(const QModelIndex& index);
2021-07-09 19:50:00 +02:00
QItemSelection getSelectionImpl(std::function<bool(const PageGroupItem::GroupItem&)> filter) const;
2021-07-06 18:35:29 +02:00
std::vector<PageGroupItem> m_pageGroupItems;
std::map<int, DocumentItem> m_documents;
2021-07-22 20:05:16 +02:00
std::map<int, ImageItem> m_images;
2021-07-09 19:50:00 +02:00
std::vector<PageGroupItem> m_trashBin;
2021-07-06 18:35:29 +02:00
};
} // namespace pdfdocpage
#endif // PDFDOCPAGEORGANIZER_PAGEITEMMODEL_H