drawingtoolactions.cpp 6.53 KB
Newer Older
1 2
/***************************************************************************
 *   Copyright (C) 2015 by Laurent Montel <montel@kde.org>                 *
3
 *   Copyright (C) 2015 by Albert Astals Cid <aacid@kde.org>               *
4 5 6 7 8 9 10
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

11
#include "drawingtoolactions.h"
12 13 14 15

#include "debug_ui.h"
#include "settings.h"

16
#include <KActionCollection>
17 18
#include <KLocalizedString>

19
#include <QAction>
20 21
#include <QPainter>

22
class ColorAction : public QAction
23 24
{
public:
25 26
    explicit ColorAction( KActionCollection *parent )
        : QAction( parent )
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    {
    }

    void setColor( const QColor &color )
    {
        QPixmap pm( 25, 25 );
        pm.fill( color );

        QIcon icon;

        icon.addPixmap( pm, QIcon::Normal, QIcon::Off );

        QPixmap pmSel( pm );
        QPainter p( &pmSel );
        QFont font = p.font();
        font.setPixelSize( pmSel.height() * 0.9 );
        p.setFont( font );

        // draw check mark
        const int lightness = ((color.red() * 299) + (color.green() * 587) + (color.blue() * 114)) / 1000;
        p.setPen( lightness < 128 ? Qt::white : Qt::black );
        p.drawText( QRect( QPoint( 0, 0 ), pmSel.size() ), Qt::AlignCenter, "\u2713" );

        icon.addPixmap( pmSel, QIcon::Normal, QIcon::On );

        setIcon( icon );
    }
};

56 57
DrawingToolActions::DrawingToolActions( KActionCollection *parent )
    : QObject( parent )
58 59
{
    loadTools();
60
}
61

62 63 64

DrawingToolActions::~DrawingToolActions()
{
65 66
}

67 68 69 70
QList<QAction*> DrawingToolActions::actions() const
{
    return m_actions;
}
71

72
void DrawingToolActions::reparseConfig()
73
{
74 75 76
    qDeleteAll(m_actions);
    m_actions.clear();
    loadTools();
77 78
}

79
void DrawingToolActions::actionTriggered()
80
{
81
    QAction *action = qobject_cast<QAction*>( sender() );
82

83 84 85
    if ( action ) {
        if ( action->isChecked() ) {
            Q_FOREACH ( QAction *btn, m_actions )
86
            {
87
                if ( action != btn ) {
88 89 90 91
                    btn->setChecked( false );
                }
            }

92
            emit changeEngine( action->property( "__document" ).value<QDomElement>() );
93 94 95 96 97 98
        } else {
            emit changeEngine( QDomElement() );
        }
    }
}

99
void DrawingToolActions::loadTools()
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
    const QStringList drawingTools = Okular::Settings::drawingTools();

    QDomDocument doc;
    QDomElement drawingDefinition = doc.createElement( "drawingTools" );
    foreach ( const QString &drawingXml, drawingTools )
    {
        QDomDocument entryParser;
        if ( entryParser.setContent( drawingXml ) )
            drawingDefinition.appendChild( doc.importNode( entryParser.documentElement(), true ) );
        else
            qCWarning(OkularUiDebug) << "Skipping malformed quick selection XML in QuickSelectionTools setting";
    }

    // Create the AnnotationToolItems from the XML dom tree
    QDomNode drawingDescription = drawingDefinition.firstChild();
    while ( drawingDescription.isElement() )
    {
        const QDomElement toolElement = drawingDescription.toElement();
        if ( toolElement.tagName() == "tool" )
        {
121
            QString tooltip;
122 123 124 125
            QString width;
            QString colorStr;
            QString opacity;

126 127 128 129 130 131 132 133 134 135
            const QString name = toolElement.attribute( QStringLiteral("name") );
            if ( toolElement.attribute( QStringLiteral("default"), QStringLiteral("false") ) == QLatin1String("true") )
            {
                tooltip = i18n( name.toLatin1().constData() );   
            }
            else
            {
                tooltip = name;
            }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            const QDomNodeList engineNodeList = toolElement.elementsByTagName( "engine" );
            if ( engineNodeList.size() > 0 )
            {
                const QDomElement engineEl = engineNodeList.item( 0 ).toElement();
                if ( engineEl.hasAttribute( QStringLiteral("color") ) )
                {
                    colorStr = engineEl.attribute( QStringLiteral("color") );
                }

                const QDomNodeList annotationList = engineEl.elementsByTagName( "annotation" );
                if ( annotationList.size() > 0 )
                {
                    const QDomElement annotationEl = annotationList.item( 0 ).toElement();
                    if ( annotationEl.hasAttribute( QStringLiteral("width") ) )
                    {
                        width = annotationEl.attribute( QStringLiteral("width") );
                        opacity = annotationEl.attribute( QStringLiteral("opacity"), QStringLiteral("1.0") );
                    }
                }
            }

            QDomDocument doc( QStringLiteral("engine") );
            QDomElement root = doc.createElement( QStringLiteral("engine") );
            root.setAttribute( QStringLiteral("color"), colorStr );
            doc.appendChild( root );
            QDomElement annElem = doc.createElement( QStringLiteral("annotation") );
            root.appendChild( annElem );
            annElem.setAttribute( QStringLiteral("type"), QStringLiteral("Ink") );
            annElem.setAttribute( QStringLiteral("color"), colorStr );
            annElem.setAttribute( QStringLiteral("width"), width );
            annElem.setAttribute( QStringLiteral("opacity"), opacity );

168 169
            const QString text = i18n("Drawing Tool: %1", tooltip);
            createToolAction( text, tooltip, colorStr, width, opacity, root );
170 171 172 173 174 175
        }

        drawingDescription = drawingDescription.nextSibling();
    }
}

176
void DrawingToolActions::createToolAction( const QString &text, const QString &toolName, const QString &colorName, const QString &width, const QString &opacity, const QDomElement &root )
177
{
178 179 180 181 182 183 184
    KActionCollection *ac = static_cast<KActionCollection*>( parent() );
    ColorAction *action = new ColorAction( ac );
    action->setText( text );
    action->setToolTip( toolName );
    action->setCheckable( true );
    action->setColor( QColor( colorName ) );
    action->setEnabled( false );
185

186
    action->setProperty( "__document", QVariant::fromValue<QDomElement>( root ) );
187

188
    m_actions.append( action );
189

190
    ac->addAction( QString("presentation_drawing_%1").arg( toolName ), action );
191

192
    connect( action, &QAction::triggered, this, &DrawingToolActions::actionTriggered );
193
}