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
00051 KXmlGuiWindow *xmlGuiWindow;
00052
00053 private:
00054 ItemView *mParent;
00055 };
00056
00057 void ItemView::Private::init()
00058 {
00059 mParent->setRootIsDecorated( false );
00060
00061 mParent->header()->setClickable( true );
00062 mParent->header()->setStretchLastSection( true );
00063
00064 mParent->connect( mParent, SIGNAL( activated( const QModelIndex& ) ),
00065 mParent, SLOT( itemActivated( const QModelIndex& ) ) );
00066
00067 Control::widgetNeedsAkonadi( mParent );
00068 }
00069
00070 void ItemView::Private::itemActivated( const QModelIndex &index )
00071 {
00072 if ( !index.isValid() )
00073 return;
00074
00075 const Item::Id currentItem = index.sibling(index.row(),ItemModel::Id).data(ItemModel::IdRole).toLongLong();
00076 if ( currentItem <= 0 )
00077 return;
00078
00079 const QString remoteId = index.sibling(index.row(),ItemModel::RemoteId).data(ItemModel::IdRole).toString();
00080
00081 Item item( currentItem );
00082 item.setRemoteId( remoteId );
00083
00084 emit mParent->activated( item );
00085 }
00086
00087 void ItemView::Private::itemCurrentChanged( const QModelIndex &index )
00088 {
00089 if ( !index.isValid() )
00090 return;
00091
00092 const Item::Id currentItem = index.sibling(index.row(),ItemModel::Id).data(ItemModel::IdRole).toLongLong();
00093 if ( currentItem <= 0 )
00094 return;
00095
00096 const QString remoteId = index.sibling(index.row(),ItemModel::RemoteId).data(ItemModel::IdRole).toString();
00097
00098 Item item( currentItem );
00099 item.setRemoteId( remoteId );
00100
00101 emit mParent->currentChanged( item );
00102 }
00103
00104 ItemView::ItemView( QWidget * parent ) :
00105 QTreeView( parent ),
00106 d( new Private( this ) )
00107 {
00108 d->init();
00109 }
00110
00111 ItemView::ItemView(KXmlGuiWindow * xmlGuiWindow, QWidget * parent) :
00112 QTreeView( parent ),
00113 d( new Private( this ) )
00114 {
00115 d->xmlGuiWindow = xmlGuiWindow;
00116 d->init();
00117 }
00118
00119 ItemView::~ItemView()
00120 {
00121 delete d;
00122 }
00123
00124 void ItemView::setModel( QAbstractItemModel * model )
00125 {
00126 QTreeView::setModel( model );
00127
00128 connect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00129 this, SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00130 }
00131
00132 void ItemView::contextMenuEvent(QContextMenuEvent * event)
00133 {
00134 if ( !d->xmlGuiWindow )
00135 return;
00136 QMenu *popup = static_cast<QMenu*>( d->xmlGuiWindow->guiFactory()->container(
00137 QLatin1String("akonadi_itemview_contextmenu"), d->xmlGuiWindow ) );
00138 if ( popup )
00139 popup->exec( event->globalPos() );
00140 }
00141
00142 void ItemView::setXmlGuiWindow(KXmlGuiWindow * xmlGuiWindow)
00143 {
00144 d->xmlGuiWindow = xmlGuiWindow;
00145 }
00146
00147 #include "itemview.moc"