krita

kis_custom_brush.cc

00001 /*
00002  *  Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
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 <KoImageResource.h>
00020 #include <kdebug.h>
00021 #include <qlabel.h>
00022 #include <qimage.h>
00023 #include <qpushbutton.h>
00024 #include <qcombobox.h>
00025 #include <qcheckbox.h>
00026 #include <kglobal.h>
00027 #include <kstandarddirs.h>
00028 #include <ktempfile.h>
00029 
00030 #include "kis_view.h"
00031 #include "kis_image.h"
00032 #include "kis_layer.h"
00033 #include "kis_paint_device.h"
00034 #include "kis_brush.h"
00035 #include "kis_imagepipe_brush.h"
00036 #include "kis_custom_brush.h"
00037 #include "kis_resource_mediator.h"
00038 #include "kis_resourceserver.h"
00039 #include "kis_paint_layer.h"
00040 #include "kis_group_layer.h"
00041 
00042 KisCustomBrush::KisCustomBrush(QWidget *parent, const char* name, const QString& caption, KisView* view)
00043     : KisWdgCustomBrush(parent, name), m_view(view)
00044 {
00045     Q_ASSERT(m_view);
00046     m_mediator = 0;
00047     setCaption(caption);
00048 
00049     m_brush = 0;
00050 
00051     preview->setScaledContents(true);
00052 
00053     connect(addButton, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
00054     connect(brushButton, SIGNAL(pressed()), this, SLOT(slotUseBrush()));
00055 //    connect(exportButton, SIGNAL(pressed()), this, SLOT(slotExport()));
00056     connect(style, SIGNAL(activated(int)), this, SLOT(slotUpdateCurrentBrush(int)));
00057     connect(colorAsMask, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateCurrentBrush(int)));
00058 }
00059 
00060 KisCustomBrush::~KisCustomBrush() {
00061     delete m_brush;
00062 }
00063 
00064 void KisCustomBrush::showEvent(QShowEvent *) {
00065     slotUpdateCurrentBrush(0);
00066 }
00067 
00068 void KisCustomBrush::slotUpdateCurrentBrush(int) {
00069     delete m_brush;
00070     if (m_view->canvasSubject() && m_view->canvasSubject()->currentImg()) {
00071         createBrush();
00072         preview->setPixmap(QPixmap(m_brush->img()));
00073     } else {
00074         m_brush = 0;
00075     }
00076 }
00077 
00078 void KisCustomBrush::slotExport() {
00079     ;
00080 }
00081 
00082 void KisCustomBrush::slotAddPredefined() {
00083     // Save in the directory that is likely to be: ~/.kde/share/apps/krita/brushes
00084     // a unique file with this brushname
00085     QString dir = KGlobal::dirs()->saveLocation("data", "krita/brushes");
00086     QString extension;
00087 
00088     if (style->currentItem() == 0) {
00089         extension = ".gbr";
00090     } else {
00091         extension = ".gih";
00092     }
00093     KTempFile file(dir, extension);
00094     file.close(); // If we don't, and brush->save first, it might get truncated!
00095 
00096     // Save it to that file 
00097     m_brush->setFilename(file.name());
00098 
00099     // Add it to the brush server, so that it automatically gets to the mediators, and
00100     // so to the other brush choosers can pick it up, if they want to
00101     if (m_server)
00102         m_server->addResource(m_brush->clone());
00103 }
00104 
00105 void KisCustomBrush::slotUseBrush() {
00106     KisBrush* copy = m_brush->clone();
00107 
00108     Q_CHECK_PTR(copy);
00109 
00110     emit(activatedResource(copy));
00111 }
00112 
00113 void KisCustomBrush::createBrush() {
00114     KisImageSP img = m_view->canvasSubject()->currentImg();
00115 
00116     if (!img)
00117         return;
00118 
00119     if (style->currentItem() == 0) {
00120         m_brush = new KisBrush(img->mergedImage(), 0, 0, img->width(), img->height());
00121         if (colorAsMask->isChecked())
00122             m_brush->makeMaskImage();
00123         return;
00124     }
00125 
00126     // For each layer in the current image, create a new image, and add it to the list
00127     QValueVector< QValueVector<KisPaintDevice*> > devices;
00128     devices.push_back(QValueVector<KisPaintDevice*>());
00129     int w = img->width();
00130     int h = img->height();
00131 
00132     // We only loop over the rootLayer. Since we actually should have a layer selection
00133     // list, no need to elaborate on that here and now
00134     KisLayer* layer = img->rootLayer()->firstChild();
00135     while (layer) {
00136         KisPaintLayer* paint = 0;
00137         if (layer->visible() && (paint = dynamic_cast<KisPaintLayer*>(layer)))
00138             devices.at(0).push_back(paint->paintDevice());
00139         layer = layer->nextSibling();
00140     }
00141     QValueVector<KisPipeBrushParasite::SelectionMode> modes;
00142 
00143     switch(comboBox2->currentItem()) {
00144         case 0: modes.push_back(KisPipeBrushParasite::Constant); break;
00145         case 1: modes.push_back(KisPipeBrushParasite::Random); break;
00146         case 2: modes.push_back(KisPipeBrushParasite::Incremental); break;
00147         case 3: modes.push_back(KisPipeBrushParasite::Pressure); break;
00148         case 4: modes.push_back(KisPipeBrushParasite::Angular); break;
00149         default: modes.push_back(KisPipeBrushParasite::Incremental);
00150     }
00151 
00152     m_brush = new KisImagePipeBrush(img->name(), w, h, devices, modes);
00153     if (colorAsMask->isChecked())
00154         m_brush->makeMaskImage();
00155 }
00156 
00157 
00158 #include "kis_custom_brush.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys