1
/***************************************************************************
2
* Copyright (C) 2004-2010 by Thomas Fischer *
3
* fischer@unix-ag.uni-kl.de *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
21
#include <QGridLayout>
24
#include <KPushButton>
29
#include <clipboard.h>
30
#include <bibtexeditor.h>
31
#include <bibtexfilemodel.h>
32
#include "searchresults.h"
34
class SearchResults::SearchResultsPrivate
42
BibTeXEditor *currentFile;
43
KPushButton *buttonImport;
45
KAction *actionViewCurrent, *actionImportSelected, *actionCopySelected;
47
SearchResultsPrivate(MDIWidget *mdiWidget, SearchResults *parent)
48
: p(parent), m(mdiWidget), currentFile(NULL) {
49
QGridLayout *layout = new QGridLayout(parent);
50
layout->setColumnStretch(0, 1);
51
layout->setColumnStretch(1, 0);
53
editor = new BibTeXEditor(parent);
54
editor->setReadOnly(true);
55
editor->setFrameShadow(QFrame::Sunken);
56
editor->setFrameShape(QFrame::StyledPanel);
57
editor->setContextMenuPolicy(Qt::ActionsContextMenu);
58
layout->addWidget(editor, 0, 0, 1, 2);
60
clipboard = new Clipboard(editor);
62
buttonImport = new KPushButton(KIcon("svn-update"), i18n("Import"), parent);
63
layout->addWidget(buttonImport, 1, 1, 1, 1);
64
buttonImport->setEnabled(false);
66
SortFilterBibTeXFileModel *model = new SortFilterBibTeXFileModel(parent);
67
model->setSourceModel(new BibTeXFileModel(parent));
68
editor->setModel(model);
70
actionViewCurrent = new KAction(KIcon("document-preview"), i18n("View Element"), parent);
71
editor->addAction(actionViewCurrent);
72
actionViewCurrent->setEnabled(false);
73
connect(actionViewCurrent, SIGNAL(triggered()), editor, SLOT(viewCurrentElement()));
75
actionImportSelected = new KAction(KIcon("svn-update"), i18n("Import"), parent);
76
editor->addAction(actionImportSelected);
77
actionImportSelected->setEnabled(false);
78
connect(actionImportSelected, SIGNAL(triggered()), parent, SLOT(importSelected()));
80
actionCopySelected = new KAction(KIcon("edit-copy"), i18n("Copy"), parent);
81
editor->addAction(actionCopySelected);
82
actionCopySelected->setEnabled(false);
83
connect(actionCopySelected, SIGNAL(triggered()), clipboard, SLOT(copy()));
85
connect(editor, SIGNAL(doubleClicked(QModelIndex)), editor, SLOT(viewCurrentElement()));
86
connect(editor, SIGNAL(selectedElementsChanged()), parent, SLOT(updateGUI()));
87
connect(buttonImport, SIGNAL(clicked()), parent, SLOT(importSelected()));
91
File *file = editor->bibTeXModel()->bibTeXFile();
93
editor->bibTeXModel()->setBibTeXFile(new File());
96
bool insertElement(Element *element) {
97
BibTeXFileModel *model = editor->bibTeXModel();
98
return model->insertRow(element, model->rowCount());
103
SearchResults::SearchResults(MDIWidget *mdiWidget, QWidget *parent)
104
: QWidget(parent), d(new SearchResultsPrivate(mdiWidget, this))
109
void SearchResults::clear()
114
bool SearchResults::insertElement(Element *element)
116
return d->insertElement(element);
119
void SearchResults::documentSwitched(BibTeXEditor *oldEditor, BibTeXEditor *newEditor)
122
d->currentFile = newEditor;
126
void SearchResults::updateGUI()
128
d->buttonImport->setEnabled(d->currentFile != NULL && !d->editor->selectedElements().isEmpty());
129
d->actionImportSelected->setEnabled(d->buttonImport->isEnabled());
130
d->actionCopySelected->setEnabled(!d->editor->selectedElements().isEmpty());
131
d->actionViewCurrent->setEnabled(d->editor->currentElement() != NULL);
134
void SearchResults::importSelected()
136
Q_ASSERT(d->currentFile != NULL);
138
BibTeXFileModel *targetModel = d->currentFile->bibTeXModel();
139
BibTeXFileModel *sourceModel = d->editor->bibTeXModel();
140
QList<QModelIndex> selList = d->editor->selectionModel()->selectedRows();
141
for (QList<QModelIndex>::ConstIterator it = selList.constBegin(); it != selList.constEnd(); ++it) {
142
int row = d->editor->sortFilterProxyModel()->mapToSource(*it).row();
143
Element *element = sourceModel->element(row);
144
targetModel->insertRow(element, targetModel->rowCount());