summaryrefslogtreecommitdiffstats
path: root/src/gui/combobox.cpp
blob: 1aff29b85aa0a30a651e05f67ec13bac0ed941a2 (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
/***************************************************************************
    copyright            : (C) 2001-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 "combobox.h"

#include <kdebug.h>

using Tellico::GUI::ComboBox;

ComboBox::ComboBox(QWidget* parent_) : KComboBox(parent_) {
  setEditable(false);
}

void ComboBox::clear() {
  KComboBox::clear();
  m_data.clear();
}

void ComboBox::insertItem(const QString& s_, const QVariant& t_, int idx_/* =-1 */) {
  KComboBox::insertItem(s_, idx_);
  if(idx_ < 0) {
    m_data.push_back(t_);
  } else {
    while(idx_ > static_cast<int>(m_data.count())) {
      m_data.push_back(QVariant());
    }
    m_data.insert(m_data.at(idx_), t_);
  }
}

void ComboBox::insertItems(const QStringList& s_, const QValueList<QVariant>& t_, int idx_ /*=-1*/) {
  if(s_.count() != t_.count()) {
    kdWarning() << "ComboBox::insertItems() - must have equal number of items in list!" << endl;
    return;
  }

  for(uint i = 0; i < s_.count(); ++i) {
    insertItem(s_[i], t_[i], idx_+i);
  }
}

const QVariant& ComboBox::currentData() const {
  return data(currentItem());
}

const QVariant& ComboBox::data(uint idx_) const {
  if(idx_ >= m_data.count()) {
    static QVariant t; // inescapable
    return t;
  }
  return m_data[idx_];
}

void ComboBox::setCurrentData(const QVariant& data_) {
  for(uint i = 0; i < m_data.count(); ++i) {
    if(m_data[i] == data_) {
      setCurrentItem(i);
      break;
    }
  }
}