GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoColorComboBox.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 "QGoColorComboBox.h"
35 #include <iostream>
36 #include <sstream>
37 #include <QStringListModel>
38 #include <QHeaderView>
39 #include <QSettings>
40 #include <QApplication>
41 #include <QClipboard>
42 #include <QToolButton>
43 #include <QPainter>
44 
45 QGoColorComboBox::QGoColorComboBox(std::string iTextToAddANewOne,
46  QWidget *iparent,
47  std::string iTextToDelete) :
48  QGoComboBox(iTextToAddANewOne,
49  iparent, iTextToDelete)
50 {
51  QObject::connect( this, SIGNAL( AddANewOneActivated() ),
52  this, SLOT( ActionWhenNewOneRequested() ) );
53 }
54 
55 //--------------------------------------------------------------------------
56 
57 //--------------------------------------------------------------------------
59 {
60 }
61 
62 //--------------------------------------------------------------------------
63 
64 //--------------------------------------------------------------------------
66  std::list< ItemColorComboboxData > iDataFromList)
67 {
68  this->SetItemsFromListWithColor(iDataFromList);
69  //if it is the 1rst time for the list to be displayed, there has to be an
70  // activated
71  //item:
72  //by default, the one selected by the combobox is the one to stick to:
73  this->EmitActivatedItem( this->currentIndex() );
74 }
75 
76 //--------------------------------------------------------------------------
77 
78 //--------------------------------------------------------------------------
80  std::list< ItemColorComboboxData > iDataFromList)
81 {
82  this->clear();
83  if ( !iDataFromList.empty() )
84  {
85  std::list< ItemColorComboboxData >::iterator iter =
86  iDataFromList.begin();
87  while ( iter != iDataFromList.end() )
88  {
89  this->AddItemWithColor(*iter, false); //we don't want the signal emitted
90  // for each added item,
91  //only the currentIndex one at the end of the added list.
92  ++iter;
93  }
94  this->AddItemsEndOfList();
95  }
96  else
97  {
98  this->SetAddText();
99  }
100 }
101 
102 //--------------------------------------------------------------------------
103 
104 //--------------------------------------------------------------------------
106  bool SelectTheAddedItem)
107 {
108  QPixmap pix(12, 12);
109  QPainter painter(&pix);
110 
111  if ( iNewItemData.second.isValid() )
112  {
113  painter.setPen(Qt::gray);
114  painter.setBrush( QBrush(iNewItemData.second) );
115  painter.drawRect(0, 0, 12, 12);
116  }
117  QIcon Icon;
118  Icon.addPixmap(pix);
119  //we want to insert the item before the "add new item"/"delete items...":
120  //if the index is 0 or negative,the new item is prepended to the list of
121  // existing items:
122  int Index = this->count() - this->m_NumberOfItemsAfterList;
123  if ( this->GetTheItemColorComboBoxData(this->count() - 1).second.isValid() )
124  {
125  this->addItem(Icon, iNewItemData.first.c_str(), iNewItemData.second);
126  }
127  else
128  {
129  this->insertItem(Index, Icon, iNewItemData.first.c_str(), iNewItemData.second);
130  }
131  //this->addItem(Icon,iNewItemData.first.c_str(),iNewItemData.second);
132  if ( SelectTheAddedItem )
133  {
134  this->setCurrentIndex(Index);
135  this->EmitActivatedItem(Index);
136  }
137 }
138 
139 //--------------------------------------------------------------------------
140 
141 //--------------------------------------------------------------------------
142 void QGoColorComboBox::EmitActivatedItem(int iIndexActivatedItem)
143 {
144  ItemColorComboboxData ItemActivated =
145  this->GetTheItemColorComboBoxData(iIndexActivatedItem);
146  emit ItemSelected(ItemActivated);
147 }
148 
149 //--------------------------------------------------------------------------
150 
151 //--------------------------------------------------------------------------
154 {
156  QVariant variant = this->itemData(iIndex);
157 
158  Item.second = variant.value< QColor >();
159  Item.first = this->itemText(iIndex).toStdString();
160  return Item;
161 }
162 
163 //--------------------------------------------------------------------------
164 
165 //--------------------------------------------------------------------------
std::string toStdString() const
void SetItemsFromListWithColor(std::list< ItemColorComboboxData > iDataFromList)
clear the items already in the combobox,displayed the one in the iDataFromList and the items to add/d...
void clear()
void AddItemWithColor(ItemColorComboboxData iNewItemData, bool SelectTheAddedItem=true)
add an item with color at the end of the list befor the &quot;add new...&quot; if they have already been added ...
T value() const
QString itemText(int index) const
std::pair< std::string, QColor > ItemColorComboboxData
void insertItem(int index, const QString &text, const QVariant &userData)
void addItem(const QString &text, const QVariant &userData)
QGoColorComboBox(std::string iTextToAddANewOne, QWidget *iparent=0, std::string iTextToDelete="")
void drawRect(const QRectF &rectangle)
int count() const
void setPen(const QColor &color)
void SetAddText()
add the &quot;add new one&quot; item at the end of the list
void setBrush(const QBrush &brush)
virtual void ActionWhenNewOneRequested()=0
slot linked to the signal AddANewOneActivated()
void addPixmap(const QPixmap &pixmap, Mode mode, State state)
QVariant itemData(int index, int role) const
virtual ~QGoColorComboBox()
void AddANewOneActivated()
int m_NumberOfItemsAfterList
Definition: QGoComboBox.h:112
inherits from Qt QCombobox but add a the end of the list of items, 1 or 2 items: the first one to add...
Definition: QGoComboBox.h:47
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
void ItemSelected(ItemColorComboboxData)
int currentIndex() const
void InitializeTheListWithColor(std::list< ItemColorComboboxData > iDataFromList)
call the method setItemsWithColorFromList and send a signal with the current index.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ItemColorComboboxData GetTheItemColorComboBoxData(int iIndex)
get the name and the QColor of the item located at the index iIndex
virtual void EmitActivatedItem(int iIndexActivatedItem)