Skip to content

GUI Calculator example with Qt libraries #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(Calc)
add_subdirectory(CalcQt)
add_subdirectory(FeatureShowcase)
21 changes: 21 additions & 0 deletions examples/CalcQt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
project(CalcQt)

find_package(Qt4 COMPONENTS QtCore QtGui QtTest)

if(QT4_FOUND)
include(${QT_USE_FILE})
set(CALCQT_HEADERS CalcQt/CalculatorWidget.h)
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
include_directories(${CUKE_INCLUDE_DIRS} CalcQt)
add_library(CalcQt CalcQt/CalculatorWidget ${CALCQT_HEADERS_MOC})

if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostCalculatorQtSteps CalcQtFeatures/BoostCalculatorQtSteps)
target_link_libraries(BoostCalculatorQtSteps CalcQt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
endif()

set(CALCQT_SOURCES CalcQt/CalcQt.cpp CalcQt/CalculatorWidget.cpp)
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
target_link_libraries(calcqt ${QT_LIBRARIES})
endif()
11 changes: 11 additions & 0 deletions examples/CalcQt/CalcQt/CalcQt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <QApplication>

#include "CalculatorWidget.h"

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
app.setApplicationName("Qt Calculator");
CalculatorWidget widget;
widget.show();
return app.exec();
}
130 changes: 130 additions & 0 deletions examples/CalcQt/CalcQt/CalculatorWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include "CalculatorWidget.h"

#include <stdio.h>
#include <QGridLayout>
#include <QKeyEvent>
#include <QLabel>
#include <QPushButton>
#include <QSignalMapper>

CalculatorWidget::CalculatorWidget(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) {
QGridLayout *layout = new QGridLayout;
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);

QSizePolicy policy = sizePolicy();

displayLabel = new QLabel(this);
layout->addWidget(displayLabel, 0, 0, 1, 3);
displayLabel->setAutoFillBackground(true);
displayLabel->setBackgroundRole(QPalette::Base);
displayLabel->setAlignment(Qt::AlignRight);
displayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
policy = displayLabel->sizePolicy();
policy.setVerticalPolicy(QSizePolicy::Fixed);
displayLabel->setSizePolicy(policy);

signalMapper = new QSignalMapper(this);
QPushButton *button = new QPushButton(QString::number(0), this);
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, 0);
layout->addWidget(button, 4, 1);
digitButtons.push_back(button);
for (unsigned int i = 1; i < 10; ++i) {
QPushButton *button = new QPushButton(QString::number(i), this);
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, i);
layout->addWidget(button, 1+(9-i)/3, (i-1)%3);
digitButtons.push_back(button);
}
QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));

clearButton = new QPushButton("C", this);
layout->addWidget(clearButton, 1, 4);
QObject::connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked()));

additionButton = new QPushButton("+", this);
layout->addWidget(additionButton, 2, 4);
QObject::connect(additionButton, SIGNAL(clicked()), SLOT(addButtonClicked()));

subtractionButton = new QPushButton("-", this);
layout->addWidget(subtractionButton, 3, 4);
QObject::connect(subtractionButton, SIGNAL(clicked()), SLOT(subtractButtonClicked()));

calculateButton = new QPushButton("=", this);
layout->addWidget(calculateButton, 4, 4);
QObject::connect(calculateButton, SIGNAL(clicked()), SLOT(calculateButtonClicked()));
}

int CalculatorWidget::calculate(const QString& expression) {
int result = 0;
char operation = '+';
QRegExp regexp("(\\d+)");
int pos = 0;
while ((pos = regexp.indexIn(expression, pos)) != -1) {
int value = regexp.cap(1).toInt();
switch (operation) {
case '+': result += value; break;
case '-': result -= value; break;
}
pos += regexp.matchedLength();
if (pos < expression.length()) {
operation = expression.at(pos).toAscii();
}
}
return result;
}

QString CalculatorWidget::display() {
return displayLabel->text();
}

void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
keyclickedButton = 0;
int key = event->key();
if (key >= Qt::Key_0 && key <= Qt::Key_9) {
keyclickedButton = digitButtons[key - Qt::Key_0];
}
else {
switch(key)
{
case Qt::Key_Plus: keyclickedButton = additionButton; break;
case Qt::Key_Minus: keyclickedButton = subtractionButton; break;
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Equal: keyclickedButton = calculateButton; break;
case Qt::Key_Escape: keyclickedButton = clearButton; break;
}
}
if (0 != keyclickedButton) {
keyclickedButton->click();
keyclickedButton->setDown(true);
}
}

void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
if (0 != keyclickedButton) {
keyclickedButton->setDown(false);
keyclickedButton = 0;
}
}

void CalculatorWidget::addButtonClicked() {
displayLabel->setText(displayLabel->text()+"+");
}

void CalculatorWidget::buttonClicked(int index) {
displayLabel->setText(displayLabel->text()+QString::number(index));
}

void CalculatorWidget::calculateButtonClicked() {
displayLabel->setText(QString::number(calculate(displayLabel->text())));
}

void CalculatorWidget::clearButtonClicked() {
displayLabel->setText("");
}

