krita

kis_layerlist.cc

00001 /*
00002   Copyright (c) 2005 Gábor Lehel <illissius@gmail.com>
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
00008 
00009   This library 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 GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public License
00015   along with this library; see the file COPYING.LIB.  If not, write to
00016   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017   Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kaboutdata.h>
00021 #include <kglobal.h>
00022 #include <kiconloader.h>
00023 #include <klocale.h>
00024 #include <kpopupmenu.h>
00025 #include <KoPartSelectAction.h>
00026 #include <qimage.h>
00027 
00028 #include "kis_layer.h"
00029 #include "kis_paint_layer.h"
00030 #include "kis_part_layer.h"
00031 #include "kis_adjustment_layer.h"
00032 #include "kis_filter.h"
00033 #include "kis_filter_configuration.h"
00034 #include "kis_filter_registry.h"
00035 #include "kis_layerlist.h"
00036 
00037 
00038 KisLayerList::KisLayerList( QWidget *parent, const char *name )
00039     : super( parent, name )
00040 {
00041     m_partLayerAction = new KoPartSelectAction( i18n( "New &Object Layer" ), "gear", this );
00042 }
00043 
00044 static const int ADJUSTMENT_LAYER = 5384; //hack?
00045 
00046 void KisLayerList::constructMenu( LayerItem *layer )
00047 {
00048     super::constructMenu( layer );
00049 
00050     contextMenu()->removeItem( MenuItems::NewLayer );
00051     contextMenu()->removeItem( MenuItems::NewFolder );
00052     contextMenu()->changeItem( MenuItems::RemoveLayer, i18n( "&Remove Layer" ) );
00053 
00054     if( layer )
00055     {
00056         static KPopupMenu submenu;
00057         submenu.clear();
00058         submenu.insertItem( SmallIconSet( "file" ), i18n( "&Layer..." ), MenuItems::NewLayer );
00059         submenu.insertItem( SmallIconSet( "folder" ), i18n( "&Group Layer..." ), MenuItems::NewFolder );
00060         submenu.insertItem( SmallIconSet( "tool_filter" ), i18n( "&Adjustment Layer..." ), ADJUSTMENT_LAYER );
00061         m_partLayerAction->setText( i18n( "&Object Layer" ) );
00062         m_partLayerAction->plug( &submenu );
00063 
00064         contextMenu()->insertItem( SmallIconSet( "filenew" ), i18n( "&New" ), &submenu );
00065     }
00066     else
00067     {
00068         contextMenu()->insertItem( SmallIconSet( "filenew" ), i18n( "&New Layer..." ), MenuItems::NewLayer );
00069         contextMenu()->insertItem( SmallIconSet( "folder" ), i18n( "New &Group Layer..." ), MenuItems::NewFolder );
00070         contextMenu()->insertItem( SmallIconSet( "tool_filter" ), i18n( "New &Adjustment Layer..." ), ADJUSTMENT_LAYER );
00071         m_partLayerAction->setText( i18n( "New &Object Layer" ) );
00072         m_partLayerAction->plug( contextMenu() );
00073     }
00074 }
00075 
00076 void KisLayerList::menuActivated( int id, LayerItem *layer )
00077 {
00078     const QValueList<LayerItem*> selected = selectedLayers();
00079     LayerItem *parent = ( layer && layer->isFolder() ) ? layer : 0;
00080     LayerItem *after = 0;
00081     if( layer && !parent )
00082     {
00083         parent = layer->parent();
00084         after = layer->prevSibling();
00085     }
00086     switch( id )
00087     {
00088         case MenuItems::NewLayer:
00089             emit requestNewLayer( parent, after );
00090             emit requestNewLayer( parent ? parent->id() : -1, after ? after->id() : -1 );
00091             break;
00092         case MenuItems::NewFolder:
00093             emit requestNewFolder( parent, after );
00094             emit requestNewFolder( parent ? parent->id() : -1, after ? after->id() : -1 );
00095             break;
00096         case ADJUSTMENT_LAYER:
00097             emit requestNewAdjustmentLayer( parent, after );
00098             emit requestNewAdjustmentLayer( parent ? parent->id() : -1, after ? after->id() : -1 );
00099             break;
00100         case MenuItems::RemoveLayer:
00101             {
00102                 QValueList<int> ids;
00103                 for( int i = 0, n = selected.count(); i < n; ++i )
00104                 {
00105                     ids.append( selected[i]->id() );
00106                     emit requestRemoveLayer( selected[i]->id() );
00107                 }
00108                 emit requestRemoveLayers( ids );
00109             }
00110             for( int i = 0, n = selected.count(); i < n; ++i )
00111                 emit requestRemoveLayer( selected[i] );
00112             emit requestRemoveLayers( selected );
00113             break;
00114         case MenuItems::LayerProperties:
00115             if( layer )
00116             {
00117                 emit requestLayerProperties( layer );
00118                 emit requestLayerProperties( layer->id() );
00119             }
00120             break;
00121         default:
00122             if( id >= MenuItems::COUNT && layer )
00123                 super::menuActivated( id, layer );
00124             else if( id != -1 ) //object layer was selected
00125             {
00126                 emit requestNewObjectLayer( parent, after, m_partLayerAction->documentEntry() );
00127                 emit requestNewObjectLayer( parent ? parent->id() : -1, after ? after->id() : -1, m_partLayerAction->documentEntry() );
00128             }
00129     }
00130 }
00131 
00132 KisLayerItem::KisLayerItem( LayerList* parent, KisLayer* layer )
00133     : super( layer->name(),
00134              parent,
00135              layer->prevSibling() ? parent->layer( layer->prevSibling()->id() ) : 0,
00136              layer->id() )
00137     , m_layer( layer )
00138 {
00139     init();
00140 }
00141 
00142 KisLayerItem::KisLayerItem( LayerItem* parent, KisLayer* layer )
00143     : super( layer->name(),
00144              parent,
00145              layer->prevSibling() ? parent->listView()->layer( layer->prevSibling()->id() ) : 0,
00146              layer->id() )
00147     , m_layer( layer )
00148 {
00149     init();
00150 }
00151 
00152 void KisLayerItem::init()
00153 {
00154     setPreviewImage( &m_preview );
00155     sync();
00156 }
00157 
00158 KisLayer* KisLayerItem::layer() const
00159 {
00160     return m_layer;
00161 }
00162 
00163 void KisLayerItem::sync()
00164 {
00165     setProperty( "visible", layer()->visible() );
00166     setProperty( "locked", layer()->locked() );
00167     setDisplayName( layer()->name() );
00168     update();
00169 }
00170 
00171 bool KisLayerItem::updatePreview()
00172 {
00173     m_preview = m_layer->createThumbnail( height()*2, height()*2 );
00174     m_preview.setAlphaBuffer( true );
00175     previewChanged();
00176     return !m_preview.isNull();
00177 }
00178 
00179 QString KisLayerItem::tooltip() const
00180 {
00181     QString text = super::tooltip();
00182     text = text.left( text.length() - 8 ); //HACK -- strip the </table>
00183     QString row = "<tr><td>%1</td><td>%2</td></tr>";
00184     text += row.arg( i18n( "Opacity:" ) ).arg( "%1%" ).arg( int( float( m_layer->opacity() * 100 ) / 255 + 0.5 ) );
00185     text += row.arg( i18n( "Composite mode:" ) ).arg( m_layer->compositeOp().id().name() );
00186     if( KisPaintLayer *player = dynamic_cast<KisPaintLayer*>( m_layer ) )
00187     {
00188         text += row.arg( i18n( "Colorspace:" ) ).arg( player->paintDevice()->colorSpace()->id().name() );
00189         if( KisProfile *profile = player->paintDevice()->colorSpace()->getProfile() )
00190             text += row.arg( i18n( "Profile:" ) ).arg( profile->productName() );
00191     }
00192     if( KisAdjustmentLayer *alayer = dynamic_cast<KisAdjustmentLayer*>( m_layer ) )
00193         text += row.arg( i18n( "Filter: " ) ).arg( KisFilterRegistry::instance()->get( alayer->filter()->name() )->id().name() );
00194     if( KisPartLayerImpl *player = dynamic_cast<KisPartLayerImpl*>( m_layer ) ) {
00195         QString type = player->docType();
00196 
00197         if( type.isEmpty() ) {
00198             type = player->childDoc()->document()->instance()->aboutData()->programName();
00199         }
00200 
00201         text += row.arg( i18n( "Document type: " ) ).arg( type );
00202     }
00203     text += "</table>";
00204 
00205     return text;
00206 }
00207 
00208 QImage KisLayerItem::tooltipPreview() const
00209 {
00210     QImage img = m_layer->createThumbnail( 400, 400 );
00211     if( img.isNull() )
00212         return img; //so Qt doesn't complain
00213     img.setAlphaBuffer( true );
00214     const int size = kMin( 200, kMax( img.width(), img.height() ) );
00215     return img.smoothScale( size, size, QImage::ScaleMin );
00216 }
00217 
00218 //void KisLayerItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int align );
00219 
00220 #include "kis_layerlist.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys