File indexing completed on 2024-05-12 05:47:30

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KITEMLISTROLEEDITOR_H
0008 #define KITEMLISTROLEEDITOR_H
0009 
0010 #include "dolphin_export.h"
0011 
0012 #include <KTextEdit>
0013 
0014 enum EditResultDirection {
0015     EditDone,
0016     EditNext,
0017     EditPrevious,
0018 };
0019 Q_DECLARE_METATYPE(EditResultDirection)
0020 
0021 struct EditResult {
0022     QString newName;
0023     EditResultDirection direction;
0024 };
0025 Q_DECLARE_METATYPE(EditResult)
0026 
0027 /**
0028  * @brief Editor for renaming roles of a KItemListWidget.
0029  *
0030  * Provides signals when the editing got cancelled (e.g. by
0031  * pressing Escape or when losing the focus) or when the editing
0032  * got finished (e.g. by pressing Enter, Tab or Return).
0033  *
0034  * The size automatically gets increased if the text does not fit.
0035  */
0036 class DOLPHIN_EXPORT KItemListRoleEditor : public KTextEdit
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041     explicit KItemListRoleEditor(QWidget *parent);
0042     ~KItemListRoleEditor() override;
0043 
0044     void setRole(const QByteArray &role);
0045     QByteArray role() const;
0046 
0047     void setAllowUpDownKeyChainEdit(bool allowChainEdit);
0048     bool eventFilter(QObject *watched, QEvent *event) override;
0049 
0050 Q_SIGNALS:
0051     void roleEditingFinished(const QByteArray &role, const QVariant &value);
0052     void roleEditingCanceled(const QByteArray &role, const QVariant &value);
0053 
0054 public Q_SLOTS:
0055     /**
0056      * Increases the size of the editor in case if there is not
0057      * enough room for the text.
0058      */
0059     void autoAdjustSize();
0060 
0061 protected:
0062     bool event(QEvent *event) override;
0063     void keyPressEvent(QKeyEvent *event) override;
0064 
0065 private:
0066     /**
0067      * Emits the signal roleEditingFinished if m_blockFinishedSignal
0068      * is false.
0069      */
0070     void emitRoleEditingFinished(EditResultDirection direction = EditDone);
0071 
0072 private:
0073     QByteArray m_role;
0074     bool m_blockFinishedSignal;
0075     bool m_allowUpDownKeyChainEdit;
0076 };
0077 
0078 #endif