GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoComboBox.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 #include "QGoComboBox.h"
35 #include <iostream>
36 #include <sstream>
37 
38 QGoComboBox::QGoComboBox(std::string iTextToAddANewOne,
39  QWidget *iparent,
40  std::string iTextToDelete) :
41  QComboBox(iparent)
42 {
43  this->m_TextToAddANewOne = iTextToAddANewOne;
44  this->m_TextToDelete = iTextToDelete;
45  if ( this->m_TextToDelete.empty() )
46  {
47  this->m_NumberOfItemsAfterList = 1;
48  }
49  else
50  {
51  this->m_NumberOfItemsAfterList = 2;
52  }
53 
54  QObject::connect( this,
55  SIGNAL( activated(int) ),
56  this,
57  SLOT( CheckUserAction(int) ) );
58 }
59 
60 //--------------------------------------------------------------------------
61 
62 //--------------------------------------------------------------------------
64 {
65 }
66 
67 //--------------------------------------------------------------------------
68 
69 //--------------------------------------------------------------------------
71 {
72  this->SetItemsFromList(iListItems);
73  //if it is the 1rst time for the list to be displayed, there has to be an
74  // activated
75  //item:
76  //by default, the one selected by the combobox is the one to stick to:
77  this->EmitActivatedItem( this->currentIndex() );
78 }
79 
80 //--------------------------------------------------------------------------
81 
82 //--------------------------------------------------------------------------
84 {
85  this->InitializeTheList( this->GetQStringListNames(iItemsData) );
86 }
87 
88 //--------------------------------------------------------------------------
89 
90 //--------------------------------------------------------------------------
92 {
93  this->SetAddText();
94  if ( !this->m_TextToDelete.empty() )
95  {
96  this->addItem( this->m_TextToDelete.c_str() );
97  }
98  this->show();
99 }
100 
101 //--------------------------------------------------------------------------
102 
103 //--------------------------------------------------------------------------
105 {
106  this->clear();
107  if ( !iDataFromList.empty() )
108  {
109  this->addItems(iDataFromList);
110  this->AddItemsEndOfList();
111  }
112  else
113  {
114  this->SetAddText();
115  }
116 }
117 
118 //--------------------------------------------------------------------------
119 
120 //--------------------------------------------------------------------------
122 {
123  this->SetItemsFromList( this->GetQStringListNames(iItemsData) );
124 }
125 
126 //--------------------------------------------------------------------------
127 
128 //--------------------------------------------------------------------------
130 {
131  QStringList oQListItems;
132  NamesDescrContainerType::iterator iter = iContainer.begin();
133 
134  while ( iter != iContainer.end() )
135  {
136  oQListItems.append( iter->first.c_str() );
137  ++iter;
138  }
139  return oQListItems;
140 }
141 
142 //--------------------------------------------------------------------------
143 
144 //--------------------------------------------------------------------------
145 void QGoComboBox::CheckUserAction(int iIndexActivatedItem)
146 {
147  //int numberitem = this->count();
148  int IndexAdd = this->count() - this->m_NumberOfItemsAfterList;
149  int IndexDelete = this->count() - 1;
150 
151  //in case there is normally an add and a delete but the list is empty,so
152  //there is temporarily only the "add..":
153  if ( IndexAdd < 0 )
154  {
155  IndexAdd = IndexDelete;
156  }
157 
158  if ( iIndexActivatedItem == IndexAdd )
159  {
160  emit AddANewOneActivated();
161  return;
162  }
163  //if there is an item "Delete..."
164  if ( IndexDelete != IndexAdd )
165  {
166  if ( iIndexActivatedItem == IndexDelete )
167  {
168  emit DeleteActivated();
169  return;
170  }
171  }
172 
173  this->EmitActivatedItem(iIndexActivatedItem);
174 }
175 
176 //--------------------------------------------------------------------------
177 
178 //--------------------------------------------------------------------------
179 void QGoComboBox::EmitActivatedItem(int iIndexActivatedItem)
180 {
181  emit ItemSelected( this->itemText(iIndexActivatedItem).toStdString() );
182 }
183 
184 //--------------------------------------------------------------------------
185 
186 //--------------------------------------------------------------------------
187 void QGoComboBox::SetCurrentItem(std::string iItemText)
188 {
189  int index = this->findText( iItemText.c_str() );
190 
191  if ( index == -1 )
192  {
193  this->setCurrentIndex(0);
194  }
195  else
196  {
197  this->setCurrentIndex(index);
198  }
199 }
200 
201 //--------------------------------------------------------------------------
202 
203 //--------------------------------------------------------------------------
205 {
206  this->addItem( this->m_TextToAddANewOne.c_str() );
207 }
208 
209 //--------------------------------------------------------------------------
210 
211 //--------------------------------------------------------------------------
213 {
214  this->setCurrentIndex(iIndex);
215  this->EmitActivatedItem(iIndex);
216 }
217 
218 //--------------------------------------------------------------------------
219 
220 //--------------------------------------------------------------------------
221 void QGoComboBox::SetCurrentItemAndActivate(std::string iItemText)
222 {
223  int index = this->findText( iItemText.c_str() );
224 
225  if ( index == -1 )
226  {
227  index = 0;
228  }
229  this->setCurrentIndex(index);
230  this->EmitActivatedItem(index);
231 }
std::string m_TextToAddANewOne
Definition: QGoComboBox.h:110
virtual void InitializeTheList(QStringList iListItems)
call the method SetItemsFromList and send a signal with the current index.
Definition: QGoComboBox.cxx:70
void clear()
void SetCurrentItemAndActivate(int iIndex)
select the current item located at iIndex and send a signal with the name of this item...
QString itemText(int index) const
void addItem(const QString &text, const QVariant &userData)
QStringList GetQStringListNames(NamesDescrContainerType iContainer)
Get a QStringList with the names of the item from a NamesDescrContainerType.
int findText(const QString &text, QFlags< Qt::MatchFlag > flags) const
virtual ~QGoComboBox()
Definition: QGoComboBox.cxx:63
int count() const
void append(const T &value)
bool empty() const
std::vector< std::pair< std::string, std::string > > NamesDescrContainerType
Definition: QGoComboBox.h:58
void ItemSelected(std::string)
void SetAddText()
add the &quot;add new one&quot; item at the end of the list
void activated(int index)
void SetCurrentItem(std::string iItemText)
set the activated item corresponding to the iTemText (no need to emit the signal ItemSelected) ...
QGoComboBox(std::string iTextToAddANewOne, QWidget *iparent=0, std::string iTextToDelete="")
if the string iTextToDelete is empty, there will be only the add a new item at the end of the list ...
Definition: QGoComboBox.cxx:38
void DeleteActivated()
virtual void EmitActivatedItem(int iIndexActivatedItem)
call the signal to send the index of the activated item.
void AddANewOneActivated()
int m_NumberOfItemsAfterList
Definition: QGoComboBox.h:112
virtual void SetItemsFromList(QStringList iDataFromList)
clear the items already in the combobox,displayed the one in the QStringList and the items to add/del...
std::string m_TextToDelete
Definition: QGoComboBox.h:111
void AddItemsEndOfList()
Add the &quot;Add a new one...&quot; and &quot;Delete...&quot; text items at the end of the items list.
Definition: QGoComboBox.cxx:91
int currentIndex() const
void CheckUserAction(int iIndexActivatedItem)
check which item has been clicked and emit the corresponding signal: addanewone, deleteactivated or i...
void addItems(const QStringList &texts)
void show()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
iterator begin()