parley
kvtsortfiltermodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kvtsortfiltermodel.h"
00024 #include "kvttablemodel.h"
00025 #include "prefs.h"
00026
00027 #include <keduvocexpression.h>
00028 #include <keduvoclesson.h>
00029
00030 #include <KDebug>
00031
00032 KVTSortFilterModel::KVTSortFilterModel(QObject *parent) : QSortFilterProxyModel(parent)
00033 {
00034
00035 setSortCaseSensitivity(Qt::CaseInsensitive);
00036
00037 setSortLocaleAware ( true );
00038
00039 m_sourceModel = 0;
00040 m_searchFilter = QRegExp();
00041 m_wordType = QRegExp();
00042 m_restoreNativeOrder = false;
00043 m_lessonSelection = Prefs::lessonEditingSelection();
00044
00045
00046 }
00047
00048 void KVTSortFilterModel::setSourceModel(KVTTableModel * sourceModel)
00049 {
00050 m_sourceModel = sourceModel;
00051 QSortFilterProxyModel::setSourceModel(sourceModel);
00052 }
00053
00054 KVTTableModel * KVTSortFilterModel::sourceModel() const
00055 {
00056 return m_sourceModel;
00057 }
00058
00059 void KVTSortFilterModel::setSearchRegExp(const QRegExp& filter)
00060 {
00061 m_searchFilter = filter;
00062 invalidateFilter();
00063 }
00064
00065 void KVTSortFilterModel::setWordType(const QRegExp& wordType)
00066 {
00067 m_wordType = wordType;
00068 invalidateFilter();
00069 }
00070
00074 bool KVTSortFilterModel::checkLesson(int sourceRow) const
00075 {
00076 switch (m_lessonSelection) {
00077 case Prefs::EnumLessonEditingSelection::CurrentLesson:
00078 return m_sourceModel->document()->entry(sourceRow)->lesson()
00079 == m_sourceModel->document()->currentLesson();
00080 break;
00081 case Prefs::EnumLessonEditingSelection::LessonsInQuery:
00082 return m_sourceModel->document()->lesson(
00083 m_sourceModel->document()->entry(sourceRow)->lesson()
00084 ).inPractice();
00085 break;
00086 case Prefs::EnumLessonEditingSelection::AllLessons:
00087 return true;
00088 break;
00089 }
00090 return false;
00091 }
00092
00093
00094 void KVTSortFilterModel::slotSearch(const QString& s)
00095 {
00096
00097 if (s.length() <= 1) {
00098 m_wordType = QRegExp();
00099 setSearchRegExp(QRegExp());
00100 return;
00101 }
00102
00103
00104
00105 QStringList searchterms = s.split(' ', QString::SkipEmptyParts);
00106
00107 QStringList types = searchterms.filter("type:", Qt::CaseInsensitive);
00108
00109 foreach (QString type, types) {
00110 searchterms.removeAt(searchterms.indexOf(type));
00111 }
00112
00113 types.replaceInStrings("type:", "", Qt::CaseInsensitive);
00114 kDebug() << "Search types: " << '(' + types.join("|") + ')';
00115 m_wordType = QRegExp('(' + types.join("|") + ')', Qt::CaseInsensitive);
00116
00117 QRegExp searchRegExp = QRegExp('(' + searchterms.join("|") + ')', Qt::CaseInsensitive);
00118
00119 setSearchRegExp(searchRegExp);
00120 }
00121
00122
00126 bool KVTSortFilterModel::checkSearch(int sourceRow) const
00127 {
00129 for (int i=0; i < m_sourceModel->document()->identifierCount(); i++) {
00130 if ( m_sourceModel->document()->
00131 entry(sourceRow)->translation(i).
00132 text().contains(m_searchFilter) ) {
00133 return true;
00134 }
00135 }
00136 return false;
00137 }
00138
00139
00140 bool KVTSortFilterModel::checkWordType(int sourceRow) const
00141 {
00142 if (m_wordType.isEmpty()) {
00143 return true;
00144 }
00145
00146 for (int i=0; i < m_sourceModel->document()->identifierCount(); i++) {
00147 if ( m_sourceModel->document()->
00148 entry(sourceRow)->translation(i).
00149 type().contains(m_wordType) ) {
00150 return true;
00151 }
00152 }
00153 return false;
00154 }
00155
00156
00163 bool KVTSortFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
00164 {
00165 Q_UNUSED(sourceParent);
00166
00167 if (!checkLesson(sourceRow)) {
00168 return false;
00169 }
00170
00171 if (!m_searchFilter.isEmpty()) {
00172 if (!checkSearch(sourceRow)) {
00173 return false;
00174 }
00175 }
00176
00177 if (!m_wordType.isEmpty()) {
00178 if (!checkWordType(sourceRow)) {
00179 return false;
00180 }
00181 }
00182
00183 return true;
00184 }
00185
00186 bool KVTSortFilterModel::lessThan(const QModelIndex & left, const QModelIndex & right) const
00187 {
00188 if (m_restoreNativeOrder) {
00189 return sourceModel()->index(right.row(), right.column(), QModelIndex()).row() <
00190 sourceModel()->index(left.row(), left.column(), QModelIndex()).row();
00191 } else {
00192 return QSortFilterProxyModel::lessThan(left, right);
00193 }
00194 }
00195
00196 void KVTSortFilterModel::restoreNativeOrder()
00197 {
00198 m_restoreNativeOrder = true;
00199 sort(-1, Qt::AscendingOrder);
00200 invalidate();
00201 m_restoreNativeOrder = false;
00202 }
00203
00204 void KVTSortFilterModel::setLessonSelection(int lessonSelection)
00205 {
00206 m_lessonSelection = lessonSelection;
00207 invalidate();
00208 }
00209
00210 void KVTSortFilterModel::slotCurrentLessonChanged(int )
00211 {
00212 if ( m_lessonSelection == Prefs::EnumLessonEditingSelection::CurrentLesson ) {
00213 invalidate();
00214 }
00215 }
00216
00217 void KVTSortFilterModel::slotLessonsInQueryChanged()
00218 {
00219 if ( m_lessonSelection == Prefs::EnumLessonEditingSelection::LessonsInQuery ) {
00220 invalidate();
00221 }
00222 }
00223
00224
00225 #include "kvtsortfiltermodel.moc"