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);
}
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)
{
int maxTempId = 0;

@ -50,6 +50,7 @@ public:
ViewsTable &operator=(ViewsTable &&rhs);
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"
// Qt
#include <QDebug>
#include <QFileDialog>
#include <QKeyEvent>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
@ -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<QAction *> actions)

@ -24,6 +24,7 @@
// Qt
#include <QAction>
#include <QDialog>
#include <QMessageBox>
#include <QObject>
#include <QTimer>
#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 hideInlineMessage();
int saveChangesConfirmation(const QString &text, QString applyBtnText = "");
int saveChangesConfirmation(const QString &text, QString applyBtnText = "", QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
private slots:
KMessageWidget *initMessageWidget();

@ -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;

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

@ -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("<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()
{
if (hasChangedData()) {

@ -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};

@ -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;
}

Loading…
Cancel
Save