Skip to content

Commit

Permalink
Confirmation dialog box for delete book and cancel download
Browse files Browse the repository at this point in the history
Added a new custom dialog box for confirmations.
Currently, it popups when a book is deleted or a download has to be cancelled
  • Loading branch information
juuz0 committed Jun 30, 2023
1 parent 8b7d65c commit 6644bd3
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 19 deletions.
3 changes: 3 additions & 0 deletions kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SOURCES += \
src/contentmanagermodel.cpp \
src/contenttypefilter.cpp \
src/findinpagebar.cpp \
src/kiwixconfirmbox.cpp \
src/node.cpp \
src/suggestionlistworker.cpp \
src/thumbnaildownloader.cpp \
Expand Down Expand Up @@ -76,6 +77,7 @@ HEADERS += \
src/contentmanagerview.h \
src/contenttypefilter.h \
src/findinpagebar.h \
src/kiwixconfirmbox.h \
src/node.h \
src/suggestionlistworker.h \
src/thumbnaildownloader.h \
Expand Down Expand Up @@ -111,6 +113,7 @@ HEADERS += \
FORMS += \
src/contentmanagerview.ui \
src/findinpagebar.ui \
ui/kiwixconfirmbox.ui \
ui/mainwindow.ui \
ui/about.ui \
src/contentmanagerside.ui \
Expand Down
27 changes: 27 additions & 0 deletions resources/css/confirmBox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
* {
font-family: 'Selawik';
background-color: white;
}

#confirmTitle {
font-size: 18px;
line-height: 44px;
font-weight: bold;
}

QPushButton {
opacity: 1;
padding: 6px;
outline: 0;
border-radius: 2px;
width: 24px;
border: 1px solid #3366cc;
color: #3366cc;
}

QPushButton:hover {
background-color: #3366cc;
color: white;
}


6 changes: 5 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,9 @@
"monitor-clear-dir-dialog-msg":"This will stop checking the monitor directory for new ZIM files.",
"monitor-directory-tooltip":"All ZIM files in this directory will be automatically added to the library.",
"next-tab":"Move to next tab",
"previous-tab":"Move to previous tab"
"previous-tab":"Move to previous tab",
"cancel-download": "Cancel Download",
"cancel-download-text": "Are you sure you want to cancel the download of <b>{{ZIM}}</b>?",
"delete-book": "Delete book",
"delete-book-text": "Are you sure you want to delete <b>{{ZIM}}</b>?"
}
1 change: 1 addition & 0 deletions resources/style.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<file>css/style.css</file>
<file>css/popup.css</file>
<file>css/localServer.css</file>
<file>css/confirmBox.css</file>
</qresource>
</RCC>
57 changes: 39 additions & 18 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QHeaderView>
#include "contentmanagerdelegate.h"
#include "node.h"
#include "kiwixconfirmbox.h"

ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
Expand Down Expand Up @@ -113,7 +114,6 @@ void ContentManager::onCustomContextMenu(const QPoint &point)

connect(&menuDeleteBook, &QAction::triggered, [=]() {
eraseBook(id);
emit(booksChanged());
});
connect(&menuOpenBook, &QAction::triggered, [=]() {
openBook(id);
Expand Down Expand Up @@ -395,21 +395,32 @@ void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QSt

void ContentManager::eraseBook(const QString& id)
{
auto tabBar = KiwixApp::instance()->getTabWidget();
tabBar->closeTabsByZimId(id);
kiwix::Book book = mp_library->getBookById(id);
QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath()));
QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, fileName);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit mp_library->bookmarksChanged();
if (m_local) {
emit(bookRemoved(id));
} else {
emit(oneBookChanged(id));
}
KiwixApp::instance()->getSettingsManager()->deleteSettings(id);
auto text = gt("delete-book-text");
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("delete-book"), text, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
auto tabBar = KiwixApp::instance()->getTabWidget();
tabBar->closeTabsByZimId(id);
kiwix::Book book = mp_library->getBookById(id);
QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath()));
QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, fileName);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit mp_library->bookmarksChanged();
if (m_local) {
emit(bookRemoved(id));
} else {
emit(oneBookChanged(id));
}
KiwixApp::instance()->getSettingsManager()->deleteSettings(id);
dialog->deleteLater();
emit booksChanged();
});
connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
dialog->deleteLater();
});
}

void ContentManager::pauseBook(const QString& id, QModelIndex index)
Expand Down Expand Up @@ -448,8 +459,18 @@ void ContentManager::resumeBook(const QString& id)

void ContentManager::cancelBook(const QString& id, QModelIndex index)
{
cancelBook(id);
emit managerModel->cancelDownload(index);
auto text = gt("cancel-download-text");
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("cancel-download"), text, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
cancelBook(id);
emit managerModel->cancelDownload(index);
dialog->deleteLater();
});
connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
dialog->deleteLater();
});
}

void ContentManager::cancelBook(const QString& id)
Expand Down
32 changes: 32 additions & 0 deletions src/kiwixconfirmbox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "kiwixconfirmbox.h"
#include "ui_kiwixconfirmbox.h"
#include <QFile>
#include <QDebug>

KiwixConfirmBox::KiwixConfirmBox(QString confirmTitle, QString confirmText, QWidget *parent) :
QDialog(parent), m_confirmTitle(confirmTitle), m_confirmText(confirmText),
ui(new Ui::kiwixconfirmbox)
{
ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint, true);

QFile styleFile(":/css/confirmBox.css");
styleFile.open(QIODevice::ReadOnly);
auto byteContent = styleFile.readAll();
styleFile.close();
QString style(byteContent);
setStyleSheet(style);
connect(ui->yesButton, &QPushButton::clicked, [=]() {
emit yesClicked();
});
connect(ui->noButton, &QPushButton::clicked, [=]() {
emit noClicked();
});
ui->confirmText->setText(confirmText);
ui->confirmTitle->setText(confirmTitle);
}

KiwixConfirmBox::~KiwixConfirmBox()
{
delete ui;
}
28 changes: 28 additions & 0 deletions src/kiwixconfirmbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef KIWIXCONFIRMBOX_H
#define KIWIXCONFIRMBOX_H

#include <QDialog>

namespace Ui {
class kiwixconfirmbox;
}

class KiwixConfirmBox : public QDialog
{
Q_OBJECT

public:
explicit KiwixConfirmBox(QString confirmTitle, QString confirmText, QWidget *parent = nullptr);
~KiwixConfirmBox();

signals:
void yesClicked();
void noClicked();

private:
QString m_confirmTitle;
QString m_confirmText;
Ui::kiwixconfirmbox *ui;
};

#endif // KIWIXCONFIRMBOX_H
129 changes: 129 additions & 0 deletions ui/kiwixconfirmbox.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>kiwixconfirmbox</class>
<widget class="QDialog" name="kiwixconfirmbox">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>368</width>
<height>166</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>474</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="confirmTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Confirm title</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="confirmText">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>600</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Would you like to confirm doing xyz?</string>
</property>
</widget>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>250</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="yesButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Yes</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="noButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>No</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit 6644bd3

Please sign in to comment.