summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_histogram_view.cc
blob: 3b94db6192c1bd629a03e52ae4448c4022231e97 (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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 *  Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <math.h>

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqbuttongroup.h>
#include <tqpushbutton.h>
#include <tqscrollbar.h>

#include <kdebug.h>

#include "kis_channelinfo.h"
#include "kis_histogram.h"
#include "kis_global.h"
#include "kis_types.h"
#include "kis_layer.h"
#include "kis_colorspace.h"
#include "kis_histogram_view.h"
#include "kis_basic_histogram_producers.h"
#include "kis_paint_device.h"

KisHistogramView::KisHistogramView(TQWidget *parent, const char *name, WFlags f)
    : TQLabel(parent, name, f)
{
    // This is needed until we can computationally scale it well. Until then, this is needed
    // And when we have it, it won't hurt to have it around
    setScaledContents(true);
    setFrameShape(TQFrame::Box); // Draw a box around ourselves
}

KisHistogramView::~KisHistogramView()
{
}

void KisHistogramView::setPaintDevice(KisPaintDeviceSP dev)
{
    m_cs = dev->colorSpace();

    setChannels(); // Sets m_currentProducer to the first in the list

    if (!m_currentProducer)
        return;

    m_from = m_currentProducer->viewFrom();
    m_width = m_currentProducer->viewWidth();

    m_histogram = new KisHistogram(dev, m_currentProducer, LINEAR);

    updateHistogram();
}

void KisHistogramView::setHistogram(KisHistogramSP histogram)
{
    m_cs = 0;
    m_histogram = histogram;
    m_currentProducer = m_histogram->producer();
    m_from = m_currentProducer->viewFrom();
    m_width = m_currentProducer->viewWidth();

    m_comboInfo.clear();
    m_channelStrings.clear();
    m_channels.clear();
    m_channelToOffset.clear();

    addProducerChannels(m_currentProducer);

    // Set the currently viewed channel:
    m_color = false;
    m_channels.append(m_comboInfo.at(1).channel);
    m_channelToOffset.append(0);

    updateHistogram();
}

void KisHistogramView::setView(double from, double size)
{
    m_from = from;
    m_width = size;
    if (m_from + m_width > 1.0)
        m_from = 1.0 - m_width;
    m_histogram->producer()->setView(m_from, m_width);

    m_histogram->updateHistogram();
    updateHistogram();
}

KisHistogramProducerSP KisHistogramView::currentProducer()
{
    return m_currentProducer;
}

TQStringList KisHistogramView::channelStrings()
{
    return m_channelStrings;
}

KisIDList KisHistogramView::listProducers()
{
    if (m_cs)
        return KisHistogramProducerFactoryRegistry::instance()->listKeysCompatibleWith(m_cs);
    return KisIDList();
}

void KisHistogramView::setCurrentChannels(const KisID& producerID, TQValueVector<KisChannelInfo *> channels)
{
    setCurrentChannels(
        KisHistogramProducerFactoryRegistry::instance()->get(producerID)->generate(),
        channels);
}

void KisHistogramView::setCurrentChannels(KisHistogramProducerSP producer, TQValueVector<KisChannelInfo *> channels)
{
    m_currentProducer = producer;
    m_currentProducer->setView(m_from, m_width);
    m_histogram->setProducer(m_currentProducer);
    m_histogram->updateHistogram();
    m_histogram->setChannel(0); // Set a default channel, just being nice

    m_channels.clear();
    m_channelToOffset.clear();

    if (channels.count() == 0) {
        updateHistogram();
        return;
    }

    TQValueVector<KisChannelInfo *> producerChannels = m_currentProducer->channels();

    for (uint i = 0; i < channels.count(); i++) {
        // Also makes sure the channel is actually in the producer's list
        for (uint j = 0; j < producerChannels.count(); j++) {
            if (channels.at(i)->name() == producerChannels.at(j)->name()) {
                m_channelToOffset.append(m_channels.count()); // The first we append maps to 0
                m_channels.append(channels.at(i));
            }
        }
    }

    updateHistogram();
}

bool KisHistogramView::hasColor()
{
    return m_color;
}

void KisHistogramView::setColor(bool set)
{
    if (set != m_color) {
        m_color = set;
        updateHistogram();
    }
}

void KisHistogramView::setActiveChannel(int channel)
{
    ComboboxInfo info = m_comboInfo.at(channel);
    if (info.producer.data() != m_currentProducer.data()) {
        m_currentProducer = info.producer;
        m_currentProducer->setView(m_from, m_width);
        m_histogram->setProducer(m_currentProducer);
        m_histogram->updateHistogram();
    }

    m_channels.clear();
    m_channelToOffset.clear();

    if (!m_currentProducer) {
        updateHistogram();
        return;
    }

    if (info.isProducer) {
        m_color = true;
        m_channels = m_currentProducer->channels();
        for (uint i = 0; i < m_channels.count(); i++)
            m_channelToOffset.append(i);
        m_histogram->setChannel(0); // Set a default channel, just being nice
    } else {
        m_color = false;
        TQValueVector<KisChannelInfo *> channels = m_currentProducer->channels();
        for (uint i = 0; i < channels.count(); i++) {
            KisChannelInfo* channel = channels.at(i);
            if (channel->name() == info.channel->name()) {
                m_channels.append(channel);
                m_channelToOffset.append(i);
                break;
            }
        }
    }

    updateHistogram();
}

void KisHistogramView::setHistogramType(enumHistogramType type)
{
    m_histogram->setHistogramType(type);
    updateHistogram();
}

void KisHistogramView::setChannels()
{
    m_comboInfo.clear();
    m_channelStrings.clear();
    m_channels.clear();
    m_channelToOffset.clear();

    KisIDList list = KisHistogramProducerFactoryRegistry::instance()->listKeysCompatibleWith(m_cs);

    if (list.count() == 0) {
        // XXX: No native histogram for this colorspace. Using converted RGB. We should have a warning
        KisGenericRGBHistogramProducerFactory f;
        addProducerChannels(f.generate());
    } else {
        for (uint i = 0; i < list.count(); i++) {
            KisID id(*(list.at(i)));
            addProducerChannels( KisHistogramProducerFactoryRegistry::instance()->get(id)->generate() );
        }
    }

    m_currentProducer = m_comboInfo.at(0).producer;
    m_color = false;
    // The currently displayed channel and its offset
    m_channels.append(m_comboInfo.at(1).channel);
    m_channelToOffset.append(0);
}

void KisHistogramView::addProducerChannels(KisHistogramProducerSP producer) {
        ComboboxInfo info;
        info.isProducer = true;
        info.producer = producer;
        // channel not used for a producer
        TQValueVector<KisChannelInfo *> channels = info.producer->channels();
        int count = channels.count();
        m_comboInfo.append(info);
        m_channelStrings.append(producer->id() . name());
        for (int j = 0; j < count; j++) {
            info.isProducer = false;
            info.channel = channels.at(j);
            m_comboInfo.append(info);
            m_channelStrings.append(TQString(" ").append(info.channel->name()));
        }
}

void KisHistogramView::updateHistogram()
{
    TQ_UINT32 height = this->height();
    int selFrom, selTo; // from - to in bins

    if (!m_currentProducer) { // Something's very wrong: no producer for this colorspace to update histogram with!
        return;
    }

    TQ_INT32 bins = m_histogram->producer()->numberOfBins();
    m_pix = TQPixmap(bins, height);
    m_pix.fill();
    TQPainter p(&m_pix);
    p.setBrush(TQt::black);

    // Draw the box of the selection, if any
    if (m_histogram->hasSelection()) {
        double width = m_histogram->selectionTo() - m_histogram->selectionFrom();
        double factor = static_cast<double>(bins) / m_histogram->producer()->viewWidth();
        selFrom = static_cast<int>(m_histogram->selectionFrom() * factor);
        selTo = selFrom + static_cast<int>(width * factor);
        p.drawRect(selFrom, 0, selTo - selFrom, height);
    } else {
        // We don't want the drawing to think we're in a selected area
        selFrom = -1;
        selTo = 2;
    }

    TQ_INT32 i = 0;
    double highest = 0;
    bool blackOnBlack = false;

    // First we iterate once, so that we have the overall maximum. This is a bit inefficient,
    // but not too much since the histogram caches the calculations
    for (uint chan = 0; chan < m_channels.count(); chan++) {
        m_histogram->setChannel(m_channelToOffset.at(chan));
        if ((double)m_histogram->calculations().getHighest() > highest)
            highest = (double)m_histogram->calculations().getHighest();
    }

    for (uint chan = 0; chan < m_channels.count(); chan++) {
        TQColor color;
        m_histogram->setChannel(m_channelToOffset.at(chan));

        if (m_color) {
            color = m_channels.at(chan)->color();
            p.setPen(color);
        } else {
            color = TQt::black;
        }
        blackOnBlack = (color == TQt::black);

        if (m_histogram->getHistogramType() == LINEAR) {
            double factor = (double)height / highest;
            for( i=0; i<bins; ++i ) {
                // So that we get a good view even with a selection box with
                // black colors on background of black selection
                if (i >= selFrom && i < selTo && blackOnBlack) {
                    p.setPen(TQt::white);
                } else {
                    p.setPen(color);
                }
                p.drawLine(i, height, i, height - int(m_histogram->getValue(i) * factor));
            }
        } else {
            double factor = (double)height / (double)log(highest);
            for( i = 0; i < bins; ++i ) {
                // Same as above
                if (i >= selFrom && i < selTo && blackOnBlack) {
                    p.setPen(TQt::white);
                } else {
                    p.setPen(color);
                }
                p.drawLine(i, height, i,
                           height - int(log((double)m_histogram->getValue(i)) * factor));
            }
        }
    }

    setPixmap(m_pix);
}

void KisHistogramView::mousePressEvent(TQMouseEvent * e) {
    if (e->button() == Qt::RightButton)
        emit rightClicked(e->globalPos());
    else
        TQLabel::mousePressEvent(e);
}


#include "kis_histogram_view.moc"