1 ////////////////////////////////////////
2 // File : datamodels.cpp //
3 // Written by: disels@gmail.com //
5 ////////////////////////////////////////
6 #include "datamodels.h"
9 ////////////////////////////////////////////////////////////////////////////////
10 QVariant
SrcLayoutModel::headerData (int section
, Qt::Orientation orientation
, int role
) const {
11 if (role
!= Qt::DisplayRole
) return QVariant();
12 if (orientation
== Qt::Horizontal
) {
14 case 1: return tr("Layout Name");
15 case 2: return tr("Map");
23 QVariant
SrcLayoutModel::data (const QModelIndex
&index
, int role
) const {
26 QHash
<QString
, QString
> layouts
;
27 QString layout
, PNG
, SVG
;
31 if (!index
.isValid()) return QVariant();
34 layouts
= mRules
->layouts
;
35 layout
= mLayoutKeys
[row
];
36 PNG
= mIconDir
+"/"+layout
+".png";
37 SVG
= mIconDir
+"/"+layout
+".svg";
38 isPNG
= QFile::exists(PNG
);
39 isSVG
= QFile::exists(SVG
);
42 isSVG
= flagSVG
.isValid();
51 flagSVG
.render(&painter
, QRectF(0, 0, 48, 22));
60 case Qt::TextAlignmentRole
: return int(Qt::AlignLeft
| Qt::AlignVCenter
);
61 case Qt::DecorationRole
:
62 if (col
== LAYOUT_COLUMN_FLAG
) return QIcon::fromTheme(layout
, icons
).pixmap(QSize(48, 32));
66 case LAYOUT_COLUMN_NAME
: return layouts
.value(layout
);
67 case LAYOUT_COLUMN_MAP
: return layout
;
77 Qt::ItemFlags
SrcLayoutModel::flags (const QModelIndex
&index
) const {
78 Qt::ItemFlags defaultFlags
= QAbstractTableModel::flags(index
);
79 return Qt::ItemIsDragEnabled
| Qt::ItemIsDropEnabled
| defaultFlags
;
83 QMimeData
*SrcLayoutModel::mimeData (const QModelIndexList
&indexes
) const {
84 QMimeData
*mimeData
= QAbstractTableModel::mimeData(indexes
);
85 mimeData
->setText(indexes
.first().data().toString());
90 bool SrcLayoutModel::dropMimeData (const QMimeData
*, Qt::DropAction action
, int, int, const QModelIndex
&) {
91 if (action
== Qt::IgnoreAction
) return false;
97 ////////////////////////////////////////////////////////////////////////////////
98 QVariant
DstLayoutModel::headerData (int section
, Qt::Orientation orientation
, int role
) const {
99 if (role
!= Qt::DisplayRole
) return QVariant();
100 if (orientation
== Qt::Horizontal
) {
102 case 1: return tr("Layout Name");
103 case 2: return tr("Map");
104 case 3: return tr("Variant");
105 case 4: return tr("Label");
113 QVariant
DstLayoutModel::data (const QModelIndex
&index
, int role
) const {
114 QHash
<QString
, QString
> layouts
;
118 if (!index
.isValid()) return QVariant();
119 col
= index
.column();
121 layouts
= mRules
->layouts
;
122 lu
= mQxkb
->layouts
[row
];
124 case Qt::TextAlignmentRole
: return int(Qt::AlignLeft
| Qt::AlignVCenter
);
125 case Qt::DecorationRole
:
126 if (col
== LAYOUT_COLUMN_FLAG
) {
127 return QIcon::fromTheme(lu
.layout
, QIcon(mIconDir
+"/"+lu
.layout
+".png")).pixmap(QSize(48, 32));
130 case Qt::DisplayRole
:
132 case LAYOUT_COLUMN_NAME
: return layouts
[lu
.layout
];
133 case LAYOUT_COLUMN_MAP
: return lu
.layout
;
134 case LAYOUT_COLUMN_VARIANT
: return lu
.variant
;
135 case LAYOUT_COLUMN_DISPLAY_NAME
: return lu
.getDisplayName();
145 bool DstLayoutModel::dropMimeData (const QMimeData
*, Qt::DropAction action
, int, int, const QModelIndex
&) {
146 if (action
== Qt::IgnoreAction
) return false;
152 Qt::ItemFlags
DstLayoutModel::flags (const QModelIndex
&index
) const {
153 Qt::ItemFlags defaultFlags
= QAbstractTableModel::flags(index
);
155 if (mQxkb
->layouts
.size() >= 4) return Qt::ItemIsDragEnabled
| defaultFlags
;
156 return Qt::ItemIsDropEnabled
| Qt::ItemIsDragEnabled
| defaultFlags
;
160 ////////////////////////////////////////////////////////////////////////////////
161 QVariant
XkbOptionsModel::data (const QModelIndex
&index
, int role
) const {
164 const XkbOptionGroup
*xkbGroup
;
166 if (!index
.isValid()) return QVariant();
169 case Qt::DisplayRole
:
170 if (!index
.parent().isValid()) return mRules
->optionGroups
.values()[row
].description
;
171 groupRow
= index
.parent().row();
172 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
173 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
174 return xkbGroup
->options
[row
].description
;
175 case Qt::CheckStateRole
:
176 if (index
.parent().isValid()) {
177 groupRow
= index
.parent().row();
178 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
179 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
180 return mXkbConfig
->options
.indexOf(xkbGroup
->options
[row
].name
) == -1 ? Qt::Unchecked
: Qt::Checked
;
189 int XkbOptionsModel::rowCount (const QModelIndex
&parent
) const {
190 if (!parent
.isValid()) return mRules
->optionGroups
.count();
191 if (!parent
.parent().isValid()) return mRules
->optionGroups
.values()[parent
.row()].options
.count();
196 QModelIndex
XkbOptionsModel::parent (const QModelIndex
&index
) const {
197 if (!index
.isValid()) return QModelIndex();
198 if (index
.internalId() < 100) return QModelIndex();
199 return createIndex(((index
.internalId()-index
.row())/100)-1, index
.column());
203 QModelIndex
XkbOptionsModel::index (int row
, int column
, const QModelIndex
&parent
) const {
204 if (!parent
.isValid()) return createIndex(row
, column
);
205 return createIndex(row
, column
, (100*(parent
.row()+1))+row
);
209 Qt::ItemFlags
XkbOptionsModel::flags (const QModelIndex
&index
) const {
210 if (!index
.isValid()) return 0;
211 if (!index
.parent().isValid()) return Qt::ItemIsEnabled
;
212 return Qt::ItemIsEnabled
| Qt::ItemIsUserCheckable
;
216 bool XkbOptionsModel::setData (const QModelIndex
&index
, const QVariant
&value
, int role
) {
218 const XkbOptionGroup
*xkbGroup
;
219 const XkbOption
*option
;
220 int groupRow
= index
.parent().row();
222 if (groupRow
< 0) return false;
223 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
224 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
225 option
= &xkbGroup
->options
[index
.row()];
226 if (value
.toInt() == Qt::Checked
) {
227 if (xkbGroup
->exclusive
) {
228 // clear if exclusive (TODO: radiobutton)
229 int idx
= mXkbConfig
->options
.indexOf(QRegExp(xkbGroupNm
+".*"));
231 for (int f
= 0; f
< xkbGroup
->options
.count(); ++f
) {
232 if (xkbGroup
->options
[f
].name
== mXkbConfig
->options
[idx
]) {
233 setData(createIndex(f
, index
.column(), (quint32
)index
.internalId()-index
.row()+f
), Qt::Unchecked
, role
);
237 //m_kxkbConfig->m_options.removeAt(idx);
238 //idx = m_kxkbConfig->m_options.indexOf(QRegExp(xkbGroupNm+".*"));
241 if (mXkbConfig
->options
.indexOf(option
->name
) < 0) mXkbConfig
->options
.append(option
->name
);
243 mXkbConfig
->options
.removeAll(option
->name
);
245 emit
dataChanged(index
, index
);
250 void XkbOptionsModel::gotoGroup (const QString
&group
, QTreeView
*view
) {
251 int index
= mRules
->optionGroups
.keys().indexOf(group
);
254 QModelIndex modelIdx
= createIndex(index
, 0);
255 view
->setExpanded(modelIdx
, true);
256 view
->scrollTo(modelIdx
, QAbstractItemView::PositionAtTop
);
257 view
->selectionModel()->setCurrentIndex(modelIdx
, QItemSelectionModel::Current
);
258 view
->setFocus(Qt::OtherFocusReason
);
260 qDebug() << "XkbOptionsModel:can't scroll to group" << group
;