1 ////////////////////////////////////////
3 ////////////////////////////////////////
4 #include "datamodels.h"
7 ////////////////////////////////////////////////////////////////////////////////
8 QVariant
SrcLayoutModel::headerData (int section
, Qt::Orientation orientation
, int role
) const {
9 if (role
!= Qt::DisplayRole
) return QVariant();
10 if (orientation
== Qt::Horizontal
) {
12 case 1: return tr("Layout Name");
13 case 2: return tr("Map");
21 QVariant
SrcLayoutModel::data (const QModelIndex
&index
, int role
) const {
24 QHash
<QString
, QString
> layouts
;
25 QString layout
, PNG
, SVG
;
29 if (!index
.isValid()) return QVariant();
32 layouts
= mRules
->layouts
;
33 layout
= mLayoutKeys
[row
];
34 PNG
= mIconDir
+"/"+fixLayoutName(layout
)+".png";
35 SVG
= mIconDir
+"/"+fixLayoutName(layout
)+".svg";
36 isPNG
= QFile::exists(PNG
);
37 isSVG
= QFile::exists(SVG
);
40 isSVG
= flagSVG
.isValid();
49 flagSVG
.render(&painter
, QRectF(0, 0, 48, 22));
58 case Qt::TextAlignmentRole
: return int(Qt::AlignLeft
| Qt::AlignVCenter
);
59 case Qt::DecorationRole
:
60 if (col
== LAYOUT_COLUMN_FLAG
) return QIcon::fromTheme(layout
, icons
).pixmap(QSize(48, 32));
64 case LAYOUT_COLUMN_NAME
: return layouts
.value(layout
);
65 case LAYOUT_COLUMN_MAP
: return layout
;
75 Qt::ItemFlags
SrcLayoutModel::flags (const QModelIndex
&index
) const {
76 Qt::ItemFlags defaultFlags
= QAbstractTableModel::flags(index
);
77 return Qt::ItemIsDragEnabled
| Qt::ItemIsDropEnabled
| defaultFlags
;
81 QMimeData
*SrcLayoutModel::mimeData (const QModelIndexList
&indexes
) const {
82 QMimeData
*mimeData
= QAbstractTableModel::mimeData(indexes
);
83 mimeData
->setText(indexes
.first().data().toString());
88 bool SrcLayoutModel::dropMimeData (const QMimeData
*, Qt::DropAction action
, int, int, const QModelIndex
&) {
89 if (action
== Qt::IgnoreAction
) return false;
95 ////////////////////////////////////////////////////////////////////////////////
96 QVariant
DstLayoutModel::headerData (int section
, Qt::Orientation orientation
, int role
) const {
97 if (role
!= Qt::DisplayRole
) return QVariant();
98 if (orientation
== Qt::Horizontal
) {
100 case 1: return tr("Layout Name");
101 case 2: return tr("Map");
102 case 3: return tr("Variant");
103 case 4: return tr("Label");
111 QVariant
DstLayoutModel::data (const QModelIndex
&index
, int role
) const {
112 QHash
<QString
, QString
> layouts
;
116 if (!index
.isValid()) return QVariant();
117 col
= index
.column();
119 layouts
= mRules
->layouts
;
120 lu
= mJrugr
->layouts
[row
];
122 case Qt::TextAlignmentRole
: return int(Qt::AlignLeft
| Qt::AlignVCenter
);
123 case Qt::DecorationRole
:
124 if (col
== LAYOUT_COLUMN_FLAG
) {
125 return QIcon::fromTheme(lu
.layout
, QIcon(mIconDir
+"/"+fixLayoutName(lu
.layout
)+".png")).pixmap(QSize(48, 32));
128 case Qt::DisplayRole
:
130 case LAYOUT_COLUMN_NAME
: return layouts
[lu
.layout
];
131 case LAYOUT_COLUMN_MAP
: return lu
.layout
;
132 case LAYOUT_COLUMN_VARIANT
: return lu
.variant
;
133 case LAYOUT_COLUMN_DISPLAY_NAME
: return lu
.getDisplayName();
143 bool DstLayoutModel::dropMimeData (const QMimeData
*, Qt::DropAction action
, int, int, const QModelIndex
&) {
144 if (action
== Qt::IgnoreAction
) return false;
150 Qt::ItemFlags
DstLayoutModel::flags (const QModelIndex
&index
) const {
151 Qt::ItemFlags defaultFlags
= QAbstractTableModel::flags(index
);
153 if (mJrugr
->layouts
.size() >= 4) return Qt::ItemIsDragEnabled
| defaultFlags
;
154 return Qt::ItemIsDropEnabled
| Qt::ItemIsDragEnabled
| defaultFlags
;
158 ////////////////////////////////////////////////////////////////////////////////
159 QVariant
XkbOptionsModel::data (const QModelIndex
&index
, int role
) const {
162 const XkbOptionGroup
*xkbGroup
;
164 if (!index
.isValid()) return QVariant();
167 case Qt::DisplayRole
:
168 if (!index
.parent().isValid()) return mRules
->optionGroups
.values()[row
].description
;
169 groupRow
= index
.parent().row();
170 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
171 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
172 return xkbGroup
->options
[row
].description
;
173 case Qt::CheckStateRole
:
174 if (index
.parent().isValid()) {
175 groupRow
= index
.parent().row();
176 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
177 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
178 return mXkbConfig
->options
.indexOf(xkbGroup
->options
[row
].name
) == -1 ? Qt::Unchecked
: Qt::Checked
;
187 int XkbOptionsModel::rowCount (const QModelIndex
&parent
) const {
188 if (!parent
.isValid()) return mRules
->optionGroups
.count();
189 if (!parent
.parent().isValid()) return mRules
->optionGroups
.values()[parent
.row()].options
.count();
194 QModelIndex
XkbOptionsModel::parent (const QModelIndex
&index
) const {
195 if (!index
.isValid()) return QModelIndex();
196 if (index
.internalId() < 100) return QModelIndex();
197 return createIndex(((index
.internalId()-index
.row())/100)-1, index
.column());
201 QModelIndex
XkbOptionsModel::index (int row
, int column
, const QModelIndex
&parent
) const {
202 if (!parent
.isValid()) return createIndex(row
, column
);
203 return createIndex(row
, column
, (100*(parent
.row()+1))+row
);
207 Qt::ItemFlags
XkbOptionsModel::flags (const QModelIndex
&index
) const {
208 if (!index
.isValid()) return 0;
209 if (!index
.parent().isValid()) return Qt::ItemIsEnabled
;
210 return Qt::ItemIsEnabled
| Qt::ItemIsUserCheckable
;
214 bool XkbOptionsModel::setData (const QModelIndex
&index
, const QVariant
&value
, int role
) {
216 const XkbOptionGroup
*xkbGroup
;
217 const XkbOption
*option
;
218 int groupRow
= index
.parent().row();
220 if (groupRow
< 0) return false;
221 xkbGroupNm
= mRules
->optionGroups
.keys()[groupRow
];
222 xkbGroup
= &mRules
->optionGroups
[xkbGroupNm
];
223 option
= &xkbGroup
->options
[index
.row()];
224 if (value
.toInt() == Qt::Checked
) {
225 if (xkbGroup
->exclusive
) {
226 // clear if exclusive (TODO: radiobutton)
227 int idx
= mXkbConfig
->options
.indexOf(QRegExp(xkbGroupNm
+".*"));
229 for (int f
= 0; f
< xkbGroup
->options
.count(); ++f
) {
230 if (xkbGroup
->options
[f
].name
== mXkbConfig
->options
[idx
]) {
231 setData(createIndex(f
, index
.column(), (quint32
)index
.internalId()-index
.row()+f
), Qt::Unchecked
, role
);
235 //m_kxkbConfig->m_options.removeAt(idx);
236 //idx = m_kxkbConfig->m_options.indexOf(QRegExp(xkbGroupNm+".*"));
239 if (mXkbConfig
->options
.indexOf(option
->name
) < 0) mXkbConfig
->options
.append(option
->name
);
241 mXkbConfig
->options
.removeAll(option
->name
);
243 emit
dataChanged(index
, index
);
248 void XkbOptionsModel::gotoGroup (const QString
&group
, QTreeView
*view
) {
249 int index
= mRules
->optionGroups
.keys().indexOf(group
);
252 QModelIndex modelIdx
= createIndex(index
, 0);
253 view
->setExpanded(modelIdx
, true);
254 view
->scrollTo(modelIdx
, QAbstractItemView::PositionAtTop
);
255 view
->selectionModel()->setCurrentIndex(modelIdx
, QItemSelectionModel::Current
);
256 view
->setFocus(Qt::OtherFocusReason
);
258 qDebug() << "XkbOptionsModel:can't scroll to group" << group
;