1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-01-12 17:17:50 +03:00

plenty of fixes for FastLayoutManager

--remove also the deprecated javascript LayoutManager
This commit is contained in:
Michail Vourlakos 2021-03-06 12:44:08 +02:00
parent f9d400fa4a
commit 62efa97e61
6 changed files with 95 additions and 495 deletions

View File

@ -1,383 +0,0 @@
/*
* Copyright 2016 Smith AR <audoban@openmailbox.org>
* 2016-2018 Michail Vourlakos <mvourlakos@gmail.com>
*
* This file is part of Latte-Dock
*
* Latte-Dock is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Latte-Dock 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var layout;
var layoutS;
var layoutE;
var root;
var metrics;
var plasmoid;
var childFoundId = 11;
var inRestore=false;
function restore() {
inRestore = true;
//rewrite, so if in the orders there were now invalid ids or if some were missing creates a correct list instead
save();
restoreOptions();
inRestore = false;
root.updateIndexes();
}
function restoreOptions() {
restoreOption("lockedZoomApplets");
restoreOption("userBlocksColorizingApplets");
}
function restoreOption(option) {
var configString;
if (option === "lockedZoomApplets") {
configString = String(plasmoid.configuration.lockedZoomApplets);
} else if (option === "userBlocksColorizingApplets") {
configString = String(plasmoid.configuration.userBlocksColorizingApplets);
}
//array, a cell for encoded item order
var itemsArray = configString.split(";");
for (var i = 0; i < itemsArray.length; i++) {
for (var j = 0; j < layout.children.length; ++j) {
var child = layout.children[j];
if (child.applet && (child.applet.id == itemsArray[i])) {
if (option === "lockedZoomApplets") {
child.lockZoom = true;
} else if (option === "userBlocksColorizingApplets") {
child.userBlocksColorizing = true;
}
}
}
}
}
function save() {
}
function saveOptions() {
saveOption("lockedZoomApplets");
saveOption("userBlocksColorizingApplets");
}
function saveOption(option) {
var ids = new Array();
for (var i = 0; i < layoutS.children.length; ++i) {
var child = layoutS.children[i];
if (child.applet
&& (option === "lockedZoomApplets" && child.lockZoom)
|| (option === "userBlocksColorizingApplets" && child.userBlocksColorizing)) {
ids.push(child.applet.id);
}
}
for (var i = 0; i < layout.children.length; ++i) {
var child = layout.children[i];
if (child.applet
&& (option === "lockedZoomApplets" && child.lockZoom)
|| (option === "userBlocksColorizingApplets" && child.userBlocksColorizing)) {
ids.push(child.applet.id);
}
}
for (var i = 0; i < layoutE.children.length; ++i) {
var child = layoutE.children[i];
if (child.applet
&& (option === "lockedZoomApplets" && child.lockZoom)
|| (option === "userBlocksColorizingApplets" && child.userBlocksColorizing)) {
ids.push(child.applet.id);
}
}
if (option === "lockedZoomApplets") {
plasmoid.configuration.lockedZoomApplets = ids.join(';');
} else if (option === "userBlocksColorizingApplets") {
plasmoid.configuration.userBlocksColorizingApplets = ids.join(';');
}
}
function removeApplet (applet) {
for (var i = layout.children.length - 1; i >= 0; --i) {
var child = layout.children[i];
if (child.applet === applet) {
child.destroy();
}
}
}
//insert item2 before item1
function insertBefore(item1, item2) {
return insertBeforeForLayout(item1.parent, item1, item2);;
}
//insert item2 before item1
function insertBeforeForLayout(tLayout, item1, item2) {
if (item1 === item2) {
return -1;
}
var removed = new Array();
var child;
var i;
for (i = tLayout.children.length - 1; i >= 0; --i) {
child = tLayout.children[i];
removed.push(child);
child.parent = root;
if (child === item1) {
break;
}
}
item2.parent = tLayout;
for (var j = removed.length - 1; j >= 0; --j) {
removed[j].parent = tLayout;
}
return i;
}
//insert item2 after item1
function insertAfter(item1, item2) {
return insertAfterForLayout(item1.parent, item1, item2);
}
//insert item2 after item1
function insertAfterForLayout(tLayout, item1, item2) {
if (item1 === item2) {
return -1;
}
var removed = new Array();
var child;
var i;
for (i = tLayout.children.length - 1; i >= 0; --i) {
child = tLayout.children[i];
//never ever insert after lastSpacer
if (child === lastSpacer && item1 === lastSpacer) {
removed.push(child);
child.parent = root;
break;
} else if (child === item1) {
//Already in position, do nothing
if (tLayout.children[i+1] === item2) {
return -1;
}
break;
}
removed.push(child);
child.parent = root;
}
item2.parent = tLayout;
for (var j = removed.length - 1; j >= 0; --j) {
removed[j].parent = tLayout;
}
return i;
}
function insertAtIndex(relevantLayout, item, position) {
var addToEnd = false;
if (position < 0 || (position > relevantLayout.children.length && !item.isInternalViewSplitter)) {
return;
} else if (position >= relevantLayout.children.length) {
addToEnd = true;
}
//never ever insert after lastSpacer
var firstItem = (relevantLayout.children.length === 1) && (relevantLayout.children[0] === lastSpacer);
//Important !!! , this is used to add the first item
if(firstItem){
lastSpacer.parent = root;
position = 0;
}
if (addToEnd){
item.parent=relevantLayout;
return;
}
if(relevantLayout.children.length > 0){
if (relevantLayout.children[position] === lastSpacer) {
--position;
}
}
var removedItems = new Array();
var totalChildren = relevantLayout.children.length;
for (var i = position; i < totalChildren; ++i) {
var child = relevantLayout.children[position];
child.parent = root;
removedItems.push(child);
}
item.parent = relevantLayout;
for (var i in removedItems) {
removedItems[i].parent = relevantLayout;
}
}
function insertAtCoordinates(item, x, y) {
if (root.isHorizontal) {
y = layout.height / 2;
} else {
x = layout.width / 2;
}
var child = layout.childAt(x, y);
//if we got a place inside the space between 2 applets, we have to find it manually
if (!child) {
if (root.isHorizontal) {
for (var i = 0; i < layout.children.length; ++i) {
var candidate = layout.children[i];
if (x >= candidate.x && x < candidate.x + candidate.width + layout.rowSpacing) {
child = candidate;
break;
}
}
} else {
for (var i = 0; i < layout.children.length; ++i) {
var candidate = layout.children[i];
if (y >= candidate.x && y < candidate.y + candidate.height + layout.columnSpacing) {
child = candidate;
break;
}
}
}
}
//already in position
if (child === item) {
return;
}
if (!child) {
// check if dragging takes place after the end of the layout
if ( ((root.isVertical && y > layout.height)||(root.isHorizontal && x > layout.width))
&& layout.children.length>0 ){
child = layout.children[layout.children.length-1];
} else {
child = layout.children[0];
}
} else {
item.parent = root;
}
if ((root.isVertical && y < child.y + child.height/2) ||
(root.isHorizontal && x < child.x + child.width/2)) {
return insertBefore(child, item);
} else {
return insertAfter(child, item);
}
}
//this is used only for the start and end layouts
function insertAtLayoutCoordinates(tLayout, item, x, y) {
if (root.isHorizontal) {
y = tLayout.height / 2;
} else {
x = tLayout.width / 2;
}
var child = tLayout.childAt(x, y);
//if we got a place inside the space between 2 applets, we have to find it manually
if (!child) {
if (root.isHorizontal) {
for (var i = 0; i < tLayout.children.length; ++i) {
var candidate = tLayout.children[i];
if (x >= candidate.x && x < candidate.x + candidate.width + tLayout.rowSpacing) {
child = candidate;
break;
}
}
} else {
for (var i = 0; i < tLayout.children.length; ++i) {
var candidate = tLayout.children[i];
if (y >= candidate.x && y < candidate.y + candidate.height + tLayout.columnSpacing) {
child = candidate;
break;
}
}
}
}
//already in position
if (child === item) {
//
return childFoundId;
}
if (!child) {
// check if dragging takes place after the end of the layout
var neededSpace = 1.5 * (metrics.iconSize + metrics.totals.lengthEdge);
if ( (((root.isVertical && (y - neededSpace) <= tLayout.height) && y>=0)
||(root.isHorizontal && (x - neededSpace) <= tLayout.width) && x>=0)
&& tLayout.children.length>0 ){
child = tLayout.children[layout.children.length-1];
// check if dragging takes place before the start of the layout
} else if ( ((root.isVertical && (y >= -neededSpace) && (y<=neededSpace))
||(root.isHorizontal && (x >= -neededSpace) && (x<=neededSpace)))
&& tLayout.children.length>0 ){
child = tLayout.children[0];
} else {
return -1;
//child = tLayout.children[0];
}
} else {
item.parent = root;
}
if ((root.isVertical && y < child.y + child.height/2) ||
(root.isHorizontal && x < child.x + child.width/2)) {
return insertBeforeForLayout(tLayout, child, item);
} else {
return insertAfterForLayout(tLayout, child, item);
}
}
function insertAtCoordinates2(item, x, y) {
var pos = -1;
var layoutPosS = layoutS.mapFromItem(root, x, y);
pos = insertAtLayoutCoordinates(layoutS, item, layoutPosS.x, layoutPosS.y);
if (pos === -1){
var layoutPosE = layoutE.mapFromItem(root, x, y);
pos = insertAtLayoutCoordinates(layoutE, item, layoutPosE.x, layoutPosE.y);
}
if (pos!==childFoundId && pos === -1) {
var layoutPos = layout.mapFromItem(root, x, y);
insertAtCoordinates(item, layoutPos.x, layoutPos.y);
}
}

View File

@ -589,6 +589,9 @@ Item {
onIsAutoFillAppletChanged: updateParabolicEffectIsSupported();
onLockZoomChanged: fastLayoutManager.saveOptions();
onUserBlocksColorizingChanged: fastLayoutManager.saveOptions();
Component.onCompleted: {
checkIndex();
root.updateIndexes.connect(checkIndex);

View File

@ -550,7 +550,6 @@ MouseArea {
onCheckedChanged: {
currentApplet.userBlocksColorizing = !checked;
root.layoutManagerSaveOptions();
}
}
@ -562,7 +561,6 @@ MouseArea {
onCheckedChanged: {
currentApplet.lockZoom = checked;
root.layoutManagerSaveOptions();
}
}

View File

@ -42,7 +42,6 @@ import "editmode" as EditMode
import "layouts" as Layouts
import "./background" as Background
import "./debugger" as Debugger
import "../code/LayoutManager.js" as LayoutManager
Item {
id: root
@ -539,18 +538,9 @@ Item {
}
Component.onCompleted: {
// currentLayout.isLayoutHorizontal = isHorizontal
LayoutManager.plasmoid = plasmoid;
LayoutManager.root = root;
LayoutManager.layout = layoutsContainer.mainLayout;
LayoutManager.layoutS = layoutsContainer.startLayout;
LayoutManager.layoutE = layoutsContainer.endLayout;
LayoutManager.metrics = metrics;
upgrader_v010_alignment();
fastLayoutManager.restore();
LayoutManager.restore();
plasmoid.action("configure").visible = !plasmoid.immutable;
plasmoid.action("configure").enabled = !plasmoid.immutable;
@ -586,22 +576,10 @@ Item {
addAppletItemInLayout(appletItem, x, y);
console.log(applet.pluginName);
fastLayoutManager.save();
console.log("org.kde.latte configuration from qml appletOrder :: " + plasmoid.configuration.appletOrder + " :: " + fastLayoutManager.appletOrder);
}
Containment.onAppletRemoved: {
LayoutManager.removeApplet(applet);
var flexibleFound = false;
for (var i = 0; i < layoutsContainer.mainLayout.children.length; ++i) {
var applet = layoutsContainer.mainLayout.children[i].applet;
if (applet && ((root.isHorizontal && applet.Layout.fillWidth) ||
(!root.isHorizontal && applet.Layout.fillHeight)) &&
applet.visible) {
flexibleFound = true;
break
}
}
fastLayoutManager.removeAppletItem(applet);
fastLayoutManager.save();
}
@ -659,33 +637,6 @@ Item {
}
}
function addInternalViewSplittersInMainLayout(){
if (internalViewSplittersCount() === 0) {
addInternalViewSplitterInMain(plasmoid.configuration.splitterPosition);
addInternalViewSplitterInMain(plasmoid.configuration.splitterPosition2);
}
}
function addInternalViewSplitterInMain(pos){
addInternalViewSplitterInLayout(layoutsContainer.mainLayout, pos);
}
function addInternalViewSplitterInLayout(area, pos){
var splittersCount = internalViewSplittersCount();
if(splittersCount<2){
var splitter = appletItemComponent.createObject(root);
splitter.internalSplitterId = splittersCount+1;
splitter.visible = true;
if(pos>=0 ){
LayoutManager.insertAtIndex(area, splitter, pos);
} else {
LayoutManager.insertAtIndex(area, splitter, Math.floor(area.count / 2));
}
}
}
//! it is used in order to check the right click position
//! the only way to take into account the visual appearance
//! of the applet (including its spacers)
@ -740,14 +691,6 @@ Item {
return splitters;
}
function layoutManager() {
return LayoutManager;
}
function layoutManagerSaveOptions() {
LayoutManager.saveOptions();
}
function mouseInCanBeHoveredApplet(){
var applets = layoutsContainer.startLayout.children;

View File

@ -39,6 +39,9 @@ namespace Containment{
LayoutManager::LayoutManager(QObject *parent)
: QObject(parent)
{
m_option["lockZoom"] = "lockedZoomApplets";
m_option["userBlocksColorizing"] = "userBlocksColorizingApplets";
connect(this, &LayoutManager::rootItemChanged, this, &LayoutManager::onRootItemChanged);
}
@ -254,7 +257,7 @@ bool LayoutManager::isValidApplet(const int &id)
for(int i=0; i<applets.count(); ++i) {
uint appletid = applets[i]->property("id").toUInt();
if (appletid == id) {
if (id>0 && appletid == (uint)id) {
return true;
}
}
@ -313,7 +316,7 @@ void LayoutManager::restore()
}
for(int j=0; j<applets.count(); ++j) {
if (applets[j]->property("id").toUInt() == appletIdsOrder[i]) {
if (applets[j]->property("id").toInt() == appletIdsOrder[i]) {
orderedApplets << applets[j];
break;
}
@ -400,39 +403,34 @@ void LayoutManager::restore()
void LayoutManager::restoreOptions()
{
restoreOption("lockedZoomApplets");
restoreOption("userBlocksColorizingApplets");
restoreOption("lockZoom");
restoreOption("userBlocksColorizing");
}
void LayoutManager::restoreOption(const QString &option)
void LayoutManager::restoreOption(const char *option)
{
QStringList applets = (*m_configuration)[option].toString().split(";");
QStringList applets = (*m_configuration)[m_option[option]].toString().split(";");
if (!m_startLayout || !m_mainLayout || !m_endLayout) {
return;
}
const char *optionchars = option.toLatin1().constData();
for (int i=0; i<=2; ++i) {
QQuickItem *layout = (i==0 ? m_startLayout : (i==1 ? m_mainLayout : m_endLayout));
if (layout->childItems().count() > 0) {
int size = layout->childItems().count();
for (int j=size-1; j>=0; --j) {
QQuickItem *item = layout->childItems()[j];
bool issplitter = item->property("isInternalViewSplitter").toBool();
if (!issplitter) {
QVariant appletVariant = item->property("applet");
if (!appletVariant.isValid()) {
continue;
}
QObject *applet = appletVariant.value<QObject *>();
uint id = applet->property("id").toUInt();
item->setProperty(optionchars, applets.contains(QString::number(id)));
for (int j=layout->childItems().count()-1; j>=0; --j) {
QQuickItem *item = layout->childItems()[j];
bool issplitter = item->property("isInternalViewSplitter").toBool();
if (!issplitter) {
QVariant appletVariant = item->property("applet");
if (!appletVariant.isValid()) {
continue;
}
QObject *applet = appletVariant.value<QObject *>();
uint id = applet->property("id").toUInt();
item->setProperty(option, applets.contains(QString::number(id)));
}
}
}
@ -507,6 +505,12 @@ void LayoutManager::save()
if (alignment == Latte::Types::Justify) {
setSplitterPosition(startChilds + 1);
setSplitterPosition2(startChilds + 1 + mainChilds + 1);
} else {
int splitterPosition = static_cast<Latte::Types::Alignment>((*m_configuration)["splitterPosition"].toInt());
int splitterPosition2 = static_cast<Latte::Types::Alignment>((*m_configuration)["splitterPosition2"].toInt());
setSplitterPosition(splitterPosition);
setSplitterPosition2(splitterPosition2);
}
//! are not writing in config file for some cases mentioned in class header so they are not used
@ -515,20 +519,19 @@ void LayoutManager::save()
//(*m_configuration)["appletOrder"] = appletIds.join(";");
setAppletOrder(appletIds.join(";"));
qDebug() << "org.kde.latte saving applets:: " << appletOrder() << " :: " << splitterPosition() << " : " << splitterPosition2();
saveOptions();
qDebug() << "org.kde.latte saving applets:: " << appletOrder() << " :: " << splitterPosition() << " : " << splitterPosition2();
qDebug() << "org.kde.latte saving properties:: " << lockedZoomApplets() << " :: " << userBlocksColorizingApplets();
}
void LayoutManager::saveOptions()
{
saveOption("lockedZoomApplets");
saveOption("userBlocksColorizingApplets");
saveOption("lockZoom");
saveOption("userBlocksColorizing");
qDebug() << "org.kde.latte saving properties:: " << lockedZoomApplets() << " :: " << userBlocksColorizingApplets();
}
void LayoutManager::saveOption(const QString &option)
void LayoutManager::saveOption(const char *option)
{
if (!m_startLayout || !m_mainLayout || !m_endLayout) {
return;
@ -536,36 +539,34 @@ void LayoutManager::saveOption(const QString &option)
QStringList applets;
const char *optionchars = option.toLatin1().constData();
for (int i=0; i<=2; ++i) {
QQuickItem *layout = (i==0 ? m_startLayout : (i==1 ? m_mainLayout : m_endLayout));
if (layout->childItems().count() > 0) {
int size = layout->childItems().count();
for (int j=size-1; j>=0; --j) {
QQuickItem *item = layout->childItems()[j];
bool issplitter = item->property("isInternalViewSplitter").toBool();
if (!issplitter) {
bool enabled = item->property(optionchars).toBool();
if (enabled) {
QVariant appletVariant = item->property("applet");
if (!appletVariant.isValid()) {
continue;
}
QObject *applet = appletVariant.value<QObject *>();
uint id = applet->property("id").toUInt();
for (int j=0; j<layout->childItems().count(); ++j) {
QQuickItem *item = layout->childItems()[j];
bool issplitter = item->property("isInternalViewSplitter").toBool();
applets << QString::number(id);
}
if (issplitter) {
continue;
}
bool enabled = item->property(option).toBool();
if (enabled) {
QVariant appletVariant = item->property("applet");
if (!appletVariant.isValid()) {
continue;
}
QObject *applet = appletVariant.value<QObject *>();
uint id = applet->property("id").toUInt();
applets << QString::number(id);
}
}
}
if (option == "lockedZoomApplets") {
if (option == "lockZoom") {
setLockedZoomApplets(applets.join(";"));
} else if (option == "userBlocksColorizingApplets") {
} else if (option == "userBlocksColorizing") {
setUserBlocksColorizingApplets(applets.join(";"));
}
}
@ -698,6 +699,37 @@ void LayoutManager::insertAtCoordinates(QQuickItem *item, const int &x, const in
}
}
void LayoutManager::removeAppletItem(QObject *applet)
{
if (!m_startLayout || !m_mainLayout || !m_endLayout) {
return;
}
for (int i=0; i<=2; ++i) {
QQuickItem *layout = (i==0 ? m_startLayout : (i==1 ? m_mainLayout : m_endLayout));
if (layout->childItems().count() > 0) {
int size = layout->childItems().count();
for (int j=size-1; j>=0; --j) {
QQuickItem *item = layout->childItems()[j];
bool issplitter = item->property("isInternalViewSplitter").toBool();
if (!issplitter) {
QVariant appletVariant = item->property("applet");
if (!appletVariant.isValid()) {
continue;
}
QObject *currentapplet = appletVariant.value<QObject *>();
if (currentapplet == applet) {
item->deleteLater();
return;
}
}
}
}
}
}
void LayoutManager::addJustifySplittersInMainLayout()
{
if (!m_configuration || !m_mainLayout) {

View File

@ -22,6 +22,7 @@
#define CONTAINMENTLAYOUTMANAGER_H
//Qt
#include <QHash>
#include <QMetaMethod>
#include <QObject>
#include <QQmlPropertyMap>
@ -89,6 +90,9 @@ public:
public slots:
Q_INVOKABLE void restore();
Q_INVOKABLE void save();
Q_INVOKABLE void saveOptions();
Q_INVOKABLE void removeAppletItem(QObject *applet);
Q_INVOKABLE void addJustifySplittersInMainLayout();
Q_INVOKABLE void moveAppletsBasedOnJustifyAlignment();
@ -119,9 +123,8 @@ private slots:
private:
void restoreOptions();
void restoreOption(const QString &option);
void saveOptions();
void saveOption(const QString &option);
void restoreOption(const char *option);
void saveOption(const char *option);
void setSplitterPosition(const int &position);
void setSplitterPosition2(const int &position);
@ -153,6 +156,10 @@ private:
QMetaMethod m_createAppletItemMethod;
QMetaMethod m_createJustifySplitterMethod;
//! first QString is the option in AppletItem
//! second QString is how the option is stored in
QHash<QString, QString> m_option;
};
}
}