chalk

kis_custom_pattern.cpp
1/*
2 * Copyright (c) 2006 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 <tdeglobal.h>
26#include <kstandarddirs.h>
27#include <tdetempfile.h>
28
29#include "kis_view.h"
30#include "kis_image.h"
31#include "kis_layer.h"
32#include "kis_paint_device.h"
33#include "kis_pattern.h"
34#include "kis_custom_pattern.h"
35#include "kis_resource_mediator.h"
36#include "kis_resourceserver.h"
37#include "kis_paint_layer.h"
38
39KisCustomPattern::KisCustomPattern(TQWidget *parent, const char* name, const TQString& caption, KisView* view)
40 : KisWdgCustomPattern(parent, name), m_view(view)
41{
42 Q_ASSERT(m_view);
43 m_mediator = 0;
44 setCaption(caption);
45
46 m_pattern = 0;
47
48 preview->setScaledContents(true);
49
50 connect(addButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotAddPredefined()));
51 connect(patternButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotUsePattern()));
52 connect(exportButton, TQ_SIGNAL(pressed()), this, TQ_SLOT(slotExport()));
53}
54
55KisCustomPattern::~KisCustomPattern() {
56 delete m_pattern;
57}
58
59void KisCustomPattern::showEvent(TQShowEvent *) {
60 slotUpdateCurrentPattern(0);
61}
62
63void KisCustomPattern::slotUpdateCurrentPattern(int) {
64 delete m_pattern;
65 if (m_view->canvasSubject() && m_view->canvasSubject()->currentImg()) {
66 createPattern();
67 preview->setPixmap(TQPixmap(m_pattern->img()));
68 } else {
69 m_pattern = 0;
70 }
71}
72
73void KisCustomPattern::slotExport() {
74 ;
75}
76
77void KisCustomPattern::slotAddPredefined() {
78 if (!m_pattern)
79 return;
80
81 // Save in the directory that is likely to be: ~/.trinity/share/apps/chalk/patterns
82 // a unique file with this pattern name
83 TQString dir = TDEGlobal::dirs()->saveLocation("data", "chalk/patterns");
84 TQString extension;
85
86 KTempFile file(dir, ".pat");
87 file.close(); // If we don't, and pattern->save first, it might get truncated!
88
89 // Save it to that file
90 m_pattern->setFilename(file.name());
91
92 // Add it to the pattern server, so that it automatically gets to the mediators, and
93 // so to the other pattern choosers can pick it up, if they want to
94 if (m_server)
95 m_server->addResource(m_pattern->clone());
96}
97
98void KisCustomPattern::slotUsePattern() {
99 if (!m_pattern)
100 return;
101 KisPattern* copy = m_pattern->clone();
102
103 TQ_CHECK_PTR(copy);
104
105 emit(activatedResource(copy));
106}
107
108void KisCustomPattern::createPattern() {
109 KisImageSP img = m_view->canvasSubject()->currentImg();
110
111 if (!img)
112 return;
113
114 m_pattern = new KisPattern(img->mergedImage(), 0, 0, img->width(), img->height());
115}
116
117
118#include "kis_custom_pattern.moc"