2
// C++ Implementation: TagTemplate
7
// Author: Chris Browet <cbro@semperpax.com>, (C) 2008
9
// Copyright: See COPYING file that comes with this distribution
13
#include "TagTemplate.h"
15
#include "Maps/MapFeature.h"
16
#include "Maps/TrackPoint.h"
17
#include "Maps/Relation.h"
18
#include "Maps/Road.h"
19
#include "Command/FeatureCommands.h"
28
/** TagTemplateWidget **/
30
TagTemplateWidget::TagTemplateWidget()
31
: theMainWidget(0), theWidget(0), theLabelWidget(0), theSelector(0)
35
TagTemplateWidget::~TagTemplateWidget()
37
// No need to delete; will be destroyed automatically by parent + crash if no active widget.
38
// delete theMainWidget;
39
for (int i=0; i<theValues.size(); ++i)
44
TagTemplateWidget* TagTemplateWidget::fromXml(const QDomElement& e)
46
if (e.tagName() != "widget") {
50
TagTemplateWidget* aTW = NULL;
51
if (e.attribute("type") == "combo") {
52
aTW = TagTemplateWidgetCombo::fromXml(e);
54
if (e.attribute("type") == "yesno") {
55
aTW = TagTemplateWidgetYesno::fromXml(e);
57
if (e.attribute("type") == "constant") {
58
aTW = TagTemplateWidgetConstant::fromXml(e);
60
if (e.attribute("type") == "edit") {
61
aTW = TagTemplateWidgetEdit::fromXml(e);
66
aTW->theId = e.attribute("id");
67
aTW->theType = e.attribute("type");
68
aTW->theTag = e.attribute("tag");
74
void TagTemplateWidget::parseCommonElements(const QDomElement& e)
76
if (e.tagName() == "description") {
77
theDescriptions.insert(e.attribute("locale"), e.firstChild().toText().nodeValue());
79
if (e.tagName() == "link") {
80
theUrl = QUrl(e.attribute("src"));
82
if (e.tagName() == "selector") {
83
theSelector = TagSelector::parse(e.attribute("expr"));
85
if (e.tagName() == "value") {
86
TagTemplateWidgetValue* aTCV = TagTemplateWidgetValue::fromXml(e);
88
theValues.append(aTCV);
92
void TagTemplateWidget::generateCommonElements(QDomElement& e)
94
if (!theUrl.isEmpty()) {
95
QDomElement c = e.ownerDocument().createElement("link");
98
c.setAttribute("src", theUrl.toString());
101
QHashIterator<QString, QString > itD(theDescriptions);
102
while(itD.hasNext()) {
105
QDomElement d = e.ownerDocument().createElement("description");
108
d.setAttribute("locale", itD.key());
110
QDomText v = e.ownerDocument().createTextNode(itD.value());
115
QDomElement s = e.ownerDocument().createElement("selector");
117
s.setAttribute("expr", theSelector->asExpression(false));
120
for (int i=0; i<theValues.size(); ++i) {
121
theValues[i]->toXML(e);
125
/** TagTemplateWidgetValue **/
127
TagTemplateWidgetValue* TagTemplateWidgetValue::fromXml(const QDomElement& e)
129
TagTemplateWidgetValue* aTCV = new TagTemplateWidgetValue(e.attribute("tag"));
131
for(QDomNode d = e.firstChild(); !d.isNull(); d = d.nextSibling()) {
132
QDomElement c = d.toElement();
136
aTCV->parseCommonElements(c);
141
bool TagTemplateWidgetValue::toXML(QDomElement& xParent)
143
QDomElement v = xParent.ownerDocument().createElement("value");
144
xParent.appendChild(v);
146
v.setAttribute("tag", theTagValue);
147
generateCommonElements(v);
153
/** TagTemplateWidgetCombo **/
155
TagTemplateWidgetCombo::~TagTemplateWidgetCombo()
159
QWidget* TagTemplateWidgetCombo::getWidget(const MapFeature* F)
161
if (theSelector && (theSelector->matches(F) != TagSelect_Match && theSelector->matches(F) != TagSelect_DefaultMatch))
164
QString lang = getDefaultLanguage();
165
QString defLang = "en";
166
if (lang == "-" || !M_PREFS->getTranslateTags())
169
theWidget = new QWidget();
170
QHBoxLayout* aLayout = new QHBoxLayout(theWidget);
171
aLayout->setContentsMargins(0, 0, 0, 0);
173
QLabel* aLabel = new QLabel();
174
aLabel->setOpenExternalLinks(true);
175
aLabel->setWordWrap(true);
176
aLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
177
aLabel->setMaximumWidth(55);
178
aLayout->addWidget(aLabel);
181
if (theDescriptions.count(lang))
182
theDescription = theDescriptions[lang];
184
if (theDescriptions.count(defLang))
185
theDescription = theDescriptions[defLang];
187
theDescription = theTag;
188
if (!theUrl.isEmpty())
189
url = theUrl.toString();
191
QComboBox* aCombo = new QComboBox();
192
aCombo->setEditable(true);
193
aCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
194
aLayout->addWidget(aCombo);
196
aCombo->addItem(tr("Undefined"), qVariantFromValue(new TagTemplateWidgetValue("__NULL__")));
197
QString val = F->tagValue(theTag, "__NULL__");
199
for (int i=0; i<theValues.size(); ++i) {
200
if (theValues[i]->theDescriptions.count(lang))
201
aCombo->addItem(theValues[i]->theDescriptions[lang], qVariantFromValue(theValues[i]));
203
if (theValues[i]->theDescriptions.count(defLang))
204
aCombo->addItem(theValues[i]->theDescriptions[defLang], qVariantFromValue(theValues[i]));
206
aCombo->addItem(theValues[i]->theTagValue, qVariantFromValue(theValues[i]));
208
if (theValues[i]->theTagValue == val)
209
idx = aCombo->count() - 1;
212
if (val != "__NULL__") {
214
aCombo->insertItem(1, val);
215
aCombo->setCurrentIndex(1);
217
aCombo->setCurrentIndex(idx);
218
if (!theValues[idx-1]->theUrl.isEmpty())
219
url = theValues[idx-1]->theUrl.toString();
222
aCombo->setCurrentIndex(0);
225
aLabel->setText(QString("<a href=\"%1\">%2</a>").arg(url).arg(theDescription));
227
aLabel->setText(theDescription);
229
connect(aCombo,SIGNAL(activated(int)), this, SLOT(on_combo_activated(int)));
230
theMainWidget = aCombo;
231
theLabelWidget = aLabel;
236
TagTemplateWidgetCombo* TagTemplateWidgetCombo::fromXml(const QDomElement& e)
238
TagTemplateWidgetCombo* aCW = new TagTemplateWidgetCombo();
240
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
241
QDomElement t = n.toElement();
245
aCW->parseCommonElements(t);
251
bool TagTemplateWidgetCombo::toXML(QDomElement& xParent, bool header)
256
QDomElement e = xParent.ownerDocument().createElement("widgetref");
257
xParent.appendChild(e);
258
if (!theId.isEmpty())
259
e.setAttribute("id", theId);
261
e.setAttribute("id", theTag);
263
QDomElement e = xParent.ownerDocument().createElement("widget");
264
xParent.appendChild(e);
266
if (!theId.isEmpty())
267
e.setAttribute("id", theId);
268
e.setAttribute("type", theType);
269
e.setAttribute("tag", theTag);
270
generateCommonElements(e);
276
void TagTemplateWidgetCombo::on_combo_activated(int idx)
278
QComboBox* W = dynamic_cast<QComboBox*>(theMainWidget);
279
TagTemplateWidgetValue* aTCV = W->itemData(idx).value<TagTemplateWidgetValue*>();
283
if (!aTCV->theUrl.isEmpty()) {
284
((QLabel *)theLabelWidget)->setText(QString("<a href=\"%1\">%2</a>").arg(aTCV->theUrl.toString()).arg(theDescription));
286
if (!theUrl.isEmpty()) {
287
((QLabel *)theLabelWidget)->setText(QString("<a href=\"%1\">%2</a>").arg(theUrl.toString()).arg(theDescription));
289
((QLabel *)theLabelWidget)->setText(theDescription);
293
val = aTCV->theTagValue;
295
val = W->currentText();
297
if (val == "__NULL__")
298
emit tagCleared(theTag);
300
emit tagChanged(theTag, val);
303
void TagTemplateWidgetCombo::apply(const MapFeature*)
307
/** TagTemplateWidgetYesno **/
309
QWidget* TagTemplateWidgetYesno::getWidget(const MapFeature* F)
311
if (theSelector && (theSelector->matches(F) != TagSelect_Match && theSelector->matches(F) != TagSelect_DefaultMatch))
314
QString lang = getDefaultLanguage();
315
QString defLang = "en";
316
if (lang == "-" || !M_PREFS->getTranslateTags())
319
theWidget = new QWidget();
320
QHBoxLayout* aLayout = new QHBoxLayout(theWidget);
321
aLayout->setContentsMargins(0, 0, 0, 0);
323
QLabel* aLabel = new QLabel();
324
aLabel->setOpenExternalLinks(true);
325
aLayout->addWidget(aLabel);
327
QCheckBox* aCB = new QCheckBox();
329
aLayout->addWidget(aCB);
331
if (theDescriptions.count(lang))
332
theDescription = theDescriptions[lang];
334
if (theDescriptions.count(defLang))
335
theDescription = theDescriptions[defLang];
337
theDescription = theTag;
339
QString val = F->tagValue(theTag, "__NULL__");
340
if (val == "yes" || val == "1" || val == "true")
341
aCB->setCheckState(Qt::Checked);
343
if (val == "no" || val == "0" || val == "false")
344
aCB->setCheckState(Qt::Unchecked);
346
aCB->setCheckState(Qt::PartiallyChecked);
348
if (!theUrl.isEmpty())
349
aLabel->setText(QString("<a href=\"%1\">%2</a>").arg(theUrl.toString()).arg(theDescription));
351
aLabel->setText(theDescription);
353
connect(aCB,SIGNAL(stateChanged(int)), this, SLOT(on_checkbox_stateChanged (int)));
355
theLabelWidget = aLabel;
360
void TagTemplateWidgetYesno::on_checkbox_stateChanged(int state)
362
//QCheckBox* W = dynamic_cast<QCheckBox*>(theMainWidget);
363
if (state == Qt::PartiallyChecked) {
364
emit tagCleared(theTag);
366
QString value = (state == Qt::Checked) ? "yes" : "no";
367
emit tagChanged(theTag, value);
371
void TagTemplateWidgetYesno::apply(const MapFeature*)
375
TagTemplateWidgetYesno* TagTemplateWidgetYesno::fromXml(const QDomElement& e)
377
TagTemplateWidgetYesno* aCB = new TagTemplateWidgetYesno();
379
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
380
QDomElement t = n.toElement();
384
aCB->parseCommonElements(t);
390
bool TagTemplateWidgetYesno::toXML(QDomElement& xParent, bool header)
395
QDomElement e = xParent.ownerDocument().createElement("widgetref");
396
xParent.appendChild(e);
397
if (!theId.isEmpty())
398
e.setAttribute("id", theId);
400
e.setAttribute("id", theTag);
402
QDomElement e = xParent.ownerDocument().createElement("widget");
403
xParent.appendChild(e);
405
if (!theId.isEmpty())
406
e.setAttribute("id", theId);
407
e.setAttribute("type", theType);
408
e.setAttribute("tag", theTag);
409
generateCommonElements(e);
415
/** TagTemplateWidgetConstant **/
417
TagTemplateWidgetConstant::~TagTemplateWidgetConstant()
421
QWidget* TagTemplateWidgetConstant::getWidget(const MapFeature* F)
423
if (theSelector && (theSelector->matches(F) != TagSelect_Match && theSelector->matches(F) != TagSelect_DefaultMatch))
426
QString lang = getDefaultLanguage();
427
QString defLang = "en";
428
if (lang == "-" || !M_PREFS->getTranslateTags())
431
theWidget = new QWidget();
432
QHBoxLayout* aLayout = new QHBoxLayout(theWidget);
433
aLayout->setContentsMargins(0, 0, 0, 0);
435
QLabel* aLabel = new QLabel();
436
aLabel->setOpenExternalLinks(true);
437
aLabel->setWordWrap(true);
438
aLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
439
aLabel->setMaximumWidth(55);
440
aLayout->addWidget(aLabel);
443
if (theDescriptions.count(lang))
444
theDescription = theDescriptions[lang];
446
if (theDescriptions.count(defLang))
447
theDescription = theDescriptions[defLang];
449
theDescription = theTag;
450
if (!theUrl.isEmpty())
451
url = theUrl.toString();
453
QLabel* aValue = new QLabel();
454
aValue->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
455
aValue->setWordWrap(true);
456
aLayout->addWidget(aValue);
458
if (theValues.count()) {
460
if (theValues[0]->theDescriptions.count(lang))
461
aValueStr = theValues[0]->theDescriptions[lang];
463
if (theValues[0]->theDescriptions.count(defLang))
464
aValueStr = theValues[0]->theDescriptions[defLang];
466
aValueStr = theValues[0]->theTagValue;
468
aValue->setText(QString("<b>%1</b>").arg(aValueStr));
470
if (!theValues[0]->theUrl.isEmpty())
471
url = theValues[0]->theUrl.toString();
474
aLabel->setText(QString("<a href=\"%1\">%2</a>").arg(url).arg(theDescription));
476
aLabel->setText(theDescription);
478
aValue->setText(QString("<b>%1</b>").arg(F->tagValue(theTag, "")));
480
theMainWidget = aValue;
481
theLabelWidget = aLabel;
486
void TagTemplateWidgetConstant::apply(const MapFeature* F)
488
if (!theValues.count()) {
489
((QLabel*)theMainWidget)->setText(QString("<b>%1</b>").arg(F->tagValue(theTag, "")));
494
QString oldVal = F->tagValue(theTag, "__NULL__");
495
QString newVal = theValues[0]->theTagValue;
496
QRegExp subst("\\$\\[(.+)\\]");
497
subst.setMinimal(true);
499
while ((pos = subst.indexIn(newVal, pos)) != -1) {
501
QString rep = F->tagValue(subst.cap(1), "__NULL__");
502
if (rep == "__NULL__") {
506
newVal.replace(subst.cap(0), rep);
509
if (!Regexp || (Regexp && OK)) {
510
((QLabel*)theMainWidget)->setText(QString("<b>%1</b>").arg(newVal));
511
if (newVal != oldVal) {
512
emit tagChanged(theTag, newVal);
515
((QLabel*)theMainWidget)->setText(QString("<b>%1</b>").arg(F->tagValue(theTag, "")));
518
TagTemplateWidgetConstant* TagTemplateWidgetConstant::fromXml(const QDomElement& e)
520
TagTemplateWidgetConstant* aCW = new TagTemplateWidgetConstant();
522
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
523
QDomElement t = n.toElement();
527
aCW->parseCommonElements(t);
533
bool TagTemplateWidgetConstant::toXML(QDomElement& xParent, bool header)
537
if (header) return OK;
539
QDomElement e = xParent.ownerDocument().createElement("widget");
540
xParent.appendChild(e);
542
if (!theId.isEmpty())
543
e.setAttribute("id", theId);
544
e.setAttribute("type", theType);
545
e.setAttribute("tag", theTag);
546
generateCommonElements(e);
551
/** TagTemplateWidgetEdit **/
553
QWidget* TagTemplateWidgetEdit::getWidget(const MapFeature* F)
555
if (theSelector && (theSelector->matches(F) != TagSelect_Match && theSelector->matches(F) != TagSelect_DefaultMatch))
558
QString lang = getDefaultLanguage();
559
QString defLang = "en";
560
if (lang == "-" || !M_PREFS->getTranslateTags())
563
theWidget = new QWidget();
564
QHBoxLayout* aLayout = new QHBoxLayout(theWidget);
565
aLayout->setContentsMargins(0, 0, 0, 0);
567
QLabel* aLabel = new QLabel();
568
aLabel->setOpenExternalLinks(true);
569
aLabel->setWordWrap(true);
570
aLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
571
aLabel->setMaximumWidth(55);
572
aLayout->addWidget(aLabel);
575
if (theDescriptions.count(lang))
576
theDescription = theDescriptions[lang];
578
if (theDescriptions.count(defLang))
579
theDescription = theDescriptions[defLang];
581
theDescription = theTag;
582
if (!theUrl.isEmpty())
583
url = theUrl.toString();
586
aLabel->setText(QString("<a href=\"%1\">%2</a>").arg(url).arg(theDescription));
588
aLabel->setText(theDescription);
590
QLineEdit* aValue = new QLineEdit();
591
aLayout->addWidget(aValue);
593
QString val = F->tagValue(theTag, "__NULL__");
594
if (val != "__NULL__")
595
aValue->setText(val);
597
connect(aValue,SIGNAL(editingFinished()),this, SLOT(on_editingFinished()));
598
theMainWidget = aValue;
599
theLabelWidget = aLabel;
604
void TagTemplateWidgetEdit::apply(const MapFeature*)
608
TagTemplateWidgetEdit* TagTemplateWidgetEdit::fromXml(const QDomElement& e)
610
TagTemplateWidgetEdit* aCW = new TagTemplateWidgetEdit();
612
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
613
QDomElement t = n.toElement();
617
aCW->parseCommonElements(t);
623
bool TagTemplateWidgetEdit::toXML(QDomElement& xParent, bool header)
628
QDomElement e = xParent.ownerDocument().createElement("widgetref");
629
xParent.appendChild(e);
630
if (!theId.isEmpty())
631
e.setAttribute("id", theId);
633
e.setAttribute("id", theTag);
635
QDomElement e = xParent.ownerDocument().createElement("widget");
636
xParent.appendChild(e);
638
if (!theId.isEmpty())
639
e.setAttribute("id", theId);
640
e.setAttribute("type", theType);
641
e.setAttribute("tag", theTag);
642
generateCommonElements(e);
648
void TagTemplateWidgetEdit::on_editingFinished()
650
QLineEdit* W = dynamic_cast<QLineEdit*>(theMainWidget);
652
if (W->text().isEmpty()) {
653
emit tagCleared(theTag);
655
emit tagChanged(theTag, W->text());
662
TagTemplate::TagTemplate()
663
: theWidget(0), theSelector(0)
667
TagTemplate::TagTemplate(QString aName)
668
: theWidget(0), theSelector(0)
673
TagTemplate::TagTemplate(QString aName, QString aSelector)
677
theSelector = TagSelector::parse(aSelector);
680
TagTemplate::TagTemplate(const TagTemplate& aTemplate)
681
: QObject(), theSelector(aTemplate.theSelector)
685
TagTemplate::~TagTemplate()
690
TagSelectorMatchResult TagTemplate::matchesTag(const MapFeature* F)
692
TagSelectorMatchResult res;
694
if (!theSelector) return TagSelect_NoMatch;
695
// Special casing for multipolygon roads
696
if (const Road* R = dynamic_cast<const Road*>(F))
698
// TODO create a isPartOfMultiPolygon(R) function for this
699
for (int i=0; i<R->sizeParents(); ++i)
701
if (const Relation* Parent = dynamic_cast<const Relation*>(R->getParent(i)))
702
if (Parent->tagValue("type","") == "multipolygon")
703
return TagSelect_NoMatch;
706
if ((res = theSelector->matches(F)))
708
// Special casing for multipolygon relations
709
if (const Relation* R = dynamic_cast<const Relation*>(F))
711
for (int i=0; i<R->size(); ++i)
712
if ((res = theSelector->matches(R->get(i))))
715
return TagSelect_NoMatch;
718
QWidget* TagTemplate::getWidget(const MapFeature* F)
720
QString lang = getDefaultLanguage();
721
QString defLang = "en";
722
if (lang == "-" || !M_PREFS->getTranslateTags())
725
theWidget = new QWidget();
726
//if (theDescriptions.count(lang))
727
// theWidget->setTitle(theDescriptions[lang]);
729
// theWidget->setTitle(theDescriptions[defLang]);
730
QVBoxLayout* aLayout = new QVBoxLayout(theWidget);
731
aLayout->setContentsMargins(2, 2, 2, 2);
733
for (int i=0; i<theFields.size(); ++i) {
734
QWidget* W = theFields[i]->getWidget(F);
736
aLayout->addWidget(W);
742
void TagTemplate::setSelector(const QString& anExpression)
745
theSelector = TagSelector::parse(anExpression);
748
void TagTemplate::setSelector(TagSelector* aSel)
754
void TagTemplate::apply(const MapFeature* F)
756
for (int i=0; i<theFields.size(); ++i) {
757
theFields[i]->apply(F);
761
TagTemplate* TagTemplate::fromXml(const QDomElement& e, TagTemplates* templates)
763
TagTemplate* aTemplate = new TagTemplate();
765
if (e.tagName() != "template") {
769
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
770
QDomElement t = n.toElement();
774
if (t.tagName() == "description") {
775
aTemplate->theDescriptions.insert(t.attribute("locale"), t.firstChild().toText().nodeValue());
777
if (t.tagName() == "selector") {
778
TagSelector* aSel = TagSelector::parse(t.attribute("expr"));
781
aTemplate->setSelector(aSel);
783
if (t.tagName() == "widget") {
784
TagTemplateWidget* aTW = TagTemplateWidget::fromXml(t);
786
templates->addWidget(aTW);
787
aTemplate->theFields.append(aTW);
788
connect(aTW, SIGNAL(tagChanged(QString, QString)), aTemplate, SLOT(on_tag_changed(QString, QString)));
789
connect(aTW, SIGNAL(tagCleared(QString)), aTemplate, SLOT(on_tag_cleared(QString)));
792
if (t.tagName() == "widgetref") {
793
TagTemplateWidget* aTW = templates->findWidget(t.attribute("id"));
795
aTemplate->theFields.append(aTW);
796
connect(aTW, SIGNAL(tagChanged(QString, QString)), aTemplate, SLOT(on_tag_changed(QString, QString)));
797
connect(aTW, SIGNAL(tagCleared(QString)), aTemplate, SLOT(on_tag_cleared(QString)));
804
bool TagTemplate::toXML(QDomElement& xParent)
808
QDomElement e = xParent.ownerDocument().createElement("template");
809
xParent.appendChild(e);
812
QDomElement s = xParent.ownerDocument().createElement("selector");
814
s.setAttribute("expr", theSelector->asExpression(false));
817
QHashIterator<QString, QString > itD(theDescriptions);
818
while(itD.hasNext()) {
821
QDomElement d = xParent.ownerDocument().createElement("description");
824
d.setAttribute("locale", itD.key());
826
QDomText v = xParent.ownerDocument().createTextNode(itD.value());
830
for (int i=0; i<theFields.size(); ++i) {
831
theFields[i]->toXML(e, false);
837
void TagTemplate::on_tag_changed(QString k, QString v)
839
emit tagChanged(k, v);
842
void TagTemplate::on_tag_cleared(QString k)
849
TagTemplates::TagTemplates()
850
: theWidget(0), theFeature(0), curTemplate(0)
854
TagTemplates::~TagTemplates()
856
for (int i=0; i< widgets.size(); ++i)
858
for (int i=0; i< items.size(); ++i)
862
QWidget* TagTemplates::getWidget(const MapFeature* F)
864
QString lang = getDefaultLanguage();
865
QString defLang = "en";
866
if (lang == "-" || !M_PREFS->getTranslateTags())
870
disconnect(curTemplate, 0, this, 0);
874
forcedTemplate = NULL;
876
curTemplate = forcedTemplate;
879
theWidget = new QWidget();
880
//if (theDescriptions.count(lang))
881
// theWidget->setTitle(theDescriptions[lang]);
883
// theWidget->setTitle(theDescriptions[defLang]);
884
QVBoxLayout* aLayout = new QVBoxLayout(theWidget);
885
aLayout->setContentsMargins(2, 2, 2, 2);
887
QHBoxLayout* headLayout = new QHBoxLayout();
888
headLayout->setContentsMargins(0, 0, 0, 0);
889
aLayout->addLayout(headLayout);
891
theCombo = new QComboBox();
892
theCombo->setEditable(true);
893
theCombo->setInsertPolicy(QComboBox::InsertAtBottom);
894
// QFont f = theCombo->font();
895
// f.setPointSize(6);
896
// theCombo->setFont(f);
897
// theCombo->setMaximumHeight(16);
898
headLayout->addWidget(theCombo);
900
QFrame* line = new QFrame();
901
line->setFrameShape(QFrame::HLine);
902
line->setFrameShadow(QFrame::Sunken);
903
headLayout->addWidget(line);
906
theCombo->addItem(tr("Undefined"), "__NULL__");
907
for (int i=0; i<items.size(); ++i) {
908
if (items[i]->theDescriptions.count(lang))
909
theCombo->addItem(items[i]->theDescriptions[lang], qVariantFromValue(items[i]));
911
theCombo->addItem(items[i]->theDescriptions[defLang], qVariantFromValue(items[i]));
913
if (forcedTemplate) {
914
if (items[i] == forcedTemplate) {
919
if (!forcedTemplate) {
920
for (int i=0; i<items.size(); ++i) {
921
if (idx == -1 && items[i]->matchesTag(F) == TagSelect_Match) {
922
curTemplate = items[i];
927
if (!forcedTemplate && idx == -1) {
928
for (int i=0; i<items.size(); ++i) {
929
if (idx == -1 && items[i]->matchesTag(F) == TagSelect_DefaultMatch) {
930
curTemplate = items[i];
937
theCombo->setCurrentIndex(0);
939
theCombo->setCurrentIndex(idx);
942
aLayout->addWidget(curTemplate->getWidget(F));
943
connect(curTemplate, SIGNAL(tagChanged(QString, QString)), this, SLOT(on_tag_changed(QString, QString)));
944
connect(curTemplate, SIGNAL(tagCleared(QString)), this, SLOT(on_tag_cleared(QString)));
946
connect(theCombo,SIGNAL(activated(int)), this, SLOT(on_combo_activated(int)));
949
line->setFrameShape(QFrame::HLine);
950
line->setFrameShadow(QFrame::Sunken);
951
aLayout->addWidget(line);
956
void TagTemplates::on_combo_activated(int idx)
958
if (idx > items.count() -1) {
959
TagTemplate* newTmpl = new TagTemplate(theCombo->currentText());
960
items.append(newTmpl);
962
for (int i=0; i<curTemplate->theFields.count(); ++i) {
963
curTemplate->theFields[i]->getCurrentValue();
966
forcedTemplate = theCombo->itemData(idx).value<TagTemplate*>();
968
emit templateChanged(forcedTemplate);
972
TagTemplates* TagTemplates::fromXml(const QDomElement& e)
974
TagTemplates* aTemplates = new TagTemplates();
976
if (e.tagName() != "templates") {
980
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling())
982
QDomElement t = n.toElement();
986
if (t.tagName() == "template") {
987
TagTemplate* tmpl = TagTemplate::fromXml(t, aTemplates);
989
aTemplates->items.append(tmpl);
993
if (t.tagName() == "widgets") {
994
for(QDomNode n = t.firstChild(); !n.isNull(); n = n.nextSibling())
996
QDomElement w = n.toElement();
997
if (w.tagName() == "widget") {
998
TagTemplateWidget* aTW = TagTemplateWidget::fromXml(w);
1000
aTemplates->addWidget(aTW);
1010
bool TagTemplates::mergeXml(const QDomElement& e)
1012
if (e.tagName() != "templates") {
1016
for(QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling())
1018
QDomElement t = n.toElement();
1022
if (t.tagName() == "template") {
1023
TagTemplate* tmpl = TagTemplate::fromXml(t, this);
1029
if (t.tagName() == "widgets") {
1030
for(QDomNode n = t.firstChild(); !n.isNull(); n = n.nextSibling())
1032
QDomElement w = n.toElement();
1033
if (w.tagName() == "widget") {
1034
TagTemplateWidget* aTW = TagTemplateWidget::fromXml(w);
1046
TagTemplate* TagTemplates::match(MapFeature* F)
1048
for (int i=0; i<items.size(); ++i) {
1049
if (items[i]->matchesTag(F) == TagSelect_Match)
1052
for (int i=0; i<items.size(); ++i) {
1053
if (items[i]->matchesTag(F) == TagSelect_DefaultMatch)
1059
bool TagTemplates::toXML(QDomDocument& doc)
1063
QDomElement e = doc.createElement("templates");
1066
QDomElement w = doc.createElement("widgets");
1069
for (int i=0; i<widgets.size(); ++i)
1070
OK = widgets[i]->toXML(w, true);
1072
for (int i=0; i<items.size(); ++i)
1073
OK = items[i]->toXML(e);
1078
TagTemplateWidget* TagTemplates::findWidget(const QString& id)
1080
for (int i=0; i<widgets.size(); ++i) {
1081
if (widgets[i]->id() == id)
1088
void TagTemplates::on_tag_changed(QString k, QString v)
1090
emit tagChanged(k, v);
1093
void TagTemplates::on_tag_cleared(QString k)
1098
void TagTemplates::addWidget(TagTemplateWidget* aWidget)
1100
widgets.append(aWidget);
1103
void TagTemplates::apply(const MapFeature* F)
1106
curTemplate->apply(F);