Skip to content
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

[TIMOB-8840] Implemented TableView Sections #112

Merged
merged 6 commits into from
Jun 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 89 additions & 27 deletions src/tibb/NativeTableViewObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@

#include <bb/cascades/ArrayDataModel>
#include <bb/cascades/ListView>

#include "V8Utils.h"
#include "NativeTableViewRowObject.h"
#include "NativeTableViewObject.h"
#include "TableView/BasicTableViewRow.h"
#include "TableView/CustomTableViewRow.h"
#include "TableView/TableViewRowData.h"
#include "TableView/HeaderTableViewRow.h"
#include "TiEventContainerFactory.h"
#include "TiObject.h"
#include "TiProxy.h"
#include "TiUITableViewRow.h"
#include "TiUITableViewSection.h"
#include <bb/cascades/SnapMode>
#include <bb/cascades/GroupDataModel>

using namespace bb::cascades;
using namespace titanium;
Expand All @@ -43,14 +47,32 @@ int NativeTableViewObject::initialize()
tableView_ = bb::cascades::ListView::create();
setControl(tableView_);
tableView_->setDataModel(new ArrayDataModel());

tableView_->setSnapMode(bb::cascades::SnapMode::LeadingEdge);
TableViewRowFactory* factory = new TableViewRowFactory(this);
tableView_->setListItemProvider(factory);
tableView_->setListItemTypeMapper(factory);

return NATIVE_ERROR_OK;
}

TiObject* NativeTableViewObject::createRowObject(Local<Object> item)
{
// Convert the dictionary object into a row object.
NativeObjectFactory* factory = tiObject_->getNativeObjectFactory();
TiUITableViewRow* row = TiUITableViewRow::createTableViewRow(factory);

// Create a JavaScript proxy for the new row object.
Handle<ObjectTemplate> templ = TiObject::getObjectTemplateFromJsObject(tiObject_->getValue());
Local<Object> proxy = templ->NewInstance();
row->setValue(proxy);
TiObject::setTiObjectToJsObject(proxy, row);

// Apply the properties in the dictionary to the new row object.
row->setParametersFromObject(row, item);

return static_cast<TiObject*>(row);
}

int NativeTableViewObject::setData(TiObject* obj)
{
HandleScope scope;
Expand All @@ -60,36 +82,68 @@ int NativeTableViewObject::setData(TiObject* obj)
return NATIVE_ERROR_INVALID_ARG;
}

ArrayDataModel* model = static_cast<ArrayDataModel*>(tableView_->dataModel());


