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

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • editor
attachmentview.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KMail.
3  * Copyright (c) 2011-2012 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 <KLocale>
43 #include <KGlobal>
44 
45 #include <messagecore/attachment/attachmentpart.h>
46 #include <boost/shared_ptr.hpp>
47 using MessageCore::AttachmentPart;
48 
49 using namespace KMail;
50 
51 class KMail::AttachmentView::Private
52 {
53 public:
54  Private(AttachmentView *qq)
55  : q(qq)
56  {
57  widget = new QWidget();
58  QHBoxLayout *lay = new QHBoxLayout;
59  lay->setMargin(0);
60  widget->setLayout(lay);
61  toolButton = new QToolButton;
62  connect(toolButton,SIGNAL(toggled(bool)),q,SLOT(slotShowHideAttchementList(bool)));
63  toolButton->setIcon(KIcon(QLatin1String( "mail-attachment" )));
64  toolButton->setAutoRaise(true);
65  toolButton->setCheckable(true);
66  lay->addWidget(toolButton);
67  infoAttachment = new QLabel;
68  infoAttachment->setMargin(0);
69  lay->addWidget(infoAttachment);
70  }
71 
72  MessageComposer::AttachmentModel *model;
73  QToolButton *toolButton;
74  QLabel *infoAttachment;
75  QWidget *widget;
76  AttachmentView *q;
77 };
78 
79 AttachmentView::AttachmentView( MessageComposer::AttachmentModel *model, QWidget *parent )
80  : QTreeView( parent )
81  , d( new Private(this) )
82 {
83  d->model = model;
84  connect( model, SIGNAL(encryptEnabled(bool)), this, SLOT(setEncryptEnabled(bool)) );
85  connect( model, SIGNAL(signEnabled(bool)), this, SLOT(setSignEnabled(bool)) );
86 
87  QSortFilterProxyModel *sortModel = new QSortFilterProxyModel( this );
88  sortModel->setSortCaseSensitivity( Qt::CaseInsensitive );
89  sortModel->setSourceModel( model );
90  setModel( sortModel );
91  connect( model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
92  connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(hideIfEmpty()) );
93  connect( model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(selectNewAttachment()) );
94  connect( model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(updateAttachmentLabel()) );
95 
96  setRootIsDecorated( false );
97  setUniformRowHeights( true );
98  setSelectionMode( QAbstractItemView::ExtendedSelection );
99  setDragDropMode( QAbstractItemView::DragDrop );
100  setDropIndicatorShown( false );
101  setSortingEnabled( true );
102 
103  header()->setResizeMode( QHeaderView::Interactive );
104  header()->setStretchLastSection( false );
105  restoreHeaderState();
106  setColumnWidth( 0, 200 );
107 }
108 
109 AttachmentView::~AttachmentView()
110 {
111  saveHeaderState();
112  delete d;
113 }
114 
115 void AttachmentView::restoreHeaderState()
116 {
117  KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
118  header()->restoreState( grp.readEntry( "State", QByteArray() ) );
119 }
120 
121 void AttachmentView::saveHeaderState()
122 {
123  KConfigGroup grp( KMKernel::self()->config(), "AttachmentView" );
124  grp.writeEntry( "State", header()->saveState() );
125  grp.sync();
126 }
127 
128 void AttachmentView::contextMenuEvent( QContextMenuEvent *event )
129 {
130  Q_UNUSED( event );
131  emit contextMenuRequested();
132 }
133 
134 void AttachmentView::keyPressEvent( QKeyEvent *event )
135 {
136  if( event->key() == Qt::Key_Delete ) {
137  // Indexes are based on row numbers, and row numbers change when items are deleted.
138  // Therefore, first we need to make a list of AttachmentParts to delete.
139  AttachmentPart::List toRemove;
140  foreach( const QModelIndex &index, selectionModel()->selectedRows() ) {
141  AttachmentPart::Ptr part = model()->data(
142  index, MessageComposer::AttachmentModel::AttachmentPartRole ).value<AttachmentPart::Ptr>();
143  toRemove.append( part );
144  }
145  foreach( const AttachmentPart::Ptr &part, toRemove ) {
146  d->model->removeAttachment( part );
147  }
148  }
149 }
150 
151 void AttachmentView::dragEnterEvent( QDragEnterEvent *event )
152 {
153  if( event->source() == this ) {
154  // Ignore drags from ourselves.
155  event->ignore();
156  } else {
157  QTreeView::dragEnterEvent( event );
158  }
159 }
160 
161 void AttachmentView::setEncryptEnabled( bool enabled )
162 {
163  setColumnHidden( MessageComposer::AttachmentModel::EncryptColumn, !enabled );
164 }
165 
166 void AttachmentView::setSignEnabled( bool enabled )
167 {
168  setColumnHidden( MessageComposer::AttachmentModel::SignColumn, !enabled );
169 }
170 
171 void AttachmentView::hideIfEmpty()
172 {
173  const bool needToShowIt = (model()->rowCount() > 0);
174  setVisible( needToShowIt );
175  d->toolButton->setChecked( needToShowIt );
176  widget()->setVisible( needToShowIt );
177  if (needToShowIt) {
178  updateAttachmentLabel();
179  } else {
180  d->infoAttachment->clear();
181  }
182  emit modified(true);
183 }
184 
185 void AttachmentView::updateAttachmentLabel()
186 {
187  MessageCore::AttachmentPart::List list = d->model->attachments();
188  qint64 size = 0;
189  Q_FOREACH(MessageCore::AttachmentPart::Ptr part, list) {
190  size += part->size();
191  }
192  d->infoAttachment->setText(i18np("1 attachment (%2)", "%1 attachments (%2)",model()->rowCount(), KGlobal::locale()->formatByteSize(qMax( 0LL, size ))));
193 }
194 
195 void AttachmentView::selectNewAttachment()
196 {
197  if ( selectionModel()->selectedRows().isEmpty() ) {
198  selectionModel()->select( selectionModel()->currentIndex(),
199  QItemSelectionModel::Select | QItemSelectionModel::Rows );
200  }
201 }
202 
203 void AttachmentView::startDrag( Qt::DropActions supportedActions )
204 {
205  Q_UNUSED( supportedActions );
206 
207  const QModelIndexList selection = selectionModel()->selectedRows();
208  if( !selection.isEmpty() ) {
209  QMimeData *mimeData = model()->mimeData( selection );
210  QDrag *drag = new QDrag( this );
211  drag->setMimeData( mimeData );
212  drag->exec( Qt::CopyAction );
213  }
214 }
215 
216 QWidget *AttachmentView::widget()
217 {
218  return d->widget;
219 }
220 
221 void AttachmentView::slotShowHideAttchementList(bool show)
222 {
223  setVisible(show);
224  if(show) {
225  d->toolButton->setToolTip(i18n("Hide attachment list"));
226  } else {
227  d->toolButton->setToolTip(i18n("Show attachment list"));
228  }
229 }
230 
231 #include "attachmentview.moc"
saveState
KDEPIM_EXPORT void saveState(QWidget *widget, KConfigGroup &config)
KMail::AttachmentView::keyPressEvent
void keyPressEvent(QKeyEvent *event)
Definition: attachmentview.cpp:134
KMail::AttachmentView
Definition: attachmentview.h:38
KMail::AttachmentView::selectNewAttachment
void selectNewAttachment()
Definition: attachmentview.cpp:195
QWidget
KMail::AttachmentView::widget
QWidget * widget()
Definition: attachmentview.cpp:216
attachmentview.h
KMail::AttachmentView::contextMenuRequested
void contextMenuRequested()
KMKernel::self
static KMKernel * self()
normal control stuff
Definition: kmkernel.cpp:1451
KMail::AttachmentView::modified
void modified(bool)
QTreeView
KMail::AttachmentView::startDrag
void startDrag(Qt::DropActions supportedActions)
reimpl to avoid default drag cursor
Definition: attachmentview.cpp:203
QMimeData
KMail::AttachmentView::updateAttachmentLabel
void updateAttachmentLabel()
Definition: attachmentview.cpp:185
KMail::AttachmentView::setEncryptEnabled
void setEncryptEnabled(bool enabled)
model sets these
Definition: attachmentview.cpp:161
KMail::AttachmentView::AttachmentView
AttachmentView(MessageComposer::AttachmentModel *model, QWidget *parent=0)
can't change model afterwards.
Definition: attachmentview.cpp:79
KMail::AttachmentView::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event)
reimpl to avoid drags from ourselves
Definition: attachmentview.cpp:151
QSortFilterProxyModel
KMail::AttachmentView::setSignEnabled
void setSignEnabled(bool enabled)
Definition: attachmentview.cpp:166
kmkernel.h
QLabel
KMail::AttachmentView::~AttachmentView
~AttachmentView()
Definition: attachmentview.cpp:109
KMail::AttachmentView::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *event)
Definition: attachmentview.cpp:128
KMail::AttachmentView::hideIfEmpty
void hideIfEmpty()
Definition: attachmentview.cpp:171
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:51 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

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