BibleTime
btbookshelfview.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btbookshelfview.h"
14 
15 #include <QAbstractItemModel>
16 #include <QKeyEvent>
17 #include <QModelIndex>
18 #include <QMouseEvent>
19 #include <QPoint>
20 #include <QRect>
21 #include <QVariant>
22 #include <QWidget>
23 #include <Qt>
24 #include "../backend/bookshelfmodel/btbookshelfmodel.h"
25 #include "../util/btconnect.h"
26 
27 
29  : QTreeView(parent)
30 {
31  setHeaderHidden(true);
32 
33  /*
34  Uncommenting the following statement will hide the [+]expand/[-]collapse
35  controls in front of first-level items, which might not be desirable since
36  hiding them also means that one has to do a total of 2 clicks (double-
37  click)to expand or collapse top-level items:
38  */
39  // setRootIsDecorated(false);
40 
41  BT_CONNECT(this, &BtBookshelfView::activated,
42  [this](QModelIndex const & index) {
43  if (auto * const module = getModule(index))
44  Q_EMIT moduleActivated(module);
45  });
46  BT_CONNECT(this, &BtBookshelfView::entered,
47  [this](QModelIndex const & index)
48  { Q_EMIT moduleHovered(getModule(index)); });
49 }
50 
51 CSwordModuleInfo * BtBookshelfView::getModule(const QModelIndex & index) const {
52  void * const module = model()->data(index,
53  BtBookshelfModel::ModulePointerRole).value<void *>();
54  return static_cast<CSwordModuleInfo *>(module);
55 }
56 
57 void BtBookshelfView::keyPressEvent(QKeyEvent *event) {
58  switch (event->key()) {
59  case Qt::Key_Menu:
60  scrollTo(currentIndex());
61  {
62  CSwordModuleInfo *i(getModule(currentIndex()));
63  QRect itemRect(visualRect(currentIndex()));
64  QPoint p(viewport()->mapToGlobal(itemRect.bottomLeft()));
65  if (i == nullptr) {
66  Q_EMIT contextMenuActivated(p);
67  }
68  else {
69  Q_EMIT moduleContextMenuActivated(i, p);
70  }
71  }
72  event->accept();
73  break;
74  case Qt::Key_Return:
75  case Qt::Key_Enter: {
76  QModelIndex i(currentIndex());
78  if (m != nullptr) {
79  Q_EMIT moduleActivated(m);
80  }
81  else {
82  setExpanded(i, !isExpanded(i));
83  }
84  }
85  event->accept();
86  break;
87  default:
88  QTreeView::keyPressEvent(event);
89  break;
90  }
91 }
92 
93 void BtBookshelfView::mousePressEvent(QMouseEvent *event) {
94  if (event->buttons() == Qt::RightButton) {
95  QModelIndex clickedItemIndex(indexAt(event->pos()));
96  if (clickedItemIndex.isValid()) {
97  setCurrentIndex(clickedItemIndex);
98  }
99  CSwordModuleInfo *i(getModule(clickedItemIndex));
100  if (i == nullptr) {
101  Q_EMIT contextMenuActivated(mapToGlobal(event->pos()));
102  }
103  else {
104  Q_EMIT moduleContextMenuActivated(i, mapToGlobal(event->pos()));
105  }
106  event->accept();
107  }
108  else {
109  QTreeView::mousePressEvent(event);
110  }
111 }
#define BT_CONNECT(...)
Definition: btconnect.h:20
void moduleHovered(CSwordModuleInfo *item)
void moduleActivated(CSwordModuleInfo *item)
void mousePressEvent(QMouseEvent *event) override
void contextMenuActivated(QPoint pos)
void moduleContextMenuActivated(CSwordModuleInfo *item, QPoint pos)
void keyPressEvent(QKeyEvent *event) override
CSwordModuleInfo * getModule(const QModelIndex &index) const
BtBookshelfView(QWidget *parent=nullptr)