File indexing completed on 2024-12-22 05:00:57
0001 /* 0002 This file is part of KTnef. 0003 0004 SPDX-FileCopyrightText: 2002 Michael Goffioul <kdeprint@swing.be> 0005 SPDX-FileCopyrightText: 2012 Allen Winter <winter@kde.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 0009 You should have received a copy of the GNU General Public License 0010 along with this program; if not, write to the Free Software Foundation, 0011 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 0012 */ 0013 0014 #include "ktnefview.h" 0015 #include "attachpropertydialog.h" 0016 0017 #include <KTNEF/KTNEFAttach> 0018 0019 #include <KLocalizedString> 0020 0021 #include <QIcon> 0022 0023 #include <QMimeDatabase> 0024 #include <QMimeType> 0025 #include <QPixmap> 0026 #include <QTimer> 0027 0028 class Attachment : public QTreeWidgetItem 0029 { 0030 public: 0031 Attachment(QTreeWidget *parent, KTNEFAttach *attach); 0032 ~Attachment() override; 0033 0034 [[nodiscard]] KTNEFAttach *getAttachment() const 0035 { 0036 return mAttach; 0037 } 0038 0039 private: 0040 KTNEFAttach *const mAttach; 0041 }; 0042 0043 Attachment::Attachment(QTreeWidget *parent, KTNEFAttach *attach) 0044 : QTreeWidgetItem(parent, QStringList(attach->name())) 0045 , mAttach(attach) 0046 { 0047 setText(2, QString::number(mAttach->size())); 0048 if (!mAttach->fileName().isEmpty()) { 0049 setText(0, mAttach->fileName()); 0050 } 0051 0052 QMimeDatabase db; 0053 const QMimeType mimeType = db.mimeTypeForName(mAttach->mimeTag()); 0054 setText(1, mimeType.comment()); 0055 0056 QPixmap pix = AttachPropertyDialog::loadRenderingPixmap(attach, qApp->palette().color(QPalette::Window)); 0057 if (!pix.isNull()) { 0058 setIcon(0, pix); 0059 } else { 0060 setIcon(0, QIcon::fromTheme(mimeType.iconName())); 0061 } 0062 } 0063 0064 Attachment::~Attachment() = default; 0065 0066 //----------------------------------------------------------------------------// 0067 0068 KTNEFView::KTNEFView(QWidget *parent) 0069 : QTreeWidget(parent) 0070 { 0071 const QStringList headerLabels = (QStringList(i18nc("@title:column file name", "File Name")) 0072 << i18nc("@title:column file type", "File Type") << i18nc("@title:column file size", "Size")); 0073 setHeaderLabels(headerLabels); 0074 setSelectionMode(QAbstractItemView::ExtendedSelection); 0075 setDragEnabled(true); 0076 setSortingEnabled(true); 0077 QTimer::singleShot(0, this, &KTNEFView::adjustColumnWidth); 0078 } 0079 0080 KTNEFView::~KTNEFView() = default; 0081 0082 void KTNEFView::setAttachments(const QList<KTNEFAttach *> &list) 0083 { 0084 clear(); 0085 for (const auto &s : list) { 0086 new Attachment(this, s); 0087 } 0088 } 0089 0090 void KTNEFView::resizeEvent(QResizeEvent *e) 0091 { 0092 adjustColumnWidth(); 0093 resize(width(), height()); 0094 if (e) { 0095 QTreeWidget::resizeEvent(e); 0096 } 0097 } 0098 0099 QList<KTNEFAttach *> KTNEFView::getSelection() 0100 { 0101 mAttachments.clear(); 0102 0103 const QList<QTreeWidgetItem *> list = selectedItems(); 0104 if (list.isEmpty() || !list.first()) { 0105 return mAttachments; 0106 } 0107 0108 QList<QTreeWidgetItem *>::const_iterator it; 0109 QList<QTreeWidgetItem *>::const_iterator end(list.constEnd()); 0110 mAttachments.reserve(list.count()); 0111 for (it = list.constBegin(); it != end; ++it) { 0112 auto a = static_cast<Attachment *>(*it); 0113 mAttachments.append(a->getAttachment()); 0114 } 0115 return mAttachments; 0116 } 0117 0118 void KTNEFView::startDrag(Qt::DropActions dropAction) 0119 { 0120 Q_UNUSED(dropAction) 0121 0122 QTreeWidgetItemIterator it(this, QTreeWidgetItemIterator::Selected); 0123 QList<KTNEFAttach *> list; 0124 while (*it) { 0125 auto a = static_cast<Attachment *>(*it); 0126 list << a->getAttachment(); 0127 ++it; 0128 } 0129 if (!list.isEmpty()) { 0130 Q_EMIT dragRequested(list); 0131 } 0132 } 0133 0134 void KTNEFView::adjustColumnWidth() 0135 { 0136 const int w = width() / 2; 0137 setColumnWidth(0, w); 0138 setColumnWidth(1, w / 2); 0139 setColumnWidth(2, w / 2); 0140 } 0141 0142 #include "moc_ktnefview.cpp"