~ubuntu-branches/ubuntu/natty/kipi-plugins/natty

« back to all changes in this revision

Viewing changes to metadataedit/common/objectattributesedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-06-13 01:07:38 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20100613010738-y8ped08n4vibopv3
Tags: 1.3.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of kipi-plugins project
 
4
 * http://www.kipi-plugins.org
 
5
 *
 
6
 * Date        : 2007-10-08
 
7
 * Description : a widget to edit Application2 ObjectAttribute
 
8
 *               Iptc tag.
 
9
 *
 
10
 * Copyright (C) 2007-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
11
 *
 
12
 * This program is free software; you can redistribute it
 
13
 * and/or modify it under the terms of the GNU General
 
14
 * Public License as published by the Free Software Foundation;
 
15
 * either version 2, or (at your option) any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * ============================================================ */
 
23
 
 
24
#include "objectattributesedit.moc"
 
25
 
 
26
// Qt includes
 
27
 
 
28
#include <QCheckBox>
 
29
#include <QPushButton>
 
30
#include <QValidator>
 
31
#include <QGridLayout>
 
32
 
 
33
// KDE includes
 
34
 
 
35
#include <kcombobox.h>
 
36
#include <kdialog.h>
 
37
#include <kiconloader.h>
 
38
#include <klineedit.h>
 
39
#include <klistwidget.h>
 
40
#include <klocale.h>
 
41
 
 
42
// LibKDcraw includes
 
43
 
 
44
#include <libkdcraw/squeezedcombobox.h>
 
45
 
 
46
// Local includes
 
47
 
 
48
#include "metadatacheckbox.h"
 
49
 
 
50
using namespace KDcrawIface;
 
51
 
 
52
namespace KIPIMetadataEditPlugin
 
