summaryrefslogtreecommitdiffstats
path: root/src/gui/kdeext/QCanvasSimpleSprite.cpp
blob: 537cc629bb2f2e5d2c3730207c8dfc78bb563a19 (plain)
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
// -*- c-basic-offset: 4 -*-

/*
    Rosegarden
    A sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <[email protected]>,
        Chris Cannam        <[email protected]>,
        Richard Bown        <[email protected]>
 
    The moral right of the authors to claim authorship of this work
    has been asserted.
 
    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.  See the file
    COPYING included with this distribution for more information.
*/

#include <vector>
#include "misc/Debug.h"

#include <qpainter.h>

#include "QCanvasSimpleSprite.h"

namespace Rosegarden {

QCanvasSimpleSprite::QCanvasSimpleSprite(QPixmap *pixmap, QCanvas *canvas)
        : QCanvasSprite(0, canvas),
        m_pixmapArray(0)
{
    m_pixmapArray = makePixmapArray(pixmap);
    setSequence(m_pixmapArray);
}

QCanvasSimpleSprite::QCanvasSimpleSprite(QCanvasPixmap *pixmap, QCanvas *canvas)
        : QCanvasSprite(0, canvas),
        m_pixmapArray(0)
{
    m_pixmapArray = makePixmapArray(pixmap);
    setSequence(m_pixmapArray);
}

QCanvasSimpleSprite::QCanvasSimpleSprite(const QString &pixmapfile,
        QCanvas *canvas)
        : QCanvasSprite(0, canvas),
        m_pixmapArray(0)
{
    m_pixmapArray = makePixmapArray(pixmapfile);
    setSequence(m_pixmapArray);
}

QCanvasSimpleSprite::QCanvasSimpleSprite(QCanvas *canvas)
        : QCanvasSprite(0, canvas),
        m_pixmapArray(0)
{
    QCanvasPixmapArray* tmpArray = makePixmapArray(new QPixmap());
    setSequence(tmpArray);
}


QCanvasSimpleSprite::~QCanvasSimpleSprite()
{
    PixmapArrayGC::registerForDeletion(m_pixmapArray);
    m_pixmapArray = 0;

    // We can't delete m_pixmapArray or we get a core dump.
    //
    // The reason I think is that after the QCanvasSprite is deleted,
    // it is removed from the QCanvas, which therefore needs the
    // pixmaps to know how to update itself (the crash is in
    // QCanvas::removeChunks(), usually).
    //
    // So instead we have to do this GCish
    // thingy. PixmapArrayGC::deleteAll() is called by
    // NotationView::redoLayout
}

QCanvasPixmapArray*
QCanvasSimpleSprite::makePixmapArray(QPixmap *pixmap)
{
    QList<QPixmap> pixlist;
    pixlist.setAutoDelete(true); // the QCanvasPixmapArray creates its
    // own copies of the pixmaps, so we
    // can delete the one we're passed
    pixlist.append(pixmap);

    QList<QPoint> spotlist;
    spotlist.setAutoDelete(true);
    spotlist.append(new QPoint(0, 0));

    return new QCanvasPixmapArray(pixlist, spotlist);
}

QCanvasPixmapArray*
QCanvasSimpleSprite::makePixmapArray(QCanvasPixmap *pixmap)
{
    QList<QPixmap> pixlist;
    pixlist.setAutoDelete(true); // the QCanvasPixmapArray creates its
    // own copies of the pixmaps, so we
    // can delete the one we're passed
    pixlist.append(pixmap);

    QList<QPoint> spotlist;
    spotlist.setAutoDelete(true);
    spotlist.append(new QPoint(pixmap->offsetX(), pixmap->offsetY()));

    return new QCanvasPixmapArray(pixlist, spotlist);
}

QCanvasPixmapArray*
QCanvasSimpleSprite::makePixmapArray(const QString &pixmapfile)
{
    return new QCanvasPixmapArray(pixmapfile);
}

//////////////////////////////////////////////////////////////////////

QCanvasNotationSprite::QCanvasNotationSprite(NotationElement& n,
        QPixmap* pixmap,
        QCanvas* canvas)
        : QCanvasSimpleSprite(pixmap, canvas),
        m_notationElement(n)
{}

QCanvasNotationSprite::QCanvasNotationSprite(NotationElement& n,
        QCanvasPixmap* pixmap,
        QCanvas* canvas)
        : QCanvasSimpleSprite(pixmap, canvas),
        m_notationElement(n)

{}

QCanvasNotationSprite::~QCanvasNotationSprite()
{}


QCanvasNonElementSprite::QCanvasNonElementSprite(QPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasSimpleSprite(pixmap, canvas)
{}

QCanvasNonElementSprite::QCanvasNonElementSprite(QCanvasPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasSimpleSprite(pixmap, canvas)
{}

QCanvasNonElementSprite::~QCanvasNonElementSprite()
{}

QCanvasTimeSigSprite::QCanvasTimeSigSprite(double layoutX,
        QPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasNonElementSprite(pixmap, canvas),
        m_layoutX(layoutX)
{}

QCanvasTimeSigSprite::QCanvasTimeSigSprite(double layoutX,
        QCanvasPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasNonElementSprite(pixmap, canvas),
        m_layoutX(layoutX)
{}

QCanvasTimeSigSprite::~QCanvasTimeSigSprite()
{}


QCanvasStaffNameSprite::QCanvasStaffNameSprite(QPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasNonElementSprite(pixmap, canvas)
{}

QCanvasStaffNameSprite::QCanvasStaffNameSprite(QCanvasPixmap *pixmap,
        QCanvas *canvas) :
        QCanvasNonElementSprite(pixmap, canvas)
{}

QCanvasStaffNameSprite::~QCanvasStaffNameSprite()
{}


//////////////////////////////////////////////////////////////////////

void PixmapArrayGC::registerForDeletion(QCanvasPixmapArray* array)
{
    m_pixmapArrays.push_back(array);
}

void PixmapArrayGC::deleteAll()
{
    RG_DEBUG << "PixmapArrayGC::deleteAll() : "
    << m_pixmapArrays.size() << " pixmap arrays to delete\n";

    static unsigned long total = 0;

    for (unsigned int i = 0; i < m_pixmapArrays.size(); ++i) {
        QCanvasPixmapArray *array = m_pixmapArrays[i];
        QPixmap *pixmap = array->image(0);
        if (pixmap) {
            total += pixmap->width() * pixmap->height();
            //	    NOTATION_DEBUG << "PixmapArrayGC::deleteAll(): " << pixmap->width() << "x" << pixmap->height() << " (" << (pixmap->width()*pixmap->height()) << " px, " << total << " total)" << endl;
        }
        delete m_pixmapArrays[i];
    }

    m_pixmapArrays.clear();
}

std::vector<QCanvasPixmapArray*> PixmapArrayGC::m_pixmapArrays;

}

//////////////////////////////////////////////////////////////////////