• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • editor
  • attachment
attachmentview.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KMail.
3  * Copyright (c) 2011-2015 Laurent Montel <montel@kde.org>
4  *
5  * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
6  *
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>
10  *
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.
15  *
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.
20  *
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.
24  */
25 
26 #include "attachmentview.h"
27 
28 #include "messagecomposer/attachment/attachmentmodel.h"
29 #include "kmkernel.h"
30 
31 #include <QContextMenuEvent>
32 #include <QHeaderView>
33 #include <QKeyEvent>
34 #include <QSortFilterProxyModel>
35 #include <QToolButton>
36 #include <QHBoxLayout>
37 #include <QLabel>
38 
39 #include <KDebug>
40 #include <KConfigGroup>
41 #include <KIcon>
42 #include <KLocalizedString>
43 #include <KGlobal>
44 #include <KLocale>
45 
46 #include <messagecore/attachment/attachmentpart.h>
47 #ifndef Q_MOC_RUN
48 #include <boost/shared_ptr.hpp>
49 #endif
50 using MessageCore::AttachmentPart;
51 
52 using namespace KMail;
53 
54 class KMail::AttachmentView::Private
55 {
56 public:
57  Private(AttachmentView *qq)
58  : model(0), q(qq)
59  {
60  widget = new QWidget();
61  QHBoxLayout *lay = new QHBoxLayout;
62  lay->setMargin(0);
63  widget->setLayout(lay);
64  toolButton = new QToolButton;
65  connect(toolButton,SIGNAL(toggled(bool)),q,SLOT(slotShowHideAttchementList(bool)));
66  toolButton->setIcon(KIcon(QLatin1String( "mail-attachment" )));
67  toolButton->setAutoRaise(true);
68  toolButton->setCheckable(true);
69  lay->addWidget(toolButton);
70  infoAttachment = new QLabel;
71  infoAttachment->setMargin(0);
72  lay->addWidget(infoAttachment);
73  }
74 
75  MessageComposer::AttachmentModel *model;
76  QToolButton *toolButton;
77  QLabel *infoAttachment;
78  QWidget *widget;
79  AttachmentView *q;
80 };
81 
82 AttachmentView::AttachmentView( MessageComposer::AttachmentModel *model, QWidget *parent )
83  : QTreeView( parent )
84  , d( new Private(this) )
85 {
86  d->model = model;
87  connect( model, SIGNAL(encryptEnabled(bool)), this, SLOT(setEncryptEnabled(bool)) );
88  connect( model, SIGNAL(signEnabled(bool)), this, SLOT(setSignEnabled(bool)) );
89 
90  QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
91  sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
92  sortModel->setSourceModel( model );
93  setModel( sortModel );
94  connect( model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
95  connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
96  connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(selectNewAttachment()) );
97  connect( model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(updateAttachmentLabel()) );
98 
99  setRootIsDecorated( false );
100  setUniformRowHeights( true );
101  setSelectionMode( QAbstractItemView::ExtendedSelection );
102  setDragDropMode( QAbstractItemView::DragDrop );
103  setDropIndicatorShown( false );
104  setSortingEnabled( true );
105 
106  header()->setResizeMode( QHeaderView::Interactive );
107  header()->setStretchLastSection( false );
108  restoreHeaderState();
109  setColumnWidth( 0, 200 );
110 }
111 
112 AttachmentView::~AttachmentView()
113 {
114  saveHeaderState();
115  delete d;
116 }
117 
118 void AttachmentView::restoreHeaderState()
119 {
120  KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
121  header()->restoreState( grp.readEntry( "State", QByteArray() ) );
122 }
123 
124 void AttachmentView::saveHeaderState()
125 {
126  KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
127  grp.writeEntry( "State", header()->saveState() );
128  grp.sync();
129 }
130 
131 void AttachmentView::contextMenuEvent( QContextMenuEvent *event )
132 {
133  Q_UNUSED( event );
134  emit contextMenuRequested();
135 }
136 
137 void AttachmentView::keyPressEvent( QKeyEvent *event )
138 {
139  if( event->key() == Qt::Key_Delete ) {
140  // Indexes are based on row numbers, and row numbers change when items are deleted.
141  // Therefore, first we need to make a list of AttachmentParts to delete.
142  AttachmentPart::List toRemove;
143  foreach( const QModelIndex &index, selectionModel()->selectedRows() ) {
144  AttachmentPart::Ptr part = model()->data(
145  index, MessageComposer::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
146  toRemove.append( part );
147  }
148  foreach( const AttachmentPart::Ptr &part, toRemove ) {
149  d->model->removeAttachment( part );
150  }
151  }
152 }
153 
154 void AttachmentView::dragEnterEvent( QDragEnterEvent *event )
155 {
156  if( event->source() == this ) {
157  // Ignore drags from ourselves.
158  event->ignore();
159  } else {
160  QTreeView::dragEnterEvent( event );
161  }
162 }
163 
164 void AttachmentView::setEncryptEnabled( bool enabled )
165 {
166  setColumnHidden( MessageComposer::AttachmentModel::EncryptColumn, !enabled );
167 }
168 
169 void AttachmentView::setSignEnabled( bool enabled )
170 {
171  setColumnHidden( MessageComposer::AttachmentModel::SignColumn, !enabled );
172 }
173 
174 void AttachmentView::hideIfEmpty()
175 {
176  const bool needToShowIt = (model()->rowCount() > 0);
177  setVisible( needToShowIt );
178  d->toolButton->setChecked( needToShowIt );
179  widget()->setVisible( needToShowIt );
180  if (needToShowIt) {
181  updateAttachmentLabel();
182  } else {
183  d->infoAttachment->clear();
184  }
185  emit modified(true);
186 }
187 
188 void AttachmentView::updateAttachmentLabel()
189 {
190  MessageCore::AttachmentPart::List list = d->model->attachments();
191  qint64 size = 0;
192  Q_FOREACH(MessageCore::AttachmentPart::Ptr part, list) {
193  size += part->size();
194  }
195  d->infoAttachment->setText(i18np("1 attachment (%2)", "%1 attachments (%2)",model()->rowCount(), KGlobal::locale()->formatByteSize(qMax( 0LL, size ))));
196 }
197 
198 void AttachmentView::selectNewAttachment()
199 {
200  if ( selectionModel()->selectedRows().isEmpty() ) {
201  selectionModel()->select( selectionModel()->currentIndex(),
202  QItemSelectionModel::Select | QItemSelectionModel::Rows );
203  }
204 }
205 
206 void AttachmentView::startDrag( Qt::DropActions supportedActions )
207 {
208  Q_UNUSED( supportedActions );
209 
210  const QModelIndexList selection = selectionModel()->selectedRows();
211  if( !selection.isEmpty() ) {
212  QMimeData *mimeData = model()->mimeData( selection );
213  QDrag *drag = new QDrag( this );
214  drag->setMimeData( mimeData );
215  drag->exec( Qt::CopyAction );
216  }
217 }
218 
219 QWidget *AttachmentView::widget() const
220 {
221  return d->widget;
222 }
223 
224 void AttachmentView::slotShowHideAttchementList(bool show)
225 {
226  setVisible(show);
227  if(show) {
228  d->toolButton->setToolTip(i18n("Hide attachment list"));
229  } else {
230  d->toolButton->setToolTip(i18n("Show attachment list"));
231  }
232 }
233 
saveState
KDEPIM_EXPORT void saveState(QWidget *widget, KConfigGroup &config)
QModelIndex
QDropEvent::source
QWidget * source() const
KMail::AttachmentView::keyPressEvent
void keyPressEvent(QKeyEvent *event)
Definition: attachmentview.cpp:137
QWidget
QAbstractItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const =0
KMail::AttachmentView
Definition: attachmentview.h:38
QAbstractItemView::setSelectionMode
void setSelectionMode(QAbstractItemView::SelectionMode mode)
KMail::AttachmentView::selectNewAttachment
void selectNewAttachment()
Definition: attachmentview.cpp:198
QDrag::setMimeData
void setMimeData(QMimeData *data)
QByteArray
QAbstractItemView::selectionModel
QItemSelectionModel * selectionModel() const
QTreeView::dataChanged
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QSortFilterProxyModel::setSourceModel
virtual void setSourceModel(QAbstractItemModel *sourceModel)
QHeaderView::restoreState
bool restoreState(const QByteArray &state)
QAbstractItemView::setDragDropMode
void setDragDropMode(DragDropMode behavior)
QWidget::setVisible
virtual void setVisible(bool visible)
QHBoxLayout
QVariant::value
T value() const
attachmentview.h
QTreeView::setUniformRowHeights
void setUniformRowHeights(bool uniform)
QTreeView::rowsRemoved
void rowsRemoved(const QModelIndex &parent, int start, int end)
KMail::AttachmentView::contextMenuRequested
void contextMenuRequested()
QMimeData
KMKernel::self
static KMKernel * self()
normal control stuff
Definition: kmkernel.cpp:1471
QDrag::exec
Qt::DropAction exec(QFlags< Qt::DropAction > supportedActions)
QWidget::size
QSize size() const
QTreeView::setColumnWidth
void setColumnWidth(int column, int width)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QWidget::setLayout
void setLayout(QLayout *layout)
KMail::AttachmentView::widget
QWidget * widget() const
Definition: attachmentview.cpp:219
QContextMenuEvent
KMail::AttachmentView::modified
void modified(bool)
QTreeView::rowsInserted
virtual void rowsInserted(const QModelIndex &parent, int start, int end)
QDrag
KMail::AttachmentView::startDrag
void startDrag(Qt::DropActions supportedActions)
reimpl to avoid default drag cursor
Definition: attachmentview.cpp:206
QItemSelectionModel::select
virtual void select(const QModelIndex &index, QFlags< QItemSelectionModel::SelectionFlag > command)
QItemSelectionModel::selectedRows
QModelIndexList selectedRows(int column) const
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QAbstractItemView::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
QAbstractItemModel::mimeData
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
QLayout::setMargin
void setMargin(int margin)
KMail::AttachmentView::updateAttachmentLabel
void updateAttachmentLabel()
Definition: attachmentview.cpp:188
QToolButton
QKeyEvent::key
int key() const
QTreeView::setColumnHidden
void setColumnHidden(int column, bool hide)
KMail::AttachmentView::setEncryptEnabled
void setEncryptEnabled(bool enabled)
model sets these
Definition: attachmentview.cpp:164
KMail::AttachmentView::AttachmentView
AttachmentView(MessageComposer::AttachmentModel *model, QWidget *parent=0)
can't change model afterwards.
Definition: attachmentview.cpp:82
QSortFilterProxyModel
KMail::AttachmentView::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event)
reimpl to avoid drags from ourselves
Definition: attachmentview.cpp:154
KMail::AttachmentView::setSignEnabled
void setSignEnabled(bool enabled)
Definition: attachmentview.cpp:169
QKeyEvent
QTreeView::setSortingEnabled
void setSortingEnabled(bool enable)
kmkernel.h
QSortFilterProxyModel::setSortCaseSensitivity
void setSortCaseSensitivity(Qt::CaseSensitivity cs)
QDragEnterEvent
QLatin1String
QTreeView
Qt::DropActions
typedef DropActions
QTreeView::setModel
virtual void setModel(QAbstractItemModel *model)
QHeaderView::setResizeMode
void setResizeMode(ResizeMode mode)
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QTreeView::header
QHeaderView * header() const
QAbstractItemView::model
QAbstractItemModel * model() const
QAbstractItemView::currentIndex
QModelIndex currentIndex() const
QTreeView::setRootIsDecorated
void setRootIsDecorated(bool show)
QHeaderView::setStretchLastSection
void setStretchLastSection(bool stretch)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QLabel::setMargin
void setMargin(int)
KMail::AttachmentView::~AttachmentView
~AttachmentView()
Definition: attachmentview.cpp:112
KMail::AttachmentView::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *event)
Definition: attachmentview.cpp:131
KMail::AttachmentView::hideIfEmpty
void hideIfEmpty()
Definition: attachmentview.cpp:174
QAbstractItemView::setDropIndicatorShown
void setDropIndicatorShown(bool enable)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal