Messagelib

mimeparttreeview.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <[email protected]>
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(QStringLiteral("mMimePartTree"));
26 
27  setModel(mMimePartModel);
28  setSelectionMode(QAbstractItemView::ExtendedSelection);
29  setSelectionBehavior(QAbstractItemView::SelectRows);
30  connect(this, &MimePartTreeView::destroyed, this, &MimePartTreeView::slotMimePartDestroyed);
31  setContextMenuPolicy(Qt::CustomContextMenu);
32  header()->setSectionResizeMode(QHeaderView::ResizeToContents);
33  connect(mMimePartModel, &MimeTreeModel::modelReset, this, &MimePartTreeView::expandAll);
34  restoreMimePartTreeConfig();
35  setDragEnabled(true);
36 }
37 
38 MimePartTreeView::~MimePartTreeView()
39 {
40  disconnect(mMimePartModel, &MimeTreeModel::modelReset, this, &MimePartTreeView::expandAll);
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(), myMimePartTreeViewConfigGroupName);
52  header()->restoreState(grp.readEntry("State", QByteArray()));
53 }
54 
55 void MimePartTreeView::saveMimePartTreeConfig()
56 {
57  KConfigGroup grp(KSharedConfig::openStateConfig(), myMimePartTreeViewConfigGroupName);
58  grp.writeEntry("State", header()->saveState());
59 }
60 
61 void MimePartTreeView::slotMimePartDestroyed()
62 {
63  // root is either null or a modified tree that we need to clean up
64  clearModel();
65 }
66 
67 void MimePartTreeView::clearModel()
68 {
69  delete mMimePartModel->root();
70  mMimePartModel->setRoot(nullptr);
71 }
72 
73 void MimePartTreeView::setRoot(KMime::Content *root)
74 {
75  delete mMimePartModel->root();
76  mMimePartModel->setRoot(root);
77 }
78 
79 KMime::Content::List MimePartTreeView::selectedContents() const
80 {
81  KMime::Content::List contents;
82  QItemSelectionModel *selectModel = selectionModel();
83  const QModelIndexList selectedRows = selectModel->selectedRows();
84  contents.reserve(selectedRows.count());
85  for (const QModelIndex &index : selectedRows) {
86  auto content = static_cast<KMime::Content *>(index.internalPointer());
87  if (content) {
88  contents.append(content);
89  }
90  }
91  return contents;
92 }
void append(const T &value)
A model representing the mime part tree of a message.
Definition: mimetreemodel.h:21
CustomContextMenu
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QModelIndexList selectedRows(int column) const const
void reserve(int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.