krita

kis_dlg_adjustment_layer.cc

00001 /*
00002  *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
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 #include <klocale.h>
00019 
00020 #include <qgroupbox.h>
00021 #include <qlabel.h>
00022 #include <qlayout.h>
00023 
00024 #include <klineedit.h>
00025 #include <klocale.h>
00026 
00027 #include "kis_filter_config_widget.h"
00028 #include "kis_transaction.h"
00029 #include "kis_filter.h"
00030 #include "kis_filter_configuration.h"
00031 #include "kis_dlg_adjustment_layer.h"
00032 #include "kis_filters_listview.h"
00033 #include "kis_image.h"
00034 #include "kis_previewwidget.h"
00035 #include "kis_layer.h"
00036 #include "kis_paint_device.h"
00037 #include "kis_paint_layer.h"
00038 #include "kis_group_layer.h"
00039 #include "kis_adjustment_layer.h"
00040 #include "kis_filter.h"
00041 #include "kis_filter_configuration.h"
00042 
00043 KisDlgAdjustmentLayer::KisDlgAdjustmentLayer(KisImage * img,
00044                                              const QString & /*layerName*/,
00045                                              const QString & caption,
00046                                              QWidget *parent,
00047                                              const char *name)
00048     : KDialogBase(parent, name, true, "", Ok | Cancel)
00049     , m_image(img)
00050     , m_currentFilter(0)
00051     , m_customName(false)
00052     , m_freezeName(false)
00053 {
00054     Q_ASSERT(img);
00055 
00056     KisLayerSP activeLayer = img->activeLayer();
00057     m_dev = 0;
00058 
00059     KisPaintLayer * pl = dynamic_cast<KisPaintLayer*>(activeLayer.data());
00060     if (pl) {
00061         m_dev = pl->paintDevice();
00062     }
00063     else {
00064         KisGroupLayer * gl = dynamic_cast<KisGroupLayer*>(activeLayer.data());
00065         if (gl) {
00066             m_dev = gl->projection(img->bounds());
00067         }
00068         else {
00069             KisAdjustmentLayer * al = dynamic_cast<KisAdjustmentLayer*>(activeLayer.data());
00070             if (al) {
00071                 m_dev = al->cachedPaintDevice();
00072             }
00073         }
00074     }
00075 
00076     setCaption(caption);
00077     QWidget * page = new QWidget(this, "page widget");
00078     QGridLayout * grid = new QGridLayout(page, 3, 2, 0, 6);
00079     setMainWidget(page);
00080 
00081     QLabel * lblName = new QLabel(i18n("Layer name:"), page, "lblName");
00082     grid->addWidget(lblName, 0, 0);
00083 
00084     m_layerName = new KLineEdit(page, "m_layerName");
00085     grid->addWidget(m_layerName, 0, 1);
00086     connect( m_layerName, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotNameChanged( const QString & ) ) );
00087 
00088     m_filtersList = new KisFiltersListView(m_dev,  page, true, "dlgadjustment.filtersList");
00089     connect(m_filtersList , SIGNAL(selectionChanged(QIconViewItem*)), this, SLOT(selectionHasChanged(QIconViewItem* )));
00090     grid->addMultiCellWidget(m_filtersList, 1, 2, 0, 0);
00091 
00092     m_preview = new KisPreviewWidget(page, "dlgadjustment.preview");
00093     m_preview->slotSetDevice( m_dev );
00094 
00095     connect( m_preview, SIGNAL(updated()), this, SLOT(refreshPreview()));
00096     grid->addWidget(m_preview, 1, 1);
00097 
00098     m_configWidgetHolder = new QGroupBox(i18n("Configuration"), page, "currentConfigWidget");
00099     m_configWidgetHolder->setColumnLayout(0, Qt::Horizontal);
00100     grid->addWidget(m_configWidgetHolder, 2, 1);
00101 
00102     m_labelNoConfigWidget = new QLabel(i18n("No configuration options are available for this filter"),
00103                                         m_configWidgetHolder);
00104     m_configWidgetHolder->layout()->add(m_labelNoConfigWidget);
00105     m_labelNoConfigWidget->hide();
00106 
00107     resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
00108 
00109     m_currentConfigWidget = 0;
00110 
00111     enableButtonOK(0);
00112 }
00113 
00114 void KisDlgAdjustmentLayer::slotNameChanged( const QString & text )
00115 {
00116     if (m_freezeName)
00117         return;
00118 
00119     m_customName = !text.isEmpty();
00120     enableButtonOK( m_currentFilter && m_customName );
00121 }
00122 
00123 KisFilterConfiguration * KisDlgAdjustmentLayer::filterConfiguration() const
00124 {
00125     return m_currentFilter->configuration(m_currentConfigWidget);
00126 }
00127 
00128 QString KisDlgAdjustmentLayer::layerName() const
00129 {
00130     return m_layerName->text();
00131 }
00132 
00133 void KisDlgAdjustmentLayer::slotConfigChanged()
00134 {
00135     if(m_preview->getAutoUpdate())
00136     {
00137         refreshPreview();
00138     } else {
00139         m_preview->needUpdate();
00140     }
00141 }
00142 
00143 void KisDlgAdjustmentLayer::refreshPreview()
00144 {
00145     KisPaintDeviceSP layer =  m_preview->getDevice();
00146 
00147     KisTransaction cmd("Temporary transaction", layer.data());
00148     KisFilterConfiguration* config = m_currentFilter->configuration(m_currentConfigWidget);
00149 
00150     QRect rect = layer->extent();
00151     m_currentFilter->process(layer.data(), layer.data(), config, rect);
00152     m_preview->slotUpdate();
00153     cmd.unexecute();
00154 }
00155 
00156 void KisDlgAdjustmentLayer::selectionHasChanged ( QIconViewItem * item )
00157 {
00158     KisFiltersIconViewItem* kisitem = (KisFiltersIconViewItem*) item;
00159 
00160     m_currentFilter = kisitem->filter();
00161 
00162     if ( m_currentConfigWidget != 0 )
00163     {
00164         m_configWidgetHolder->layout()->remove(m_currentConfigWidget);
00165 
00166         delete m_currentConfigWidget;
00167         m_currentConfigWidget = 0;
00168 
00169     } else {
00170 
00171         m_labelNoConfigWidget->hide();
00172     }
00173 
00174     if (m_dev) {
00175         m_currentConfigWidget = m_currentFilter->createConfigurationWidget(m_configWidgetHolder,
00176                                                                            m_dev);
00177     }
00178 
00179     if (m_currentConfigWidget != 0)
00180     {
00181         m_configWidgetHolder->layout()->add(m_currentConfigWidget);
00182         m_currentConfigWidget->show();
00183         connect(m_currentConfigWidget, SIGNAL(sigPleaseUpdatePreview()), this, SLOT(slotConfigChanged()));
00184     } else {
00185         m_labelNoConfigWidget->show();
00186     }
00187 
00188     if (!m_customName) {
00189         m_freezeName = true;
00190         m_layerName->setText(m_currentFilter->id().name());
00191         m_freezeName = false;
00192     }
00193 
00194     enableButtonOK( !m_layerName->text().isEmpty() );
00195     refreshPreview();
00196 }
00197 
00198 #include "kis_dlg_adjustment_layer.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys