~ubuntu-branches/ubuntu/raring/kbibtex/raring

« back to all changes in this revision

Viewing changes to src/program/searchresults.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2010 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
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.                                   *
 
9
*                                                                         *
 
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.                          *
 
14
*                                                                         *
 
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
***************************************************************************/
 
20
 
 
21
#include <QGridLayout>
 
22
 
 
23
#include <KLocale>
 
24
#include <KPushButton>
 
25
#include <KDebug>
 
26
#include <KAction>
 
27
 
 
28
#include <file.h>
 
29
#include <clipboard.h>
 
30
#include <bibtexeditor.h>
 
31
#include <bibtexfilemodel.h>
 
32
#include "searchresults.h"
 
33
 
 
34
class SearchResults::SearchResultsPrivate
 
35
{
 
36
private:
 
37
    SearchResults *p;
 
38
    Clipboard *clipboard;
 
39
 
 
40
public:
 
41
    MDIWidget *m;
 
42
    BibTeXEditor *currentFile;
 
43
    KPushButton *buttonImport;
 
44
    BibTeXEditor *editor;
 
45
    KAction *actionViewCurrent, *actionImportSelected, *actionCopySelected;
 
46
 
 
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);
 
52
 
 
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);
 
59
 
 
60
        clipboard = new Clipboard(editor);
 
61
 
 
62
        buttonImport = new KPushButton(KIcon("svn-update"), i18n("Import"), parent);
 
63
        layout->addWidget(buttonImport, 1, 1, 1, 1);
 
64
        buttonImport->setEnabled(false);
 
65
 
 
66
        SortFilterBibTeXFileModel *model = new SortFilterBibTeXFileModel(parent);
 
67
        model->setSourceModel(new BibTeXFileModel(parent));
 
68
        editor->setModel(model);
 
69
 
 
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()));
 
74
 
 
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()));
 
79
 
 
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()));
 
84
 
 
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()));
 
88
    }
 
89
 
 
90
    void clear() {
 
91
        File *file = editor->bibTeXModel()->bibTeXFile();
 
92
        delete file;
 
93
        editor->bibTeXModel()->setBibTeXFile(new File());
 
94
    }
 
95
 
 
96
    bool insertElement(Element *element) {
 
97
        BibTeXFileModel *model = editor->bibTeXModel();
 
98
        return model->insertRow(element,  model->rowCount());
 
99
    }
 
100
 
 
101
};
 
102
 
 
103
SearchResults::SearchResults(MDIWidget *mdiWidget, QWidget *parent)
 
104
        : QWidget(parent), d(new SearchResultsPrivate(mdiWidget, this))
 
105
{
 
106
    // nothing
 
107
}
 
108
 
 
109
void SearchResults::clear()
 
110
{
 
111
    d->clear();
 
112
}
 
113
 
 
114
bool SearchResults::insertElement(Element *element)
 
115
{
 
116
    return d->insertElement(element);
 
117
}
 
118
 
 
119
void SearchResults::documentSwitched(BibTeXEditor *oldEditor, BibTeXEditor *newEditor)
 
120
{
 
121
    Q_UNUSED(oldEditor);
 
122
    d->currentFile = newEditor;
 
123
    updateGUI();
 
124
}
 
125
 
 
126
void SearchResults::updateGUI()
 
127
{
 
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);
 
132
}
 
133
 
 
134
void SearchResults::importSelected()
 
135
{
 
136
    Q_ASSERT(d->currentFile != NULL);
 
137
 
 
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());
 
145
    }
 
146
}