2727using namespace bb ;
2828using 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-
4030GraphControl::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
139130void 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
152144void 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
202196bool 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 ();
0 commit comments