ArrayDataModel* model = static_cast<ArrayDataModel*>(tableView_->dataModel());
QVariantList allRows;
Handle<Array> data = Handle<Array>::Cast(value);
uint32_t length = data->Length();
for (uint32_t i = 0; i < length; i++) {
Local<Value> item = data->Get(i);
Handle<Array> newData;
Handle<Array> sections = Array::New();
for(uint32_t i = 0, len = data->Length(); i < len; i++)
{
TiObject* sectionObject = TiObject::getTiObjectFromJsObject(data->Get(i));
if(sectionObject)
{
TiUITableViewSection *sect = static_cast<TiUITableViewSection*>(sectionObject);
if(sect == NULL) continue;
Handle<Array> rowsInSection = sect->getRowsInSection();
int index = sections->Length();
for(int ii = 0, llen = rowsInSection->Length(); ii < llen; ii++) {
sections->Set(index, rowsInSection->Get(ii));
index++;
}
}
}

if(sections->Length() > 0)
{
newData = Handle<Array>::Cast(sections);
}
else
{
newData = Handle<Array>::Cast(data);
}
int x = 0;
for (uint32_t i = 0, len = newData->Length(); i < len; i++)
{
Local<Object> item = newData->Get(i)->ToObject();
if (!item->IsObject()) {
// Silently ignore any invalid data items.
continue;
}

TiObject* itemObject = TiObject::getTiObjectFromJsObject(item);
if (!itemObject) {
// Convert the dictionary object into a row object.
NativeObjectFactory* factory = tiObject_->getNativeObjectFactory();
TiUITableViewRow* row = TiUITableViewRow::createTableViewRow(factory);
itemObject = static_cast<TiObject*>(row);

// Create a JavaScript proxy for the new row object.
Handle<ObjectTemplate> templ = TiObject::getObjectTemplateFromJsObject(tiObject_->getValue());
Local<Object> proxy = templ->NewInstance();
row->setValue(proxy);
TiObject::setTiObjectToJsObject(proxy, row);

// Apply the properties in the dictionary to the new row object.
row->setParametersFromObject(row, item->ToObject());

// Replace the dictionary object in the data array with the row object.
// This allows developers to later update properties on the row.
data->Set(i, itemObject->getValue());
if (!itemObject)
{
if(item->Has(String::New("header")) || item->Has(String::New("isHeader")))
{
TiObject* h = createRowObject(item);
// TODO: Pedro: something else for sections
newData->Set(x, h->getValue());
NativeTableViewRowObject* listItem = static_cast<NativeTableViewRowObject*>(h->getNativeObject());
allRows.append(listItem->data());
item->Delete(String::New("header"));
item->Delete(String::New("isHeader"));
}
if(!item->Has(String::New("title")))
{
continue;
}
itemObject = createRowObject(item);
// Replace the dictionary object in the data array with the row object.
// This allows developers to later update properties on the row.
// TODO: something else for sections
newData->Set(x, itemObject->getValue());
}

NativeObject* native = itemObject->getNativeObject();
Expand All @@ -99,9 +153,15 @@ int NativeTableViewObject::setData(TiObject* obj)
}

NativeTableViewRowObject* listItem = static_cast<NativeTableViewRowObject*>(native);
model->append(listItem->data());
TiObject o;
o.setValue(String::New(""));
listItem->setHeader(&o);
allRows.append(listItem->data());
x++;
}

model->clear();
model->append(allRows);
qDebug() << "all rows: " << allRows << "\n";
return NATIVE_ERROR_OK;
}

