akonadi
itemview.cpp
00001 /* 00002 Copyright (c) 2007 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 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 the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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 xmlGuiClient( 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 KXMLGUIClient *xmlGuiClient; 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(QModelIndex)), 00069 mParent, SLOT(itemActivated(QModelIndex)) ); 00070 mParent->connect( mParent, SIGNAL(clicked(QModelIndex)), 00071 mParent, SLOT(itemClicked(QModelIndex)) ); 00072 mParent->connect( mParent, SIGNAL(doubleClicked(QModelIndex)), 00073 mParent, SLOT(itemDoubleClicked(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->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow ); 00149 d->init(); 00150 } 00151 00152 ItemView::ItemView(KXMLGUIClient * xmlGuiClient, QWidget * parent) : 00153 QTreeView( parent ), 00154 d( new Private( this ) ) 00155 { 00156 d->xmlGuiClient = xmlGuiClient; 00157 d->init(); 00158 } 00159 00160 ItemView::~ItemView() 00161 { 00162 delete d; 00163 } 00164 00165 void ItemView::setModel( QAbstractItemModel * model ) 00166 { 00167 QTreeView::setModel( model ); 00168 00169 connect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), 00170 this, SLOT(itemCurrentChanged(QModelIndex)) ); 00171 } 00172 00173 void ItemView::contextMenuEvent(QContextMenuEvent * event) 00174 { 00175 if ( !d->xmlGuiClient ) 00176 return; 00177 QMenu *popup = static_cast<QMenu*>( d->xmlGuiClient->factory()->container( 00178 QLatin1String( "akonadi_itemview_contextmenu" ), d->xmlGuiClient ) ); 00179 if ( popup ) 00180 popup->exec( event->globalPos() ); 00181 } 00182 00183 void ItemView::setXmlGuiWindow(KXmlGuiWindow * xmlGuiWindow) 00184 { 00185 d->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow ); 00186 } 00187 00188 void ItemView::setXmlGuiClient(KXMLGUIClient * xmlGuiClient) 00189 { 00190 d->xmlGuiClient = xmlGuiClient; 00191 } 00192 00193 #include "itemview.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:23 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Aug 27 2012 22:09:23 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.