This commit is contained in:
Jakub Melka 2021-07-08 13:48:27 +02:00
parent 01f97028ed
commit 461f4544a3
6 changed files with 208 additions and 2 deletions

View File

@ -25,6 +25,8 @@
#include <QMessageBox>
#include <QInputDialog>
#include <QDesktopWidget>
#include <QClipboard>
#include <QToolBar>
namespace pdfdocpage
{
@ -43,7 +45,68 @@ MainWindow::MainWindow(QWidget* parent) :
ui->documentItemsView->setItemDelegate(m_delegate);
setMinimumSize(pdf::PDFWidgetUtils::scaleDPI(this, QSize(800, 600)));
ui->actionCloneSelection->setData(int(Operation::CloneSelection));
ui->actionRemoveSelection->setData(int(Operation::RemoveSelection));
ui->actionReplaceSelection->setData(int(Operation::ReplaceSelection));
ui->actionRestoreRemovedItems->setData(int(Operation::RestoreRemovedItems));
ui->actionCut->setData(int(Operation::Cut));
ui->actionCopy->setData(int(Operation::Copy));
ui->actionPaste->setData(int(Operation::Paste));
ui->actionRotate_Left->setData(int(Operation::RotateLeft));
ui->actionRotate_Right->setData(int(Operation::RotateRight));
ui->actionGroup->setData(int(Operation::Group));
ui->actionUngroup->setData(int(Operation::Ungroup));
ui->actionSelect_None->setData(int(Operation::SelectNone));
ui->actionSelect_All->setData(int(Operation::SelectAll));
ui->actionSelect_Even->setData(int(Operation::SelectEven));
ui->actionSelect_Odd->setData(int(Operation::SelectOdd));
ui->actionSelect_Portrait->setData(int(Operation::SelectPortrait));
ui->actionSelect_Landscape->setData(int(Operation::SelectLandscape));
ui->actionZoom_In->setData(int(Operation::ZoomIn));
ui->actionZoom_Out->setData(int(Operation::ZoomOut));
ui->actionUnited_Document->setData(int(Operation::Unite));
ui->actionSeparate_to_Multiple_Documents->setData(int(Operation::Separate));
ui->actionSeparate_to_Multiple_Documents_Grouped->setData(int(Operation::SeparateGrouped));
QToolBar* mainToolbar = addToolBar(tr("Main"));
mainToolbar->addAction(ui->actionAddDocument);
mainToolbar->addSeparator();
mainToolbar->addActions({ ui->actionCloneSelection, ui->actionRemoveSelection });
mainToolbar->addSeparator();
mainToolbar->addActions({ ui->actionCut, ui->actionCopy, ui->actionPaste });
mainToolbar->addSeparator();
mainToolbar->addActions({ ui->actionGroup, ui->actionUngroup });
QToolBar* insertToolbar = addToolBar(tr("Insert"));
insertToolbar->addActions({ ui->actionInsert_Page_from_PDF, ui->actionInsert_Image, ui->actionInsert_Empty_Page });
QToolBar* selectToolbar = addToolBar(tr("Select"));
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->addActions({ ui->actionZoom_In, ui->actionZoom_Out });
QToolBar* makeToolbar = addToolBar(tr("Make"));
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*>())
{
toolbar->setIconSize(iconSize);
}
connect(&m_mapper, QOverload<int>::of(&QSignalMapper::mapped), this, &MainWindow::onMappedActionTriggered);
connect(ui->documentItemsView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::updateActions);
QList<QAction*> actions = findChildren<QAction*>();
for (QAction* action : actions)
{
QVariant actionData = action->data();
if (actionData.isValid())
{
connect(action, &QAction::triggered, &m_mapper, QOverload<>::of(&QSignalMapper::map));
m_mapper.setMapping(action, actionData.toInt());
}
}
loadSettings();
updateActions();
}
MainWindow::~MainWindow()
@ -81,9 +144,22 @@ void MainWindow::on_actionAddDocument_triggered()
}
}
void MainWindow::onMappedActionTriggered(int actionId)
{
performOperation(static_cast<Operation>(actionId));
}
void MainWindow::updateActions()
{
QList<QAction*> actions = findChildren<QAction*>();
for (QAction* action : actions)
{
QVariant actionData = action->data();
if (actionData.isValid())
{
action->setEnabled(canPerformOperation(static_cast<Operation>(actionData.toInt())));
}
}
}
void MainWindow::loadSettings()
@ -167,4 +243,70 @@ void MainWindow::addDocument(const QString& fileName)
updateActions();
}
bool MainWindow::canPerformOperation(Operation operation) const
{
QModelIndexList selection = ui->documentItemsView->selectionModel()->selection().indexes();
const bool isSelected = !selection.isEmpty();
const bool isModelEmpty = m_model->rowCount(QModelIndex()) == 0;
switch (operation)
{
case Operation::CloneSelection:
case Operation::RemoveSelection:
case Operation::ReplaceSelection:
return isSelected;
case Operation::RestoreRemovedItems:
return !m_trashBin.empty();
case Operation::Cut:
case Operation::Copy:
return isSelected;
case Operation::Paste:
return !QApplication::clipboard()->text().isEmpty();
case Operation::RotateLeft:
case Operation::RotateRight:
return isSelected;
case Operation::Group:
return isSelected;
case Operation::Ungroup:
return m_model->isGrouped(selection);
case Operation::SelectNone:
return isSelected;
case Operation::SelectAll:
case Operation::SelectEven:
case Operation::SelectOdd:
case Operation::SelectPortrait:
case Operation::SelectLandscape:
return !isModelEmpty;
case Operation::ZoomIn:
return m_delegate->getPageImageSize() != getMaxPageImageSize();
case Operation::ZoomOut:
return m_delegate->getPageImageSize() != getMinPageImageSize();
case Operation::Unite:
case Operation::Separate:
case Operation::SeparateGrouped:
return !isModelEmpty;
default:
Q_ASSERT(false);
break;
}
return false;
}
void MainWindow::performOperation(Operation operation)
{
}
} // namespace pdfdocpage

