Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
335 Kevin 1
#include "Gate_INPUT.h"
2
 
3
Gate_INPUT::Gate_INPUT(int gateID, gType type, int numInputs, int gateLevel)
4
    : Gate_BASE(gateID, type, numInputs, gateLevel)
5
{
6
    // Override base gate to limit to one output point only
7
    this->xSize = BASE_GATE_SIZE_X + ADDITONAL_INPUTS;
8
    this->ySize = BASE_GATE_SIZE_Y + ADDITONAL_INPUTS;
9
 
10
    outputPoint = QPointF(this->xSize, this->ySize / 2);
11
    gateOutputPoint = QPointF(this->scenePos().x() + this->xSize, this->scenePos().y() + this->ySize / 2);
12
 
13
    textRect = QRectF(BORDER_OFFSET, BORDER_OFFSET, xSize - BORDER_OFFSET * 3, ySize - BORDER_OFFSET * 2);
14
 
15
    createActions();
16
}
17
 
18
void Gate_INPUT::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
19
{
20
    Q_UNUSED(widget);
21
    Q_UNUSED(option);
22
 
23
#ifdef _DEBUG
24
    painter->save();
25
    painter->setPen((auxSelected) ? debugSelectedPen : debugPen);
26
    painter->setBrush(debugBrush);
27
    painter->drawRect(0, 0, xSize, ySize);
28
    painter->drawRect(textRect);
29
    if (numInputs != 0) {
30
        painter->setPen(debugErrorPen);
31
        painter->drawLine(0, 0, xSize, ySize);
32
        painter->drawLine(xSize, 0, 0, ySize);
33
    }
34
    painter->restore();
35
#endif
36
 
37
    if (auxSelected) painter->setPen(highlightedPen);
38
    else painter->setPen(defaultPen);
39
    painter->setBrush(defaultBrush);
40
 
41
    // Draw circles indicating I/O points
42
    painter->drawEllipse(outputPoint, INPUT_CIRCLE_SIZE, INPUT_CIRCLE_SIZE);
43
 
44
    // Draw gate outline
45
    QPointF points[5] = {
46
        QPointF(BORDER_OFFSET, BORDER_OFFSET),
47
        QPointF(xSize - BORDER_OFFSET * 2, BORDER_OFFSET),
48
        QPointF(xSize - BORDER_OFFSET, ySize / 2),
49
        QPointF(xSize - BORDER_OFFSET * 2, ySize - BORDER_OFFSET),
50
        QPointF(BORDER_OFFSET, ySize - BORDER_OFFSET)
51
    };
52
    painter->drawPolygon(points, 5);
53
 
54
    // Draw I/O lines
55
    painter->drawLine(outputPoint, QPointF(outputPoint.x() - BORDER_OFFSET, outputPoint.y()));
56
 
57
    // Draw text showing gate ID
58
    painter->setPen(defaultPen);
59
    painter->setFont(defaultFont);
60
    painter->drawText(textRect, Qt::AlignCenter, QString::number(gateID));
61
 
62
    // If enqueued, draw circle around gate ID
63
    if (enqueued) {
64
        painter->drawEllipse(textRect.center(), ENQUEUED_CIRCLE_WIDTH, ENQUEUED_CIRCLE_WIDTH);
65
    }
66
}
67
 
68
void Gate_INPUT::simulateToOutput()
69
{
70
    // Do nothing
71
 
72
    // Update connected wire values
73
    for (int i = 0; i < gateOutputWires.size(); i++) {
74
        gateOutputWires[i]->setValue(outputValue, outputFaultyValue, false);
75
    }
76
}
77
 
78
void Gate_INPUT::promptValue()
79
{
80
    // Prompt for the input value
81
    bool ok;
82
    QStringList options;
83
    options << "0" << "1" << "X";
84
    QString sel = QInputDialog::getItem(0, "Choose Value", "Enter Value:", options, 0, false, &ok);
85
    if (ok && !sel.isEmpty()) {
86
        if (sel == "0") {
87
            outputValue = logicValue_0;
88
            outputFaultyValue = logicValue_0;
89
        } else if (sel == "1") {
90
            outputValue = logicValue_1;
91
            outputFaultyValue = logicValue_1;
92
        } else {
93
            outputValue = logicValue_X;
94
            outputFaultyValue = logicValue_X;
95
        }
96
    }
97
 
98
    // Propogate to connected wires
99
    for (int i = 0; i < gateOutputWires.size(); i++) {
100
        gateOutputWires[i]->setValue(outputValue, outputFaultyValue, false);
101
    }
102
}
103
 
104
void Gate_INPUT::promptFaultyValue()
105
{
106
    // Prompt for the fault value
107
    bool ok;
108
    QStringList options;
109
    options << "0" << "1" << "X";
110
    QString sel = QInputDialog::getItem(0, "Choose Value", "Enter Value:", options, 0, false, &ok);
111
    if (ok && !sel.isEmpty()) {
112
        if (sel == "0") {
113
            outputValue = logicValue_1;
114
            outputFaultyValue = logicValue_0;
115
        } else if (sel == "1") {
116
            outputValue = logicValue_0;
117
            outputFaultyValue = logicValue_1;
118
        } else {
119
            outputValue = logicValue_X;
120
            outputFaultyValue = logicValue_X;
121
        }
122
    }
123
 
124
    // Propogate to connected wires
125
    for (int i = 0; i < gateOutputWires.size(); i++) {
126
        gateOutputWires[i]->setValue(outputValue, outputFaultyValue, false);
127
    }
128
}
129
 
130
void Gate_INPUT::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
131
{
132
    QMenu menu;
133
    menu.addAction(setInputValueAction);
134
    menu.addAction(setFaultValueAction);
135
    menu.exec(event->screenPos());
136
    event->accept();
137
}
138
 
139
void Gate_INPUT::createActions()
140
{
141
    setInputValueAction = new QAction("&Set Value", this);
142
    connect(setInputValueAction, SIGNAL(triggered()), this, SLOT(promptValue()));
143
 
144
    setFaultValueAction = new QAction("&Inject Fault", this);
145
    connect(setFaultValueAction, SIGNAL(triggered()), this, SLOT(promptFaultyValue()));
146
}