diff --git a/app/data/viewstable.cpp b/app/data/viewstable.cpp index 024ccda71..797106954 100644 --- a/app/data/viewstable.cpp +++ b/app/data/viewstable.cpp @@ -73,6 +73,23 @@ bool ViewsTable::operator!=(const ViewsTable &rhs) const return !(*this == rhs); } +ViewsTable ViewsTable::subtracted(const ViewsTable &rhs) const +{ + ViewsTable subtract; + + if ((*this) == rhs) { + return subtract; + } + + for(int i=0; i #include #include -#include #include #include @@ -64,11 +64,12 @@ KMessageWidget *GenericDialog::initMessageWidget() return messagewidget; } -int GenericDialog::saveChangesConfirmation(const QString &text, QString applyBtnText) +int GenericDialog::saveChangesConfirmation(const QString &text, QString applyBtnText, QMessageBox::StandardButton defaultButton) { auto msg = new QMessageBox(this); msg->setIcon(QMessageBox::Warning); msg->setWindowTitle(i18n("Apply Settings")); + msg->setAttribute(Qt::WA_DeleteOnClose, true); if (text.isEmpty()) { msg->setText(i18n("The settings have changed. Do you want to apply the changes or discard them?")); @@ -78,18 +79,26 @@ int GenericDialog::saveChangesConfirmation(const QString &text, QString applyBtn if (applyBtnText.isEmpty()) { msg->setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Cancel); - msg->setDefaultButton(QMessageBox::Apply); - } else if (!applyBtnText.isEmpty()) { + msg->setDefaultButton(defaultButton == QMessageBox::NoButton ? QMessageBox::Apply : defaultButton); + return msg->exec(); + } else { msg->setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel); QPushButton *applyCustomBtn = new QPushButton(msg); applyCustomBtn->setText(applyBtnText); applyCustomBtn->setIcon(QIcon::fromTheme("dialog-yes")); - msg->addButton(applyCustomBtn, QMessageBox::AcceptRole); - msg->setDefaultButton(applyCustomBtn); - } + msg->addButton(applyCustomBtn, QMessageBox::ApplyRole); + msg->setDefaultButton(defaultButton == QMessageBox::NoButton ? QMessageBox::Apply : defaultButton); + + if (defaultButton == QMessageBox::NoButton || defaultButton == QMessageBox::Apply) { + msg->setDefaultButton(applyCustomBtn); + } else { + msg->setDefaultButton(defaultButton); + } + + int result = msg->exec(); - connect(msg, &QFileDialog::finished, msg, &QFileDialog::deleteLater); - return msg->exec(); + return (msg->clickedButton() == applyCustomBtn ? QMessageBox::Apply : result); + } } void GenericDialog::showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent, QList actions) diff --git a/app/settings/generic/genericdialog.h b/app/settings/generic/genericdialog.h index 87a772319..c21a1f672 100644 --- a/app/settings/generic/genericdialog.h +++ b/app/settings/generic/genericdialog.h @@ -24,6 +24,7 @@ // Qt #include #include +#include #include #include #include @@ -46,7 +47,7 @@ public: void showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent = false, QList actions = QList()); void hideInlineMessage(); - int saveChangesConfirmation(const QString &text, QString applyBtnText = ""); + int saveChangesConfirmation(const QString &text, QString applyBtnText = "", QMessageBox::StandardButton defaultButton = QMessageBox::NoButton); private slots: KMessageWidget *initMessageWidget(); diff --git a/app/settings/viewsdialog/viewscontroller.cpp b/app/settings/viewsdialog/viewscontroller.cpp index eb78b72ea..f0fbf16c7 100644 --- a/app/settings/viewsdialog/viewscontroller.cpp +++ b/app/settings/viewsdialog/viewscontroller.cpp @@ -198,8 +198,23 @@ void Views::onCurrentLayoutChanged() m_model->setOriginalData(layout.views); } +int Views::viewsForRemovalCount() const +{ + if (!hasChangedData()) { + return 0; + } + + Latte::Data::ViewsTable originalViews = m_model->originalViewsData(); + Latte::Data::ViewsTable currentViews = m_model->currentViewsData(); + Latte::Data::ViewsTable removedViews = originalViews.subtracted(currentViews); + + return removedViews.rowCount(); +} + void Views::save() { + //! when this function is called we consider that removal has already been approved + Latte::Data::Layout originallayout = m_handler->originalData(); Latte::Data::Layout currentlayout = m_handler->currentData(); Latte::CentralLayout *centralActive = m_handler->isSelectedLayoutOriginal() ? m_handler->corona()->layoutsManager()->synchronizer()->centralLayout(originallayout.name) : nullptr; diff --git a/app/settings/viewsdialog/viewscontroller.h b/app/settings/viewsdialog/viewscontroller.h index d5e91abe3..0808b02ad 100644 --- a/app/settings/viewsdialog/viewscontroller.h +++ b/app/settings/viewsdialog/viewscontroller.h @@ -64,6 +64,8 @@ public: bool hasChangedData() const; + int viewsForRemovalCount() const; + void sortByColumn(int column, Qt::SortOrder order); bool hasSelectedView() const; diff --git a/app/settings/viewsdialog/viewshandler.cpp b/app/settings/viewsdialog/viewshandler.cpp index 64bebf6d8..9c192aa74 100644 --- a/app/settings/viewsdialog/viewshandler.cpp +++ b/app/settings/viewsdialog/viewshandler.cpp @@ -225,8 +225,9 @@ void ViewsHandler::resetDefaults() void ViewsHandler::save() { - m_viewsController->save(); - // m_dialog->layoutsController()->setLayoutProperties(currentData()); + if (removalConfirmation(m_viewsController->viewsForRemovalCount()) == QMessageBox::Yes) { + m_viewsController->save(); + } } @@ -270,9 +271,16 @@ void ViewsHandler::onCurrentLayoutIndexChanged(int row) int result = saveChangesConfirmation(); if (result == QMessageBox::Apply) { - switchtonewlayout = true; - m_lastConfirmedLayoutIndex = row; - save(); + int removalviews = m_viewsController->viewsForRemovalCount(); + int removalresponse = removalConfirmation(removalviews); + + if ( removalresponse == QMessageBox::Yes) { + switchtonewlayout = true; + m_lastConfirmedLayoutIndex = row; + m_viewsController->save(); + } else { + //do nothing + } } else if (result == QMessageBox::Discard) { switchtonewlayout = true; m_lastConfirmedLayoutIndex = row; @@ -301,6 +309,33 @@ void ViewsHandler::updateWindowTitle() m_dialog->setWindowTitle(i18nc(" Docks/Panels","%0 Docks/Panels").arg(m_ui->layoutsCmb->currentText())); } +int ViewsHandler::removalConfirmation(const int &viewsCount) +{ + if (viewsCount<=0) { + return QMessageBox::Yes; + } + + if (hasChangedData()) { + QString removalTxt = i18n("You are going to remove 1 dock or panel completely from your layout.

Would you like to continue?"); + + if (viewsCount > 1) { + removalTxt = i18n ("You are going to remove %0 docks and panels completely from your layout.

Would you like to continue?").arg(viewsCount); + } + + auto msg = new QMessageBox(m_dialog); + msg->setIcon(QMessageBox::Warning); + msg->setWindowTitle(i18n("Approve Removal")); + msg->setText(removalTxt); + msg->setStandardButtons(QMessageBox::Yes | QMessageBox::No); + msg->setDefaultButton(QMessageBox::No); + msg->setAttribute(Qt::WA_DeleteOnClose, true); + + return msg->exec(); + } + + return QMessageBox::Yes; +} + int ViewsHandler::saveChangesConfirmation() { if (hasChangedData()) { diff --git a/app/settings/viewsdialog/viewshandler.h b/app/settings/viewsdialog/viewshandler.h index b9489a6bc..ffb3dc1e4 100644 --- a/app/settings/viewsdialog/viewshandler.h +++ b/app/settings/viewsdialog/viewshandler.h @@ -100,6 +100,7 @@ private: void loadLayout(const Latte::Data::Layout &data); int saveChangesConfirmation(); + int removalConfirmation(const int &count); private: Dialog::ViewsDialog *m_dialog{nullptr}; diff --git a/app/settings/viewsdialog/viewsmodel.cpp b/app/settings/viewsdialog/viewsmodel.cpp index 660799583..cee832ae4 100644 --- a/app/settings/viewsdialog/viewsmodel.cpp +++ b/app/settings/viewsdialog/viewsmodel.cpp @@ -180,6 +180,7 @@ void Views::removeView(const QString &id) if (index >= 0) { removeRows(index, 1); + emit rowsRemoved(); } } @@ -196,7 +197,6 @@ bool Views::removeRows(int row, int count, const QModelIndex &parent) m_viewsTable.remove(firstRow); } endRemoveRows(); - return true; }