xitemnode.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #include "xitemnode.h"
15 
16 XItemNodeBase::XItemNodeBase(const char *name, bool runtime, bool auto_set_any) :
17  XValueNodeBase(name, runtime) {
18  if(auto_set_any) {
19  iterate_commit([=](Transaction &tr){
20  m_lsnTryAutoSet = tr[ *this].onListChanged().connect( *this,
21  &XItemNodeBase::onTryAutoSet);
22  });
23  }
24 }
25 void
26 XItemNodeBase::onTryAutoSet(const Snapshot &shot, const Payload::ListChangeEvent &e) {
27  if( !autoSetAny()) return;
28  XString var = shot[ *this].to_str();
29  if(var.length()) return;
30  auto items = itemStrings(e.shot_of_list);
31  if(items.size()) {
32  trans( *this).str(items.front().label);
33  }
34 }
35 
36 void
37 xpointeritemnode_throwConversionError_() {
38  throw XKameError(i18n_noncontext("No item."), __FILE__, __LINE__);
39 }
40 
41 XComboNode::XComboNode(const char *name, bool runtime, bool auto_set_any)
42  : XItemNodeBase(name, runtime, auto_set_any) {
43 }
44 
45 void
47  *this = var;
48 }
49 
51 XComboNode::Payload::operator=(const XString &var) {
52  int i = -1;
53  if(var.length()) {
54  for(i = 0; i < m_strings->size(); ++i) {
55  if(m_strings->at(i) == var) {
56  break;
57  }
58  }
59  }
60  if(i == m_strings->size())
61  i = -1;
62  m_var = std::pair<XString, int>(var, i);
63  tr().mark(onValueChanged(), static_cast<XValueNodeBase*>( &node()));
64  return *this;
65 }
66 
68 XComboNode::Payload::operator=(int t) {
69  if((t >= 0) && (t < (int)m_strings->size()))
70  m_var = std::pair<XString, int>(m_strings->at(t), t);
71  else
72  m_var = std::pair<XString, int>("", -1);
73  tr().mark(onValueChanged(), static_cast<XValueNodeBase*>( &node()));
74  return *this;
75 }
76 
77 void
78 XComboNode::Payload::add(const XString &str) {
79  m_strings = std::make_shared<std::deque<XString>>( *m_strings);
80  m_strings->push_back(str);
81  ListChangeEvent e(tr(), static_cast<XItemNodeBase*>( &node()));
82  tr().mark(onListChanged(), std::move(e));
83  if(str == m_var.first) {
84  m_var.second = m_strings->size() - 1;
85  tr().mark(onValueChanged(), static_cast<XValueNodeBase*>( &node()));
86  }
87 }
88 
89 void
90 XComboNode::Payload::clear() {
91  m_strings = std::make_shared<std::deque<XString>>();
92  ListChangeEvent e(tr(), static_cast<XItemNodeBase*>( &node()));
93  tr().mark(onListChanged(), std::move(e));
94  if(m_var.second >= 0) {
95  m_var.second = -1;
96  tr().mark(onValueChanged(), static_cast<XValueNodeBase*>( &node()));
97  }
98 }
99 
100 std::vector<XItemNodeBase::Item> XComboNode::Payload::itemStrings() const {
101  std::vector<XItemNodeBase::Item> items;
102  for(auto it = m_strings->begin(); it != m_strings->end(); it++) {
103  XItemNodeBase::Item item;
104  item.name = *it;
105  item.label = *it;
106  items.push_back(std::move(item));
107  }
108  assert(m_strings->size() || (m_var.second < 0));
109  if(m_var.second < 0) {
110  XItemNodeBase::Item item;
111  item.name = m_var.first;
112  if(item.name.length()) {
113  item.label = formatString("(%s)", item.name.c_str());
114  items.push_back(std::move(item));
115  }
116  }
117  return items;
118 }

Generated for KAME4 by  doxygen 1.8.3