DocPage Organizer: Insert image

This commit is contained in:
Jakub Melka 2021-07-22 20:05:16 +02:00
parent c075c15f2f
commit b644ea48e3
3 changed files with 89 additions and 4 deletions

View File

@ -30,6 +30,7 @@
#include <QClipboard>
#include <QToolBar>
#include <QDesktopServices>
#include <QImageReader>
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:

View File

@ -18,6 +18,7 @@
#include "pageitemmodel.h"
#include <QFileInfo>
#include <QImageReader>
#include <iterator>
@ -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())

View File

@ -21,6 +21,7 @@
#include "pdfdocument.h"
#include "pdfutils.h"
#include <QImage>
#include <QAbstractItemModel>
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<PageGroupItem> m_pageGroupItems;
std::map<int, DocumentItem> m_documents;
std::map<int, ImageItem> m_images;
std::vector<PageGroupItem> m_trashBin;
};