2
import QtGraphicalEffects 1.0
3
import Ubuntu.Components 0.1
4
import "databasemodule_v2.js" as DB
5
import "./imgSeparator.js" as ImageSeparator
12
property variant rectangleList: []
14
property bool isAll: false
15
property variant feedArray: []
18
* \brief check collision of two article items
21
function checkCollision(rect1, rect2) {
22
return !((rect1.y + rect1.height <= rect2.y) ||
23
(rect2.y + rect2.height <= rect1.y) ||
24
(rect1.x >= rect2.x + rect2.width) ||
25
(rect2.x >= rect1.x + rect1.width))
29
* \brief Returns a margin within given numbers. Used to make the view
32
function randomMargin() {
33
return units.gu(2 /*3 + Math.random() * 4*/);
37
* Adds a given article to the model. Also adds metadata about the
38
* model to the article.
40
function addArticleToModel(article) {
41
article.model = articleModel
42
article.modelIndex = articleModel.count
43
articleModel.append(article)
48
* Clears all articles from the model and the view.
51
for(var i in rectangleList) {
52
rectangleList[i].destroy()
60
* Reloads the organic view. See inline comments for details.
64
// Decide if we should load all articles in our database or a subset.
67
for (var j=0; j < feedArray.length ; j++)
69
var feedArticles = DB.loadArticles({"isAll": false, "feedId": feedArray[j].id});
70
for(var i = 0; i < feedArticles.rows.length; i++) {
71
addArticleToModel(feedArticles.rows[i])
75
var feedArticles = DB.loadArticles({"isAll": true});
76
for (var i=0; i < feedArticles.rows.length; i++)
78
addArticleToModel(feedArticles.rows[i])
82
// Initial values for the starting column of items
85
var y = randomMargin();
86
// A hard-coded sequence that gives an organic look, but is not
88
var sequence = [0,1,0,1,2,1,0,0,2,1,0,1,0,0];
89
// TODO: Remove hard limit on 300 items
90
for(var i = 0; i < articleModel.count; i++) {
91
var article = articleModel.get(i);
92
// var imageArray = ImageSeparator.separate(article.content)
93
var imageArray = [article.image]
94
// console.log("imageArray: ", imageArray)
95
var hasImage = (article.image !== "");
97
// Pick the type of size we will use for this item from the sequence
98
var sizeType = sequence[i % sequence.length];
99
var alignType = parseInt(Math.random() * 2)
101
// alignType = parseInt(Math.random() * 4);
103
// alignType = parseInt(Math.random() * 2);
108
// Name of the QML file to be used
120
noImageName += "NoImage";
126
alignName = "OneImgA"
129
alignName = "FullImg"
133
componentName = "article_items/" + "Article" + alignName + ".qml";
135
// create component for an article
136
var component = Qt.createComponent(componentName);
137
if(component.status !== Component.Ready) {
138
console.log("Error loading component:", component.errorString());
139
continue; // Skip if we for some reason could not create this component
144
"imageArray": imageArray,
145
"rss_model": articleModel,
146
"model_index": article.modelIndex
149
// This is the actual item that will be on display
150
var articleItem = component.createObject(itemContainer /*organicFlickable.contentItem*/, properties);
152
if(y + articleItem.height > organicGridRoot.height) {
157
// The xEdge is where the last "column" of items were placed last
158
// This keeps us moving forward in x-direction
159
var x = xEdge + randomMargin();
161
// We add some extra random margin to make it look a bit more organic
167
// Check for collisions with all other items
168
// TODO: See if this may be limited to only the previous two colums
169
for(var j = 0; j < rectangleList.length; j++) {
170
var rect2 = rectangleList[j]
171
if(checkCollision(articleItem, rect2)) {
172
articleItem.x = rect2.x + rect2.width + randomMargin();
176
// Did we move the x-edge value further to the right than before?
177
lastColumnX = Math.max(articleItem.x, lastColumnX);
179
// Push this item to all lists and all that stuff
180
var list = rectangleList
181
list.push(articleItem)
184
// Increment y for the next item
185
y += articleItem.height + randomMargin();
187
organicFlickable.scrollToStart()
191
* use Flickable as articles container
196
contentWidth: itemContainer.width /*contentItem.childrenRect.width*/
197
contentHeight: parent.height
207
width: childrenRect.width + units.gu(6)
208
height: parent.height
211
// Behavior on contentX {
214
// easing.type: Easing.InOutQuart
218
function scrollToStart() {