krita

kis_custom_palette.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 <qlineedit.h>
00022 #include <qimage.h>
00023 #include <qpushbutton.h>
00024 #include <qregexp.h>
00025 #include <qvalidator.h>
00026 
00027 #include <kglobal.h>
00028 #include <kstandarddirs.h>
00029 #include <ktempfile.h>
00030 #include <kcolordialog.h>
00031 #include <kinputdialog.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 
00035 #include "kis_view.h"
00036 #include "kis_palette.h"
00037 #include "kis_palette_view.h"
00038 #include "kis_custom_palette.h"
00039 #include "kis_resource_mediator.h"
00040 #include "kis_resourceserver.h"
00041 
00042 KisCustomPalette::KisCustomPalette(QWidget *parent, const char* name, const QString& caption, KisView* view)
00043     : KisWdgCustomPalette(parent, name), m_view(view)
00044 {
00045     Q_ASSERT(m_view);
00046     m_mediator = 0;
00047     m_server = 0;
00048     m_editMode = false;
00049     setCaption(caption);
00050 
00051     m_palette = new KisPalette();
00052     m_ownPalette = true;
00053     this->view->setPalette(m_palette);
00054 
00055     connect(addColor, SIGNAL(pressed()), this, SLOT(slotAddNew()));
00056     connect(removeColor, SIGNAL(pressed()), this, SLOT(slotRemoveCurrent()));
00057     connect(addPalette, SIGNAL(pressed()), this, SLOT(slotAddPredefined()));
00058 }
00059 
00060 KisCustomPalette::~KisCustomPalette() {
00061     if (m_ownPalette)
00062         delete m_palette;
00063 }
00064 
00065 void KisCustomPalette::setPalette(KisPalette* p) {
00066     if (m_ownPalette)
00067         delete m_palette;
00068     m_ownPalette = false;
00069     m_palette = p;
00070     view->setPalette(m_palette);
00071 }
00072 
00073 void KisCustomPalette::setEditMode(bool b) {
00074     m_editMode = b;
00075 
00076     if (m_editMode) {
00077         addPalette->setText(i18n("Save changes"));
00078     } else {
00079         addPalette->setText(i18n("Add to Predefined Palettes"));
00080     }
00081 }
00082 
00083 void KisCustomPalette::slotAddNew() {
00084     // Let the user select a new color
00085     // FIXME also let him add the current paint color to the palette
00086     // or even better, let the color picker have an option 'Add to palette'!
00087 
00088     QColor color;
00089     int result = KColorDialog::getColor(color);
00090     if (result != KColorDialog::Accepted)
00091         return;
00092 
00093     bool ok;
00094     QRegExpValidator validator(QRegExp(".*"), this);
00095     QString name = KInputDialog::getText(i18n("Add Color to Palette"),
00096                                          i18n("Color name (optional):"),
00097                                          QString::null, &ok,
00098                                          0, 0, &validator);
00099     if (!ok)
00100         return;
00101 
00102     KisPaletteEntry entry;
00103     entry.color = color;
00104     entry.name = name;
00105 
00106     m_palette->add(entry);
00107 
00108     // Just reload the palette completely for the view updating
00109     view->setPalette(m_palette);
00110 }
00111 
00112 void KisCustomPalette::slotRemoveCurrent() {
00113     m_palette->remove(view->currentEntry());
00114     // Just reload the palette completely for the view updating
00115     view->setPalette(m_palette);
00116 }
00117 
00118 void KisCustomPalette::slotAddPredefined() {
00119     m_palette->setName(palettename->text());
00120 
00121     if (!m_editMode) {
00122         // Save in the directory that is likely to be: ~/.kde/share/apps/krita/palettes
00123         // a unique file with this palettename
00124         QString dir = KGlobal::dirs()->saveLocation("data", "krita/palettes");
00125         QString extension;
00126 
00127         extension = ".gpl";
00128         KTempFile file(dir, extension);
00129         file.close(); // If we don't, and palette->save first, it might get truncated!
00130 
00131         // Save it to that file 
00132         m_palette->setFilename(file.name());
00133     } else {
00134         // The filename is already set
00135     }
00136 
00137     if (!m_palette->save()) {
00138         KMessageBox::error(0, i18n("Cannot write to palette file %1. Maybe it is read-only.")
00139                                    .arg(m_palette->filename()), i18n("Palette"));
00140         return;
00141     }
00142 
00143     // Add it to the palette server, so that it automatically gets to the mediators, and
00144     // so to the other choosers can pick it up, if they want to
00145     // This probably leaks!
00146     if (m_server)
00147         m_server->addResource(new KisPalette(*m_palette));
00148 }
00149 
00150 
00151 #include "kis_custom_palette.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys