Some basic action functionality for application DocPage Organizer

This commit is contained in:
Jakub Melka 2021-07-08 17:55:34 +02:00
parent 461f4544a3
commit c8be46a7b3
8 changed files with 243 additions and 5 deletions

View File

@ -69,6 +69,7 @@ MainWindow::MainWindow(QWidget* parent) :
ui->actionSeparate_to_Multiple_Documents_Grouped->setData(int(Operation::SeparateGrouped));
QToolBar* mainToolbar = addToolBar(tr("Main"));
mainToolbar->setObjectName("main_toolbar");
mainToolbar->addAction(ui->actionAddDocument);
mainToolbar->addSeparator();
mainToolbar->addActions({ ui->actionCloneSelection, ui->actionRemoveSelection });
@ -77,16 +78,21 @@ MainWindow::MainWindow(QWidget* parent) :
mainToolbar->addSeparator();
mainToolbar->addActions({ ui->actionGroup, ui->actionUngroup });
QToolBar* insertToolbar = addToolBar(tr("Insert"));
insertToolbar->setObjectName("insert_toolbar");
insertToolbar->addActions({ ui->actionInsert_Page_from_PDF, ui->actionInsert_Image, ui->actionInsert_Empty_Page });
QToolBar* selectToolbar = addToolBar(tr("Select"));
selectToolbar->setObjectName("select_toolbar");
selectToolbar->addActions({ ui->actionSelect_None, ui->actionSelect_All, ui->actionSelect_Even, ui->actionSelect_Odd, ui->actionSelect_Portrait, ui->actionSelect_Landscape });
QToolBar* zoomToolbar = addToolBar(tr("Zoom"));
zoomToolbar->setObjectName("zoom_toolbar");
zoomToolbar->addActions({ ui->actionZoom_In, ui->actionZoom_Out });
QToolBar* makeToolbar = addToolBar(tr("Make"));
makeToolbar->setObjectName("make_toolbar");
makeToolbar->addActions({ ui->actionUnited_Document, ui->actionSeparate_to_Multiple_Documents, ui->actionSeparate_to_Multiple_Documents_Grouped });
QSize iconSize = pdf::PDFWidgetUtils::scaleDPI(this, QSize(24, 24));
for (QToolBar* toolbar : findChildren<QToolBar*>())
auto toolbars = findChildren<QToolBar*>();
for (QToolBar* toolbar : toolbars)
{
toolbar->setIconSize(iconSize);
}
@ -306,7 +312,83 @@ bool MainWindow::canPerformOperation(Operation operation) const
void MainWindow::performOperation(Operation operation)
{
switch (operation)
{
case Operation::CloneSelection:
case Operation::RemoveSelection:
case Operation::ReplaceSelection:
case Operation::RestoreRemovedItems:
case Operation::Cut:
case Operation::Copy:
case Operation::Paste:
case Operation::RotateLeft:
case Operation::RotateRight:
break;
case Operation::Group:
case Operation::Ungroup:
break;
case Operation::SelectNone:
ui->documentItemsView->clearSelection();
break;
case Operation::SelectAll:
ui->documentItemsView->selectAll();
break;
case Operation::SelectEven:
ui->documentItemsView->selectionModel()->select(m_model->getSelectionEven(), QItemSelectionModel::ClearAndSelect);
break;
case Operation::SelectOdd:
ui->documentItemsView->selectionModel()->select(m_model->getSelectionOdd(), QItemSelectionModel::ClearAndSelect);
break;
case Operation::SelectPortrait:
ui->documentItemsView->selectionModel()->select(m_model->getSelectionPortrait(), QItemSelectionModel::ClearAndSelect);
break;
case Operation::SelectLandscape:
ui->documentItemsView->selectionModel()->select(m_model->getSelectionLandscape(), QItemSelectionModel::ClearAndSelect);
break;
case Operation::ZoomIn:
{
QSize pageImageSize = m_delegate->getPageImageSize();
pageImageSize *= 1.2;
pageImageSize = pageImageSize.boundedTo(getMaxPageImageSize());
if (pageImageSize != m_delegate->getPageImageSize())
{
m_delegate->setPageImageSize(pageImageSize);
}
break;
}
case Operation::ZoomOut:
{
QSize pageImageSize = m_delegate->getPageImageSize();
pageImageSize /= 1.2;
pageImageSize = pageImageSize.expandedTo(getMinPageImageSize());
if (pageImageSize != m_delegate->getPageImageSize())
{
m_delegate->setPageImageSize(pageImageSize);
}
break;
}
case Operation::Unite:
case Operation::Separate:
case Operation::SeparateGrouped:
default:
Q_ASSERT(false);
break;
}
updateActions();
}
} // namespace pdfdocpage