View File

@ -23,6 +23,8 @@
#include "pageitemmodel.h"
#include "pageitemdelegate.h"
#include <QSignalMapper>
namespace Ui
{
class MainWindow;
@ -43,9 +45,42 @@ public:
QSize getDefaultPageImageSize() const;
QSize getMaxPageImageSize() const;
enum class Operation
{
CloneSelection,
RemoveSelection,
ReplaceSelection,
RestoreRemovedItems,
Cut,
Copy,
Paste,
RotateLeft,
RotateRight,
Group,
Ungroup,
SelectNone,
SelectAll,
SelectEven,
SelectOdd,
SelectPortrait,
SelectLandscape,
ZoomIn,
ZoomOut,
Unite,
Separate,
SeparateGrouped
};
private slots:
void on_actionClose_triggered();
void on_actionAddDocument_triggered();
void onMappedActionTriggered(int actionId);
void updateActions();
private:
@ -53,6 +88,9 @@ private:
void saveSettings();
void addDocument(const QString& fileName);
bool canPerformOperation(Operation operation) const;
void performOperation(Operation operation);
struct Settings
{
QString directory;
@ -63,6 +101,8 @@ private:
PageItemModel* m_model;
PageItemDelegate* m_delegate;
Settings m_settings;
QSignalMapper m_mapper;
std::vector<PageGroupItem> m_trashBin;
};
} // namespace pdfdocpage

View File

@ -23,6 +23,9 @@
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>

View File

@ -87,7 +87,7 @@ QSize PageItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QMode
QSize scaledSize = pdf::PDFWidgetUtils::scaleDPI(option.widget, m_pageImageSize);
int height = scaledSize.height() + option.fontMetrics.lineSpacing() * 2 + 2 * pdf::PDFWidgetUtils::scaleDPI_y(option.widget, getVerticalSpacing());
int width = qMax(pdf::PDFWidgetUtils::scaleDPI_x(option.widget, 40), scaledSize.width() + 2 * pdf::PDFWidgetUtils::scaleDPI_x(option.widget, getHorizontalSpacing()));
return QSize(height, width);
return QSize(width, height);
}
QSize PageItemDelegate::getPageImageSize() const

View File

@ -125,6 +125,22 @@ const PageGroupItem* PageItemModel::getItem(const QModelIndex& index) const
return nullptr;
}
bool PageItemModel::isGrouped(const QModelIndexList& indices) const
{
for (const QModelIndex& index : indices)
{
if (const PageGroupItem* item = getItem(index))
{
if (item->isGrouped())
{
return true;
}
}
}
return false;
}
void PageItemModel::createDocumentGroup(int index)
{
const DocumentItem& item = m_documents.at(index);

View File

@ -40,6 +40,8 @@ struct PageGroupItem
};
std::vector<GroupItem> groups;
bool isGrouped() const { return groups.size() > 1; }
};
struct DocumentItem
@ -75,6 +77,9 @@ public:
/// \param index Index
const PageGroupItem* getItem(const QModelIndex& index) const;
/// Returns true, if grouped item exists in the indices
bool isGrouped(const QModelIndexList& indices) const;
private:
void createDocumentGroup(int index);
QString getGroupNameFromDocument(int index) const;