void CalculatorWidget::subtractButtonClicked() {
displayLabel->setText(displayLabel->text()+"-");
}
44 changes: 44 additions & 0 deletions examples/CalcQt/CalcQt/CalculatorWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class QLabel;
class QPushButton;
#include <QString>
class QSignalMapper;
#include <QWidget>

class CalculatorWidget : public QWidget {
Q_OBJECT

public:

CalculatorWidget(QWidget *parent = 0, Qt::WindowFlags flags = 0);

QString display();

protected:

virtual void keyPressEvent(QKeyEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);

private:

QLabel *displayLabel;
QVector<QPushButton*> digitButtons;
QPushButton *additionButton;
QPushButton *calculateButton;
QPushButton *clearButton;
QPushButton *subtractionButton;

QPushButton *keyclickedButton;

QSignalMapper *signalMapper;

int calculate(const QString& expression);

private Q_SLOTS:

void addButtonClicked();
void buttonClicked(int index);
void calculateButtonClicked();
void clearButtonClicked();
void subtractButtonClicked();
};

73 changes: 73 additions & 0 deletions examples/CalcQt/CalcQtFeatures/BoostCalculatorQtSteps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#include <cstdlib>

#include <boost/test/unit_test.hpp>
#include <cucumber-cpp/defs.hpp>

#include <QApplication>
#include <QTest>

#include <CalculatorWidget.h>

static int argc = 0;
static QApplication app(argc, 0);
static int milliseconds = -1;

int millisecondsToWait() {
if (milliseconds < 0)
{
char* envVariable = getenv("CALCQT_STEP_DELAY");
milliseconds = (0 != envVariable) ? atoi(envVariable) : 0;
}
return milliseconds;
}

std::istream& operator>> (std::istream& in, QString& val) { std::string s; in >> s; val = s.c_str(); return in; }
std::ostream& operator<< (std::ostream& out, const QString& val) { out << val.toAscii().data(); return out; }

GIVEN("^I just turned on the calculator$") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
calculator->move(0, 0);
calculator->show();
QTest::qWaitForWindowShown(calculator.get());
QTest::qWait(millisecondsToWait());
}

WHEN("^I press (\\d+)$") {
REGEX_PARAM(unsigned int, n);
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_0 + n, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press add") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Plus, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press calculate") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Return, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press clear") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Escape, Qt::NoModifier, millisecondsToWait());
}

WHEN("^I press subtract") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
QTest::keyClick(calculator.get(), Qt::Key_Minus, Qt::NoModifier, millisecondsToWait());
}

THEN("^the display should be empty$") {
cucumber::ScenarioScope<CalculatorWidget> calculator;
BOOST_CHECK_EQUAL(calculator->display().size(), 0);
QTest::qWait(millisecondsToWait());
}

THEN("^the display should show (.*)$") {
REGEX_PARAM(QString, expected);
cucumber::ScenarioScope<CalculatorWidget> calculator;
BOOST_CHECK_EQUAL(expected, calculator->display());
QTest::qWait(millisecondsToWait());
}
19 changes: 19 additions & 0 deletions examples/CalcQt/CalcQtFeatures/features/addition.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# language: en
Feature: Addition
In order to avoid silly mistakes
As a math idiot
I want to be told the sum of two numbers

Scenario Outline: Add two numbers
Given I just turned on the calculator
When I press <button1>
And I press add
And I press <button2>
And I press calculate
Then the display should show <result>

Examples:
| button1 | button2 | result |
| 2 | 3 | 5 |
| 7 | 5 | 12 |
| 9 | 1 | 10 |
77 changes: 77 additions & 0 deletions examples/CalcQt/CalcQtFeatures/features/behavior.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# language: en
Feature: GUI behavior
tests for buttons

Scenario Outline: Digit buttons
Given I just turned on the calculator
When I press <button>
Then the display should show <output>

Examples:
| button | output |
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |

Scenario: Clear button
Given I just turned on the calculator
When I press 2
And I press clear
Then the display should be empty

Scenario: Addition button
Given I just turned on the calculator
When I press add
Then the display should show +

Scenario: Subtraction button
Given I just turned on the calculator
When I press subtract
Then the display should show -

Scenario Outline: Digit sequence
Given I just turned on the calculator
When I press <button1>
And I press <button2>
Then the display should show <output>

Examples:
| button1 | button2 | output |
| 2 | 2 | 22 |
| 7 | 4 | 74 |
| 3 | 8 | 38 |

Scenario Outline: Addition operation
Given I just turned on the calculator
When I press <button1>
And I press add
And I press <button2>
Then the display should show <output>

Examples:
| button1 | button2 | output |
| 2 | 2 | 2+2 |
| 7 | 4 | 7+4 |
| 3 | 8 | 3+8 |


Scenario Outline: Subtraction operation
Given I just turned on the calculator
When I press <button1>
And I press subtract
And I press <button2>
Then the display should show <output>

Examples:
| button1 | button2 | output |
| 2 | 2 | 2-2 |
| 7 | 4 | 7-4 |
| 3 | 8 | 3-8 |

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# language: en
Feature: GUI initalization
test the initial status of the calculator

Scenario: Initialization
Given I just turned on the calculator
Then the display should be empty

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: localhost
port: 3902
Loading