View File

@ -18,6 +18,7 @@
#include "pageitemdelegate.h"
#include "pageitemmodel.h"
#include "pdfwidgetutils.h"
#include "pdfpainterutils.h"
#include <QPainter>
@ -49,6 +50,7 @@ void PageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& opti
QSize scaledSize = pdf::PDFWidgetUtils::scaleDPI(option.widget, m_pageImageSize);
int verticalSpacing = pdf::PDFWidgetUtils::scaleDPI_y(option.widget, getVerticalSpacing());
int horizontalSpacing = pdf::PDFWidgetUtils::scaleDPI_x(option.widget, getHorizontalSpacing());
QRect pageBoundingRect = QRect(QPoint(rect.left() + (rect.width() - scaledSize.width()) / 2, rect.top() + verticalSpacing), scaledSize);
@ -78,6 +80,21 @@ void PageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& opti
selectedColor.setAlphaF(0.3);
painter->fillRect(rect, selectedColor);
}
QPoint tagPoint = rect.topRight() + QPoint(-horizontalSpacing, verticalSpacing);
for (const QString& tag : item->tags)
{
QStringList splitted = tag.split('@', Qt::KeepEmptyParts);
if (splitted.size() != 2 || splitted.back().isEmpty())
{
continue;
}
QColor color;
color.setNamedColor(splitted.front());
QRect bubbleRect = pdf::PDFPainterHelper::drawBubble(painter, tagPoint, color, splitted.back(), Qt::AlignLeft | Qt::AlignBottom);
tagPoint.ry() += bubbleRect.height() + verticalSpacing;
}
}
QSize PageItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
@ -95,9 +112,13 @@ QSize PageItemDelegate::getPageImageSize() const
return m_pageImageSize;
}
void PageItemDelegate::setPageImageSize(const QSize& pageImageSize)
void PageItemDelegate::setPageImageSize(QSize pageImageSize)
{
if (m_pageImageSize != pageImageSize)
{
m_pageImageSize = pageImageSize;
emit sizeHintChanged(QModelIndex());
}
}
} // namespace pdfdocpage

View File

@ -40,7 +40,7 @@ public:
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
QSize getPageImageSize() const;
void setPageImageSize(const QSize& pageImageSize);
void setPageImageSize(QSize pageImageSize);
private:
static constexpr int getVerticalSpacing() { return 5; }

View File

@ -141,6 +141,55 @@ bool PageItemModel::isGrouped(const QModelIndexList& indices) const
return false;
}
QItemSelection PageItemModel::getSelectionEven() const
{
return getSelectionImpl([](const PageGroupItem::GroupItem& groupItem) { return groupItem.pageIndex % 2 == 1; });
}
QItemSelection PageItemModel::getSelectionOdd() const
{
return getSelectionImpl([](const PageGroupItem::GroupItem& groupItem) { return groupItem.pageIndex % 2 == 0; });
}
QItemSelection PageItemModel::getSelectionPortrait() const
{
return getSelectionImpl([](const PageGroupItem::GroupItem& groupItem) { return groupItem.rotatedPageDimensionsMM.width() <= groupItem.rotatedPageDimensionsMM.height(); });
}
QItemSelection PageItemModel::getSelectionLandscape() const
{
return getSelectionImpl([](const PageGroupItem::GroupItem& groupItem) { return groupItem.rotatedPageDimensionsMM.width() >= groupItem.rotatedPageDimensionsMM.height(); });
}
QItemSelection PageItemModel::getSelectionImpl(std::function<bool (const PageGroupItem::GroupItem&)> filter) const
{
QItemSelection result;
for (int i = 0; i < rowCount(QModelIndex()); ++i)
{
QModelIndex rowIndex = index(i, 0, QModelIndex());
if (const PageGroupItem* item = getItem(rowIndex))
{
bool isApplied = false;
for (const auto& groupItem : item->groups)
{
if (filter(groupItem))
{
isApplied = true;
break;
}
}
if (isApplied)
{
result.select(rowIndex, rowIndex);
}
}
}
return result;
}
void PageItemModel::createDocumentGroup(int index)
{
const DocumentItem& item = m_documents.at(index);
@ -154,7 +203,7 @@ void PageItemModel::createDocumentGroup(int index)
if (pageCount > 1)
{
newItem.tags = QStringList() << QString("0x00FF00@+%1").arg(pageCount - 1);
newItem.tags = QStringList() << QString("#00CC00@+%1").arg(pageCount - 1);
}
newItem.groups.reserve(pageCount);
@ -186,4 +235,5 @@ QString PageItemModel::getGroupNameFromDocument(int index) const
return fileInfo.fileName();
}
} // namespace pdfdocpage

