diff --git a/Pdf4QtLibWidgets/sources/pdfadvancedtools.cpp b/Pdf4QtLibWidgets/sources/pdfadvancedtools.cpp index 6348280..f81f976 100644 --- a/Pdf4QtLibWidgets/sources/pdfadvancedtools.cpp +++ b/Pdf4QtLibWidgets/sources/pdfadvancedtools.cpp @@ -22,9 +22,11 @@ #include "pdfcompiler.h" #include "pdfwidgetformmanager.h" #include "pdfwidgetannotation.h" +#include "pdfwidgetutils.h" #include #include +#include #include #include "pdfdbgheap.h" @@ -924,12 +926,14 @@ PDFCreateHighlightTextTool::PDFCreateHighlightTextTool(PDFDrawWidgetProxy* proxy BaseClass(proxy, parent), m_toolManager(toolManager), m_actionGroup(actionGroup), + m_colorDialog(nullptr), m_type(AnnotationType::Highlight), m_isCursorOverText(false) { connect(m_actionGroup, &QActionGroup::triggered, this, &PDFCreateHighlightTextTool::onActionTriggered); updateActions(); + updateInitialColor(); } void PDFCreateHighlightTextTool::drawPage(QPainter* painter, @@ -988,7 +992,7 @@ void PDFCreateHighlightTextTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* // Jakub Melka: handle the selection PDFTextLayoutGetter textLayoutGetter = getProxy()->getTextLayoutCompiler()->getTextLayoutLazy(pageIndex); PDFTextLayout textLayout = textLayoutGetter; - setSelection(textLayout.createTextSelection(pageIndex, m_selectionInfo.selectionStartPoint, pagePoint)); + setSelection(textLayout.createTextSelection(pageIndex, m_selectionInfo.selectionStartPoint, pagePoint, m_color)); QPolygonF quadrilaterals; PDFTextSelectionPainter textSelectionPainter(&m_textSelection); @@ -1003,21 +1007,21 @@ void PDFCreateHighlightTextTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* switch (m_type) { case AnnotationType::Highlight: - annotationReference = modifier.getBuilder()->createAnnotationHighlight(page, quadrilaterals, Qt::yellow); + annotationReference = modifier.getBuilder()->createAnnotationHighlight(page, quadrilaterals, m_color); modifier.getBuilder()->setAnnotationOpacity(annotationReference, 0.2); modifier.getBuilder()->updateAnnotationAppearanceStreams(annotationReference); break; case AnnotationType::Underline: - annotationReference = modifier.getBuilder()->createAnnotationUnderline(page, quadrilaterals, Qt::black); + annotationReference = modifier.getBuilder()->createAnnotationUnderline(page, quadrilaterals, m_color); break; case AnnotationType::Squiggly: - annotationReference = modifier.getBuilder()->createAnnotationSquiggly(page, quadrilaterals, Qt::red); + annotationReference = modifier.getBuilder()->createAnnotationSquiggly(page, quadrilaterals, m_color); break; case AnnotationType::StrikeOut: - annotationReference = modifier.getBuilder()->createAnnotationStrikeout(page, quadrilaterals, Qt::red); + annotationReference = modifier.getBuilder()->createAnnotationStrikeout(page, quadrilaterals, m_color); break; default: @@ -1057,7 +1061,7 @@ void PDFCreateHighlightTextTool::mouseMoveEvent(QWidget* widget, QMouseEvent* ev if (m_selectionInfo.pageIndex == pageIndex) { // Jakub Melka: handle the selection - setSelection(textLayout.createTextSelection(pageIndex, m_selectionInfo.selectionStartPoint, pagePoint)); + setSelection(textLayout.createTextSelection(pageIndex, m_selectionInfo.selectionStartPoint, pagePoint, m_color)); } else { @@ -1094,17 +1098,44 @@ void PDFCreateHighlightTextTool::setActiveImpl(bool active) { // Just clear the text selection setSelection(PDFTextSelection()); + + delete m_colorDialog; + m_colorDialog = nullptr; + } + else + { + m_colorDialog = new QColorDialog(m_color, getProxy()->getWidget()); + m_colorDialog->setWindowTitle(tr("Select Color")); + m_colorDialog->setOption(QColorDialog::ShowAlphaChannel, false); + m_colorDialog->setOption(QColorDialog::NoButtons, true); + m_colorDialog->setOption(QColorDialog::DontUseNativeDialog, true); + m_colorDialog->setOption(QColorDialog::NoEyeDropperButton, true); + m_colorDialog->setWindowFlag(Qt::Tool); + m_colorDialog->move(pdf::PDFWidgetUtils::scaleDPI_x(m_colorDialog, 50), pdf::PDFWidgetUtils::scaleDPI_y(m_colorDialog, 50)); + connect(m_colorDialog, &QColorDialog::currentColorChanged, this, &PDFCreateHighlightTextTool::onColorChanged); + m_colorDialog->show(); } } void PDFCreateHighlightTextTool::onActionTriggered(QAction* action) { - setActive(action && action->isChecked()); - if (action) { - m_type = static_cast(action->data().toInt()); + AnnotationType type = static_cast(action->data().toInt()); + + if (m_type != type) + { + m_type = type; + updateInitialColor(); + } } + + setActive(action && action->isChecked()); +} + +void PDFCreateHighlightTextTool::onColorChanged(const QColor& color) +{ + m_color = color; } void PDFCreateHighlightTextTool::updateCursor() @@ -1131,6 +1162,33 @@ void PDFCreateHighlightTextTool::setSelection(PDFTextSelection&& textSelection) } } +void PDFCreateHighlightTextTool::updateInitialColor() +{ + switch (m_type) + { + case AnnotationType::Highlight: + m_color = Qt::yellow; + break; + + case AnnotationType::Underline: + m_color = Qt::black; + break; + + case AnnotationType::Squiggly: + m_color = Qt::red; + break; + + case AnnotationType::StrikeOut: + m_color = Qt::red; + break; + } + + if (m_colorDialog) + { + m_colorDialog->setCurrentColor(m_color); + } +} + PDFCreateRedactRectangleTool::PDFCreateRedactRectangleTool(PDFDrawWidgetProxy* proxy, PDFToolManager* toolManager, QAction* action, QObject* parent) : BaseClass(proxy, action, parent), m_toolManager(toolManager), diff --git a/Pdf4QtLibWidgets/sources/pdfadvancedtools.h b/Pdf4QtLibWidgets/sources/pdfadvancedtools.h index 8a0a3d1..eba6656 100644 --- a/Pdf4QtLibWidgets/sources/pdfadvancedtools.h +++ b/Pdf4QtLibWidgets/sources/pdfadvancedtools.h @@ -23,6 +23,7 @@ #include "pdfannotation.h" class QActionGroup; +class QColorDialog; namespace pdf { @@ -307,8 +308,10 @@ protected: private: void onActionTriggered(QAction* action); + void onColorChanged(const QColor& color); void updateCursor(); void setSelection(pdf::PDFTextSelection&& textSelection); + void updateInitialColor(); struct SelectionInfo { @@ -318,9 +321,11 @@ private: PDFToolManager* m_toolManager; QActionGroup* m_actionGroup; + QColorDialog* m_colorDialog; AnnotationType m_type; pdf::PDFTextSelection m_textSelection; SelectionInfo m_selectionInfo; + QColor m_color; bool m_isCursorOverText; }; diff --git a/RELEASES.txt b/RELEASES.txt index 3a4f329..bfc1a79 100644 --- a/RELEASES.txt +++ b/RELEASES.txt @@ -1,4 +1,5 @@ CURRENT: + - Issue #236: Highligting - Allow color selection and intensity [Enhancement] - Issue #229: Can't add picture to PDF - Issue #228: [feature] reverse pages order - Issue #222: UnitTests fails several tests