File indexing completed on 2025-01-05 04:55:48

0001 /*
0002     keylistview.h
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "kleo_export.h"
0013 
0014 #include "treewidget.h"
0015 
0016 #include <QByteArray>
0017 #include <QHeaderView>
0018 #include <QIcon>
0019 
0020 #include <gpgme++/key.h>
0021 
0022 class QFont;
0023 class QColor;
0024 
0025 namespace Kleo
0026 {
0027 
0028 // work around moc parser bug...
0029 #define TEMPLATE_TYPENAME(T) template<typename T>
0030 TEMPLATE_TYPENAME(T)
0031 inline T *lvi_cast(QTreeWidgetItem *item)
0032 {
0033     return item && (item->type() == T::RTTI) ? static_cast<T *>(item) : nullptr;
0034 }
0035 
0036 TEMPLATE_TYPENAME(T)
0037 inline const T *lvi_cast(const QTreeWidgetItem *item)
0038 {
0039     return item && (item->type() == T::RTTI) ? static_cast<const T *>(item) : nullptr;
0040 }
0041 #undef TEMPLATE_TYPENAME
0042 
0043 class KeyListView;
0044 
0045 class KeyListViewItem : public QTreeWidgetItem
0046 {
0047 public:
0048     KeyListViewItem(KeyListView *parent, const GpgME::Key &key);
0049     KeyListViewItem(KeyListView *parent, KeyListViewItem *after, const GpgME::Key &key);
0050     KeyListViewItem(KeyListViewItem *parent, const GpgME::Key &key);
0051     KeyListViewItem(KeyListViewItem *parent, KeyListViewItem *after, const GpgME::Key &key);
0052     ~KeyListViewItem() override;
0053 
0054     void setKey(const GpgME::Key &key);
0055     const GpgME::Key &key() const
0056     {
0057         return mKey;
0058     }
0059 
0060     enum { RTTI = QTreeWidgetItem::UserType + 1 };
0061 
0062     //
0063     // only boring stuff below:
0064     //
0065     virtual QString toolTip(int column) const;
0066 
0067     /*! \reimp for covariant return */
0068     KeyListView *listView() const;
0069     /*! \reimp for covariant return */
0070     KeyListViewItem *nextSibling() const;
0071     /*! \reimp */
0072     bool operator<(const QTreeWidgetItem &other) const override;
0073     /*! \reimp */
0074     void takeItem(QTreeWidgetItem *item);
0075 
0076 private:
0077     GpgME::Key mKey;
0078 };
0079 
0080 class KLEO_EXPORT KeyListView : public TreeWidget
0081 {
0082     Q_OBJECT
0083     friend class KeyListViewItem;
0084 
0085 public:
0086     class KLEO_EXPORT ColumnStrategy
0087     {
0088     public:
0089         virtual ~ColumnStrategy();
0090         virtual QString title(int column) const = 0;
0091         virtual int width(int column, const QFontMetrics &fm) const;
0092         virtual QHeaderView::ResizeMode resizeMode(int) const
0093         {
0094             return QHeaderView::Interactive;
0095         }
0096 
0097         virtual QString text(const GpgME::Key &key, int column) const = 0;
0098         virtual QString accessibleText(const GpgME::Key &key, int column) const = 0;
0099         virtual QString toolTip(const GpgME::Key &key, int column) const;
0100         virtual QIcon icon(const GpgME::Key &, int) const
0101         {
0102             return QIcon();
0103         }
0104         virtual int compare(const GpgME::Key &key1, const GpgME::Key &key2, const int column) const;
0105     };
0106 
0107     class KLEO_EXPORT DisplayStrategy
0108     {
0109     public:
0110         virtual ~DisplayStrategy();
0111         // font
0112         virtual QFont keyFont(const GpgME::Key &, const QFont &) const;
0113         // foreground
0114         virtual QColor keyForeground(const GpgME::Key &, const QColor &) const;
0115         // background
0116         virtual QColor keyBackground(const GpgME::Key &, const QColor &) const;
0117     };
0118 
0119     explicit KeyListView(const ColumnStrategy *strategy, const DisplayStrategy *display = nullptr, QWidget *parent = nullptr, Qt::WindowFlags f = {});
0120 
0121     ~KeyListView() override;
0122 
0123     const ColumnStrategy *columnStrategy() const
0124     {
0125         return mColumnStrategy;
0126     }
0127     const DisplayStrategy *displayStrategy() const
0128     {
0129         return mDisplayStrategy;
0130     }
0131 
0132     bool hierarchical() const
0133     {
0134         return mHierarchical;
0135     }
0136     virtual void setHierarchical(bool hier);
0137 
0138     void flushKeys()
0139     {
0140         slotUpdateTimeout();
0141     }
0142 
0143     bool isMultiSelection() const;
0144 
0145     KeyListViewItem *itemByFingerprint(const QByteArray &) const;
0146 
0147 public:
0148     using QTreeWidget::selectionChanged; // for below, but moc doesn't like it to be in the Q_SIGNALS: section
0149 Q_SIGNALS:
0150     void doubleClicked(Kleo::KeyListViewItem *, int);
0151     void returnPressed(Kleo::KeyListViewItem *);
0152     void selectionChanged(Kleo::KeyListViewItem *);
0153     void contextMenu(Kleo::KeyListViewItem *, const QPoint &);
0154 
0155 protected:
0156     void keyPressEvent(QKeyEvent *event) override;
0157 
0158 public Q_SLOTS:
0159     virtual void slotAddKey(const GpgME::Key &key);
0160     virtual void slotRefreshKey(const GpgME::Key &key);
0161 
0162     //
0163     // Only boring stuff below:
0164     //
0165 private Q_SLOTS:
0166     void slotEmitDoubleClicked(QTreeWidgetItem *, int);
0167     void slotEmitReturnPressed(QTreeWidgetItem *);
0168     void slotEmitSelectionChanged();
0169     void slotEmitContextMenu(const QPoint &pos);
0170     void slotUpdateTimeout();
0171 
0172 public:
0173     /*! \reimp for covariant return */
0174     KeyListViewItem *selectedItem() const;
0175     /*! \reimp */
0176     QList<KeyListViewItem *> selectedItems() const;
0177     /*! \reimp for covariant return */
0178     KeyListViewItem *firstChild() const;
0179     /*! \reimp */
0180     void clear();
0181     /*! \reimp */
0182     void takeItem(QTreeWidgetItem *);
0183 
0184 private:
0185     void doHierarchicalInsert(const GpgME::Key &);
0186     void gatherScattered();
0187     void scatterGathered(KeyListViewItem *);
0188     void registerItem(KeyListViewItem *);
0189     void deregisterItem(const KeyListViewItem *);
0190 
0191 private:
0192     const ColumnStrategy *mColumnStrategy = nullptr;
0193     const DisplayStrategy *mDisplayStrategy = nullptr;
0194     bool mHierarchical = false;
0195 
0196     class KeyListViewPrivate;
0197     std::unique_ptr<KeyListViewPrivate> const d;
0198 };
0199 }