File indexing completed on 2024-03-24 04:59:57

0001 /***************************************************************************
0002  *   Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #ifndef AUTOPASTEMODEL
0021 #define AUTOPASTEMODEL
0022 
0023 #include <QAbstractTableModel>
0024 #include <QStyledItemDelegate>
0025 
0026 class AutoPasteDelegate : public QStyledItemDelegate
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     AutoPasteDelegate(QAbstractItemModel *types, QAbstractItemModel *syntaxes, QObject *parent = nullptr);
0032 
0033     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0034     void setEditorData(QWidget *editor, const QModelIndex &index) const override;
0035     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
0036     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0037     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0038 
0039 private:
0040     QAbstractItemModel *m_types;
0041     QAbstractItemModel *m_syntaxes;
0042 };
0043 
0044 class AutoPasteModel : public QAbstractTableModel
0045 {
0046     Q_OBJECT
0047 
0048 public:
0049     enum DataType { Type = 0, Pattern, PatternSyntax };
0050     enum TypeData { Include = 0, Exclude };
0051     enum PatternSyntaxData { Wildcard = 0, RegExp };
0052 
0053     explicit AutoPasteModel(QObject *parent = nullptr);
0054     ~AutoPasteModel() override;
0055 
0056     int rowCount(const QModelIndex &index = QModelIndex()) const override;
0057     int columnCount(const QModelIndex &index = QModelIndex()) const override;
0058     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0059     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0060     Qt::ItemFlags flags(const QModelIndex &index) const override;
0061     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0062     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0063 
0064     /**
0065      * Adds an an item
0066      * @note do not use this for loading data
0067      * @see load()
0068      */
0069     void addItem(TypeData dataType, PatternSyntaxData patternSyntax, const QString &pattern);
0070 
0071     /**
0072      * Moves an item to a new position
0073      * @param sourceRow the current row of the selected item
0074      * @param destinationRow is the new row before the move,
0075      * i.e. if sourceRow was not removed from the model
0076      * @see QAbstractItemModel::beginMoveRows()
0077      */
0078     bool moveItem(int sourceRow, int destinationRow);
0079 
0080 public Q_SLOTS:
0081     /**
0082      * Loads the stored data
0083      * @see save()
0084      */
0085     void load();
0086 
0087     /**
0088      * Saves the current data
0089      * @see load()
0090      */
0091     void save();
0092 
0093     /**
0094      * Resets the model to the default data
0095      */
0096     void resetDefaults();
0097 
0098 private:
0099     void addItems(const QList<int> &dataTypes, const QList<int> patternSyntaxes, const QStringList &patterns);
0100 
0101 private:
0102     struct Data {
0103         TypeData type;
0104         QString pattern;
0105         PatternSyntaxData syntax;
0106     };
0107 
0108     QList<Data> m_data;
0109 };
0110 
0111 #endif