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 <QApplication>
24
#include <QSignalMapper>
25
#include <QPaintEngine>
30
#include <KPushButton>
31
#include <KColorButton>
33
#include <fieldlineedit.h>
34
#include <fieldlistedit.h>
35
#include "fieldinput.h"
37
class FieldInput::FieldInputPrivate
41
FieldLineEdit *fieldLineEdit;
42
FieldListEdit *fieldListEdit;
43
KColorButton *colorButton;
44
KPushButton *predefColorButton;
45
KPushButton *resetColorButton;
48
QSignalMapper *colorSignalMapper;
51
KBibTeX::FieldInputType fieldInputType;
52
KBibTeX::TypeFlags typeFlags;
53
KBibTeX::TypeFlag preferredTypeFlag;
55
FieldInputPrivate(FieldInput *parent)
56
: p(parent), fieldLineEdit(NULL), fieldListEdit(NULL), colorButton(NULL), colorWidget(NULL) {
61
QHBoxLayout *layout = new QHBoxLayout(p);
64
switch (fieldInputType) {
65
case KBibTeX::MultiLine:
66
fieldLineEdit = new FieldLineEdit(preferredTypeFlag, typeFlags, true, p);
67
layout->addWidget(fieldLineEdit);
70
fieldListEdit = new FieldListEdit(preferredTypeFlag, typeFlags, p);
71
layout->addWidget(fieldListEdit);
73
case KBibTeX::Month: {
74
fieldLineEdit = new FieldLineEdit(preferredTypeFlag, typeFlags, false, p);
75
layout->addWidget(fieldLineEdit);
76
KPushButton *monthSelector = new KPushButton(KIcon("view-calendar-month"), "");
77
monthSelector->setToolTip(i18n("Select a predefined month"));
78
fieldLineEdit->prependWidget(monthSelector);
80
QSignalMapper *sm = new QSignalMapper(monthSelector);
81
connect(sm, SIGNAL(mapped(int)), p, SLOT(setMonth(int)));
82
QMenu *monthMenu = new QMenu(monthSelector);
83
for (int i = 1; i <= 12; ++i) {
84
QAction *monthAction = monthMenu->addAction(QDate::longMonthName(i, QDate::StandaloneFormat), sm, SLOT(map()));
85
sm->setMapping(monthAction, i);
87
monthSelector->setMenu(monthMenu);
90
case KBibTeX::Color: {
91
colorWidget = new QWidget(p);
92
QBoxLayout *boxLayout = new QHBoxLayout(colorWidget);
93
boxLayout->setMargin(0);
94
predefColorButton = new KPushButton(KIcon("color-picker-white"), i18n("Predefined colors"), colorWidget);
95
boxLayout->addWidget(predefColorButton, 0);
96
colorButton = new KColorButton(colorWidget);
97
boxLayout->addWidget(colorButton, 0);
98
layout->addWidget(colorWidget, 0);
99
resetColorButton = new KPushButton(KIcon("edit-clear-locationbar-rtl"), i18n("Reset"), colorWidget);
100
layout->addWidget(resetColorButton, 0, Qt::AlignLeft);
101
connect(resetColorButton, SIGNAL(clicked()), p, SLOT(resetColor()));
103
colorSignalMapper = new QSignalMapper(predefColorButton);
104
connect(colorSignalMapper, SIGNAL(mapped(QString)), p, SLOT(setColor(QString)));
105
colorMenu = new QMenu(predefColorButton);
106
predefColorButton->setMenu(colorMenu);
108
// TODO: Make it configurable
110
QAction *action = colorAction(i18n("Important"), "#cc3300");
111
colorMenu->addAction(action);
112
colorSignalMapper->setMapping(action, "#cc3300");
113
connect(action, SIGNAL(triggered()), colorSignalMapper, SLOT(map()));
115
action = colorAction(i18n("Read"), "#009966");
116
colorMenu->addAction(action);
117
colorSignalMapper->setMapping(action, "#009966");
118
connect(action, SIGNAL(triggered()), colorSignalMapper, SLOT(map()));
121
case KBibTeX::PersonList:
122
fieldListEdit = new PersonListEdit(preferredTypeFlag, typeFlags, p);
123
layout->addWidget(fieldListEdit);
125
case KBibTeX::UrlList:
126
fieldListEdit = new UrlListEdit(p);
127
layout->addWidget(fieldListEdit);
130
fieldLineEdit = new FieldLineEdit(preferredTypeFlag, typeFlags, false, p);
131
layout->addWidget(fieldLineEdit);
134
enalbeModifiedSignal();
137
QAction *colorAction(const QString&label, const QString &color) {
138
int h = predefColorButton->fontMetrics().height() - 4;
140
QPainter painter(&pm);
141
painter.setPen(QColor(color));
142
painter.setBrush(QBrush(painter.pen().color()));
143
painter.drawRect(0, 0, h, h);
144
return new QAction(KIcon(pm), label, p);
148
disableModifiedSignal();
149
if (fieldLineEdit != NULL)
150
fieldLineEdit->setText("");
151
else if (fieldListEdit != NULL)
152
fieldListEdit->clear();
153
enalbeModifiedSignal();
156
bool reset(const Value& value) {
157
/// if signals are not deactivated, the "modified" signal would be emitted when
158
/// resetting the widget's value
159
disableModifiedSignal();
162
if (fieldLineEdit != NULL)
163
result = fieldLineEdit->reset(value);
164
else if (fieldListEdit != NULL)
165
result = fieldListEdit->reset(value);
166
else if (colorWidget != NULL) {
167
disconnect(colorButton, SIGNAL(changed(QColor)), p, SIGNAL(modified()));
169
VerbatimText *verbatimText = NULL;
170
if (value.count() == 1 && (verbatimText = dynamic_cast<VerbatimText*>(value.first())) != NULL)
171
colorButton->setColor(QColor(verbatimText->text()));
177
enalbeModifiedSignal();
181
bool apply(Value& value) const {
183
if (fieldLineEdit != NULL)
184
result = fieldLineEdit->apply(value);
185
else if (fieldListEdit != NULL)
186
result = fieldListEdit->apply(value);
187
else if (colorWidget != NULL) {
189
const QString colorName = colorButton->color().name();
190
if (!(colorButton->color() == QColor(Qt::black)) && colorName != QLatin1String("#000000")) { // FIXME test looks redundant
191
VerbatimText *verbatimText = new VerbatimText(colorName);
192
value << verbatimText;
199
void setReadOnly(bool isReadOnly) {
200
if (fieldLineEdit != NULL)
201
fieldLineEdit->setReadOnly(isReadOnly);
202
else if (fieldListEdit != NULL)
203
fieldListEdit->setReadOnly(isReadOnly);
206
void setFile(const File *file) {
207
if (fieldLineEdit != NULL)
208
fieldLineEdit->setFile(file);
209
if (fieldListEdit != NULL)
210
fieldListEdit->setFile(file);
213
void enalbeModifiedSignal() {
214
if (fieldLineEdit != NULL)
215
connect(fieldLineEdit, SIGNAL(textChanged(QString)), p, SIGNAL(modified()));
216
if (fieldListEdit != NULL)
217
connect(fieldListEdit, SIGNAL(modified()), p, SIGNAL(modified()));
218
if (colorButton != NULL) {
219
connect(resetColorButton, SIGNAL(clicked()), p, SIGNAL(modified()));
220
connect(colorButton, SIGNAL(changed(QColor)), p, SIGNAL(modified()));
221
connect(colorSignalMapper, SIGNAL(mapped(int)), p, SIGNAL(modified()));
226
void disableModifiedSignal() {
227
if (fieldLineEdit != NULL)
228
disconnect(fieldLineEdit, SIGNAL(textChanged(QString)), p, SIGNAL(modified()));
229
if (fieldListEdit != NULL)
230
disconnect(fieldListEdit, SIGNAL(modified()), p, SIGNAL(modified()));
231
if (colorButton != NULL) {
232
disconnect(resetColorButton, SIGNAL(clicked()), p, SIGNAL(modified()));
233
disconnect(colorButton, SIGNAL(changed(QColor)), p, SIGNAL(modified()));
234
disconnect(colorSignalMapper, SIGNAL(mapped(int)), p, SIGNAL(modified()));
240
FieldInput::FieldInput(KBibTeX::FieldInputType fieldInputType, KBibTeX::TypeFlag preferredTypeFlag, KBibTeX::TypeFlags typeFlags, QWidget *parent)
241
: QWidget(parent), d(new FieldInputPrivate(this))
243
d->fieldInputType = fieldInputType;
244
d->typeFlags = typeFlags;
245
d->preferredTypeFlag = preferredTypeFlag;
249
void FieldInput::clear()
254
bool FieldInput::reset(const Value& value)
256
return d->reset(value);
259
bool FieldInput::apply(Value& value) const
261
return d->apply(value);
264
void FieldInput::setReadOnly(bool isReadOnly)
266
d->setReadOnly(isReadOnly);
269
void FieldInput::setFile(const File *file)
274
void FieldInput::setMonth(int month)
276
MacroKey *macro = new MacroKey(KBibTeX::MonthsTriple[month-1]);
282
void FieldInput::setColor(const QString&color)
284
VerbatimText *verbatimText = new VerbatimText(color);
286
value.append(verbatimText);
290
void FieldInput::resetColor()
292
VerbatimText *verbatimText = new VerbatimText(QLatin1String("#000000"));
294
value.append(verbatimText);