File indexing completed on 2024-05-12 05:39:49

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Renaud Guezennec                                *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef SHORTCUTMODEL_H
0021 #define SHORTCUTMODEL_H
0022 
0023 #include <QAbstractItemModel>
0024 #include <QKeySequence>
0025 #include <core_global.h>
0026 #include <vector>
0027 #include <memory>
0028 
0029 class CORE_EXPORT ShortCutItem
0030 {
0031 public:
0032     virtual ~ShortCutItem();
0033     virtual bool isLeaf() const= 0;
0034 };
0035 
0036 class CORE_EXPORT ShortCut : public ShortCutItem
0037 {
0038 public:
0039     ShortCut(const QString& name, const QString& seq);
0040 
0041     QKeySequence getSequence() const;
0042     void setSequence(const QKeySequence& seq);
0043 
0044     bool isLeaf() const override { return true; }
0045 
0046     QString getName() const;
0047     void setName(const QString& name);
0048 
0049 private:
0050     QString m_name;
0051     QKeySequence m_seq;
0052 };
0053 
0054 class CORE_EXPORT Category : public ShortCutItem
0055 {
0056 public:
0057     Category(const QString& name);
0058     Category(Category&& other);
0059 
0060     QString name() const;
0061     void setName(const QString& name);
0062 
0063     ShortCut* getShortCut(int i) const;
0064 
0065     bool isLeaf() const override { return false; }
0066 
0067     int size() const;
0068 
0069     bool hasShortCut(ShortCut* cut) const;
0070     int indexOf(ShortCut* cut) const;
0071 
0072     void insertShortcut(const QString& name, const QString& key);
0073 
0074 private:
0075     std::vector<std::unique_ptr<ShortCut>> m_shortcuts;
0076     QString m_name;
0077 };
0078 /**
0079  * @brief The ShortCutModel class
0080  */
0081 class CORE_EXPORT ShortCutModel : public QAbstractItemModel
0082 {
0083     Q_OBJECT
0084 public:
0085     ShortCutModel();
0086 
0087     virtual QModelIndex index(int, int, const QModelIndex& parent) const override;
0088     virtual QModelIndex parent(const QModelIndex& parent) const override;
0089 
0090     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0091     int columnCount(const QModelIndex& parent) const override;
0092     QVariant data(const QModelIndex& index, int role) const override;
0093     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0094 
0095     void insertShortCut(const QString& category, const QString& name, const QString& key);
0096     void addCategory(const QString& category);
0097     void removeCategory(const QString& category, bool isDestoyed = false);
0098 
0099 
0100 private:
0101     std::vector<std::unique_ptr<Category>> m_root;
0102 };
0103 
0104 #endif