diff --git a/Pdf4QtDocPageOrganizer/mainwindow.cpp b/Pdf4QtDocPageOrganizer/mainwindow.cpp index 47e4377..f3b737f 100644 --- a/Pdf4QtDocPageOrganizer/mainwindow.cpp +++ b/Pdf4QtDocPageOrganizer/mainwindow.cpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace pdfdocpage { @@ -473,10 +474,6 @@ void MainWindow::performOperation(Operation operation) m_model->rotateRight(ui->documentItemsView->selectionModel()->selection().indexes()); break; - case Operation::ReplaceSelection: - Q_ASSERT(false); - break; - case Operation::GetSource: QDesktopServices::openUrl(QUrl("https://github.com/JakubMelka/PdfForQt")); break; @@ -492,7 +489,28 @@ void MainWindow::performOperation(Operation operation) break; } + case Operation::ReplaceSelection: + Q_ASSERT(false); + break; + case Operation::InsertImage: + { + QStringList filters; + for (const QByteArray& imageFormat : QImageReader::supportedImageFormats()) + { + filters << QString::fromLatin1(imageFormat).toLower(); + } + QString filter = tr("Images (*.%1)").arg(filters.join(" *.")); + QString fileName = QFileDialog::getOpenFileName(this, tr("Select Image"), m_settings.directory, filter, nullptr); + + if (!fileName.isEmpty()) + { + QModelIndexList indexes = ui->documentItemsView->selectionModel()->selection().indexes(); + m_model->insertImage(fileName, !indexes.isEmpty() ? indexes.front() : QModelIndex()); + } + break; + } + case Operation::InsertPDF: case Operation::Unite: diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp index 4f48e53..32a6fa3 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.cpp +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.cpp @@ -18,6 +18,7 @@ #include "pageitemmodel.h" #include +#include #include @@ -114,6 +115,56 @@ int PageItemModel::addDocument(QString fileName, pdf::PDFDocument document) return newIndex; } +int PageItemModel::insertImage(QString fileName, const QModelIndex& index) +{ + QFile file(fileName); + + if (file.open(QFile::ReadOnly)) + { + ImageItem item; + item.imageData = file.readAll(); + + QImageReader reader(fileName); + item.image = reader.read(); + + file.close(); + + if (!item.image.isNull()) + { + int newIndex = 1; + + if (!m_images.empty()) + { + newIndex = (m_images.rbegin()->first) + 1; + } + + m_images[newIndex] = qMove(item); + + // Insert image item + PageGroupItem newItem; + + newItem.groups.reserve(1); + + PageGroupItem::GroupItem groupItem; + groupItem.imageIndex = newIndex; + groupItem.rotatedPageDimensionsMM = m_images[newIndex].image.size() * 0.1; + groupItem.pageType = PT_Image; + newItem.groups.push_back(qMove(groupItem)); + + updateItemCaptionAndTags(newItem); + int insertRow = index.isValid() ? index.row() + 1 : int(m_pageGroupItems.size()); + + beginInsertRows(QModelIndex(), insertRow, insertRow); + m_pageGroupItems.insert(std::next(m_pageGroupItems.begin(), insertRow), qMove(newItem)); + endInsertRows(); + + return newIndex; + } + } + + return -1; +} + const PageGroupItem* PageItemModel::getItem(const QModelIndex& index) const { if (index.isValid()) diff --git a/Pdf4QtDocPageOrganizer/pageitemmodel.h b/Pdf4QtDocPageOrganizer/pageitemmodel.h index fb70491..016e8c0 100644 --- a/Pdf4QtDocPageOrganizer/pageitemmodel.h +++ b/Pdf4QtDocPageOrganizer/pageitemmodel.h @@ -21,6 +21,7 @@ #include "pdfdocument.h" #include "pdfutils.h" +#include #include namespace pdfdocpage @@ -69,6 +70,12 @@ struct DocumentItem pdf::PDFDocument document; }; +struct ImageItem +{ + QImage image; + QByteArray imageData; +}; + class PageItemModel : public QAbstractItemModel { Q_OBJECT @@ -104,6 +111,14 @@ public: /// \returns Identifier of the document (internal index) int addDocument(QString fileName, pdf::PDFDocument document); + /// Adds image to the model, inserts one single page containing + /// the image. Returns index of a newly added image. If image + /// cannot be read from the file, -1 is returned. + /// \param fileName Image file + /// \param index Index, where image is inserted + /// \returns Identifier of the image (internal index) + int insertImage(QString fileName, const QModelIndex& index); + /// Returns item at a given index. If item doesn't exist, /// then nullptr is returned. /// \param index Index @@ -148,6 +163,7 @@ private: std::vector m_pageGroupItems; std::map m_documents; + std::map m_images; std::vector m_trashBin; };