From 45e3531ad382c5ece28d0da97dda977b110c2551 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Sun, 15 Mar 2020 16:20:29 +0200 Subject: [PATCH] move logic out of settings dialog --layouts controller in settings window should take ownership for all logic related to layouts model/view --- .../controllers/layoutscontroller.cpp | 752 +++++++++++++- app/settings/controllers/layoutscontroller.h | 59 +- app/settings/data/layoutdata.cpp | 6 + app/settings/data/layoutdata.h | 4 +- app/settings/data/layoutstable.cpp | 15 +- app/settings/data/layoutstable.h | 3 +- app/settings/delegates/activitiesdelegate.cpp | 11 +- app/settings/models/layoutsmodel.cpp | 7 +- app/settings/models/layoutsmodel.h | 5 +- app/settings/settingsdialog.cpp | 979 ++---------------- app/settings/settingsdialog.h | 56 +- 11 files changed, 917 insertions(+), 980 deletions(-) diff --git a/app/settings/controllers/layoutscontroller.cpp b/app/settings/controllers/layoutscontroller.cpp index a2826e0b7..96531ab8a 100644 --- a/app/settings/controllers/layoutscontroller.cpp +++ b/app/settings/controllers/layoutscontroller.cpp @@ -20,16 +20,86 @@ #include "layoutscontroller.h" +// local +#include "../universalsettings.h" +#include "../delegates/activitiesdelegate.h" +#include "../delegates/backgroundcmbdelegate.h" +#include "../delegates/checkboxdelegate.h" +#include "../delegates/layoutnamedelegate.h" +#include "../delegates/shareddelegate.h" +#include "../tools/settingstools.h" +#include "../../layout/genericlayout.h" +#include "../../layout/centrallayout.h" +#include "../../layout/sharedlayout.h" +#include "../../layouts/importer.h" +#include "../../layouts/manager.h" +#include "../../layouts/synchronizer.h" + +// Qt +#include +#include +#include +#include +#include +#include + +// KDE +#include +#include +#include +#include + namespace Latte { namespace Settings { namespace Controller { -Layouts::Layouts(QObject *parent, Latte::Corona *corona) +Layouts::Layouts(QDialog *parent, Latte::Corona *corona, QTableView *view) : QObject(parent), + m_parent(parent), m_corona(corona), - m_model(new Model::Layouts(this, corona)) + m_model(new Model::Layouts(this, corona)), + m_view(view) { + initView(); + loadLayouts(); +} + +Layouts::~Layouts() +{ + //! save column widths + QStringList columnWidths; + columnWidths << QString::number(m_view->columnWidth(Model::Layouts::BACKGROUNDCOLUMN)); + columnWidths << QString::number(m_view->columnWidth(Model::Layouts::NAMECOLUMN)); + columnWidths << QString::number(m_view->columnWidth(Model::Layouts::MENUCOLUMN)); + columnWidths << QString::number(m_view->columnWidth(Model::Layouts::BORDERSCOLUMN)); + + if (inMultipleMode()) { + columnWidths << QString::number(m_view->columnWidth(Model::Layouts::ACTIVITYCOLUMN)); + } else { + //! In Single Mode, keed recorded value for ACTIVITYCOLUMN + QStringList currentWidths = m_corona->universalSettings()->layoutsColumnWidths(); + if (currentWidths.count()>=5) { + columnWidths << currentWidths[4]; + } + } + + m_corona->universalSettings()->setLayoutsColumnWidths(columnWidths); + + //! remove + qDeleteAll(m_layouts); + + if (m_model) { + delete m_model; + } + + for (const auto &tempDir : m_tempDirectories) { + QDir tDir(tempDir); + + if (tDir.exists() && tempDir.startsWith("/tmp/")) { + tDir.removeRecursively(); + } + } } Model::Layouts *Layouts::model() const @@ -37,6 +107,684 @@ Model::Layouts *Layouts::model() const return m_model; } +QTableView *Layouts::view() const +{ + return m_view; +} + +void Layouts::initView() +{ + m_view->setModel(m_model); + m_view->horizontalHeader()->setStretchLastSection(true); + m_view->verticalHeader()->setVisible(false); + + //!find the available colors + QString iconsPath(m_corona->kPackage().path() + "../../plasmoids/org.kde.latte.containment/contents/icons/"); + QDir layoutDir(iconsPath); + QStringList filter; + filter.append(QString("*print.jpg")); + QStringList files = layoutDir.entryList(filter, QDir::Files | QDir::NoSymLinks); + QStringList colors; + + for (auto &file : files) { + int colorEnd = file.lastIndexOf("print.jpg"); + QString color = file.remove(colorEnd, 9); + colors.append(color); + } + + m_view->setItemDelegateForColumn(Model::Layouts::NAMECOLUMN, new Settings::Layout::Delegate::LayoutName(this)); + m_view->setItemDelegateForColumn(Model::Layouts::BACKGROUNDCOLUMN, new Settings::Layout::Delegate::BackgroundCmbBox(this, iconsPath, colors)); + m_view->setItemDelegateForColumn(Model::Layouts::MENUCOLUMN, new Settings::Layout::Delegate::CheckBox(this)); + m_view->setItemDelegateForColumn(Model::Layouts::BORDERSCOLUMN, new Settings::Layout::Delegate::CheckBox(this)); + m_view->setItemDelegateForColumn(Model::Layouts::ACTIVITYCOLUMN, new Settings::Layout::Delegate::Activities(this)); + m_view->setItemDelegateForColumn(Model::Layouts::SHAREDCOLUMN, new Settings::Layout::Delegate::Shared(this)); +} + +bool Layouts::dataAreChanged() const +{ + return ((o_originalInMultipleMode != m_model->inMultipleMode()) + || (o_layoutsOriginalData != m_model->currentData())); +} + +bool Layouts::selectedLayoutIsCurrentActive() const +{ + Data::Layout selected = selectedLayout(); + + return (selected.isActive && (selected.originalName() == m_corona->layoutsManager()->synchronizer()->currentLayoutName())); +} + +const Data::Layout &Layouts::selectedLayout() const +{ + int selectedRow = m_view->currentIndex().row(); + + return m_model->at(selectedRow); +} + +bool Layouts::inMultipleMode() const +{ + return m_model->inMultipleMode(); +} + +void Layouts::setInMultipleMode(bool inMultiple) +{ + m_model->setInMultipleMode(inMultiple); + + if (inMultiple) { + m_view->setColumnHidden(Model::Layouts::SHAREDCOLUMN, false); + + //! column widths + QStringList cWidths = m_corona->universalSettings()->layoutsColumnWidths(); + + if (cWidths.count()>=5) { + m_view->setColumnWidth(Model::Layouts::ACTIVITYCOLUMN, cWidths[4].toInt()); + } + } else { + m_view->setColumnHidden(Model::Layouts::SHAREDCOLUMN, true); + } +} + +void Layouts::setOriginalInMultipleMode(bool inMultiple) +{ + o_originalInMultipleMode = inMultiple; + setInMultipleMode(inMultiple); +} + +int Layouts::rowForId(QString id) const +{ + for (int i = 0; i < m_model->rowCount(); ++i) { + QString rowId = m_model->data(m_model->index(i, Model::Layouts::IDCOLUMN), Qt::DisplayRole).toString(); + + if (rowId == id) { + return i; + } + } + + return -1; +} + +int Layouts::rowForName(QString layoutName) const +{ + for (int i = 0; i < m_model->rowCount(); ++i) { + QString rowName = m_model->data(m_model->index(i, Model::Layouts::NAMECOLUMN), Qt::DisplayRole).toString(); + + if (rowName == layoutName) { + return i; + } + } + + return -1; +} + +QString Layouts::uniqueTempDirectory() +{ + QTemporaryDir tempDir; + tempDir.setAutoRemove(false); + m_tempDirectories.append(tempDir.path()); + + return tempDir.path(); +} + +QString Layouts::uniqueLayoutName(QString name) +{ + int pos_ = name.lastIndexOf(QRegExp(QString("[-][0-9]+"))); + + if (m_model->containsCurrentName(name) && pos_ > 0) { + name = name.left(pos_); + } + + int i = 2; + + QString namePart = name; + + while (m_model->containsCurrentName(name)) { + name = namePart + "-" + QString::number(i); + i++; + } + + return name; +} + +void Layouts::removeSelected() +{ + if (m_view->currentIndex().row() < 0) { + return; + } + + Data::Layout selected = selectedLayout(); + + if (m_corona->layoutsManager()->synchronizer()->layout(selected.originalName())) { + return; + } + + int row = m_view->currentIndex().row(); + m_model->removeRow(row); + + row = qMin(qMax(row - 1, 0), m_model->rowCount()); + m_view->selectRow(row); +} + +void Layouts::toggleLockedForSelected() +{ + if (m_view->currentIndex().row() < 0) { + return; + } + + Data::Layout selected = selectedLayout(); + + m_model->setData(m_model->index(m_view->currentIndex().row(), Model::Layouts::NAMECOLUMN), !selected.isLocked, Settings::Model::Layouts::LAYOUTISLOCKEDROLE); +} + +void Layouts::toggleSharedForSelected() +{ + if (m_view->currentIndex().row() < 0) { + return; + } + + Data::Layout selected = selectedLayout(); + + if (selected.isShared()) { + m_model->setData(m_model->index(m_view->currentIndex().row(), Model::Layouts::SHAREDCOLUMN), QStringList(), Qt::UserRole); + } else { + /* bool assigned{false}; + QStringList assignedList; + + QStringList availableShares = availableSharesFor(row); + + for (const auto &id : availableShares) { + QString name = nameForId(id); + if (m_corona->layoutsManager()->synchronizer()->layout(name)) { + assignedList << id; + m_model->setData(m_model->index(row, Model::Layouts::SHAREDCOLUMN), assignedList, Qt::UserRole); + assigned = true; + break; + } + } + + if (!assigned && availableShares.count()>0) { + assignedList << availableShares[0]; + m_model->setData(m_model->index(row, Model::Layouts::SHAREDCOLUMN), assignedList, Qt::UserRole); + assigned = true; + }*/ + } +} + + +void Layouts::loadLayouts() +{ + m_model->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 + Latte::Layouts::SharesMap sharesMap; + + int i = 0; + QStringList brokenLayouts; + + if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { + m_corona->layoutsManager()->synchronizer()->syncActiveLayoutsToOriginalFiles(); + } + + Settings::Data::LayoutsTable layoutsBuffer; + + for (const auto layout : m_corona->layoutsManager()->layouts()) { + Settings::Data::Layout original; + original.id = QDir::homePath() + "/.config/latte/" + layout + ".layout.latte"; + + CentralLayout *central = new CentralLayout(this, original.id); + + original.setOriginalName(central->name()); + original.background = central->background(); + original.color = central->color(); + original.textColor = central->textColor(); + original.isActive = (m_corona->layoutsManager()->synchronizer()->layout(original.originalName()) != nullptr); + original.isLocked = !central->isWritable(); + original.isShownInMenu = central->showInMenu(); + original.hasDisabledBorders = central->disableBordersForMaximizedWindows(); + original.activities = central->activities(); + + //! add central layout properties + if (original.background.isEmpty()) { + original.textColor = QString(); + } + + m_layouts[original.id] = central; + + //! create initial SHARES maps + QString shared = central->sharedLayoutName(); + if (!shared.isEmpty()) { + sharesMap[shared].append(original.id); + } + + layoutsBuffer << original; + + qDebug() << "counter:" << i << " total:" << m_model->rowCount(); + + i++; + + Latte::Layout::GenericLayout *generic = m_corona->layoutsManager()->synchronizer()->layout(central->name()); + + if ((generic && generic->layoutIsBroken()) || (!generic && central->layoutIsBroken())) { + brokenLayouts.append(central->name()); + } + } + + //! update SHARES map keys in order to use the #settingsid(s) + QStringList tempSharedNames; + + //! remove these records after updating + for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { + tempSharedNames << i.key(); + } + + //! update keys + for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { + QString shareid = layoutsBuffer.idForOriginalName(i.key()); + if (!shareid.isEmpty()) { + sharesMap[shareid] = i.value(); + } + } + + //! remove deprecated keys + for (const auto &key : tempSharedNames) { + sharesMap.remove(key); + } + + qDebug() << "SHARES MAP ::: " << sharesMap; + + for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { + layoutsBuffer[i.key()].shares = i.value(); + } + + //! Send original loaded data to model + m_model->setCurrentData(layoutsBuffer); + + m_view->selectRow(rowForName(m_corona->layoutsManager()->currentLayoutName())); + + //! this line should be commented for debugging layouts window functionality + m_view->setColumnHidden(Model::Layouts::IDCOLUMN, true); + m_view->setColumnHidden(Model::Layouts::HIDDENTEXTCOLUMN, true); + + if (m_corona->universalSettings()->canDisableBorders()) { + m_view->setColumnHidden(Model::Layouts::BORDERSCOLUMN, false); + } else { + m_view->setColumnHidden(Model::Layouts::BORDERSCOLUMN, true); + } + + m_view->resizeColumnsToContents(); + + QStringList columnWidths = m_corona->universalSettings()->layoutsColumnWidths(); + + if (!columnWidths.isEmpty()) { + for (int i=0; isetColumnWidth(Model::Layouts::BACKGROUNDCOLUMN+i, columnWidths[i].toInt()); + } + } + + o_layoutsOriginalData = m_model->currentData(); + + //! there are broken layouts and the user must be informed! + if (brokenLayouts.count() > 0) { + auto msg = new QMessageBox(m_parent); + msg->setIcon(QMessageBox::Warning); + msg->setWindowTitle(i18n("Layout Warning")); + msg->setText(i18n("The layout(s) %0 have broken configuration!!! Please remove them to improve the system stability...").arg(brokenLayouts.join(","))); + msg->setStandardButtons(QMessageBox::Ok); + + msg->open(); + } +} + +void Layouts::addLayoutForFile(QString file, QString layoutName, bool newTempDirectory, bool showNotification) +{ + if (layoutName.isEmpty()) { + layoutName = CentralLayout::layoutName(file); + } + + layoutName = uniqueLayoutName(layoutName); + + Settings::Data::Layout copied; + + if (newTempDirectory) { + copied.id = uniqueTempDirectory() + "/" + layoutName + ".layout.latte"; + QFile(file).copy(copied.id); + } else { + copied.id = file; + } + + QFileInfo newFileInfo(copied.id); + + if (newFileInfo.exists() && !newFileInfo.isWritable()) { + QFile(copied.id).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); + } + + if (m_layouts.contains(copied.id)) { + CentralLayout *oldSettings = m_layouts.take(copied.id); + delete oldSettings; + } + + CentralLayout *settings = new CentralLayout(this, copied.id); + m_layouts[copied.id] = settings; + + copied.setOriginalName(uniqueLayoutName(layoutName)); + copied.color = settings->color(); + copied.textColor = settings->textColor(); + copied.background = settings->background(); + copied.isLocked = !settings->isWritable(); + copied.isShownInMenu = settings->showInMenu(); + copied.hasDisabledBorders = settings->disableBordersForMaximizedWindows(); + + if (copied.background.isEmpty()) { + copied.textColor = QString(); + } + + m_model->appendLayout(copied); + + // ui->layoutsView->selectRow(row); + + if (showNotification) { + //NOTE: The pointer is automatically deleted when the event is closed + auto notification = new KNotification("import-done", KNotification::CloseOnTimeout); + notification->setText(i18nc("import-done", "Layout: %0 imported successfully
").arg(layoutName)); + notification->sendEvent(); + } +} + +void Layouts::copySelectedLayout() +{ + int row = m_view->currentIndex().row(); + + if (row < 0) { + return; + } + + Settings::Data::Layout selected = selectedLayout(); + + //! Update original layout before copying if this layout is active + if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { + Latte::Layout::GenericLayout *generic = m_corona->layoutsManager()->synchronizer()->layout(selected.originalName()); + if (generic) { + generic->syncToLayoutFile(); + } + } + + Settings::Data::Layout copied = selected; + + copied.setOriginalName(uniqueLayoutName(selected.currentName())); + copied.id = uniqueTempDirectory() + "/" + copied.originalName() + ".layout.latte";; + copied.activities = QStringList(); + copied.isLocked = false; + + QFile(selected.id).copy(copied.id); + QFileInfo newFileInfo(copied.id); + + if (newFileInfo.exists() && !newFileInfo.isWritable()) { + QFile(copied.id).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); + } + + CentralLayout *settings = new CentralLayout(this, copied.id); + + m_layouts[copied.id] = settings; + m_model->appendLayout(copied); + + m_view->selectRow(row + 1); +} + +void Layouts::importLayoutsFromV1ConfigFile(QString file) +{ + KTar archive(file, QStringLiteral("application/x-tar")); + archive.open(QIODevice::ReadOnly); + + //! if the file isnt a tar archive + if (archive.isOpen()) { + QDir tempDir{uniqueTempDirectory()}; + + const auto archiveRootDir = archive.directory(); + + for (const auto &name : archiveRootDir->entries()) { + auto fileEntry = archiveRootDir->file(name); + fileEntry->copyTo(tempDir.absolutePath()); + } + + QString name = Latte::Layouts::Importer::nameOfConfigFile(file); + + QString applets(tempDir.absolutePath() + "/" + "lattedock-appletsrc"); + + if (QFile(applets).exists()) { + if (m_corona->layoutsManager()->importer()->importOldLayout(applets, name, false, tempDir.absolutePath())) { + addLayoutForFile(tempDir.absolutePath() + "/" + name + ".layout.latte", name, false); + } + + QString alternativeName = name + "-" + i18nc("layout", "Alternative"); + + if (m_corona->layoutsManager()->importer()->importOldLayout(applets, alternativeName, false, tempDir.absolutePath())) { + addLayoutForFile(tempDir.absolutePath() + "/" + alternativeName + ".layout.latte", alternativeName, false); + } + } + } +} + +void Layouts::save() +{ + //! Update Layouts + QStringList knownActivities = m_corona->layoutsManager()->synchronizer()->activities(); + + QTemporaryDir layoutTempDir; + + qDebug() << "Temporary Directory ::: " << layoutTempDir.path(); + + QStringList fromRenamePaths; + QStringList toRenamePaths; + QStringList toRenameNames; + + QString switchToLayout; + + QHash activeLayoutsToRename; + + Settings::Data::LayoutsTable removedLayouts = o_layoutsOriginalData.subtracted(m_model->currentData()); + + //! remove layouts that have been removed from the user + for (int i=0; irowCount(); ++i) { + Data::Layout iLayout = m_model->at(i); + QStringList cleanedActivities; + + //!update only activities that are valid + for (const auto &activity : iLayout.activities) { + if (knownActivities.contains(activity) && activity != Settings::Data::Layout::FREEACTIVITIESID) { + cleanedActivities.append(activity); + } + } + + //qDebug() << i << ". " << id << " - " << color << " - " << name << " - " << menu << " - " << lActivities; + //! update the generic parts of the layouts + bool isOriginalLayout = o_layoutsOriginalData.containsId(iLayout.id); + Latte::Layout::GenericLayout *genericActive= isOriginalLayout ? m_corona->layoutsManager()->synchronizer()->layout(iLayout.originalName()) : nullptr; + Latte::Layout::GenericLayout *generic = genericActive ? genericActive : m_layouts[iLayout.id]; + + //! unlock read-only layout + if (!generic->isWritable()) { + generic->unlock(); + } + + if (iLayout.color.startsWith("/")) { + //it is image file in such case + if (iLayout.color != generic->background()) { + generic->setBackground(iLayout.color); + } + + if (generic->textColor() != iLayout.textColor) { + generic->setTextColor(iLayout.textColor); + } + } else { + if (iLayout.color != generic->color()) { + generic->setColor(iLayout.color); + generic->setBackground(QString()); + generic->setTextColor(QString()); + } + } + + //! update only the Central-specific layout parts + CentralLayout *centralActive = isOriginalLayout ? m_corona->layoutsManager()->synchronizer()->centralLayout(iLayout.originalName()) : nullptr; + CentralLayout *central = centralActive ? centralActive : m_layouts[iLayout.id]; + + if (central->showInMenu() != iLayout.isShownInMenu) { + central->setShowInMenu(iLayout.isShownInMenu); + } + + if (central->disableBordersForMaximizedWindows() != iLayout.hasDisabledBorders) { + central->setDisableBordersForMaximizedWindows(iLayout.hasDisabledBorders); + } + + if (central->activities() != cleanedActivities) { + central->setActivities(cleanedActivities); + } + + //! If the layout name changed OR the layout path is a temporary one + if (iLayout.nameWasEdited()) { + //! If the layout is Active in MultipleLayouts + if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts && generic->isActive()) { + qDebug() << " Active Layout Should Be Renamed From : " << generic->name() << " TO :: " << iLayout.currentName(); + activeLayoutsToRename[iLayout.currentName()] = generic; + } + + QString tempFile = layoutTempDir.path() + "/" + QString(generic->name() + ".layout.latte"); + qDebug() << "new temp file ::: " << tempFile; + + if ((m_corona->layoutsManager()->memoryUsage() == Types::SingleLayout) && (generic->name() == m_corona->layoutsManager()->currentLayoutName())) { + switchToLayout = iLayout.currentName(); + } + + generic = m_layouts.take(iLayout.id); + delete generic; + + QFile(iLayout.id).rename(tempFile); + + fromRenamePaths.append(iLayout.id); + toRenamePaths.append(tempFile); + toRenameNames.append(iLayout.currentName()); + } + } + + //! this is necessary in case two layouts have to swap names + //! so we copy first the layouts in a temp directory and afterwards all + //! together we move them in the official layout directory + for (int i = 0; i < toRenamePaths.count(); ++i) { + QString newFile = QDir::homePath() + "/.config/latte/" + toRenameNames[i] + ".layout.latte"; + QFile(toRenamePaths[i]).rename(newFile); + + CentralLayout *nLayout = new CentralLayout(this, newFile); + m_layouts[newFile] = nLayout; + + //! updating the #SETTINGSID in the model for the layout that was renamed + for (int j = 0; j < m_model->rowCount(); ++j) { + Data::Layout jLayout = m_model->at(j); + + if (jLayout.id == fromRenamePaths[i]) { + m_model->setData(m_model->index(j, Model::Layouts::IDCOLUMN), newFile, Qt::DisplayRole); + } + } + } + + QString layoutForFreeActivities; + + if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { + for (const auto &newLayoutName : activeLayoutsToRename.keys()) { + Latte::Layout::GenericLayout *layoutPtr = activeLayoutsToRename[newLayoutName]; + qDebug() << " Active Layout of Type: " << layoutPtr->type() << " Is Renamed From : " << activeLayoutsToRename[newLayoutName]->name() << " TO :: " << newLayoutName; + layoutPtr->renameLayout(newLayoutName); + + if (layoutPtr->type() == Latte::Layout::Type::Central) { + CentralLayout *central = qobject_cast(layoutPtr); + + if (central->activities().isEmpty()) { + //! that means it is an active layout for orphaned Activities + layoutForFreeActivities = newLayoutName; + } + } + } + } + + //! lock layouts in the end when the user has chosen it + for (int i = 0; i < m_model->rowCount(); ++i) { + Data::Layout layout = m_model->at(i); + Latte::Layout::GenericLayout *layoutPtr = m_corona->layoutsManager()->synchronizer()->layout(layout.originalName()); + + if (!layoutPtr && m_layouts.contains(layout.id)) { + layoutPtr = m_layouts[layout.id]; + } + + if (layout.isLocked && layoutPtr && layoutPtr->isWritable()) { + layoutPtr->lock(); + } + } + + //! update SharedLayouts that are Active + syncActiveShares(); + + //! reload layouts in layoutsmanager + m_corona->layoutsManager()->synchronizer()->loadLayouts(); + + + //! send to layout manager in which layout to switch + /* Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); + + if (m_corona->layoutsManager()->memoryUsage() != inMemoryOption) { + Types::LayoutsMemoryUsage previousMemoryUsage = m_corona->layoutsManager()->memoryUsage(); + m_corona->layoutsManager()->setMemoryUsage(inMemoryOption); + + QVariant value = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), NAMECOLUMN), Qt::DisplayRole); + QString layoutName = value.toString(); + + m_corona->layoutsManager()->switchToLayout(layoutName, previousMemoryUsage); + } else { + if (!switchToLayout.isEmpty()) { + m_corona->layoutsManager()->switchToLayout(switchToLayout); + } else if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { + m_corona->layoutsManager()->synchronizer()->syncMultipleLayoutsToActivities(orphanedLayout); + } + }*/ +} + +void Layouts::syncActiveShares() +{ + if (m_corona->layoutsManager()->memoryUsage() != Types::MultipleLayouts) { + return; + } + + Settings::Data::LayoutsTable currentLayoutsData = m_model->currentData(); + + Latte::Layouts::SharesMap currentSharesNamesMap = currentLayoutsData.sharesMap(); + QStringList originalSharesIds = o_layoutsOriginalData.allSharesIds(); + QStringList currentSharesIds = currentLayoutsData.allSharesIds(); + + QStringList deprecatedSharesIds = Latte::subtracted(originalSharesIds, currentSharesIds); + QStringList deprecatedSharesNames; + + for(int i=0; ilayoutsManager()->synchronizer()->syncActiveShares(currentSharesNamesMap, deprecatedSharesNames); +} + } } } diff --git a/app/settings/controllers/layoutscontroller.h b/app/settings/controllers/layoutscontroller.h index aaf3bf1d9..ece14e635 100644 --- a/app/settings/controllers/layoutscontroller.h +++ b/app/settings/controllers/layoutscontroller.h @@ -22,8 +22,20 @@ #define SETTINGSLAYOUTSCONTROLLER_H // local +#include "../data/layoutdata.h" +#include "../data/layoutstable.h" #include "../models/layoutsmodel.h" #include "../../lattecorona.h" +#include "../../../liblatte2/types.h" + +// Qt +#include +#include + +namespace Latte { +class Corona; +class CentralLayout; +} namespace Latte { namespace Settings { @@ -34,14 +46,59 @@ class Layouts : public QObject Q_OBJECT public: - explicit Layouts(QObject *parent, Latte::Corona *corona); + explicit Layouts(QDialog *parent, Latte::Corona *corona, QTableView *view); + ~Layouts(); Model::Layouts *model() const; + QTableView *view() const; + + bool dataAreChanged() const; + + bool inMultipleMode() const; + void setInMultipleMode(bool inMultiple); + + void setOriginalInMultipleMode(bool inMultiple); + + bool selectedLayoutIsCurrentActive() const; + const Data::Layout &selectedLayout() const; + + //! actions + void save(); + void loadLayouts(); + void removeSelected(); + void toggleLockedForSelected(); + void toggleSharedForSelected(); + + void addLayoutForFile(QString file, QString layoutName = QString(), bool newTempDirectory = true, bool showNotification = true); + void copySelectedLayout(); + //! import layouts from Latte versions <= v0.7.x + void importLayoutsFromV1ConfigFile(QString file); + +private: + void initView(); + void syncActiveShares(); + + int rowForId(QString id) const; + int rowForName(QString layoutName) const; + + QString uniqueTempDirectory(); + QString uniqueLayoutName(QString name); private: + QDialog *m_parent{nullptr}; Latte::Corona *m_corona{nullptr}; + QTableView *m_view{nullptr}; + + //! original data + bool o_originalInMultipleMode{false}; + Settings::Data::LayoutsTable o_layoutsOriginalData; + + //! current data Model::Layouts *m_model{nullptr}; + QHash m_layouts; + //! temp data + QStringList m_tempDirectories; }; } diff --git a/app/settings/data/layoutdata.cpp b/app/settings/data/layoutdata.cpp index fb9e2ff78..9e58439b0 100644 --- a/app/settings/data/layoutdata.cpp +++ b/app/settings/data/layoutdata.cpp @@ -20,6 +20,7 @@ #include "layoutdata.h" + namespace Latte { namespace Settings { namespace Data { @@ -117,6 +118,11 @@ bool Layout::operator!=(const Layout &rhs) const return !(*this == rhs); } +bool Layout::isForFreeActivities() const +{ + return ((activities.count() == 1) && (activities[0] == FREEACTIVITIESID)); +} + bool Layout::isShared() const { return !shares.isEmpty(); diff --git a/app/settings/data/layoutdata.h b/app/settings/data/layoutdata.h index 97283702a..8124219ce 100644 --- a/app/settings/data/layoutdata.h +++ b/app/settings/data/layoutdata.h @@ -31,8 +31,9 @@ namespace Data { class Layout { - public: + static constexpr const char* FREEACTIVITIESID = "{0000-0000}"; + Layout(); Layout(Layout &&o); Layout(const Layout &o); @@ -57,6 +58,7 @@ public: //! Functionality bool isShared() const; + bool isForFreeActivities() const; bool nameWasEdited() const; //! Operators diff --git a/app/settings/data/layoutstable.cpp b/app/settings/data/layoutstable.cpp index e1c5c1882..55696825e 100644 --- a/app/settings/data/layoutstable.cpp +++ b/app/settings/data/layoutstable.cpp @@ -168,7 +168,7 @@ LayoutsTable LayoutsTable::subtracted(const LayoutsTable &rhs) const } for(int i=0; i=0 && row>=0 && rowsetData(Model::Layouts::FREEACTIVITIESID); + action->setData(Data::Layout::FREEACTIVITIESID); action->setIcon(QIcon::fromTheme(freeActivities_icon())); action->setCheckable(true); action->setChecked(isFreeActivitiesChecked); @@ -145,7 +146,7 @@ QWidget *Activities::createEditor(QWidget *parent, const QStyleOptionViewItem &o } else { foreach (QAction *action, button->menu()->actions()) { QString actId = action->data().toString(); - if (actId == Model::Layouts::FREEACTIVITIESID) { + if (actId == Data::Layout::FREEACTIVITIESID) { action->setChecked(false); } } @@ -309,7 +310,7 @@ QString Activities::joinedActivities(const QStringList &activities, bool isActiv bool bold{false}; bool italic{false}; - if (activityId == Model::Layouts::FREEACTIVITIESID) { + if (activityId == Data::Layout::FREEACTIVITIESID) { name = freeActivities_text(); if (formatText) { diff --git a/app/settings/models/layoutsmodel.cpp b/app/settings/models/layoutsmodel.cpp index 2cb53e779..25e2d5834 100644 --- a/app/settings/models/layoutsmodel.cpp +++ b/app/settings/models/layoutsmodel.cpp @@ -54,6 +54,11 @@ Layouts::Layouts(QObject *parent, Latte::Corona *corona) }); } +bool Layouts::containsCurrentName(const QString &name) const +{ + return m_layoutsTable.containsCurrentName(name); +} + bool Layouts::inMultipleMode() const { return m_inMultipleMode; @@ -266,7 +271,7 @@ QVariant Layouts::data(const QModelIndex &index, int role) const return m_layoutsTable[row].nameWasEdited(); } else if (role == ALLACTIVITIESROLE) { QStringList activities; - activities << QString(FREEACTIVITIESID); + activities << QString(Data::Layout::FREEACTIVITIESID); activities << m_corona->layoutsManager()->synchronizer()->activities(); return activities; } else if (role == ALLLAYOUTSROLE) { diff --git a/app/settings/models/layoutsmodel.h b/app/settings/models/layoutsmodel.h index 20b57530d..2dc84f332 100644 --- a/app/settings/models/layoutsmodel.h +++ b/app/settings/models/layoutsmodel.h @@ -40,8 +40,6 @@ class Layouts : public QAbstractTableModel Q_OBJECT public: - static constexpr const char* FREEACTIVITIESID = "{0000-0000}"; - enum Columns { IDCOLUMN = 0, @@ -68,6 +66,8 @@ public: explicit Layouts(QObject *parent, Latte::Corona *corona); + bool containsCurrentName(const QString &name) const; + bool inMultipleMode() const; void setInMultipleMode(bool inMultiple); @@ -81,6 +81,7 @@ public: QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; + const Data::Layout &at(const int &row); bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; diff --git a/app/settings/settingsdialog.cpp b/app/settings/settingsdialog.cpp index 2157c00d4..ac1e2f75f 100644 --- a/app/settings/settingsdialog.cpp +++ b/app/settings/settingsdialog.cpp @@ -26,20 +26,13 @@ #include "ui_settingsdialog.h" #include "../lattecorona.h" #include "../screenpool.h" -#include "../layout/genericlayout.h" #include "../layout/centrallayout.h" -#include "../layout/sharedlayout.h" #include "../layouts/importer.h" #include "../layouts/manager.h" #include "../layouts/synchronizer.h" #include "../liblatte2/types.h" #include "../plasma/extended/theme.h" #include "data/layoutdata.h" -#include "delegates/activitiesdelegate.h" -#include "delegates/backgroundcmbdelegate.h" -#include "delegates/checkboxdelegate.h" -#include "delegates/layoutnamedelegate.h" -#include "delegates/shareddelegate.h" #include "tools/settingstools.h" // Qt @@ -102,37 +95,12 @@ SettingsDialog::SettingsDialog(QWidget *parent, Latte::Corona *corona) connect(ui->buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked , this, &SettingsDialog::restoreDefaults); - m_layoutsController = new Settings::Controller::Layouts(this, m_corona); + m_layoutsController = new Settings::Controller::Layouts(this, m_corona, ui->layoutsView); m_model = m_layoutsController->model(); - ui->layoutsView->setModel(m_layoutsController->model()); - ui->layoutsView->horizontalHeader()->setStretchLastSection(true); - ui->layoutsView->verticalHeader()->setVisible(false); - //connect(m_corona->layoutsManager(), &Layouts::Manager::currentLayoutNameChanged, this, &SettingsDialog::layoutsChanged); //connect(m_corona->layoutsManager(), &Layouts::Manager::centralLayoutsChanged, this, &SettingsDialog::layoutsChanged); - QString iconsPath(m_corona->kPackage().path() + "../../plasmoids/org.kde.latte.containment/contents/icons/"); - - //!find the available colors - QDir layoutDir(iconsPath); - QStringList filter; - filter.append(QString("*print.jpg")); - QStringList files = layoutDir.entryList(filter, QDir::Files | QDir::NoSymLinks); - QStringList colors; - - for (auto &file : files) { - int colorEnd = file.lastIndexOf("print.jpg"); - QString color = file.remove(colorEnd, 9); - colors.append(color); - } - - ui->layoutsView->setItemDelegateForColumn(NAMECOLUMN, new Settings::Layout::Delegate::LayoutName(this)); - ui->layoutsView->setItemDelegateForColumn(COLORCOLUMN, new Settings::Layout::Delegate::BackgroundCmbBox(this, iconsPath, colors)); - ui->layoutsView->setItemDelegateForColumn(MENUCOLUMN, new Settings::Layout::Delegate::CheckBox(this)); - ui->layoutsView->setItemDelegateForColumn(BORDERSCOLUMN, new Settings::Layout::Delegate::CheckBox(this)); - ui->layoutsView->setItemDelegateForColumn(ACTIVITYCOLUMN, new Settings::Layout::Delegate::Activities(this)); - ui->layoutsView->setItemDelegateForColumn(SHAREDCOLUMN, new Settings::Layout::Delegate::Shared(this)); m_inMemoryButtons = new QButtonGroup(this); m_inMemoryButtons->addButton(ui->singleToolBtn, Latte::Types::SingleLayout); @@ -202,7 +170,6 @@ SettingsDialog::SettingsDialog(QWidget *parent, Latte::Corona *corona) loadSettings(); //! SIGNALS - connect(ui->layoutsView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, [&]() { updatePerLayoutButtonsState(); updateApplyButtonsState(); @@ -211,7 +178,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, Latte::Corona *corona) connect(m_inMemoryButtons, static_cast(&QButtonGroup::buttonToggled), [ = ](int id, bool checked) { - m_model->setInMultipleMode(inMultipleLayoutsLook()); + m_model->setInMultipleMode(m_inMemoryButtons->checkedId() == Latte::Types::MultipleLayouts); updateApplyButtonsState(); updateSharedLayoutsUiElements(); @@ -255,7 +222,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, Latte::Corona *corona) }); connect(m_editLayoutAction, &QAction::triggered, this, [&]() { - QString file = idForRow(ui->layoutsView->currentIndex().row()); + QString file = m_layoutsController->selectedLayout().id; if (!file.isEmpty()) { QProcess::startDetached("kwrite \"" + file + "\""); @@ -279,46 +246,10 @@ SettingsDialog::~SettingsDialog() { qDebug() << Q_FUNC_INFO; - qDeleteAll(m_layouts); - - if (m_model) { - delete m_model; - } - - if (m_corona && m_corona->universalSettings()) { - m_corona->universalSettings()->setLayoutsWindowSize(size()); - - QStringList columnWidths; - columnWidths << QString::number(ui->layoutsView->columnWidth(COLORCOLUMN)); - columnWidths << QString::number(ui->layoutsView->columnWidth(NAMECOLUMN)); - columnWidths << QString::number(ui->layoutsView->columnWidth(MENUCOLUMN)); - columnWidths << QString::number(ui->layoutsView->columnWidth(BORDERSCOLUMN)); - - Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); - - if (inMemoryOption == Latte::Types::MultipleLayouts) { - columnWidths << QString::number(ui->layoutsView->columnWidth(ACTIVITYCOLUMN)); - } else { - //! In Single Mode, keed recorded value for ACTIVITYCOLUMN - QStringList currentWidths = m_corona->universalSettings()->layoutsColumnWidths(); - if (currentWidths.count()>=5) { - columnWidths << currentWidths[4]; - } - } - - m_corona->universalSettings()->setLayoutsColumnWidths(columnWidths); - } + m_corona->universalSettings()->setLayoutsWindowSize(size()); m_inMemoryButtons->deleteLater(); m_mouseSensitivityButtons->deleteLater(); - - for (const auto &tempDir : m_tempDirectories) { - QDir tDir(tempDir); - - if (tDir.exists() && tempDir.startsWith("/tmp/")) { - tDir.removeRecursively(); - } - } } void SettingsDialog::blockDeleteOnActivityStopped() @@ -336,31 +267,6 @@ void SettingsDialog::blockDeleteOnActivityStopped() }); } -QStringList SettingsDialog::availableSharesFor(int row) -{ - QStringList availables; - QStringList regs; - - for (int i = 0; i < m_model->rowCount(); ++i) { - QString id = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString(); - QStringList shares = m_model->data(m_model->index(i, SHAREDCOLUMN), Qt::UserRole).toStringList(); - - if (i != row) { - if (shares.isEmpty()) { - availables << id; - } else { - regs << shares; - } - } - } - - for (const auto r : regs) { - availables.removeAll(r); - } - - return availables; -} - void SettingsDialog::toggleCurrentPage() { if (ui->tabWidget->currentIndex() == 0) { @@ -384,11 +290,7 @@ void SettingsDialog::on_newButton_clicked() QString presetName = CentralLayout::layoutName(preset); if (presetName == "Default") { - QByteArray presetNameChars = presetName.toUtf8(); - const char *prset_str = presetNameChars.data(); - presetName = uniqueLayoutName(i18n(prset_str)); - - addLayoutForFile(preset, presetName, true, false); + m_layoutsController->addLayoutForFile(preset, presetName, true, false); break; } } @@ -398,43 +300,7 @@ void SettingsDialog::on_copyButton_clicked() { qDebug() << Q_FUNC_INFO; - int row = ui->layoutsView->currentIndex().row(); - - if (row < 0) { - return; - } - - //! Update original layout before copying if this layout is active - if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { - QString lName = (m_model->data(m_model->index(row, NAMECOLUMN), Qt::DisplayRole)).toString(); - - Layout::GenericLayout *generic = m_corona->layoutsManager()->synchronizer()->layout(lName); - if (generic) { - generic->syncToLayoutFile(); - } - } - - Settings::Data::Layout original = m_model->at(row); - Settings::Data::Layout copied = original; - - copied.setOriginalName(uniqueLayoutName(m_model->data(m_model->index(row, NAMECOLUMN), Qt::DisplayRole).toString())); - copied.id = uniqueTempDirectory() + "/" + copied.originalName() + ".layout.latte";; - copied.activities = QStringList(); - copied.isLocked = false; - - QFile(original.id).copy(copied.id); - QFileInfo newFileInfo(copied.id); - - if (newFileInfo.exists() && !newFileInfo.isWritable()) { - QFile(copied.id).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); - } - - CentralLayout *settings = new CentralLayout(this, copied.id); - m_layouts[copied.id] = settings; - - m_model->appendLayout(copied); - - ui->layoutsView->selectRow(row + 1); + m_layoutsController->copySelectedLayout(); } void SettingsDialog::on_downloadButton_clicked() @@ -454,7 +320,7 @@ void SettingsDialog::on_downloadButton_clicked() if (version == Layouts::Importer::LayoutVersion2) { layoutAdded = true; - addLayoutForFile(entryFile); + m_layoutsController->addLayoutForFile(entryFile); break; } } @@ -472,40 +338,16 @@ void SettingsDialog::on_removeButton_clicked() { qDebug() << Q_FUNC_INFO; - int row = ui->layoutsView->currentIndex().row(); - - if (row < 0) { - return; - } - - QString layoutName = m_model->data(m_model->index(row, NAMECOLUMN), Qt::DisplayRole).toString(); - - if (m_corona->layoutsManager()->synchronizer()->centralLayout(layoutName)) { - return; - } - - m_model->removeRow(row); + m_layoutsController->removeSelected(); updateApplyButtonsState(); - - row = qMax(row - 1, 0); - - ui->layoutsView->selectRow(row); } void SettingsDialog::on_lockedButton_clicked() { qDebug() << Q_FUNC_INFO; - int row = ui->layoutsView->currentIndex().row(); - - if (row < 0) { - return; - } - - bool lockedModel = m_model->data(m_model->index(row, NAMECOLUMN), Settings::Model::Layouts::LAYOUTISLOCKEDROLE).toBool(); - - m_model->setData(m_model->index(row, NAMECOLUMN), !lockedModel, Settings::Model::Layouts::LAYOUTISLOCKEDROLE); + m_layoutsController->toggleLockedForSelected(); updatePerLayoutButtonsState(); updateApplyButtonsState(); @@ -515,36 +357,7 @@ void SettingsDialog::on_sharedButton_clicked() { qDebug() << Q_FUNC_INFO; - int row = ui->layoutsView->currentIndex().row(); - - if (row < 0) { - return; - } - - if (isShared(row)) { - m_model->setData(m_model->index(row, SHAREDCOLUMN), QStringList(), Qt::UserRole); - } else { - bool assigned{false}; - QStringList assignedList; - - QStringList availableShares = availableSharesFor(row); - - for (const auto &id : availableShares) { - QString name = nameForId(id); - if (m_corona->layoutsManager()->synchronizer()->layout(name)) { - assignedList << id; - m_model->setData(m_model->index(row, SHAREDCOLUMN), assignedList, Qt::UserRole); - assigned = true; - break; - } - } - - if (!assigned && availableShares.count()>0) { - assignedList << availableShares[0]; - m_model->setData(m_model->index(row, SHAREDCOLUMN), assignedList, Qt::UserRole); - assigned = true; - } - } + m_layoutsController->toggleSharedForSelected(); updatePerLayoutButtonsState(); updateApplyButtonsState(); @@ -577,7 +390,7 @@ void SettingsDialog::on_importButton_clicked() qDebug() << "VERSION :::: " << version; if (version == Layouts::Importer::LayoutVersion2) { - addLayoutForFile(file); + m_layoutsController->addLayoutForFile(file); } else if (version == Layouts::Importer::ConfigVersion1) { auto msg = new QMessageBox(this); msg->setIcon(QMessageBox::Warning); @@ -604,7 +417,7 @@ void SettingsDialog::on_importButton_clicked() connect(layoutsBtn, &QPushButton::clicked , this, [ &, file](bool check) { - importLayoutsFromV1ConfigFile(file); + m_layoutsController->importLayoutsFromV1ConfigFile(file); }); connect(fullBtn, &QPushButton::clicked @@ -638,54 +451,13 @@ void SettingsDialog::on_importButton_clicked() fileDialog->open(); } -bool SettingsDialog::importLayoutsFromV1ConfigFile(QString file) -{ - KTar archive(file, QStringLiteral("application/x-tar")); - archive.open(QIODevice::ReadOnly); - - //! if the file isnt a tar archive - if (archive.isOpen()) { - QDir tempDir{uniqueTempDirectory()}; - - const auto archiveRootDir = archive.directory(); - - for (const auto &name : archiveRootDir->entries()) { - auto fileEntry = archiveRootDir->file(name); - fileEntry->copyTo(tempDir.absolutePath()); - } - - QString name = Layouts::Importer::nameOfConfigFile(file); - - QString applets(tempDir.absolutePath() + "/" + "lattedock-appletsrc"); - - if (QFile(applets).exists()) { - if (m_corona->layoutsManager()->importer()->importOldLayout(applets, name, false, tempDir.absolutePath())) { - addLayoutForFile(tempDir.absolutePath() + "/" + name + ".layout.latte", name, false); - } - - QString alternativeName = name + "-" + i18nc("layout", "Alternative"); - - if (m_corona->layoutsManager()->importer()->importOldLayout(applets, alternativeName, false, tempDir.absolutePath())) { - addLayoutForFile(tempDir.absolutePath() + "/" + alternativeName + ".layout.latte", alternativeName, false); - } - } - - return true; - } - - return false; -} - - void SettingsDialog::on_exportButton_clicked() { - int row = ui->layoutsView->currentIndex().row(); - - if (row < 0) { + if (ui->layoutsView->currentIndex().row() < 0) { return; } - QString layoutExported = m_model->data(m_model->index(row, IDCOLUMN), Qt::DisplayRole).toString(); + QString layoutExported = m_layoutsController->selectedLayout().id; //! Update ALL active original layouts before exporting, //! this is needed because the export method can export also the full configuration @@ -790,7 +562,7 @@ void SettingsDialog::requestImagesDialog(int row) QFileDialog dialog(this); dialog.setMimeTypeFilters(mimeTypeFilters); - QString background = m_model->data(m_model->index(row, COLORCOLUMN), Qt::BackgroundRole).toString(); + QString background = "";// m_model->data(m_model->index(row, COLORCOLUMN), Qt::BackgroundRole).toString(); if (background.startsWith("/") && QFileInfo(background).exists()) { dialog.setDirectory(QFileInfo(background).absolutePath()); @@ -801,7 +573,7 @@ void SettingsDialog::requestImagesDialog(int row) QStringList files = dialog.selectedFiles(); if (files.count() > 0) { - m_model->setData(m_model->index(row, COLORCOLUMN), files[0], Qt::BackgroundRole); + // m_model->setData(m_model->index(row, COLORCOLUMN), files[0], Qt::BackgroundRole); } } } @@ -814,7 +586,7 @@ void SettingsDialog::requestColorsDialog(int row) if (dialog.exec()) { qDebug() << dialog.selectedColor().name(); - m_model->setData(m_model->index(row, COLORCOLUMN), dialog.selectedColor().name(), Qt::UserRole); + //m_model->setData(m_model->index(row, COLORCOLUMN), dialog.selectedColor().name(), Qt::UserRole); } } @@ -842,18 +614,15 @@ void SettingsDialog::ok() qDebug() << Q_FUNC_INFO; - if (saveAllChanges()) { - deleteLater(); - } + saveAllChanges(); + deleteLater(); } void SettingsDialog::apply() { qDebug() << Q_FUNC_INFO; - saveAllChanges(); - o_settingsOriginalData = currentSettings(); - o_layoutsOriginalData = m_model->currentData(); + saveAllChanges(); updateApplyButtonsState(); updatePerLayoutButtonsState(); @@ -864,17 +633,7 @@ void SettingsDialog::restoreDefaults() qDebug() << Q_FUNC_INFO; if (ui->tabWidget->currentIndex() == 0) { - //! Default layouts missing from layouts list - for (const auto &preset : m_corona->layoutsManager()->presetsPaths()) { - QString presetName = CentralLayout::layoutName(preset); - QByteArray presetNameChars = presetName.toUtf8(); - const char *prset_str = presetNameChars.data(); - presetName = i18n(prset_str); - - if (!nameExistsInModel(presetName)) { - addLayoutForFile(preset, presetName); - } - } + //! do nothing, should be disabled } else if (ui->tabWidget->currentIndex() == 1) { //! Defaults for general Latte settings ui->autostartChkBox->setChecked(true); @@ -889,181 +648,19 @@ void SettingsDialog::restoreDefaults() } } -void SettingsDialog::addLayoutForFile(QString file, QString layoutName, bool newTempDirectory, bool showNotification) -{ - if (layoutName.isEmpty()) { - layoutName = CentralLayout::layoutName(file); - } - - Settings::Data::Layout copied; - - if (newTempDirectory) { - copied.id = uniqueTempDirectory() + "/" + layoutName + ".layout.latte"; - QFile(file).copy(copied.id); - } else { - copied.id = file; - } - - QFileInfo newFileInfo(copied.id); - - if (newFileInfo.exists() && !newFileInfo.isWritable()) { - QFile(copied.id).setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser | QFileDevice::ReadGroup | QFileDevice::ReadOther); - } - - if (m_layouts.contains(copied.id)) { - CentralLayout *oldSettings = m_layouts.take(copied.id); - delete oldSettings; - } - - CentralLayout *settings = new CentralLayout(this, copied.id); - m_layouts[copied.id] = settings; - - copied.setOriginalName(uniqueLayoutName(layoutName)); - copied.color = settings->color(); - copied.textColor = settings->textColor(); - copied.background = settings->background(); - copied.isLocked = !settings->isWritable(); - copied.isShownInMenu = settings->showInMenu(); - copied.hasDisabledBorders = settings->disableBordersForMaximizedWindows(); - - if (copied.background.isEmpty()) { - copied.textColor = QString(); - } - - m_model->appendLayout(copied); - - // ui->layoutsView->selectRow(row); - - if (showNotification) { - //NOTE: The pointer is automatically deleted when the event is closed - auto notification = new KNotification("import-done", KNotification::CloseOnTimeout); - notification->setText(i18nc("import-done", "Layout: %0 imported successfully
").arg(layoutName)); - notification->sendEvent(); - } -} - void SettingsDialog::loadSettings() { - m_model->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; - - if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { - m_corona->layoutsManager()->synchronizer()->syncActiveLayoutsToOriginalFiles(); - } - - Settings::Data::LayoutsTable layoutsBuffer; - - for (const auto layout : m_corona->layoutsManager()->layouts()) { - Settings::Data::Layout original; - original.id = QDir::homePath() + "/.config/latte/" + layout + ".layout.latte"; - - CentralLayout *central = new CentralLayout(this, original.id); - - original.setOriginalName(central->name()); - original.background = central->background(); - original.color = central->color(); - original.textColor = central->textColor(); - original.isActive = (m_corona->layoutsManager()->synchronizer()->layout(original.originalName()) != nullptr); - original.isLocked = !central->isWritable(); - original.isShownInMenu = central->showInMenu(); - original.hasDisabledBorders = central->disableBordersForMaximizedWindows(); - original.activities = central->activities(); - - //! add central layout properties - if (original.background.isEmpty()) { - original.textColor = QString(); - } - - m_layouts[original.id] = central; - - //! create initial SHARES maps - QString shared = central->sharedLayoutName(); - if (!shared.isEmpty()) { - sharesMap[shared].append(original.id); - } - - layoutsBuffer << original; - - qDebug() << "counter:" << i << " total:" << m_model->rowCount(); - - i++; - - Layout::GenericLayout *generic = m_corona->layoutsManager()->synchronizer()->layout(central->name()); - - if ((generic && generic->layoutIsBroken()) || (!generic && central->layoutIsBroken())) { - brokenLayouts.append(central->name()); - } - } - - //! update SHARES map keys in order to use the #settingsid(s) - QStringList tempSharedNames; - - //! remove these records after updating - for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { - tempSharedNames << i.key(); - } - - //! update keys - for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { - QString shareid = layoutsBuffer.idForOriginalName(i.key()); - if (!shareid.isEmpty()) { - sharesMap[shareid] = i.value(); - } - } - - //! remove deprecated keys - for (const auto &key : tempSharedNames) { - sharesMap.remove(key); - } - - qDebug() << "SHARES MAP ::: " << sharesMap; - - for (QHash::iterator i=sharesMap.begin(); i!=sharesMap.end(); ++i) { - layoutsBuffer[i.key()].shares = i.value(); - } - - //! Send original loaded data to model - m_model->setCurrentData(layoutsBuffer); - - ui->layoutsView->selectRow(rowForName(m_corona->layoutsManager()->currentLayoutName())); - - //! this line should be commented for debugging layouts window functionality - ui->layoutsView->setColumnHidden(IDCOLUMN, true); - ui->layoutsView->setColumnHidden(HIDDENTEXTCOLUMN, true); - - if (m_corona->universalSettings()->canDisableBorders()) { - ui->layoutsView->setColumnHidden(BORDERSCOLUMN, false); - } else { - ui->layoutsView->setColumnHidden(BORDERSCOLUMN, true); - } - - ui->layoutsView->resizeColumnsToContents(); - - QStringList columnWidths = m_corona->universalSettings()->layoutsColumnWidths(); - - if (!columnWidths.isEmpty()) { - for (int i=0; ilayoutsView->setColumnWidth(COLORCOLUMN+i, columnWidths[i].toInt()); - } - } - bool inMultiple{m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts}; + m_layoutsController->loadLayouts(); + m_layoutsController->setOriginalInMultipleMode(inMultiple); + if (inMultiple) { ui->multipleToolBtn->setChecked(true); } else { ui->singleToolBtn->setChecked(true); } - m_model->setInMultipleMode(inMultiple); - updatePerLayoutButtonsState(); ui->autostartChkBox->setChecked(m_corona->universalSettings()->autostart()); @@ -1082,26 +679,13 @@ void SettingsDialog::loadSettings() } o_settingsOriginalData = currentSettings(); - o_layoutsOriginalData = m_model->currentData(); updateApplyButtonsState(); updateSharedLayoutsUiElements(); - - //! there are broken layouts and the user must be informed! - if (brokenLayouts.count() > 0) { - auto msg = new QMessageBox(this); - msg->setIcon(QMessageBox::Warning); - msg->setWindowTitle(i18n("Layout Warning")); - msg->setText(i18n("The layout(s) %0 have broken configuration!!! Please remove them to improve the system stability...").arg(brokenLayouts.join(","))); - msg->setStandardButtons(QMessageBox::Ok); - - msg->open(); - } } QList SettingsDialog::currentSettings() { QList settings; - settings << m_inMemoryButtons->checkedId(); settings << (int)ui->autostartChkBox->isChecked(); settings << (int)ui->badges3DStyleChkBox->isChecked(); settings << (int)ui->infoWindowChkBox->isChecked(); @@ -1111,46 +695,21 @@ QList SettingsDialog::currentSettings() settings << m_mouseSensitivityButtons->checkedId(); settings << ui->screenTrackerSpinBox->value(); settings << ui->outlineSpinBox->value(); - settings << m_model->rowCount(); return settings; } -void SettingsDialog::appendLayout(Settings::Data::Layout &layout) -{ - m_model->appendLayout(layout); -} - void SettingsDialog::on_switchButton_clicked() { - int currentIndex = ui->layoutsView->currentIndex().row(); - QStringList currentActivities = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), ACTIVITYCOLUMN), Qt::UserRole).toStringList(); - - if (ui->buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) { - //! thus there are changes in the settings - - QString lName; - QStringList lActivities; - - if (m_inMemoryButtons->checkedId() == Latte::Types::MultipleLayouts) { - lName = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), NAMECOLUMN), Qt::DisplayRole).toString(); - lActivities = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), ACTIVITYCOLUMN), Qt::UserRole).toStringList(); - } - - apply(); + Settings::Data::Layout selectedLayout = m_layoutsController->selectedLayout(); - if (!lName.isEmpty() && !lActivities.isEmpty()) { - //! an activities-assigned layout is chosen and at the same time we are moving - //! to multiple layouts state - m_corona->layoutsManager()->switchToLayout(lName); + if (m_layoutsController->inMultipleMode()) { + if (selectedLayout.isActive && !m_layoutsController->selectedLayoutIsCurrentActive()) { + m_corona->layoutsManager()->switchToLayout(selectedLayout.originalName()); } } else { - QVariant value = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), NAMECOLUMN), Qt::DisplayRole); - - if (value.isValid()) { - m_corona->layoutsManager()->switchToLayout(value.toString()); - } else { - qDebug() << "not valid layout"; + if (!m_layoutsController->selectedLayoutIsCurrentActive()) { + m_corona->layoutsManager()->switchToLayout(selectedLayout.originalName()); } } @@ -1159,24 +718,23 @@ void SettingsDialog::on_switchButton_clicked() void SettingsDialog::on_pauseButton_clicked() { + Settings::Data::Layout selectedLayout = m_layoutsController->selectedLayout(); ui->pauseButton->setEnabled(false); - QString id = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), IDCOLUMN), Qt::DisplayRole).toString(); - CentralLayout *layout = m_layouts[id]; - - if (layout) { - m_corona->layoutsManager()->synchronizer()->pauseLayout(layout->name()); + if (m_layoutsController->inMultipleMode() + && selectedLayout.isActive + && !selectedLayout.isShared() + && !m_layoutsController->selectedLayoutIsCurrentActive()) { + m_corona->layoutsManager()->synchronizer()->pauseLayout(selectedLayout.originalName()); } } - void SettingsDialog::updateApplyButtonsState() { bool changed{false}; //! Ok, Apply Buttons - if ((o_settingsOriginalData != currentSettings()) - || (o_layoutsOriginalData != m_model->currentData())) { + if ((o_settingsOriginalData != currentSettings()) || (m_layoutsController->dataAreChanged())) { changed = true; } @@ -1190,27 +748,7 @@ void SettingsDialog::updateApplyButtonsState() //! RestoreDefaults Button if (ui->tabWidget->currentIndex() == 0) { - //! Check Default layouts missing from layouts list - - bool layoutMissing{false}; - - for (const auto &preset : m_corona->layoutsManager()->presetsPaths()) { - QString presetName = CentralLayout::layoutName(preset); - QByteArray presetNameChars = presetName.toUtf8(); - const char *prset_str = presetNameChars.data(); - presetName = i18n(prset_str); - - if (!nameExistsInModel(presetName)) { - layoutMissing = true; - break; - } - } - - if (layoutMissing) { - ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(true); - } else { - ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(false); - } + ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setEnabled(false); } else if (ui->tabWidget->currentIndex() == 1) { //! Defaults for general Latte settings @@ -1232,22 +770,12 @@ void SettingsDialog::updateApplyButtonsState() void SettingsDialog::updatePerLayoutButtonsState() { - int currentRow = ui->layoutsView->currentIndex().row(); - - QString id = m_model->data(m_model->index(currentRow, IDCOLUMN), Qt::DisplayRole).toString(); - QString nameInModel = m_model->data(m_model->index(currentRow, NAMECOLUMN), Qt::DisplayRole).toString(); - QString originalName = o_layoutsOriginalData.contains(id) ? o_layoutsOriginalData[id].originalName() : ""; - bool lockedInModel = m_model->data(m_model->index(currentRow, NAMECOLUMN), Qt::UserRole).toBool(); - bool sharedInModel = !m_model->data(m_model->index(currentRow, SHAREDCOLUMN), Qt::UserRole).toStringList().isEmpty(); - bool editable = !isActive(originalName) && !lockedInModel; - - Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); + Settings::Data::Layout selectedLayout = m_layoutsController->selectedLayout(); //! Switch Button - if (id.startsWith("/tmp/") - || originalName != nameInModel - || (inMemoryOption == Types::MultipleLayouts && sharedInModel) - || (m_corona->layoutsManager()->synchronizer()->currentLayoutName() == originalName)) { + if (selectedLayout.nameWasEdited() + || (m_layoutsController->inMultipleMode() && selectedLayout.isShared()) + || (m_layoutsController->selectedLayoutIsCurrentActive())) { ui->switchButton->setEnabled(false); } else { ui->switchButton->setEnabled(true); @@ -1259,11 +787,9 @@ void SettingsDialog::updatePerLayoutButtonsState() } else if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { ui->pauseButton->setVisible(true); - QStringList lActivities = m_model->data(m_model->index(currentRow, ACTIVITYCOLUMN), Qt::UserRole).toStringList(); - - Latte::CentralLayout *layout = m_layouts[id]; - - if (!lActivities.isEmpty() && layout && m_corona->layoutsManager()->synchronizer()->centralLayout(originalName)) { + if (selectedLayout.isActive + && !selectedLayout.isForFreeActivities() + && !selectedLayout.isShared()) { ui->pauseButton->setEnabled(true); } else { ui->pauseButton->setEnabled(false); @@ -1271,59 +797,40 @@ void SettingsDialog::updatePerLayoutButtonsState() } //! Remove Layout Button - if ((originalName == m_corona->layoutsManager()->currentLayoutName()) - || (m_corona->layoutsManager()->synchronizer()->centralLayout(originalName)) - || lockedInModel) { + if (selectedLayout.isActive || selectedLayout.isLocked) { ui->removeButton->setEnabled(false); } else { ui->removeButton->setEnabled(true); } //! Layout Locked Button - if (lockedInModel) { + if (selectedLayout.isLocked) { ui->lockedButton->setChecked(true); } else { ui->lockedButton->setChecked(false); } //! Layout Shared Button - if (sharedInModel) { + if (selectedLayout.isShared()) { ui->sharedButton->setChecked(true); } else { ui->sharedButton->setChecked(false); } - - if (editable) { - m_editLayoutAction->setEnabled(true); - } else { - m_editLayoutAction->setEnabled(false); - } } void SettingsDialog::updateSharedLayoutsUiElements() { //! UI Elements that need to be enabled/disabled - - Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); - if (inMemoryOption == Latte::Types::MultipleLayouts) { - ui->layoutsView->setColumnHidden(SHAREDCOLUMN, false); + if (m_layoutsController->inMultipleMode()) { ui->sharedButton->setVisible(true); - - //! column widths - QStringList cWidths = m_corona->universalSettings()->layoutsColumnWidths(); - - if (cWidths.count()>=5) { - ui->layoutsView->setColumnWidth(ACTIVITYCOLUMN, cWidths[4].toInt()); - } } else { - ui->layoutsView->setColumnHidden(SHAREDCOLUMN, true); ui->sharedButton->setVisible(false); } } bool SettingsDialog::dataAreAccepted() { - for (int i = 0; i < m_model->rowCount(); ++i) { + /* for (int i = 0; i < m_model->rowCount(); ++i) { QString layout1 = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString(); for (int j = i + 1; j < m_model->rowCount(); ++j) { @@ -1352,14 +859,14 @@ bool SettingsDialog::dataAreAccepted() return false; } } - } + }*/ return true; } void SettingsDialog::showLayoutInformation() { - int currentRow = ui->layoutsView->currentIndex().row(); + /* int currentRow = ui->layoutsView->currentIndex().row(); QString id = m_model->data(m_model->index(currentRow, IDCOLUMN), Qt::DisplayRole).toString(); QString name = m_model->data(m_model->index(currentRow, NAMECOLUMN), Qt::DisplayRole).toString(); @@ -1371,12 +878,12 @@ void SettingsDialog::showLayoutInformation() msg->setWindowTitle(name); msg->setText(generic->reportHtml(m_corona->screenPool())); - msg->open(); + msg->open();*/ } void SettingsDialog::showScreensInformation() { - QList assignedScreens; + /* QList assignedScreens; for (int i = 0; i < m_model->rowCount(); ++i) { QString id = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString(); @@ -1398,13 +905,13 @@ void SettingsDialog::showScreensInformation() msg->setWindowTitle(i18n("Screens Information")); msg->setText(m_corona->screenPool()->reportHtml(assignedScreens)); - msg->open(); + msg->open();*/ } -bool SettingsDialog::saveAllChanges() +void SettingsDialog::saveAllChanges() { if (!dataAreAccepted()) { - return false; + return; } //! Update universal settings @@ -1427,376 +934,8 @@ bool SettingsDialog::saveAllChanges() m_corona->themeExtended()->setOutlineWidth(ui->outlineSpinBox->value()); - //! Update Layouts - QStringList knownActivities = m_corona->layoutsManager()->synchronizer()->activities(); - - QTemporaryDir layoutTempDir; - - qDebug() << "Temporary Directory ::: " << layoutTempDir.path(); - - QStringList fromRenamePaths; - QStringList toRenamePaths; - QStringList toRenameNames; - - QString switchToLayout; - - QHash activeLayoutsToRename; - - Settings::Data::LayoutsTable removedLayouts = o_layoutsOriginalData.subtracted(m_model->currentData()); - - //! remove layouts that have been removed from the user - for (int i=0; irowCount(); ++i) { - QString id = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString(); - QString color = m_model->data(m_model->index(i, COLORCOLUMN), Qt::BackgroundRole).toString(); - QString textColor = m_model->data(m_model->index(i, COLORCOLUMN), Qt::UserRole).toString(); - QString name = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString(); - bool locked = m_model->data(m_model->index(i, NAMECOLUMN), Qt::UserRole).toBool(); - bool menu = m_model->data(m_model->index(i, MENUCOLUMN), Qt::DisplayRole).toString() == CheckMark; - bool disabledBorders = m_model->data(m_model->index(i, BORDERSCOLUMN), Qt::DisplayRole).toString() == CheckMark; - QStringList lActivities = m_model->data(m_model->index(i, ACTIVITYCOLUMN), Qt::UserRole).toStringList(); - - QStringList cleanedActivities; - - //!update only activities that are valid - for (const auto &activity : lActivities) { - if (knownActivities.contains(activity) && activity != Settings::Model::Layouts::FREEACTIVITIESID) { - cleanedActivities.append(activity); - } - } - - //qDebug() << i << ". " << id << " - " << color << " - " << name << " - " << menu << " - " << lActivities; - //! update the generic parts of the layouts - bool isOriginalLayout = o_layoutsOriginalData.contains(id); - Layout::GenericLayout *genericActive= isOriginalLayout ?m_corona->layoutsManager()->synchronizer()->layout(o_layoutsOriginalData[id].originalName()) : nullptr; - Layout::GenericLayout *generic = genericActive ? genericActive : m_layouts[id]; - - //! unlock read-only layout - if (!generic->isWritable()) { - generic->unlock(); - } - - if (color.startsWith("/")) { - //it is image file in such case - if (color != generic->background()) { - generic->setBackground(color); - } - - if (generic->textColor() != textColor) { - generic->setTextColor(textColor); - } - } else { - if (color != generic->color()) { - generic->setColor(color); - generic->setBackground(QString()); - generic->setTextColor(QString()); - } - } - - //! update only the Central-specific layout parts - CentralLayout *centralActive = isOriginalLayout ? m_corona->layoutsManager()->synchronizer()->centralLayout(o_layoutsOriginalData[id].originalName()) : nullptr; - CentralLayout *central = centralActive ? centralActive : m_layouts[id]; - - if (central->showInMenu() != menu) { - central->setShowInMenu(menu); - } - - if (central->disableBordersForMaximizedWindows() != disabledBorders) { - central->setDisableBordersForMaximizedWindows(disabledBorders); - } - - if (central->activities() != cleanedActivities) { - central->setActivities(cleanedActivities); - } - - //! If the layout name changed OR the layout path is a temporary one - if (generic->name() != name || (id.startsWith("/tmp/"))) { - //! If the layout is Active in MultipleLayouts - if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts && generic->isActive()) { - qDebug() << " Active Layout Should Be Renamed From : " << generic->name() << " TO :: " << name; - activeLayoutsToRename[name] = generic; - } - - QString tempFile = layoutTempDir.path() + "/" + QString(generic->name() + ".layout.latte"); - qDebug() << "new temp file ::: " << tempFile; - - if ((m_corona->layoutsManager()->memoryUsage() == Types::SingleLayout) && (generic->name() == m_corona->layoutsManager()->currentLayoutName())) { - switchToLayout = name; - } - - generic = m_layouts.take(id); - delete generic; - - QFile(id).rename(tempFile); - - fromRenamePaths.append(id); - toRenamePaths.append(tempFile); - toRenameNames.append(name); - } - } - - //! this is necessary in case two layouts have to swap names - //! so we copy first the layouts in a temp directory and afterwards all - //! together we move them in the official layout directory - for (int i = 0; i < toRenamePaths.count(); ++i) { - QString newFile = QDir::homePath() + "/.config/latte/" + toRenameNames[i] + ".layout.latte"; - QFile(toRenamePaths[i]).rename(newFile); - - CentralLayout *nLayout = new CentralLayout(this, newFile); - m_layouts[newFile] = nLayout; - - //! updating the #SETTINGSID in the model for the layout that was renamed - for (int j = 0; j < m_model->rowCount(); ++j) { - QString tId = m_model->data(m_model->index(j, IDCOLUMN), Qt::DisplayRole).toString(); - - if (tId == fromRenamePaths[i]) { - m_model->setData(m_model->index(j, IDCOLUMN), newFile, Qt::DisplayRole); - } - } - } - - QString orphanedLayout; - - if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { - for (const auto &newLayoutName : activeLayoutsToRename.keys()) { - Layout::GenericLayout *layout = activeLayoutsToRename[newLayoutName]; - qDebug() << " Active Layout of Type: " << layout->type() << " Is Renamed From : " << activeLayoutsToRename[newLayoutName]->name() << " TO :: " << newLayoutName; - layout->renameLayout(newLayoutName); - - if (layout->type() == Layout::Type::Central) { - CentralLayout *central = qobject_cast(layout); - - if (central->activities().isEmpty()) { - //! that means it is an active layout for orphaned Activities - orphanedLayout = newLayoutName; - } - } - } - } - - //! lock layouts in the end when the user has chosen it - for (int i = 0; i < m_model->rowCount(); ++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(); - bool locked = m_model->data(m_model->index(i, NAMECOLUMN), Qt::UserRole).toBool(); - - bool isOriginalLayout{o_layoutsOriginalData.contains(id)}; - Layout::GenericLayout *generic = isOriginalLayout ? m_corona->layoutsManager()->synchronizer()->layout(o_layoutsOriginalData[id].originalName()) : nullptr; - Layout::GenericLayout *layout = generic ? generic : m_layouts[id]; - - if (layout && locked && layout->isWritable()) { - layout->lock(); - } - } - - //! update SharedLayouts that are Active - syncActiveShares(); - - //! reload layouts in layoutsmanager - m_corona->layoutsManager()->synchronizer()->loadLayouts(); - - //! send to layout manager in which layout to switch - Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); - - if (m_corona->layoutsManager()->memoryUsage() != inMemoryOption) { - Types::LayoutsMemoryUsage previousMemoryUsage = m_corona->layoutsManager()->memoryUsage(); - m_corona->layoutsManager()->setMemoryUsage(inMemoryOption); - - QVariant value = m_model->data(m_model->index(ui->layoutsView->currentIndex().row(), NAMECOLUMN), Qt::DisplayRole); - QString layoutName = value.toString(); - - m_corona->layoutsManager()->switchToLayout(layoutName, previousMemoryUsage); - } else { - if (!switchToLayout.isEmpty()) { - m_corona->layoutsManager()->switchToLayout(switchToLayout); - } else if (m_corona->layoutsManager()->memoryUsage() == Types::MultipleLayouts) { - m_corona->layoutsManager()->synchronizer()->syncMultipleLayoutsToActivities(orphanedLayout); - } - } - - return true; -} - -void SettingsDialog::syncActiveShares() -{ - if (m_corona->layoutsManager()->memoryUsage() != Types::MultipleLayouts) { - return; - } - - Settings::Data::LayoutsTable currentLayoutsData = m_model->currentData(); - - Layouts::SharesMap currentSharesNamesMap = currentLayoutsData.sharesMap(); - QStringList originalSharesIds = o_layoutsOriginalData.allSharesIds(); - QStringList currentSharesIds = currentLayoutsData.allSharesIds(); - - QStringList deprecatedSharesIds = Latte::subtracted(originalSharesIds, currentSharesIds); - QStringList deprecatedSharesNames; - - for(int i=0; ilayoutsManager()->synchronizer()->syncActiveShares(currentSharesNamesMap, deprecatedSharesNames); -} - -bool SettingsDialog::idExistsInModel(QString id) -{ - for (int i = 0; i < m_model->rowCount(); ++i) { - QString rowId = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString(); - - if (rowId == id) { - return true; - } - } - - return false; -} - -bool SettingsDialog::nameExistsInModel(QString name) -{ - for (int i = 0; i < m_model->rowCount(); ++i) { - QString rowName = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString(); - - if (rowName == name) { - return true; - } - } - - return false; -} - -bool SettingsDialog::inMultipleLayoutsLook() const -{ - Latte::Types::LayoutsMemoryUsage inMemoryOption = static_cast(m_inMemoryButtons->checkedId()); - return inMemoryOption == Latte::Types::MultipleLayouts; -} - -bool SettingsDialog::isActive(int row) const -{ - QString id = m_model->data(m_model->index(row, IDCOLUMN), Qt::DisplayRole).toString(); - if (o_layoutsOriginalData.contains(id)){ - return (m_corona->layoutsManager()->synchronizer()->layout(o_layoutsOriginalData[id].originalName()) != nullptr); - } - - return false; -} - -bool SettingsDialog::isActive(QString layoutName) const -{ - return (m_corona->layoutsManager()->synchronizer()->layout(layoutName) != nullptr); -} - -bool SettingsDialog::isMenuCell(int column) const -{ - return column == MENUCOLUMN; -} - -bool SettingsDialog::isShared(int row) const -{ - if (row >=0 ) { - QStringList shares = m_model->data(m_model->index(row, SHAREDCOLUMN), Qt::UserRole).toStringList(); - if (!shares.isEmpty()) { - return true; - } - } - - return false; -} - -int SettingsDialog::ascendingRowFor(QString name) -{ - for (int i = 0; i < m_model->rowCount(); ++i) { - QString rowName = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString(); - - if (rowName.toUpper() > name.toUpper()) { - return i; - } - } - - return m_model->rowCount(); -} - -int SettingsDialog::rowForId(QString id) const -{ - for (int i = 0; i < m_model->rowCount(); ++i) { - QString rowId = m_model->data(m_model->index(i, IDCOLUMN), Qt::DisplayRole).toString(); - - if (rowId == id) { - return i; - } - } - - return -1; -} - -int SettingsDialog::rowForName(QString layoutName) const -{ - for (int i = 0; i < m_model->rowCount(); ++i) { - QString rowName = m_model->data(m_model->index(i, NAMECOLUMN), Qt::DisplayRole).toString(); - - if (rowName == layoutName) { - return i; - } - } - - return -1; -} - -QString SettingsDialog::idForRow(int row) const -{ - return m_model->data(m_model->index(row, IDCOLUMN), Qt::DisplayRole).toString(); -} - -QString SettingsDialog::nameForId(QString id) const -{ - int row = rowForId(id); - return m_model->data(m_model->index(row, NAMECOLUMN), Qt::DisplayRole).toString(); -} - -QString SettingsDialog::uniqueTempDirectory() -{ - QTemporaryDir tempDir; - tempDir.setAutoRemove(false); - m_tempDirectories.append(tempDir.path()); - - return tempDir.path(); -} - -QString SettingsDialog::uniqueLayoutName(QString name) -{ - int pos_ = name.lastIndexOf(QRegExp(QString("[-][0-9]+"))); - - if (nameExistsInModel(name) && pos_ > 0) { - name = name.left(pos_); - } - - int i = 2; - - QString namePart = name; - - while (nameExistsInModel(name)) { - name = namePart + "-" + QString::number(i); - i++; - } - - return name; + o_settingsOriginalData = currentSettings(); + m_layoutsController->save(); } }//end of namespace diff --git a/app/settings/settingsdialog.h b/app/settings/settingsdialog.h index 702254ad9..77c60f06c 100644 --- a/app/settings/settingsdialog.h +++ b/app/settings/settingsdialog.h @@ -64,19 +64,6 @@ public: void toggleCurrentPage(); void setCurrentPage(int page); - bool inMultipleLayoutsLook() const; - bool isActive(int row) const; - bool isActive(QString layoutName) const; - bool isShared(int row) const; - bool isMenuCell(int column) const; - - int currentFreeActiviesLayout() const; - - QString nameForId(QString id) const; - QString idForRow(int row) const; - - QStringList availableSharesFor(int row); - void requestImagesDialog(int row); void requestColorsDialog(int row); @@ -104,63 +91,42 @@ private slots: void updatePerLayoutButtonsState(); private: - void addLayoutForFile(QString file, QString layoutName = QString(), bool newTempDirectory = true, bool showNotification = true); //! When an activity is closed for some reason the window manager hides and reshows //! the windows. This function prevents this because we don't want to delete the window //! on reject in such case. void blockDeleteOnActivityStopped(); void loadSettings(); - void recalculateAvailableActivities(); - - void appendLayout(Settings::Data::Layout &layout); void updateApplyButtonsState(); void updateSharedLayoutsUiElements(); - void syncActiveShares(); + void saveAllChanges(); void setCurrentFreeActivitiesLayout(const int &row); bool dataAreAccepted(); - bool idExistsInModel(QString id); - bool importLayoutsFromV1ConfigFile(QString file); - bool nameExistsInModel(QString name); - bool saveAllChanges(); - - int rowForId(QString id) const; - int rowForName(QString layoutName) const; - int ascendingRowFor(QString name); - - QString uniqueTempDirectory(); - QString uniqueLayoutName(QString name); QList currentSettings(); private: - int m_currentFreeActivitiesLayout{-1}; - - QStringList m_tempDirectories; + Latte::Corona *m_corona{nullptr}; + Settings::Model::Layouts *m_model{nullptr}; + Settings::Controller::Layouts *m_layoutsController{nullptr}; + Ui::SettingsDialog *ui; QButtonGroup *m_inMemoryButtons; QButtonGroup *m_mouseSensitivityButtons; - QTimer m_activityClosedTimer; - bool m_blockDeleteOnReject{false}; - - KHelpMenu *m_helpMenu{nullptr}; - - Latte::Corona *m_corona{nullptr}; - QAction *m_editLayoutAction{nullptr}; - //QStandardItemModel *m_model{nullptr}; - Settings::Model::Layouts *m_model{nullptr}; - Settings::Controller::Layouts *m_layoutsController{nullptr}; - Ui::SettingsDialog *ui; + KHelpMenu *m_helpMenu{nullptr}; - QHash m_layouts; + //! workaround to avoid dialog closing when kwin decides faulty to close it + //! because of Activities changes + QTimer m_activityClosedTimer; + bool m_blockDeleteOnReject{false}; + //! original data QList o_settingsOriginalData; - Settings::Data::LayoutsTable o_layoutsOriginalData; }; }