kcm_view_models.h 5.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#ifndef KCM_VIEW_MODELS_H_
#define KCM_VIEW_MODELS_H_

#include <QAbstractItemModel>
#include <QAbstractTableModel>
#include <QSet>
#include <QStyledItemDelegate>

class QTreeView;
class KeyboardConfig;
30
struct Rules;
31 32 33 34 35 36 37
class Flags;

class LayoutsTableModel : public QAbstractTableModel
{
     Q_OBJECT

 public:
38
     LayoutsTableModel(Rules* rules, Flags *flags, KeyboardConfig* keyboardConfig, QObject *parent = nullptr);
39

40 41
     int columnCount(const QModelIndex&) const override;
     Qt::ItemFlags flags(const QModelIndex &index) const override;
42
     QVariant headerData(int section, Qt::Orientation orientation,
43
                         int role = Qt::DisplayRole) const override;
44

45 46 47
     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
     QVariant data(const QModelIndex &index, int role) const override;
     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#ifdef DRAG_ENABLED
     Qt::DropActions supportedDropActions() const {
         return Qt::MoveAction;
     }
     QStringList mimeTypes() const;
     QMimeData *mimeData(const QModelIndexList &indexes) const;
#endif
     void refresh();

     static const int MAP_COLUMN;
     static const int LAYOUT_COLUMN;
     static const int VARIANT_COLUMN;
     static const int DISPLAY_NAME_COLUMN;
     static const int SHORTCUT_COLUMN;

 private:
     KeyboardConfig* keyboardConfig;
     const Rules *rules;
     Flags *countryFlags;
};

class LabelEditDelegate : public QStyledItemDelegate
{
	Q_OBJECT

public:
74
	explicit LabelEditDelegate(const KeyboardConfig* keyboardConfig, QObject *parent = nullptr);
75 76

	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
77
			const QModelIndex &index) const override;
78

79
	void updateEditorGeometry(QWidget *editor,
80
			const QStyleOptionViewItem &option, const QModelIndex &index) const override;
81

82 83 84 85 86 87 88 89 90 91 92
//    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:
    const KeyboardConfig* keyboardConfig;
};

class VariantComboDelegate : public QStyledItemDelegate
{
	Q_OBJECT

public:
93
	VariantComboDelegate(const KeyboardConfig* keyboardConfig, const Rules* rules, QObject *parent = nullptr);
94 95

	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
96
			const QModelIndex &index) const override;
97

98
	void setEditorData(QWidget *editor, const QModelIndex &index) const override;
99
	void setModelData(QWidget *editor, QAbstractItemModel *model,
100
			const QModelIndex &index) const override;
101 102

	void updateEditorGeometry(QWidget *editor,
103
			const QStyleOptionViewItem &option, const QModelIndex &index) const override;
104 105 106 107 108 109 110 111 112 113 114

private:
	const KeyboardConfig* keyboardConfig;
	const Rules* rules;
};

class KKeySequenceWidgetDelegate : public QStyledItemDelegate
{
	Q_OBJECT

public:
115
	KKeySequenceWidgetDelegate(const KeyboardConfig* keyboardConfig_, QObject *parent = nullptr);
116 117

	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
118
			const QModelIndex &index) const override;
119 120
//	void setEditorData(QWidget *editor, const QModelIndex &index) const;
	void setModelData(QWidget *editor, QAbstractItemModel *model,
121
			const QModelIndex &index) const override;
122
	void paint(QPainter* painter, const QStyleOptionViewItem& option,
123
	                                    const QModelIndex& index) const override;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

private:
    const KeyboardConfig* keyboardConfig;
    mutable QSet<QModelIndex> itemsBeingEdited;
};


class XkbOptionsTreeModel: public QAbstractItemModel
{
public:
    XkbOptionsTreeModel(Rules* rules_, KeyboardConfig* keyboardConfig_, QObject *parent)
		: QAbstractItemModel(parent),
		  keyboardConfig(keyboardConfig_),
		  rules(rules_) { }

139 140 141
    int columnCount(const QModelIndex& /*parent*/) const override { return 1; }
    int rowCount(const QModelIndex& parent) const override;
    QModelIndex parent(const QModelIndex& index) const override {
142 143 144 145 146 147
        if (!index.isValid() )
            return QModelIndex();
        if( index.internalId() < 100 )
            return QModelIndex();
        return createIndex(((index.internalId() - index.row())/100) - 1, index.column());
    }
148
    QModelIndex index(int row, int column, const QModelIndex& parent) const override {
149 150 151
        if(!parent.isValid()) return createIndex(row, column);
        return createIndex(row, column, (100 * (parent.row()+1)) + row);
    }
152
    Qt::ItemFlags flags ( const QModelIndex & index ) const override {
153
        if( ! index.isValid() )
154
            return Qt::ItemFlags();
155 156

        if( !index.parent().isValid() )
157
            return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
158

159
        return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
160 161
    }

162 163
    bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) override;
    QVariant data(const QModelIndex& index, int role) const override;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
164 165 166 167
    void reset() {
        beginResetModel();
        endResetModel();
    }
168 169 170 171 172 173 174 175
    void gotoGroup(const QString& group, QTreeView* view);

private:
    KeyboardConfig* keyboardConfig;
    Rules *rules;
};

#endif /* KCM_VIEW_MODELS_H_ */