1 | #include "deck.h" |
---|
2 | |
---|
3 | using namespace QPunchCard; |
---|
4 | |
---|
5 | Deck::Deck(QObject* parent) : QObject(parent), undo_stack(this) { |
---|
6 | init(); |
---|
7 | } |
---|
8 | |
---|
9 | Deck::Deck(const FileFormat* format, QObject* parent) : |
---|
10 | QObject(parent), undo_stack(this), format(format) { |
---|
11 | init(); |
---|
12 | } |
---|
13 | |
---|
14 | void Deck::init() { |
---|
15 | // undo ist jetzt direktes member, nicht mher noetig |
---|
16 | //undo = new QUndoStack(this); |
---|
17 | } |
---|
18 | |
---|
19 | bool Deck::save() { |
---|
20 | return file ? save(*file) : false; |
---|
21 | } |
---|
22 | |
---|
23 | bool Deck::save(QFile& file) { |
---|
24 | return save(format.data(), file); |
---|
25 | } |
---|
26 | |
---|
27 | bool Deck::save(const FileFormat* format, QFile& file) { |
---|
28 | return format ? format->write(file, *this) : false; |
---|
29 | } |
---|
30 | |
---|
31 | bool Deck::read() { |
---|
32 | return file ? read(*file) : false; |
---|
33 | } |
---|
34 | |
---|
35 | bool Deck::read(QFile& file) { |
---|
36 | return read(format.data(), file); |
---|
37 | } |
---|
38 | |
---|
39 | bool Deck::read(const FileFormat* format, QFile& file) { |
---|
40 | return format ? format->read(file, *this) : false; |
---|
41 | } |
---|
42 | |
---|
43 | void Deck::setFormat(const FileFormat* new_format) { |
---|
44 | format = QSharedPointer<const FileFormat>(new_format); |
---|
45 | } |
---|
46 | |
---|
47 | void Deck::setFile(QFile* file) { |
---|
48 | this->file = QSharedPointer<QFile>(file); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | bool Deck::isValid(int i) { |
---|
53 | return createIndex(i).isValid(); |
---|
54 | } |
---|
55 | |
---|
56 | DeckIndex Deck::createIndex(int i) { |
---|
57 | return DeckIndex(this, i); |
---|
58 | } |
---|
59 | |
---|
60 | void Deck::emitChanged(DeckIndex lower, DeckIndex upper) { |
---|
61 | emit contentsChanged(lower, upper); |
---|
62 | } |
---|
63 | |
---|
64 | bool Deck::insert(DeckIndex after) { |
---|
65 | // TODO: Undo Redo Framework |
---|
66 | if(!after.assertDeck(this)) |
---|
67 | return false; |
---|
68 | DeckIndex new_index = createIndex(after + 1); |
---|
69 | qDebug("Inserting card at %i", (int)new_index.normalized()); |
---|
70 | cards.insert( new_index.normalized(), Card() ); |
---|
71 | qDebug() << "Deck is now:" << *this; |
---|
72 | emit contentsChanged(after, createIndex(count()) ); |
---|
73 | return true; |
---|
74 | } |
---|
75 | |
---|
76 | bool Deck::insertTimes(DeckIndex after, int times) { |
---|
77 | // ja, das ist bloed, aber reicht erst mal: |
---|
78 | for(;times>0;times--) insert(++after); |
---|
79 | return true; |
---|
80 | } |
---|
81 | |
---|
82 | void Deck::append(Card card) { |
---|
83 | // man ist das bloed: |
---|
84 | cards.push_back(card); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | bool Deck::move(DeckIndex /*before*/, DeckIndex /*after*/) { |
---|
89 | // asdasdas |
---|
90 | return true; |
---|
91 | // emit cardChanged |
---|
92 | } |
---|
93 | |
---|
94 | bool Deck::erase(DeckIndex from, DeckIndex to) { |
---|
95 | qDebug() << "Deck: Erasing from" << from << " to " << to; |
---|
96 | // main->deck->erase( main->deck->begin() += new_max_blocks+1, |
---|
97 | // main->deck->begin() += end_block_number+1 ); |
---|
98 | return true; |
---|
99 | // emit cardChanged |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | QDebug QPunchCard::operator<<(QDebug dbg, const Deck &c) { |
---|
106 | dbg.nospace() << "QPunchCard::File object with [" << c.count() << "] cards:\n\n"; |
---|
107 | for(int i = 0; i < c.size(); i++) { |
---|
108 | dbg.nospace() << "Card no. #" << i << " (" << c.at(i) << ")\n"; |
---|
109 | dbg.nospace() << c.at(i); |
---|
110 | dbg.nospace() << '\n'; |
---|
111 | } |
---|
112 | return dbg.nospace(); |
---|
113 | } |
---|
114 | |
---|
115 | QDebug QPunchCard::operator<<(QDebug dbg, const DeckIndex &c) { |
---|
116 | dbg.nospace() << "[QPunchCard::DeckIndex: i=" << c.asInt() << ". Has deck: " << c.hasDeck() << "]"; |
---|
117 | return dbg.nospace(); |
---|
118 | } |
---|