chalk

kis_autobrush.cpp
1/*
2 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "kis_autobrush.h"
20#include <KoImageResource.h>
21#include <kdebug.h>
22#include <tqspinbox.h>
23#include <tqtoolbutton.h>
24#include <tqimage.h>
25#include <tqcombobox.h>
26#include <tqlabel.h>
27
28
29KisAutobrush::KisAutobrush(TQWidget *parent, const char* name, const TQString& caption) : KisWdgAutobrush(parent, name)
30{
31 setCaption(caption);
32
33 m_linkSize = true;
34 m_linkFade = true;
35
36 linkFadeToggled(m_linkSize);
37 linkSizeToggled(m_linkFade);
38
39 connect(bnLinkSize, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(linkSizeToggled( bool )));
40 connect(bnLinkFade, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(linkFadeToggled( bool )));
41
42 connect((TQObject*)comboBoxShape, TQ_SIGNAL(activated(int)), this, TQ_SLOT(paramChanged()));
43 spinBoxWidth->setMinValue(1);
44 connect(spinBoxWidth,TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(spinBoxWidthChanged(int)));
45 spinBoxHeight->setMinValue(1);
46 connect(spinBoxHeight,TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(spinBoxHeightChanged(int)));
47 spinBoxHorizontal->setMinValue(0);
48 connect(spinBoxHorizontal,TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(spinBoxHorizontalChanged(int)));
49 spinBoxVertical->setMinValue(0);
50 connect(spinBoxVertical,TQ_SIGNAL(valueChanged(int)),this,TQ_SLOT(spinBoxVerticalChanged(int)));
51
52 m_brsh = new TQImage(1,1,32);
53 TQ_CHECK_PTR(m_brsh);
54
55 paramChanged();
56
57
58 connect(brushPreview, TQ_SIGNAL(clicked()), TQ_SLOT(paramChanged()));
59
60}
61
62void KisAutobrush::resizeEvent ( TQResizeEvent * )
63{
64 brushPreview->setMinimumHeight(brushPreview->width()); // dirty hack !
65 brushPreview->setMaximumHeight(brushPreview->width()); // dirty hack !
66}
67
68void KisAutobrush::activate()
69{
70 paramChanged();
71}
72
73void KisAutobrush::paramChanged()
74{
75 TQ_INT32 fh = TQMIN( spinBoxWidth->value()/2, spinBoxHorizontal->value() ) ;
76 TQ_INT32 fv = TQMIN( spinBoxHeight->value()/2, spinBoxVertical->value() );
77 KisAutobrushShape* kas;
78
79 if(comboBoxShape->currentItem() == 0) // use index compare instead of comparing a translatable string
80 {
81 kas = new KisAutobrushCircleShape(spinBoxWidth->value(), spinBoxHeight->value(), fh, fv);
82 TQ_CHECK_PTR(kas);
83
84 } else {
85 kas = new KisAutobrushRectShape(spinBoxWidth->value(), spinBoxHeight->value(), fh, fv);
86 TQ_CHECK_PTR(kas);
87
88 }
89 kas->createBrush(m_brsh);
90
91 TQPixmap p;
92 TQImage pi(*m_brsh);
93 double coeff = 1.0;
94 int bPw = brushPreview->width()-3;
95 if(pi.width() > bPw)
96 {
97 coeff = bPw /(double)pi.width();
98 }
99 int bPh = brushPreview->height()-3;
100 if(pi.height() > coeff * bPh)
101 {
102 coeff = bPh /(double)pi.height();
103 }
104 if( coeff < 1.0)
105 {
106 pi = pi.smoothScale( (int)(coeff * pi.width()) , (int)(coeff * pi.height()));
107 }
108
109 p.convertFromImage(pi);
110 brushPreview->setPixmap(p);
111 KisAutobrushResource * resource = new KisAutobrushResource(*m_brsh);
112 TQ_CHECK_PTR(resource);
113
114 emit(activatedResource(resource));
115 delete kas;
116}
117void KisAutobrush::spinBoxWidthChanged(int a)
118{
119 spinBoxHorizontal->setMaxValue(a/2);
120 if(m_linkSize)
121 {
122 spinBoxHeight->setValue(a);
123 spinBoxVertical->setMaxValue(a/2);
124 }
125 this->paramChanged();
126}
127void KisAutobrush::spinBoxHeightChanged(int a)
128{
129 spinBoxVertical->setMaxValue(a/2);
130 if(m_linkSize)
131 {
132 spinBoxWidth->setValue(a);
133 spinBoxHorizontal->setMaxValue(a/2);
134 }
135 this->paramChanged();
136}
137void KisAutobrush::spinBoxHorizontalChanged(int a)
138{
139 if(m_linkFade)
140 spinBoxVertical->setValue(a);
141 this->paramChanged();
142}
143void KisAutobrush::spinBoxVerticalChanged(int a)
144{
145 if(m_linkFade)
146 spinBoxHorizontal->setValue(a);
147 this->paramChanged();
148}
149
150void KisAutobrush::linkSizeToggled(bool b)
151{
152 m_linkSize = b;
153
154 KoImageResource kir;
155 if (b) {
156 bnLinkSize->setPixmap(kir.chain());
157 }
158 else {
159 bnLinkSize->setPixmap(kir.chainBroken());
160 }
161}
162
163void KisAutobrush::linkFadeToggled(bool b)
164{
165 m_linkFade = b;
166
167 KoImageResource kir;
168 if (b) {
169 bnLinkFade->setPixmap(kir.chain());
170 }
171 else {
172 bnLinkFade->setPixmap(kir.chainBroken());
173 }
174}
175
176
177#include "kis_autobrush.moc"