1 #include "cardlist.h"
2 
3 #include "carddatabase.h"
4 #include "carditem.h"
5 
6 #include <algorithm>
7 
CardList(bool _contentsKnown)8 CardList::CardList(bool _contentsKnown) : QList<CardItem *>(), contentsKnown(_contentsKnown)
9 {
10 }
11 
findCard(const int id,const bool remove,int * position)12 CardItem *CardList::findCard(const int id, const bool remove, int *position)
13 {
14     if (!contentsKnown) {
15         if (empty())
16             return 0;
17         CardItem *temp = at(0);
18         if (remove)
19             removeAt(0);
20         if (position)
21             *position = id;
22         return temp;
23     } else
24         for (int i = 0; i < size(); i++) {
25             CardItem *temp = at(i);
26             if (temp->getId() == id) {
27                 if (remove)
28                     removeAt(i);
29                 if (position)
30                     *position = i;
31                 return temp;
32             }
33         }
34     return 0;
35 }
36 
37 class CardList::compareFunctor
38 {
39 private:
40     int flags;
41 
42 public:
compareFunctor(int _flags)43     explicit compareFunctor(int _flags) : flags(_flags)
44     {
45     }
operator ()(CardItem * a,CardItem * b) const46     inline bool operator()(CardItem *a, CardItem *b) const
47     {
48         if (flags & SortByType) {
49             QString t1 = a->getInfo() ? a->getInfo()->getMainCardType() : QString();
50             QString t2 = b->getInfo() ? b->getInfo()->getMainCardType() : QString();
51             if ((t1 == t2) && (flags & SortByName))
52                 return a->getName() < b->getName();
53             return t1 < t2;
54         } else
55             return a->getName() < b->getName();
56     }
57 };
58 
sort(int flags)59 void CardList::sort(int flags)
60 {
61     compareFunctor cf(flags);
62     std::sort(begin(), end(), cf);
63 }
64