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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/**************************************************************************
* This file is part of the Fraqtive program
* Copyright (C) 2004-2008 Micha Mciski
*
* 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 "bookmarkmodel.h"
#include <math.h>
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
#include <QPalette>
#include <QPainter>
#include <QIcon>
#include "fraqtiveapplication.h"
#include "fractaldata.h"
#include "jobscheduler.h"
#include "datafunctions.h"
Q_DECLARE_METATYPE( QModelIndex )
BookmarkModel::BookmarkModel( QObject* parent ) : QAbstractListModel( parent ),
m_gradientCache( NULL ),
m_enabled( false ),
m_activeJobs( 0 )
{
qRegisterMetaType<QModelIndex>();
}
BookmarkModel::~BookmarkModel()
{
QMutexLocker locker( &m_mutex );
cancelJobs();
while ( m_activeJobs > 0 )
m_allJobsDone.wait( &m_mutex );
delete[] m_gradientCache;
}
void BookmarkModel::setMap( BookmarkMap* map )
{
m_map = map;
update();
m_enabled = true;
m_queue = m_keys;
}
static const int GradientSize = 16384;
void BookmarkModel::setColorSettings( const Gradient& gradient, const QColor& backgroundColor, const ColorMapping& mapping )
{
QMutexLocker locker( &m_mutex );
cancelJobs();
if ( !m_gradientCache )
m_gradientCache = new QRgb[ GradientSize ];
DataFunctions::fillGradientCache( gradient, m_gradientCache, GradientSize );
m_backgroundColor = backgroundColor;
m_colorMapping = mapping;
}
void BookmarkModel::abortGeneration()
{
QMutexLocker locker( &m_mutex );
m_enabled = false;
cancelJobs();
}
void BookmarkModel::continueGeneration()
{
QMutexLocker locker( &m_mutex );
m_enabled = true;
addJobs();
}
void BookmarkModel::invalidateBookmark( const QString& name )
{
QMutexLocker locker( &m_mutex );
m_images.remove( name );
if ( !m_queue.contains( name ) ) {
m_queue.append( name );
addJobs( 1 );
}
}
static bool localeAwareLessThan( const QString& s1, const QString& s2 )
{
return QString::localeAwareCompare( s1, s2 ) < 0;
}
void BookmarkModel::update()
{
QMutexLocker locker( &m_mutex );
cancelJobs();
m_keys = m_map->keys();
qSort( m_keys.begin(), m_keys.end(), localeAwareLessThan );
reset();
}
int BookmarkModel::rowCount( const QModelIndex& parent ) const
{
if ( !parent.isValid() )
return m_keys.count();
return 0;
}
QVariant BookmarkModel::data( const QModelIndex& index, int role ) const
{
if ( role == Qt::DisplayRole )
return m_keys.at( index.row() );
if ( role == Qt::DecorationRole ) {
QMutexLocker locker( &m_mutex );
QString name = m_keys.at( index.row() );
QPixmap pixmap;
if ( m_images.contains( name ) ) {
pixmap = QPixmap::fromImage( m_images.value( name ) );
} else {
pixmap = QPixmap( 48, 48 );
pixmap.fill( QApplication::palette().color( QPalette::Disabled, QPalette::Window ) );
}
locker.unlock();
QPainter painter( &pixmap );
QPixmap pixmap2 = pixmap;
QPainter painter2( &pixmap2 );
painter.setPen( QApplication::palette().color( QPalette::Dark ) );
painter.drawRect( pixmap.rect().adjusted( 0, 0, -1, -1 ) );
painter2.setPen( QApplication::palette().color( QPalette::Highlight ) );
painter2.drawRect( pixmap.rect().adjusted( 0, 0, -1, -1 ) );
painter2.drawRect( pixmap.rect().adjusted( 1, 1, -2, -2 ) );
QIcon icon;
icon.addPixmap( pixmap, QIcon::Normal );
icon.addPixmap( pixmap2, QIcon::Selected );
return icon;
}
return QVariant();
}
int BookmarkModel::priority() const
{
return 1;
}
static int roundToCellSize( int size )
{
// round up to nearest N * CellSize + 1
return ( ( size - 1 + GeneratorCore::CellSize - 1 ) / GeneratorCore::CellSize ) * GeneratorCore::CellSize + 1;
}
void BookmarkModel::executeJob()
{
QMutexLocker locker( &m_mutex );
if ( !m_enabled || m_queue.isEmpty() ) {
finishJob();
return;
}
QString name = m_queue.takeFirst();
if ( !m_map->contains( name ) ) {
finishJob();
return;
}
Bookmark bookmark = m_map->value( name );
locker.unlock();
const int imageSize = 48;
const int bufferSize = roundToCellSize( imageSize + 2 ); // 2 pixel margin for anti-aliasing
double* buffer = new double[ bufferSize * bufferSize ];
calculate( bookmark, buffer, QSize( bufferSize, bufferSize ), QSize( imageSize, imageSize ) );
FractalData data;
data.transferBuffer( buffer, bufferSize, QSize( imageSize, imageSize ) );
QImage image( imageSize, imageSize, QImage::Format_RGB32 );
ViewSettings settings = DataFunctions::defaultViewSettings();
DataFunctions::ColorMapper mapper( m_gradientCache, GradientSize, m_backgroundColor.rgb(), m_colorMapping );
DataFunctions::drawImage( image, &data, image.rect(), mapper, settings.antiAliasing() );
locker.relock();
if ( m_queue.contains( name ) ) {
finishJob();
return;
}
m_images.insert( name, image );
int row = m_keys.indexOf( name );
if ( row >= 0 )
emit dataChanged( index( row ), index( row ) );
finishJob();
}
void BookmarkModel::calculate( const Bookmark& bookmark, double* buffer, const QSize& size, const QSize& resolution )
{
GeneratorCore::Input input;
Position position = bookmark.position();
double scale = pow( 10.0, -position.zoomFactor() ) / (double)resolution.height();
double sa = scale * sin( position.angle() * M_PI / 180.0 );
double ca = scale * cos( position.angle() * M_PI / 180.0 );
double offsetX = -(double)resolution.width() / 2.0 - 0.5;
double offsetY = -(double)resolution.height() / 2.0 - 0.5;
input.m_sa = sa;
input.m_ca = ca;
input.m_x = position.center().x() + ca * offsetX + sa * offsetY;
input.m_y = position.center().y() - sa * offsetX + ca * offsetY;
GeneratorCore::Output output;
output.m_buffer = buffer;
output.m_width = size.width();
output.m_height = size.height();
output.m_stride = size.width();
GeneratorSettings settings = DataFunctions::defaultGeneratorSettings();
int maxIterations = pow( 10.0, settings.calculationDepth() ) * qMax( 1.0, 1.45 + position.zoomFactor() );
double threshold = settings.detailThreshold();
#if defined( HAVE_SSE2 )
GeneratorCore::FunctorSSE2* functorSSE2 = DataFunctions::createFunctorSSE2( bookmark.fractalType() );
if ( functorSSE2 ) {
GeneratorCore::generatePreviewSSE2( input, output, functorSSE2, maxIterations );
GeneratorCore::interpolate( output );
GeneratorCore::generateDetailsSSE2( input, output, functorSSE2, maxIterations, threshold );
delete functorSSE2;
return;
}
#endif
GeneratorCore::Functor* functor = DataFunctions::createFunctor( bookmark.fractalType() );
if ( functor ) {
GeneratorCore::generatePreview( input, output, functor, maxIterations );
GeneratorCore::interpolate( output );
GeneratorCore::generateDetails( input, output, functor, maxIterations, threshold );
delete functor;
}
}
void BookmarkModel::addJobs( int count /*= -1*/ )
{
if ( count < 0 )
count = m_queue.count();
if ( count > 0 ) {
fraqtive()->jobScheduler()->addJobs( this, count );
m_activeJobs += count;
}
}
void BookmarkModel::cancelJobs()
{
int count = fraqtive()->jobScheduler()->cancelAllJobs( this );
m_activeJobs -= count;
if ( m_activeJobs == 0 )
m_allJobsDone.wakeAll();
}
void BookmarkModel::finishJob()
{
m_activeJobs--;
if ( m_activeJobs == 0 )
m_allJobsDone.wakeAll();
}
|