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 <QHeaderView>
22
#include <QSignalMapper>
29
#include <bibtexfields.h>
30
#include "bibtexfilemodel.h"
31
#include "bibtexfileview.h"
33
BibTeXFileView::BibTeXFileView(QWidget * parent)
34
: QTreeView(parent), m_signalMapperBibTeXFields(new QSignalMapper(this))
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);
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);
51
/// build context menu for header to show/hide single columns
52
BibTeXFields *bibtexFields = BibTeXFields::self();
54
for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it, ++col) {
55
QString label = (*it).label;
56
KAction *action = new KAction(label, header());
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);
64
connect(m_signalMapperBibTeXFields, SIGNAL(mapped(QObject*)), this, SLOT(headerActionToggled(QObject*)));
66
/// add separator to header's context menu
67
KAction *action = new KAction(header());
68
action->setSeparator(true);
69
header()->addAction(action);
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);
77
BibTeXFileView::~BibTeXFileView()
79
BibTeXFields *bibtexFields = BibTeXFields::self();
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);
89
void BibTeXFileView::setModel(QAbstractItemModel * model)
91
QTreeView::setModel(model);
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());
100
Q_ASSERT(m_bibTeXFileModel != NULL);
103
BibTeXFileModel *BibTeXFileView::bibTeXModel()
105
return m_bibTeXFileModel;
108
QSortFilterProxyModel *BibTeXFileView::sortFilterProxyModel()
110
return m_sortFilterProxyModel;
113
void BibTeXFileView::resizeEvent(QResizeEvent */*event*/)
115
BibTeXFields *bibtexFields = BibTeXFields::self();
117
int widgetWidth = size().width() - verticalScrollBar()->size().width();
119
for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it)
124
for (BibTeXFields::Iterator it = bibtexFields->begin(); it != bibtexFields->end(); ++it, ++col) {
125
setColumnWidth(col, (*it).width * widgetWidth / sum);
126
setColumnHidden(col, !((*it).visible));
130
void BibTeXFileView::headerActionToggled(QObject *obj)
132
KAction *action = dynamic_cast<KAction*>(obj);
133
if (action == NULL) return;
135
int col = (int)action->data().toInt(&ok);
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);
147
void BibTeXFileView::headerResetToDefaults()
149
BibTeXFields::self()->resetToDefaults();
153
void BibTeXFileView::sort(int t, Qt::SortOrder s)
155
SortFilterBibTeXFileModel *sortedModel = dynamic_cast<SortFilterBibTeXFileModel*>(model());
156
if (sortedModel != NULL)
157
sortedModel->sort(t, s);