summaryrefslogtreecommitdiffstats
path: root/archiveproxy.cpp
blob: 02494593d2a018c5f1256f7ffb968c8babe2a268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QStringList>
#include <QRegExp>
#include <QList>

#include <QDebug>

#include "archiveproxy.h"
#include "moviemodel.h"

ArchiveProxy::ArchiveProxy(QObject *parent) : QSortFilterProxyModel(parent){};

void ArchiveProxy::setFilter(const QString &filter, FilterMode mode){
	mFilterMode = mode;
	switch(mFilterMode){
		case NoFilter:
			mGenreFilter = QString();
			mActorFilter = QString();
			mTitleFilter = QString();
			break;
		case GenreFilter:
			mGenreFilter = filter;
			break;
		case ActorFilter:
			mActorFilter = filter;
			break;
		case TitleFilter:
			mTitleFilter = filter.toLower();
			break;
	}
	invalidate();
}

bool ArchiveProxy::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const{
	if(mFilterMode == NoFilter){
		return true;
	}
	MovieModel *model = static_cast<MovieModel*>(sourceModel());
	Q_ASSERT(model != 0);
	if(mFilterMode == ActorFilter){
		QModelIndex idx = model->index(sourceRow, 0, QModelIndex());
		QStringList actors = model->data(idx, MovieModel::ActorsRole).toStringList();
		return actors.contains(mActorFilter);
	}
	if(mFilterMode == GenreFilter){
		QModelIndex idx = model->index(sourceRow, 4, QModelIndex());
		QString genre = idx.data().toString();
		return (genre == mGenreFilter);
	}
	if(mFilterMode == TitleFilter){
		QModelIndex idx = model->index(sourceRow, 0, QModelIndex());
		QString title = idx.data().toString();
		QRegExp re(mTitleFilter);
		return( re.indexIn(title) != -1);
	}
	return true;
}

void ArchiveProxy::clearFilter(){
	mFilterMode = NoFilter;
	mGenreFilter = QString();
	mActorFilter = QString();
	mTitleFilter = QString();
	invalidate();
}

bool ArchiveProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const{
	if(left.column() > 0){
		return QSortFilterProxyModel::lessThan(left, right);
	}
	QString l = left.data().toString();
	QString r = right.data().toString();
	QRegExp nos("(\\d+)");
	QList<int> lnos, rnos;
	int pos = 0;
	bool dummy;
	while((pos = nos.indexIn(l, pos)) != -1){
		lnos << nos.cap(1).toInt(&dummy);	
		pos += nos.matchedLength();
	}
	pos = 0;
	while((pos = nos.indexIn(r, pos)) != -1){
		rnos << nos.cap(1).toInt(&dummy);	
		pos += nos.matchedLength();
	}
	QRegExp baseRe("(.*)\\s+\\d+");
	baseRe.indexIn(l);
	QString lbase = baseRe.cap(1);
	baseRe.indexIn(r);
	QString rbase = baseRe.cap(1);
	if((lnos.size() == 1) && (rnos.size() == 1)){
		if(lbase == rbase){
			return (lnos.at(0) < rnos.at(0));
		}
	}
	if((lnos.size() == 2) && (rnos.size() == 2)){
		if(lbase == rbase){
			return (lnos.at(1) < rnos.at(1));
		}	
	}
	return QSortFilterProxyModel::lessThan(left, right);
}