File indexing completed on 2024-04-14 03:40:36

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool...
0003     SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
0004     SPDX-FileCopyrightText: 2005 Paul Temple <paul.temple@gmx.net>
0005     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0006     SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "entrylistmodel.h"
0012 
0013 #include "entry.h"
0014 
0015 EntryListModel::EntryListModel(const EntryList &list)
0016     : _list(list)
0017 {
0018 }
0019 
0020 EntryList EntryListModel::entryList() const
0021 {
0022     return _list;
0023 }
0024 
0025 Qt::ItemFlags EntryListModel::flags(const QModelIndex &index) const
0026 {
0027     return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
0028 }
0029 
0030 bool EntryListModel::setData(const QModelIndex &index, const QVariant &value, int role)
0031 {
0032     if (role == Qt::EditRole) {
0033         const QString &separator = _list[index.row()]->outputListDelimiter;
0034 
0035         switch (index.column()) {
0036         case 0:
0037             _list[index.row()]->Word = value.toString();
0038             break;
0039         case 1:
0040             _list[index.row()]->Readings = value.toString().split(separator);
0041             break;
0042         case 2:
0043             _list[index.row()]->Meanings = value.toString().split(separator);
0044             break;
0045         }
0046     }
0047 
0048     return false;
0049 }
0050 
0051 void EntryListModel::setEntryList(const EntryList &list)
0052 {
0053     _list = list;
0054     Q_EMIT layoutChanged();
0055 }
0056 
0057 int EntryListModel::rowCount(const QModelIndex &parent) const
0058 {
0059     return parent.isValid() ? 0 : _list.size();
0060 }
0061 
0062 int EntryListModel::columnCount(const QModelIndex &parent) const
0063 {
0064     return parent.isValid() ? 0 : 3;
0065 }
0066 
0067 QVariant EntryListModel::data(const QModelIndex &index, int role) const
0068 {
0069     // kDebug() << "Retrieving data at line" << index.row();
0070     switch (role) {
0071     /* DisplayRole and EditRole have the same output */
0072     case Qt::DisplayRole:
0073     case Qt::EditRole: {
0074         switch (index.column()) {
0075         case 0:
0076             return _list.at(index.row())->getWord();
0077             break;
0078         case 1:
0079             return _list.at(index.row())->getReadings();
0080             break;
0081         case 2:
0082             return _list.at(index.row())->getMeanings();
0083             break;
0084         }
0085 
0086         break;
0087     }
0088     }
0089 
0090     return {};
0091 }
0092 
0093 #include "moc_entrylistmodel.cpp"