28 #include "messagecomposer/attachment/attachmentmodel.h"
31 #include <QContextMenuEvent>
32 #include <QHeaderView>
34 #include <QSortFilterProxyModel>
35 #include <QToolButton>
36 #include <QHBoxLayout>
40 #include <KConfigGroup>
45 #include <messagecore/attachment/attachmentpart.h>
46 #include <boost/shared_ptr.hpp>
47 using MessageCore::AttachmentPart;
49 using namespace KMail;
51 class KMail::AttachmentView::Private
58 QHBoxLayout *lay =
new QHBoxLayout;
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);
72 MessageComposer::AttachmentModel *model;
73 QToolButton *toolButton;
81 , d( new Private(this) )
84 connect( model, SIGNAL(encryptEnabled(
bool)),
this, SLOT(
setEncryptEnabled(
bool)) );
85 connect( model, SIGNAL(signEnabled(
bool)),
this, SLOT(
setSignEnabled(
bool)) );
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()) );
96 setRootIsDecorated(
false );
97 setUniformRowHeights(
true );
98 setSelectionMode( QAbstractItemView::ExtendedSelection );
99 setDragDropMode( QAbstractItemView::DragDrop );
100 setDropIndicatorShown(
false );
101 setSortingEnabled(
true );
103 header()->setResizeMode( QHeaderView::Interactive );
104 header()->setStretchLastSection(
false );
105 restoreHeaderState();
106 setColumnWidth( 0, 200 );
115 void AttachmentView::restoreHeaderState()
118 header()->restoreState( grp.readEntry(
"State", QByteArray() ) );
121 void AttachmentView::saveHeaderState()
124 grp.writeEntry(
"State", header()->
saveState() );
136 if( event->key() == Qt::Key_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 );
145 foreach(
const AttachmentPart::Ptr &part, toRemove ) {
146 d->model->removeAttachment( part );
153 if( event->source() == this ) {
157 QTreeView::dragEnterEvent( event );
163 setColumnHidden( MessageComposer::AttachmentModel::EncryptColumn, !enabled );
168 setColumnHidden( MessageComposer::AttachmentModel::SignColumn, !enabled );
173 const bool needToShowIt = (model()->rowCount() > 0);
174 setVisible( needToShowIt );
175 d->toolButton->setChecked( needToShowIt );
176 widget()->setVisible( needToShowIt );
180 d->infoAttachment->clear();
187 MessageCore::AttachmentPart::List list = d->model->attachments();
189 Q_FOREACH(MessageCore::AttachmentPart::Ptr part, list) {
190 size += part->size();
192 d->infoAttachment->setText(i18np(
"1 attachment (%2)",
"%1 attachments (%2)",model()->rowCount(), KGlobal::locale()->formatByteSize(qMax( 0LL, size ))));
197 if ( selectionModel()->selectedRows().isEmpty() ) {
198 selectionModel()->select( selectionModel()->currentIndex(),
199 QItemSelectionModel::Select | QItemSelectionModel::Rows );
205 Q_UNUSED( supportedActions );
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 );
221 void AttachmentView::slotShowHideAttchementList(
bool show)
225 d->toolButton->setToolTip(i18n(
"Hide attachment list"));
227 d->toolButton->setToolTip(i18n(
"Show attachment list"));
231 #include "attachmentview.moc"
KDEPIM_EXPORT void saveState(QWidget *widget, KConfigGroup &config)
void keyPressEvent(QKeyEvent *event)
void selectNewAttachment()
void contextMenuRequested()
static KMKernel * self()
normal control stuff
void startDrag(Qt::DropActions supportedActions)
reimpl to avoid default drag cursor
void updateAttachmentLabel()
void setEncryptEnabled(bool enabled)
model sets these
AttachmentView(MessageComposer::AttachmentModel *model, QWidget *parent=0)
can't change model afterwards.
void dragEnterEvent(QDragEnterEvent *event)
reimpl to avoid drags from ourselves
void setSignEnabled(bool enabled)
void contextMenuEvent(QContextMenuEvent *event)