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