Better wording
[kdepim.git] / kmail / attachmentview.cpp
blobc374547c2adfafedd4e689524536841e97442931
1 /*
2 * This file is part of KMail.
3 * Copyright (c) 2011 Laurent Montel <montel@kde.org>
4 *
5 * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
7 * Parts based on KMail code by:
8 * Copyright (c) 2003 Ingo Kloecker <kloecker@kde.org>
9 * Copyright (c) 2007 Thomas McGuire <Thomas.McGuire@gmx.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "attachmentview.h"
28 #include "messagecomposer/attachmentmodel.h"
29 #include "kmkernel.h"
31 #include <QContextMenuEvent>
32 #include <QHeaderView>
33 #include <QKeyEvent>
34 #include <QSortFilterProxyModel>
36 #include <KDebug>
37 #include <KConfigGroup>
39 #include <messagecore/attachmentpart.h>
40 #include <boost/shared_ptr.hpp>
41 using MessageCore::AttachmentPart;
43 using namespace KMail;
45 class KMail::AttachmentView::Private
47 public:
48 Message::AttachmentModel *model;
51 AttachmentView::AttachmentView( Message::AttachmentModel *model, QWidget *parent )
52 : QTreeView( parent )
53 , d( new Private )
55 d->model = model;
56 connect( model, SIGNAL(encryptEnabled(bool)), this, SLOT(setEncryptEnabled(bool)) );
57 connect( model, SIGNAL(signEnabled(bool)), this, SLOT(setSignEnabled(bool)) );
59 QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
60 sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
61 sortModel->setSourceModel( model );
62 setModel( sortModel );
63 connect( sortModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
64 connect( sortModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
65 connect( sortModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(selectNewAttachment()) );
67 setRootIsDecorated( false );
68 setUniformRowHeights( true );
69 setSelectionMode( QAbstractItemView::ExtendedSelection );
70 setDragDropMode( QAbstractItemView::DragDrop );
71 setDropIndicatorShown( false );
72 setSortingEnabled( true );
74 header()->setResizeMode( QHeaderView::Interactive );
75 header()->setStretchLastSection( false );
76 restoreHeaderState();
77 setColumnWidth( 0, 200 );
80 AttachmentView::~AttachmentView()
82 saveHeaderState();
83 delete d;
86 void AttachmentView::restoreHeaderState()
88 KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
89 header()->restoreState( grp.readEntry( "State", QByteArray() ) );
92 void AttachmentView::saveHeaderState()
94 KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
95 grp.writeEntry( "State", header()->saveState() );
96 grp.sync();
99 void AttachmentView::contextMenuEvent( QContextMenuEvent *event )
101 Q_UNUSED( event );
102 emit contextMenuRequested();
105 void AttachmentView::keyPressEvent( QKeyEvent *event )
107 if( event->key() == Qt::Key_Delete ) {
108 // Indexes are based on row numbers, and row numbers change when items are deleted.
109 // Therefore, first we need to make a list of AttachmentParts to delete.
110 AttachmentPart::List toRemove;
111 foreach( const QModelIndex &index, selectionModel()->selectedRows() ) {
112 AttachmentPart::Ptr part = model()->data(
113 index, Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
114 toRemove.append( part );
116 foreach( const AttachmentPart::Ptr &part, toRemove ) {
117 d->model->removeAttachment( part );
122 void AttachmentView::dragEnterEvent( QDragEnterEvent *event )
124 if( event->source() == this ) {
125 // Ignore drags from ourselves.
126 event->ignore();
127 } else {
128 QTreeView::dragEnterEvent( event );
132 void AttachmentView::setEncryptEnabled( bool enabled )
134 setColumnHidden( Message::AttachmentModel::EncryptColumn, !enabled );
137 void AttachmentView::setSignEnabled( bool enabled )
139 setColumnHidden( Message::AttachmentModel::SignColumn, !enabled );
142 void AttachmentView::hideIfEmpty()
144 setVisible( model()->rowCount() > 0 );
147 void AttachmentView::selectNewAttachment()
149 if ( selectionModel()->selectedRows().isEmpty() ) {
150 selectionModel()->select( selectionModel()->currentIndex(),
151 QItemSelectionModel::Select | QItemSelectionModel::Rows );
155 void AttachmentView::startDrag( Qt::DropActions supportedActions )
157 Q_UNUSED( supportedActions );
159 const QModelIndexList selection = selectionModel()->selectedRows();
160 if( !selection.isEmpty() ) {
161 QMimeData *mimeData = model()->mimeData( selection );
162 QDrag *drag = new QDrag( this );
163 drag->setMimeData( mimeData );
164 drag->exec( Qt::CopyAction );
168 #include "attachmentview.moc"