summaryrefslogtreecommitdiffstats
path: root/src/gui/stringmapdialog.cpp
blob: 4a5374fbd15617db49d711645d6c776ff3a78aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/***************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "stringmapdialog.h"

#include <klistview.h>
#include <klocale.h>
#include <klineedit.h>
#include <kbuttonbox.h>
#include <kiconloader.h>

#include <qlayout.h>
#include <qheader.h>
#include <qhbox.h>
#include <qwhatsthis.h>
#include <qpushbutton.h>

using Tellico::StringMapDialog;

StringMapDialog::StringMapDialog(const QMap<QString, QString>& map_, QWidget* parent_, const char* name_/*=0*/, bool modal_/*=false*/)
    : KDialogBase(parent_, name_, modal_, QString::null, Ok|Cancel) {
  QWidget* page = new QWidget(this);
  QVBoxLayout* l = new QVBoxLayout(page, 0, KDialog::spacingHint());

  m_listView = new KListView(page);
  m_listView->setAllColumnsShowFocus(true);
  m_listView->setShowSortIndicator(true);
  m_listView->addColumn(QString::null);
  m_listView->addColumn(QString::null);
  m_listView->header()->hide(); // hide header since neither column has a label initially
  m_listView->setColumnWidthMode(0, QListView::Maximum);
  m_listView->setColumnWidthMode(1, QListView::Maximum);
  m_listView->setResizeMode(QListView::AllColumns);
  connect(m_listView, SIGNAL(currentChanged(QListViewItem*)), SLOT(slotUpdate(QListViewItem*)));
  connect(m_listView, SIGNAL(clicked(QListViewItem*)), SLOT(slotUpdate(QListViewItem*)));
  l->addWidget(m_listView);

  QHBox* box = new QHBox(page);
  box->setMargin(4);
  box->setSpacing(KDialog::spacingHint());

  m_edit1 = new KLineEdit(box);
  m_edit1->setFocus();
  m_edit2 = new KLineEdit(box);
  KButtonBox* bb = new KButtonBox(box);
  bb->addStretch();
  QPushButton* btn = bb->addButton(i18n("&Set"), this, SLOT(slotAdd()));
  btn->setIconSet(BarIcon(QString::fromLatin1("filenew"), KIcon::SizeSmall));
  btn = bb->addButton(i18n("&Delete"), this, SLOT(slotDelete()));
  btn->setIconSet(BarIcon(QString::fromLatin1("editdelete"), KIcon::SizeSmall));

  l->addWidget(box);
  l->addStretch(1);
  setMainWidget(page);

  for(QMap<QString, QString>::ConstIterator it = map_.begin(); it != map_.end(); ++it) {
    if(!it.data().isEmpty()) {
      (void) new KListViewItem(m_listView, it.key(), it.data());
    }
  }

  setMinimumWidth(400);
  enableButtonSeparator(true);
}

void StringMapDialog::slotAdd() {
  QString s1 = m_edit1->text();
  QString s2 = m_edit2->text();
  if(s1.isEmpty() && s2.isEmpty()) {
    return;
  }
  QListViewItem* item = m_listView->currentItem();
  if(item && s1 == item->text(0)) { // only update values if same key
    item->setText(1, s2);
  } else {
    item = new KListViewItem(m_listView, s1, s2);
  }
  m_listView->ensureItemVisible(item);
  m_listView->setSelected(item, true);
  m_listView->setCurrentItem(item);
}

void StringMapDialog::slotDelete() {
  delete m_listView->currentItem();
  m_edit1->clear();
  m_edit2->clear();
  m_listView->setSelected(m_listView->currentItem(), true);
}

void StringMapDialog::slotUpdate(QListViewItem* item_) {
  if(item_) {
    m_edit1->setText(item_->text(0));
    m_edit2->setText(item_->text(1));
    m_listView->header()->adjustHeaderSize();
  } else {
    m_edit1->clear();
    m_edit2->clear();
  }
}

void StringMapDialog::setLabels(const QString& label1_, const QString& label2_) {
  m_listView->header()->setLabel(0, label1_);
  m_listView->header()->setLabel(1, label2_);
  m_listView->header()->show();
}

QMap<QString, QString> StringMapDialog::stringMap() {
  QMap<QString, QString> map;
  for(QListViewItem* item = m_listView->firstChild(); item; item = item->nextSibling()) {
    map.insert(item->text(0), item->text(1));
  }
  return map;
}

#include "stringmapdialog.moc"