View File

@ -80,9 +80,15 @@ public:
/// Returns true, if grouped item exists in the indices
bool isGrouped(const QModelIndexList& indices) const;
QItemSelection getSelectionEven() const;
QItemSelection getSelectionOdd() const;
QItemSelection getSelectionPortrait() const;
QItemSelection getSelectionLandscape() const;
private:
void createDocumentGroup(int index);
QString getGroupNameFromDocument(int index) const;
QItemSelection getSelectionImpl(std::function<bool(const PageGroupItem::GroupItem&)> filter) const;
std::vector<PageGroupItem> m_pageGroupItems;
std::map<int, DocumentItem> m_documents;

View File

@ -68,6 +68,7 @@ SOURCES += \
sources/pdfoutline.cpp \
sources/pdfpagenavigation.cpp \
sources/pdfpagetransition.cpp \
sources/pdfpainterutils.cpp \
sources/pdfparser.cpp \
sources/pdfdocument.cpp \
sources/pdfdocumentreader.cpp \

View File

@ -0,0 +1,63 @@
// 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/>.
#include "pdfpainterutils.h"
#include <QFontMetrics>
namespace pdf
{
QRect PDFPainterHelper::drawBubble(QPainter* painter, QPoint point, QColor color, QString text, Qt::Alignment alignment)
{
QFontMetrics fontMetrics = painter->fontMetrics();
const int lineSpacing = fontMetrics.lineSpacing();
const int bubbleHeight = lineSpacing* 2;
const int bubbleWidth = lineSpacing + fontMetrics.horizontalAdvance(text);
QRect rectangle(point, QSize(bubbleWidth, bubbleHeight));
if (alignment.testFlag(Qt::AlignVCenter))
{
rectangle.translate(0, -rectangle.height() / 2);
}
else if (alignment.testFlag(Qt::AlignTop))
{
rectangle.translate(0, -rectangle.height());
}
if (alignment.testFlag(Qt::AlignHCenter))
{
rectangle.translate(-rectangle.width() / 2, 0);
}
else if (alignment.testFlag(Qt::AlignLeft))
{
rectangle.translate(-rectangle.width(), 0);
}
PDFPainterStateGuard guard(painter);
painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(color));
painter->drawRoundedRect(rectangle, rectangle.height() / 2, rectangle.height() / 2, Qt::AbsoluteSize);
painter->setPen(Qt::black);
painter->drawText(rectangle, Qt::AlignCenter, text);
return rectangle;
}
} // namespace pdf

View File

@ -18,6 +18,8 @@
#ifndef PDFPAINTERUTILS_H
#define PDFPAINTERUTILS_H
#include "pdfglobal.h"
#include <QPainter>
namespace pdf
@ -42,6 +44,19 @@ private:
QPainter* m_painter;
};
class Pdf4QtLIBSHARED_EXPORT PDFPainterHelper
{
public:
/// Draws bubble using painter. Bubble is aligned to the point, colored with color
/// and inside bubble, text is being paint. Bubble bounding box is being returned.
/// \param painter Painter
/// \param point Position of the bubble
/// \param color Color of the bubble
/// \param text Text inside the bubble
/// \param alignment Bubble alignment relative to the bubble position point
static QRect drawBubble(QPainter* painter, QPoint point, QColor color, QString text, Qt::Alignment alignment);
};
} // namespace pdf
#endif // PDFPAINTERUTILS_H