GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoDeleteFromListDialog.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 =========================================================================*/
35 #include <QStringListModel>
36 #include <QStringList>
37 #include <QTreeWidget>
38 #include <QListWidget>
39 #include <QListWidgetItem>
40 #include <QVBoxLayout>
41 #include <QSortFilterProxyModel>
42 #include <QDialogButtonBox>
43 #include <QMessageBox>
44 #include <QModelIndex>
45 #include <QPainter>
46 #include "ConvertToStringHelper.h"
47 
49  (std::vector< std::string > iVectorEntities,
50  QWidget *iParent,
51  std::string iEntityName
52  ) : QDialog(iParent)
53 {
54  this->SetUpUi(iEntityName);
55  this->SetItemsFromTheVector(iVectorEntities);
56 }
57 
58 //--------------------------------------------------------------------------
59 
60 //--------------------------------------------------------------------------
63  QWidget *iParent,
64  std::string iEntityName
65  ) : QDialog(iParent)
66 {
67  this->SetUpUi(iEntityName);
68  this->SetItemsInTheListWithColor(iDataListWithColor);
69 }
70 
71 //--------------------------------------------------------------------------
72 
73 //--------------------------------------------------------------------------
75 {
76 }
77 
78 //--------------------------------------------------------------------------
79 
80 //--------------------------------------------------------------------------
82  std::vector< std::string > iVectorItems)
83 {
84  for ( size_t i = 0; i < iVectorItems.size(); ++i )
85  {
86  QListWidgetItem *item =
87  new QListWidgetItem(iVectorItems[i].c_str(), this->m_ListWidget);
88  (void)item;
89  }
90 }
91 
92 //--------------------------------------------------------------------------
93 
94 //-------------------------------------------------------------------------
96  std::list< ItemColorComboboxData > iDataList)
97 {
98  std::list< ItemColorComboboxData >::iterator iter = iDataList.begin();
99  QPixmap pix(12, 12);
100  QPainter painter(&pix);
101  QIcon Icon;
102  while ( iter != iDataList.end() )
103  {
104  if ( iter->second.isValid() )
105  {
106  painter.setPen(Qt::gray);
107  painter.setBrush( QBrush(iter->second) );
108  painter.drawRect(0, 0, 12, 12);
109  }
110  Icon.addPixmap(pix);
111  QListWidgetItem *item =
112  new QListWidgetItem(Icon, iter->first.c_str(), this->m_ListWidget);
113  (void)item;
114  ++iter;
115  }
116 }
117 
118 //--------------------------------------------------------------------------
119 
120 //-------------------------------------------------------------------------
122 {
123  QList< QListWidgetItem * > ListEntitiesToDeleteSelected =
124  this->m_ListWidget->selectedItems();
125  if ( !ListEntitiesToDeleteSelected.empty() )
126  {
127  QMessageBox msgBox;
128  msgBox.setText(
129  tr("Are you sure you want to delete these %1s ?")
130  .arg( this->m_EntityName.c_str() ) );
131  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
132  int r = msgBox.exec();
133  if ( r == 16384 )
134  {
135  DeleteSelection(ListEntitiesToDeleteSelected);
136  this->accept();
137  }
138  else
139  {
140  msgBox.close();
141  }
142  }
143  else
144  {
145  QMessageBox msgBox;
146  msgBox.setText(
147  tr("Please select at least one %1.")
148  .arg( this->m_EntityName.c_str() ) );
149  msgBox.exec();
150  }
151 }
152 
153 //--------------------------------------------------------------------------
154 
155 //--------------------------------------------------------------------------
157  QList< QListWidgetItem * > iListEntitiesToDelete)
158 {
159  std::vector< std::string > VectorNamesToDelete;
160  for ( int i = 0; i < iListEntitiesToDelete.size(); i++ )
161  {
162  VectorNamesToDelete.push_back( iListEntitiesToDelete.at(i)->text().toStdString() );
163  }
164  emit ListEntitiesToDelete(VectorNamesToDelete);
165 }
166 
167 //--------------------------------------------------------------------------
168 
169 //--------------------------------------------------------------------------
170 void QGoDeleteFromListDialog::SetUpUi(std::string iEntityName)
171 {
172  this->m_EntityName = iEntityName;
173  this->m_ListWidget = new QListWidget(this);
174  this->m_ListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
175 
176  QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok
177  | QDialogButtonBox::Cancel);
178 
179  QVBoxLayout *vlayout = new QVBoxLayout(this);
180  vlayout->addWidget(this->m_ListWidget);
181  vlayout->addWidget(ButtonBox);
182  this->setWindowTitle( tr("Delete a %1").arg( this->m_EntityName.c_str() ) );
183  this->setLayout(vlayout);
184 
185  QObject::connect( ButtonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );
186  QObject::connect( ButtonBox, SIGNAL( rejected() ), this, SIGNAL( CancelRequested() ) );
187  QObject::connect( ButtonBox, SIGNAL( accepted() ), this, SLOT( SelectionValidation() ) );
188 }
bool close()
virtual void reject()
void setSelectionMode(QAbstractItemView::SelectionMode mode)
void rejected()
void SetUpUi(std::string iEntityName)
set the layout with all the objects, the connections and the entity name
const T & at(int i) const
void accepted()
std::list< ItemColorComboboxData > ListOfItemColorComboboxData
QString tr(const char *sourceText, const char *disambiguation, int n)
void DeleteSelection(QList< QListWidgetItem * > iListEntitiesToDelete)
emit a signal which sends vector with the names of the entities the user selected to be deleted ...
int size() const
void drawRect(const QRectF &rectangle)
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
void setLayout(QLayout *layout)
bool empty() const
void ListEntitiesToDelete(std::vector< std::string >)
void setPen(const QColor &color)
void setText(const QString &text)
void SelectionValidation()
ask the user to select at least one item if nothying has been selected and ask the user confirmation ...
void setBrush(const QBrush &brush)
void addPixmap(const QPixmap &pixmap, Mode mode, State state)
void setStandardButtons(QFlags< QMessageBox::StandardButton > buttons)
virtual void accept()
QList< QListWidgetItem * > selectedItems() const
void SetItemsFromTheVector(std::vector< std::string > iVectorItems)
create the corresponding QListWidgetItems
QGoDeleteFromListDialog(std::vector< std::string > iVectorEntities, QWidget *iParent=0, std::string iEntityName="")
void setWindowTitle(const QString &)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void SetItemsInTheListWithColor(std::list< ItemColorComboboxData > iDataList)
create the corresponding QListWidgetItems with a QColor