summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/viewplugins/histogram_docker/histogramdocker.cc
blob: 9c679507283410952e0669839d52d7306c57f776 (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
/*
 * This file is part of the KDE project
 *
 *  Copyright (c) 2005 Bart Coppens <[email protected]>
 *
 *  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 <tqtooltip.h>

#include <klocale.h>
#include <kiconloader.h>
#include <kinstance.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <ktempfile.h>
#include <kdebug.h>
#include <kgenericfactory.h>

#include <kopalettemanager.h>

#include "kis_meta_registry.h"
#include <kis_doc.h>
#include <kis_global.h>
#include <kis_types.h>
#include <kis_view.h>

#include <kis_basic_histogram_producers.h>
#include <kis_colorspace_factory_registry.h>

#include "histogramdocker.h"
#include "kis_imagerasteredcache.h"
#include "kis_accumulating_producer.h"

typedef KGenericFactory<ChalkHistogramDocker> ChalkHistogramDockerFactory;
K_EXPORT_COMPONENT_FACTORY( chalkhistogramdocker, ChalkHistogramDockerFactory( "chalk" ) )

ChalkHistogramDocker::ChalkHistogramDocker(TQObject *parent, const char *name, const TQStringList&)
        : KParts::Plugin(parent, name)
{

    if ( parent->inherits("KisView") ) {
        m_view = dynamic_cast<KisView*>(parent);

        setInstance(ChalkHistogramDockerFactory::instance());
        setXMLFile(locate("data","chalkplugins/chalkhistogramdocker.rc"), true);

        KisImageSP img = m_view->canvasSubject()->currentImg();
        if (!img) {
            m_cache = 0;
            return;
        }

        m_hview = 0; // producerChanged wants to setCurrentChannels, prevent that here
        m_cache = 0; // we try to delete it in producerChanged
        colorSpaceChanged(img->colorSpace()); // calls producerChanged(0)


        m_hview = new KisHistogramView(m_view);
        TQToolTip::add(m_hview, i18n("Right-click to select histogram type"));
        m_hview->setHistogram(m_histogram);
        m_hview->setColor(true);
        m_hview->setCurrentChannels(m_producer, m_producer->channels());
        m_hview->setFixedSize(256, 100); // XXX if not it keeps expanding
        m_hview->setCaption(i18n("Histogram"));


        connect(m_hview, TQT_SIGNAL(rightClicked(const TQPoint&)),
                this, TQT_SLOT(popupMenu(const TQPoint&)));
        connect(m_cache, TQT_SIGNAL(cacheUpdated()),
                new HistogramDockerUpdater(this, m_histogram, m_hview, m_producer), TQT_SLOT(updated()));
        connect(&m_popup, TQT_SIGNAL(activated(int)),
                this, TQT_SLOT(producerChanged(int)));
        connect(img, TQT_SIGNAL(sigColorSpaceChanged(KisColorSpace*)),
                this, TQT_SLOT(colorSpaceChanged(KisColorSpace*))); // No need to force updates here

        // Add it to the control palette
        m_view->canvasSubject()->paletteManager()->addWidget(
            m_hview, "histodocker", chalk::CONTROL_PALETTE);
    } else {
        m_cache = 0;
    }
}

ChalkHistogramDocker::~ChalkHistogramDocker()
{
    uint count = m_producers . count();
    for (uint i = 0; i < count; i++) {
        delete m_producers . at(i);
    }

    if (m_cache)
        m_cache->deleteLater();
}

void ChalkHistogramDocker::producerChanged(int pos)
{
    if (m_cache)
        m_cache->deleteLater();
    m_cache = 0;

    if (m_currentProducerPos < m_popup.count())
        m_popup.setItemChecked(m_currentProducerPos, false);
    m_currentProducerPos = pos;
    m_popup.setItemChecked(m_currentProducerPos, true);

    uint count = m_producers . count();
    for (uint i = 0; i < count; i++) {
        delete m_producers . at(i);
    }
    m_producers.clear();

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

    m_factory = KisHistogramProducerFactoryRegistry::instance()->get(*(keys.at(pos)));

    KisCachedHistogramObserver observer(&m_producers, m_factory, 0, 0, 0, 0, false);

    // We can reference observer because it will be only used as a factory to create new
    // instances
    m_cache = new KisImageRasteredCache(m_view, &observer);

    m_producer = new KisAccumulatingHistogramProducer(&m_producers);

    // use dummy layer as a source; we are not going to actually use or need it
    // All of these are SP, no need to delete them afterwards
    m_histogram = new KisHistogram( new KisPaintDevice(KisMetaRegistry::instance()->csRegistry()->getAlpha8(), "dummy histogram"), m_producer, LOGARITHMIC);

    if (m_hview) {
        m_hview->setHistogram(m_histogram);
        m_hview->setColor(true);
        m_hview->setCurrentChannels(m_producer, m_producer->channels());

        connect(m_cache, TQT_SIGNAL(cacheUpdated()),
                new HistogramDockerUpdater(this, m_histogram, m_hview, m_producer), TQT_SLOT(updated()));
    }
}

void ChalkHistogramDocker::popupMenu(const TQPoint& pos)
{
    m_popup.popup(pos, m_currentProducerPos);
}

void ChalkHistogramDocker::colorSpaceChanged(KisColorSpace* cs)
{
    m_cs = cs;

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

    m_popup.clear();
    m_currentProducerPos = 0;

    for (uint i = 0; i < keys.count(); i++) {
        KisID id(*(keys.at(i)));
        m_popup . insertItem(id.name(), static_cast<int>(i));
    }

    producerChanged(0);
}

HistogramDockerUpdater::HistogramDockerUpdater(TQObject* /*parent*/, KisHistogramSP h, KisHistogramView* v,
                                               KisAccumulatingHistogramProducer* p)
    : m_histogram(h), m_view(v), m_producer(p)
{
    connect(p, TQT_SIGNAL(completed()), this, TQT_SLOT(completed()));
}

void HistogramDockerUpdater::updated() {
    // We don't [!] do m_histogram->updateHistogram();, because that will try to compute
    // the histogram synchronously, while we want it asynchronously.
    m_producer->addRegionsToBinAsync();
}

void HistogramDockerUpdater::completed() {
    m_histogram->computeHistogram();
    m_view->updateHistogram();
}

#include "histogramdocker.moc"