summaryrefslogtreecommitdiffstats
path: root/kolourpaint/pixmapfx/kpeffectflatten.cpp
blob: 1768aa96aa393f5688fb2287caaa7068c90c175f (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

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define DEBUG_KP_EFFECT_FLATTEN 0


#include <kpeffectflatten.h>

#include <tqcheckbox.h>
#include <tqimage.h>
#include <tqlayout.h>
#include <tqpixmap.h>
#include <tqvbox.h>

#include <kcolorbutton.h>
#include <kconfig.h>
#include <kdialog.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kimageeffect.h>
#include <klocale.h>

#include <kpdefs.h>
#include <kppixmapfx.h>


//
// kpEffectFlattenCommand
//

kpEffectFlattenCommand::kpEffectFlattenCommand (const TQColor &color1,
                                                const TQColor &color2,
                                                bool actOnSelection,
                                                kpMainWindow *mainWindow)
    : kpColorEffectCommand (i18n ("Flatten"), actOnSelection, mainWindow),
      m_color1 (color1), m_color2 (color2)
{
}

kpEffectFlattenCommand::~kpEffectFlattenCommand ()
{
}


// public static
void kpEffectFlattenCommand::apply (TQPixmap *destPixmapPtr,
                                    const TQColor &color1, const TQColor &color2)
{
    if (!destPixmapPtr)
        return;

    TQImage image = kpPixmapFX::convertToImage (*destPixmapPtr);
    apply (&image, color1, color2);
    *destPixmapPtr = kpPixmapFX::convertToPixmap (image);
}

// public static
TQPixmap kpEffectFlattenCommand::apply (const TQPixmap &pm,
                                       const TQColor &color1, const TQColor &color2)
{
    TQImage image = kpPixmapFX::convertToImage (pm);
    apply (&image, color1, color2);
    return kpPixmapFX::convertToPixmap (image);
}

// public static
void kpEffectFlattenCommand::apply (TQImage *destImagePtr,
                                    const TQColor &color1, const TQColor &color2)
{
    if (!destImagePtr)
        return;

    KImageEffect::flatten (*destImagePtr/*ref*/, color1, color2);
}

// public static
TQImage kpEffectFlattenCommand::apply (const TQImage &img,
                                      const TQColor &color1, const TQColor &color2)
{
    TQImage retImage = img;
    apply (&retImage, color1, color2);
    return retImage;
}


//
// kpEffectFlattenCommand implements kpColorEffectCommand interface
//

// protected virtual [base kpColorEffectCommand]
TQPixmap kpEffectFlattenCommand::applyColorEffect (const TQPixmap &pixmap)
{
    return apply (pixmap, m_color1, m_color2);
}


//
// kpEffectFlattenWidget
//

// public static
// Don't initialise globally when we probably don't have a colour
// allocation context.  This way, the colours aren't sometimes invalid
// (e.g. at 8-bit).
TQColor kpEffectFlattenWidget::s_lastColor1;
TQColor kpEffectFlattenWidget::s_lastColor2;

kpEffectFlattenWidget::kpEffectFlattenWidget (bool actOnSelection,
                                              kpMainWindow *mainWindow,
                                              TQWidget *parent,
                                              const char *name)
    : kpColorEffectWidget (actOnSelection, mainWindow, parent, name)
{
    if (!s_lastColor1.isValid () || !s_lastColor2.isValid ())
    {
        KConfigGroupSaver cfgGroupSaver (KGlobal::config (), kpSettingsGroupFlattenEffect);
        KConfigBase *cfg = cfgGroupSaver.config ();

        s_lastColor1 = cfg->readColorEntry (kpSettingFlattenEffectColor1);
        if (!s_lastColor1.isValid ())
            s_lastColor1 = TQt::red;

        s_lastColor2 = cfg->readColorEntry (kpSettingFlattenEffectColor2);
        if (!s_lastColor2.isValid ())
            s_lastColor2 = TQt::blue;
    }


    m_enableCheckBox = new TQCheckBox (i18n ("E&nable"), this);

    TQVBox *colorButtonContainer = new TQVBox (this);
    colorButtonContainer->setMargin (KDialog::marginHint () / 2);
    colorButtonContainer->setSpacing (spacingHint ());
    m_color1Button = new KColorButton (s_lastColor1, colorButtonContainer);
    m_color2Button = new KColorButton (s_lastColor2, colorButtonContainer);


    m_color1Button->setEnabled (false);
    m_color2Button->setEnabled (false);


    TQVBoxLayout *lay = new TQVBoxLayout (this, marginHint (), spacingHint ());
    lay->addWidget (m_enableCheckBox);
    lay->addWidget (colorButtonContainer);


    connect (m_enableCheckBox, TQT_SIGNAL (toggled (bool)),
             this, TQT_SLOT (slotEnableChanged (bool)));

    connect (m_color1Button, TQT_SIGNAL (changed (const TQColor &)),
             this, TQT_SIGNAL (settingsChanged ()));
    connect (m_color2Button, TQT_SIGNAL (changed (const TQColor &)),
             this, TQT_SIGNAL (settingsChanged ()));
}

kpEffectFlattenWidget::~kpEffectFlattenWidget ()
{
    s_lastColor1 = color1 ();
    s_lastColor2 = color2 ();


    KConfigGroupSaver cfgGroupSaver (KGlobal::config (), kpSettingsGroupFlattenEffect);
    KConfigBase *cfg = cfgGroupSaver.config ();

    cfg->writeEntry (kpSettingFlattenEffectColor1, s_lastColor1);
    cfg->writeEntry (kpSettingFlattenEffectColor2, s_lastColor2);
    cfg->sync ();
}


// public
TQColor kpEffectFlattenWidget::color1 () const
{
    return m_color1Button->color ();
}

// public
TQColor kpEffectFlattenWidget::color2 () const
{
    return m_color2Button->color ();
}


//
// kpEffectFlattenWidget implements kpColorEffectWidget interface
//

// public virtual [base kpColorEffectWidget]
TQString kpEffectFlattenWidget::caption () const
{
    return i18n ("Colors");
}


// public virtual [base kpColorEffectWidget]
bool kpEffectFlattenWidget::isNoOp () const
{
    return !m_enableCheckBox->isChecked ();
}

// public virtual [base kpColorEffectWidget]
TQPixmap kpEffectFlattenWidget::applyColorEffect (const TQPixmap &pixmap)
{
#if DEBUG_KP_EFFECT_FLATTEN
    kdDebug () << "kpEffectFlattenWidget::applyColorEffect() nop="
               << isNoOp () << endl;
#endif

    if (isNoOp ())
        return pixmap;

    return kpEffectFlattenCommand::apply (pixmap, color1 (), color2 ());
}


// public virtual [base kpColorEffectWidget]
kpColorEffectCommand *kpEffectFlattenWidget::createCommand () const
{
    return new kpEffectFlattenCommand (color1 (), color2 (),
                                       m_actOnSelection,
                                       m_mainWindow);
}


// protected slot:
void kpEffectFlattenWidget::slotEnableChanged (bool enable)
{
#if DEBUG_KP_EFFECT_FLATTEN
    kdDebug () << "kpEffectFlattenWidget::slotEnableChanged(" << enable
               << ") enableButton=" << m_enableCheckBox->isChecked ()
               << endl;
#endif

    m_color1Button->setEnabled (enable);
    m_color2Button->setEnabled (enable);

    emit settingsChanged ();
}


#include <kpeffectflatten.moc>