DocPage Organizer: Insert/replace document in selection

This commit is contained in:
Jakub Melka 2021-07-25 15:48:26 +02:00
parent 229234fc15
commit 4570b12359
5 changed files with 53 additions and 18 deletions

View File

@ -77,7 +77,7 @@ MainWindow::MainWindow(QWidget* parent) :
ui->actionSeparate_to_Multiple_Documents_Grouped->setData(int(Operation::SeparateGrouped));
ui->actionInsert_Image->setData(int(Operation::InsertImage));
ui->actionInsert_Empty_Page->setData(int(Operation::InsertEmptyPage));
ui->actionInsert_Page_from_PDF->setData(int(Operation::InsertPDF));
ui->actionInsert_PDF->setData(int(Operation::InsertPDF));
ui->actionGet_Source->setData(int(Operation::GetSource));
ui->actionAbout->setData(int(Operation::About));
@ -92,7 +92,7 @@ MainWindow::MainWindow(QWidget* parent) :
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 });
insertToolbar->addActions({ ui->actionInsert_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 });
@ -160,7 +160,7 @@ void MainWindow::on_actionAddDocument_triggered()
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings.directory, tr("PDF document (*.pdf)"));
if (!fileName.isEmpty())
{
addDocument(fileName);
insertDocument(fileName, QModelIndex());
}
}
@ -224,7 +224,7 @@ void MainWindow::saveSettings()
settings.endGroup();
}
void MainWindow::addDocument(const QString& fileName)
void MainWindow::insertDocument(const QString& fileName, const QModelIndex& insertIndex)
{
auto queryPassword = [this](bool* ok)
{
@ -248,7 +248,7 @@ void MainWindow::addDocument(const QString& fileName)
if (securityHandler->isAllowed(pdf::PDFSecurityHandler::Permission::Assemble) ||
securityHandler->isAllowed(pdf::PDFSecurityHandler::Permission::Modify))
{
m_model->addDocument(fileName, qMove(document));
m_model->insertDocument(fileName, qMove(document), insertIndex);
}
else
{
@ -654,9 +654,34 @@ void MainWindow::performOperation(Operation operation)
}
case Operation::InsertPDF:
case Operation::ReplaceSelection:
Q_ASSERT(false);
{
QModelIndexList indexes = ui->documentItemsView->selectionModel()->selection().indexes();
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings.directory, tr("PDF document (*.pdf)"));
if (!fileName.isEmpty())
{
insertDocument(fileName, !indexes.isEmpty() ? indexes.back() : QModelIndex());
}
break;
}
case Operation::ReplaceSelection:
{
QModelIndexList indexes = ui->documentItemsView->selectionModel()->selection().indexes();
if (indexes.isEmpty())
{
// Jakub Melka: we have nothing to do, selection is empty
return;
}
QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings.directory, tr("PDF document (*.pdf)"));
if (!fileName.isEmpty())
{
insertDocument(fileName, indexes.back());
m_model->removeSelection(indexes);
}
break;
}
default:
Q_ASSERT(false);

View File

@ -94,7 +94,7 @@ private slots:
private:
void loadSettings();
void saveSettings();
void addDocument(const QString& fileName);
void insertDocument(const QString& fileName, const QModelIndex& insertIndex);
bool canPerformOperation(Operation operation) const;
void performOperation(Operation operation);

View File

@ -82,7 +82,7 @@
<property name="title">
<string>Insert</string>
</property>
<addaction name="actionInsert_Page_from_PDF"/>
<addaction name="actionInsert_PDF"/>
<addaction name="actionInsert_Image"/>
<addaction name="actionInsert_Empty_Page"/>
</widget>
@ -189,13 +189,16 @@
<string>Ctrl+Shift+R</string>
</property>
</action>
<action name="actionInsert_Page_from_PDF">
<action name="actionInsert_PDF">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/pdfdocpage/resources/insert-page-from-pdf.svg</normaloff>:/pdfdocpage/resources/insert-page-from-pdf.svg</iconset>
</property>
<property name="text">
<string>Insert Page from PDF</string>
<string>Insert PDF</string>
</property>
<property name="toolTip">
<string>Insert PDF</string>
</property>
<property name="shortcut">
<string>Ctrl+I</string>

View File

@ -95,7 +95,7 @@ QVariant PageItemModel::data(const QModelIndex& index, int role) const
return QVariant();
}
int PageItemModel::addDocument(QString fileName, pdf::PDFDocument document)
int PageItemModel::insertDocument(QString fileName, pdf::PDFDocument document, const QModelIndex& index)
{
auto it = std::find_if(m_documents.cbegin(), m_documents.cend(), [&](const auto& item) { return item.second.fileName == fileName; });
if (it != m_documents.cend())
@ -111,7 +111,7 @@ int PageItemModel::addDocument(QString fileName, pdf::PDFDocument document)
}
m_documents[newIndex] = { qMove(fileName), qMove(document) };
createDocumentGroup(newIndex);
createDocumentGroup(newIndex, index);
return newIndex;
}
@ -518,7 +518,7 @@ QItemSelection PageItemModel::getSelectionImpl(std::function<bool (const PageGro
return result;
}
void PageItemModel::createDocumentGroup(int index)
void PageItemModel::createDocumentGroup(int index, const QModelIndex& insertIndex)
{
const DocumentItem& item = m_documents.at(index);
const pdf::PDFInteger pageCount = item.document.getCatalog()->getPageCount();
@ -544,8 +544,14 @@ void PageItemModel::createDocumentGroup(int index)
newItem.groups.push_back(qMove(groupItem));
}
beginInsertRows(QModelIndex(), rowCount(QModelIndex()), rowCount(QModelIndex()));
m_pageGroupItems.push_back(qMove(newItem));
int insertRow = rowCount(QModelIndex());
if (insertIndex.isValid())
{
insertRow = insertIndex.row() + 1;
}
beginInsertRows(QModelIndex(), insertRow, insertRow);
m_pageGroupItems.insert(std::next(m_pageGroupItems.begin(), insertRow), qMove(newItem));
endInsertRows();
}

View File

@ -118,8 +118,9 @@ public:
/// cannot be added (for example, it already exists), -1 is returned.
/// \param fileName File name
/// \param document Document
/// \param index Index, where image is inserted
/// \returns Identifier of the document (internal index)
int addDocument(QString fileName, pdf::PDFDocument document);
int insertDocument(QString fileName, pdf::PDFDocument document, const QModelIndex& index);
/// Adds image to the model, inserts one single page containing
/// the image. Returns index of a newly added image. If image
@ -167,7 +168,7 @@ public:
const std::map<int, ImageItem>& getImages() const { return m_images; }
private:
void createDocumentGroup(int index);
void createDocumentGroup(int index, const QModelIndex& insertIndex);
QString getGroupNameFromDocument(int index) const;
void updateItemCaptionAndTags(PageGroupItem& item) const;
void insertEmptyPage(const QModelIndex& index);