viewsdialog:protect view removal-confirmate dialog

work/spdx
Michail Vourlakos 4 years ago
parent 938dcdef6c
commit 585864df8c

@ -73,6 +73,23 @@ bool ViewsTable::operator!=(const ViewsTable &rhs) const
return !(*this == rhs); return !(*this == rhs);
} }
ViewsTable ViewsTable::subtracted(const ViewsTable &rhs) const
{
ViewsTable subtract;
if ((*this) == rhs) {
return subtract;
}
for(int i=0; i<m_list.count(); ++i) {
if (!rhs.containsId(m_list[i].id)) {
subtract << m_list[i];
}
}
return subtract;
}
void ViewsTable::appendTemporaryView(const Data::View &view) void ViewsTable::appendTemporaryView(const Data::View &view)
{ {
int maxTempId = 0; int maxTempId = 0;

@ -50,6 +50,7 @@ public:
ViewsTable &operator=(ViewsTable &&rhs); ViewsTable &operator=(ViewsTable &&rhs);
bool operator==(const ViewsTable &rhs) const; bool operator==(const ViewsTable &rhs) const;
bool operator!=(const ViewsTable &rhs) const; bool operator!=(const ViewsTable &rhs) const;
ViewsTable subtracted(const ViewsTable &rhs) const;
}; };
} }

@ -21,9 +21,9 @@
#include "genericdialog.h" #include "genericdialog.h"
// Qt // Qt
#include <QDebug>
#include <QFileDialog> #include <QFileDialog>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -64,11 +64,12 @@ KMessageWidget *GenericDialog::initMessageWidget()
return messagewidget; 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); auto msg = new QMessageBox(this);
msg->setIcon(QMessageBox::Warning); msg->setIcon(QMessageBox::Warning);
msg->setWindowTitle(i18n("Apply Settings")); msg->setWindowTitle(i18n("Apply Settings"));
msg->setAttribute(Qt::WA_DeleteOnClose, true);
if (text.isEmpty()) { if (text.isEmpty()) {
msg->setText(i18n("The settings have changed. Do you want to apply the changes or discard them?")); 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()) { if (applyBtnText.isEmpty()) {
msg->setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Cancel); msg->setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Cancel);
msg->setDefaultButton(QMessageBox::Apply); msg->setDefaultButton(defaultButton == QMessageBox::NoButton ? QMessageBox::Apply : defaultButton);
} else if (!applyBtnText.isEmpty()) { return msg->exec();
} else {
msg->setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel); msg->setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
QPushButton *applyCustomBtn = new QPushButton(msg); QPushButton *applyCustomBtn = new QPushButton(msg);
applyCustomBtn->setText(applyBtnText); applyCustomBtn->setText(applyBtnText);
applyCustomBtn->setIcon(QIcon::fromTheme("dialog-yes")); applyCustomBtn->setIcon(QIcon::fromTheme("dialog-yes"));
msg->addButton(applyCustomBtn, QMessageBox::AcceptRole); msg->addButton(applyCustomBtn, QMessageBox::ApplyRole);
msg->setDefaultButton(defaultButton == QMessageBox::NoButton ? QMessageBox::Apply : defaultButton);
if (defaultButton == QMessageBox::NoButton || defaultButton == QMessageBox::Apply) {
msg->setDefaultButton(applyCustomBtn); msg->setDefaultButton(applyCustomBtn);
} else {
msg->setDefaultButton(defaultButton);
} }
connect(msg, &QFileDialog::finished, msg, &QFileDialog::deleteLater); int result = msg->exec();
return msg->exec();
return (msg->clickedButton() == applyCustomBtn ? QMessageBox::Apply : result);
}
} }
void GenericDialog::showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent, QList<QAction *> actions) void GenericDialog::showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent, QList<QAction *> actions)

