event.cpp 4.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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
/***************************************************************************
 *   Copyright (C) 2018 by Intevation GmbH <intevation@intevation.de>      *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "event_p.h"

#include "../form.h"

using namespace Okular;

class Event::Private
{
    public:
        Private( EventType eventType ): m_target( nullptr ),
                                        m_targetPage( nullptr ),
                                        m_source( nullptr ),
                                        m_sourcePage( nullptr ),
                                        m_eventType( eventType )
        {
        }

        void *m_target;
        Page *m_targetPage;
        FormField *m_source;
        Page *m_sourcePage;
        EventType m_eventType;
        QString m_targetName;
        QVariant m_value;
};

Event::Event(): d( new Private( UnknownEvent ) )
{
}

Event::Event( EventType eventType ): d( new Private( eventType ) )
{

}

Event::EventType Event::eventType() const
{
    return d->m_eventType;
}

QString Event::name() const
{
    switch ( d->m_eventType )
    {
        case ( FieldCalculate ):
            return QStringLiteral( "Calculate" );
56 57
        case ( FieldFormat ):
            return QStringLiteral( "Format" );
58 59 60 61 62 63 64 65 66 67 68
        case ( UnknownEvent ):
        default:
            return QStringLiteral( "Unknown" );
    }
}

QString Event::type() const
{
    switch ( d->m_eventType )
    {
        case ( FieldCalculate ):
69
        case ( FieldFormat ):
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            return QStringLiteral( "Field" );
        case ( UnknownEvent ):
        default:
            return QStringLiteral( "Unknown" );
    }
}

QString Event::targetName() const
{
    if ( !d->m_targetName.isNull() )
    {
        return d->m_targetName;
    }

    return QStringLiteral( "JavaScript for: " ) + type() + name();
}

void Event::setTargetName( const QString &val )
{
    d->m_targetName = val;
}

FormField *Event::source() const
{
    return d->m_source;
}

void Event::setSource( FormField *val )
{
    d->m_source = val;
}

Page *Event::sourcePage() const
{
    return d->m_sourcePage;
}

void Event::setSourcePage( Page *val )
{
    d->m_sourcePage = val;
}

void *Event::target() const
{
    return d->m_target;
}

void Event::setTarget( void *target )
{
    d->m_target = target;
}

Page *Event::targetPage() const
{
124
    return d->m_targetPage;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

void Event::setTargetPage( Page *val )
{
    d->m_targetPage = val;
}

QVariant Event::value() const
{
    return d->m_value;
}

void Event::setValue( const QVariant &val )
{
    d->m_value = val;
}

// static
std::shared_ptr<Event> Event::createFormCalculateEvent( FormField *target,
                                                        Page *targetPage,
                                                        FormField *source,
                                                        Page *sourcePage,
                                                        const QString &targetName )
{
    std::shared_ptr<Event> ret( new Event( Event::FieldCalculate ) );
    ret->setSource( source );
    ret->setSourcePage( sourcePage );
    ret->setTarget( target );
    ret->setTargetPage( targetPage );
    ret->setTargetName( targetName );

    FormFieldText *fft = dynamic_cast< FormFieldText * >(target);
    if ( fft )
    {
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        ret->setValue( QVariant( fft->internalText() ) );
    }
    return ret;
}

// static
std::shared_ptr<Event> Event::createFormatEvent( FormField *target,
                                                 Page *targetPage,
                                                 const QString &targetName )
{
    std::shared_ptr<Event> ret( new Event( Event::FieldFormat ) );
    ret->setTarget( target );
    ret->setTargetPage( targetPage );
    ret->setTargetName( targetName );

    FormFieldText *fft = dynamic_cast< FormFieldText * >(target);
    if ( fft )
    {
        ret->setValue( QVariant( fft->internalText() ) );
178 179 180
    }
    return ret;
}