Changeset 52 in projects for punch-card/punch-card-editor/src/qpunchcard/card.h
- Timestamp:
- Dec 16, 2009, 2:47:25 AM (10 years ago)
- Location:
- punch-card/punch-card-editor
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
punch-card/punch-card-editor
-
Property
svn:ignore
set to
-
Property
svn:ignore
set to
-
punch-card/punch-card-editor/src/qpunchcard/card.h
r49 r52 14 14 15 15 namespace QPunchCard { 16 class Column; class Card; classDeck; class DeckIndex;16 class Column; class Deck; class DeckIndex; 17 17 }; 18 19 #include "format.h"20 //#include "codec.h"21 18 22 19 namespace QPunchCard { … … 29 26 Column() : QBitArray(13) {} 30 27 28 // TODO: int-Casting komplett entfernen. Das ist nicht Sinn 29 // der Sache bei der *Modellierung*, sondern eben eher 30 // beim Import/Export. Natuerlich *kann* man hier Methoden 31 // fuer sowas bereitstellen. 31 32 Column(int value) : QBitArray(13) { 32 33 // TODO: range check, throw exception if bad … … 36 37 } 37 38 } 38 39 39 40 40 operator int() const { … … 49 49 * This is a punch card. 50 50 **/ 51 class Card : public QObject { 52 Q_OBJECT // must be qobject for signals/slots and QPointer usage 51 class Card { 53 52 54 53 public: … … 78 77 79 78 ~Card() {}; 80 81 signals:82 // auf das level will echt keiner runter!83 void dataChanged(int left_index, int right_index);84 // wenn signal: klasse muss QObject sein =>85 // kein copy constructor möglich =>86 // File = QVector<Card*>87 79 }; 88 80 89 90 /**91 * File uses the strategy pattern to switch between different92 * I/O formats (using the FileFormat class).93 **/94 class Deck : public QObject, public QList< QPointer<Card> > {95 Q_OBJECT96 97 QSharedPointer<const FileFormat> created_format;98 void init();99 100 public:101 typedef QList< QPointer<Card> >::iterator iterator;102 QPointer<QUndoStack> undo;103 104 // Creation and I/O handling ===================105 /// Create empty106 Deck() : QList< QPointer<Card> >() { init(); }107 ~Deck() {};108 /// Create from file/stream/etc., that is, calls format->read()109 Deck(const FileFormat* format) : QList< QPointer<Card> >(), created_format(format) { init(); }110 /// Save with same device/format as created111 bool save(QFile& file) { return save(created_format.data(), file); }112 /// Save113 bool save(const FileFormat* format, QFile& file);114 bool read(QFile& file) { return read(created_format.data(), file); }115 bool read(const FileFormat* format, QFile& file);116 117 bool canSave() const { return created_format; }118 void setFormat(const FileFormat* format);119 //File* getFile() { if(created_format && created_format->file) return created_format->file; }120 121 bool isModified() const { return !undo->isClean(); }122 123 // index handling ======================124 inline bool isValid(int i);125 inline DeckIndex createIndex(int i);126 127 // Bearbeitungsfunktionen128 bool insert(DeckIndex after); // neue Karte hinzufuegen129 bool insertTimes(DeckIndex after, int times = 1);130 bool move(DeckIndex from, DeckIndex to);131 bool erase(DeckIndex from, DeckIndex to);132 133 // Quick & dirty134 inline void emitChanged(DeckIndex lower, DeckIndex upper);135 signals:136 void cardCountChanged(int newCardCount); // wer braucht das?137 138 // das hier ist das FAVORISIERTE FORMAT:139 void contentsChanged(DeckIndex lowerCard, DeckIndex upperCard);140 141 142 void modified(bool modified);143 //void redoAvailable(bool available);144 //void undoAvailable(bool available);145 //void undoCommandAdded();146 147 public slots:148 //void redo() {}149 //void undo() {}150 //void setModified(bool modified) {}151 };152 153 // Java-style iterator154 typedef QListIterator< QPointer<Card> > DeckIterator;155 156 157 /**158 * CardIndex feels like an int (normalized)159 **/160 class DeckIndex {161 private:162 QPointer<Deck> deck;163 int i;164 public:165 // behaves like int, copy constructor and operator= will be created automatically166 DeckIndex(int i = 0) : i(i) {}167 operator int() const { return normalized(); }168 int asInt() const { return normalized(); }169 170 DeckIndex(Deck* d, int i) : deck(d), i(i) { if(!deck) qDebug() << "NULL DECK!"; }171 bool isValid() const { return deck ? (i >= 0 && i < deck->count()) : false; }172 173 // "normalisierung" = in Deck Bounds174 int normalized() const { if(!deck) return i; if(i < 0) return 0; else if(i >= deck->count()) return deck->count()-1; else return i; }175 void normalize() { i = normalized(); }176 bool canNormalize() const { return deck; }177 178 // special positions179 bool isTop() const { return i < 0; }180 bool isEnd() const { return i >= deck->count(); }181 bool isFirst() const { return i == 0; }182 bool isLast() const { return i == deck->count(); }183 184 const QPointer<Deck> getDeck() const { return deck; }185 void setDeck(Deck* deck) { this->deck = deck; }186 bool assertDeck(Deck* fallback) { if(!deck) deck=fallback; return deck == fallback; }187 bool hasDeck() const { return deck; }188 189 // ein paar abkuerzungen190 /// @returns null wenn nicht normalisierbar (also letztlich kein Deck da ist)191 QPointer<Card> getCard() const { return canNormalize() ? deck->at(normalized()) : NULL; }192 193 DeckIndex& operator++() { i++; return *this; }194 DeckIndex& operator+=(int x) { i+= x; return *this; }195 };196 81 197 82 /**************************************************************************** … … 199 84 ***************************************************************************/ 200 85 201 QDebug operator<<(QDebug dbg, const Column &c); 202 QDebug operator<<(QDebug dbg, const Card &c); 203 QDebug operator<<(QDebug dbg, const Deck &c); 204 QDebug operator<<(QDebug dbg, const DeckIndex &c); 86 QDebug operator<<(QDebug dbg, const Column &c); 87 QDebug operator<<(QDebug dbg, const Card &c); 205 88 206 207 /****************************************************************************208 inline functions209 ***************************************************************************/210 211 inline bool Deck::isValid(int i) { return createIndex(i).isValid(); }212 inline DeckIndex Deck::createIndex(int i) { return DeckIndex(this, i); }213 inline void Deck::emitChanged(DeckIndex lower, DeckIndex upper) {214 emit contentsChanged(lower, upper);215 }216 89 217 90 }; // Namespace
Note: See TracChangeset
for help on using the changeset viewer.