krita

kis_custom_pattern.cc

00001 /*
00002  *  Copyright (c) 2006 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 <kglobal.h>
00026 #include <kstandarddirs.h>
00027 #include <ktempfile.h>
00028 
00029 #include "kis_view.h"
00030 #include "kis_image.h"
00031 #include "kis_layer.h"
00032 #include "kis_paint_device.h"
00033 #include "kis_pattern.h"
00034 #include "kis_custom_pattern.h"
00035 #include "kis_resource_mediator.h"
00036 #include "kis_resourceserver.h"
00037 #include "kis_paint_layer.h"
00038 
00039 KisCustomPattern::KisCustomPattern(QWidget *parent, const char* name, const QString& caption, KisView* view)
00040     : KisWdgCustomPattern(parent, name), m_view(view)
00041 {
00042     Q_ASSERT(m_view);
00043     m_mediator = 0;
00044     setCaption(caption);
00045 
00046     m_pattern = 0;
00047 
00048     preview->setScaledContents(true);
00049 
00050     connect(addButton, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
00051     connect(patternButton, SIGNAL(pressed()), this, SLOT(slotUsePattern()));
00052     connect(exportButton, SIGNAL(pressed()), this, SLOT(slotExport()));
00053 }
00054 
00055 KisCustomPattern::~KisCustomPattern() {
00056     delete m_pattern;
00057 }
00058 
00059 void KisCustomPattern::showEvent(QShowEvent *) {
00060     slotUpdateCurrentPattern(0);
00061 }
00062 
00063 void KisCustomPattern::slotUpdateCurrentPattern(int) {
00064     delete m_pattern;
00065     if (m_view->canvasSubject() && m_view->canvasSubject()->currentImg()) {
00066         createPattern();
00067         preview->setPixmap(QPixmap(m_pattern->img()));
00068     } else {
00069         m_pattern = 0;
00070     }
00071 }
00072 
00073 void KisCustomPattern::slotExport() {
00074     ;
00075 }
00076 
00077 void KisCustomPattern::slotAddPredefined() {
00078     if (!m_pattern)
00079         return;
00080 
00081     // Save in the directory that is likely to be: ~/.kde/share/apps/krita/patterns
00082     // a unique file with this pattern name
00083     QString dir = KGlobal::dirs()->saveLocation("data", "krita/patterns");
00084     QString extension;
00085 
00086     KTempFile file(dir, ".pat");
00087     file.close(); // If we don't, and pattern->save first, it might get truncated!
00088 
00089     // Save it to that file 
00090     m_pattern->setFilename(file.name());
00091 
00092     // Add it to the pattern server, so that it automatically gets to the mediators, and
00093     // so to the other pattern choosers can pick it up, if they want to
00094     if (m_server)
00095         m_server->addResource(m_pattern->clone());
00096 }
00097 
00098 void KisCustomPattern::slotUsePattern() {
00099     if (!m_pattern)
00100         return;
00101     KisPattern* copy = m_pattern->clone();
00102 
00103     Q_CHECK_PTR(copy);
00104 
00105     emit(activatedResource(copy));
00106 }
00107 
00108 void KisCustomPattern::createPattern() {
00109     KisImageSP img = m_view->canvasSubject()->currentImg();
00110 
00111     if (!img)
00112         return;
00113 
00114     m_pattern = new KisPattern(img->mergedImage(), 0, 0, img->width(), img->height());
00115 }
00116 
00117 
00118 #include "kis_custom_pattern.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys