Messagelib

mimeparttreeview.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "mimeparttreeview.h"
8#include "mimetreemodel.h"
9#include "settings/messageviewersettings.h"
10
11#include <KMime/Content>
12
13#include <KConfigGroup>
14#include <QHeaderView>
15
16using namespace MessageViewer;
17namespace
18{
19static const char myMimePartTreeViewConfigGroupName[] = "MimePartTree";
20}
21MimePartTreeView::MimePartTreeView(QWidget *parent)
22 : QTreeView(parent)
23 , mMimePartModel(new MimeTreeModel(this))
24{
25 setObjectName(QLatin1StringView("mMimePartTree"));
26
27 setModel(mMimePartModel);
29 setSelectionBehavior(QAbstractItemView::SelectRows);
30 setContextMenuPolicy(Qt::CustomContextMenu);
31 header()->setSectionResizeMode(QHeaderView::ResizeToContents);
32 connect(mMimePartModel, &MimeTreeModel::modelReset, this, &MimePartTreeView::expandAll);
33 restoreMimePartTreeConfig();
34 setDragEnabled(true);
35}
36
37MimePartTreeView::~MimePartTreeView()
38{
39 // root is either null or a modified tree that we need to clean up
40 clearModel();
41 saveMimePartTreeConfig();
42}
43
44MimeTreeModel *MimePartTreeView::mimePartModel() const
45{
46 return mMimePartModel;
47}
48
49void MimePartTreeView::restoreMimePartTreeConfig()
50{
51 KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myMimePartTreeViewConfigGroupName));
52 header()->restoreState(grp.readEntry("State", QByteArray()));
53}
54
55void MimePartTreeView::saveMimePartTreeConfig()
56{
57 KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myMimePartTreeViewConfigGroupName));
58 grp.writeEntry("State", header()->saveState());
59}
60
61void MimePartTreeView::clearModel()
62{
63 delete mMimePartModel->root();
64 mMimePartModel->setRoot(nullptr);
65}
66
67void MimePartTreeView::setRoot(KMime::Content *root)
68{
69 delete mMimePartModel->root();
70 mMimePartModel->setRoot(root);
71}
72
73KMime::Content::List MimePartTreeView::selectedContents() const
74{
75 KMime::Content::List contents;
76 QItemSelectionModel *selectModel = selectionModel();
77 const QModelIndexList selectedRows = selectModel->selectedRows();
78 contents.reserve(selectedRows.count());
79 for (const QModelIndex &index : selectedRows) {
80 auto content = static_cast<KMime::Content *>(index.internalPointer());
81 if (content) {
82 contents.append(content);
83 }
84 }
85 return contents;
86}
87
88#include "moc_mimeparttreeview.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
A model representing the mime part tree of a message.
QItemSelectionModel * selectionModel() const const
bool restoreState(const QByteArray &state)
QModelIndexList selectedRows(int column) const const
void append(QList< T > &&value)
void reserve(qsizetype size)
CustomContextMenu
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QHeaderView * header() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.