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 603 Lines • ▼ Show 20 Line(s) | 652 | private: | |||
651 | // data | 653 | // data | ||
652 | PageView * m_pageView; | 654 | PageView * m_pageView; | ||
653 | // TODO: support more pages | 655 | // TODO: support more pages | ||
654 | std::unique_ptr<Okular::RegularAreaRect> selection; | 656 | std::unique_ptr<Okular::RegularAreaRect> selection; | ||
655 | Okular::NormalizedPoint lastPoint; | 657 | Okular::NormalizedPoint lastPoint; | ||
656 | QRect rect; | 658 | QRect rect; | ||
657 | }; | 659 | }; | ||
658 | 660 | | |||
659 | 661 | /** @short AnnotationTools*/ | |||
660 | /** PageViewAnnotator **/ | 662 | class AnnotationTools | ||
661 | | ||||
662 | PageViewAnnotator::PageViewAnnotator( PageView * parent, Okular::Document * storage ) | | |||
663 | : QObject( parent ), m_document( storage ), m_pageView( parent ), | | |||
664 | m_toolBar( nullptr ), m_engine( nullptr ), m_textToolsEnabled( false ), m_toolsEnabled( false ), | | |||
665 | m_continuousMode( false ), m_hidingWasForced( false ), m_lastToolID( -1 ), m_lockedItem( nullptr ) | | |||
666 | { | 663 | { | ||
667 | reparseConfig(); | 664 | public: | ||
668 | } | 665 | AnnotationTools() : m_toolsCount( 0 ) {} | ||
669 | 666 | | |||
670 | void PageViewAnnotator::reparseConfig() | 667 | void setTools( QStringList tools ) | ||
671 | { | 668 | { | ||
672 | m_items.clear(); | | |||
673 | | ||||
674 | // Read tool list from configuration. It's a list of XML <tool></tool> elements | | |||
675 | const QStringList userTools = Okular::Settings::annotationTools(); | | |||
676 | | ||||
677 | // Populate m_toolsDefinition | 669 | // Populate m_toolsDefinition | ||
678 | QDomDocument doc; | 670 | m_toolsCount = 0; | ||
679 | m_toolsDefinition = doc.createElement( QStringLiteral("annotatingTools") ); | 671 | m_toolsDefinition.clear(); | ||
680 | foreach ( const QString &toolXml, userTools ) | 672 | QDomElement root = m_toolsDefinition.createElement( QStringLiteral( "root" ) ); | ||
673 | m_toolsDefinition.appendChild( root ); | ||||
674 | foreach ( const QString &toolXml, tools ) | ||||
681 | { | 675 | { | ||
682 | QDomDocument entryParser; | 676 | QDomDocument entryParser; | ||
683 | if ( entryParser.setContent( toolXml ) ) | 677 | if ( entryParser.setContent( toolXml ) ) { | ||
684 | m_toolsDefinition.appendChild( doc.importNode( entryParser.documentElement(), true ) ); | 678 | root.appendChild( m_toolsDefinition.importNode( entryParser.documentElement(), true ) ); | ||
685 | else | 679 | m_toolsCount++; | ||
680 | } else { | ||||
686 | qCWarning(OkularUiDebug) << "Skipping malformed tool XML in AnnotationTools setting"; | 681 | qCWarning(OkularUiDebug) << "Skipping malformed tool XML in AnnotationTools setting"; | ||
687 | } | 682 | } | ||
688 | | ||||
689 | // Create the AnnotationToolItems from the XML dom tree | | |||
690 | QDomNode toolDescription = m_toolsDefinition.firstChild(); | | |||
691 | while ( toolDescription.isElement() ) | | |||
692 | { | | |||
693 | QDomElement toolElement = toolDescription.toElement(); | | |||
694 | if ( toolElement.tagName() == QLatin1String("tool") ) | | |||
695 | { | | |||
696 | AnnotationToolItem item; | | |||
697 | item.id = toolElement.attribute(QStringLiteral("id")).toInt(); | | |||
698 | if ( toolElement.hasAttribute( QStringLiteral("name") ) ) | | |||
699 | item.text = toolElement.attribute( QStringLiteral("name") ); | | |||
700 | else | | |||
701 | item.text = defaultToolName( toolElement ); | | |||
702 | item.pixmap = makeToolPixmap( toolElement ); | | |||
703 | QDomNode shortcutNode = toolElement.elementsByTagName( QStringLiteral("shortcut") ).item( 0 ); | | |||
704 | if ( shortcutNode.isElement() ) | | |||
705 | item.shortcut = shortcutNode.toElement().text(); | | |||
706 | QDomNodeList engineNodeList = toolElement.elementsByTagName( QStringLiteral("engine") ); | | |||
707 | if ( engineNodeList.size() > 0 ) | | |||
708 | { | | |||
709 | QDomElement engineEl = engineNodeList.item( 0 ).toElement(); | | |||
710 | if ( !engineEl.isNull() && engineEl.hasAttribute( QStringLiteral("type") ) ) | | |||
711 | item.isText = engineEl.attribute( QStringLiteral("type") ) == QLatin1String( "TextSelector" ); | | |||
712 | } | 683 | } | ||
713 | m_items.push_back( item ); | | |||
714 | } | 684 | } | ||
715 | toolDescription = toolDescription.nextSibling(); | 685 | | ||
686 | QStringList toStringList() | ||||
687 | { | ||||
688 | QStringList tools; | ||||
689 | QDomElement toolElement = m_toolsDefinition.documentElement().firstChildElement(); | ||||
690 | QString str; | ||||
691 | QTextStream stream(&str); | ||||
692 | while( !toolElement.isNull() ) | ||||
693 | { | ||||
694 | str.clear(); | ||||
695 | toolElement.save(stream, -1 /* indent disabled */); | ||||
696 | tools << str; | ||||
697 | toolElement = toolElement.nextSiblingElement(); | ||||
716 | } | 698 | } | ||
699 | return tools; | ||||
717 | } | 700 | } | ||
718 | 701 | | |||
719 | PageViewAnnotator::~PageViewAnnotator() | 702 | QDomElement tool( int toolID ) | ||
720 | { | 703 | { | ||
721 | delete m_engine; | 704 | QDomElement toolElement = m_toolsDefinition.documentElement().firstChildElement(); | ||
705 | while( !toolElement.isNull() && toolElement.attribute("id").toInt() != toolID ) { | ||||
706 | toolElement = toolElement.nextSiblingElement(); | ||||
707 | } | ||||
708 | return toolElement; // can return a null element | ||||
722 | } | 709 | } | ||
723 | 710 | | |||
724 | void PageViewAnnotator::setEnabled( bool on ) | 711 | void appendTool( QDomElement toolElement ) | ||
725 | { | | |||
726 | if ( !on ) | | |||
727 | { | 712 | { | ||
728 | // remove toolBar | 713 | toolElement = toolElement.cloneNode().toElement(); | ||
729 | if ( m_toolBar ) | 714 | toolElement.setAttribute( QStringLiteral( "id" ), ++m_toolsCount ); | ||
730 | m_toolBar->hideAndDestroy(); | 715 | m_toolsDefinition.documentElement().appendChild( toolElement ); | ||
731 | m_toolBar = nullptr; | | |||
732 | // deactivate the active tool, if any | | |||
733 | slotToolSelected( -1 ); | | |||
734 | return; | | |||
735 | } | 716 | } | ||
736 | 717 | | |||
737 | // if no tools are defined, don't show the toolbar | 718 | bool updateTool( QDomElement newToolElement, int toolID ) | ||
738 | if ( !m_toolsDefinition.hasChildNodes() ) | | |||
739 | return; | | |||
740 | | ||||
741 | // create toolBar | | |||
742 | if ( !m_toolBar ) | | |||
743 | { | 719 | { | ||
744 | m_toolBar = new PageViewToolBar( m_pageView, m_pageView->viewport() ); | 720 | QDomElement toolElement = tool( toolID ); | ||
745 | m_toolBar->setSide( (PageViewToolBar::Side)Okular::Settings::editToolBarPlacement() ); | 721 | if ( toolElement.isNull() ) | ||
746 | m_toolBar->setItems( m_items ); | 722 | return false; | ||
747 | m_toolBar->setToolsEnabled( m_toolsEnabled ); | 723 | newToolElement = newToolElement.cloneNode().toElement(); | ||
748 | m_toolBar->setTextToolsEnabled( m_textToolsEnabled ); | 724 | newToolElement.setAttribute( QStringLiteral( "id" ), toolID ); | ||
749 | connect(m_toolBar, &PageViewToolBar::toolSelected, this, &PageViewAnnotator::slotToolSelected); | 725 | QDomNode oldTool = m_toolsDefinition.documentElement().replaceChild( newToolElement, toolElement ); | ||
750 | connect(m_toolBar, &PageViewToolBar::orientationChanged, this, &PageViewAnnotator::slotSaveToolbarOrientation); | 726 | return !oldTool.isNull(); | ||
751 | | ||||
752 | connect(m_toolBar, &PageViewToolBar::buttonDoubleClicked, this, &PageViewAnnotator::slotToolDoubleClicked); | | |||
753 | m_toolBar->setCursor(Qt::ArrowCursor); | | |||
754 | } | 727 | } | ||
755 | 728 | | |||
756 | // show the toolBar | 729 | private: | ||
757 | m_toolBar->showAndAnimate(); | 730 | QDomDocument m_toolsDefinition; | ||
758 | } | 731 | int m_toolsCount; | ||
732 | }; | ||||
759 | 733 | | |||
760 | void PageViewAnnotator::setTextToolsEnabled( bool enabled ) | 734 | /** PageViewAnnotator **/ | ||
761 | { | 735 | const int PageViewAnnotator::STAMP_TOOL_ID = 14; | ||
762 | m_textToolsEnabled = enabled; | | |||
763 | if ( m_toolBar ) | | |||
764 | m_toolBar->setTextToolsEnabled( m_textToolsEnabled ); | | |||
765 | } | | |||
766 | 736 | | |||
767 | void PageViewAnnotator::setToolsEnabled( bool enabled ) | 737 | PageViewAnnotator::PageViewAnnotator( PageView * parent, Okular::Document * storage ) | ||
738 | : QObject( parent ), m_document( storage ), m_pageView( parent ), | ||||
739 | m_actionHandler( nullptr ), m_engine( nullptr ), m_toolsDefinition( nullptr ), | ||||
740 | m_quickToolsDefinition( nullptr ), m_continuousMode( true ), m_lastToolID( -1 ), | ||||
741 | m_lockedItem( nullptr ) | ||||
768 | { | 742 | { | ||
769 | m_toolsEnabled = enabled; | 743 | reparseConfig(); | ||
770 | if ( m_toolBar ) | | |||
771 | m_toolBar->setToolsEnabled( m_toolsEnabled ); | | |||
772 | } | 744 | } | ||
773 | 745 | | |||
774 | void PageViewAnnotator::setHidingForced( bool forced ) | 746 | void PageViewAnnotator::reparseConfig() | ||
775 | { | 747 | { | ||
776 | m_hidingWasForced = forced; | 748 | // Read tool list from configuration. It's a list of XML <tool></tool> elements | ||
749 | if( !m_toolsDefinition ) | ||||
750 | m_toolsDefinition = new AnnotationTools(); | ||||
751 | m_toolsDefinition->setTools( Okular::Settings::annotationTools() ); | ||||
752 | | ||||
753 | if( !m_quickToolsDefinition ) | ||||
754 | m_quickToolsDefinition = new AnnotationTools(); | ||||
755 | m_quickToolsDefinition->setTools( Okular::Settings::quickAnnotationTools() ); | ||||
756 | | ||||
757 | m_continuousMode = Okular::Settings::annotationContinuousMode(); | ||||
758 | | ||||
759 | if ( Okular::Settings::identityAuthor().isEmpty() ) | ||||
760 | detachAnnotation(); | ||||
761 | | ||||
762 | if( m_actionHandler ) | ||||
763 | m_actionHandler->reparseTools(); | ||||
777 | } | 764 | } | ||
778 | 765 | | |||
779 | bool PageViewAnnotator::hidingWasForced() const | 766 | PageViewAnnotator::~PageViewAnnotator() | ||
780 | { | 767 | { | ||
781 | return m_hidingWasForced; | 768 | delete m_engine; | ||
782 | } | 769 | } | ||
783 | 770 | | |||
784 | bool PageViewAnnotator::active() const | 771 | bool PageViewAnnotator::active() const | ||
785 | { | 772 | { | ||
786 | return m_engine && m_toolBar; | 773 | return m_engine != nullptr; | ||
787 | } | 774 | } | ||
788 | 775 | | |||
789 | bool PageViewAnnotator::annotating() const | 776 | bool PageViewAnnotator::annotating() const | ||
790 | { | 777 | { | ||
791 | return active() && m_lockedItem; | 778 | return active() && m_lockedItem; | ||
792 | } | 779 | } | ||
793 | 780 | | |||
794 | QCursor PageViewAnnotator::cursor() const | 781 | QCursor PageViewAnnotator::cursor() const | ||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Line(s) | 843 | { | |||
861 | annotation->setAuthor( Okular::Settings::identityAuthor() ); | 848 | annotation->setAuthor( Okular::Settings::identityAuthor() ); | ||
862 | m_document->addPageAnnotation( m_lockedItem->pageNumber(), annotation ); | 849 | m_document->addPageAnnotation( m_lockedItem->pageNumber(), annotation ); | ||
863 | 850 | | |||
864 | if ( annotation->openDialogAfterCreation() ) | 851 | if ( annotation->openDialogAfterCreation() ) | ||
865 | m_pageView->openAnnotationWindow( annotation, m_lockedItem->pageNumber() ); | 852 | m_pageView->openAnnotationWindow( annotation, m_lockedItem->pageNumber() ); | ||
866 | } | 853 | } | ||
867 | 854 | | |||
868 | if ( m_continuousMode ) | 855 | if ( m_continuousMode ) | ||
869 | slotToolSelected( m_lastToolID ); | 856 | selectTool( m_lastToolID ); | ||
870 | else | 857 | else | ||
871 | detachAnnotation(); | 858 | detachAnnotation(); | ||
872 | } | 859 | } | ||
873 | 860 | | |||
874 | return modifiedRect; | 861 | return modifiedRect; | ||
875 | } | 862 | } | ||
876 | 863 | | |||
877 | QRect PageViewAnnotator::routeMouseEvent( QMouseEvent * e, PageViewItem * item ) | 864 | QRect PageViewAnnotator::routeMouseEvent( QMouseEvent * e, PageViewItem * item ) | ||
Show All 12 Lines | 876 | { | |||
890 | // Unlike routeMouseEvent, routeTabletEvent must explicitly ignore events it doesn't care about so that | 877 | // Unlike routeMouseEvent, routeTabletEvent must explicitly ignore events it doesn't care about so that | ||
891 | // the corresponding mouse event will later be delivered. | 878 | // the corresponding mouse event will later be delivered. | ||
892 | if ( !item ) | 879 | if ( !item ) | ||
893 | { | 880 | { | ||
894 | e->ignore(); | 881 | e->ignore(); | ||
895 | return QRect(); | 882 | return QRect(); | ||
896 | } | 883 | } | ||
897 | 884 | | |||
898 | // We set all tablet events that take place over the annotations toolbar to ignore so that corresponding mouse | | |||
899 | // events will be delivered to the toolbar. However, we still allow the annotations code to handle | | |||
900 | // TabletMove and TabletRelease events in case the user is drawing an annotation onto the toolbar. | | |||
901 | const QPoint toolBarPos = m_toolBar->mapFromGlobal( e->globalPos() ); | | |||
902 | const QRect toolBarRect = m_toolBar->rect(); | | |||
903 | if ( toolBarRect.contains( toolBarPos ) ) | | |||
904 | { | | |||
905 | e->ignore(); | | |||
906 | if (e->type() == QEvent::TabletPress) | | |||
907 | return QRect(); | | |||
908 | } | | |||
909 | | ||||
910 | AnnotatorEngine::EventType eventType; | 885 | AnnotatorEngine::EventType eventType; | ||
911 | AnnotatorEngine::Button button; | 886 | AnnotatorEngine::Button button; | ||
912 | 887 | | |||
913 | // figure out the event type and button | 888 | // figure out the event type and button | ||
914 | AnnotatorEngine::decodeEvent( e, &eventType, &button ); | 889 | AnnotatorEngine::decodeEvent( e, &eventType, &button ); | ||
915 | 890 | | |||
916 | const QPointF globalPosF = e->globalPosF(); | 891 | const QPointF globalPosF = e->globalPosF(); | ||
917 | const QPointF localPosF = globalPosF - localOriginInGlobal; | 892 | const QPointF localPosF = globalPosF - localOriginInGlobal; | ||
918 | return performRouteMouseOrTabletEvent( eventType, button, localPosF, item ); | 893 | return performRouteMouseOrTabletEvent( eventType, button, localPosF, item ); | ||
919 | } | 894 | } | ||
920 | 895 | | |||
921 | bool PageViewAnnotator::routeKeyEvent( QKeyEvent * event ) | 896 | bool PageViewAnnotator::routeKeyEvent( QKeyEvent * event ) | ||
922 | { | 897 | { | ||
923 | if ( event->key() == Qt::Key_Escape ) | 898 | if ( event->key() == Qt::Key_Escape ) | ||
924 | { | 899 | { | ||
925 | detachAnnotation(); | 900 | detachAnnotation(); | ||
926 | return true; | 901 | return true; | ||
927 | } | 902 | } | ||
928 | return false; | 903 | return false; | ||
929 | } | 904 | } | ||
930 | 905 | | |||
931 | bool PageViewAnnotator::routePaints( const QRect & wantedRect ) const | 906 | bool PageViewAnnotator::routePaints( const QRect & wantedRect ) const | ||
932 | { | 907 | { | ||
933 | return m_engine && m_toolBar && wantedRect.intersects( m_lastDrawnRect ) && m_lockedItem; | 908 | return m_engine && wantedRect.intersects( m_lastDrawnRect ) && m_lockedItem; | ||
934 | } | 909 | } | ||
935 | 910 | | |||
936 | void PageViewAnnotator::routePaint( QPainter * painter, const QRect & paintRect ) | 911 | void PageViewAnnotator::routePaint( QPainter * painter, const QRect & paintRect ) | ||
937 | { | 912 | { | ||
938 | // if there's no locked item, then there's no decided place to draw on | 913 | // if there's no locked item, then there's no decided place to draw on | ||
939 | if ( !m_lockedItem ) | 914 | if ( !m_lockedItem ) | ||
940 | return; | 915 | return; | ||
941 | 916 | | |||
Show All 12 Lines | 921 | #endif | |||
954 | QRect annotRect = paintRect.intersected( m_lastDrawnRect ); | 929 | QRect annotRect = paintRect.intersected( m_lastDrawnRect ); | ||
955 | annotRect.translate( -itemRect.topLeft() ); | 930 | annotRect.translate( -itemRect.topLeft() ); | ||
956 | 931 | | |||
957 | // use current engine for painting (in virtual page coordinates) | 932 | // use current engine for painting (in virtual page coordinates) | ||
958 | m_engine->paint( painter, m_lockedItem->uncroppedWidth(), m_lockedItem->uncroppedHeight(), annotRect ); | 933 | m_engine->paint( painter, m_lockedItem->uncroppedWidth(), m_lockedItem->uncroppedHeight(), annotRect ); | ||
959 | painter->restore(); | 934 | painter->restore(); | ||
960 | } | 935 | } | ||
961 | 936 | | |||
962 | void PageViewAnnotator::slotToolSelected( int toolID ) | 937 | void PageViewAnnotator::selectTool( int toolID ) | ||
963 | { | 938 | { | ||
939 | | ||||
940 | // ask for Author's name if not already set | ||||
941 | if ( toolID > 0 && Okular::Settings::identityAuthor().isEmpty() ) | ||||
942 | { | ||||
943 | // get default username from the kdelibs/kdecore/KUser | ||||
944 | KUser currentUser; | ||||
945 | QString userName = currentUser.property( KUser::FullName ).toString(); | ||||
946 | userName = ""; | ||||
947 | // ask the user for confirmation/change | ||||
948 | if ( userName.isEmpty() ) | ||||
949 | { | ||||
950 | bool ok = false; | ||||
951 | userName = QInputDialog::getText(nullptr, | ||||
952 | i18n( "Bookmark annotation" ), | ||||
953 | i18n( "Insert a custom name for the annotation:" ), | ||||
954 | QLineEdit::Normal, | ||||
955 | QString(), | ||||
956 | &ok ); | ||||
957 | | ||||
958 | if ( !ok ) | ||||
959 | { | ||||
960 | detachAnnotation(); | ||||
961 | return; | ||||
962 | } | ||||
963 | } | ||||
964 | // save the name | ||||
965 | Okular::Settings::setIdentityAuthor( userName ); | ||||
966 | Okular::Settings::self()->save(); | ||||
967 | } | ||||
968 | | ||||
964 | // terminate any previous operation | 969 | // terminate any previous operation | ||
965 | if ( m_engine ) | 970 | if ( m_engine ) | ||
966 | { | 971 | { | ||
967 | delete m_engine; | 972 | delete m_engine; | ||
968 | m_engine = nullptr; | 973 | m_engine = nullptr; | ||
969 | } | 974 | } | ||
970 | m_lockedItem = nullptr; | 975 | m_lockedItem = nullptr; | ||
971 | if ( m_lastDrawnRect.isValid() ) | 976 | if ( m_lastDrawnRect.isValid() ) | ||
972 | { | 977 | { | ||
973 | m_pageView->viewport()->update( m_lastDrawnRect.translated( -m_pageView->contentAreaPosition() ) ); | 978 | m_pageView->viewport()->update( m_lastDrawnRect.translated( -m_pageView->contentAreaPosition() ) ); | ||
974 | m_lastDrawnRect = QRect(); | 979 | m_lastDrawnRect = QRect(); | ||
975 | } | 980 | } | ||
976 | 981 | | |||
977 | if ( toolID != m_lastToolID ) m_continuousMode = false; | | |||
978 | // store current tool for later usage | 982 | // store current tool for later usage | ||
979 | m_lastToolID = toolID; | 983 | m_lastToolID = toolID; | ||
980 | 984 | | |||
981 | // handle tool deselection | 985 | // handle tool deselection | ||
982 | if ( toolID == -1 ) | 986 | if ( toolID == -1 ) | ||
983 | { | 987 | { | ||
984 | m_pageView->displayMessage( QString() ); | 988 | m_pageView->displayMessage( QString() ); | ||
985 | m_pageView->updateCursor(); | 989 | m_pageView->updateCursor(); | ||
986 | return; | 990 | return; | ||
987 | } | 991 | } | ||
988 | 992 | | |||
989 | // for the selected tool create the Engine | 993 | // for the selected tool create the Engine | ||
990 | QDomNode toolNode = m_toolsDefinition.firstChild(); | 994 | QDomElement toolElement = m_toolsDefinition->tool( toolID ); | ||
991 | while ( toolNode.isElement() ) | 995 | if ( !toolElement.isNull() ) | ||
992 | { | 996 | { | ||
993 | QDomElement toolElement = toolNode.toElement(); | | |||
994 | toolNode = toolNode.nextSibling(); | | |||
995 | | ||||
996 | // only find out the element describing selected tool | | |||
997 | if ( toolElement.tagName() != QLatin1String("tool") || toolElement.attribute(QStringLiteral("id")).toInt() != toolID ) | | |||
998 | continue; | | |||
999 | | ||||
1000 | // parse tool properties | 997 | // parse tool properties | ||
1001 | QDomNode toolSubNode = toolElement.firstChild(); | 998 | QDomElement engineElement = toolElement.firstChildElement( QStringLiteral("engine") ); | ||
1002 | while ( toolSubNode.isElement() ) | 999 | if ( !engineElement.isNull() ) | ||
1003 | { | 1000 | { | ||
1004 | QDomElement toolSubElement = toolSubNode.toElement(); | | |||
1005 | toolSubNode = toolSubNode.nextSibling(); | | |||
1006 | | ||||
1007 | // create the AnnotatorEngine | 1001 | // create the AnnotatorEngine | ||
1008 | if ( toolSubElement.tagName() == QLatin1String("engine") ) | 1002 | QString type = engineElement.attribute( QStringLiteral("type") ); | ||
1009 | { | | |||
1010 | QString type = toolSubElement.attribute( QStringLiteral("type") ); | | |||
1011 | if ( type == QLatin1String("SmoothLine") ) | 1003 | if ( type == QLatin1String("SmoothLine") ) | ||
1012 | m_engine = new SmoothPathEngine( toolSubElement ); | 1004 | m_engine = new SmoothPathEngine( engineElement ); | ||
1013 | else if ( type == QLatin1String("PickPoint") ) | 1005 | else if ( type == QLatin1String("PickPoint") ) | ||
1014 | m_engine = new PickPointEngine( toolSubElement ); | 1006 | m_engine = new PickPointEngine( engineElement ); | ||
1015 | else if ( type == QLatin1String("PolyLine") ) | 1007 | else if ( type == QLatin1String("PolyLine") ) | ||
1016 | m_engine = new PolyLineEngine( toolSubElement ); | 1008 | m_engine = new PolyLineEngine( engineElement ); | ||
1017 | else if ( type == QLatin1String("TextSelector") ) | 1009 | else if ( type == QLatin1String("TextSelector") ) | ||
1018 | m_engine = new TextSelectorEngine( toolSubElement, m_pageView ); | 1010 | m_engine = new TextSelectorEngine( engineElement, m_pageView ); | ||
1019 | else | 1011 | else | ||
1020 | qCWarning(OkularUiDebug).nospace() << "tools.xml: engine type:'" << type << "' is not defined!"; | 1012 | qCWarning(OkularUiDebug).nospace() << "tools.xml: engine type:'" << type << "' is not defined!"; | ||
1021 | } | | |||
1022 | 1013 | | |||
1023 | // display the tooltip | 1014 | // display the tooltip | ||
1024 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | 1015 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | ||
1025 | QString tip; | 1016 | QString tip; | ||
1026 | 1017 | | |||
1027 | if ( annotType == QLatin1String("ellipse") ) | 1018 | if ( annotType == QLatin1String("ellipse") ) | ||
1028 | tip = i18nc( "Annotation tool", "Draw an ellipse (drag to select a zone)" ); | 1019 | tip = i18nc( "Annotation tool", "Draw an ellipse (drag to select a zone)" ); | ||
1029 | else if ( annotType == QLatin1String("highlight") ) | 1020 | else if ( annotType == QLatin1String("highlight") ) | ||
Show All 27 Lines | |||||
1057 | 1048 | | |||
1058 | // consistency warning | 1049 | // consistency warning | ||
1059 | if ( !m_engine ) | 1050 | if ( !m_engine ) | ||
1060 | { | 1051 | { | ||
1061 | qCWarning(OkularUiDebug) << "tools.xml: couldn't find good engine description. check xml."; | 1052 | qCWarning(OkularUiDebug) << "tools.xml: couldn't find good engine description. check xml."; | ||
1062 | } | 1053 | } | ||
1063 | 1054 | | |||
1064 | m_pageView->updateCursor(); | 1055 | m_pageView->updateCursor(); | ||
1065 | // stop after parsing selected tool's node | | |||
1066 | break; | | |||
1067 | } | | |||
1068 | } | 1056 | } | ||
1069 | 1057 | | |||
1070 | void PageViewAnnotator::slotSaveToolbarOrientation( int side ) | 1058 | if( toolID > 0 ) | ||
1071 | { | 1059 | emit toolSelected(); | ||
1072 | Okular::Settings::setEditToolBarPlacement( (int)side ); | | |||
1073 | Okular::Settings::self()->save(); | | |||
1074 | } | 1060 | } | ||
1075 | 1061 | | |||
1076 | void PageViewAnnotator::slotToolDoubleClicked( int /*toolID*/ ) | 1062 | void PageViewAnnotator::selectStampTool( const QString &stampSymbol) | ||
1077 | { | 1063 | { | ||
1078 | m_continuousMode = true; | 1064 | QDomElement toolElement = builtinTool( STAMP_TOOL_ID ); | ||
1065 | QDomElement engineElement = toolElement.firstChildElement( QStringLiteral("engine") ); | ||||
1066 | QDomElement annotationElement = engineElement.firstChildElement( QStringLiteral("annotation") ); | ||||
1067 | engineElement.setAttribute( QStringLiteral( "hoverIcon" ), stampSymbol ); | ||||
1068 | annotationElement.setAttribute( QStringLiteral( "icon" ), stampSymbol ); | ||||
1069 | saveAnnotationTools(); | ||||
1070 | selectTool( STAMP_TOOL_ID ); | ||||
1079 | } | 1071 | } | ||
1080 | 1072 | | |||
1081 | void PageViewAnnotator::detachAnnotation() | 1073 | void PageViewAnnotator::detachAnnotation() | ||
1082 | { | 1074 | { | ||
1083 | m_toolBar->selectButton( -1 ); | 1075 | selectTool( -1 ); | ||
1076 | if( m_actionHandler ) | ||||
1077 | m_actionHandler->deselectAllAnnotationActions(); | ||||
1084 | } | 1078 | } | ||
1085 | 1079 | | |||
1086 | QString PageViewAnnotator::defaultToolName( const QDomElement &toolElement ) | 1080 | QString PageViewAnnotator::defaultToolName( const QDomElement &toolElement ) | ||
1087 | { | 1081 | { | ||
1088 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | 1082 | const QString annotType = toolElement.attribute( QStringLiteral("type") ); | ||
1089 | 1083 | | |||
1090 | if ( annotType == QLatin1String("ellipse") ) | 1084 | if ( annotType == QLatin1String("ellipse") ) | ||
1091 | return i18n( "Ellipse" ); | 1085 | return i18n( "Ellipse" ); | ||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Line(s) | 1266 | { | |||
1273 | /* Unrecognized annotation type -- It shouldn't happen */ | 1267 | /* Unrecognized annotation type -- It shouldn't happen */ | ||
1274 | p.setPen( QPen( engineColor ) ); | 1268 | p.setPen( QPen( engineColor ) ); | ||
1275 | p.drawText( QPoint(20, 31), QStringLiteral("?") ); | 1269 | p.drawText( QPoint(20, 31), QStringLiteral("?") ); | ||
1276 | } | 1270 | } | ||
1277 | 1271 | | |||
1278 | return pixmap; | 1272 | return pixmap; | ||
1279 | } | 1273 | } | ||
1280 | 1274 | | |||
1275 | void PageViewAnnotator::setupActions( KActionCollection * ac ) | ||||
1276 | { | ||||
1277 | if ( !m_actionHandler ) { | ||||
1278 | m_actionHandler = new AnnotationActionHandler( this, ac ); | ||||
1279 | } | ||||
1280 | } | ||||
1281 | | ||||
1282 | bool PageViewAnnotator::continuousMode() | ||||
1283 | { | ||||
1284 | return m_continuousMode; | ||||
1285 | } | ||||
1286 | | ||||
1287 | void PageViewAnnotator::setContinuousMode( bool enabled ) | ||||
1288 | { | ||||
1289 | m_continuousMode = enabled; | ||||
1290 | Okular::Settings::setAnnotationContinuousMode( enabled ); | ||||
1291 | Okular::Settings::self()->save(); | ||||
1292 | } | ||||
1293 | | ||||
1294 | void PageViewAnnotator::setToolsEnabled( bool enabled ) | ||||
1295 | { | ||||
1296 | if ( m_actionHandler ) | ||||
1297 | m_actionHandler->setToolsEnabled( enabled ); | ||||
1298 | } | ||||
1299 | | ||||
1300 | void PageViewAnnotator::setTextToolsEnabled( bool enabled ) | ||||
1301 | { | ||||
1302 | if ( m_actionHandler ) | ||||
1303 | m_actionHandler->setTextToolsEnabled( enabled ); | ||||
1304 | } | ||||
1305 | | ||||
1306 | void PageViewAnnotator::saveAnnotationTools() | ||||
1307 | { | ||||
1308 | Okular::Settings::setAnnotationTools( m_toolsDefinition->toStringList() ); | ||||
1309 | Okular::Settings::setQuickAnnotationTools( m_quickToolsDefinition->toStringList() ); | ||||
1310 | Okular::Settings::self()->save(); | ||||
1311 | } | ||||
1312 | | ||||
1313 | int PageViewAnnotator::setQuickTool( int favToolID ) | ||||
1314 | { | ||||
1315 | int toolId = -1; | ||||
1316 | QDomElement favToolElement = m_quickToolsDefinition->tool( favToolID ); | ||||
1317 | if ( !favToolElement.isNull() && favToolElement.hasAttribute( QStringLiteral("sourceId") ) ) | ||||
1318 | { | ||||
1319 | toolId = favToolElement.attribute( QStringLiteral("sourceId") ).toInt(); | ||||
1320 | if ( m_toolsDefinition->updateTool( favToolElement, toolId ) ) | ||||
1321 | saveAnnotationTools(); | ||||
1322 | } | ||||
1323 | return toolId; | ||||
1324 | } | ||||
1325 | | ||||
1326 | QDomElement PageViewAnnotator::builtinTool( int toolID ) | ||||
1327 | { | ||||
1328 | return m_toolsDefinition->tool( toolID ); | ||||
1329 | } | ||||
1330 | | ||||
1331 | QDomElement PageViewAnnotator::quickTool( int toolID ) | ||||
1332 | { | ||||
1333 | return m_quickToolsDefinition->tool( toolID ); | ||||
1334 | } | ||||
1335 | | ||||
1336 | QDomElement PageViewAnnotator::currentEngineElement() | ||||
1337 | { | ||||
1338 | return m_toolsDefinition->tool( m_lastToolID ).firstChildElement( QStringLiteral("engine") ); | ||||
1339 | } | ||||
1340 | | ||||
1341 | QDomElement PageViewAnnotator::currentAnnotationElement() | ||||
1342 | { | ||||
1343 | return currentEngineElement().firstChildElement( QStringLiteral("annotation") ); | ||||
1344 | } | ||||
1345 | | ||||
1346 | void PageViewAnnotator::setAnnotationWidth( double width ) | ||||
1347 | { | ||||
1348 | currentAnnotationElement().setAttribute( QStringLiteral( "width" ), QString::number( width ) ); | ||||
1349 | saveAnnotationTools(); | ||||
1350 | selectTool( m_lastToolID ); | ||||
1351 | } | ||||
1352 | | ||||
1353 | void PageViewAnnotator::setAnnotationColor( const QColor &color ) | ||||
1354 | { | ||||
1355 | currentEngineElement().setAttribute( QStringLiteral( "color" ), color.name(QColor::HexRgb ) ); | ||||
1356 | QDomElement annotationElement = currentAnnotationElement(); | ||||
1357 | QString annotType = annotationElement.attribute( QStringLiteral( "type" ) ); | ||||
1358 | if ( annotType == "Typewriter" ) { | ||||
1359 | annotationElement.setAttribute( QStringLiteral( "textColor" ), color.name( QColor::HexRgb ) ); | ||||
1360 | } else { | ||||
1361 | annotationElement.setAttribute( QStringLiteral( "color" ), color.name( QColor::HexRgb ) ); | ||||
1362 | } | ||||
1363 | saveAnnotationTools(); | ||||
1364 | selectTool( m_lastToolID ); | ||||
1365 | } | ||||
1366 | | ||||
1367 | void PageViewAnnotator::setAnnotationInnerColor( const QColor &color ) | ||||
1368 | { | ||||
1369 | QDomElement annotationElement = currentAnnotationElement(); | ||||
1370 | QString annotType = annotationElement.attribute( QStringLiteral( "type" ) ); | ||||
1371 | if ( color == Qt::transparent ) { | ||||
1372 | annotationElement.removeAttribute( QStringLiteral( "innerColor" ) ); | ||||
1373 | } else { | ||||
1374 | annotationElement.setAttribute( QStringLiteral( "innerColor" ), color.name( QColor::HexRgb ) ); | ||||
1375 | } | ||||
1376 | saveAnnotationTools(); | ||||
1377 | selectTool( m_lastToolID ); | ||||
1378 | } | ||||
1379 | | ||||
1380 | void PageViewAnnotator::setAnnotationOpacity( double opacity ) | ||||
1381 | { | ||||
1382 | currentAnnotationElement().setAttribute( QStringLiteral( "opacity" ), QString::number( opacity ) ); | ||||
1383 | saveAnnotationTools(); | ||||
1384 | selectTool( m_lastToolID ); | ||||
1385 | } | ||||
1386 | | ||||
1387 | void PageViewAnnotator::setAnnotationFont( const QFont &font ) | ||||
1388 | { | ||||
1389 | currentAnnotationElement().setAttribute( QStringLiteral( "font" ), font.toString() ); | ||||
1390 | saveAnnotationTools(); | ||||
1391 | selectTool( m_lastToolID ); | ||||
1392 | } | ||||
1393 | | ||||
1394 | void PageViewAnnotator::addToQuickAnnotations() | ||||
1395 | { | ||||
1396 | QDomElement sourceToolElement = m_toolsDefinition->tool( m_lastToolID ); | ||||
1397 | if( sourceToolElement.isNull() ) | ||||
1398 | return; | ||||
1399 | | ||||
1400 | // set custom name for quick annotation | ||||
1401 | bool ok = false; | ||||
1402 | QString itemText = QInputDialog::getText(nullptr, | ||||
1403 | i18n( "Add favorite annotation" ), | ||||
1404 | i18n( "Custom annotation name:" ), | ||||
1405 | QLineEdit::Normal, | ||||
1406 | defaultToolName( sourceToolElement ), | ||||
1407 | &ok ); | ||||
1408 | if( !ok ) | ||||
1409 | return; | ||||
1410 | | ||||
1411 | QDomElement toolElement = sourceToolElement.cloneNode().toElement(); | ||||
1412 | // store name attribute only if the user specified a customized name | ||||
1413 | if ( !itemText.isEmpty() ) | ||||
1414 | toolElement.setAttribute( QStringLiteral("name"), itemText ); | ||||
1415 | toolElement.setAttribute( QStringLiteral("sourceId"), sourceToolElement.attribute( QStringLiteral("id") ) ); | ||||
1416 | m_quickToolsDefinition->appendTool( toolElement ); | ||||
1417 | saveAnnotationTools(); | ||||
1418 | } | ||||
1419 | | ||||
1420 | void PageViewAnnotator::slotAdvancedSettings() | ||||
1421 | { | ||||
1422 | QDomElement toolElement = m_toolsDefinition->tool( m_lastToolID ); | ||||
1423 | | ||||
1424 | EditAnnotToolDialog t( nullptr, toolElement, true ); | ||||
1425 | if ( t.exec() != QDialog::Accepted ) | ||||
1426 | return; | ||||
1427 | | ||||
1428 | QDomElement toolElementUpdated = t.toolXml().documentElement(); | ||||
1429 | int toolID = toolElement.attribute( QStringLiteral("id") ).toInt(); | ||||
1430 | m_toolsDefinition->updateTool( toolElementUpdated, toolID ); | ||||
1431 | saveAnnotationTools(); | ||||
1432 | selectTool( m_lastToolID ); | ||||
1433 | } | ||||
1434 | | ||||
1281 | #include "moc_pageviewannotator.cpp" | 1435 | #include "moc_pageviewannotator.cpp" | ||
1282 | 1436 | | |||
1283 | /* kate: replace-tabs on; indent-width 4; */ | 1437 | /* kate: replace-tabs on; indent-width 4; */ |