File indexing completed on 2024-06-16 04:56:15

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     view/keytreeview.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2009 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <QWidget>
0013 
0014 #include <QString>
0015 #include <QStringList>
0016 
0017 #include <gpgme++/key.h>
0018 
0019 #include <memory>
0020 #include <vector>
0021 
0022 #include <KConfigGroup>
0023 
0024 #include <Libkleo/TreeView>
0025 
0026 class QTreeView;
0027 
0028 namespace Kleo
0029 {
0030 
0031 class KeyFilter;
0032 class AbstractKeyListModel;
0033 class AbstractKeyListSortFilterProxyModel;
0034 class KeyListSortFilterProxyModel;
0035 class SearchBar;
0036 
0037 class KeyTreeView : public QWidget
0038 {
0039     Q_OBJECT
0040 public:
0041     explicit KeyTreeView(QWidget *parent = nullptr);
0042     KeyTreeView(const QString &stringFilter,
0043                 const std::shared_ptr<KeyFilter> &keyFilter,
0044                 AbstractKeyListSortFilterProxyModel *additionalProxy,
0045                 QWidget *parent,
0046                 const KConfigGroup &group);
0047     ~KeyTreeView() override;
0048 
0049     QTreeView *view() const
0050     {
0051         return m_view;
0052     }
0053 
0054     AbstractKeyListModel *model() const
0055     {
0056         return m_isHierarchical ? hierarchicalModel() : flatModel();
0057     }
0058 
0059     AbstractKeyListModel *flatModel() const
0060     {
0061         return m_flatModel;
0062     }
0063     AbstractKeyListModel *hierarchicalModel() const
0064     {
0065         return m_hierarchicalModel;
0066     }
0067 
0068     void setFlatModel(AbstractKeyListModel *model);
0069     void setHierarchicalModel(AbstractKeyListModel *model);
0070 
0071     void setKeys(const std::vector<GpgME::Key> &keys);
0072     const std::vector<GpgME::Key> &keys() const
0073     {
0074         return m_keys;
0075     }
0076 
0077     void selectKeys(const std::vector<GpgME::Key> &keys);
0078     std::vector<GpgME::Key> selectedKeys() const;
0079 
0080     void addKeysUnselected(const std::vector<GpgME::Key> &keys);
0081     void addKeysSelected(const std::vector<GpgME::Key> &keys);
0082     void removeKeys(const std::vector<GpgME::Key> &keys);
0083 
0084 #if 0
0085     void setToolTipOptions(int options);
0086     int toolTipOptions() const;
0087 #endif
0088 
0089     QString stringFilter() const
0090     {
0091         return m_stringFilter;
0092     }
0093     const std::shared_ptr<KeyFilter> &keyFilter() const
0094     {
0095         return m_keyFilter;
0096     }
0097     bool isHierarchicalView() const
0098     {
0099         return m_isHierarchical;
0100     }
0101 
0102     void setColumnSizes(const std::vector<int> &sizes);
0103     std::vector<int> columnSizes() const;
0104 
0105     void setSortColumn(int sortColumn, Qt::SortOrder sortOrder);
0106     int sortColumn() const;
0107     Qt::SortOrder sortOrder() const;
0108 
0109     virtual KeyTreeView *clone() const
0110     {
0111         return new KeyTreeView(*this);
0112     }
0113 
0114     void disconnectSearchBar();
0115     bool connectSearchBar(const SearchBar *bar);
0116     void resizeColumns();
0117     void restoreLayout(const KConfigGroup &group);
0118 
0119 public Q_SLOTS:
0120     virtual void setStringFilter(const QString &text);
0121     virtual void setKeyFilter(const std::shared_ptr<Kleo::KeyFilter> &filter);
0122     virtual void setHierarchicalView(bool on);
0123 
0124 Q_SIGNALS:
0125     void stringFilterChanged(const QString &filter);
0126     void keyFilterChanged(const std::shared_ptr<Kleo::KeyFilter> &filter);
0127     void hierarchicalChanged(bool on);
0128 
0129 protected:
0130     KeyTreeView(const KeyTreeView &);
0131 
0132 private:
0133     void init();
0134     void addKeysImpl(const std::vector<GpgME::Key> &, bool);
0135     void restoreExpandState();
0136     void setUpTagKeys();
0137     void updateModelConnections(AbstractKeyListModel *oldModel, AbstractKeyListModel *newModel);
0138     void saveStateBeforeModelChange();
0139     void restoreStateAfterModelChange();
0140 
0141 private:
0142     std::vector<GpgME::Key> m_keys;
0143 
0144     KeyListSortFilterProxyModel *m_proxy;
0145     AbstractKeyListSortFilterProxyModel *m_additionalProxy;
0146 
0147     TreeView *m_view;
0148 
0149     AbstractKeyListModel *m_flatModel;
0150     AbstractKeyListModel *m_hierarchicalModel;
0151 
0152     QString m_stringFilter;
0153     std::shared_ptr<KeyFilter> m_keyFilter;
0154 
0155     QStringList m_expandedKeys;
0156     std::vector<GpgME::Key> m_selectedKeys;
0157     GpgME::Key m_currentKey;
0158 
0159     std::vector<QMetaObject::Connection> m_connections;
0160 
0161     KConfigGroup m_group;
0162 
0163     bool m_isHierarchical : 1;
0164     bool m_onceResized : 1;
0165 };
0166 
0167 }