summaryrefslogtreecommitdiffstats
path: root/src/gui/combobox.cpp
blob: d6457f1295df5702f1e022b0377c603aedcd3eb5 (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                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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(TQWidget* parent_) : KComboBox(parent_) {
  setEditable(false);
}

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

void ComboBox::insertItem(const TQString& s_, const TQVariant& 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(TQVariant());
    }
    m_data.insert(m_data.at(idx_), t_);
  }
}

void ComboBox::insertItems(const TQStringList& s_, const TQValueList<TQVariant>& 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 TQVariant& ComboBox::currentData() const {
  return data(currentItem());
}

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

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