diff --git a/Pdf4QtDocPageOrganizer/mainwindow.cpp b/Pdf4QtDocPageOrganizer/mainwindow.cpp index fa82f59..fa74f57 100644 --- a/Pdf4QtDocPageOrganizer/mainwindow.cpp +++ b/Pdf4QtDocPageOrganizer/mainwindow.cpp @@ -451,7 +451,13 @@ void MainWindow::performOperation(Operation operation) } case Operation::RotateLeft: + m_model->rotateLeft(ui->documentItemsView->selectionModel()->selection().indexes()); + break; + case Operation::RotateRight: + m_model->rotateRight(ui->documentItemsView->selectionModel()->selection().indexes()); + break; + case Operation::ReplaceSelection: Q_ASSERT(false); break; diff --git a/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp b/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp index 40aaa21..39a7689 100644 --- a/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp +++ b/Pdf4QtDocPageOrganizer/pageitemdelegate.cpp @@ -58,7 +58,8 @@ void PageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& opti if (!item->groups.empty()) { const PageGroupItem::GroupItem& groupItem = item->groups.front(); - QSize pageImageSize = groupItem.rotatedPageDimensionsMM.scaled(pageBoundingRect.size(), Qt::KeepAspectRatio).toSize(); + QSizeF rotatedPageSize = pdf::PDFPage::getRotatedBox(QRectF(QPointF(0, 0), groupItem.rotatedPageDimensionsMM), groupItem.pageAdditionalRotation).size(); + QSize pageImageSize = rotatedPageSize.scaled(pageBoundingRect.size(), Qt::KeepAspectRatio).toSize(); QRect pageImageRect(pageBoundingRect.topLeft() + QPoint((pageBoundingRect.width() - pageImageSize.width()) / 2, (pageBoundingRect.height() - pageImageSize.height()) / 2), pageImageSize); painter->setPen(QPen(Qt::black)); diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp index 6ed051d..d6a4cc9 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp @@ -344,6 +344,60 @@ void PageItemModel::removeSelection(const QModelIndexList& list) endResetModel(); } +void PageItemModel::rotateLeft(const QModelIndexList& list) +{ + if (list.isEmpty()) + { + return; + } + + int rowMin = list.front().row(); + int rowMax = list.front().row(); + + for (const QModelIndex& index : list) + { + if (PageGroupItem* item = getItem(index)) + { + item->rotateLeft(); + } + + rowMin = qMin(rowMin, index.row()); + rowMax = qMax(rowMax, index.row()); + } + + rowMin = qMax(rowMin, 0); + rowMax = qMin(rowMax, rowCount(QModelIndex()) - 1); + + emit dataChanged(index(rowMin, 0, QModelIndex()), index(rowMax, 0, QModelIndex())); +} + +void PageItemModel::rotateRight(const QModelIndexList& list) +{ + if (list.isEmpty()) + { + return; + } + + int rowMin = list.front().row(); + int rowMax = list.front().row(); + + for (const QModelIndex& index : list) + { + if (PageGroupItem* item = getItem(index)) + { + item->rotateRight(); + } + + rowMin = qMin(rowMin, index.row()); + rowMax = qMax(rowMax, index.row()); + } + + rowMin = qMax(rowMin, 0); + rowMax = qMin(rowMax, rowCount(QModelIndex()) - 1); + + emit dataChanged(index(rowMin, 0, QModelIndex()), index(rowMax, 0, QModelIndex())); +} + QItemSelection PageItemModel::getSelectionImpl(std::function filter) const { QItemSelection result; @@ -463,6 +517,22 @@ std::set PageGroupItem::getDocumentIndices() const return result; } +void PageGroupItem::rotateLeft() +{ + for (auto& groupItem : groups) + { + groupItem.pageAdditionalRotation = pdf::getPageRotationRotatedLeft(groupItem.pageAdditionalRotation); + } +} + +void PageGroupItem::rotateRight() +{ + for (auto& groupItem : groups) + { + groupItem.pageAdditionalRotation = pdf::getPageRotationRotatedRight(groupItem.pageAdditionalRotation); + } +} + QStringList PageItemModel::mimeTypes() const { return { getMimeDataType() }; diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.h b/Pdf4QtDocPageOrganizer/pageitemmodel.h index bac2e3c..0e2b1b8 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.h +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.h @@ -39,6 +39,7 @@ struct PageGroupItem int documentIndex = 0; pdf::PDFInteger pageIndex; QSizeF rotatedPageDimensionsMM; + pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; }; std::vector groups; @@ -48,6 +49,9 @@ struct PageGroupItem bool isGrouped() const { return groups.size() > 1; } std::set getDocumentIndices() const; + + void rotateLeft(); + void rotateRight(); }; struct DocumentItem @@ -119,6 +123,9 @@ public: QModelIndexList cloneSelection(const QModelIndexList& list); void removeSelection(const QModelIndexList& list); + void rotateLeft(const QModelIndexList& list); + void rotateRight(const QModelIndexList& list); + static QString getMimeDataType() { return QLatin1String("application/pagemodel.PDF4QtDocPageOrganizer"); } private: