~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/paintops/libpaintop/kis_custom_brush_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
 
3
 *  Copyright (c) 2010 Lukáš Tvrdý <lukast.dev@gmail.com>
3
4
 *
4
5
 *  This program is free software; you can redistribute it and/or modify
5
6
 *  it under the terms of the GNU General Public License as published by
26
27
#include <QComboBox>
27
28
#include <QCheckBox>
28
29
 
 
30
#include <QDateTime>
 
31
 
29
32
#include <QPixmap>
30
33
#include <QShowEvent>
31
34
#include <kglobal.h>
32
35
#include <kstandarddirs.h>
33
 
#include <ktemporaryfile.h>
34
36
 
35
37
#include "kis_image.h"
36
38
#include "kis_layer.h"
41
43
#include "kis_brush_server.h"
42
44
#include "kis_paint_layer.h"
43
45
#include "kis_group_layer.h"
 
46
#include <kis_selection.h>
44
47
 
45
48
KisCustomBrushWidget::KisCustomBrushWidget(QWidget *parent, const QString& caption, KisImageWSP image)
46
49
        : KisWdgCustomBrush(parent)
52
55
 
53
56
    KoResourceServer<KisBrush>* rServer = KisBrushServer::instance()->brushServer();
54
57
    m_rServerAdapter = new KoResourceServerAdapter<KisBrush>(rServer);
55
 
 
 
58
    
 
59
    m_brush = 0;
 
60
    m_brushCreated = false;
 
61
    
56
62
    connect(addButton, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
57
63
    connect(brushButton, SIGNAL(pressed()), this, SLOT(slotUpdateCurrentBrush()));
58
 
    //    connect(exportButton, SIGNAL(pressed()), this, SLOT(slotExport()));
59
64
    connect(brushStyle, SIGNAL(activated(int)), this, SLOT(slotUpdateCurrentBrush(int)));
60
 
    connect(colorAsMask, SIGNAL(stateChanged(int)), this, SLOT(slotUpdateCurrentBrush(int)));
 
65
    connect(colorAsMask, SIGNAL(toggled(bool)), this, SLOT(slotUpdateUseColorAsMask(bool)));
 
66
    connect(spacingSlider, SIGNAL(valueChanged(qreal)), this, SLOT(slotUpdateSpacing(qreal)));
61
67
    slotUpdateCurrentBrush();
62
68
}
63
69
 
74
80
 
75
81
void KisCustomBrushWidget::showEvent(QShowEvent *)
76
82
{
77
 
    slotUpdateCurrentBrush(0);
 
83
    if (!m_brushCreated){
 
84
        slotUpdateCurrentBrush(0);
 
85
        m_brushCreated = true;
 
86
    }
78
87
}
79
88
 
80
89
void KisCustomBrushWidget::slotUpdateCurrentBrush(int)
81
90
{
82
91
    if (m_image) {
83
92
        createBrush();
84
 
        if (m_brush)
85
 
            preview->setPixmap(QPixmap::fromImage(m_brush->image()));
86
 
    }
87
 
    emit sigBrushChanged();
88
 
}
89
 
 
90
 
void KisCustomBrushWidget::slotExport()
91
 
{
92
 
    ;
93
 
}
 
93
        if (m_brush){
 
94
            preview->setPixmap(QPixmap::fromImage( m_brush->image() ));
 
95
        }
 
96
    }
 
97
    emit sigBrushChanged();
 
98
}
 
99
 
 
100
void KisCustomBrushWidget::slotUpdateSpacing(qreal spacing)
 
101
{
 
102
    if (m_brush) {
 
103
        m_brush->setSpacing(spacing);
 
104
    }
 
105
    emit sigBrushChanged();
 
106
}
 
107
 
 
108
void KisCustomBrushWidget::slotUpdateUseColorAsMask(bool useColorAsMask)
 
109
{
 
110
    if (m_brush){
 
111
        static_cast<KisGbrBrush*>( m_brush.data() )->setUseColorAsMask( useColorAsMask );
 
112
        preview->setPixmap(QPixmap::fromImage( m_brush->image() ));
 
113
    }
 
114
    emit sigBrushChanged();
 
115
}
 
116
 
94
117
 
95
118
void KisCustomBrushWidget::slotAddPredefined()
96
119
{
105
128
        extension = ".gih";
106
129
    }
107
130
 
 
131
    QString name = nameLineEdit->text();
108
132
    QString tempFileName;
109
133
    {
110
 
        KTemporaryFile file;
111
 
        file.setPrefix(dir);
112
 
        file.setSuffix(extension);
113
 
        file.setAutoRemove(false);
114
 
        file.open();
115
 
        tempFileName = file.fileName();
 
134
        QFileInfo fileInfo;
 
135
        fileInfo.setFile(dir + name + extension);
 
136
 
 
137
        int i = 1;
 
138
        while (fileInfo.exists()) {
 
139
            fileInfo.setFile(dir + name + QString("%1").arg(i) + extension);
 
140
            i++;
 
141
        }
 
142
 
 
143
        tempFileName = fileInfo.filePath();
116
144
    }
117
145
 
118
 
    // Save it to that file
119
 
    m_brush->setFilename(tempFileName);
120
 
    m_brush->setValid(true);
121
 
 
122
146
    // Add it to the brush server, so that it automatically gets to the mediators, and
123
147
    // so to the other brush choosers can pick it up, if they want to
124
 
    if (m_rServerAdapter)
125
 
        m_rServerAdapter->addResource(static_cast<KisGbrBrush*>(m_brush.data())->clone());
 
148
    if (m_rServerAdapter) {
 
149
        KisGbrBrush * resource = static_cast<KisGbrBrush*>( m_brush.data() )->clone();
 
150
        resource->setFilename(tempFileName);
 
151
        
 
152
        if (nameLineEdit->text().isEmpty()){
 
153
            resource->setName(QDateTime::currentDateTime().toString("yyyy-MM-ddThh:mm"));
 
154
        }else{
 
155
            resource->setName( name );
 
156
        }
 
157
 
 
158
        if (colorAsMask->isChecked()){
 
159
            resource->makeMaskImage();
 
160
        }
 
161
    
 
162
        m_rServerAdapter->addResource( resource );
 
163
    }
126
164
}
127
165
 
128
166
void KisCustomBrushWidget::createBrush()
130
168
    if (!m_image)
131
169
        return;
132
170
 
 
171
    if (m_brush){
 
172
        // don't delete shared pointer, please
 
173
        bool removedCorrectly = KisBrushServer::instance()->brushServer()->removeResourceFromServer(  m_brush.data(), false );
 
174
        if (!removedCorrectly){
 
175
            kWarning() << "Brush was not removed correctly for the resource server";
 
176
        }
 
177
    }
 
178
    
133
179
    if (brushStyle->currentIndex() == 0) {
134
 
        m_brush = new KisGbrBrush(m_image->mergedImage().data(), 0, 0, m_image->width(), m_image->height());
135
 
        if (colorAsMask->isChecked())
136
 
            static_cast<KisGbrBrush*>(m_brush.data())->makeMaskImage();
137
 
        return;
138
 
    }
139
 
 
140
 
    // For each layer in the current image, create a new image, and add it to the list
141
 
    QVector< QVector<KisPaintDevice*> > devices;
142
 
    devices.push_back(QVector<KisPaintDevice*>());
143
 
    int w = m_image->width();
144
 
    int h = m_image->height();
145
 
 
146
 
    // We only loop over the rootLayer. Since we actually should have a layer selection
147
 
    // list, no need to elaborate on that here and now
148
 
    KisLayer* layer = dynamic_cast<KisLayer*>(m_image->rootLayer()->firstChild().data());
149
 
    while (layer) {
150
 
        KisPaintLayer* paint = 0;
151
 
        if (layer->visible() && (paint = dynamic_cast<KisPaintLayer*>(layer)))
152
 
            devices[0].push_back(paint->paintDevice().data());
153
 
        layer = dynamic_cast<KisLayer*>(layer->nextSibling().data());
154
 
    }
155
 
    QVector<KisParasite::SelectionMode> modes;
156
 
 
157
 
    switch (comboBox2->currentIndex()) {
158
 
    case 0: modes.push_back(KisParasite::Constant); break;
159
 
    case 1: modes.push_back(KisParasite::Random); break;
160
 
    case 2: modes.push_back(KisParasite::Incremental); break;
161
 
    case 3: modes.push_back(KisParasite::Pressure); break;
162
 
    case 4: modes.push_back(KisParasite::Angular); break;
163
 
    default: modes.push_back(KisParasite::Incremental);
164
 
    }
165
 
 
166
 
    m_brush = new KisImagePipeBrush(m_image->objectName(), w, h, devices, modes);
167
 
    if (colorAsMask->isChecked())
168
 
        static_cast<KisGbrBrush*>(m_brush.data())->makeMaskImage();
 
180
        KisSelectionSP selection = m_image->globalSelection();
 
181
        // create copy of the data
 
182
        m_image->lock();
 
183
        KisPaintDeviceSP dev = new KisPaintDevice(*m_image->mergedImage());
 
184
        m_image->unlock();
 
185
        
 
186
        if (!selection){
 
187
            m_brush = new KisGbrBrush(dev, 0, 0, m_image->width(), m_image->height());
 
188
        }else{
 
189
            dev->applySelectionMask(selection);
 
190
            QRect rc = dev->exactBounds();
 
191
            m_brush = new KisGbrBrush(dev, rc.x(), rc.y(), rc.width(), rc.height());
 
192
        }
 
193
    
 
194
    } else {
 
195
        // For each layer in the current image, create a new image, and add it to the list
 
196
        QVector< QVector<KisPaintDevice*> > devices;
 
197
        devices.push_back(QVector<KisPaintDevice*>());
 
198
        int w = m_image->width();
 
199
        int h = m_image->height();
 
200
 
 
201
        // We only loop over the rootLayer. Since we actually should have a layer selection
 
202
        // list, no need to elaborate on that here and now
 
203
        KisLayer* layer = dynamic_cast<KisLayer*>(m_image->rootLayer()->firstChild().data());
 
204
        while (layer) {
 
205
            KisPaintLayer* paint = 0;
 
206
            if (layer->visible() && (paint = dynamic_cast<KisPaintLayer*>(layer)))
 
207
                devices[0].push_back(paint->paintDevice().data());
 
208
            layer = dynamic_cast<KisLayer*>(layer->nextSibling().data());
 
209
        }
 
210
        QVector<KisParasite::SelectionMode> modes;
 
211
 
 
212
        switch (comboBox2->currentIndex()) {
 
213
        case 0: modes.push_back(KisParasite::Constant); break;
 
214
        case 1: modes.push_back(KisParasite::Random); break;
 
215
        case 2: modes.push_back(KisParasite::Incremental); break;
 
216
        case 3: modes.push_back(KisParasite::Pressure); break;
 
217
        case 4: modes.push_back(KisParasite::Angular); break;
 
218
        default: modes.push_back(KisParasite::Incremental);
 
219
        }
 
220
 
 
221
        m_brush = new KisImagePipeBrush(m_image->objectName(), w, h, devices, modes);
 
222
    }
 
223
 
 
224
    static_cast<KisGbrBrush*>( m_brush.data() )->setUseColorAsMask( colorAsMask->isChecked() );
 
225
    m_brush->setSpacing(spacingSlider->value());
 
226
    m_brush->setFilename(TEMPORARY_FILENAME);
 
227
    m_brush->setName(TEMPORARY_BRUSH_NAME);
 
228
    m_brush->setValid(true);
 
229
    
 
230
    KisBrushServer::instance()->brushServer()->addResource( m_brush.data() , false);
169
231
}
170
232
 
171
233
void KisCustomBrushWidget::setImage(KisImageWSP image)