Skip to content

Commit 4ccf0c2

Browse files
author
Gianni
committed
Added CUKE_CALCQT_WAIT env variable to set CalcQt example wait time for each step.
Skip compilation when FindQt4 fails. Cleaned indentation and curly brackets. Removed commented code and useless includes.
1 parent c0c47c4 commit 4ccf0c2

File tree

5 files changed

+161
-169
lines changed

5 files changed

+161
-169
lines changed

examples/CalcQt/CMakeLists.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
project(CalcQt)
22

3-
find_package(Qt4 REQUIRED QtCore QtGui QtTest)
4-
include(${QT_USE_FILE})
3+
find_package(Qt4 COMPONENTS QtCore QtGui QtTest)
54

6-
set(CALCQT_HEADERS CalcQt/CalculatorWidget.h)
7-
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
8-
9-
include_directories(${CUKE_INCLUDE_DIRS} CalcQt)
10-
add_library(CalcQt CalcQt/CalculatorWidget ${CALCQT_HEADERS_MOC})
11-
12-
if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
13-
include_directories(${Boost_INCLUDE_DIRS})
14-
add_executable(BoostCalculatorQtSteps CalcQtFeatures/BoostCalculatorQtSteps)
15-
target_link_libraries(BoostCalculatorQtSteps CalcQt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
16-
endif()
17-
18-
# build CalcQt executable
19-
set(CALCQT_SOURCES CalcQt/CalcQt.cpp CalcQt/CalculatorWidget.cpp)
20-
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
21-
target_link_libraries(calcqt ${QT_LIBRARIES})
5+
if(QT4_FOUND)
6+
include(${QT_USE_FILE})
7+
set(CALCQT_HEADERS CalcQt/CalculatorWidget.h)
8+
qt4_wrap_cpp(CALCQT_HEADERS_MOC ${CALCQT_HEADERS})
9+
include_directories(${CUKE_INCLUDE_DIRS} CalcQt)
10+
add_library(CalcQt CalcQt/CalculatorWidget ${CALCQT_HEADERS_MOC})
11+
12+
if(Boost_UNIT_TEST_FRAMEWORK_FOUND)
13+
include_directories(${Boost_INCLUDE_DIRS})
14+
add_executable(BoostCalculatorQtSteps CalcQtFeatures/BoostCalculatorQtSteps)
15+
target_link_libraries(BoostCalculatorQtSteps CalcQt ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${CUKE_LIBRARIES} ${QT_LIBRARIES})
16+
endif()
17+
18+
set(CALCQT_SOURCES CalcQt/CalcQt.cpp CalcQt/CalculatorWidget.cpp)
19+
add_executable(calcqt ${CALCQT_SOURCES} ${CALCQT_HEADERS_MOC})
20+
target_link_libraries(calcqt ${QT_LIBRARIES})
21+
endif()

examples/CalcQt/CalcQt/CalcQt.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
#include "CalculatorWidget.h"
44

5-
int main(int argc, char *argv[])
6-
{
7-
//Q_INIT_RESOURCE(application);
5+
int main(int argc, char *argv[]) {
86
QApplication app(argc, argv);
97
app.setApplicationName("Qt Calculator");
108
CalculatorWidget widget;

examples/CalcQt/CalcQt/CalculatorWidget.cpp

Lines changed: 102 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -7,140 +7,124 @@
77
#include <QPushButton>
88
#include <QSignalMapper>
99

10-
CalculatorWidget::CalculatorWidget(QWidget* parent, Qt::WindowFlags flags)
11-
: QWidget(parent, flags)
12-
{
13-
QGridLayout* layout = new QGridLayout;
14-
layout->setSizeConstraint(QLayout::SetFixedSize);
15-
setLayout(layout);
16-
17-
QSizePolicy policy = sizePolicy();
18-
19-
displayLabel = new QLabel(this);
20-
layout->addWidget(displayLabel, 0, 0, 1, 3);
21-
displayLabel->setAutoFillBackground(true);
22-
displayLabel->setBackgroundRole(QPalette::Base);
23-
displayLabel->setAlignment(Qt::AlignRight);
24-
displayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
25-
policy = displayLabel->sizePolicy();
26-
policy.setVerticalPolicy(QSizePolicy::Fixed);
27-
displayLabel->setSizePolicy(policy);
28-
29-
signalMapper = new QSignalMapper(this);
30-
QPushButton* button = new QPushButton(QString::number(0), this);
31-
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
32-
signalMapper->setMapping(button, 0);
33-
layout->addWidget(button, 4, 1);
34-
digitButtons.push_back(button);
35-
for (unsigned int i = 1; i < 10; ++i)
36-
{
37-
QPushButton* button = new QPushButton(QString::number(i), this);
38-
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
39-
signalMapper->setMapping(button, i);
40-
layout->addWidget(button, 1+(9-i)/3, (i-1)%3);
41-
digitButtons.push_back(button);
42-
}
43-
QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
44-
45-
clearButton = new QPushButton("C", this);
46-
layout->addWidget(clearButton, 1, 4);
47-
QObject::connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked()));
48-
49-
additionButton = new QPushButton("+", this);
50-
layout->addWidget(additionButton, 2, 4);
51-
QObject::connect(additionButton, SIGNAL(clicked()), SLOT(addButtonClicked()));
52-
53-
subtractionButton = new QPushButton("-", this);
54-
layout->addWidget(subtractionButton, 3, 4);
55-
QObject::connect(subtractionButton, SIGNAL(clicked()), SLOT(subtractButtonClicked()));
56-
57-
calculateButton = new QPushButton("=", this);
58-
layout->addWidget(calculateButton, 4, 4);
59-
QObject::connect(calculateButton, SIGNAL(clicked()), SLOT(calculateButtonClicked()));
10+
CalculatorWidget::CalculatorWidget(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) {
11+
QGridLayout *layout = new QGridLayout;
12+
layout->setSizeConstraint(QLayout::SetFixedSize);
13+
setLayout(layout);
14+
15+
QSizePolicy policy = sizePolicy();
16+
17+
displayLabel = new QLabel(this);
18+
layout->addWidget(displayLabel, 0, 0, 1, 3);
19+
displayLabel->setAutoFillBackground(true);
20+
displayLabel->setBackgroundRole(QPalette::Base);
21+
displayLabel->setAlignment(Qt::AlignRight);
22+
displayLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
23+
policy = displayLabel->sizePolicy();
24+
policy.setVerticalPolicy(QSizePolicy::Fixed);
25+
displayLabel->setSizePolicy(policy);
26+
27+
signalMapper = new QSignalMapper(this);
28+
QPushButton *button = new QPushButton(QString::number(0), this);
29+
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
30+
signalMapper->setMapping(button, 0);
31+
layout->addWidget(button, 4, 1);
32+
digitButtons.push_back(button);
33+
for (unsigned int i = 1; i < 10; ++i) {
34+
QPushButton *button = new QPushButton(QString::number(i), this);
35+
QObject::connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
36+
signalMapper->setMapping(button, i);
37+
layout->addWidget(button, 1+(9-i)/3, (i-1)%3);
38+
digitButtons.push_back(button);
39+
}
40+
QObject::connect(signalMapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
41+
42+
clearButton = new QPushButton("C", this);
43+
layout->addWidget(clearButton, 1, 4);
44+
QObject::connect(clearButton, SIGNAL(clicked()), SLOT(clearButtonClicked()));
45+
46+
additionButton = new QPushButton("+", this);
47+
layout->addWidget(additionButton, 2, 4);
48+
QObject::connect(additionButton, SIGNAL(clicked()), SLOT(addButtonClicked()));
49+
50+
subtractionButton = new QPushButton("-", this);
51+
layout->addWidget(subtractionButton, 3, 4);
52+
QObject::connect(subtractionButton, SIGNAL(clicked()), SLOT(subtractButtonClicked()));
53+
54+
calculateButton = new QPushButton("=", this);
55+
layout->addWidget(calculateButton, 4, 4);
56+
QObject::connect(calculateButton, SIGNAL(clicked()), SLOT(calculateButtonClicked()));
6057
}
6158

62-
int CalculatorWidget::calculate(const QString& expression)
63-
{
64-
int result = 0;
65-
char operation = '+';
66-
QRegExp regexp("(\\d+)");
67-
int pos = 0;
68-
while ((pos = regexp.indexIn(expression, pos)) != -1) {
69-
int value = regexp.cap(1).toInt();
70-
switch (operation) {
71-
case '+': result += value; break;
72-
case '-': result -= value; break;
73-
}
74-
pos += regexp.matchedLength();
75-
if (pos < expression.length()) {
76-
operation = expression.at(pos).toAscii();
77-
}
78-
}
79-
return result;
59+
int CalculatorWidget::calculate(const QString& expression) {
60+
int result = 0;
61+
char operation = '+';
62+
QRegExp regexp("(\\d+)");
63+
int pos = 0;
64+
while ((pos = regexp.indexIn(expression, pos)) != -1) {
65+
int value = regexp.cap(1).toInt();
66+
switch (operation) {
67+
case '+': result += value; break;
68+
case '-': result -= value; break;
69+
}
70+
pos += regexp.matchedLength();
71+
if (pos < expression.length()) {
72+
operation = expression.at(pos).toAscii();
73+
}
74+
}
75+
return result;
8076
}
8177

82-
QString CalculatorWidget::display()
83-
{
84-
return displayLabel->text();
78+
QString CalculatorWidget::display() {
79+
return displayLabel->text();
8580
}
8681

87-
void CalculatorWidget::keyPressEvent(QKeyEvent* event)
88-
{
89-
keyclickedButton = 0;
90-
int key = event->key();
91-
if (key >= Qt::Key_0 && key <= Qt::Key_9)
92-
{
93-
keyclickedButton = digitButtons[key - Qt::Key_0];
94-
}
95-
else
96-
{
97-
switch(key)
98-
{
99-
case Qt::Key_Plus: keyclickedButton = additionButton; break;
100-
case Qt::Key_Minus: keyclickedButton = subtractionButton; break;
101-
case Qt::Key_Return:
102-
case Qt::Key_Enter:
103-
case Qt::Key_Equal: keyclickedButton = calculateButton; break;
104-
case Qt::Key_Escape: keyclickedButton = clearButton; break;
105-
}
106-
}
107-
if (0 != keyclickedButton)
108-
{
109-
keyclickedButton->click();
110-
keyclickedButton->setDown(true);
111-
}
82+
void CalculatorWidget::keyPressEvent(QKeyEvent *event) {
83+
keyclickedButton = 0;
84+
int key = event->key();
85+
if (key >= Qt::Key_0 && key <= Qt::Key_9) {
86+
keyclickedButton = digitButtons[key - Qt::Key_0];
87+
}
88+
else {
89+
switch(key)
90+
{
91+
case Qt::Key_Plus: keyclickedButton = additionButton; break;
92+
case Qt::Key_Minus: keyclickedButton = subtractionButton; break;
93+
case Qt::Key_Return:
94+
case Qt::Key_Enter:
95+
case Qt::Key_Equal: keyclickedButton = calculateButton; break;
96+
case Qt::Key_Escape: keyclickedButton = clearButton; break;
97+
}
98+
}
99+
if (0 != keyclickedButton) {
100+
keyclickedButton->click();
101+
keyclickedButton->setDown(true);
102+
}
112103
}
113104

114-
void CalculatorWidget::keyReleaseEvent(QKeyEvent* event)
115-
{
116-
if (0 != keyclickedButton)
117-
{
118-
keyclickedButton->setDown(false);
119-
keyclickedButton = 0;
120-
}
105+
void CalculatorWidget::keyReleaseEvent(QKeyEvent *event) {
106+
if (0 != keyclickedButton) {
107+
keyclickedButton->setDown(false);
108+
keyclickedButton = 0;
109+
}
121110
}
122111

123-
void CalculatorWidget::addButtonClicked()
124-
{
125-
displayLabel->setText(displayLabel->text()+"+");
112+
void CalculatorWidget::addButtonClicked() {
113+
displayLabel->setText(displayLabel->text()+"+");
126114
}
127115

128-
void CalculatorWidget::buttonClicked(int index)
129-
{
130-
displayLabel->setText(displayLabel->text()+QString::number(index));
116+
void CalculatorWidget::buttonClicked(int index) {
117+
displayLabel->setText(displayLabel->text()+QString::number(index));
131118
}
132119

133-
void CalculatorWidget::calculateButtonClicked()
134-
{
135-
displayLabel->setText(QString::number(calculate(displayLabel->text())));
120+
void CalculatorWidget::calculateButtonClicked() {
121+
displayLabel->setText(QString::number(calculate(displayLabel->text())));
136122
}
137123

138-
void CalculatorWidget::clearButtonClicked()
139-
{
140-
displayLabel->setText("");
124+
void CalculatorWidget::clearButtonClicked() {
125+
displayLabel->setText("");
141126
}
142127

143-
void CalculatorWidget::subtractButtonClicked()
144-
{
145-
displayLabel->setText(displayLabel->text()+"-");
128+
void CalculatorWidget::subtractButtonClicked() {
129+
displayLabel->setText(displayLabel->text()+"-");
146130
}

examples/CalcQt/CalcQt/CalculatorWidget.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,41 @@ class QPushButton;
44
class QSignalMapper;
55
#include <QWidget>
66

7-
class CalculatorWidget : public QWidget
8-
{
9-
Q_OBJECT
7+
class CalculatorWidget : public QWidget {
8+
Q_OBJECT
109

1110
public:
1211

13-
CalculatorWidget(QWidget* parent = 0, Qt::WindowFlags flags = 0);
12+
CalculatorWidget(QWidget *parent = 0, Qt::WindowFlags flags = 0);
1413

15-
QString display();
14+
QString display();
1615

1716
protected:
1817

19-
virtual void keyPressEvent(QKeyEvent* event);
20-
virtual void keyReleaseEvent(QKeyEvent* event);
18+
virtual void keyPressEvent(QKeyEvent *event);
19+
virtual void keyReleaseEvent(QKeyEvent *event);
2120

2221
private:
2322

24-
QLabel* displayLabel;
25-
QVector<QPushButton*> digitButtons;
26-
QPushButton* additionButton;
27-
QPushButton* calculateButton;
28-
QPushButton* clearButton;
29-
QPushButton* subtractionButton;
23+
QLabel *displayLabel;
24+
QVector<QPushButton*> digitButtons;
25+
QPushButton *additionButton;
26+
QPushButton *calculateButton;
27+
QPushButton *clearButton;
28+
QPushButton *subtractionButton;
3029

31-
QPushButton* keyclickedButton;
30+
QPushButton *keyclickedButton;
3231

33-
QSignalMapper* signalMapper;
32+
QSignalMapper *signalMapper;
3433

35-
int calculate(const QString& expression);
34+
int calculate(const QString& expression);
3635

3736
private Q_SLOTS:
3837

39-
void addButtonClicked();
40-
void buttonClicked(int index);
41-
void calculateButtonClicked();
42-
void clearButtonClicked();
43-
void subtractButtonClicked();
38+
void addButtonClicked();
39+
void buttonClicked(int index);
40+
void calculateButtonClicked();
41+
void clearButtonClicked();
42+
void subtractButtonClicked();
4443
};
4544

0 commit comments

Comments
 (0)