BibleTime
csearchresultview.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 "csearchresultview.h"
14 
15 #include <QContextMenuEvent>
16 #include <QList>
17 #include <QMenu>
18 #include <QTreeWidget>
19 #include <QTreeWidgetItem>
20 #include <QWidget>
21 #include "../../backend/config/btconfig.h"
22 #include "../../backend/drivers/cswordmoduleinfo.h"
23 #include "../../backend/keys/cswordkey.h"
24 #include "../../util/btconnect.h"
25 #include "../../util/cresmgr.h"
26 #include "../BtMimeData.h"
27 #include "../cexportmanager.h"
28 
29 
30 namespace Search {
31 
33  : QTreeWidget(parent),
34  m_module(nullptr) {
35  initView();
37 }
38 
39 /** Initializes the view of this widget. */
41  setToolTip(tr("Search result of the selected work"));
42  setHeaderLabel(tr("Results"));
43  setDragEnabled(true);
44  setRootIsDecorated( false );
45  setSelectionMode(QAbstractItemView::ExtendedSelection);
46 
47  //setup the popup menu
48  m_popup = new QMenu(this);
49 
50  m_actions.copyMenu = new QMenu(tr("Copy..."), m_popup);
51  m_actions.copyMenu->setIcon(CResMgr::searchdialog::result::foundItems::copyMenu::icon());
52 
53  struct SelectedKeysList : QList<CSwordKey *> {
54  SelectedKeysList(CSearchResultView & self) {
55  auto const * const m = self.m_module;
56  auto const selectedItems(self.selectedItems());
57  reserve(selectedItems.size());
58  try {
59  for (auto const * const i : self.selectedItems()) {
60  append(m->createKey());
61  last()->setKey(i->text(0));
62  }
63  } catch (...) {
64  qDeleteAll(*this);
65  }
66  }
67 
68  ~SelectedKeysList() noexcept { qDeleteAll(*this); }
69  };
70 
71  m_actions.copy.result = new QAction(tr("Reference only"), this);
72  BT_CONNECT(m_actions.copy.result, &QAction::triggered,
73  [this]{
74  SelectedKeysList keys(*this);
75  CExportManager(true, tr("Copying search result"))
76  .copyKeyList(keys, CExportManager::Text, false);
77  });
78  m_actions.copyMenu->addAction(m_actions.copy.result);
79 
80  m_actions.copy.resultWithText = new QAction(tr("Reference with text"), this);
81  BT_CONNECT(m_actions.copy.resultWithText, &QAction::triggered,
82  [this]{
83  SelectedKeysList keys(*this);
84  CExportManager(true, tr("Copying search result"))
85  .copyKeyList(keys, CExportManager::Text, true);
86  });
87  m_actions.copyMenu->addAction(m_actions.copy.resultWithText);
88 
89  m_popup->addMenu(m_actions.copyMenu);
90 
91  m_actions.saveMenu = new QMenu(tr("Save..."), m_popup);
92  m_actions.saveMenu->setIcon(CResMgr::searchdialog::result::foundItems::saveMenu::icon());
93 
94  m_actions.save.result = new QAction(tr("Reference only"), this);
95  BT_CONNECT(m_actions.save.result, &QAction::triggered,
96  [this]{
97  SelectedKeysList keys(*this);
98  CExportManager(true, tr("Saving search result"))
99  .saveKeyList( keys, CExportManager::Text, false);
100  });
101  m_actions.saveMenu->addAction(m_actions.save.result);
102 
103  m_actions.save.resultWithText = new QAction(tr("Reference with text"), this);
104  m_actions.saveMenu->addAction(m_actions.save.resultWithText);
105  BT_CONNECT(m_actions.save.resultWithText, &QAction::triggered,
106  [this]{
107  SelectedKeysList keys(*this);
108  CExportManager(true, tr("Saving search result"))
109  .saveKeyList(keys, CExportManager::Text, true);
110  });
111  m_popup->addMenu(m_actions.saveMenu);
112 
113  m_actions.printMenu = new QMenu(tr("Print..."), m_popup);
114  m_actions.printMenu->setIcon(CResMgr::searchdialog::result::foundItems::printMenu::icon());
115 
116  m_actions.print.result = new QAction(tr("Reference with text"), this);
117  BT_CONNECT(m_actions.print.result, &QAction::triggered,
118  [this]{
119  QStringList list;
120  for (auto const * const k : selectedItems())
121  list.append(k->text(0));
122  CExportManager(true, tr("Printing search result"))
123  .printKeyList(list,
124  m_module,
125  btConfig().getDisplayOptions(),
126  btConfig().getFilterOptions());
127  });
128  m_actions.printMenu->addAction(m_actions.print.result);
129  m_popup->addMenu(m_actions.printMenu);
130 }
131 
132 /** No descriptions */
134  /// \todo are these right after porting?
135  //items: current, previous
136  BT_CONNECT(this, &CSearchResultView::currentItemChanged,
137  [this](QTreeWidgetItem * const current, QTreeWidgetItem *) {
138  if (current) {
139  Q_EMIT keySelected(current->text(0));
140  } else {
141  Q_EMIT keyDeselected();
142  }
143  });
144 }
145 
146 /** Setups the list with the given module. */
148  CSwordModuleInfo const * m,
150 {
151  clear();
152 
153  if (!m) return;
154 
155  m_module = m;
156  if (result.empty())
157  return;
158 
159  setUpdatesEnabled(false);
160 
161  QTreeWidgetItem* oldItem = nullptr;
162  QTreeWidgetItem* item = nullptr;
163  for (auto const & keyPtr : result) {
164  item = new QTreeWidgetItem(this, oldItem);
165  item->setText(0, QString::fromUtf8(keyPtr->getText()));
166  oldItem = item;
167  }
168 
169  setUpdatesEnabled(true);
170  //pre-select the first item
171  this->setCurrentItem(this->topLevelItem(0), 0);
172 }
173 
174 void CSearchResultView::setupStrongsTree(CSwordModuleInfo* m, const QStringList &vList) {
175  clear();
176  if (!m) return;
177 
178  m_module = m;
179 
180  if (vList.empty()) return;
181 
182  setUpdatesEnabled(false);
183 
184  QTreeWidgetItem* oldItem = nullptr;
185  QTreeWidgetItem* item = nullptr;
186 
187  for (auto const & s : vList) {
188  item = new QTreeWidgetItem(this, oldItem);
189  item->setText(0, (s));
190  oldItem = item;
191  }
192 
193  setUpdatesEnabled(true);
194 
195  /// \todo select the first item
196  //setSelected(firstChild(), true);
197  //executed(currentItem());
198 }
199 
200 /// \todo another function?
201 /** Reimplementation to show the popup menu. */
202 void CSearchResultView::contextMenuEvent(QContextMenuEvent* event) {
203  m_popup->exec(event->globalPos());
204 }
205 
206 /// \todo port this to the new d'n'd
207 // Q3DragObject* CSearchResultView::dragObject() {
208 // //return a valid DragObject to make DnD possible!
209 //
210 // /*
211 // * First get all selected items and fill with them the dndItems list. The return the QDragObject we got from CDRagDropMgr
212 // */
213 // CDragDropMgr::ItemList dndItems;
214 //
215 // Q3PtrList<Q3ListViewItem> items = selectedItems();
216 // for (items.first(); items.current(); items.next()) {
217 // dndItems.append( CDragDropMgr::Item(m_module->name(), items.current()->text(0), QString()) ); //no description
218 // };
219 //
220 // return CDragDropMgr::dragObject(dndItems, viewport());
221 // }
222 
223 
224 QMimeData *
225 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
227 #else
229 #endif
230  if (items.empty())
231  return nullptr;
232  BTMimeData::ItemList bookmarks;
233  for (auto const * const i : items)
234  bookmarks.append({m_module->name(), i->text(0), {}});
235  return new BTMimeData(std::move(bookmarks));
236 }
237 
238 QStringList CSearchResultView::mimeTypes() const
239 { return QStringList(QStringLiteral("BibleTime/Bookmark")); }
240 
241 } //end of namespace
242 
#define BT_CONNECT(...)
Definition: btconnect.h:20
QList< BookmarkItem > ItemList
Definition: BtMimeData.h:38
QString const & name() const
void contextMenuEvent(QContextMenuEvent *event) override
void setupStrongsTree(CSwordModuleInfo *, const QStringList &)
CSearchResultView(QWidget *parent)
void keySelected(const QString &)
QStringList mimeTypes() const override
void setupTree(CSwordModuleInfo const *m, CSwordModuleSearch::ModuleResultList const &results)
const CSwordModuleInfo * m_module
QMimeData * mimeData(QList< QTreeWidgetItem * > const &items) const override
struct Search::CSearchResultView::@13 m_actions
std::vector< std::shared_ptr< sword::SWKey const > > ModuleResultList
result append(std::move(e))