@ -24,6 +24,7 @@
// Qt // Qt
#include <QAction> #include <QAction>
#include <QDialog> #include <QDialog>
#include <QMessageBox>
#include <QObject> #include <QObject>
#include <QTimer> #include <QTimer>
#include <QWidget> #include <QWidget>
@ -46,7 +47,7 @@ public:
void showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent = false, QList<QAction *> actions = QList<QAction *>()); void showInlineMessage(const QString &msg, const KMessageWidget::MessageType &type, const bool &isPersistent = false, QList<QAction *> actions = QList<QAction *>());
void hideInlineMessage(); void hideInlineMessage();
int saveChangesConfirmation(const QString &text, QString applyBtnText = ""); int saveChangesConfirmation(const QString &text, QString applyBtnText = "", QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
private slots: private slots:
KMessageWidget *initMessageWidget(); KMessageWidget *initMessageWidget();

@ -198,8 +198,23 @@ void Views::onCurrentLayoutChanged()
m_model->setOriginalData(layout.views); 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() 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 originallayout = m_handler->originalData();
Latte::Data::Layout currentlayout = m_handler->currentData(); Latte::Data::Layout currentlayout = m_handler->currentData();
Latte::CentralLayout *centralActive = m_handler->isSelectedLayoutOriginal() ? m_handler->corona()->layoutsManager()->synchronizer()->centralLayout(originallayout.name) : nullptr; Latte::CentralLayout *centralActive = m_handler->isSelectedLayoutOriginal() ? m_handler->corona()->layoutsManager()->synchronizer()->centralLayout(originallayout.name) : nullptr;

@ -64,6 +64,8 @@ public:
bool hasChangedData() const; bool hasChangedData() const;
int viewsForRemovalCount() const;
void sortByColumn(int column, Qt::SortOrder order); void sortByColumn(int column, Qt::SortOrder order);
bool hasSelectedView() const; bool hasSelectedView() const;

@ -225,8 +225,9 @@ void ViewsHandler::resetDefaults()
void ViewsHandler::save() void ViewsHandler::save()
{ {
if (removalConfirmation(m_viewsController->viewsForRemovalCount()) == QMessageBox::Yes) {
m_viewsController->save(); m_viewsController->save();
// m_dialog->layoutsController()->setLayoutProperties(currentData()); }
} }
@ -270,9 +271,16 @@ void ViewsHandler::onCurrentLayoutIndexChanged(int row)
int result = saveChangesConfirmation(); int result = saveChangesConfirmation();
if (result == QMessageBox::Apply) { if (result == QMessageBox::Apply) {
int removalviews = m_viewsController->viewsForRemovalCount();
int removalresponse = removalConfirmation(removalviews);
if ( removalresponse == QMessageBox::Yes) {
switchtonewlayout = true; switchtonewlayout = true;
m_lastConfirmedLayoutIndex = row; m_lastConfirmedLayoutIndex = row;
save(); m_viewsController->save();
} else {
//do nothing
}
} else if (result == QMessageBox::Discard) { } else if (result == QMessageBox::Discard) {
switchtonewlayout = true; switchtonewlayout = true;
m_lastConfirmedLayoutIndex = row; m_lastConfirmedLayoutIndex = row;
@ -301,6 +309,33 @@ void ViewsHandler::updateWindowTitle()
m_dialog->setWindowTitle(i18nc("<layout name> Docks/Panels","%0 Docks/Panels").arg(m_ui->layoutsCmb->currentText())); m_dialog->setWindowTitle(i18nc("<layout name> 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 <b>remove 1</b> dock or panel completely from your layout.<br/><br/> Would you like to continue?");
if (viewsCount > 1) {
removalTxt = i18n ("You are going to <b>remove %0</b> docks and panels completely from your layout.<br/><br/> 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() int ViewsHandler::saveChangesConfirmation()
{ {
if (hasChangedData()) { if (hasChangedData()) {

@ -100,6 +100,7 @@ private:
void loadLayout(const Latte::Data::Layout &data); void loadLayout(const Latte::Data::Layout &data);
int saveChangesConfirmation(); int saveChangesConfirmation();
int removalConfirmation(const int &count);
private: private:
Dialog::ViewsDialog *m_dialog{nullptr}; Dialog::ViewsDialog *m_dialog{nullptr};

@ -180,6 +180,7 @@ void Views::removeView(const QString &id)
if (index >= 0) { if (index >= 0) {
removeRows(index, 1); removeRows(index, 1);
emit rowsRemoved();
} }
} }
@ -196,7 +197,6 @@ bool Views::removeRows(int row, int count, const QModelIndex &parent)
m_viewsTable.remove(firstRow); m_viewsTable.remove(firstRow);
} }
endRemoveRows(); endRemoveRows();
return true; return true;
} }

Loading…
Cancel
Save