akonadi
itemview.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "itemview.h"
00022
00023 #include "control.h"
00024 #include "itemmodel.h"
00025
00026 #include <KXMLGUIFactory>
00027 #include <KXmlGuiWindow>
00028
00029 #include <QtGui/QContextMenuEvent>
00030 #include <QtGui/QHeaderView>
00031 #include <QtGui/QMenu>
00032
00033 using namespace Akonadi;
00034
00038 class ItemView::Private
00039 {
00040 public:
00041 Private( ItemView *parent ) :
00042 xmlGuiWindow( 0 ),
00043 mParent( parent )
00044 {
00045 }
00046
00047 void init();
00048 void itemActivated( const QModelIndex& );
00049 void itemCurrentChanged( const QModelIndex& );
00050 void itemClicked( const QModelIndex& );
00051 void itemDoubleClicked( const QModelIndex& );
00052
00053 Item itemForIndex( const QModelIndex& );
00054
00055 KXmlGuiWindow *xmlGuiWindow;
00056
00057 private:
00058 ItemView *mParent;
00059 };
00060
00061 void ItemView::Private::init()
00062 {
00063 mParent->setRootIsDecorated( false );
00064
00065 mParent->header()->setClickable( true );
00066 mParent->header()->setStretchLastSection( true );
00067
00068 mParent->connect( mParent, SIGNAL( activated( const QModelIndex& ) ),
00069 mParent, SLOT( itemActivated( const QModelIndex& ) ) );
00070 mParent->connect( mParent, SIGNAL( clicked( const QModelIndex& ) ),
00071 mParent, SLOT( itemClicked( const QModelIndex& ) ) );
00072 mParent->connect( mParent, SIGNAL( doubleClicked( const QModelIndex& ) ),
00073 mParent, SLOT( itemDoubleClicked( const QModelIndex& ) ) );
00074
00075 Control::widgetNeedsAkonadi( mParent );
00076 }
00077
00078 Item ItemView::Private::itemForIndex( const QModelIndex &index )
00079 {
00080 if ( !index.isValid() )
00081 return Item();
00082
00083 const Item::Id currentItem = index.sibling( index.row(), ItemModel::Id ).data( ItemModel::IdRole ).toLongLong();
00084 if ( currentItem <= 0 )
00085 return Item();
00086
00087 const QString remoteId = index.sibling( index.row(), ItemModel::RemoteId ).data( ItemModel::IdRole ).toString();
00088 const QString mimeType = index.sibling( index.row(), ItemModel::MimeType ).data( ItemModel::MimeTypeRole ).toString();
00089
00090 Item item( currentItem );
00091 item.setRemoteId( remoteId );
00092 item.setMimeType( mimeType );
00093
00094 return item;
00095 }
00096
00097 void ItemView::Private::itemActivated( const QModelIndex &index )
00098 {
00099 const Item item = itemForIndex( index );
00100
00101 if ( !item.isValid() )
00102 return;
00103
00104 emit mParent->activated( item );
00105 }
00106
00107 void ItemView::Private::itemCurrentChanged( const QModelIndex &index )
00108 {
00109 const Item item = itemForIndex( index );
00110
00111 if ( !item.isValid() )
00112 return;
00113
00114 emit mParent->currentChanged( item );
00115 }
00116
00117 void ItemView::Private::itemClicked( const QModelIndex &index )
00118 {
00119 const Item item = itemForIndex( index );
00120
00121 if ( !item.isValid() )
00122 return;
00123
00124 emit mParent->clicked( item );
00125 }
00126
00127 void ItemView::Private::itemDoubleClicked( const QModelIndex &index )
00128 {
00129 const Item item = itemForIndex( index );
00130
00131 if ( !item.isValid() )
00132 return;
00133
00134 emit mParent->doubleClicked( item );
00135 }
00136
00137 ItemView::ItemView( QWidget * parent ) :
00138 QTreeView( parent ),
00139 d( new Private( this ) )
00140 {
00141 d->init();
00142 }
00143
00144 ItemView::ItemView(KXmlGuiWindow * xmlGuiWindow, QWidget * parent) :
00145 QTreeView( parent ),
00146 d( new Private( this ) )
00147 {
00148 d->xmlGuiWindow = xmlGuiWindow;
00149 d->init();
00150 }
00151
00152 ItemView::~ItemView()
00153 {
00154 delete d;
00155 }
00156
00157 void ItemView::setModel( QAbstractItemModel * model )
00158 {
00159 QTreeView::setModel( model );
00160
00161 connect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00162 this, SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00163 }
00164
00165 void ItemView::contextMenuEvent(QContextMenuEvent * event)
00166 {
00167 if ( !d->xmlGuiWindow )
00168 return;
00169 QMenu *popup = static_cast<QMenu*>( d->xmlGuiWindow->guiFactory()->container(
00170 QLatin1String("akonadi_itemview_contextmenu"), d->xmlGuiWindow ) );
00171 if ( popup )
00172 popup->exec( event->globalPos() );
00173 }
00174
00175 void ItemView::setXmlGuiWindow(KXmlGuiWindow * xmlGuiWindow)
00176 {
00177 d->xmlGuiWindow = xmlGuiWindow;
00178 }
00179
00180 #include "itemview.moc"