File: presetmodel.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 (121 lines) | stat: -rw-r--r-- 3,679 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
/**************************************************************************
* 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 "presetmodel.h"

#include <QPainter>
#include <QApplication>
#include <QPalette>
#include <QIcon>

#include "datafunctions.h"

PresetModel::PresetModel( QObject* parent ) : QAbstractListModel( parent ),
    m_map( NULL )
{
}

PresetModel::~PresetModel()
{
}

void PresetModel::setMap( PresetMap* map )
{
    m_map = map;

    update();
}

static bool localeAwareLessThan( const QString& s1, const QString& s2 )
{
    return QString::localeAwareCompare( s1, s2 ) < 0;
}
 
void PresetModel::update()
{
    m_keys = m_map->keys();

    qSort( m_keys.begin(), m_keys.end(), localeAwareLessThan );

    reset();
}

int PresetModel::rowCount( const QModelIndex& parent ) const
{
    if ( !parent.isValid() )
        return m_keys.count();
    return 0;
}

QVariant PresetModel::data( const QModelIndex& index, int role ) const
{
    if ( role == Qt::DisplayRole )
        return m_keys.at( index.row() );

    if ( role == Qt::DecorationRole ) {
        QString name = m_keys.at( index.row() );

        Preset preset = m_map->value( name );

        QPixmap pixmap( 48, 48 );
        QPainter painter( &pixmap );

        QGradientStops stops = DataFunctions::calculateGradientStops( preset.gradient() );

        bool reversed = preset.colorMapping().isReversed();
        bool mirrored = preset.colorMapping().isMirrored();

        if ( !mirrored && !reversed || mirrored && reversed ) {
            for ( int i = 0; i < stops.count() / 2; i++ ) {
                QGradientStop stop1 = stops.at( i );
                QGradientStop stop2 = stops.at( stops.count() - i - 1 );
                stops[ i ] = QGradientStop( 1.0 - stop2.first, stop2.second );
                stops[ stops.count() - i - 1 ] = QGradientStop( 1.0 - stop1.first, stop1.second );
            }
            if ( stops.count() % 2 == 1 ) {
                QGradientStop stop = stops.at( stops.count() / 2 );
                stops[ stops.count() / 2 ] = QGradientStop( 1.0 - stop.first, stop.second );
            }
        }

        if ( mirrored ) {
            for ( int i = stops.count() - 1; i >= 0; i-- ) {
                QGradientStop stop = stops.at( i );
                stops[ i ] = QGradientStop( 0.5 * stop.first, stop.second );
                stops.append( QGradientStop( 1.0 - 0.5 * stop.first, stop.second ) );
            }
        }

        double angle = 90.0 + 360.0 * preset.colorMapping().offset();

        QConicalGradient gradient( pixmap.width() / 2, pixmap.height() / 2, angle );
        gradient.setStops( stops );

        painter.fillRect( pixmap.rect(), gradient );

        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();
}