mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Actions
This commit is contained in:
@ -25,6 +25,8 @@
|
|||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QInputDialog>
|
#include <QInputDialog>
|
||||||
#include <QDesktopWidget>
|
#include <QDesktopWidget>
|
||||||
|
#include <QClipboard>
|
||||||
|
#include <QToolBar>
|
||||||
|
|
||||||
namespace pdfdocpage
|
namespace pdfdocpage
|
||||||
{
|
{
|
||||||
@ -43,7 +45,68 @@ MainWindow::MainWindow(QWidget* parent) :
|
|||||||
ui->documentItemsView->setItemDelegate(m_delegate);
|
ui->documentItemsView->setItemDelegate(m_delegate);
|
||||||
setMinimumSize(pdf::PDFWidgetUtils::scaleDPI(this, QSize(800, 600)));
|
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();
|
loadSettings();
|
||||||
|
updateActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
@ -81,9 +144,22 @@ void MainWindow::on_actionAddDocument_triggered()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::onMappedActionTriggered(int actionId)
|
||||||
|
{
|
||||||
|
performOperation(static_cast<Operation>(actionId));
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::updateActions()
|
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()
|
void MainWindow::loadSettings()
|
||||||
@ -167,4 +243,70 @@ void MainWindow::addDocument(const QString& fileName)
|
|||||||
updateActions();
|
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
|
} // namespace pdfdocpage
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
#include "pageitemmodel.h"
|
#include "pageitemmodel.h"
|
||||||
#include "pageitemdelegate.h"
|
#include "pageitemdelegate.h"
|
||||||
|
|
||||||
|
#include <QSignalMapper>
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
@ -43,9 +45,42 @@ public:
|
|||||||
QSize getDefaultPageImageSize() const;
|
QSize getDefaultPageImageSize() const;
|
||||||
QSize getMaxPageImageSize() 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:
|
private slots:
|
||||||
void on_actionClose_triggered();
|
void on_actionClose_triggered();
|
||||||
void on_actionAddDocument_triggered();
|
void on_actionAddDocument_triggered();
|
||||||
|
void onMappedActionTriggered(int actionId);
|
||||||
void updateActions();
|
void updateActions();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -53,6 +88,9 @@ private:
|
|||||||
void saveSettings();
|
void saveSettings();
|
||||||
void addDocument(const QString& fileName);
|
void addDocument(const QString& fileName);
|
||||||
|
|
||||||
|
bool canPerformOperation(Operation operation) const;
|
||||||
|
void performOperation(Operation operation);
|
||||||
|
|
||||||
struct Settings
|
struct Settings
|
||||||
{
|
{
|
||||||
QString directory;
|
QString directory;
|
||||||
@ -63,6 +101,8 @@ private:
|
|||||||
PageItemModel* m_model;
|
PageItemModel* m_model;
|
||||||
PageItemDelegate* m_delegate;
|
PageItemDelegate* m_delegate;
|
||||||
Settings m_settings;
|
Settings m_settings;
|
||||||
|
QSignalMapper m_mapper;
|
||||||
|
std::vector<PageGroupItem> m_trashBin;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pdfdocpage
|
} // namespace pdfdocpage
|
||||||
|
@ -23,6 +23,9 @@
|
|||||||
<property name="dragDropMode">
|
<property name="dragDropMode">
|
||||||
<enum>QAbstractItemView::InternalMove</enum>
|
<enum>QAbstractItemView::InternalMove</enum>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="defaultDropAction">
|
||||||
|
<enum>Qt::MoveAction</enum>
|
||||||
|
</property>
|
||||||
<property name="selectionMode">
|
<property name="selectionMode">
|
||||||
<enum>QAbstractItemView::ExtendedSelection</enum>
|
<enum>QAbstractItemView::ExtendedSelection</enum>
|
||||||
</property>
|
</property>
|
||||||
|
@ -87,7 +87,7 @@ QSize PageItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QMode
|
|||||||
QSize scaledSize = pdf::PDFWidgetUtils::scaleDPI(option.widget, m_pageImageSize);
|
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 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()));
|
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
|
QSize PageItemDelegate::getPageImageSize() const
|
||||||
|
@ -125,6 +125,22 @@ const PageGroupItem* PageItemModel::getItem(const QModelIndex& index) const
|
|||||||
return nullptr;
|
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)
|
void PageItemModel::createDocumentGroup(int index)
|
||||||
{
|
{
|
||||||
const DocumentItem& item = m_documents.at(index);
|
const DocumentItem& item = m_documents.at(index);
|
||||||
|
@ -40,6 +40,8 @@ struct PageGroupItem
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector<GroupItem> groups;
|
std::vector<GroupItem> groups;
|
||||||
|
|
||||||
|
bool isGrouped() const { return groups.size() > 1; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DocumentItem
|
struct DocumentItem
|
||||||
@ -75,6 +77,9 @@ public:
|
|||||||
/// \param index Index
|
/// \param index Index
|
||||||
const PageGroupItem* getItem(const QModelIndex& index) const;
|
const PageGroupItem* getItem(const QModelIndex& index) const;
|
||||||
|
|
||||||
|
/// Returns true, if grouped item exists in the indices
|
||||||
|
bool isGrouped(const QModelIndexList& indices) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void createDocumentGroup(int index);
|
void createDocumentGroup(int index);
|
||||||
QString getGroupNameFromDocument(int index) const;
|
QString getGroupNameFromDocument(int index) const;
|
||||||
|
Reference in New Issue
Block a user