File indexing completed on 2024-04-21 03:50:59

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "vocabularyfilter.h"
0007 
0008 #include "vocabularymodel.h"
0009 
0010 VocabularyFilter::VocabularyFilter(QObject *parent)
0011     : QSortFilterProxyModel(parent)
0012     , m_model(nullptr)
0013 {
0014     // do not use capitalization for searches
0015     setSortCaseSensitivity(Qt::CaseInsensitive);
0016     setFilterCaseSensitivity(Qt::CaseInsensitive);
0017     // sort locale aware: at least puts umlauts and accents in the right position.
0018     // Not sure about languages that are more different.
0019     // Also depends on the current locale.
0020     setSortLocaleAware(true);
0021 
0022     // eventually accept only one language if so desired...
0023     setFilterKeyColumn(-1);
0024 }
0025 
0026 QModelIndex VocabularyFilter::appendEntry(KEduVocExpression *expression)
0027 {
0028     if (!m_model) {
0029         return QModelIndex();
0030     }
0031     return mapFromSource(m_model->appendEntry(expression));
0032 }
0033 
0034 void VocabularyFilter::setSourceModel(VocabularyModel *model)
0035 {
0036     QSortFilterProxyModel::setSourceModel(model);
0037     m_model = model;
0038 }
0039 
0040 void VocabularyFilter::setSearchString(const QString &expression)
0041 {
0042     m_filterString = expression;
0043     invalidateFilter();
0044 }
0045 
0046 bool VocabularyFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0047 {
0048     if (m_filterString.isEmpty()) {
0049         return true;
0050     }
0051 
0052     int columns = m_model->columnCount(QModelIndex());
0053     for (int i = 0; i < columns; i += VocabularyModel::EntryColumnsMAX) {
0054         QModelIndex index = sourceModel()->index(sourceRow, i, sourceParent);
0055         if (sourceModel()->data(index).toString().contains(m_filterString, Qt::CaseInsensitive)) {
0056             return true;
0057         }
0058     }
0059     return false;
0060 }
0061 
0062 KEduVocLesson *VocabularyFilter::lesson()
0063 {
0064     if (m_model) {
0065         return m_model->lesson();
0066     }
0067     return nullptr;
0068 }
0069 
0070 #include "moc_vocabularyfilter.cpp"