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