backport 1145339: no error box spam
[kdepim.git] / kmail / attachmentview.cpp
blob1f696e8b11c125a609a778ebb33ec2fcd3949701
1 /*
2 * This file is part of KMail.
3 * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
5 * Parts based on KMail code by:
6 * Copyright (c) 2003 Ingo Kloecker <kloecker@kde.org>
7 * Copyright (c) 2007 Thomas McGuire <Thomas.McGuire@gmx.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "attachmentview.h"
26 #include "messagecomposer/attachmentmodel.h"
28 #include <QContextMenuEvent>
29 #include <QHeaderView>
30 #include <QKeyEvent>
31 #include <QSortFilterProxyModel>
33 #include <KDebug>
35 #include <messagecore/attachmentpart.h>
36 #include <boost/shared_ptr.hpp>
37 using KPIM::AttachmentPart;
39 using namespace KMail;
41 class KMail::AttachmentView::Private
43 public:
44 Message::AttachmentModel *model;
47 AttachmentView::AttachmentView( Message::AttachmentModel *model, QWidget *parent )
48 : QTreeView( parent )
49 , d( new Private )
51 d->model = model;
52 connect( model, SIGNAL(encryptEnabled(bool)), this, SLOT(setEncryptEnabled(bool)) );
53 connect( model, SIGNAL(signEnabled(bool)), this, SLOT(setSignEnabled(bool)) );
55 QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
56 sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
57 sortModel->setSourceModel( model );
58 setModel( sortModel );
59 connect( sortModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
60 connect( sortModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
61 connect( sortModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(selectNewAttachment()) );
63 setRootIsDecorated( false );
64 setUniformRowHeights( true );
65 setSelectionMode( QAbstractItemView::ExtendedSelection );
66 setDragDropMode( QAbstractItemView::DragDrop );
67 setDropIndicatorShown( false );
68 setSortingEnabled( true );
70 header()->setResizeMode( QHeaderView::Interactive );
71 header()->setResizeMode( Message::AttachmentModel::MimeTypeColumn, QHeaderView::Stretch );
72 header()->setStretchLastSection( false );
73 setColumnWidth( 0, 200 );
76 AttachmentView::~AttachmentView()
78 delete d;
81 void AttachmentView::contextMenuEvent( QContextMenuEvent *event )
83 Q_UNUSED( event );
84 emit contextMenuRequested();
87 void AttachmentView::keyPressEvent( QKeyEvent *event )
89 if( event->key() == Qt::Key_Delete ) {
90 // Indexes are based on row numbers, and row numbers change when items are deleted.
91 // Therefore, first we need to make a list of AttachmentParts to delete.
92 AttachmentPart::List toRemove;
93 foreach( const QModelIndex &index, selectionModel()->selectedRows() ) {
94 AttachmentPart::Ptr part = model()->data(
95 index, Message::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
96 toRemove.append( part );
98 foreach( const AttachmentPart::Ptr &part, toRemove ) {
99 d->model->removeAttachment( part );
104 void AttachmentView::dragEnterEvent( QDragEnterEvent *event )
106 if( event->source() == this ) {
107 // Ignore drags from ourselves.
108 event->ignore();
109 } else {
110 QTreeView::dragEnterEvent( event );
114 void AttachmentView::setEncryptEnabled( bool enabled )
116 setColumnHidden( Message::AttachmentModel::EncryptColumn, !enabled );
119 void AttachmentView::setSignEnabled( bool enabled )
121 setColumnHidden( Message::AttachmentModel::SignColumn, !enabled );
124 void AttachmentView::hideIfEmpty()
126 setVisible( model()->rowCount() > 0 );
129 void AttachmentView::selectNewAttachment()
131 if ( selectionModel()->selectedRows().isEmpty() ) {
132 selectionModel()->select( selectionModel()->currentIndex(),
133 QItemSelectionModel::Select | QItemSelectionModel::Rows );
137 void AttachmentView::startDrag( Qt::DropActions supportedActions )
139 Q_UNUSED( supportedActions );
141 const QModelIndexList selection = selectionModel()->selectedRows();
142 if( !selection.isEmpty() ) {
143 QMimeData *mimeData = model()->mimeData( selection );
144 QDrag *drag = new QDrag( this );
145 drag->setMimeData( mimeData );
146 drag->exec( Qt::CopyAction );
150 #include "attachmentview.moc"