cosmetix
[jrugr.git] / src / datamodels.h
blob8af2d03844fb0836d4418a78fcaf63b119c7d0d1
1 ////////////////////////////////////////
2 // File : datamodels.h //
3 // Written by: disels@gmail.com //
4 // Copyright : GPL //
5 ////////////////////////////////////////
7 #ifndef DATAMODELS_H
8 #define DATAMODELS_H
10 #include "defs.h"
11 #include "x11tools.h"
14 enum {
15 LAYOUT_COLUMN_FLAG = 0,
16 LAYOUT_COLUMN_NAME = 1,
17 LAYOUT_COLUMN_MAP = 2,
18 LAYOUT_COLUMN_VARIANT = 3,
19 LAYOUT_COLUMN_DISPLAY_NAME = 4,
20 SRC_LAYOUT_COLUMN_COUNT = 3,
21 DST_LAYOUT_COLUMN_COUNT = 5
25 static bool localeAwareLessThan (const QString &s1, const QString &s2) {
26 return QString::localeAwareCompare(s1, s2) < 0;
30 static QList<QString> getKeysSortedByVaue (const QHash<QString, QString> &map) {
31 QList<QString> outList;
32 QMap<QString, QString> reverseMap;
34 // we have to add nums as translations can be dups and them reverse map will miss items
35 int f = 0;
36 QString fmt("%1%2");
37 foreach (const QString& str, map.keys()) reverseMap.insert(fmt.arg(map[str], QString::number(f++)), str);
38 QList<QString> values = reverseMap.keys();
39 qSort(values.begin(), values.end(), localeAwareLessThan);
40 foreach (const QString& value, values) outList << reverseMap[value];
41 return outList;
45 class SrcLayoutModel: public QAbstractTableModel {
46 Q_OBJECT
48 public:
49 SrcLayoutModel (RulesInfo *rules, QString icoPath, QObject *parent) : QAbstractTableModel(parent) {
50 setRules(rules);
51 mIconDir = icoPath;
54 //bool hasChildren (const QModelIndex &parent=QModelIndex()) const { return false; }
56 int columnCount (const QModelIndex &parent) const { return !parent.isValid() ? SRC_LAYOUT_COLUMN_COUNT : 0; }
57 int rowCount (const QModelIndex &) const { return mRules->layouts.keys().count(); }
59 QVariant data (const QModelIndex &index, int role) const;
60 QVariant headerData (int section, Qt::Orientation orientation, int role) const;
62 void setRules (RulesInfo *rules) {
63 mRules = rules;
64 mLayoutKeys = getKeysSortedByVaue( mRules->layouts);
67 QString getLayoutAt (int row) { return mLayoutKeys[row]; }
69 bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
70 Qt::ItemFlags flags (const QModelIndex &index) const;
71 QMimeData *mimeData (const QModelIndexList &indexes) const;
73 signals:
74 void layoutRemoved ();
76 private:
77 RulesInfo *mRules;
78 QStringList mLayoutKeys;
79 QString mIconDir;
83 class DstLayoutModel: public QAbstractTableModel {
84 Q_OBJECT
86 public:
87 DstLayoutModel (RulesInfo *rules, XKBConf *qxkb, QString icoPath, QObject *parent) : QAbstractTableModel(parent) {
88 setRules(rules);
89 setConf(qxkb);
90 mIconDir = icoPath;
93 int columnCount (const QModelIndex &parent) const { return !parent.isValid() ? DST_LAYOUT_COLUMN_COUNT : 0; }
94 int rowCount (const QModelIndex &) const { return mQxkb->layouts.size();}
96 QVariant data (const QModelIndex &index, int role) const;
97 QVariant headerData (int section, Qt::Orientation orientation, int role) const;
99 void setRules (RulesInfo *rules) { mRules = rules; }
100 void setConf (XKBConf *qxkb) { mQxkb = qxkb; }
101 void reset () { QAbstractTableModel::reset(); }
102 void emitDataChange (int row, int col) { emit dataChanged(createIndex(row, col), createIndex(row, col)); }
103 bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
104 Qt::ItemFlags flags (const QModelIndex &index) const;
106 signals:
107 void layoutAdded ();
109 private:
110 RulesInfo *mRules;
111 XKBConf *mQxkb;
112 QString mIconDir;
116 class XkbOptionsModel: public QAbstractItemModel {
117 Q_OBJECT
119 public:
120 XkbOptionsModel (RulesInfo *rules, XKBConf *xkbConfig, QObject *parent) : QAbstractItemModel(parent) {
121 setRules(rules, xkbConfig);
124 int columnCount (const QModelIndex &) const { return 1; }
125 int rowCount (const QModelIndex &parent) const;
126 QModelIndex parent (const QModelIndex &index) const;
127 QModelIndex index (int row, int column, const QModelIndex &parent) const;
128 Qt::ItemFlags flags (const QModelIndex &index) const;
129 bool setData ( const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
130 QVariant data (const QModelIndex &index, int role) const;
132 void setRules (RulesInfo *rules, XKBConf *xkbConfig) { mRules = rules; mXkbConfig = xkbConfig; }
133 void reset () { QAbstractItemModel::reset(); }
134 void gotoGroup (const QString &group, QTreeView *view);
136 private:
137 RulesInfo *mRules;
138 XKBConf *mXkbConfig;
142 #endif