53
{
 
54
 
 
55
class ObjectAttributesEditPriv
 
56
{
 
57
public:
 
58
 
 
59
    ObjectAttributesEditPriv()
 
60
    {
 
61
        addValueButton = 0;
 
62
        delValueButton = 0;
 
63
        repValueButton = 0;
 
64
        valueBox       = 0;
 
65
        valueCheck     = 0;
 
66
        valueEdit      = 0;
 
67
        dataList       = 0;
 
68
    }
 
69
 
 
70
    QStringList                    oldValues;
 
71
 
 
72
    QPushButton                   *addValueButton;
 
73
    QPushButton                   *delValueButton;
 
74
    QPushButton                   *repValueButton;
 
75
 
 
76
    KLineEdit                     *valueEdit;
 
77
 
 
78
    KListWidget                   *valueBox;
 
79
 
 
80
    MetadataCheckBox              *valueCheck;
 
81
 
 
82
    SqueezedComboBox              *dataList;
 
83
};
 
84
 
 
85
ObjectAttributesEdit::ObjectAttributesEdit(QWidget* parent, bool ascii, int size)
 
86
                    : QWidget(parent), d(new ObjectAttributesEditPriv)
 
87
{
 
88
    QGridLayout *grid = new QGridLayout(this);
 
89
 
 
90
    // IPTC only accept printable Ascii char.
 
91
    QRegExp asciiRx("[\x20-\x7F]+$");
 
92
    QValidator *asciiValidator = new QRegExpValidator(asciiRx, this);
 
93
 
 
94
    // --------------------------------------------------------
 
95
 
 
96
    d->valueCheck = new MetadataCheckBox(i18n("Attribute:"), this);
 
97
 
 
98
    d->addValueButton = new QPushButton(this);
 
99
    d->delValueButton = new QPushButton(this);
 
100
    d->repValueButton = new QPushButton(this);
 
101
    d->addValueButton->setIcon(SmallIcon("list-add"));
 
102
    d->delValueButton->setIcon(SmallIcon("edit-delete"));
 
103
    d->repValueButton->setIcon(SmallIcon("view-refresh"));
 
104
    d->addValueButton->setWhatsThis(i18n("Add a new value to the list"));
 
105
    d->delValueButton->setWhatsThis(i18n("Remove the current selected value from the list"));
 
106
    d->repValueButton->setWhatsThis(i18n("Replace the current selected value from the list"));
 
107
    d->delValueButton->setEnabled(false);
 
108
    d->repValueButton->setEnabled(false);
 
109
 
 
110
    d->valueBox = new KListWidget(this);
 
111
    d->valueBox->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored);
 
112
    d->valueBox->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
113
 
 
114
    // --------------------------------------------------------
 
115
 
 
116
    d->dataList = new SqueezedComboBox(this);
 
117
    d->dataList->model()->sort(0);
 
118
    d->dataList->setWhatsThis(i18n("Select here the editorial attribute of content."));
 
119
    d->dataList->addSqueezedItem(QString("001 - ") + i18nc("Content type", "Current"));
 
120
    d->dataList->addSqueezedItem(QString("002 - ") + i18nc("Content type", "Analysis"));
 
121
    d->dataList->addSqueezedItem(QString("003 - ") + i18nc("Content type", "Archive material"));
 
122
    d->dataList->addSqueezedItem(QString("004 - ") + i18nc("Content type", "Background"));
 
123
    d->dataList->addSqueezedItem(QString("005 - ") + i18nc("Content type", "Feature"));
 
124
    d->dataList->addSqueezedItem(QString("006 - ") + i18nc("Content type", "Forecast"));
 
125
    d->dataList->addSqueezedItem(QString("007 - ") + i18nc("Content type", "History"));
 
126
    d->dataList->addSqueezedItem(QString("008 - ") + i18nc("Content type", "Obituary"));
 
127
    d->dataList->addSqueezedItem(QString("009 - ") + i18nc("Content type", "Opinion"));
 
128
    d->dataList->addSqueezedItem(QString("010 - ") + i18nc("Content type", "Polls & Surveys"));
 
129
    d->dataList->addSqueezedItem(QString("011 - ") + i18nc("Content type", "Profile"));
 
130
    d->dataList->addSqueezedItem(QString("012 - ") + i18nc("Content type", "Results Listings & Table"));
 
131
    d->dataList->addSqueezedItem(QString("013 - ") + i18nc("Content type", "Side bar & Supporting information"));
 
132
    d->dataList->addSqueezedItem(QString("014 - ") + i18nc("Content type", "Summary"));
 
133
    d->dataList->addSqueezedItem(QString("015 - ") + i18nc("Content type", "Transcript & Verbatim"));
 
134
    d->dataList->addSqueezedItem(QString("016 - ") + i18nc("Content type", "Interview"));
 
135
    d->dataList->addSqueezedItem(QString("017 - ") + i18nc("Content type", "From the Scene"));
 
136
    d->dataList->addSqueezedItem(QString("018 - ") + i18nc("Content type", "Retrospective"));
 
137
    d->dataList->addSqueezedItem(QString("019 - ") + i18nc("Content type", "Statistics"));
 
138
    d->dataList->addSqueezedItem(QString("020 - ") + i18nc("Content type", "Update"));
 
139
    d->dataList->addSqueezedItem(QString("021 - ") + i18nc("Content type", "Wrap-up"));
 
140
    d->dataList->addSqueezedItem(QString("022 - ") + i18nc("Content type", "Press Release"));
 
141
 
 
142
    // --------------------------------------------------------
 
143
 
 
144
    d->valueEdit = new KLineEdit(this);
 
145
    d->valueEdit->setClearButtonShown(true);
 
146
    QString whatsThis = i18n("Set here the editorial attribute description of "
 
147
                             "content. This field is limited to 64 ASCII characters.");
 
148
 
 
149
    if (ascii || size != -1)
 
150
    {
 
151
        whatsThis.append(i18n(" This field is limited to:"));
 
152
    }
 
153
 
 
154
    if (ascii)
 
155
    {
 
156
        d->valueEdit->setValidator(asciiValidator);
 
157
        whatsThis.append(i18n("<p>Printable ASCII characters.</p>"));
 
158
    }
 
159
 
 
160
    if (size != -1)
 
161
    {
 
162
        d->valueEdit->setMaxLength(size);
 
163
        whatsThis.append(i18np("<p>1 character.</p>","<p>%1 characters.</p>", size));
 
164
    }
 
165
 
 
166
    d->valueEdit->setWhatsThis(whatsThis);
 
167
 
 
168
    // --------------------------------------------------------
 
169
 
 
170
    grid->setAlignment( Qt::AlignTop );
 
171
    grid->addWidget(d->valueCheck,      0, 0, 1, 1);
 
172
    grid->addWidget(d->addValueButton,  0, 1, 1, 1);
 
173
    grid->addWidget(d->delValueButton,  0, 2, 1, 1);
 
174
    grid->addWidget(d->repValueButton,  0, 3, 1, 1);
 
175
    grid->addWidget(d->valueBox,        0, 4, 4, 1);
 
176
    grid->addWidget(d->dataList,        1, 0, 1, 4);
 
177
    grid->addWidget(d->valueEdit,       2, 0, 1, 4);
 
178
    grid->setRowStretch(3, 10);
 
179
    grid->setColumnStretch(0, 10);
 
180
    grid->setColumnStretch(4, 100);
 
181
    grid->setMargin(0);
 
182
    grid->setSpacing(KDialog::spacingHint());
 
183
 
 
184
    // --------------------------------------------------------
 
185
 
 
186
    connect(d->valueBox, SIGNAL(itemSelectionChanged()),
 
187
            this, SLOT(slotSelectionChanged()));
 
188
 
 
189
    connect(d->addValueButton, SIGNAL(clicked()),
 
190
            this, SLOT(slotAddValue()));
 
191
 
 
192
    connect(d->delValueButton, SIGNAL(clicked()),
 
193
            this, SLOT(slotDeleteValue()));
 
194
 
 
195
    connect(d->repValueButton, SIGNAL(clicked()),
 
196
            this, SLOT(slotReplaceValue()));
 
197
 
 
198
    // --------------------------------------------------------
 
199
 
 
200
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
201
            d->valueEdit, SLOT(setEnabled(bool)));
 
