1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-23 13:33:50 +03:00

simplify shares map create/sync in settings window

This commit is contained in:
Michail Vourlakos 2020-03-14 17:50:28 +02:00
parent 4de91c535c
commit 4437472fa9
6 changed files with 105 additions and 39 deletions

View File

@ -127,6 +127,38 @@ const Layout LayoutsTable::operator[](const uint &index) const
return m_layouts[index];
}
QStringList LayoutsTable::allSharesIds() const
{
QStringList sharesIds;
for(int i=0; i<m_layouts.count(); ++i) {
if (m_layouts[i].isShared()) {
for(int j=0; j<m_layouts[i].shares.count(); ++j) {
sharesIds << m_layouts[i].shares[j];
}
}
}
return sharesIds;
}
QStringList LayoutsTable::allSharesNames() const
{
QStringList sharesNames;
for(int i=0; i<m_layouts.count(); ++i) {
if (m_layouts[i].isShared()) {
for(int j=0; j<m_layouts[i].shares.count(); ++j) {
QString shareId = m_layouts[i].shares[j];
int sid = indexOf(shareId);
sharesNames << m_layouts[sid].editedName();
}
}
}
return sharesNames;
}
LayoutsTable LayoutsTable::subtracted(const LayoutsTable &rhs) const
{
LayoutsTable subtract;
@ -144,6 +176,26 @@ LayoutsTable LayoutsTable::subtracted(const LayoutsTable &rhs) const
return subtract;
}
Latte::Layouts::SharesMap LayoutsTable::sharesMap() const
{
Latte::Layouts::SharesMap map;
for(int i=0; i<m_layouts.count(); ++i) {
if (m_layouts[i].isShared()) {
QStringList sharesNames;
for(int j=0; j<m_layouts[i].shares.count(); ++j) {
QString shareId = m_layouts[i].shares[j];
int sid = indexOf(shareId);
sharesNames << m_layouts[sid].editedName();
}
map[m_layouts[i].editedName()] = sharesNames;
}
}
return map;
}
bool LayoutsTable::contains(const QString &id) const
{

View File

@ -21,8 +21,11 @@
#ifndef SETTINGSDATALAYOUTSTABLE_H
#define SETTINGSDATALAYOUTSTABLE_H
// local
#include "layoutdata.h"
#include "../../layouts/synchronizer.h"
// Qt
#include <QList>
namespace Latte {
@ -50,6 +53,10 @@ public:
LayoutsTable subtracted(const LayoutsTable &rhs) const;
QStringList allSharesIds() const;
QStringList allSharesNames() const;
Latte::Layouts::SharesMap sharesMap() const;
bool contains(const QString &id) const;
bool rowExists(const int &row) const;

View File

@ -40,6 +40,7 @@
#include "delegates/checkboxdelegate.h"
#include "delegates/layoutnamedelegate.h"
#include "delegates/shareddelegate.h"
#include "tools/settingstools.h"
// Qt
#include <QButtonGroup>
@ -944,7 +945,11 @@ void SettingsDialog::addLayoutForFile(QString file, QString layoutName, bool new
void SettingsDialog::loadSettings()
{
m_model->clear();
m_sharesMap.clear();
//! The shares map needs to be constructed for start/scratch.
//! We start feeding information with layout_names and during the process
//! we update them to valid layout_ids
Layouts::SharesMap sharesMap;
int i = 0;
QStringList brokenLayouts;
@ -981,7 +986,7 @@ void SettingsDialog::loadSettings()
//! create initial SHARES maps
QString shared = central->sharedLayoutName();
if (!shared.isEmpty()) {
m_sharesMap[shared].append(original.id);
sharesMap[shared].append(original.id);
}
layoutsBuffer << original;
@ -1001,26 +1006,26 @@ void SettingsDialog::loadSettings()
QStringList tempSharedNames;
//! remove these records after updating
for (QHash<const QString, QStringList>::iterator i=m_sharesMap.begin(); i!=m_sharesMap.end(); ++i) {
for (QHash<const QString, QStringList>::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) {
tempSharedNames << i.key();
}
//! update keys
for (QHash<const QString, QStringList>::iterator i=m_sharesMap.begin(); i!=m_sharesMap.end(); ++i) {
for (QHash<const QString, QStringList>::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) {
QString shareid = layoutsBuffer.idForOriginalName(i.key());
if (!shareid.isEmpty()) {
m_sharesMap[shareid] = i.value();
sharesMap[shareid] = i.value();
}
}
//! remove deprecated keys
for (const auto &key : tempSharedNames) {
m_sharesMap.remove(key);
sharesMap.remove(key);
}
qDebug() << "SHARES MAP ::: " << m_sharesMap;
qDebug() << "SHARES MAP ::: " << sharesMap;
for (QHash<const QString, QStringList>::iterator i=m_sharesMap.begin(); i!=m_sharesMap.end(); ++i) {
for (QHash<const QString, QStringList>::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) {
layoutsBuffer[i.key()].shares = i.value();
}
@ -1626,42 +1631,29 @@ void SettingsDialog::syncActiveShares()
return;
}
QHash<const QString, QStringList> currentSharesIdMap;
Layouts::SharesMap currentSharesNamesMap;
Settings::Data::LayoutsTable currentLayoutsData = m_model->currentData();
for (int i = 0; i < m_model->rowCount(); ++i) {
if (isShared(i)) {
QString id = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString();
QString name = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString();
QStringList shareIds = m_model->data(m_model->index(i, SHAREDCOLUMN), Qt::UserRole).toStringList();
QStringList shareNames;
Layouts::SharesMap currentSharesNamesMap = currentLayoutsData.sharesMap();
QStringList originalSharesIds = o_layoutsOriginalData.allSharesIds();
QStringList currentSharesIds = currentLayoutsData.allSharesIds();
for (const auto &shareid : shareIds) {
QString shareName = nameForId(shareid);
shareNames << shareName;
}
QStringList deprecatedSharesIds = Latte::subtracted(originalSharesIds, currentSharesIds);
QStringList deprecatedSharesNames;
currentSharesIdMap[id] = shareIds;
currentSharesNamesMap[name] = shareNames;
for(int i=0; i<deprecatedSharesIds.count(); ++i) {
QString shareId = deprecatedSharesIds[i];
if (currentLayoutsData.contains(shareId)) {
deprecatedSharesNames << currentLayoutsData[shareId].editedName();
} else if (o_layoutsOriginalData.contains(shareId)) {
deprecatedSharesNames << o_layoutsOriginalData[shareId].editedName();
}
}
QStringList deprecatedShares;
qDebug() << " CURRENT SHARES NAMES MAP :: " << currentSharesNamesMap;
qDebug() << " DEPRECATED SHARES ::";
for (const auto &oldSharesIds : m_sharesMap) {
for(const auto &oldId : oldSharesIds) {
QString oldShareName = nameForId(oldId);
if (!m_corona->layoutsManager()->synchronizer()->mapHasRecord(oldShareName, currentSharesNamesMap)) {
deprecatedShares << oldShareName;
}
}
}
qDebug() << " CURRENT SHARES ID MAP :: " << currentSharesIdMap;
m_corona->layoutsManager()->synchronizer()->syncActiveShares(currentSharesNamesMap, deprecatedShares);
m_sharesMap.clear();
m_sharesMap = currentSharesIdMap;
m_corona->layoutsManager()->synchronizer()->syncActiveShares(currentSharesNamesMap, deprecatedSharesNames);
}
bool SettingsDialog::idExistsInModel(QString id)

View File

@ -157,8 +157,6 @@ private:
Settings::Controller::Layouts *m_layoutsController{nullptr};
Ui::SettingsDialog *ui;
//! SharedLayout #settingsid, Shares #settingsid
QHash<const QString, QStringList> m_sharesMap;
QHash<const QString, Latte::CentralLayout *> m_layouts;
QList<int> o_settingsOriginalData;

View File

@ -43,4 +43,17 @@ QPalette::ColorGroup colorGroup(const QStyleOptionViewItem &option)
}
}
QStringList subtracted(const QStringList &original, const QStringList &current)
{
QStringList subtract;
for(int i=0; i<original.count(); ++i) {
if (!current.contains(original[i])) {
subtract << original[i];
}
}
return subtract;
}
}

View File

@ -30,6 +30,10 @@ namespace Latte {
bool isSelected(const QStyleOptionViewItem &option);
QPalette::ColorGroup colorGroup(const QStyleOptionViewItem &option);
//! strings that even though they were initially at original list
//! now they are not present to current list
QStringList subtracted(const QStringList &original, const QStringList &current);
}
#endif