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

« back to all changes in this revision

Viewing changes to src/gui/field/fieldinput.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
#include <QLayout>
 
21
#include <QApplication>
 
22
#include <QMenu>
 
23
#include <QDate>
 
24
#include <QSignalMapper>
 
25
#include <QPaintEngine>
 
26
#include <Q3Painter>
 
27
 
 
28
#include <KDebug>
 
29
#include <KLocale>
 
30
#include <KPushButton>
 
31
#include <KColorButton>
 
32
 
 
33
#include <fieldlineedit.h>
 
34
#include <fieldlistedit.h>
 
35
#include "fieldinput.h"
 
36
 
 
37
class FieldInput::FieldInputPrivate
 
38
{
 
39
private:
 
40
    FieldInput *p;
 
41
    FieldLineEdit *fieldLineEdit;
 
42
    FieldListEdit *fieldListEdit;
 
43
    KColorButton *colorButton;
 
44
    KPushButton *predefColorButton;
 
45
    KPushButton *resetColorButton;
 
46
    QWidget *colorWidget;
 
47
    QMenu *colorMenu;
 
48
    QSignalMapper *colorSignalMapper;
 
49
 
 
50
public:
 
51
    KBibTeX::FieldInputType fieldInputType;
 
52
    KBibTeX::TypeFlags typeFlags;
 
53
    KBibTeX::TypeFlag preferredTypeFlag;
 
54
 
 
55
    FieldInputPrivate(FieldInput *parent)
 
56
            : p(parent), fieldLineEdit(NULL), fieldListEdit(NULL), colorButton(NULL), colorWidget(NULL) {
 
57
        // TODO
 
58
    }
 
59
 
 
60
    void createGUI() {
 
61
        QHBoxLayout *layout = new QHBoxLayout(p);
 
62
        layout->setMargin(0);
 
63
 
 
64
        switch (fieldInputType) {
 
65
        case KBibTeX::MultiLine:
 
66
            fieldLineEdit = new FieldLineEdit(preferredTypeFlag, typeFlags, true, p);
 
67
            layout->addWidget(fieldLineEdit);
 
68
            break;
 
69
        case KBibTeX::List:
 
70
            fieldListEdit = new FieldListEdit(preferredTypeFlag, typeFlags, p);
 
71
            layout->addWidget(fieldListEdit);
 
72
            break;
 
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);
 
79
 
 
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);
 
86
            }
 
87
            monthSelector->setMenu(monthMenu);
 
88
        }
 
89
        break;
 
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()));
 
102
 
 
103
            colorSignalMapper = new QSignalMapper(predefColorButton);
 
104
            connect(colorSignalMapper, SIGNAL(mapped(QString)), p, SLOT(setColor(QString)));
 
105
            colorMenu = new QMenu(predefColorButton);
 
106
            predefColorButton->setMenu(colorMenu);
 
107
 
 
108
            // TODO: Make it configurable
 
109
 
 
110
            QAction *action = colorAction(i18n("Important"), "#cc3300");
 
111
            colorMenu->addAction(action);
 
112
            colorSignalMapper->setMapping(action, "#cc3300");
 
113
            connect(action, SIGNAL(triggered()), colorSignalMapper, SLOT(map()));
 
114
 
 
115
            action = colorAction(i18n("Read"), "#009966");
 
116
            colorMenu->addAction(action);
 
117
            colorSignalMapper->setMapping(action, "#009966");
 
118
            connect(action, SIGNAL(triggered()), colorSignalMapper, SLOT(map()));
 
119
        }
 
120
        break;
 
121
        case KBibTeX::PersonList:
 
122
            fieldListEdit = new PersonListEdit(preferredTypeFlag, typeFlags, p);
 
123
            layout->addWidget(fieldListEdit);
 
124
            break;
 
125
        case KBibTeX::UrlList:
 
126
            fieldListEdit = new UrlListEdit(p);
 
127
            layout->addWidget(fieldListEdit);
 
128
            break;
 
129
        default:
 
130
            fieldLineEdit = new FieldLineEdit(preferredTypeFlag, typeFlags, false, p);
 
131
            layout->addWidget(fieldLineEdit);
 
132
        }
 
133
 
 
134
        enalbeModifiedSignal();
 
135
    }
 
136
 
 
137
    QAction *colorAction(const QString&label, const QString &color) {
 
138
        int h = predefColorButton->fontMetrics().height() - 4;
 
139
        QPixmap pm(h, h);
 
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);
 
