Changeset View
Changeset View
Standalone View
Standalone View
ui/pageviewannotator.cpp
Show All 27 Lines | |||||
28 | #include <qmenu.h> | 28 | #include <qmenu.h> | ||
29 | 29 | | |||
30 | // system includes | 30 | // system includes | ||
31 | #include <math.h> | 31 | #include <math.h> | ||
32 | #include <memory> | 32 | #include <memory> | ||
33 | #include <QStandardPaths> | 33 | #include <QStandardPaths> | ||
34 | 34 | | |||
35 | // local includes | 35 | // local includes | ||
36 | #include "conf/editannottooldialog.h" | ||||
36 | #include "core/area.h" | 37 | #include "core/area.h" | ||
37 | #include "core/document.h" | 38 | #include "core/document.h" | ||
38 | #include "core/page.h" | 39 | #include "core/page.h" | ||
39 | #include "core/annotations.h" | 40 | #include "core/annotations.h" | ||
41 | #include "ui/annotationactionhandler.h" | ||||
40 | #include "settings.h" | 42 | #include "settings.h" | ||
41 | #include "annotationtools.h" | 43 | #include "annotationtools.h" | ||
42 | #include "guiutils.h" | 44 | #include "guiutils.h" | ||
43 | #include "pageview.h" | 45 | #include "pageview.h" | ||
44 | #include "debug_ui.h" | 46 | #include "debug_ui.h" | ||
45 | 47 | | |||
46 | /** @short PickPointEngine */ | 48 | /** @short PickPointEngine */ | ||
47 | class PickPointEngine : public AnnotatorEngine | 49 | class PickPointEngine : public AnnotatorEngine | ||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Line(s) | 147 | { | |||
157 | { | 159 | { | ||
158 | QFont f; | 160 | QFont f; | ||
159 | f.fromString( m_annotElement.attribute( QStringLiteral("font") ) ); | 161 | f.fromString( m_annotElement.attribute( QStringLiteral("font") ) ); | ||
160 | ta->setTextFont( f ); | 162 | ta->setTextFont( f ); | ||
161 | } | 163 | } | ||
162 | // set font color | 164 | // set font color | ||
163 | if ( m_annotElement.hasAttribute( QStringLiteral("textColor") ) ) | 165 | if ( m_annotElement.hasAttribute( QStringLiteral("textColor") ) ) | ||
164 | { | 166 | { | ||
165 | if ( inplaceIntent == Okular::TextAnnotation::TypeWriter ) | | |||
166 | ta->setTextColor( m_annotElement.attribute( QStringLiteral("textColor") ) ); | 167 | ta->setTextColor( m_annotElement.attribute( QStringLiteral("textColor") ) ); | ||
167 | else | | |||
168 | ta->setTextColor( Qt::black ); | | |||
169 | } | 168 | } | ||
170 | //set width | 169 | //set width | ||
171 | if ( m_annotElement.hasAttribute( QStringLiteral ( "width" ) ) ) | 170 | if ( m_annotElement.hasAttribute( QStringLiteral ( "width" ) ) ) | ||
172 | { | 171 | { | ||
173 | ta->style().setWidth( m_annotElement.attribute( QStringLiteral ( "width" ) ).toDouble() ); | 172 | ta->style().setWidth( m_annotElement.attribute( QStringLiteral ( "width" ) ).toDouble() ); | ||
174 | } | 173 | } | ||
175 | //set boundary | 174 | //set boundary | ||
176 | rect.left = qMin(startpoint.x,point.x); | 175 | rect.left = qMin(startpoint.x,point.x); | ||
▲ Show 20 Lines • Show All 472 Lines • ▼ Show 20 Line(s) | 647 | private: | |||
649 | // data | 648 | // data | ||
650 | PageView * m_pageView; | 649 | PageView * m_pageView; | ||
651 | // TODO: support more pages | 650 | // TODO: support more pages | ||
652 | std::unique_ptr<Okular::RegularAreaRect> selection; | 651 | std::unique_ptr<Okular::RegularAreaRect> selection; | ||
653 | Okular::NormalizedPoint lastPoint; | 652 | Okular::NormalizedPoint lastPoint; | ||
654 | QRect rect; | 653 | QRect rect; | ||
655 | }; | 654 | }; | ||
656 | 655 | | |||
657 | 656 | /** @short AnnotationTools*/ | |||
658 | /** PageViewAnnotator **/ | 657 | class AnnotationTools | ||
659 | | ||||
660 | PageViewAnnotator::PageViewAnnotator( PageView * parent, Okular::Document * storage ) | | |||
661 | : QObject( parent ), m_document( storage ), m_pageView( parent ), | | |||
662 | m_toolBar( nullptr ), m_engine( nullptr ), m_textToolsEnabled( false ), m_toolsEnabled( false ), | | |||
663 | m_continuousMode( false ), m_hidingWasForced( false ), m_lastToolID( -1 ), m_lockedItem( nullptr ) | | |||
664 | { | 658 | { | ||
665 | reparseConfig(); | 659 | public: | ||
666 | } | 660 | AnnotationTools() : m_toolsCount( 0 ) {} | ||
667 | 661 | | |||
668 | void PageViewAnnotator::reparseConfig() | 662 | void setTools( QStringList tools ) | ||
669 | { | 663 | { | ||
670 | m_items.clear(); | | |||
671 | | ||||
672 | // Read tool list from configuration. It's a list of XML <tool></tool> elements | | |||
673 | const QStringList userTools = Okular::Settings::annotationTools(); | | |||
674 | | ||||
675 | // Populate m_toolsDefinition | 664 | // Populate m_toolsDefinition | ||
676 | QDomDocument doc; | 665 | m_toolsCount = 0; | ||
677 | m_toolsDefinition = doc.createElement( QStringLiteral("annotatingTools") ); | 666 | m_toolsDefinition.clear(); | ||
678 | foreach ( const QString &toolXml, userTools ) | 667 | QDomElement root = m_toolsDefinition.createElement( QStringLiteral( "root" ) ); | ||
668 | m_toolsDefinition.appendChild( root ); | ||||
669 | foreach ( const QString &toolXml, tools ) | ||||
679 | { | 670 | { | ||
680 | QDomDocument entryParser; | 671 | QDomDocument entryParser; | ||
681 | if ( entryParser.setContent( toolXml ) ) | 672 | if ( entryParser.setContent( toolXml ) ) { | ||
682 | m_toolsDefinition.appendChild( doc.importNode( entryParser.documentElement(), true ) ); | 673 | root.appendChild( m_toolsDefinition.importNode( entryParser.documentElement(), true ) ); | ||
683 | else | 674 | m_toolsCount++; | ||
675 | } else { | ||||
684 | qCWarning(OkularUiDebug) << "Skipping malformed tool XML in AnnotationTools setting"; | 676 | qCWarning(OkularUiDebug) << "Skipping malformed tool XML in AnnotationTools setting"; | ||
685 | } | 677 | } | ||
678 | } | ||||
679 | } | ||||
686 | 680 | | |||
687 | // Create the AnnotationToolItems from the XML dom tree | 681 | QStringList toStringList() | ||
688 | QDomNode toolDescription = m_toolsDefinition.firstChild(); | | |||
689 | while ( toolDescription.isElement() ) | | |||
690 | { | | |||
691 | QDomElement toolElement = toolDescription.toElement(); | | |||
692 | if ( toolElement.tagName() == QLatin1String("tool") ) | | |||
693 | { | | |||
694 | AnnotationToolItem item; | | |||
695 | item.id = toolElement.attribute(QStringLiteral("id")).toInt(); | | |||
696 | if ( toolElement.hasAttribute( QStringLiteral("name") ) ) | | |||
697 | item.text = toolElement.attribute( QStringLiteral("name") ); | | |||
698 | else | | |||
699 | item.text = defaultToolName( toolElement ); | | |||
700 | item.pixmap = makeToolPixmap( toolElement ); | | |||
701 | QDomNode shortcutNode = toolElement.elementsByTagName( QStringLiteral("shortcut") ).item( 0 ); | | |||
702 | if ( shortcutNode.isElement() ) | | |||
703 | item.shortcut = shortcutNode.toElement().text(); | | |||
704 | QDomNodeList engineNodeList = toolElement.elementsByTagName( QStringLiteral("engine") ); | | |||
705 | if ( engineNodeList.size() > 0 ) | | |||
706 | { | 682 | { | ||
707 | QDomElement engineEl = engineNodeList.item( 0 ).toElement(); | 683 | QStringList tools; | ||
708 | if ( !engineEl.isNull() && engineEl.hasAttribute( QStringLiteral("type") ) ) | 684 | QDomElement toolElement = m_toolsDefinition.documentElement().firstChildElement(); | ||
709 | item.isText = engineEl.attribute( QStringLiteral("type") ) == QLatin1String( "TextSelector" ); | 685 | QString str; | ||
686 | QTextStream stream(&str); | ||||
687 | while( !toolElement.isNull() ) | ||||
688 | { | ||||
689 | str.clear(); | ||||
690 | toolElement.save(stream, -1 /* indent disabled */); | ||||
691 | tools << str; | ||||
692 | toolElement = toolElement.nextSiblingElement(); | ||||
710 | } | 693 | } | ||
711 | m_items.push_back( item ); | 694 | return tools; | ||
712 | } | 695 | } | ||
713 | toolDescription = toolDescription.nextSibling(); | 696 | | ||
697 | QDomElement tool( int toolID ) | ||||
698 | { | ||||
699 | QDomElement toolElement = m_toolsDefinition.documentElement().firstChildElement(); | ||||
700 | while( !toolElement.isNull() && toolElement.attribute("id").toInt() != toolID ) { | ||||
701 | toolElement = toolElement.nextSiblingElement(); | ||||
714 | } | 702 | } | ||
703 | return toolElement; // can return a null element | ||||
715 | } | 704 | } | ||
716 | 705 | | |||
717 | PageViewAnnotator::~PageViewAnnotator() | 706 | void appendTool( QDomElement toolElement ) | ||
718 | { | 707 | { | ||
719 | delete m_engine; | 708 | toolElement = toolElement.cloneNode().toElement(); | ||
709 | toolElement.setAttribute( QStringLiteral( "id" ), ++m_toolsCount ); | ||||
710 | m_toolsDefinition.documentElement().appendChild( toolElement ); | ||||
720 | } | 711 | } | ||
721 | 712 | | |||
722 | void PageViewAnnotator::setEnabled( bool on ) | 713 | bool updateTool( QDomElement newToolElement, int toolID ) | ||
723 | { | 714 | { | ||
724 | if ( !on ) | 715 | newToolElement = newToolElement.cloneNode().toElement(); | ||
725 | { | 716 | newToolElement.setAttribute( QStringLiteral( "id" ), toolID ); | ||
726 | // remove toolBar | 717 | QDomElement toolElement = tool( toolID ); | ||
727 | if ( m_toolBar ) | 718 | QDomNode oldTool = m_toolsDefinition.documentElement().replaceChild( newToolElement, toolElement ); | ||
728 | m_toolBar->hideAndDestroy(); | 719 | bool success = !oldTool.isNull(); | ||
729 | m_toolBar = nullptr; | 720 | return success; | ||
730 | // deactivate the active tool, if any | | |||
731 | slotToolSelected( -1 ); | | |||
732 | return; | | |||
733 | } | 721 | } | ||
734 | 722 | | |||
735 | // if no tools are defined, don't show the toolbar | 723 | private: | ||
736 | if ( !m_toolsDefinition.hasChildNodes() ) | 724 | QDomDocument m_toolsDefinition; | ||
737 | return; | 725 | int m_toolsCount; | ||
738 | 726 | | |||
739 | // create toolBar | 727 | }; | ||
740 | if ( !m_toolBar ) | | |||
741 | { | | |||
742 | m_toolBar = new PageViewToolBar( m_pageView, m_pageView->viewport() ); | | |||
743 | m_toolBar->setSide( (PageViewToolBar::Side)Okular::Settings::editToolBarPlacement() ); | | |||
744 | m_toolBar->setItems( m_items ); | | |||
745 | m_toolBar->setToolsEnabled( m_toolsEnabled ); | | |||
746 | m_toolBar->setTextToolsEnabled( m_textToolsEnabled ); | | |||
747 | connect(m_toolBar, &PageViewToolBar::toolSelected, this, &PageViewAnnotator::slotToolSelected); | | |||
748 | connect(m_toolBar, &PageViewToolBar::orientationChanged, this, &PageViewAnnotator::slotSaveToolbarOrientation); | | |||
749 | 728 | | |||
750 | connect(m_toolBar, &PageViewToolBar::buttonDoubleClicked, this, &PageViewAnnotator::slotToolDoubleClicked); | 729 | /** PageViewAnnotator **/ | ||
751 | m_toolBar->setCursor(Qt::ArrowCursor); | 730 | PageViewAnnotator::PageViewAnnotator( PageView * parent, Okular::Document * storage ) | ||
731 | : QObject( parent ), m_document( storage ), m_pageView( parent ), | ||||
732 | m_actionHandler( nullptr ), m_engine( nullptr ), m_continuousMode( true ), | ||||
733 | m_lastToolID( -1 ), m_lockedItem( nullptr ) | ||||
734 | { | ||||
735 | reparseConfig(); | ||||
752 | } | 736 | } | ||
753 | 737 | | |||
754 | // show the toolBar | 738 | void PageViewAnnotator::reparseConfig() | ||
755 | m_toolBar->showAndAnimate(); | 739 | { | ||
740 | // Read tool list from configuration. It's a list of XML <tool></tool> elements | ||||
741 | if( !m_toolsDefinition ) | ||||
742 | m_toolsDefinition = new AnnotationTools(); | ||||
743 | m_toolsDefinition->setTools( Okular::Settings::annotationTools() ); | ||||
744 | | ||||
745 | if( !m_favoriteToolsDefinition ) | ||||
746 | m_favoriteToolsDefinition = new AnnotationTools(); | ||||
747 | m_favoriteToolsDefinition->setTools( Okular::Settings::favoriteAnnotationTools() ); | ||||
748 | | ||||
749 | if ( Okular::Settings::identityAuthor().isEmpty() ) | ||||
750 | detachAnnotation(); | ||||
751 | | ||||
752 | if( m_actionHandler ) | ||||
753 | m_actionHandler->reparseTools(); | ||||
756 | } | 754 | } | ||
757 | 755 | | |||
758 | void PageViewAnnotator::setTextToolsEnabled( bool enabled ) | 756 | PageViewAnnotator::~PageViewAnnotator() | ||
759 | { | 757 | { | ||
760 | m_textToolsEnabled = enabled; | 758 | delete m_engine; | ||
761 | if ( m_toolBar ) | | |||
762 | m_toolBar->setTextToolsEnabled( m_textToolsEnabled ); | | |||
763 | } | 759 | } | ||
764 | 760 | | |||
765 | void PageViewAnnotator::setToolsEnabled( bool enabled ) | 761 | void PageViewAnnotator::setEnabled( bool on ) | ||
766 | { | 762 | { | ||
767 | m_toolsEnabled = enabled; | 763 | if ( !on ) | ||
768 | if ( m_toolBar ) | 764 | { | ||
769 | m_toolBar->setToolsEnabled( m_toolsEnabled ); | 765 | // deactivate the active tool, if any | ||
766 | selectTool( -1 ); | ||||
767 | return; | ||||
768 | } | ||||
770 | } | 769 | } | ||
771 | 770 | | |||
772 | void PageViewAnnotator::setHidingForced( bool forced ) | 771 | void PageViewAnnotator::setTextToolsEnabled( bool enabled ) | ||
773 | { | 772 | { | ||
774 | m_hidingWasForced = forced; | 773 | if ( m_actionHandler ) | ||
774 | m_actionHandler->setTextToolsEnabled( enabled ); | ||||
775 | } | 775 | } | ||
776 | 776 | | |||
777 | bool PageViewAnnotator::hidingWasForced() const | 777 | void PageViewAnnotator::setToolsEnabled( bool enabled ) | ||
778 | { | 778 | { | ||
779 | return m_hidingWasForced; | 779 | if ( m_actionHandler ) | ||
780 | m_actionHandler->setToolsEnabled( enabled ); | ||||
780 | } | 781 | } | ||
781 | 782 | | |||
782 | bool PageViewAnnotator::active() const | 783 | bool PageViewAnnotator::active() const | ||
783 | { | 784 | { | ||
784 | return m_engine && m_toolBar; | 785 | return m_engine != nullptr; | ||
785 | } | 786 | } | ||
786 | 787 | | |||
787 | bool PageViewAnnotator::annotating() const | 788 | bool PageViewAnnotator::annotating() const | ||
788 | { | 789 | { | ||
789 | return active() && m_lockedItem; | 790 | return active() && m_lockedItem; | ||
790 | } | 791 | } | ||
791 | 792 | | |||
792 | QCursor PageViewAnnotator::cursor() const | 793 | QCursor PageViewAnnotator::cursor() const | ||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 855 | { | |||
859 | annotation->setAuthor( Okular::Settings::identityAuthor() ); | 860 | annotation->setAuthor( Okular::Settings::identityAuthor() ); | ||
860 | m_document->addPageAnnotation( m_lockedItem->pageNumber(), annotation ); | 861 | m_document->addPageAnnotation( m_lockedItem->pageNumber(), annotation ); | ||
861 | 862 | | |||
862 | if ( annotation->openDialogAfterCreation() ) | 863 | if ( annotation->openDialogAfterCreation() ) | ||
863 | m_pageView->openAnnotationWindow( annotation, m_lockedItem->pageNumber() ); | 864 | m_pageView->openAnnotationWindow( annotation, m_lockedItem->pageNumber() ); | ||
864 | } | 865 | } | ||
865 | 866 | | |||
866 | if ( m_continuousMode ) | 867 | if ( m_continuousMode ) | ||
867 | slotToolSelected( m_lastToolID ); | 868 | selectTool( m_lastToolID ); | ||
868 | else | 869 | else | ||
869 | detachAnnotation(); | 870 | detachAnnotation(); | ||
870 | } | 871 | } | ||
871 | 872 | | |||
872 | return modifiedRect; | 873 | return modifiedRect; | ||
873 | } | 874 | } | ||
874 | 875 | | |||
875 | QRect PageViewAnnotator::routeMouseEvent( QMouseEvent * e, PageViewItem * item ) | 876 | QRect PageViewAnnotator::routeMouseEvent( QMouseEvent * e, PageViewItem * item ) | ||
Show All 12 Lines | 888 | { | |||
888 | // Unlike routeMouseEvent, routeTabletEvent must explicitly ignore events it doesn't care about so that | 889 | // Unlike routeMouseEvent, routeTabletEvent must explicitly ignore events it doesn't care about so that | ||
889 | // the corresponding mouse event will later be delivered. | 890 | // the corresponding mouse event will later be delivered. | ||
890 | if ( !item ) | 891 | if ( !item ) | ||
891 | { | 892 | { | ||
892 | e->ignore(); | 893 | e->ignore(); | ||
893 | return QRect(); | 894 | return QRect(); | ||
894 | } | 895 | } | ||
895 | 896 | | |||
896 | // We set all tablet events that take place over the annotations toolbar to ignore so that corresponding mouse | | |||
897 | // events will be delivered to the toolbar. However, we still allow the annotations code to handle | | |||
898 | // TabletMove and TabletRelease events in case the user is drawing an annotation onto the toolbar. | | |||
899 | const QPoint toolBarPos = m_toolBar->mapFromGlobal( e->globalPos() ); | | |||
900 | const QRect toolBarRect = m_toolBar->rect(); | | |||
901 | if ( toolBarRect.contains( toolBarPos ) ) | | |||
902 | { | | |||
903 | e->ignore(); | | |||
904 | if (e->type() == QEvent::TabletPress) | | |||
905 | return QRect(); | | |||
906 | } | | |||
907 | | ||||
908 | AnnotatorEngine::EventType eventType; | 897 | AnnotatorEngine::EventType eventType; | ||
909 | AnnotatorEngine::Button button; | 898 | AnnotatorEngine::Button button; | ||
910 | 899 | | |||
911 | // figure out the event type and button | 900 | // figure out the event type and button | ||
912 | AnnotatorEngine::decodeEvent( e, &eventType, &button ); | 901 | AnnotatorEngine::decodeEvent( e, &eventType, &button ); | ||
913 | 902 | | |||
914 | const QPointF globalPosF = e->globalPosF(); | 903 | const QPointF globalPosF = e->globalPosF(); | ||
915 | const QPointF localPosF = globalPosF - localOriginInGlobal; | 904 | const QPointF localPosF = globalPosF - localOriginInGlobal; | ||
916 | return performRouteMouseOrTabletEvent( eventType, button, localPosF, item ); | 905 | return performRouteMouseOrTabletEvent( eventType, button, localPosF, item ); | ||
917 | } | 906 | } | ||
918 | 907 | | |||
919 | bool PageViewAnnotator::routeKeyEvent( QKeyEvent * event ) | 908 | bool PageViewAnnotator::routeKeyEvent( QKeyEvent * event ) | ||
920 | { | 909 | { | ||
921 | if ( event->key() == Qt::Key_Escape ) | 910 | if ( event->key() == Qt::Key_Escape ) | ||
922 | { | 911 | { | ||
923 | detachAnnotation(); | 912 | detachAnnotation(); | ||
924 | return true; | 913 | return true; | ||
925 | } | 914 | } | ||
926 | return false; | 915 | return false; | ||
927 | } | 916 | } | ||
928 | 917 | | |||
929 | bool PageViewAnnotator::routePaints( const QRect & wantedRect ) const | 918 | bool PageViewAnnotator::routePaints( const QRect & wantedRect ) const | ||
930 | { | 919 | { | ||
931 | return m_engine && m_toolBar && wantedRect.intersects( m_lastDrawnRect ) && m_lockedItem; | 920 | return m_engine && wantedRect.intersects( m_lastDrawnRect ) && m_lockedItem; | ||
932 | } | 921 | } | ||
933 | 922 | | |||
934 | void PageViewAnnotator::routePaint( QPainter * painter, const QRect & paintRect ) | 923 | void PageViewAnnotator::routePaint( QPainter * painter, const QRect & paintRect ) | ||
935 | { | 924 | { | ||
936 | // if there's no locked item, then there's no decided place to draw on | 925 | // if there's no locked item, then there's no decided place to draw on | ||
937 | if ( !m_lockedItem ) | 926 | if ( !m_lockedItem ) | ||
938 | return; | 927 | return; | ||
939 | 928 | | |||
Show All 12 Lines | 933 | #endif | |||
952 | QRect annotRect = paintRect.intersected( m_lastDrawnRect ); | 941 | QRect annotRect = paintRect.intersected( m_lastDrawnRect ); | ||
953 | annotRect.translate( -itemRect.topLeft() ); | 942 | annotRect.translate( -itemRect.topLeft() ); | ||
954 | 943 | | |||
955 | // use current engine for painting (in virtual page coordinates) | 944 | // use current engine for painting (in virtual page coordinates) | ||
956 | m_engine->paint( painter, m_lockedItem->uncroppedWidth(), m_lockedItem->uncroppedHeight(), annotRect ); | 945 | m_engine->paint( painter, m_lockedItem->uncroppedWidth(), m_lockedItem->uncroppedHeight(), annotRect ); | ||
957 | painter->restore(); | 946 | painter->restore(); | ||
958 | } | 947 | } | ||
959 | 948 | | |||
960 | void PageViewAnnotator::slotToolSelected( int toolID ) | 949 | void PageViewAnnotator::selectTool( int toolID ) | ||
950 | { | ||||
951 | | ||||
952 | // ask for Author's name if not already set | ||||
953 | if ( toolID > 0 && Okular::Settings::identityAuthor().isEmpty() ) | ||||
954 | { | ||||
955 | // get default username from the kdelibs/kdecore/KUser | ||||
956 | KUser currentUser; | ||||
957 | QString userName = currentUser.property( KUser::FullName ).toString(); | ||||
958 | userName = ""; | ||||
959 | // ask the user for confirmation/change | ||||
960 | if ( userName.isEmpty() ) | ||||
961 | { | ||||
962 | bool ok = false; | ||||
963 | userName = QInputDialog::getText(nullptr, | ||||
964 | i18n( "Bookmark annotation" ), | ||||
965 | i18n( "Insert a custom name for the annotation:" ), | ||||
966 | QLineEdit::Normal, | ||||
967 | QString(), | ||||
968 | &ok ); | ||||
969 | | ||||
970 | if ( !ok ) | ||||
961 | { | 971 | { | ||
972 | detachAnnotation(); | ||||
973 | return; | ||||
974 | } | ||||
975 | } | ||||
976 | // save the name | ||||
977 | Okular::Settings::setIdentityAuthor( userName ); | ||||
978 | Okular::Settings::self()->save(); | ||||
979 | } | ||||
980 | | ||||
962 | // terminate any previous operation | 981 | // terminate any previous operation | ||
963 | if ( m_engine ) | 982 | if ( m_engine ) | ||
964 | { | 983 | { | ||
965 | delete m_engine; | 984 | delete m_engine; | ||
966 | m_engine = nullptr; | 985 | m_engine = nullptr; | ||
967 | } | 986 | } | ||
968 | m_lockedItem = nullptr; | 987 | m_lockedItem = nullptr; | ||
969 | if ( m_lastDrawnRect.isValid() ) | 988 | if ( m_lastDrawnRect.isValid() ) | ||
970 | { | 989 | { | ||
971 | m_pageView->viewport()->update( m_lastDrawnRect.translated( -m_pageView->contentAreaPosition() ) ); | 990 | m_pageView->viewport()->update( m_lastDrawnRect.translated( -m_pageView->contentAreaPosition() ) ); | ||
972 | m_lastDrawnRect = QRect(); | 991 | m_lastDrawnRect = QRect(); | ||
973 | } | 992 | } | ||
974 | 993 | | |||
975 | if ( toolID != m_lastToolID ) m_continuousMode = false; | | |||
976 | // store current tool for later usage | 994 | // store current tool for later usage | ||
977 | m_lastToolID = toolID; | 995 | m_lastToolID = toolID; | ||
978 | 996 | | |||
979 | // handle tool deselection | 997 | // handle tool deselection | ||
980 | if ( toolID == -1 ) | 998 | if ( toolID == -1 ) | ||
981 | { | 999 | { | ||
982 | m_pageView->displayMessage( QString() ); | 1000 | m_pageView->displayMessage( QString() ); | ||
983 | m_pageView->updateCursor(); | 1001 | m_pageView->updateCursor(); | ||
984 | return; | 1002 | return; | ||
985 | } | 1003 | } | ||
986 | 1004 | | |||
987 | // for the selected tool create the Engine | 1005 | // for the selected tool create the Engine | ||
988 | QDomNode toolNode = m_toolsDefinition.firstChild(); | 1006 | QDomElement toolElement = m_toolsDefinition->tool( toolID ); | ||
989 | while ( toolNode.isElement() ) | 1007 | if ( !toolElement.isNull() ) | ||
990 | { | 1008 | { | ||
991 | QDomElement toolElement = toolNode.toElement(); | | |||
992 | toolNode = toolNode.nextSibling(); | | |||
993 | | ||||
994 | // only find out the element describing selected tool | | |||
995 | if ( toolElement.tagName() != QLatin1String("tool") || toolElement.attribute(QStringLiteral("id")).toInt() != toolID ) | | |||
996 | continue; | | |||
997 | | ||||
998 | // parse tool properties | 1009 | // parse tool properties | ||
999 | QDomNode toolSubNode = toolElement.firstChild(); | 1010 | QDomElement engineElement = toolElement.firstChildElement( QStringLiteral("engine") ); | ||
1000 | while ( toolSubNode.isElement() ) | 1011 | if ( !engineElement.isNull() ) | ||
1001 | { | 1012 | { | ||
1002 | QDomElement toolSubElement = toolSubNode.toElement(); | | |||
1003 | toolSubNode = toolSubNode.nextSibling(); | | |||
1004 | | ||||
1005 | // create the AnnotatorEngine | 1013 | // create the AnnotatorEngine | ||
1006 | if ( toolSubElement.tagName() == QLatin1String("engine") ) | 1014 | QString type = engineElement.attribute( QStringLiteral("type") ); | ||
1007 | { | | |||
1008 | QString type = toolSubElement.attribute( QStringLiteral("type") ); | | |||
1009 | if ( type == QLatin1String("SmoothLine") ) | 1015 | if ( type == QLatin1String("SmoothLine") ) | ||
1010 | m_engine = new SmoothPathEngine( toolSubElement ); | 1016 | m_engine = new SmoothPathEngine( engineElement ); | ||
1011 | else if ( type == QLatin1String("PickPoint") ) | 1017 | else if ( type == QLatin1String("PickPoint") ) | ||
1012 | m_engine = new PickPointEngine( toolSubElement ); | 1018 | m_engine = new PickPointEngine( engineElement ); | ||
1013 | else if ( type == QLatin1String("PolyLine") ) | 1019 | else if ( type == QLatin1String("PolyLine") ) | ||
1014 | m_engine = new PolyLineEngine( toolSubElement ); | 1020 | m_engine = new PolyLineEngine( engineElement ); | ||
1015 | else if ( type == QLatin1String("TextSelector") ) | 1021 | else if ( type == QLatin1String("TextSelector") ) | ||
1016 | m_engine = new TextSelectorEngine( toolSubElement, m_pageView ); | 1022 | m_engine = new TextSelectorEngine( engineElement, m_pageView ); | ||
1017 | else | 1023 | else | ||
1018 | qCWarning(OkularUiDebug).nospace() << "tools.xml: engine type:'" << type << "' is not defined!"; | 1024 | qCWarning(OkularUiDebug).nospace() << "tools.xml: engine type:'" << type << "' is not defined!"; | ||
1019 | } | | |||
1020 | 1025 | | |||
1021 | // display the tooltip | 1026 | // display the tooltip | ||
1022 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | 1027 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | ||
1023 | QString tip; | 1028 | QString tip; | ||
1024 | 1029 | | |||
1025 | if ( annotType == QLatin1String("ellipse") ) | 1030 | if ( annotType == QLatin1String("ellipse") ) | ||
1026 | tip = i18nc( "Annotation tool", "Draw an ellipse (drag to select a zone)" ); | 1031 | tip = i18nc( "Annotation tool", "Draw an ellipse (drag to select a zone)" ); | ||
1027 | else if ( annotType == QLatin1String("highlight") ) | 1032 | else if ( annotType == QLatin1String("highlight") ) | ||
Show All 27 Lines | |||||
1055 | 1060 | | |||
1056 | // consistency warning | 1061 | // consistency warning | ||
1057 | if ( !m_engine ) | 1062 | if ( !m_engine ) | ||
1058 | { | 1063 | { | ||
1059 | qCWarning(OkularUiDebug) << "tools.xml: couldn't find good engine description. check xml."; | 1064 | qCWarning(OkularUiDebug) << "tools.xml: couldn't find good engine description. check xml."; | ||
1060 | } | 1065 | } | ||
1061 | 1066 | | |||
1062 | m_pageView->updateCursor(); | 1067 | m_pageView->updateCursor(); | ||
1063 | // stop after parsing selected tool's node | | |||
1064 | break; | | |||
1065 | } | 1068 | } | ||
1069 | | ||||
1070 | if( toolID > 0 ) | ||||
1071 | emit toolSelected(); | ||||
1072 | } | ||||
1073 | | ||||
1074 | int PageViewAnnotator::setFavoriteTool( int favToolID ) | ||||
1075 | { | ||||
1076 | QDomElement favToolElement = m_favoriteToolsDefinition->tool( favToolID ); | ||||
1077 | int toolID = favToolElement.attribute( QStringLiteral("sourceId") ).toInt(); | ||||
1078 | m_toolsDefinition->updateTool( favToolElement, toolID ); | ||||
1079 | saveAnnotationTools(); | ||||
1080 | return toolID; | ||||
1066 | } | 1081 | } | ||
1067 | 1082 | | |||
1068 | void PageViewAnnotator::slotSaveToolbarOrientation( int side ) | 1083 | void PageViewAnnotator::bookmarkAnnotation() | ||
1069 | { | 1084 | { | ||
1070 | Okular::Settings::setEditToolBarPlacement( (int)side ); | 1085 | QDomElement sourceToolElement = m_toolsDefinition->tool( m_lastToolID ); | ||
1071 | Okular::Settings::self()->save(); | 1086 | if( sourceToolElement.isNull() ) | ||
1087 | return; | ||||
1088 | | ||||
1089 | bool ok = false; | ||||
1090 | QString itemText = QInputDialog::getText(nullptr, | ||||
1091 | i18n( "Add favorite annotation" ), | ||||
1092 | i18n( "Custom annotation name:" ), | ||||
1093 | QLineEdit::Normal, | ||||
1094 | defaultToolName( sourceToolElement ), | ||||
1095 | &ok ); | ||||
1096 | if( !ok ) | ||||
1097 | return; | ||||
1098 | | ||||
1099 | QDomElement toolElement = sourceToolElement.cloneNode().toElement(); | ||||
1100 | // Store name attribute only if the user specified a customized name | ||||
1101 | if ( !itemText.isEmpty() ) | ||||
1102 | toolElement.setAttribute( QStringLiteral("name"), itemText ); | ||||
1103 | toolElement.setAttribute( QStringLiteral("sourceId"), sourceToolElement.attribute( QStringLiteral("id") ) ); | ||||
1104 | m_favoriteToolsDefinition->appendTool( toolElement ); | ||||
1105 | saveAnnotationTools(); | ||||
1106 | } | ||||
1107 | | ||||
1108 | void PageViewAnnotator::setContinuousMode( bool enabled ) | ||||
1109 | { | ||||
1110 | m_continuousMode = enabled; | ||||
1111 | } | ||||
1112 | | ||||
1113 | void PageViewAnnotator::slotAdvancedSettings() | ||||
1114 | { | ||||
1115 | QDomElement toolElement = m_toolsDefinition->tool( m_lastToolID ); | ||||
1116 | EditAnnotToolDialog t( nullptr, toolElement); | ||||
1117 | | ||||
1118 | if ( t.exec() != QDialog::Accepted ) | ||||
1119 | return; | ||||
1120 | | ||||
1121 | QDomElement toolElementUpdated = t.toolXml().documentElement(); | ||||
1122 | int toolID = toolElement.attribute( QStringLiteral("id") ).toInt(); | ||||
1123 | m_toolsDefinition->updateTool( toolElementUpdated, toolID ); | ||||
1124 | m_actionHandler->reparseTools(); | ||||
1125 | saveAnnotationTools(); | ||||
1126 | selectTool( m_lastToolID ); | ||||
1127 | } | ||||
1128 | | ||||
1129 | QDomElement PageViewAnnotator::builtinTool( int toolID ) | ||||
1130 | { | ||||
1131 | return m_toolsDefinition->tool( toolID ); | ||||
1072 | } | 1132 | } | ||
1073 | 1133 | | |||
1074 | void PageViewAnnotator::slotToolDoubleClicked( int /*toolID*/ ) | 1134 | QDomElement PageViewAnnotator::favoriteTool( int toolID ) | ||
1075 | { | 1135 | { | ||
1076 | m_continuousMode = true; | 1136 | return m_favoriteToolsDefinition->tool( toolID ); | ||
1137 | } | ||||
1138 | | ||||
1139 | QDomElement PageViewAnnotator::currentEngineElement() | ||||
1140 | { | ||||
1141 | return m_toolsDefinition->tool( m_lastToolID ).firstChildElement( QStringLiteral("engine") ); | ||||
1142 | } | ||||
1143 | | ||||
1144 | QDomElement PageViewAnnotator::currentAnnotationElement() | ||||
1145 | { | ||||
1146 | return currentEngineElement().firstChildElement( QStringLiteral("annotation") ); | ||||
1147 | } | ||||
1148 | | ||||
1149 | void PageViewAnnotator::setAnnotationColor( QColor color ) | ||||
1150 | { | ||||
1151 | currentEngineElement().setAttribute( QStringLiteral( "color" ), color.name(QColor::HexRgb ) ); | ||||
1152 | QDomElement annotationElement = currentAnnotationElement(); | ||||
1153 | QString annotType = annotationElement.attribute( QStringLiteral( "type" ) ); | ||||
1154 | if ( annotType == "Typewriter" || annotType == "FreeText" ) { | ||||
1155 | annotationElement.setAttribute( QStringLiteral( "textColor" ), color.name( QColor::HexRgb ) ); | ||||
1156 | } else { | ||||
1157 | annotationElement.setAttribute( QStringLiteral( "color" ), color.name( QColor::HexRgb ) ); | ||||
1158 | } | ||||
1159 | saveAnnotationTools(); | ||||
1160 | selectTool( m_lastToolID ); | ||||
1161 | } | ||||
1162 | | ||||
1163 | void PageViewAnnotator::setAnnotationInnerColor( QColor color ) | ||||
1164 | { | ||||
1165 | QDomElement annotationElement = currentAnnotationElement(); | ||||
1166 | QString annotType = annotationElement.attribute( QStringLiteral( "type" ) ); | ||||
1167 | if ( annotType == "FreeText" ) { | ||||
1168 | annotationElement.setAttribute( QStringLiteral( "color" ), color.name( QColor::HexRgb ) ); | ||||
1169 | } else { | ||||
1170 | annotationElement.setAttribute( QStringLiteral( "innerColor" ), color.name( QColor::HexRgb ) ); | ||||
1171 | } | ||||
1172 | saveAnnotationTools(); | ||||
1173 | selectTool( m_lastToolID ); | ||||
1174 | } | ||||
1175 | | ||||
1176 | void PageViewAnnotator::setAnnotationOpacity( double opacity ) | ||||
1177 | { | ||||
1178 | currentAnnotationElement().setAttribute( QStringLiteral( "opacity" ), opacity ); | ||||
1179 | saveAnnotationTools(); | ||||
1180 | selectTool( m_lastToolID ); | ||||
1181 | } | ||||
1182 | | ||||
1183 | void PageViewAnnotator::setAnnotationFont( QFont font ) | ||||
1184 | { | ||||
1185 | currentAnnotationElement().setAttribute( QStringLiteral( "font" ), font.toString() ); | ||||
1186 | saveAnnotationTools(); | ||||
1187 | selectTool( m_lastToolID ); | ||||
1188 | } | ||||
1189 | | ||||
1190 | void PageViewAnnotator::setAnnotationWidth( double width) | ||||
1191 | { | ||||
1192 | currentAnnotationElement().setAttribute( QStringLiteral( "width" ), width ); | ||||
1193 | saveAnnotationTools(); | ||||
1194 | selectTool( m_lastToolID ); | ||||
1077 | } | 1195 | } | ||
1078 | 1196 | | |||
1079 | void PageViewAnnotator::detachAnnotation() | 1197 | void PageViewAnnotator::detachAnnotation() | ||
1080 | { | 1198 | { | ||
1081 | m_toolBar->selectButton( -1 ); | 1199 | selectTool( -1 ); | ||
1200 | if( m_actionHandler ) | ||||
1201 | m_actionHandler->deselectAllAnnotationActions(); | ||||
1202 | } | ||||
1203 | | ||||
1204 | void PageViewAnnotator::saveAnnotationTools() | ||||
1205 | { | ||||
1206 | Okular::Settings::setAnnotationTools( m_toolsDefinition->toStringList() ); | ||||
1207 | Okular::Settings::setFavoriteAnnotationTools( m_favoriteToolsDefinition->toStringList() ); | ||||
1208 | Okular::Settings::self()->save(); | ||||
1082 | } | 1209 | } | ||
1083 | 1210 | | |||
1084 | QString PageViewAnnotator::defaultToolName( const QDomElement &toolElement ) | 1211 | QString PageViewAnnotator::defaultToolName( const QDomElement &toolElement ) | ||
1085 | { | 1212 | { | ||
1086 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | 1213 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | ||
1087 | 1214 | | |||
1088 | if ( annotType == QLatin1String("ellipse") ) | 1215 | if ( annotType == QLatin1String("ellipse") ) | ||
1089 | return i18n( "Ellipse" ); | 1216 | return i18n( "Ellipse" ); | ||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Line(s) | 1397 | { | |||
1271 | /* Unrecognized annotation type -- It shouldn't happen */ | 1398 | /* Unrecognized annotation type -- It shouldn't happen */ | ||
1272 | p.setPen( QPen( engineColor ) ); | 1399 | p.setPen( QPen( engineColor ) ); | ||
1273 | p.drawText( QPoint(20, 31), QStringLiteral("?") ); | 1400 | p.drawText( QPoint(20, 31), QStringLiteral("?") ); | ||
1274 | } | 1401 | } | ||
1275 | 1402 | | |||
1276 | return pixmap; | 1403 | return pixmap; | ||
1277 | } | 1404 | } | ||
1278 | 1405 | | |||
1406 | void PageViewAnnotator::setupActions( KActionCollection * ac ) | ||||
1407 | { | ||||
1408 | if ( !m_actionHandler ) | ||||
1409 | { | ||||
1410 | m_actionHandler = new AnnotationActionHandler( this, ac ); | ||||
1411 | connect( m_actionHandler, &AnnotationActionHandler::advancedSettingsTriggered, this, &PageViewAnnotator::slotAdvancedSettings ); | ||||
1412 | } | ||||
1413 | } | ||||
1414 | | ||||
1279 | #include "moc_pageviewannotator.cpp" | 1415 | #include "moc_pageviewannotator.cpp" | ||
1280 | 1416 | | |||
1281 | /* kate: replace-tabs on; indent-width 4; */ | 1417 | /* kate: replace-tabs on; indent-width 4; */ |