source: projects/punch-card/punch-card-editor/src/qpunchcard/card.h @ 44

Last change on this file since 44 was 44, checked in by sven, 10 years ago

Import of the Punch Card Editor Project.
This is a C++/Qt project (using qmake) that I've started this weekend.
Of course it's supposed to be released as open source.

I've tried to start with a clean (but now still empty, of course)
directory structure. There will come the sourcecode for a complete
AVR ATmega microcontroller punch card device controller, soon.
I'm planing to finish this editing program and to implement the
communication protocol (over TTY, using some platform indepentent library).
Unfortunately that will take some time (and I don't have much time
any more...)

-- Sven @ workstation

File size: 3.7 KB
Line 
1#ifndef CARD_H
2#define CARD_H
3
4#include <QBitArray>
5#include <QHash>
6#include <QVector>
7#include <QPointer>
8#include <QDebug>
9#include <QUndoStack>
10#include <QFile>
11
12namespace QPunchCard {
13        class Column; class Card; class Deck;
14};
15
16#include "format.h"
17
18namespace QPunchCard {
19
20/**
21 * A Column is a 12 bit storage...
22 **/
23class Column : public QBitArray {
24public:
25        Column() : QBitArray(13) {}
26
27        Column(int value) : QBitArray(13) {
28                // TODO: range check, throw exception if bad
29                for(int s=0; s<12; s++) {
30                        setBit(s, (1 << s) & value);
31                        //setBit(s, (value >> s) & 0x01);
32                }
33        }
34
35
36        operator int() const {
37                int ret=0;
38                for(int s=0; s<12; s++)
39                        ret |= testBit(s) << s;
40                return ret;
41        }
42};
43
44/**
45 * This is a punch card.
46 **/
47class Card : public QObject {
48        Q_OBJECT // must be qobject for signals/slots and QPointer usage
49
50public:
51        Card() {};
52        QHash<QString,QString> properties;
53
54        Column column[80];
55        Column& get(int x) {
56                if(x < 0 || x > 80)
57                        throw "Out of range exception";
58                return column[x];
59        }
60        Column& operator[](int x) { return get(x); }
61        const Column& operator[](int x) const { return column[x]; }
62
63        /*Card& operator=(const Card& other) {
64                // copy assignment, etc.
65                for(int x=0; x<80; x++) column[x] = other[x];
66                return *this;
67        }*/
68
69        ~Card() {};
70
71signals:
72        // auf das level will echt keiner runter!
73        void dataChanged(int left_index, int right_index);
74        // wenn signal: klasse muss QObject sein =>
75        //              kein copy constructor möglich =>
76        //              File = QVector<Card*>
77};
78
79
80/**
81 * File uses the strategy pattern to switch between different
82 * I/O formats (using the FileFormat class).
83 **/
84class Deck : public QObject, public QVector< QPointer<Card> > {
85        Q_OBJECT
86
87        QPointer<FileFormat> created_format;
88        void init();
89
90public:
91        typedef QVector< QPointer<Card> >::iterator iterator;
92        QPointer<QUndoStack> undo;
93
94        /// Create empty
95        Deck() : QVector< QPointer<Card> >() { init(); }
96        ~Deck() {};
97        /// Create from file/stream/etc., that is, calls format->read()
98        Deck(FileFormat* format) : QVector< QPointer<Card> >(), created_format(format) { init(); }
99        /// Save with same device/format as created
100        bool save(QFile& file) { return save(created_format, file); }
101        /// Save
102        bool save(FileFormat* format, QFile& file);
103
104
105        bool canSave() const { return created_format; }
106        void setFormat(FileFormat* format) { created_format = format; } // TODO: was ist mit altem format?
107        //File* getFile() { if(created_format && created_format->file) return created_format->file; }
108
109        bool isModified() const { return !undo->isClean(); }
110
111        /// checks whether position is valid
112        bool isValid(int i) const { return i >= 0 && i < count(); }
113
114signals:
115        void cardCountChanged(int newCardCount); // wer braucht das?
116
117        // das hier ist das FAVORISIERTE FORMAT:
118        void contentsChanged(int lowerCard, int upperCard);
119
120
121        void modified(bool modified);
122        //void redoAvailable(bool available);
123        //void undoAvailable(bool available);
124        //void undoCommandAdded();
125
126public slots:
127        // changeCard soll so nicht genutzt werden -- direkt auf den Daten arbeiten
128        // und dann contentsChanged() abfeuern oder so
129        //void changeCard(int card, const Column& new_value);
130
131        //void redo() {}
132        //void undo() {}
133        //void setModified(bool modified) {}
134};
135
136
137
138/****************************************************************************
139  debug functions
140 ***************************************************************************/
141
142 QDebug operator<<(QDebug dbg, const Column &c);
143 QDebug operator<<(QDebug dbg, const Card &c);
144 QDebug operator<<(QDebug dbg, const Deck &c);
145
146
147/****************************************************************************
148  inline functions
149 ***************************************************************************/
150
151
152
153}; // Namespace
154#endif // CARD_H
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License