28 #include "subjectwidget.moc"
33 #include <QDomDocument>
34 #include <QDomElement>
36 #include <QGridLayout>
38 #include <QPushButton>
39 #include <QRadioButton>
44 #include <kcombobox.h>
48 #include <kiconloader.h>
49 #include <klineedit.h>
50 #include <klistwidget.h>
52 #include <kstandarddirs.h>
58 class SubjectWidget::Private
86 typedef QMap<QString, SubjectData> SubjectCodesMap;
88 SubjectCodesMap subMap;
94 QPushButton* addSubjectButton;
95 QPushButton* delSubjectButton;
96 QPushButton* repSubjectButton;
104 QButtonGroup* btnGroup;
106 QRadioButton* stdBtn;
107 QRadioButton* customBtn;
111 KListWidget* subjectsBox;
117 :
QWidget(parent), d(new Private)
122 KGlobal::dirs()->addResourceDir(
"iptcschema", KStandardDirs::installPath(
"data") +
123 QString(
"libkexiv2/data"));
124 QString path = KGlobal::dirs()->findResource(
"iptcschema",
"topicset.iptc-subjectcode.xml");
127 kDebug() <<
"Cannot load IPTC/NAA subject codes XML database";
132 QRegExp refDigitRx(
"^[0-9]{8}$");
133 QValidator *refValidator =
new QRegExpValidator(refDigitRx,
this);
137 m_subjectsCheck =
new QCheckBox(i18n(
"Use structured definition of the subject matter:"),
this);
139 d->btnGroup =
new QButtonGroup(
this);
140 d->stdBtn =
new QRadioButton;
141 d->customBtn =
new QRadioButton;
143 QLabel* codeLink =
new QLabel(i18n(
"Use standard "
144 "<b><a href='http://www.iptc.org/site/NewsCodes'>"
145 "reference code</a></b>"));
146 codeLink->setOpenExternalLinks(
true);
147 codeLink->setWordWrap(
false);
152 QLabel* customLabel =
new QLabel(i18n(
"Use custom definition"));
154 d->btnGroup->addButton(d->stdBtn, Private::STANDARD);
155 d->btnGroup->addButton(d->customBtn, Private::CUSTOM);
156 d->btnGroup->setExclusive(
true);
157 d->stdBtn->setChecked(
true);
159 for (Private::SubjectCodesMap::Iterator it = d->subMap.begin();
160 it != d->subMap.end(); ++it)
161 d->refCB->addItem(it.key());
196 d->iprLabel =
new QLabel(i18nc(
"Information Provider Reference: "
197 "A name, registered with the IPTC/NAA, "
198 "identifying the provider that guarantees "
199 "the uniqueness of the UNO",
"I.P.R:"));
200 d->refLabel =
new QLabel(i18n(
"Reference:"));
201 d->nameLabel =
new QLabel(i18n(
"Name:"));
202 d->matterLabel =
new QLabel(i18n(
"Matter:"));
203 d->detailLabel =
new QLabel(i18n(
"Detail:"));
207 d->subjectsBox =
new KListWidget;
208 d->subjectsBox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
210 d->addSubjectButton =
new QPushButton(i18n(
"&Add"));
211 d->delSubjectButton =
new QPushButton(i18n(
"&Delete"));
212 d->repSubjectButton =
new QPushButton(i18n(
"&Replace"));
213 d->addSubjectButton->setIcon(SmallIcon(
"list-add"));
214 d->delSubjectButton->setIcon(SmallIcon(
"edit-delete"));
215 d->repSubjectButton->setIcon(SmallIcon(
"view-refresh"));
216 d->delSubjectButton->setEnabled(
false);
217 d->repSubjectButton->setEnabled(
false);
222 m_note->setMaximumWidth(150);
223 m_note->setOpenExternalLinks(
true);
224 m_note->setWordWrap(
true);
225 m_note->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
229 QGridLayout* optionsBoxLayout =
new QGridLayout;
230 optionsBoxLayout->addWidget(d->stdBtn, 0, 0, 1, 1);
231 optionsBoxLayout->addWidget(codeLink, 0, 1, 1, 2);
232 optionsBoxLayout->addWidget(d->refCB, 0, 3, 1, 1);
233 optionsBoxLayout->addWidget(d->customBtn, 1, 0, 1, 4);
234 optionsBoxLayout->addWidget(customLabel, 1, 1, 1, 4);
235 optionsBoxLayout->addWidget(d->iprLabel, 2, 0, 1, 1);
236 optionsBoxLayout->addWidget(
m_iprEdit, 2, 1, 1, 4);
237 optionsBoxLayout->addWidget(d->refLabel, 3, 0, 1, 1);
238 optionsBoxLayout->addWidget(
m_refEdit, 3, 1, 1, 1);
239 optionsBoxLayout->addWidget(d->nameLabel, 4, 0, 1, 1);
240 optionsBoxLayout->addWidget(
m_nameEdit, 4, 1, 1, 4);
241 optionsBoxLayout->addWidget(d->matterLabel, 5, 0, 1, 1);
243 optionsBoxLayout->addWidget(d->detailLabel, 6, 0, 1, 1);
245 optionsBoxLayout->setColumnStretch(4, 10);
246 optionsBoxLayout->setMargin(0);
247 optionsBoxLayout->setSpacing(KDialog::spacingHint());
248 d->optionsBox->setLayout(optionsBoxLayout);
252 QGridLayout* mainLayout =
new QGridLayout;
253 mainLayout->setAlignment( Qt::AlignTop );
255 mainLayout->addWidget(d->optionsBox, 1, 0, 1, 4);
256 mainLayout->addWidget(d->subjectsBox, 2, 0, 5, 3);
257 mainLayout->addWidget(d->addSubjectButton, 2, 3, 1, 1);
258 mainLayout->addWidget(d->delSubjectButton, 3, 3, 1, 1);
259 mainLayout->addWidget(d->repSubjectButton, 4, 3, 1, 1);
260 mainLayout->addWidget(
m_note, 5, 3, 1, 1);
261 mainLayout->setRowStretch(6, 10);
262 mainLayout->setColumnStretch(2, 1);
263 mainLayout->setMargin(0);
264 mainLayout->setSpacing(KDialog::spacingHint());
265 setLayout(mainLayout);
269 connect(d->subjectsBox, SIGNAL(itemSelectionChanged()),
272 connect(d->addSubjectButton, SIGNAL(clicked()),
275 connect(d->delSubjectButton, SIGNAL(clicked()),
278 connect(d->repSubjectButton, SIGNAL(clicked()),
281 connect(d->btnGroup, SIGNAL(buttonReleased(
int)),
284 connect(d->refCB, SIGNAL(activated(
int)),
297 connect(d->addSubjectButton, SIGNAL(clicked()),
300 connect(d->delSubjectButton, SIGNAL(clicked()),
303 connect(d->repSubjectButton, SIGNAL(clicked()),
318 d->optionsBox->setEnabled(b);
319 d->subjectsBox->setEnabled(b);
320 d->addSubjectButton->setEnabled(b);
321 d->delSubjectButton->setEnabled(b);
322 d->repSubjectButton->setEnabled(b);
328 if (b == Private::CUSTOM)
330 d->refCB->setEnabled(
false);
339 d->refCB->setEnabled(
true);
351 QString key = d->refCB->currentText();
352 QString name, matter, detail;
354 for (Private::SubjectCodesMap::Iterator it = d->subMap.begin();
355 it != d->subMap.end(); ++it)
359 name = it.value().name;
360 matter = it.value().matter;
361 detail = it.value().detail;
387 QListWidgetItem* item = d->subjectsBox->currentItem();
389 d->subjectsBox->takeItem(d->subjectsBox->row(item));
396 if (newSubject.isEmpty())
return;
398 if (!d->subjectsBox->selectedItems().isEmpty())
400 d->subjectsBox->selectedItems()[0]->setText(newSubject);
411 if (!d->subjectsBox->selectedItems().isEmpty())
413 QString subject = d->subjectsBox->selectedItems()[0]->text();
414 m_iprEdit->setText(subject.section(
':', 0, 0));
415 m_refEdit->setText(subject.section(
':', 1, 1));
416 m_nameEdit->setText(subject.section(
':', 2, 2));
419 d->delSubjectButton->setEnabled(
true);
420 d->repSubjectButton->setEnabled(
true);
424 d->delSubjectButton->setEnabled(
false);
425 d->repSubjectButton->setEnabled(
false);
432 if (newSubject.isEmpty())
return;
435 for (
int i = 0 ; i < d->subjectsBox->count(); i++)
437 QListWidgetItem* item = d->subjectsBox->item(i);
438 if (newSubject == item->text())
447 d->subjectsBox->insertItem(d->subjectsBox->count(), newSubject);
458 QFile xmlfile(url.toLocalFile());
460 if (!xmlfile.open(QIODevice::ReadOnly))
463 QDomDocument xmlDoc(
"NewsML");
464 if (!xmlDoc.setContent(&xmlfile))
467 QDomElement xmlDocElem = xmlDoc.documentElement();
468 if (xmlDocElem.tagName()!=
"NewsML")
471 for (QDomNode nbE1 = xmlDocElem.firstChild();
472 !nbE1.isNull(); nbE1 = nbE1.nextSibling())
474 QDomElement newsItemElement = nbE1.toElement();
475 if (newsItemElement.isNull())
continue;
476 if (newsItemElement.tagName() !=
"NewsItem")
continue;
478 for (QDomNode nbE2 = newsItemElement.firstChild();
479 !nbE2.isNull(); nbE2 = nbE2.nextSibling())
481 QDomElement topicSetElement = nbE2.toElement();
482 if (topicSetElement.isNull())
continue;
483 if (topicSetElement.tagName() !=
"TopicSet")
continue;
485 for (QDomNode nbE3 = topicSetElement.firstChild();
486 !nbE3.isNull(); nbE3 = nbE3.nextSibling())
488 QDomElement topicElement = nbE3.toElement();
489 if (topicElement.isNull())
continue;
490 if (topicElement.tagName() !=
"Topic")
continue;
492 QString type, name, matter, detail, ref;
493 for (QDomNode nbE4 = topicElement.firstChild();
494 !nbE4.isNull(); nbE4 = nbE4.nextSibling())
496 QDomElement topicSubElement = nbE4.toElement();
497 if (topicSubElement.isNull())
continue;
499 if (topicSubElement.tagName() ==
"TopicType")
500 type = topicSubElement.attribute(
"FormalName");
502 if (topicSubElement.tagName() ==
"FormalName")
503 ref = topicSubElement.text();
505 if (topicSubElement.tagName() ==
"Description" &&
506 topicSubElement.attribute(
"Variant") ==
"Name")
508 if (type ==
"Subject")
509 name = topicSubElement.text();
510 else if (type ==
"SubjectMatter")
511 matter = topicSubElement.text();
512 else if (type ==
"SubjectDetail")
513 detail = topicSubElement.text();
517 d->subMap.insert(ref,
SubjectData(name, matter, detail));
524 for (Private::SubjectCodesMap::Iterator it = d->subMap.begin();
525 it != d->subMap.end(); ++it)
527 QString name, keyPrefix;
528 if (it.key().endsWith(QLatin1String(
"00000")))
530 keyPrefix = it.key().left(3);
531 name = it.value().name;
533 for (Private::SubjectCodesMap::Iterator it2 = d->subMap.begin();
534 it2 != d->subMap.end(); ++it2)
536 if (it2.key().startsWith(keyPrefix) &&
537 !it2.key().endsWith(QLatin1String(
"00000")))
539 it2.value().name = name;
547 for (Private::SubjectCodesMap::Iterator it = d->subMap.begin();
548 it != d->subMap.end(); ++it)
550 QString matter, keyPrefix;
551 if (it.key().endsWith(QLatin1String(
"000")))
553 keyPrefix = it.key().left(5);
554 matter = it.value().matter;
556 for (Private::SubjectCodesMap::Iterator it2 = d->subMap.begin();
557 it2 != d->subMap.end(); ++it2)
559 if (it2.key().startsWith(keyPrefix) &&
560 !it2.key().endsWith(QLatin1String(
"000")))
562 it2.value().matter = matter;
573 d->subjectsList = list;
576 d->subjectsBox->clear();
578 if (!d->subjectsList.isEmpty())
580 d->subjectsBox->insertItems(0, d->subjectsList);
590 QStringList newSubjects;
592 for (
int i = 0 ; i < d->subjectsBox->count(); i++)
594 QListWidgetItem* item = d->subjectsBox->item(i);
595 newSubjects.append(item->text());