Skip to content

Commit

Permalink
Context menu for book rows on right click
Browse files Browse the repository at this point in the history
Added a menu on right click to show options.
Current actions supported:
1. Open Book (if the library is showing local files)
2. Download Book (if the library is showing remote files)
3. Delete Book
  • Loading branch information
juuz0 committed Jun 23, 2023
1 parent 9fed218 commit d3d39c0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 9 deletions.
20 changes: 18 additions & 2 deletions resources/css/_contentManager.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ QTreeView::branch:closed:has-children {
}

QTreeView::item:has-children {
border-bottom: 1px solid #b7bec9;
border-bottom: 1px solid #cccccc;
}

QTreeView {
Expand All @@ -18,7 +18,7 @@ QTreeView {
}

QHeaderView::section {
color: grey;
color: #666666;
background-color: #fff;
border-width: 0px 0px 2px 0px;

Check warning on line 23 in resources/css/_contentManager.css

View check run for this annotation

codefactor.io / CodeFactor

resources/css/_contentManager.css#L23

Expected "0px 0px 2px 0px" to be "0px 0px 2px" (shorthand-property-no-redundant-values)
border-color: black;
Expand All @@ -44,3 +44,19 @@ QHeaderView::up-arrow {
image: url(:/icons/caret-up-solid.svg);
margin: 5px;
}

QMenu {
background-color: white;
margin: 2px;
font-family: 'Selawik';
}

QMenu::item {
padding: 2px 25px 2px 20px;
border-bottom: 1px solid #cccccc;
}

QMenu::item:selected {
background-color: #cccccc;
}

35 changes: 35 additions & 0 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <zim/item.h>
#include <QHeaderView>
#include "contentmanagerdelegate.h"
#include "node.h"

ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
Expand Down Expand Up @@ -57,6 +58,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
managerModel->setBooksData(nBookList);
});
connect(&m_remoteLibraryManager, &OpdsRequestManager::requestReceived, this, &ContentManager::updateRemoteLibrary);
connect(mp_view, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &)));
}

QList<QMap<QString, QVariant>> ContentManager::getBooksList()
Expand Down Expand Up @@ -85,6 +87,39 @@ QList<QMap<QString, QVariant>> ContentManager::getBooksList()
return bookList;
}

void ContentManager::onCustomContextMenu(const QPoint &point)
{
QModelIndex index = mp_view->indexAt(point);
QMenu contextMenu("optionsMenu", mp_view);
Node* bookNode = static_cast<Node*>(index.internalPointer());
const auto id = bookNode->getBookId();

QAction menuDeleteBook("Delete book", this);
QAction menuOpenBook("Open book", this);
QAction menuDownloadBook("Download book", this);

connect(&menuDeleteBook, &QAction::triggered, [=]() {
eraseBook(id);
emit(booksChanged());
});
connect(&menuOpenBook, &QAction::triggered, [=]() {
openBook(id);
});
connect(&menuDownloadBook, &QAction::triggered, [=]() {
downloadBook(id);
});

if (m_local)
contextMenu.addAction(&menuOpenBook);
else
contextMenu.addAction(&menuDownloadBook);
contextMenu.addAction(&menuDeleteBook);

if (index.isValid()) {
contextMenu.exec(mp_view->viewport()->mapToGlobal(point));
}
}

void ContentManager::setLocal(bool local) {
if (local == m_local) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public slots:
void pauseBook(const QString& id);
void resumeBook(const QString& id);
void cancelBook(const QString& id);
void onCustomContextMenu(const QPoint &point);
};

#endif // CONTENTMANAGER_H
2 changes: 1 addition & 1 deletion src/contentmanagerdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void ContentManagerDelegate::paint(QPainter *painter, const QStyleOptionViewItem
return;
} else if (index.column() == 1) {
auto bFont = painter->font();
bFont.setBold(true);
bFont.setWeight(60);
eOpt.font = bFont;
}
QStyledItemDelegate::paint(painter, eOpt, index);
Expand Down
5 changes: 2 additions & 3 deletions src/contentmanagermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ void ContentManagerModel::setupNodes()
auto id = bookItem["id"].toString();
auto description = bookItem["description"].toString();
auto icon = bookItem["icon"];
const auto temp = new Node({icon, name, date, size, content, id}, rootNode);
const auto tempsTemp = new Node({"", description, "", "", "", ""}, temp, true);
const auto temp = new Node({icon, name, date, size, content, id}, rootNode, id);
const auto tempsTemp = new Node({"", description, "", "", "", ""}, temp, "", true);
temp->appendChild(tempsTemp);
rootNode->appendChild(temp);
}
Expand Down Expand Up @@ -187,4 +187,3 @@ void ContentManagerModel::sort(int column, Qt::SortOrder order)
emit layoutChanged();

Check notice on line 188 in src/contentmanagermodel.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/contentmanagermodel.cpp#L188

Redundant blank line at the end of a code block should be deleted. (whitespace/blank_line)
}

1 change: 1 addition & 0 deletions src/contentmanagerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ ContentManagerView::ContentManagerView(QWidget *parent)
file.open(QFile::ReadOnly);
QString styleSheet = QString(file.readAll());
setStyleSheet(styleSheet);
setContextMenuPolicy(Qt::CustomContextMenu);
}
4 changes: 2 additions & 2 deletions src/node.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "node.h"

Node::Node(const QList<QVariant> &data, Node *parent, bool isAdditional)
: m_itemData(data), m_parentItem(parent), m_isAdditonal(isAdditional)
Node::Node(const QList<QVariant> &data, Node *parent, QString bookId, bool isAdditional)
: m_itemData(data), m_parentItem(parent), m_isAdditonal(isAdditional), m_bookId(bookId)
{}

Node::~Node()
Expand Down
4 changes: 3 additions & 1 deletion src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Node
{
public:
explicit Node(const QList<QVariant> &data, Node *parentItem = nullptr, bool isAdditional = false);
explicit Node(const QList<QVariant> &data, Node *parentItem = nullptr, QString bookId = "", bool isAdditional = false);
~Node();
void appendChild(Node *child);
Node *child(int row);
Expand All @@ -18,13 +18,15 @@ class Node
int row() const;
Node *parentItem();
bool isAdditonal() const { return m_isAdditonal; }
QString getBookId() const { return m_bookId; }
friend class ContentManagerModel;

private:
QList<QVariant> m_itemData;
Node *m_parentItem;
QList<Node *> m_childItems;
bool m_isAdditonal;
QString m_bookId;
};


Expand Down

0 comments on commit d3d39c0

Please sign in to comment.