File: fractalpresenter.cpp

package info (click to toggle)
fraqtive 0.4.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 988 kB
  • ctags: 1,249
  • sloc: cpp: 8,387; sh: 103; makefile: 45
file content (297 lines) | stat: -rw-r--r-- 8,149 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
124
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**************************************************************************
* This file is part of the Fraqtive program
* Copyright (C) 2004-2008 Michał Męciński
*
* 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 "fractalpresenter.h"

#include <math.h>

#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif

#include <QMatrix>

#include "fractalgenerator.h"
#include "fractalmodel.h"
#include "fractaldata.h"
#include "abstractview.h"

FractalPresenter::FractalPresenter( QObject* parent ) : QObject( parent ),
    m_model( NULL ),
    m_view( NULL ),
    m_preview( false ),
    m_enabled( false )
{
    m_generator = new FractalGenerator( this );
    m_generator->setReceiver( this );
}

FractalPresenter::~FractalPresenter()
{
}

void FractalPresenter::setModel( FractalModel* model )
{
    m_model = model;
}

void FractalPresenter::setView( AbstractView* view )
{
    m_view = view;

    if ( m_model ) {
        m_view->setColorSettings( m_model->gradient(), m_model->backgroundColor(), m_model->colorMapping() );
        m_view->setViewSettings( m_model->viewSettings() );
        m_view->setAnimationState( m_model->animationState() );
    }
}

void FractalPresenter::setPreviewMode( bool preview )
{
    if ( m_preview != preview ) {
        m_preview = preview;
        m_generator->setPreviewMode( preview );
    }
}

void FractalPresenter::setPriority( int priority )
{
    m_generator->setPriority( priority );
}

void FractalPresenter::setEnabled( bool enabled )
{
    if ( m_enabled != enabled ) {
        m_enabled = enabled;
        m_generator->setEnabled( enabled );
        if ( !enabled )
            m_view->clearView();
        else
            m_view->initialUpdate( fractalData() );
    }
}

void FractalPresenter::setParameters( const FractalType& type, const Position& position )
{
    if ( m_type != type ) {
        m_type = type;
        m_position = position;
        m_generator->setParameters( type, position );
        if ( type.fractal() == JuliaFractal )
            clearHovering();
    } else {
        setPosition( position );
    }
}

void FractalPresenter::setFractalType( const FractalType& type )
{
    if ( m_type != type ) {
        m_type = type;
        m_generator->setFractalType( type );
        if ( type.fractal() == JuliaFractal )
            clearHovering();
    }
}

void FractalPresenter::setPosition( const Position& position )
{
    if ( m_position != position ) {
        if ( m_enabled && !m_preview ) {
            QMatrix oldMatrix = matrixFromPosition( m_position );
            QMatrix newMatrix = matrixFromPosition( position );
            QMatrix transform = ( newMatrix * oldMatrix.inverted() ).inverted();
            m_view->transformView( transform );
        }
        m_position = position;
        m_generator->setPosition( position );
    }
}

void FractalPresenter::setColorSettings( const Gradient& gradient, const QColor& background, const ColorMapping& mapping )
{
    m_view->setColorSettings( gradient, background, mapping );
}

void FractalPresenter::setGradient( const Gradient& gradient )
{
    m_view->setGradient( gradient );
}

void FractalPresenter::setBackgroundColor( const QColor& color )
{
    m_view->setBackgroundColor( color );
}

void FractalPresenter::setColorMapping( const ColorMapping& mapping )
{
    m_view->setColorMapping( mapping );
}

void FractalPresenter::setGeneratorSettings( const GeneratorSettings& settings )
{
    m_generator->setGeneratorSettings( settings );
}

void FractalPresenter::setViewSettings( const ViewSettings& settings )
{
    m_view->setViewSettings( settings );
}

void FractalPresenter::setAnimationState( const AnimationState& state )
{
    m_view->setAnimationState( state );
}

const FractalData* FractalPresenter::fractalData()
{
    m_generator->updateData( &m_data );
    return &m_data;
}

int FractalPresenter::maximumIterations() const
{
    return m_generator->maximumIterations();
}

