Oops fix logic
[kdepim.git] / ktnef / ktnefview.cpp
blob96cad3378d4fc36dc99b84bbd6c8daaa8bda9d3d
1 /*
2 This file is part of KTnef.
4 Copyright (C) 2002 Michael Goffioul <kdeprint@swing.be>
5 Copyright (c) 2012 Allen Winter <winter@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software Foundation,
14 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #include "ktnefview.h"
18 #include "attachpropertydialog.h"
20 #include <KTNEF/KTNEFAttach>
22 #include <KLocalizedString>
24 #include <QIcon>
26 #include <QPixmap>
27 #include <QTimer>
28 #include <QMimeDatabase>
29 #include <QMimeType>
31 class Attachment : public QTreeWidgetItem
33 public:
34 Attachment(QTreeWidget *parent, KTNEFAttach *attach);
35 ~Attachment();
37 KTNEFAttach *getAttachment() const
39 return mAttach;
42 private:
43 KTNEFAttach *mAttach;
46 Attachment::Attachment(QTreeWidget *parent, KTNEFAttach *attach)
47 : QTreeWidgetItem(parent, QStringList(attach->name())), mAttach(attach)
49 setText(2, QString::number(mAttach->size()));
50 if (!mAttach->fileName().isEmpty()) {
51 setText(0, mAttach->fileName());
54 QMimeDatabase db;
55 QMimeType mimeType = db.mimeTypeForName(mAttach->mimeTag());
56 setText(1, mimeType.comment());
58 QPixmap pix = AttachPropertyDialog::loadRenderingPixmap(attach, qApp->palette().color(QPalette::Background));
59 if (!pix.isNull()) {
60 setIcon(0, pix);
61 } else {
62 setIcon(0, QIcon::fromTheme(mimeType.iconName()));
66 Attachment::~Attachment()
70 //----------------------------------------------------------------------------//
72 KTNEFView::KTNEFView(QWidget *parent)
73 : QTreeWidget(parent)
75 const QStringList headerLabels =
76 (QStringList(i18nc("@title:column file name", "File Name"))
77 << i18nc("@title:column file type", "File Type")
78 << i18nc("@title:column file size", "Size"));
79 setHeaderLabels(headerLabels);
80 setSelectionMode(QAbstractItemView::ExtendedSelection);
81 setDragEnabled(true);
82 setSortingEnabled(true);
83 QTimer::singleShot(0, this, &KTNEFView::adjustColumnWidth);
86 KTNEFView::~KTNEFView()
90 void KTNEFView::setAttachments(const QList<KTNEFAttach *> &list)
92 clear();
93 if (!list.isEmpty()) {
94 QList<KTNEFAttach *>::ConstIterator it;
95 QList<KTNEFAttach *>::ConstIterator end(list.constEnd());
96 for (it = list.constBegin(); it != end; ++it) {
97 new Attachment(this, (*it));
102 void KTNEFView::resizeEvent(QResizeEvent *e)
104 adjustColumnWidth();
105 resize(width(), height());
106 if (e) {
107 QTreeWidget::resizeEvent(e);
111 QList<KTNEFAttach *> KTNEFView::getSelection()
113 mAttachments.clear();
115 QList<QTreeWidgetItem *> list = selectedItems();
116 if (list.isEmpty() || !list.first()) {
117 return mAttachments;
120 QList<QTreeWidgetItem *>::const_iterator it;
121 QList<QTreeWidgetItem *>::const_iterator end(list.constEnd());
122 for (it = list.constBegin(); it != end; ++it) {
123 Attachment *a = static_cast<Attachment *>(*it);
124 mAttachments.append(a->getAttachment());
126 return mAttachments;
129 void KTNEFView::startDrag(Qt::DropActions dropAction)
131 Q_UNUSED(dropAction);
133 QTreeWidgetItemIterator it(this, QTreeWidgetItemIterator::Selected);
134 QList<KTNEFAttach *> list;
135 while (*it) {
136 Attachment *a = static_cast<Attachment *>(*it);
137 list << a->getAttachment();
138 ++it;
140 if (!list.isEmpty()) {
141 Q_EMIT dragRequested(list);
145 void KTNEFView::adjustColumnWidth()
147 const int w = width() / 2;
148 setColumnWidth(0, w);
149 setColumnWidth(1, w / 2);
150 setColumnWidth(2, w / 2);