Expand Down Expand Up @@ -145,6 +205,8 @@ bb::cascades::VisualNode* TableViewRowFactory::createItem(bb::cascades::ListView
{
if (type == "custom") {
return new CustomTableViewRow(tableView_->layout());
} else if(type == "header") {
return new HeaderTableViewRow();
}
return new BasicTableViewRow();
}
Expand All @@ -157,7 +219,7 @@ void TableViewRowFactory::updateItem(bb::cascades::ListView*, bb::cascades::Visu
item->setData(data.value<QObject*>());
}

QString TableViewRowFactory::itemType(const QVariant &data, const QVariantList &indexPat)
QString TableViewRowFactory::itemType(const QVariant &data, const QVariantList &indexPath)
{
Q_ASSERT(data.canConvert<QObject*>());
return data.value<QObject*>()->property("dataType").toString();
Expand Down
2 changes: 1 addition & 1 deletion src/tibb/NativeTableViewObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NativeTableViewObject : public NativeControlObject
//Not copy-constructible; Not assignable.
NativeTableViewObject(const NativeTableViewObject& obj);
NativeTableViewObject& operator=(const NativeTableViewObject& obj);

TiObject* createRowObject(v8::Local<v8::Object> item);
bb::cascades::ListView* tableView_;
};

Expand Down
14 changes: 14 additions & 0 deletions src/tibb/NativeTableViewRowObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ NativeTableViewRowObject::NativeTableViewRowObject(TiObject* object)
NativeTableViewRowObject::~NativeTableViewRowObject() {
}

void NativeTableViewRowObject::removeHeader() {
data_->setProperty("header", "");
}

int NativeTableViewRowObject::addChildNativeObject(NativeObject* obj) {
if (!data_->content()) {
data_->setContent(this);
Expand All @@ -48,6 +52,16 @@ int NativeTableViewRowObject::setTitle(TiObject* obj) {
return NATIVE_ERROR_OK;
}

int NativeTableViewRowObject::setHeader(TiObject* obj) {
data_->setProperty("header", V8ValueToQString(obj->getValue()));
return NATIVE_ERROR_OK;
}

int NativeTableViewRowObject::setSubHeader(TiObject* obj) {
data_->setProperty("subHeader", V8ValueToQString(obj->getValue()));
return NATIVE_ERROR_OK;
}

QVariant NativeTableViewRowObject::data() const {
return QVariant::fromValue(static_cast<QObject*>(data_.data()));
}
Expand Down
4 changes: 3 additions & 1 deletion src/tibb/NativeTableViewRowObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class NativeTableViewRowObject : public NativeControlObject {

virtual int setLeftImage(TiObject* obj);
virtual int setTitle(TiObject* obj);

virtual int setHeader(TiObject* obj);
virtual int setSubHeader(TiObject* obj);
void removeHeader();
QVariant data() const;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/tibb/TableView/AbstractTableViewRow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace titanium {

class AbstractTableViewRow : public bb::cascades::CustomControl {
public:
AbstractTableViewRow() { }
AbstractTableViewRow() : data_(NULL) { }

QObject* data() const {
return data_;
Expand Down
44 changes: 44 additions & 0 deletions src/tibb/TableView/HeaderTableViewRow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "HeaderTableViewRow.h"

namespace titanium {

HeaderTableViewRow::HeaderTableViewRow() {
item_ = new bb::cascades::Header();
setRoot(item_);
}

HeaderTableViewRow::~HeaderTableViewRow() {
// TODO Auto-generated destructor stub
}

void HeaderTableViewRow::setData(QObject* newData)
{
updateTitle(newData->property("header").toString());
updateSubTitle(newData->property("subHeader").toString());

connect(newData, SIGNAL(headerChanged(const QString&)), SLOT(updateTitle(const QString&)));
connect(newData, SIGNAL(subHeaderChanged(const QString&)), SLOT(updateSubTitle(const QString&)));

if (data()) {
// Remove any signals from the previous data object.
data()->disconnect(this);
}

AbstractTableViewRow::setData(newData);
}

void HeaderTableViewRow::updateTitle(const QString& title) {
item_->setTitle(title);
}
void HeaderTableViewRow::updateSubTitle(const QString& subtitle) {
item_->setSubtitle(subtitle);
}

}
32 changes: 32 additions & 0 deletions src/tibb/TableView/HeaderTableViewRow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef HEADERTABLEVIEWROW_H_
#define HEADERTABLEVIEWROW_H_

#include "AbstractTableViewRow.h"
#include <bb/cascades/Header>

namespace titanium {

class HeaderTableViewRow: public titanium::AbstractTableViewRow {
Q_OBJECT

public:
HeaderTableViewRow();
virtual ~HeaderTableViewRow();
virtual void setData(QObject* data);
bb::cascades::Header* item_;

private slots:
void updateTitle(const QString& title);
void updateSubTitle(const QString& subtitle);

};

#endif /* HEADERTABLEVIEWROW_H_ */
}
30 changes: 29 additions & 1 deletion src/tibb/TableView/TableViewRowData.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ class TableViewRowData : public QObject {
Q_PROPERTY(QString dataType READ dataType)
Q_PROPERTY(QUrl leftImage READ leftImage WRITE setLeftImage NOTIFY leftImageChanged)
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
Q_PROPERTY(QString header READ header WRITE setHeader NOTIFY headerChanged)
Q_PROPERTY(QString subHeader READ subHeader WRITE setSubHeader NOTIFY subHeaderChanged)

public:
explicit TableViewRowData(TiObject* row)
: row_(row)
, content_(0) { }
, content_(0)
, header_("")
, subHeader_(""){ }

QString dataType() const {
// If the data has custom views for content,
// display using the "custom" list view control.
if (content_) {
return "custom";
} else if(header_.length()>0) {
return "header";
}
return "basic";
}
Expand All @@ -60,6 +66,24 @@ class TableViewRowData : public QObject {
emit titleChanged(title);
}

QString header() const {
return header_;
}

void setHeader(const QString& header) {
header_ = header;
emit headerChanged(header);
}

QString subHeader() const {
return subHeader_;
}

void setSubHeader(const QString& subHeader) {
subHeader_ = subHeader;
emit subHeaderChanged(subHeader);
}

NativeControlObject* content() const {
return content_;
}
Expand All @@ -71,11 +95,15 @@ class TableViewRowData : public QObject {
signals:
void leftImageChanged(const QUrl& image);
void titleChanged(const QString& title);
void headerChanged(const QString& header);
void subHeaderChanged(const QString& header);

private:
TiObject* row_;
QUrl leftImage_;
QString title_;
QString header_;
QString subHeader_;
NativeControlObject* content_;
};

Expand Down