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 
16 using namespace MessageViewer;
17 namespace
18 {
19 static const char myMimePartTreeViewConfigGroupName[] = "MimePartTree";
20 }
21 MimePartTreeView::MimePartTreeView(QWidget *parent)
22  : QTreeView(parent)
23  , mMimePartModel(new MimeTreeModel(this))
24 {
25  setObjectName(QLatin1StringView("mMimePartTree"));
26 
27  setModel(mMimePartModel);
28  setSelectionMode(QAbstractItemView::ExtendedSelection);
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 
37 MimePartTreeView::~MimePartTreeView()
38 {
39  // root is either null or a modified tree that we need to clean up
40  clearModel();
41  saveMimePartTreeConfig();
42 }
43 
44 MimeTreeModel *MimePartTreeView::mimePartModel() const
45 {
46  return mMimePartModel;
47 }
48 
49 void MimePartTreeView::restoreMimePartTreeConfig()
50 {
51  KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myMimePartTreeViewConfigGroupName));
52  header()->restoreState(grp.readEntry("State", QByteArray()));
53 }
54 
55 void MimePartTreeView::saveMimePartTreeConfig()
56 {
57  KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myMimePartTreeViewConfigGroupName));
58  grp.writeEntry("State", header()->saveState());
59 }
60 
61 void MimePartTreeView::clearModel()
62 {
63  delete mMimePartModel->root();
64  mMimePartModel->setRoot(nullptr);
65 }
66 
67 void MimePartTreeView::setRoot(KMime::Content *root)
68 {
69  delete mMimePartModel->root();
70  mMimePartModel->setRoot(root);
71 }
72 
73 KMime::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"
void append(const T &value)
CustomContextMenu
void reserve(int alloc)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
A model representing the mime part tree of a message.
Definition: mimetreemodel.h:21
QModelIndexList selectedRows(int column) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.