145
    }
 
146
 
 
147
    void clear() {
 
148
        disableModifiedSignal();
 
149
        if (fieldLineEdit != NULL)
 
150
            fieldLineEdit->setText("");
 
151
        else if (fieldListEdit != NULL)
 
152
            fieldListEdit->clear();
 
153
        enalbeModifiedSignal();
 
154
    }
 
155
 
 
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();
 
160
 
 
161
        bool result = false;
 
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()));
 
168
 
 
169
            VerbatimText *verbatimText = NULL;
 
170
            if (value.count() == 1 && (verbatimText = dynamic_cast<VerbatimText*>(value.first())) != NULL)
 
171
                colorButton->setColor(QColor(verbatimText->text()));
 
172
            else
 
173
                p->resetColor();
 
174
            result = true;
 
175
        }
 
176
 
 
177
        enalbeModifiedSignal();
 
178
        return result;
 
179
    }
 
180
 
 
181
    bool apply(Value& value) const {
 
182
        bool result = false;
 
183
        if (fieldLineEdit != NULL)
 
184
            result = fieldLineEdit->apply(value);
 
185
        else if (fieldListEdit != NULL)
 
186
            result = fieldListEdit->apply(value);
 
187
        else if (colorWidget != NULL) {
 
188
            value.clear();
 
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;
 
193
            }
 
194
            result = true;
 
195
        }
 
196
        return result;
 
197
    }
 
198
 
 
199
    void setReadOnly(bool isReadOnly) {
 
200
        if (fieldLineEdit != NULL)
 
201
            fieldLineEdit->setReadOnly(isReadOnly);
 
202
        else if (fieldListEdit != NULL)
 
203
            fieldListEdit->setReadOnly(isReadOnly);
 
204
    }
 
205
 
 
206
    void setFile(const File *file) {
 
207
        if (fieldLineEdit != NULL)
 
208
            fieldLineEdit->setFile(file);
 
209
        if (fieldListEdit != NULL)
 
210
            fieldListEdit->setFile(file);
 
211
    }
 
212
 
 
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()));
 
222
        }
 
223
        // TODO
 
224
    }
 
225
 
 
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()));
 
235
        }
 
236
        // TODO
 
237
    }
 
238
};
 
239
 
 
240
FieldInput::FieldInput(KBibTeX::FieldInputType fieldInputType, KBibTeX::TypeFlag preferredTypeFlag, KBibTeX::TypeFlags typeFlags, QWidget *parent)
 
241
        : QWidget(parent), d(new FieldInputPrivate(this))
 
242
{
 
243
    d->fieldInputType = fieldInputType;
 
244
    d->typeFlags = typeFlags;
 
245
    d->preferredTypeFlag = preferredTypeFlag;
 
246
    d->createGUI();
 
247
}
 
248
 
 
249
void FieldInput::clear()
 
250
{
 
251
    d->clear();
 
252
}
 
253
 
 
254
bool FieldInput::reset(const Value& value)
 
255
{
 
256
    return d->reset(value);
 
257
}
 
258
 
 
259
bool FieldInput::apply(Value& value) const
 
260
{
 
261
    return d->apply(value);
 
262
}
 
263
 
 
264
void FieldInput::setReadOnly(bool isReadOnly)
 
265
{
 
266
    d->setReadOnly(isReadOnly);
 
267
}
 
268
 
 
269
void FieldInput::setFile(const File *file)
 
270
{
 
271
    d->setFile(file);
 
272
}
 
273
 
 
274
void FieldInput::setMonth(int month)
 
275
{
 
276
    MacroKey *macro = new MacroKey(KBibTeX::MonthsTriple[month-1]);
 
277
    Value value;
 
278
    value.append(macro);
 
279
    reset(value);
 
280
}
 
281
 
 
282
void FieldInput::setColor(const QString&color)
 
283
{
 
284
    VerbatimText *verbatimText = new VerbatimText(color);
 
285
    Value value;
 
286
    value.append(verbatimText);
 
287
    reset(value);
 
288
}
 
289
 
 
290
void FieldInput::resetColor()
 
291
{
 
292
    VerbatimText *verbatimText = new VerbatimText(QLatin1String("#000000"));
 
293
    Value value;
 
294
    value.append(verbatimText);
 
295
    reset(value);
 
296
}