chalk

kis_custom_brush.cpp
1/*
2 * Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
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 <KoImageResource.h>
20#include <kdebug.h>
21#include <tqlabel.h>
22#include <tqimage.h>
23#include <tqpushbutton.h>
24#include <tqcombobox.h>
25#include <tqcheckbox.h>
26#include <tdeglobal.h>
27#include <kstandarddirs.h>
28#include <tdetempfile.h>
29
30#include "kis_view.h"
31#include "kis_image.h"
32#include "kis_layer.h"
33#include "kis_paint_device.h"
34#include "kis_brush.h"
35#include "kis_imagepipe_brush.h"
36#include "kis_custom_brush.h"
37#include "kis_resource_mediator.h"
38#include "kis_resourceserver.h"
39#include "kis_paint_layer.h"
40#include "kis_group_layer.h"
41
42KisCustomBrush::KisCustomBrush(TQWidget *parent, const char* name, const TQString& caption, KisView* view)
43 : KisWdgCustomBrush(parent, name), m_view(view)
44{
45 Q_ASSERT(m_view);
46 m_mediator = 0;
47 setCaption(caption);
48
49 m_brush = 0;
50
51 preview->setScaledContents(true);
52
53 connect(addButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotAddPredefined()));
54 connect(brushButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotUseBrush()));
55// connect(exportButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotExport()));
56 connect(style, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotUpdateCurrentBrush(int)));
57 connect(colorAsMask, TQ_SIGNAL(stateChanged(int)), this, TQ_SLOT(slotUpdateCurrentBrush(int)));
58}
59
60KisCustomBrush::~KisCustomBrush() {
61 delete m_brush;
62}
63
64void KisCustomBrush::showEvent(TQShowEvent *) {
65 slotUpdateCurrentBrush(0);
66}
67
68void KisCustomBrush::slotUpdateCurrentBrush(int) {
69 delete m_brush;
70 if (m_view->canvasSubject() && m_view->canvasSubject()->currentImg()) {
71 createBrush();
72 preview->setPixmap(TQPixmap(m_brush->img()));
73 } else {
74 m_brush = 0;
75 }
76}
77
78void KisCustomBrush::slotExport() {
79 ;
80}
81
82void KisCustomBrush::slotAddPredefined() {
83 // Save in the directory that is likely to be: ~/.trinity/share/apps/chalk/brushes
84 // a unique file with this brushname
85 TQString dir = TDEGlobal::dirs()->saveLocation("data", "chalk/brushes");
86 TQString extension;
87
88 if (style->currentItem() == 0) {
89 extension = ".gbr";
90 } else {
91 extension = ".gih";
92 }
93 KTempFile file(dir, extension);
94 file.close(); // If we don't, and brush->save first, it might get truncated!
95
96 // Save it to that file
97 m_brush->setFilename(file.name());
98
99 // Add it to the brush server, so that it automatically gets to the mediators, and
100 // so to the other brush choosers can pick it up, if they want to
101 if (m_server)
102 m_server->addResource(m_brush->clone());
103}
104
105void KisCustomBrush::slotUseBrush() {
106 KisBrush* copy = m_brush->clone();
107
108 TQ_CHECK_PTR(copy);
109
110 emit(activatedResource(copy));
111}
112
113void KisCustomBrush::createBrush() {
114 KisImageSP img = m_view->canvasSubject()->currentImg();
115
116 if (!img)
117 return;
118
119 if (style->currentItem() == 0) {
120 m_brush = new KisBrush(img->mergedImage(), 0, 0, img->width(), img->height());
121 if (colorAsMask->isChecked())
122 m_brush->makeMaskImage();
123 return;
124 }
125
126 // For each layer in the current image, create a new image, and add it to the list
127 TQValueVector< TQValueVector<KisPaintDevice*> > devices;
128 devices.push_back(TQValueVector<KisPaintDevice*>());
129 int w = img->width();
130 int h = img->height();
131
132 // We only loop over the rootLayer. Since we actually should have a layer selection
133 // list, no need to elaborate on that here and now
134 KisLayer* layer = img->rootLayer()->firstChild();
135 while (layer) {
136 KisPaintLayer* paint = 0;
137 if (layer->visible() && (paint = dynamic_cast<KisPaintLayer*>(layer)))
138 devices.at(0).push_back(paint->paintDevice());
139 layer = layer->nextSibling();
140 }
141 TQValueVector<KisPipeBrushParasite::SelectionMode> modes;
142
143 switch(comboBox2->currentItem()) {
144 case 0: modes.push_back(KisPipeBrushParasite::Constant); break;
145 case 1: modes.push_back(KisPipeBrushParasite::Random); break;
146 case 2: modes.push_back(KisPipeBrushParasite::Incremental); break;
147 case 3: modes.push_back(KisPipeBrushParasite::Pressure); break;
148 case 4: modes.push_back(KisPipeBrushParasite::Angular); break;
149 default: modes.push_back(KisPipeBrushParasite::Incremental);
150 }
151
152 m_brush = new KisImagePipeBrush(img->name(), w, h, devices, modes);
153 if (colorAsMask->isChecked())
154 m_brush->makeMaskImage();
155}
156
157
158#include "kis_custom_brush.moc"
virtual KisLayerSP nextSibling() const
Returns the next sibling of this layer in the parent's list.