Skip to content

Commit 56d8109

Browse files
committed
adding more comments to the graph stumper
1 parent 64dd274 commit 56d8109

File tree

6 files changed

+52
-50
lines changed

6 files changed

+52
-50
lines changed

stumpers/rcmaniac25/assets/GraphView.qml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ Container {
2626
layout: DockLayout {
2727
}
2828

29+
// A custom control for presenting custom data, see
30+
// data/graph.dat and GraphControl.cpp for further detail.
2931
GraphControl {
3032
id: graph
3133
graphDataSource: "data/graph.dat"
34+
35+
// The preferred width and height has to be set in the current implementation.
3236
preferredWidth: 1280 - graphContainer.leftPadding * 2
3337
preferredHeight: 768 - graphContainer.leftPadding * 2
3438
}
3539

3640
onTouch: {
41+
// When the user release the finger on the Graph a new data set
42+
// is loaded.
3743
if (event.isUp()) {
3844
if (graph.graphDataSource == "data/graph3.dat") {
3945
graph.graphDataSource = "data/graph.dat"

stumpers/rcmaniac25/assets/main.qml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Page {
2222
layout: DockLayout {
2323
}
2424

25+
// This is the custom component containing the Graph (see GraphView.qml)
2526
GraphView {
2627
}
2728
}

stumpers/rcmaniac25/src/GraphControl.cpp

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,33 @@
2727
using namespace bb;
2828
using namespace bb::cascades;
2929

30-
/*
31-
* Colors:
32-
* light column 0F233C
33-
* dark column 0D1F35
34-
* divider lines 35475B
35-
* label bkg 050C14
36-
* graph lines 63E2D9 (thickness 3-4 px)
37-
*
38-
*/
39-
4030
GraphControl::GraphControl(Container *parent) :
4131
CustomControl(parent)
4232
{
4333
Container *content = new Container();
44-
//content->setBackground(Color(Color::Red));
4534

4635
// The content Container will be set to lay out children using a dock layout (to center everything on screen).
4736
content->setLayout(new DockLayout());
4837

49-
Container *xAxis = Container::create().vertical(VerticalAlignment::Bottom).horizontal(HorizontalAlignment::Fill).background(Color::fromARGB(0xff2F4C6E)).preferredHeight(5);
50-
Container *yAxis = Container::create().vertical(VerticalAlignment::Fill).horizontal(HorizontalAlignment::Left).background(Color::fromARGB(0xff2F4C6E)).preferredWidth(5);
51-
38+
// Decoration grid only, needs adjusting to data for a more realistic implementation
39+
Container *xAxis = Container::create().vertical(VerticalAlignment::Bottom).horizontal(
40+
HorizontalAlignment::Fill).background(Color::fromARGB(0xff2F4C6E)).preferredHeight(5);
41+
Container *yAxis = Container::create().vertical(VerticalAlignment::Fill).horizontal(
42+
HorizontalAlignment::Left).background(Color::fromARGB(0xff2F4C6E)).preferredWidth(5);
5243
content->add(xAxis);
5344
content->add(yAxis);
5445

55-
// Decoration grid only, needs adjusting to data for a more realistic implementation
56-
for(int x = 200; x < 1280; x += 200) {
57-
content->add(Container::create().vertical(VerticalAlignment::Fill).background(Color::fromARGB(0xff2F4C6E)).preferredWidth(3).translate(x, 0));
46+
for (int x = 200; x < 1280; x += 200) {
47+
content->add(
48+
Container::create().vertical(VerticalAlignment::Fill).background(
49+
Color::fromARGB(0xff2F4C6E)).preferredWidth(3).translate(x, 0));
5850
}
59-
for(int y = 200; y < 768; y += 200) {
60-
content->add(Container::create().horizontal(HorizontalAlignment::Fill).background(Color::fromARGB(0xff2F4C6E)).preferredHeight(3).translate(0, y));
51+
for (int y = 200; y < 768; y += 200) {
52+
content->add(
53+
Container::create().horizontal(HorizontalAlignment::Fill).background(
54+
Color::fromARGB(0xff2F4C6E)).preferredHeight(3).translate(0, y));
6155
}
6256

63-
/*
64-
mDebugLabel1 = Label::create().translate(30, 30).text("1");
65-
mDebugLabel2 = Label::create().translate(30, 130).text("2");
66-
content->add(mDebugLabel1);
67-
content->add(mDebugLabel2);
68-
*/
6957
mIsCreated = false;
7058
connect(this, SIGNAL(creationCompleted ()), this, SLOT(onCreationCompleted ()));
7159

@@ -84,6 +72,7 @@ void GraphControl::drawLine(QPoint point)
8472
uint width = mDrawArea->preferredWidth();
8573
uint height = mDrawArea->preferredHeight();
8674

75+
// Normalize the point to the max and min in x/y direction.
8776
QPoint currentPoint(((point.x() - mMinPoint.x()) * mXNormalization),
8877
((point.y() - mMinPoint.y()) * mYNormalization));
8978

@@ -97,7 +86,7 @@ void GraphControl::drawLine(QPoint point)
9786

9887
if (mPreviousPoint.x() != -1 && mPreviousPoint.y() != -1
9988
&& mPreviousPoint != currentPoint) {
100-
// A straight line
89+
// Draw a straight line from the previous point
10190
// y = k * x + m
10291
// k = (y1-y2) / (x1 - x2)
10392
// m = y1 - k * x1
@@ -108,6 +97,7 @@ void GraphControl::drawLine(QPoint point)
10897

10998
for (int lineX = mPreviousPoint.x(); lineX < currentPoint.x(); lineX++) {
11099

100+
// Draw each point as a 4x4 square.
111101
for (int i = 0; i < brushSize; i++) {
112102
int brushX = lineX + i;
113103
int xOffset = brushX * 4;
@@ -121,6 +111,7 @@ void GraphControl::drawLine(QPoint point)
121111

122112
if ((offset + 3) < arraySize) {
123113

114+
// Set the fill color in the buffer at the drawing position calculated above.
124115
mBuffer[(offset)] = (0x63 * ALPHA) >> 8;
125116
mBuffer[(offset + 1)] = (0xE2 * ALPHA) >> 8;
126117
mBuffer[(offset + 2)] = (0xD9 * ALPHA) >> 8;
@@ -138,6 +129,7 @@ void GraphControl::drawLine(QPoint point)
138129

139130
void GraphControl::generatePixels(uint width, uint height, unsigned char* buf)
140131
{
132+
// Clear the buffer.
141133
for (uint y = 0; y < height; ++y) {
142134
for (uint x = 0; x < width; ++x) {
143135
buf[0] = (0xFF * 0x00) >> 8;
@@ -151,23 +143,26 @@ void GraphControl::generatePixels(uint width, uint height, unsigned char* buf)
151143

152144
void GraphControl::setUpDrawArea(uint width, uint height)
153145
{
146+
// Find and destroy the old ImageView used to display the Graph
154147
Container *content = qobject_cast<Container*>(this->root());
155148

156149
if (content) {
157-
// Remove the current recipe once we return to the ListView
158150
if (mDrawArea && content->remove(mDrawArea)) {
159151
delete mDrawArea;
160152
}
161153
}
162154

155+
// Create a new Image view and set up a buffer to draw to of the corresponding size
163156
mDrawArea = new ImageView();
164157
mDrawArea->setPreferredSize(width, height);
165158

166159
if (!mBuffer) {
167160
mBuffer = (unsigned char*) malloc((width + 1) * height * 4);
168161
}
169162

163+
// Clear the buffer.
170164
generatePixels(width, height, mBuffer);
165+
171166
content->add(mDrawArea);
172167

173168
}
@@ -179,18 +174,17 @@ void GraphControl::drawGraph()
179174

180175
mXNormalization = ((float) width) / (mMaxPoint.x() - mMinPoint.x());
181176
mYNormalization = ((float) height) / (mMaxPoint.y() - mMinPoint.y());
182-
183-
//mDebugLabel1->setText(QString::number( mXNormalization));
184-
//mDebugLabel2->setText(QString::number( mYNormalization));
185-
186177
setUpDrawArea(width, height);
187178

188179
if (!mValues.isEmpty()) {
189180
for (int i = 0; i < mValues.size(); ++i) {
181+
182+
// Draws a line to the the Point in mValues.at(i) in mBuffer.
190183
drawLine(mValues.at(i));
191184
}
192185

193186
if (mBuffer) {
187+
// Create a new Image from the buffer and display it in the draw area
194188
Image img(
195189
bb::ImageData::fromPixels(mBuffer, bb::PixelFormat::RGBA_Premultiplied, width,
196190
height, width * 4));
@@ -201,7 +195,7 @@ void GraphControl::drawGraph()
201195

202196
bool GraphControl::readGraphDataSource()
203197
{
204-
// Now read the data from the file and plot the lines.
198+
// Now read the data from the file.
205199
QString appFolder(QDir::homePath());
206200
appFolder.chop(4);
207201
QString dataFileName = appFolder + "app/native/assets/" + mGraphDataSource;
@@ -249,8 +243,6 @@ bool GraphControl::readGraphDataSource()
249243
}
250244
dataFile.close();
251245

252-
253-
254246
return true;
255247
}
256248
} else {
@@ -263,6 +255,8 @@ void GraphControl::onCreationCompleted()
263255
{
264256
mIsCreated = true;
265257

258+
// To ensure all properties being set, the reading of data and drawing is
259+
//delayed on the first creation of the Control.
266260
if (!mGraphDataSource.isEmpty()) {
267261
if (readGraphDataSource()) {
268262
drawGraph();

stumpers/rcmaniac25/src/GraphControl.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,33 @@ namespace bb
3333
/**
3434
* GraphControl Description:
3535
*
36+
* This is a custom control that will present data contained in a file
37+
* as a graph.
3638
*/
3739
class GraphControl: public bb::cascades::CustomControl
3840
{
3941
Q_OBJECT
4042

4143
/**
42-
*
44+
* The path to the data source to be used, needs to be a two column comma separated text file.
4345
*/
44-
Q_PROPERTY(QString graphDataSource READ graphDataSource WRITE setGraphDataSource NOTIFY graphDataSourceChanged)
46+
Q_PROPERTY(QString graphDataSource READ graphDataSource WRITE setGraphDataSource NOTIFY graphDataSourceChanged)
4547

4648
public:
4749
GraphControl(Container *parent = 0);
4850
~GraphControl();
4951

5052
/**
53+
* Sets the data source path for the Graph.
5154
*
52-
*
53-
* @param graphDataSource
55+
* @param graphDataSource the path to the data source.
5456
*/
5557
void setGraphDataSource(QString graphDataSource);
5658

5759
/**
58-
* @return
60+
* Returns the current data source path.
61+
*
62+
* @return A string containing the path to the data source.
5963
*/
6064
QString graphDataSource();
6165

@@ -66,29 +70,30 @@ private slots:
6670
void onCreationCompleted();
6771

6872
private:
73+
// Functions for setting up and drawing to a pixel buffer that will
74+
// visualize the graph data in an ImageView.
6975
void setUpDrawArea(uint width, uint height);
7076
void generatePixels(uint width, uint height, unsigned char* buf);
7177
void drawLine(QPoint currentPoint);
7278
void drawGraph();
7379
bool readGraphDataSource();
7480

75-
// State variables
76-
int mNbrOfChars;
77-
81+
// Custom control components.
7882
ImageView *mDrawArea;
7983
unsigned char *mBuffer;
8084
QString mGraphDataSource;
85+
86+
// State variable
8187
bool mIsCreated;
82-
QPoint mPreviousPoint;
8388

89+
// Variables used to draw lines in the pixelbuffer presented in mDrawArea
8490
QPoint mMaxPoint;
8591
QPoint mMinPoint;
8692
QList<QPoint> mValues;
8793
float mXNormalization;
8894
float mYNormalization;
95+
QPoint mPreviousPoint;
8996

90-
Label *mDebugLabel1;
91-
Label *mDebugLabel2;
9297
};
9398

9499
#endif // ifndef _GraphControl_H_

stumpers/rcmaniac25/src/Graphs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Graphs::Graphs(bb::cascades::Application *app) :
3434

3535
// create root object for the UI
3636
AbstractPane *root = qml->createRootObject<AbstractPane>();
37+
3738
// set created root object as a scene
3839
app->setScene(root);
3940
}

stumpers/rcmaniac25/src/Graphs.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ namespace bb
2626
}
2727
}
2828

29-
/*!
30-
* @brief Application pane object
31-
*
32-
*Use this object to create and init app UI, to create context objects, to register the new meta types etc.
33-
*/
3429
class Graphs: public QObject
3530
{
3631
Q_OBJECT

0 commit comments

Comments
 (0)