1 /*
2  * SPDX-FileCopyrightText: 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
3  * SPDX-FileCopyrightText: 2011-2021 Laurent Montel <montel@kde.org>
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7 
8 #pragma once
9 
10 #include "templateparser_export.h"
11 
12 #include <QStyledItemDelegate>
13 #include <QTreeWidgetItem>
14 #include <QWidget>
15 
16 class KActionCollection;
17 
18 class Ui_CustomTemplatesBase;
19 
20 namespace TemplateParser
21 {
22 class CustomTemplateItem;
23 /**
24  * @brief The CustomTemplates class
25  */
26 class TEMPLATEPARSER_EXPORT CustomTemplates : public QWidget
27 {
28     Q_OBJECT
29 public:
30     enum Type { TUniversal, TReply, TReplyAll, TForward };
31 
32 public:
33     explicit CustomTemplates(const QList<KActionCollection *> &actionCollection, QWidget *parent = nullptr);
34     ~CustomTemplates() override;
35 
36     void load();
37     void save();
38 
39 Q_SIGNALS:
40     void changed();
41     void templatesUpdated();
42 
43 private Q_SLOTS:
44     void slotInsertCommand(const QString &cmd, int adjustCursor = 0);
45     void slotTextChanged();
46     void slotAddClicked();
47     void slotRemoveClicked();
48     void slotListSelectionChanged();
49     void slotTypeActivated(int index);
50     void slotShortcutChanged(const QKeySequence &newSeq);
51     void slotItemChanged(QTreeWidgetItem *item, int column);
52     void slotHelpLinkClicked(const QString &);
53     void slotNameChanged(const QString &text);
54     void slotDuplicateClicked();
55 
56 private:
57     Q_REQUIRED_RESULT bool nameAlreadyExists(const QString &str, QTreeWidgetItem *item = nullptr);
58     Q_REQUIRED_RESULT QString indexToType(int index);
59     Q_REQUIRED_RESULT QString createUniqueName(const QString &name) const;
60     void iconFromType(CustomTemplates::Type type, CustomTemplateItem *item);
61 
62     /// These templates will be deleted when we're saving.
63     QStringList mItemsToDelete;
64 
65     QIcon mReplyPix;
66     QIcon mReplyAllPix;
67     QIcon mForwardPix;
68 
69     /// Whether or not to Q_EMIT the changed() signal. This is useful to disable when loading
70     /// templates, which changes the UI without user action
71     bool mBlockChangeSignal = false;
72 
73     Ui_CustomTemplatesBase *const mUi;
74 };
75 
76 class CustomTemplateItem : public QTreeWidgetItem
77 {
78 public:
79     explicit CustomTemplateItem(QTreeWidget *parent,
80                                 const QString &name,
81                                 const QString &content,
82                                 const QKeySequence &shortcut,
83                                 CustomTemplates::Type type,
84                                 const QString &to,
85                                 const QString &cc);
86     ~CustomTemplateItem() override;
87     void setCustomType(CustomTemplates::Type type);
88     CustomTemplates::Type customType() const;
89 
90     Q_REQUIRED_RESULT QString to() const;
91     Q_REQUIRED_RESULT QString cc() const;
92 
93     void setTo(const QString &);
94     void setCc(const QString &);
95 
96     Q_REQUIRED_RESULT QString content() const;
97     void setContent(const QString &);
98 
99     Q_REQUIRED_RESULT QKeySequence shortcut() const;
100     void setShortcut(const QKeySequence &);
101 
102     Q_REQUIRED_RESULT QString oldName() const;
103     void setOldName(const QString &);
104 
105 private:
106     QString mName, mContent;
107     QKeySequence mShortcut;
108     CustomTemplates::Type mType;
109     QString mTo, mCC;
110 };
111 
112 class CustomTemplateItemDelegate : public QStyledItemDelegate
113 {
114     Q_OBJECT
115 public:
116     explicit CustomTemplateItemDelegate(QObject *parent = nullptr);
117     ~CustomTemplateItemDelegate() override;
118     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
119 
120     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
121 };
122 }
123 
124