krita

kis_autobrush.cc

00001 /*
00002  *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018  
00019 #include "kis_autobrush.h"
00020 #include <KoImageResource.h>
00021 #include <kdebug.h>
00022 #include <qspinbox.h>
00023 #include <qtoolbutton.h>
00024 #include <qimage.h>
00025 #include <qcombobox.h>
00026 #include <qlabel.h>
00027 
00028 
00029 KisAutobrush::KisAutobrush(QWidget *parent, const char* name, const QString& caption) : KisWdgAutobrush(parent, name)
00030 {
00031     setCaption(caption);
00032 
00033     m_linkSize = true;
00034     m_linkFade = true;
00035     
00036     linkFadeToggled(m_linkSize);
00037     linkSizeToggled(m_linkFade);
00038 
00039     connect(bnLinkSize, SIGNAL(toggled(bool)), this, SLOT(linkSizeToggled( bool )));
00040     connect(bnLinkFade, SIGNAL(toggled(bool)), this, SLOT(linkFadeToggled( bool )));
00041     
00042     connect((QObject*)comboBoxShape, SIGNAL(activated(int)), this, SLOT(paramChanged()));
00043     spinBoxWidth->setMinValue(1);
00044     connect(spinBoxWidth,SIGNAL(valueChanged(int)),this,SLOT(spinBoxWidthChanged(int)));
00045     spinBoxHeigth->setMinValue(1);
00046     connect(spinBoxHeigth,SIGNAL(valueChanged(int)),this,SLOT(spinBoxHeigthChanged(int)));
00047     spinBoxHorizontal->setMinValue(0);
00048     connect(spinBoxHorizontal,SIGNAL(valueChanged(int)),this,SLOT(spinBoxHorizontalChanged(int)));
00049     spinBoxVertical->setMinValue(0);
00050     connect(spinBoxVertical,SIGNAL(valueChanged(int)),this,SLOT(spinBoxVerticalChanged(int)));
00051 
00052     m_brsh = new QImage(1,1,32);
00053     Q_CHECK_PTR(m_brsh);
00054 
00055     paramChanged();
00056 
00057 
00058     connect(brushPreview, SIGNAL(clicked()), SLOT(paramChanged()));
00059 
00060 }
00061 
00062 void KisAutobrush::resizeEvent ( QResizeEvent * )
00063 {
00064     brushPreview->setMinimumHeight(brushPreview->width()); // dirty hack !
00065     brushPreview->setMaximumHeight(brushPreview->width()); // dirty hack !
00066 }
00067 
00068 void KisAutobrush::activate()
00069 {
00070     paramChanged();
00071 }
00072 
00073 void KisAutobrush::paramChanged()
00074 {
00075     Q_INT32 fh = QMIN( spinBoxWidth->value()/2, spinBoxHorizontal->value() ) ;
00076     Q_INT32 fv = QMIN( spinBoxHeigth->value()/2, spinBoxVertical->value() );
00077     KisAutobrushShape* kas;
00078 
00079     if(comboBoxShape->currentItem() == 0) // use index compare instead of comparing a translatable string
00080     {
00081         kas = new KisAutobrushCircleShape(spinBoxWidth->value(),  spinBoxHeigth->value(), fh, fv);
00082         Q_CHECK_PTR(kas);
00083 
00084     } else {
00085         kas = new KisAutobrushRectShape(spinBoxWidth->value(),  spinBoxHeigth->value(), fh, fv);
00086         Q_CHECK_PTR(kas);
00087 
00088     }
00089     kas->createBrush(m_brsh);
00090 
00091     QPixmap p;
00092     QImage pi(*m_brsh);
00093     double coeff = 1.0;
00094     int bPw = brushPreview->width()-3;
00095     if(pi.width() > bPw)
00096     {
00097         coeff =  bPw /(double)pi.width();
00098     }
00099     int bPh = brushPreview->height()-3;
00100     if(pi.height() > coeff * bPh)
00101     {
00102         coeff = bPh /(double)pi.height();
00103     }
00104     if( coeff < 1.0)
00105     {
00106         pi = pi.smoothScale( (int)(coeff * pi.width()) , (int)(coeff * pi.height()));
00107     }
00108     
00109     p.convertFromImage(pi);
00110     brushPreview->setPixmap(p);
00111     KisAutobrushResource * resource = new KisAutobrushResource(*m_brsh);
00112     Q_CHECK_PTR(resource);
00113 
00114     emit(activatedResource(resource));
00115     delete kas;
00116 }
00117 void KisAutobrush::spinBoxWidthChanged(int a)
00118 {
00119     spinBoxHorizontal->setMaxValue(a/2);
00120     if(m_linkSize)
00121     {
00122         spinBoxHeigth->setValue(a);
00123         spinBoxVertical->setMaxValue(a/2);
00124     }
00125     this->paramChanged();
00126 }
00127 void KisAutobrush::spinBoxHeigthChanged(int a)
00128 {
00129     spinBoxVertical->setMaxValue(a/2);
00130     if(m_linkSize)
00131     {
00132         spinBoxWidth->setValue(a);
00133         spinBoxHorizontal->setMaxValue(a/2);
00134     }
00135     this->paramChanged();
00136 }
00137 void KisAutobrush::spinBoxHorizontalChanged(int a)
00138 {
00139     if(m_linkFade)
00140         spinBoxVertical->setValue(a);
00141     this->paramChanged();
00142 }
00143 void KisAutobrush::spinBoxVerticalChanged(int a)
00144 {
00145     if(m_linkFade)
00146         spinBoxHorizontal->setValue(a);
00147     this->paramChanged();
00148 }
00149 
00150 void KisAutobrush::linkSizeToggled(bool b)
00151 {
00152     m_linkSize = b;
00153 
00154     KoImageResource kir;
00155     if (b) {
00156         bnLinkSize->setPixmap(kir.chain());
00157     }
00158     else {
00159         bnLinkSize->setPixmap(kir.chainBroken());
00160     }
00161 }
00162 
00163 void KisAutobrush::linkFadeToggled(bool b)
00164 {
00165     m_linkFade = b;
00166 
00167     KoImageResource kir;
00168     if (b) {
00169         bnLinkFade->setPixmap(kir.chain());
00170     }
00171     else {
00172         bnLinkFade->setPixmap(kir.chainBroken());
00173     }
00174 }
00175 
00176 
00177 #include "kis_autobrush.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys