~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/gui/bibtex/bibtexfileview.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 <QHeaderView>
 
22
#include <QSignalMapper>
 
23
#include <QScrollBar>
 
24
 
 
25
#include <KAction>
 
26
#include <KLocale>
 
27
#include <KDebug>
 
28
 
 
29
#include <bibtexfields.h>
 
30
#include "bibtexfilemodel.h"
 
31
#include "bibtexfileview.h"
 
32
 
 
33
BibTeXFileView::BibTeXFileView(QWidget * parent)
 
34
        : QTreeView(parent), m_signalMapperBibTeXFields(new QSignalMapper(this))
 
35
{
 
36
    /// general visual appearance and behaviour
 
37
    setSelectionMode(QAbstractItemView::ExtendedSelection);
 
38
    setSelectionBehavior(QAbstractItemView::SelectRows);
 
39
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
 
40
    setFrameStyle(QFrame::NoFrame);
 
41
    setAlternatingRowColors(true);
 
42
    setAllColumnsShowFocus(true);
 
43
 
 
44
    /// header appearance and behaviour
 
45
    header()->setClickable(true);
 
46
    header()->setSortIndicatorShown(true);
 
47
    header()->setSortIndicator(-1, Qt::AscendingOrder);
 
48
    connect(header(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), this, SLOT(sort(int, Qt::SortOrder)));
 
49
    header()->setContextMenuPolicy(Qt::ActionsContextMenu);
 
50
 
 
51
    /// build context menu for header to show/hide single columns
 
52
    BibTeXFields *bibtexFields = BibTeXFields::self();
 
53
    int col = 0;
 
54
    for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it, ++col) {
 
55
        QString label = (*it).label;
 
56
        KAction *action = new KAction(label, header());
 
57
        action->setData(col);
 
58
        action->setCheckable(true);
 
59
        action->setChecked((*it).visible);
 
60
        connect(action, SIGNAL(triggered()), m_signalMapperBibTeXFields, SLOT(map()));
 
61
        m_signalMapperBibTeXFields->setMapping(action, action);
 
62
        header()->addAction(action);
 
63
    }
 
64
    connect(m_signalMapperBibTeXFields, SIGNAL(mapped(QObject*)), this, SLOT(headerActionToggled(QObject*)));
 
65
 
 
66
    /// add separator to header's context menu
 
67
    KAction *action = new KAction(header());
 
68
    action->setSeparator(true);
 
69
    header()->addAction(action);
 
70
 
 
71
    /// add action to reset to defaults (regarding column visibility) to header's context menu
 
72
    action = new KAction(i18n("Reset to defaults"), header());
 
73
    connect(action, SIGNAL(triggered()), this, SLOT(headerResetToDefaults()));
 
74
    header()->addAction(action);
 
75
}
 
76
 
 
77
BibTeXFileView::~BibTeXFileView()
 
78
{
 
79
    BibTeXFields *bibtexFields = BibTeXFields::self();
 
80
 
 
81
    for (int i = header()->count() - 1; i >= 0; --i) {
 
82
        FieldDescription fd = bibtexFields->at(i);
 
83
        fd.width = columnWidth(i);
 
84
        bibtexFields->replace(i, fd);
 
85
    }
 
86
    bibtexFields->save();
 
87
}
 
88
 
 
89
void BibTeXFileView::setModel(QAbstractItemModel * model)
 
90
{
 
91
    QTreeView::setModel(model);
 
92
 
 
93
    m_sortFilterProxyModel = NULL;
 
94
    m_bibTeXFileModel = dynamic_cast<BibTeXFileModel*>(model);
 
95
    if (m_bibTeXFileModel == NULL) {
 
96
        m_sortFilterProxyModel = dynamic_cast<QSortFilterProxyModel*>(model);
 
97
        Q_ASSERT(m_sortFilterProxyModel != NULL);
 
98
        m_bibTeXFileModel = dynamic_cast<BibTeXFileModel*>(m_sortFilterProxyModel->sourceModel());
 
99
    }
 
100
    Q_ASSERT(m_bibTeXFileModel != NULL);
 
101
}
 
102
 
 
103
BibTeXFileModel *BibTeXFileView::bibTeXModel()
 
104
{
 
105
    return m_bibTeXFileModel;
 
106
}
 
107
 
 
108
QSortFilterProxyModel *BibTeXFileView::sortFilterProxyModel()
 
109
{
 
110
    return m_sortFilterProxyModel;
 
111
}
 
112
 
 
113
void BibTeXFileView::resizeEvent(QResizeEvent */*event*/)
 
114
{
 
115
    BibTeXFields *bibtexFields = BibTeXFields::self();
 
116
    int sum = 0;
 
117
    int widgetWidth = size().width() - verticalScrollBar()->size().width();
 
118
 
 
119
    for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it)
 
120
        if ((*it).visible)
 
121
            sum += (*it).width;
 
122
 
 
123
    int col = 0;
 
124
    for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it, ++col) {
 
125
        setColumnWidth(col, (*it).width * widgetWidth / sum);
 
126
        setColumnHidden(col, !((*it).visible));
 
127
    }
 
128
}
 
129
 
 
130
void BibTeXFileView::headerActionToggled(QObject *obj)
 
131
{
 
132
    KAction *action = dynamic_cast<KAction*>(obj);
 
133
    if (action == NULL) return;
 
134
    bool ok = false;
 
135
    int col = (int)action->data().toInt(&ok);
 
136
    if (!ok) return;
 
137
 
 
138
    BibTeXFields *bibtexFields = BibTeXFields::self();
 
139
    FieldDescription fd = bibtexFields->at(col);
 
140
    fd.visible = action->isChecked();
 
141
    if (fd.width < 4) fd.width = width() / 10;
 
142
    bibtexFields->replace(col, fd);
 
143
 
 
144
    resizeEvent(NULL);
 
145
}
 
146
 
 
147
void BibTeXFileView::headerResetToDefaults()
 
148
{
 
149
    BibTeXFields::self()->resetToDefaults();
 
150
    resizeEvent(NULL);
 
151
}
 
152
 
 
153
void BibTeXFileView::sort(int t, Qt::SortOrder s)
 
154
{
 
155
    SortFilterBibTeXFileModel *sortedModel = dynamic_cast<SortFilterBibTeXFileModel*>(model());
 
156
    if (sortedModel != NULL)
 
157
        sortedModel->sort(t, s);
 
158
}