* Copyright 2011 Marco Martin <mart@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
var positions = []
var cellSize = {
width: 24,
height: 24
}
var defaultAppletSize = {
width: cellSize.width * 6,
height: cellSize.height * 6
}
var resultsFlow
var plasmoid;
var itemsConfig
var itemGroups = {}
function restore()
{
var appletsMap = {};
var applet;
var appletsLength = plasmoid.applets.length
for (var i = 0; i < appletsLength; ++i) {
applet = plasmoid.applets[i];
appletsMap["Applet-" + applet.id] = applet;
}
itemsConfig = {};
var configString = String(plasmoid.configuration.ItemsGeometries)
var itemsStrings = configString.split(";")
var itemsStringsLength = itemsStrings.length
for (var i = 0; i < itemsStringsLength; ++i) {
var idConfig = itemsStrings[i].split(":")
idConfig[0] = idConfig[0].replace("%3A", ":")
if (idConfig.length < 2 || appletsMap[idConfig[0]] === undefined) {
continue;
}
var rect = idConfig[1].split(",")
if (rect.length < 5) {
continue
}
itemsConfig[idConfig[0]] = {
x: rect[0],
y: rect[1],
width: rect[2],
height: rect[3],
rotation: parseFloat(rect[4])
};
}
}
function save()
{
var configString = "";
for (var _id in itemsConfig) {
var rect = itemsConfig[_id];
var idstring = _id.replace(":", "%3A");
if (idstring != "undefined") {
configString += idstring + ":" + rect.x + "," + rect.y + "," + rect.width + "," + rect.height + "," + rect.rotation + ";";
}
}
if (plasmoid) {
plasmoid.configuration.ItemsGeometries = configString;
}
}
function resetPositions()
{
positions = []
}
function availableSpace(x, y, width, height)
{
var row = Math.round(x/cellSize.width)
var column = Math.round(y/cellSize.height)
var rowsWidth = Math.ceil(width/cellSize.width)
var columnsHeight = Math.ceil(height/cellSize.height)
var availableSize = {
width: 0,
height: 0
}
if (x < 0 || y < 0) {
return availableSize;
}
if (positions[row] === undefined) {
return {
width: width - Math.max(0, (x + width) - resultsFlow.width),
height: height
}
}
if (!positions[row][column]) {
for (var w = 0; w < rowsWidth; ++w) {
if (positions[row + w] && positions[row + w][column]) {
break;
} else {
availableSize.width = w+1
}
}
for (var h = 0; h < columnsHeight; ++h) {
var free = true
var rowEnd = row + availableSize.width
for (var i = row; i < rowEnd; ++i) {
if (positions[i] && positions[i][column + h]) {
free = false
break
}
}
if (free) {
availableSize.height = h+1
} else {
break
}
}
}
availableSize.width *= cellSize.width
availableSize.height *= cellSize.height
availableSize.width = Math.min(availableSize.width,
(resultsFlow.width-row*cellSize.width))
return availableSize
}
function setSpaceAvailable(x, y, width, height, available)
{
var row = Math.round(x/cellSize.width)
var column = Math.round(y/cellSize.height)
var rowsWidth = Math.round(width/cellSize.width)
var columnsHeight = Math.round(height/cellSize.height)
var rowEnd = row + rowsWidth;
for (var i = row; i < rowEnd; ++i) {
if (!positions[i]) {
positions[i] = []
}
var columnEnd = column + columnsHeight;
for (var j = column; j < columnEnd; ++j) {
positions[i][j] = !available
}
}
}
function normalizeItemPosition(item)
{
var x = Math.max(0, Math.round(item.x/cellSize.width)*cellSize.width)
var y = Math.max(0, Math.round(item.y/cellSize.height)*cellSize.height)
var width = Math.max(cellSize.width, Math.round(item.width/cellSize.width)*cellSize.width)
var height = Math.max(cellSize.height, Math.round(item.height/cellSize.height)*cellSize.height)
var pos = plasmoid.adjustToAvailableScreenRegion(x, y, width, height);
item.x = pos.x;
item.y = pos.y;
item.height = height*/
}
function positionItem(item)
{
var x = Math.max(0, Math.round(item.x/cellSize.width)*cellSize.width)
var y = Math.max(0, Math.round(item.y/cellSize.height)*cellSize.height)
var pos = plasmoid.adjustToAvailableScreenRegion(x, y, item.width, item.height);
x = pos.x;
y = pos.y;
var forwardX = x
var forwardY = y
var backX = x - cellSize.width
var backY = y
var avail
while (forwardY < plasmoid.height) {
var forwardAvail = availableSpace(forwardX, forwardY,
Math.max(item.minimumWidth, item.width),
Math.max(item.minimumHeight, item.height))
if (forwardAvail.width >= Math.max(item.minimumWidth, cellSize.width) &&
forwardAvail.height >= Math.max(item.minimumHeight, cellSize.height)) {
x = forwardX
y = forwardY
avail = forwardAvail
break
}
forwardX += cellSize.width
if (forwardX+item.width > resultsFlow.width) {
forwardX = 0
forwardY += cellSize.height
break;
}*/
}
if (backY < 0) {
continue
}
var backAvail = availableSpace(backX, backY,
Math.max(item.minimumWidth, item.width),
Math.max(item.minimumHeight, item.height))
if (backAvail.width >= Math.max(item.minimumWidth, cellSize.width) &&
backAvail.height >= Math.max(item.minimumHeight, cellSize.height)) {
x = backX
y = backY
avail = backAvail
break
}
backX -= cellSize.width
if (backX < 0) {
backX = resultsFlow.width - item.width
backY -= cellSize.height
}
}
var width = Math.max(cellSize.width*Math.max(1, Math.ceil(item.minimumWidth/cellSize.width)),
Math.round(Math.min(avail.width, item.width)/cellSize.width)*cellSize.width)
var height = Math.max(cellSize.height*Math.max(1, Math.ceil(item.minimumHeight/cellSize.height)),
Math.round(Math.min(avail.height, item.height)/cellSize.height)*cellSize.height)
setSpaceAvailable(x, y, width, height, false)
item.x = x
item.y = y
item.width = width
item.height = height
saveItem(item);
}
function removeApplet(applet) {
delete itemsConfig["Applet-"+applet.id];
delete itemGroups["Applet-"+applet.id];
}
function saveItem(item) {
var rect = {
x: item.x,
y: item.y,
width: item.width,
height: item.height,
rotation: item.rotation
};
if (item.category) {
itemsConfig[item.category] = rect
}
}