void FractalPresenter::setResolution( const QSize& resolution )
{
    if ( m_resolution != resolution ) {
        m_resolution = resolution;
        m_generator->setResolution( resolution );
    }
}

void FractalPresenter::setHoveringPoint( const QPointF& point )
{
    if ( m_model && m_model->fractalType().fractal() != JuliaFractal )
        m_model->setHoveringParameters( juliaType( point ), juliaPosition() );
}

void FractalPresenter::clearHovering()
{
    if ( m_model )
        m_model->clearHovering();
}

void FractalPresenter::setTrackingTransform( const QMatrix& transform )
{
    QMatrix oldMatrix = matrixFromPosition( m_position );
    QMatrix newMatrix = transform.inverted() * oldMatrix;

    Position position = positionFromMatrix( newMatrix );
    m_model->setTrackingPosition( position );
}

void FractalPresenter::clearTracking()
{
    m_model->clearTracking();
}

void FractalPresenter::changePosition( const QMatrix& transform )
{
    QMatrix oldMatrix = matrixFromPosition( m_position );
    QMatrix newMatrix = transform.inverted() * oldMatrix;

    Position position = positionFromMatrix( newMatrix );
    m_model->setPosition( position );
}

void FractalPresenter::switchToJulia( const QPointF& point )
{
    if ( m_model && m_model->fractalType().fractal() != JuliaFractal )
        m_model->setParameters( juliaType( point ), juliaPosition() );
}

void FractalPresenter::adjustCameraZoom( double delta )
{
    ViewSettings settings = m_model->viewSettings();

    double zoom = qBound( 10.0, settings.cameraZoom() + 3.5 * delta, 45.0 );

    settings.setCameraZoom( zoom );
    m_model->setViewSettings( settings );
}

void FractalPresenter::customEvent( QEvent* e )
{
    if ( e->type() == FractalGenerator::UpdateEvent && m_enabled ) {
        FractalGenerator::UpdateStatus status = m_generator->updateData( &m_data );
        switch ( status ) {
            case FractalGenerator::InitialUpdate:
                m_view->initialUpdate( &m_data );
                break;

            case FractalGenerator::PartialUpdate:
                m_view->partialUpdate( &m_data );
                break;

            case FractalGenerator::FullUpdate:
                m_view->fullUpdate( &m_data );
                break;
        }
    }
}

QMatrix FractalPresenter::matrixFromPosition( const Position& position )
{
    QPointF center( m_resolution.width() / 2.0, m_resolution.height() / 2.0 );

    double scale = pow( 10.0, -position.zoomFactor() ) / m_resolution.height();

    QMatrix matrix;
    matrix.translate( position.center().x(), position.center().y() );
    matrix.rotate( -position.angle() );
    matrix.scale( scale, scale );
    matrix.translate( -center.x(), -center.y() );

    return matrix;
}

Position FractalPresenter::positionFromMatrix( const QMatrix& matrix )
{
    QPointF center( m_resolution.width() / 2.0, m_resolution.height() / 2.0 );

    QLineF line( center, QPointF( center.x() + m_resolution.height(), center.y() ) );
    QLineF mapped = matrix.map( line );

    Position position;
    position.setCenter( mapped.p1() );
    position.setZoomFactor( -log10( mapped.length() ) );
    position.setAngle( -atan2( mapped.dy(), mapped.dx() ) * 180.0 / M_PI );

    if ( position.angle() < 0.0 )
        position.setAngle( position.angle() + 360.0 );

    return position;
}

FractalType FractalPresenter::juliaType( const QPointF& point )
{
    QMatrix matrix = matrixFromPosition( m_position );
    QPointF mapped = matrix.map( point );

    FractalType type = m_type;
    type.setFractal( JuliaFractal );
    type.setParameter( mapped );

    return type;
}

Position FractalPresenter::juliaPosition()
{
    double exponent = m_type.exponentType() == IntegralExponent ? m_type.integralExponent() : m_type.realExponent();
    double zoom = ( m_position.zoomFactor() + 0.45 ) / exponent - 0.45;

    Position position;
    position.setZoomFactor( zoom );

    return position;
}