F:/KPlato/koffice/libs/kofficeui/KoDocumentSectionView.cpp

Aller à la documentation de ce fichier.
00001 /*
00002   Copyright (c) 2006 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 <QtDebug>
00021 #include <QContextMenuEvent>
00022 #include <QHeaderView>
00023 #include <QHelpEvent>
00024 #include <QMenu>
00025 #include <QMouseEvent>
00026 #include <QPersistentModelIndex>
00027 #include "KoDocumentSectionPropertyAction_p.h"
00028 #include "KoDocumentSectionDelegate.h"
00029 #include "KoDocumentSectionModel.h"
00030 #include "KoDocumentSectionView.h"
00031 
00032 class KoDocumentSectionView::Private
00033 {
00034     public:
00035         KoDocumentSectionDelegate *delegate;
00036         DisplayMode mode;
00037         QPersistentModelIndex hovered;
00038         Private(): delegate( 0 ), mode( DetailedMode ) { }
00039 };
00040 
00041 KoDocumentSectionView::KoDocumentSectionView( QWidget *parent )
00042     : QTreeView( parent )
00043     , d( new Private )
00044 {
00045     d->delegate = new KoDocumentSectionDelegate( this, this );
00046     setMouseTracking( true );
00047     setVerticalScrollMode( ScrollPerPixel );
00048     setSelectionMode( SingleSelection );
00049     setSelectionBehavior( SelectItems );
00050     header()->hide();
00051 }
00052 
00053 KoDocumentSectionView::~KoDocumentSectionView()
00054 {
00055     delete d;
00056 }
00057 
00058 void KoDocumentSectionView::setDisplayMode( DisplayMode mode )
00059 {
00060     if( d->mode != mode )
00061     {
00062         d->mode = mode;
00063         scheduleDelayedItemsLayout();
00064     }
00065 }
00066 
00067 KoDocumentSectionView::DisplayMode KoDocumentSectionView::displayMode() const
00068 {
00069     return d->mode;
00070 }
00071 
00072 void KoDocumentSectionView::addPropertyActions( QMenu *menu, const QModelIndex &index )
00073 {
00074     Model::PropertyList list = index.data( Model::PropertiesRole ).value<Model::PropertyList>();
00075     for( int i = 0, n = list.count(); i < n; ++i )
00076         if( list.at( i ).isMutable )
00077         {
00078             PropertyAction *a = new PropertyAction( i, list.at( i ), index, menu );
00079             connect( a, SIGNAL( toggled( bool, const QPersistentModelIndex&, int ) ),
00080                      this, SLOT( slotActionToggled( bool, const QPersistentModelIndex&, int ) ) );
00081             menu->addAction( a );
00082         }
00083 }
00084 
00085 bool KoDocumentSectionView::viewportEvent( QEvent *e )
00086 {
00087     if( model() )
00088     {
00089         switch( e->type() )
00090         {
00091             case QEvent::MouseButtonPress:
00092             {
00093                 const QPoint pos = static_cast<QMouseEvent*>( e )->pos();
00094                 if( !indexAt( pos ).isValid() )
00095                     return super::viewportEvent( e );
00096                 QModelIndex index = model()->buddy( indexAt( pos ) );
00097                 return d->delegate->editorEvent( e, model(), optionForIndex( index ), index );
00098             } break;
00099             case QEvent::Leave:
00100             {
00101                 QEvent e( QEvent::Leave );
00102                 d->delegate->editorEvent( &e, model(), optionForIndex( d->hovered ), d->hovered );
00103                 d->hovered = QModelIndex();
00104             } break;
00105             case QEvent::MouseMove:
00106             {
00107                 const QPoint pos = static_cast<QMouseEvent*>( e )->pos();
00108                 QModelIndex hovered = indexAt( pos );
00109                 if( hovered != d->hovered )
00110                 {
00111                     if( d->hovered.isValid() )
00112                     {
00113                         QEvent e( QEvent::Leave );
00114                         d->delegate->editorEvent( &e, model(), optionForIndex( d->hovered ), d->hovered );
00115                     }
00116                     if( hovered.isValid() )
00117                     {
00118                         QEvent e( QEvent::Enter );
00119                         d->delegate->editorEvent( &e, model(), optionForIndex( hovered ), hovered );
00120                     }
00121                     d->hovered = hovered;
00122                 }
00123             } break;
00124             case QEvent::ToolTip:
00125             {
00126                 const QPoint pos = static_cast<QHelpEvent*>( e )->pos();
00127                 if( !indexAt( pos ).isValid() )
00128                     return super::viewportEvent( e );
00129                 QModelIndex index = model()->buddy( indexAt( pos ) );
00130                 return d->delegate->editorEvent( e, model(), optionForIndex( index ), index );
00131             } break;
00132             case QEvent::Resize:
00133             {
00134                 scheduleDelayedItemsLayout();
00135             } break;
00136             default: break;
00137         }
00138     }
00139 
00140     return super::viewportEvent( e );
00141 }
00142 
00143 void KoDocumentSectionView::contextMenuEvent( QContextMenuEvent *e )
00144 {
00145     super::contextMenuEvent( e );
00146     QModelIndex i = indexAt( e->pos() );
00147     if( model() )
00148         i = model()->buddy( i );
00149     showContextMenu( e->globalPos(), i );
00150 }
00151 
00152 void KoDocumentSectionView::showContextMenu( const QPoint &globalPos, const QModelIndex &index )
00153 {
00154     emit contextMenuRequested( globalPos, index );
00155 }
00156 
00157 void KoDocumentSectionView::currentChanged( const QModelIndex &current, const QModelIndex &previous )
00158 {
00159     super::currentChanged( current, previous );
00160     if( current != previous ) //hack?
00161         const_cast<QAbstractItemModel*>( current.model() )->setData( current, true, Model::ActiveRole );
00162 }
00163 
00164 void KoDocumentSectionView::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight )
00165 {
00166     super::dataChanged( topLeft, bottomRight );
00167     for( int x = topLeft.row(); x <= bottomRight.row(); ++x )
00168         for( int y = topLeft.column(); y <= bottomRight.column(); ++y )
00169             if( topLeft.sibling( x, y ).data( Model::ActiveRole ).toBool() )
00170             {
00171                 setCurrentIndex( topLeft.sibling( x, y ) );
00172                 return;
00173             }
00174 }
00175 
00176 void KoDocumentSectionView::slotActionToggled( bool on, const QPersistentModelIndex &index, int num )
00177 {
00178     Model::PropertyList list = index.data( Model::PropertiesRole ).value<Model::PropertyList>();
00179     list[num].state = on;
00180     const_cast<QAbstractItemModel*>( index.model() )->setData( index, QVariant::fromValue( list ), Model::PropertiesRole );
00181 }
00182 
00183 QStyleOptionViewItem KoDocumentSectionView::optionForIndex( const QModelIndex &index ) const
00184 {
00185     QStyleOptionViewItem option = viewOptions();
00186     option.rect = visualRect( index );
00187     if( index == currentIndex() )
00188         option.state |= QStyle::State_HasFocus;
00189     return option;
00190 }
00191 
00192 #include "KoDocumentSectionPropertyAction_p.moc"
00193 #include "KoDocumentSectionView.moc"

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1