1 ////////////////////////////////////////
2 // File : datamodels.h //
3 // Written by: disels@gmail.com //
5 ////////////////////////////////////////
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
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
];
45 class SrcLayoutModel
: public QAbstractTableModel
{
49 SrcLayoutModel (RulesInfo
*rules
, QString icoPath
, QObject
*parent
) : QAbstractTableModel(parent
) {
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
) {
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;
74 void layoutRemoved ();
78 QStringList mLayoutKeys
;
83 class DstLayoutModel
: public QAbstractTableModel
{
87 DstLayoutModel (RulesInfo
*rules
, XKBConf
*qxkb
, QString icoPath
, QObject
*parent
) : QAbstractTableModel(parent
) {
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;
116 class XkbOptionsModel
: public QAbstractItemModel
{
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
);