202
 
 
203
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
204
            d->dataList, SLOT(setEnabled(bool)));
 
205
 
 
206
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
207
            d->addValueButton, SLOT(setEnabled(bool)));
 
208
 
 
209
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
210
            d->delValueButton, SLOT(setEnabled(bool)));
 
211
 
 
212
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
213
            d->repValueButton, SLOT(setEnabled(bool)));
 
214
 
 
215
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
216
            d->valueBox, SLOT(setEnabled(bool)));
 
217
 
 
218
    // --------------------------------------------------------
 
219
 
 
220
    connect(d->valueCheck, SIGNAL(toggled(bool)),
 
221
            this, SIGNAL(signalModified()));
 
222
 
 
223
    connect(d->addValueButton, SIGNAL(clicked()),
 
224
            this, SIGNAL(signalModified()));
 
225
 
 
226
    connect(d->delValueButton, SIGNAL(clicked()),
 
227
            this, SIGNAL(signalModified()));
 
228
 
 
229
    connect(d->repValueButton, SIGNAL(clicked()),
 
230
            this, SIGNAL(signalModified()));
 
231
}
 
232
 
 
233
ObjectAttributesEdit::~ObjectAttributesEdit()
 
234
{
 
235
    delete d;
 
236
}
 
237
 
 
238
void ObjectAttributesEdit::slotDeleteValue()
 
239
{
 
240
    QListWidgetItem *item = d->valueBox->currentItem();
 
241
    if (!item) return;
 
242
    d->valueBox->takeItem(d->valueBox->row(item));
 
243
    delete item;
 
244
}
 
