summaryrefslogtreecommitdiff
path: root/src/frontend/searchdialog/csearchresultview.cpp
blob: fc137e223e3528681d209b6a72cce15b4c50d0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/searchdialog/csearchresultview.h"

#include <QContextMenuEvent>
#include <QList>
#include <QMenu>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QWidget>
#include <QDebug>
#include "backend/keys/cswordversekey.h"
#include "frontend/cdragdrop.h"
#include "frontend/cexportmanager.h"
#include "util/cresmgr.h"
#include "util/directory.h"


namespace Search {

CSearchResultView::CSearchResultView(QWidget* parent)
        : QTreeWidget(parent),
        m_module(0) {
    initView();
    initConnections();
}

CSearchResultView::~CSearchResultView() {}

/** Initializes the view of this widget. */
void CSearchResultView::initView() {
    namespace DU = util::directory;

    setToolTip(tr("Search result of the selected work"));
    setHeaderLabel(tr("Results"));
    setDragEnabled(true);
    setRootIsDecorated( false );
    setSelectionMode(QAbstractItemView::ExtendedSelection);

    //setup the popup menu
    m_popup = new QMenu(this);

    m_actions.copyMenu = new QMenu(tr("Copy..."), m_popup);
    m_actions.copyMenu->setIcon(DU::getIcon(CResMgr::searchdialog::result::foundItems::copyMenu::icon));

    m_actions.copy.result = new QAction(tr("Reference only"), this);
    QObject::connect(m_actions.copy.result, SIGNAL(triggered()), this, SLOT(copyItems()) );
    m_actions.copyMenu->addAction(m_actions.copy.result);

    m_actions.copy.resultWithText = new QAction(tr("Reference with text"), this);
    QObject::connect(m_actions.copy.resultWithText, SIGNAL(triggered()),
                     this, SLOT(copyItemsWithText()));
    m_actions.copyMenu->addAction(m_actions.copy.resultWithText);

    m_popup->addMenu(m_actions.copyMenu);

    m_actions.saveMenu = new QMenu(tr("Save..."), m_popup);
    m_actions.saveMenu->setIcon(DU::getIcon(CResMgr::searchdialog::result::foundItems::saveMenu::icon));

    m_actions.save.result = new QAction(tr("Reference only"), this);
    QObject::connect(m_actions.save.result, SIGNAL(triggered()), this, SLOT(saveItems()) );
    m_actions.saveMenu->addAction(m_actions.save.result);

    m_actions.save.resultWithText = new QAction(tr("Reference with text"), this);
    m_actions.saveMenu->addAction(m_actions.save.resultWithText);
    QObject::connect(m_actions.save.resultWithText, SIGNAL(triggered()), this, SLOT(saveItemsWithText()));
    m_popup->addMenu(m_actions.saveMenu);

    m_actions.printMenu = new QMenu(tr("Print..."), m_popup);
    m_actions.printMenu->setIcon(DU::getIcon(CResMgr::searchdialog::result::foundItems::printMenu::icon));

    m_actions.print.result = new QAction(tr("Reference with text"), this);
    QObject::connect(m_actions.print.result, SIGNAL(triggered()), this, SLOT(printItems()) );
    m_actions.printMenu->addAction(m_actions.print.result);
    m_popup->addMenu(m_actions.printMenu);
}

/** No descriptions */
void CSearchResultView::initConnections() {
    //  connect(this, SIGNAL(executed(QListViewItem*)),
    //   this, SLOT(executed(QListViewItem*)));
    /// \todo are these right after porting?
    //items: current, previous
    connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
            this, SLOT(executed(QTreeWidgetItem*, QTreeWidgetItem*)));
}

/** Setups the list with the given module. */
void CSearchResultView::setupTree(CSwordModuleInfo* m) {
    clear();

    if (!m) return;

    m_module = m;
    sword::ListKey& result = m->searchResult();
    const int count = result.Count();
    if (!count) return;

    setUpdatesEnabled(false);

    QTreeWidgetItem* oldItem = 0;
    QTreeWidgetItem* item = 0;
    for (int index = 0; index < count; index++) {
        item = new QTreeWidgetItem(this, oldItem);
        item->setText(0, QString::fromUtf8(result.GetElement(index)->getText()));
        oldItem = item;
    }

    setUpdatesEnabled(true);
    //pre-select the first item
    this->setCurrentItem(this->topLevelItem(0), 0);
}

void CSearchResultView::setupStrongsTree(CSwordModuleInfo* m, QStringList* vList) {
    clear();
    if (!m) {
        return;
    }

    m_module = m;

    if (vList->count() <= 0) {
        return;
    }

    setUpdatesEnabled(false);

    QTreeWidgetItem* oldItem = 0;
    QTreeWidgetItem* item = 0;

    foreach (QString s, *vList) {
        item = new QTreeWidgetItem(this, oldItem);
        item->setText(0, (s));
        oldItem = item;
    }

    setUpdatesEnabled(true);

    /// \todo select the first item
    //setSelected(firstChild(), true);
    //executed(currentItem());
}

/// \todo is this still valid?
/** Is connected to the signal executed, which is emitted when a mew item was chosen. */
void CSearchResultView::executed(QTreeWidgetItem* current, QTreeWidgetItem*) {
    if (current) {
        emit keySelected(current->text(0));
    }
    else {
        emit keyDeselected();
    }
}

/// \todo another function?
/** Reimplementation to show the popup menu. */
void CSearchResultView::contextMenuEvent(QContextMenuEvent* event) {
    qDebug() << "CSearchResultView::showPopup";
    m_popup->exec(event->globalPos());
}

void CSearchResultView::printItems() {
    QList<QTreeWidgetItem*> items = selectedItems();
    CExportManager mgr(tr("Print search result..."), true, tr("Printing search result"));

    QStringList list;
    foreach (QTreeWidgetItem* k, items) {
        list.append( k->text(0) );
    }
    mgr.printKeyList( list, module(), CBTConfig::getDisplayOptionDefaults(), CBTConfig::getFilterOptionDefaults() );
}

void CSearchResultView::saveItems() {
    CExportManager mgr(tr("Save search result..."), true, tr("Saving search result"));

    CSwordModuleInfo* m = module();
    CSwordKey* k = 0;
    QList<QTreeWidgetItem*> items = selectedItems();
    QList<CSwordKey*> keys;
    foreach (QTreeWidgetItem* i, items) {
        k = CSwordKey::createInstance( m );
        k->key(i->text(0));
        keys.append( k );
    }
    mgr.saveKeyList( keys, CExportManager::Text, false);

    qDeleteAll(keys);
    keys.clear(); //delete all the keys we created
}

void CSearchResultView::saveItemsWithText() {
    CExportManager mgr(tr("Save search result..."), true, tr("Saving search result"));

    CSwordModuleInfo* m = module();
    CSwordKey* k = 0;
    QList<QTreeWidgetItem*> items = selectedItems();
    QList<CSwordKey*> keys;
    foreach (QTreeWidgetItem* i, items) {
        k = CSwordKey::createInstance( m );
        k->key(i->text(0));
        keys.append( k );
    };
    mgr.saveKeyList( keys, CExportManager::Text, true);

    qDeleteAll(keys);
    keys.clear(); //delete all the keys we created
}

void CSearchResultView::copyItems() {
    CExportManager mgr(tr("Copy search result..."), true, tr("Copying search result"));

    CSwordModuleInfo* m = module();
    CSwordKey* k = 0;
    QList<QTreeWidgetItem*> items = selectedItems();
    QList<CSwordKey*> keys;
    foreach (QTreeWidgetItem* i, items) {
        k = CSwordKey::createInstance( m );
        k->key(i->text(0));
        keys.append( k );
    };
    mgr.copyKeyList( keys, CExportManager::Text, false);

    qDeleteAll(keys);
    keys.clear(); //delete all the keys we created
}

void CSearchResultView::copyItemsWithText() {
    CExportManager mgr(tr("Copy search result..."), true, tr("Copying search result"));

    CSwordModuleInfo* m = module();
    CSwordKey* k = 0;
    QList<QTreeWidgetItem*> items = selectedItems();
    QList<CSwordKey*> keys;
    foreach (QTreeWidgetItem* i, items) {
        k = CSwordKey::createInstance( m );
        k->key(i->text(0));
        keys.append( k );
    };
    mgr.copyKeyList( keys, CExportManager::Text, true);

    qDeleteAll(keys);
    keys.clear(); //delete all the keys we created
}

CSwordModuleInfo* CSearchResultView::module() {
    return m_module;
}

/// \todo port this to the new d'n'd
// Q3DragObject* CSearchResultView::dragObject() {
// 	//return a valid DragObject to make DnD possible!
//
// 	/*
// 	* First get all selected items and fill with them the dndItems list. The return the QDragObject we got from CDRagDropMgr
// 	*/
// 	CDragDropMgr::ItemList dndItems;
//
// 	Q3PtrList<Q3ListViewItem> items = selectedItems();
// 	for (items.first(); items.current(); items.next()) {
// 		dndItems.append( CDragDropMgr::Item(m_module->name(), items.current()->text(0), QString::null) ); //no description
// 	};
//
// 	return CDragDropMgr::dragObject(dndItems, viewport());
// }


QMimeData * CSearchResultView::mimeData ( const QList<QTreeWidgetItem *> items ) const {
    BTMimeData* mdata = new BTMimeData(m_module->name(), items.first()->text(0), QString::null);
    foreach (QTreeWidgetItem* i, items) {
        mdata->appendBookmark(m_module->name(), i->text(0), QString::null);
    }
    return mdata;
}

QStringList CSearchResultView::mimeTypes () const {
    return QStringList("BibleTime/Bookmark");
}

} //end of namespace