245
 
 
246
void ObjectAttributesEdit::slotReplaceValue()
 
247
{
 
248
    QString newValue = d->dataList->itemHighlighted().left(3);
 
249
    newValue.append(QString(":%1").arg(d->valueEdit->text()));
 
250
 
 
251
    if (!d->valueBox->selectedItems().isEmpty())
 
252
        d->valueBox->selectedItems()[0]->setText(newValue);
 
253
}
 
254
 
 
255
void ObjectAttributesEdit::slotSelectionChanged()
 
256
{
 
257
    if (!d->valueBox->selectedItems().isEmpty())
 
258
    {
 
259
        bool ok   = false;
 
260
        int index = d->valueBox->selectedItems()[0]->text().section(':', 0, 0).toInt(&ok);
 
261
        if (ok)
 
262
        {
 
263
            d->dataList->setCurrentIndex(index-1);
 
264
            d->valueEdit->setText(d->valueBox->selectedItems()[0]->text().section(':', -1));
 
265
            d->delValueButton->setEnabled(true);
 
266
            d->repValueButton->setEnabled(true);
 
267
            return;
 
268
        }
 
269
    }
 
270
 
 
271
    d->delValueButton->setEnabled(false);
 
272
    d->repValueButton->setEnabled(false);
 
273
}
 
274
 
 
275
void ObjectAttributesEdit::slotAddValue()
 
276
{
 
277
    QString newValue = d->dataList->itemHighlighted().left(3);
 
278
    newValue.append(QString(":%1").arg(d->valueEdit->text()));
 
279
 
 
280
    bool found = false;
 
281
    for (int i = 0 ; i < d->valueBox->count(); i++)
 
282
    {
 
283
        QListWidgetItem *item = d->valueBox->item(i);
 
284
        if (newValue == item->text())
 
285
        {
 
286
            found = true;
 
287
            break;
 
288
        }
 
289
    }
 
290
 
 
291
    if (!found)
 
292
        d->valueBox->insertItem(d->valueBox->count(), newValue);
 
293
}
 
294
 
 
295
void ObjectAttributesEdit::setValues(const QStringList& values)
 
296
{
 
297
    blockSignals(true);
 
298
    d->oldValues = values;
 
299
 
 
300
    d->valueBox->clear();
 
301
    d->valueCheck->setChecked(false);
 
302
    if (!d->oldValues.isEmpty())
 
303
    {
 
304
        d->valueBox->insertItems(0, d->oldValues);
 
305
        d->valueCheck->setChecked(true);
 
306
    }
 
307
    d->dataList->setEnabled(d->valueCheck->isChecked());
 
308
    d->valueBox->setEnabled(d->valueCheck->isChecked());
 
309
    d->addValueButton->setEnabled(d->valueCheck->isChecked());
 
310
    d->delValueButton->setEnabled(d->valueCheck->isChecked());
 
311
 
 
312
    blockSignals(false);
 
313
}
 
314
 
 
315
bool ObjectAttributesEdit::getValues(QStringList& oldValues, QStringList& newValues)
 
316
{
 
317
    oldValues = d->oldValues;
 
318
 
 
319
    newValues.clear();
 
320
    for (int i = 0 ; i < d->valueBox->count(); i++)
 
321
    {
 
322
        QListWidgetItem *item = d->valueBox->item(i);
 
323
        newValues.append(item->text());
 
324
    }
 
325
 
 
326
    return d->valueCheck->isChecked();
 
327
}
 
328
 
 
329
void ObjectAttributesEdit::setValid(bool v)
 
330
{
 
331
    d->valueCheck->setValid(v);
 
332
}
 
333
 
 
334
bool ObjectAttributesEdit::isValid() const
 
335
{
 
336
    return d->valueCheck->isValid();
 
337
}
 
338
 
 
339
}  // namespace KIPIMetadataEditPlugin