GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoTableWidget.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 
35 #include "QGoTableWidget.h"
36 #include <iostream>
37 #include <sstream>
38 #include <QTableWidgetSelectionRange>
39 #include <QHeaderView>
40 #include <QSettings>
41 #include <QApplication>
42 #include <QClipboard>
43 #include <QToolButton>
44 #include <QMessageBox>
45 
47 {
48  PrevCol = -1;
49  PrevOrder = -1;
50  QObject::connect( this,
51  SIGNAL( cellClicked(int, int) ),
52  this, SLOT( UpdateColumnsWithCheckBoxes(int, int) ) );
53 
54  this->setContextMenuPolicy(Qt::CustomContextMenu);
55 }
56 
57 //--------------------------------------------------------------------------
58 
59 //--------------------------------------------------------------------------
60 QGoTableWidget::QGoTableWidget(int rows, int columns, QWidget *iParent) :
61  QTableWidget(rows, columns, iParent), PrevCol(-1), PrevOrder(-1)
62 {
63 }
64 
65 //--------------------------------------------------------------------------
66 
67 //--------------------------------------------------------------------------
69 {
70 }
71 
72 //--------------------------------------------------------------------------
73 
74 //--------------------------------------------------------------------------
75 void QGoTableWidget::sortItems(int iColumn, Qt::SortOrder iOrder)
76 {
77  if ( ( iColumn != PrevCol ) && ( iOrder != PrevOrder ) )
78  {
79  PrevCol = iColumn;
80  PrevOrder = iOrder;
81  QTableWidget::sortItems(iColumn, iOrder);
82  }
83 }
84 
85 //--------------------------------------------------------------------------
86 
87 //--------------------------------------------------------------------------
88 int QGoTableWidget::findValueGivenColumn(int iValue, const QString & iColumn)
89 {
90  int ColumnIndex = findColumnName(iColumn);
91 
92  if ( ColumnIndex == -1 )
93  {
94  std::cout << "The column figureID has not been found";
95  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
96  std::cout << std::endl;
97  std::cout << std::endl;
98  return -1;
99  }
100  else
101  {
102  for ( int i = 0; i < rowCount(); i++ )
103  {
104  if ( this->item(i, ColumnIndex)->text().toInt() == iValue )
105  {
106  return i;
107  }
108  }
109  }
110  return -1;
111 }
112 
113 //--------------------------------------------------------------------------
114 
115 //--------------------------------------------------------------------------
116 int QGoTableWidget::findColumnName(const QString & iColumnName)
117 {
118  QStringList ColumnsHeader = recordHeaderNamesOrder();
119 
120  if ( ColumnsHeader.isEmpty() )
121  {
122  std::cout << "The QStringlist ColumnsHeader is empty";
123  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
124  std::cout << std::endl;
125  return -1;
126  }
127  return ColumnsHeader.indexOf(iColumnName, 0);
128 }
129 
130 //--------------------------------------------------------------------------
131 
132 //--------------------------------------------------------------------------
134 {
135  QStringList ColumnNamesOrder;
136 
137  for ( int i = 0; i < this->columnCount(); i++ )
138  {
139  ColumnNamesOrder.append( this->horizontalHeaderItem(i)->text() );
140  }
141 
142  return ColumnNamesOrder;
143 }
144 
145 //--------------------------------------------------------------------------
146 
147 //--------------------------------------------------------------------------
148 void QGoTableWidget::SetVisibleStateForTraceID(unsigned int iTraceID,
149  const std::string & iTraceName,
150  Qt::CheckState iState,
151  bool EmitSignal)
152 {
153  unsigned int ColumnIndex = this->findColumnName("Show");
154  int RowIndex = this->GetRowForTraceID(iTraceID, iTraceName);
155 
156  QTableWidgetItem* temp = this->item(RowIndex, ColumnIndex);
157 
158  if( temp )
159  {
160  this->setVisibleStateCheckBox( temp, iState, EmitSignal);
161  }
162 }
163 
164 //--------------------------------------------------------------------------
165 
166 //--------------------------------------------------------------------------
168  const std::list< unsigned int > & iListTraceIDs,
169  Qt::CheckState iState,
170  const std::string & iTraceName)
171 {
172  std::list< unsigned int >::const_iterator iter = iListTraceIDs.begin();
173 
174  while ( iter != iListTraceIDs.end() )
175  {
176  this->SetVisibleStateForTraceID(*iter, iTraceName, iState, false);
177  ++iter;
178  }
179 }
180 
181 //--------------------------------------------------------------------------
182 
183 //--------------------------------------------------------------------------
184 void QGoTableWidget::SetCheckStateForTraceID(unsigned int iTraceID,
185  const std::string & iTraceName,
186  Qt::CheckState iState,
187  bool EmitSignal)
188 {
189 
190 std::cout << "iTraceID: " << iTraceID << std::endl;
191 std::cout << "iTraceName: " << iTraceName << std::endl;
192 std::cout << "EmitSignal: " << EmitSignal << std::endl;
193 
194 unsigned int ColumnIndex = this->findColumnName("");
195 int RowIndex = this->GetRowForTraceID(iTraceID, iTraceName);
196 
197 std::cout << "ColumnIndex: " << ColumnIndex << std::endl;
198 std::cout << "RowIndex: " << RowIndex << std::endl;
199 std::cout << "this->item(RowIndex, ColumnIndex): " << this->item(RowIndex, ColumnIndex) << std::endl;
200 
202  this->item(RowIndex, ColumnIndex), iState, EmitSignal);
203 }
204 
205 //--------------------------------------------------------------------------
206 
207 //--------------------------------------------------------------------------
208 int QGoTableWidget::GetRowForTraceID(unsigned int iTraceID,
209  const std::string & iTraceName)
210 {
211  //get the row index based on iTraceName:
212  std::stringstream TraceIDName;
213 
214  TraceIDName << iTraceName;
215  TraceIDName << "ID";
216  int oRowIndex = this->findValueGivenColumn( iTraceID, TraceIDName.str().c_str() );
217  if ( oRowIndex == -1 )
218  {
219  //std::cerr << "The trace " << TraceID << "has not been found in TW";
220  std::cerr << "Debug: In " << __FILE__ << ", line " << __LINE__;
221  std::cerr << std::endl;
222  return oRowIndex;
223  }
224  return oRowIndex;
225 }
226 
227 //--------------------------------------------------------------------------
228 
229 //--------------------------------------------------------------------------
231 {
233  int ColumnIndex = findColumnName(iColumnName);
234 
235  QList< QString > Values;
236  for ( int i = 0; i < Selection.size(); i++ )
237  {
238  int TopRowSelected = Selection[i].topRow();
239  int BottomRowSelected = Selection[i].bottomRow();
240 
241  for ( int j = TopRowSelected; j < BottomRowSelected + 1; j++ )
242  {
243  Values.append( this->item(j, ColumnIndex)->text() );
244  }
245  }
246  return Values;
247 }
248 
249 //--------------------------------------------------------------------------
250 
251 //--------------------------------------------------------------------------
253  const std::list< std::pair< std::string, std::string > >& iColumnNamesAndToolTip)
254 {
255  size_t numberCol = iColumnNamesAndToolTip.size();
256 
257  this->setColumnCount( static_cast< int >( numberCol ) );
258 
259  int i = 0;
260 
261  for ( std::list< std::pair< std::string, std::string > >::const_iterator
262  iter = iColumnNamesAndToolTip.begin();
263  iter != iColumnNamesAndToolTip.end();
264  ++iter, ++i )
265  {
266  QTableWidgetItem *HeaderCol = new QTableWidgetItem;
267  std::string NameHeader, ToolTip;
268  NameHeader = iter->first;
269  ToolTip = iter->second;
270 
271  HeaderCol->setText( NameHeader.c_str() );
272  if ( ToolTip != "None" )
273  {
274  HeaderCol->setToolTip( ToolTip.c_str() );
275  }
276 
277  QFont serifFont("Arial", 10, QFont::Bold);
278  HeaderCol->setFont(serifFont);
279  this->setHorizontalHeaderItem(i, HeaderCol);
280  }
281 
283  //Need to disabled the Sorting while printing the values from the database in
284  // the table widget as the sorting is making trouble
285  this->setSortingEnabled(false);
286  this->horizontalHeader()->setMovable(true);
287 
289  SIGNAL( sortIndicatorChanged(int, Qt::SortOrder) ),
290  this, SLOT( sortItems(int, Qt::SortOrder) ) );
291 
292  // QSettings settings( "MegasonLab", "Gofigure2" );
293  QSettings settings;
294  QByteArray stateTableWidget = settings.value("StateTableWidget").toByteArray();
295  //QTabTableName->horizontalHeader()->restoreState(stateTableWidget);
296 }
297 
298 //--------------------------------------------------------------------------
299 
300 //--------------------------------------------------------------------------
301 void QGoTableWidget
302 ::DisplayInitialContent(const TWContainerType & iTWRowContainer,
303  const std::vector< int > & iIndexColorTraceRowContainer,
304  const std::vector< int > & iIndexColorCollectionRowContainer,
305  const std::string & iTraceName,
306  const std::string & iCollectionName,
307  const std::list< std::pair< std::string, std::string > > & iColumnNamesAndToolTip,
308  Qt::CheckState iState,
309  int iIndexShowColumn)
310 {
311  this->DisplayColumnNames(iColumnNamesAndToolTip);
312  this->InsertNewRows(iTWRowContainer,
313  iIndexColorTraceRowContainer,
314  iIndexColorCollectionRowContainer,
315  iTraceName, iCollectionName,
316  iState);
317 }
318 
319 //--------------------------------------------------------------------------
320 
321 //--------------------------------------------------------------------------
322 void QGoTableWidget::SetSelectedColumn(unsigned int iIndexRow)
323 {
324  int indexCol = findColumnName("");
325 
326  QTableWidgetItem *Checkbox = new QTableWidgetItem;
327  //Checkbox->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled |
328  // Qt::ItemIsSelectable );
329  Checkbox->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
330  // Checkbox->setFlags( Qt::ItemIsUserCheckable);
331  QColor WhiteColor(Qt::white);
332  Checkbox->setTextColor(WhiteColor);
333  this->setCheckedUncheckedStateCheckBox(Checkbox, Qt::Unchecked, false);
334  this->setItem(iIndexRow, indexCol, Checkbox);
335 }
336 
337 //--------------------------------------------------------------------------
338 
339 //--------------------------------------------------------------------------
340 void QGoTableWidget::SetVisibleColumn(unsigned int iIndexRow,
341  Qt::CheckState iState)
342 {
343  int indexCol = findColumnName("Show");
344 
345  QTableWidgetItem *Checkbox = new QTableWidgetItem;
346  //Checkbox->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable |
347  // Qt::ItemIsUserCheckable);
348  Checkbox->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
349  QColor WhiteColor(Qt::white);
350  Checkbox->setTextColor(WhiteColor);
351  this->setItem(iIndexRow, indexCol, Checkbox);
352  this->setVisibleStateCheckBox(Checkbox, iState, false);
353 }
354 
355 //--------------------------------------------------------------------------
356 
357 //--------------------------------------------------------------------------
359  unsigned int iIndexTWRowContainer,
360  const std::vector< int > & iIndexColorRowContainer,
361  const std::string & iNameGroupColor,
362  unsigned int iIndexRowTW)
363 {
364  //for the collection color:
365  std::string ColumnNameID = iNameGroupColor;
366  ColumnNameID += "ID";
367  int indexGroupIDInTableWidget = findColumnName( ColumnNameID.c_str() );
368  QColor Color;
369  QColor TextColor;
370  int Alpha = atoi( iTWRowContainer[iIndexColorRowContainer[3]].second[iIndexTWRowContainer].c_str() );
371  if ( iTWRowContainer[indexGroupIDInTableWidget].second[iIndexTWRowContainer] == "0" || Alpha == 0 )
372  {
373  Color.setRgb(255, 255, 255, 255);
374  int rgb = 255 - ( 255 * 3 ) / 3;
375  TextColor.setRgb(rgb, rgb, rgb, 255);
376  }
377  else
378  {
379  int Red = atoi( iTWRowContainer[iIndexColorRowContainer[0]].second[iIndexTWRowContainer].c_str() );
380  int Green = atoi( iTWRowContainer[iIndexColorRowContainer[1]].second[iIndexTWRowContainer].c_str() );
381  int Blue = atoi( iTWRowContainer[iIndexColorRowContainer[2]].second[iIndexTWRowContainer].c_str() );
382  Color.setRgb(Red, Green, Blue, Alpha);
383  int rgb = 255 - ( Red + Green + Blue ) / 3;
384  TextColor.setRgb(rgb, rgb, rgb, 255);
385  }
386 
387  this->item(iIndexRowTW, indexGroupIDInTableWidget)->setBackgroundColor(Color);
388  this->item(iIndexRowTW, indexGroupIDInTableWidget)->setTextColor(TextColor);
389 }
390 
391 //--------------------------------------------------------------------------
392 
393 //--------------------------------------------------------------------------
395  unsigned int iIndexTWRowContainer,
396  int iIndexTWRow,
397  const std::vector< int > & iIndexColorTraceRowContainer,
398  const std::vector< int > & iIndexColorCollectionRowContainer,
399  const std::string & iTraceName,
400  const std::string & iCollectionName)
401 {
402  if ( iIndexTWRow != -1 )
403  {
404  QTableWidgetItem *t_item = NULL;
405  unsigned int NumberOfRows = iTWRowContainer.size();
406  int NumberOfColumns = this->columnCount();
407 
408  for ( unsigned int i = 0; i < NumberOfRows; i++ )
409  {
410  if ( iTWRowContainer[i].first.ColumnNameTableWidget != "None"
411  && !iTWRowContainer[i].second.empty() )
412  {
413  for ( int j = 0; j < NumberOfColumns; j++ )
414  {
415  std::string HeaderCol = this->horizontalHeaderItem(j)->text().toStdString();
416  if ( HeaderCol == iTWRowContainer[i].first.ColumnNameTableWidget )
417  {
418  std::string Value = iTWRowContainer[i].second[iIndexTWRowContainer];
419  t_item = this->item(iIndexTWRow, j);
420  if (!t_item && this->CheckValueToDisplayData(Value, HeaderCol))
421  {
422  t_item = new QTableWidgetItem;
423  t_item->setTextAlignment(Qt::AlignCenter);
424  this->setItem(iIndexTWRow, j, t_item);
425  }
426  if ( t_item )
427  {
428  if ( iTWRowContainer[i].first.TypeName == "string" )
429  {
430  t_item->setData( 0, QString::fromStdString(Value) );
431  }
432  else
433  {
434  t_item->setData( 0, QString::fromStdString(Value).toDouble() );
435  }
436  }
437  } //ENDIF
438  } //ENDFOR
439  } //ENDIF
440  } //ENDFOR
441  this->SetColorForTable( iTWRowContainer, iIndexTWRowContainer,
442  iIndexColorTraceRowContainer, iTraceName, iIndexTWRow);
443  if (iCollectionName != "None") //no collection for lineages
444  {
445  this->SetColorForTable( iTWRowContainer, iIndexTWRowContainer,
446  iIndexColorCollectionRowContainer,
447  iCollectionName, iIndexTWRow);
448  }
449  }
450 }
451 //--------------------------------------------------------------------------
452 
453 //--------------------------------------------------------------------------
454 void QGoTableWidget::InsertNewRow(const TWContainerType & iTWRowContainer,
455  unsigned int iIndexTWRowContainer,
456  const std::vector< int > & iIndexColorTraceRowContainer,
457  const std::vector< int > & iIndexColorCollectionRowContainer,
458  const std::string & iTraceName,
459  const std::string & iCollectionName,
460  Qt::CheckState iVisible)
461 {
462  int IndexNewRow = this->rowCount();
463  this->setRowCount(IndexNewRow + 1);
464  this->DisplayDataForOneRow(iTWRowContainer, iIndexTWRowContainer,
465  IndexNewRow, iIndexColorTraceRowContainer,
466  iIndexColorCollectionRowContainer,
467  iTraceName, iCollectionName);
468  this->SetSelectedColumn(IndexNewRow);
469  this->SetVisibleColumn(IndexNewRow, iVisible);
470 }
471 //--------------------------------------------------------------------------
472 
473 //--------------------------------------------------------------------------
474 void QGoTableWidget::InsertNewRows(const TWContainerType & iTWRowContainer,
475  const std::vector< int > & iIndexColorTraceRowContainer,
476  const std::vector< int > & iIndexColorCollectionRowContainer,
477  const std::string & iTraceName,
478  const std::string & iCollectionName,
479  Qt::CheckState iVisible)
480 {
481  if ( !iTWRowContainer[1].second.empty() )
482  {
483  this->setSortingEnabled(false);
484  for ( unsigned int i = 0; i < iTWRowContainer[1].second.size(); i++ )
485  {
486  this->InsertNewRow(iTWRowContainer, i, iIndexColorTraceRowContainer,
487  iIndexColorCollectionRowContainer, iTraceName, iCollectionName,
488  iVisible);
489  }
490  this->setSortingEnabled(true);
491  this->resizeColumnsToContents();
492  }
493 }
494 //--------------------------------------------------------------------------
495 
496 //--------------------------------------------------------------------------
498  const std::vector< int > & iIndexColorTraceRowContainer,
499  const std::vector< int > & iIndexColorCollectionRowContainer,
500  const std::string & TraceName,
501  const std::string & CollectionName,
502  Qt::CheckState iVisible)
503 {
504 // no sorting inside for performance issue while import
505 // this->setSortingEnabled(false);
506  if ( iTWRowContainer.size() == 0 || iTWRowContainer[1].second.size() != 1 )
507  {
508  std::cout << "The New Trace Row Container is totally empty or there is more than 1 trace in it";
509  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
510  std::cout << std::endl;
511  }
512  else
513  {
514  this->InsertNewRow(iTWRowContainer, 0,
515  iIndexColorTraceRowContainer,
516  iIndexColorCollectionRowContainer,
517  TraceName, CollectionName, iVisible);
518  }
519 // this->setSortingEnabled(true);
520 // this->resizeColumnsToContents();
521 }
522 
523 //--------------------------------------------------------------------------
524 
525 //--------------------------------------------------------------------------
526 bool QGoTableWidget::CheckValueToDisplayData(const std::string & iValue,
527  const std::string & iHeaderCol)
528 {
529  if ( iValue == "" )
530  {
531  return false;
532  }
533  if ( iValue == "-1" && iHeaderCol.compare("T.I.Channel") > 0 )
534  {
535  return false;
536  }
537  return true;
538 }
539 
540 //--------------------------------------------------------------------------
541 
542 //--------------------------------------------------------------------------
543 void QGoTableWidget::UpdateRow(const TWContainerType & iTWRowContainer,
544  const std::vector< int > & iIndexColorTraceRowContainer,
545  const std::vector< int > & iIndexColorCollectionRowContainer,
546  const std::string & iTraceName,
547  const std::string & iCollectionName,
548  int iTraceID)
549 {
550  if ( iTraceID != 0 )
551  {
552  this->setSortingEnabled(false);
553  if ( iTWRowContainer.size() == 0 || iTWRowContainer[1].second.size() != 1 )
554  {
555  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
556  std::cout << std::endl;
557  std::cout << "The Update Trace Row Container is totally empty or there is more than 1 trace in it";
558  std::cout << std::endl;
559  }
560  else
561  {
562  QString TraceNameID = QString("%1ID").arg( iTraceName.c_str() );
563  int IndexUpdateRow = this->findValueGivenColumn(iTraceID, TraceNameID);
564  if ( IndexUpdateRow != -1 )
565  {
566  this->DisplayDataForOneRow(iTWRowContainer,0,
567  IndexUpdateRow,
568  iIndexColorTraceRowContainer,
569  iIndexColorCollectionRowContainer,
570  iTraceName, iCollectionName);
571  }
572  else
573  {
574  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
575  std::cout << std::endl;
576  std::cout << "the row doesn't exist";
577  std::cout << std::endl;
578  }
579  }
580  this->setSortingEnabled(true);
581  }
582 }
583 
584 //--------------------------------------------------------------------------
585 
586 //--------------------------------------------------------------------------
587 void QGoTableWidget::DeleteCheckedRows(const std::string & iTraceNameID,
588  const std::list< unsigned int > & iTraceIDs)
589 {
590  this->setSortingEnabled(false);
591  std::list< unsigned int >::const_iterator iter = iTraceIDs.begin();
592  while ( iter != iTraceIDs.end() )
593  {
594  int RowToDelete = this->findValueGivenColumn( *iter, iTraceNameID.c_str() );
595  removeRow(RowToDelete);
596  ++iter;
597  }
598  this->setSortingEnabled(true);
599 }
600 
601 //--------------------------------------------------------------------------
602 
603 //--------------------------------------------------------------------------
605 {
606  if ( this->horizontalHeaderItem(iColumn)->text() == "" )
607  {
608  //force the checkbox to change state, even if the user didn't click
609  //directly in the checkbox but in the cell containing it:
610  if ( this->item(iRow, iColumn)->checkState() == 0 )
611  {
612  this->setCheckedUncheckedStateCheckBox(this->item(iRow, iColumn), Qt::Checked, true);
613  }
614  else
615  {
616  this->setCheckedUncheckedStateCheckBox(this->item(iRow, iColumn), Qt::Unchecked, true);
617  }
618  }
619  if ( this->horizontalHeaderItem(iColumn)->text() == "Show" )
620  {
621  if ( this->item(iRow, iColumn)->checkState() == 0 )
622  {
623  this->setVisibleStateCheckBox(this->item(iRow, iColumn), Qt::Checked, true);
624  }
625  else
626  {
627  this->setVisibleStateCheckBox(this->item(iRow, iColumn), Qt::Unchecked, true);
628  }
629  }
630 }
631 
632 //--------------------------------------------------------------------------
633 
634 //--------------------------------------------------------------------------
636 {
637  //Get only the first selected Range:
638  QList< QTableWidgetSelectionRange > SelectedRanges = this->selectedRanges();
639  //QTableWidgetSelectionRange range = SelectedRanges.first();
640  QString str;
641  for ( int k = 0; k < SelectedRanges.size(); k++ )
642  {
643  QTableWidgetSelectionRange range = SelectedRanges.at(k);
644  this->PrepareRangeToCopy(range, str);
645  str += "\n";
646  }
648 }
649 
650 //--------------------------------------------------------------------------
651 
652 //--------------------------------------------------------------------------
654 {
655  //first, copy the column names:
656  QStringList ColumnsNames = this->recordHeaderNamesOrder();
657  QString str;
658 
659  str += "IsSelected";
660  str += "\t";
661  for ( int i = 1; i < ColumnsNames.size() - 1; i++ )
662  {
663  str += ColumnsNames.at(i);
664  str += "\t";
665  }
666  str += ColumnsNames.at(ColumnsNames.size() - 1);
667  str += "\n";
668  //second, get the range for the total selection:
669  QTableWidgetSelectionRange RangeForAllTableSelected(
670  0, 0, this->rowCount() - 1, this->columnCount() - 1);
671  //third, copy the range:
672  this->PrepareRangeToCopy(RangeForAllTableSelected, str);
674 }
675 
676 //--------------------------------------------------------------------------
677 
678 //--------------------------------------------------------------------------
680  QString & istr)
681 {
682  QTableWidgetItem *t_item = NULL;
683 
684  for ( int i = 0; i < iRange.rowCount(); ++i )
685  {
686  if ( i > 0 )
687  {
688  istr += "\n";
689  }
690  for ( int j = 0; j < iRange.columnCount() - 1; ++j )
691  {
692  int k = iRange.leftColumn() + j;
693  if ( k == 0 )
694  {
695  //for the selected column:
696  if ( item(i, 0)->checkState() == 0 )
697  {
698  istr += "No \t";
699  }
700  else
701  {
702  istr += "Yes \t";
703  }
704  }
705  else
706  {
707  t_item = this->item(iRange.topRow() + i, iRange.leftColumn() + j);
708  if ( t_item )
709  {
710  istr += t_item->text();
711  }
712  else
713  {
714  istr += "*";
715  }
716  istr += "\t";
717  }
718  }
719  t_item =
720  this->item(iRange.topRow() + i, iRange.leftColumn() + iRange.columnCount() - 1);
721 
722  if ( t_item )
723  {
724  istr += t_item->text();
725  }
726  else
727  {
728  istr += "*";
729  }
730  }
731 }
732 
733 //--------------------------------------------------------------------------
734 
735 //--------------------------------------------------------------------------
737  std::string iTraceNameID, Qt::CheckState iState)
738 {
739  QStringList ListSelectedTracesID = this->ValuesForSelectedRows(
740  iTraceNameID.c_str() );
741 
742  if ( !ListSelectedTracesID.empty() )
743  {
744  for ( int i = 0; i < ListSelectedTracesID.size(); i++ )
745  {
746  this->SetCheckStateForTraceID(ListSelectedTracesID.at(i).toUInt(),
747  iTraceName, iState, false);
748  }
749  emit ModifyHighlightListTraces(ListSelectedTracesID, iState);
750  }
751  else
752  {
753  std::cout << "The list of selected Traces ID is empty";
754  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
755  std::cout << std::endl;
756  }
757 }
758 
759 //--------------------------------------------------------------------------
760 
761 //--------------------------------------------------------------------------
763  std::string iTraceNameID,
764  Qt::CheckState iState)
765 {
766  QStringList ListSelectedTracesID = this->ValuesForSelectedRows(
767  iTraceNameID.c_str() );
768 
769  if ( !ListSelectedTracesID.empty() )
770  {
771  for ( int i = 0; i < ListSelectedTracesID.size(); i++ )
772  {
773  this->SetVisibleStateForTraceID(ListSelectedTracesID.at(i).toUInt(), iTraceName,
774  iState, false);
775  }
776  emit ModifyVisibilityListTraces(ListSelectedTracesID, iState);
777  }
778  else
779  {
780  std::cout << "The list of selected Traces ID is empty";
781  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
782  std::cout << std::endl;
783  }
784 }
785 
786 //--------------------------------------------------------------------------
787 
788 //--------------------------------------------------------------------------
789 void QGoTableWidget::AddValuesForID(const std::vector< std::string > & iColumnsNames,
790  const std::vector< std::string > & iValues,
791  unsigned int iID,
792  const std::string & iColumnNameForTraceID)
793 {
794  int RowIndex = this->findValueGivenColumn( iID, iColumnNameForTraceID.c_str() );
795 
796  if ( RowIndex != -1 )
797  {
798  for ( unsigned int i = 0; i < iColumnsNames.size(); i++ )
799  {
800  int ColumnIndex = this->findColumnName( iColumnsNames.at(i).c_str() );
801  if ( ColumnIndex != -1 )
802  {
803  QTableWidgetItem *CellTable = new QTableWidgetItem;
804  CellTable->setData( 0, QString::fromStdString( iValues.at(i) ).toDouble() );
805 //QString::fromStdString( iValues.at(i)));
806  CellTable->setTextAlignment(Qt::AlignCenter);
807  this->setItem(RowIndex, ColumnIndex, CellTable);
808  }
809  }
810  }
811 }
812 
813 //--------------------------------------------------------------------------
814 
815 //--------------------------------------------------------------------------
817  unsigned int iTraceID, const std::string & iTraceName)
818 {
819  GoDBCoordinateRow CenterCoord;
820  int RowIndex = this->GetRowForTraceID(iTraceID, iTraceName);
821 
822  if ( RowIndex != -1 )
823  {
824  CenterCoord.SetField( "TCoord",
825  this->GetValueForItem("TimePoint", RowIndex) );
826  CenterCoord.SetField( "XCoord",
827  this->GetMeanValue("XMax", "XMin", RowIndex) );
828  CenterCoord.SetField( "YCoord",
829  this->GetMeanValue("YMax", "YMin", RowIndex) );
830  CenterCoord.SetField( "ZCoord",
831  this->GetMeanValue("ZMax", "ZMin", RowIndex) );
832  }
833  return CenterCoord;
834 }
835 //--------------------------------------------------------------------------
836 
837 //--------------------------------------------------------------------------
838 QString QGoTableWidget::GetValue(unsigned int iTraceID,
839  const std::string & iTraceName,
840  const std::string & iColumn)
841 {
842  int RowIndex = this->GetRowForTraceID(iTraceID, iTraceName);
843  return this->item(
844  RowIndex, this->findColumnName( iColumn.c_str() ) )->text();
845 }
846 //--------------------------------------------------------------------------
847 
848 //--------------------------------------------------------------------------
849 int QGoTableWidget::GetValueForItem(const std::string & iColumnName, int iRowIndex)
850 {
851  return
852  this->item(
853  iRowIndex, this->findColumnName( iColumnName.c_str() ) )->text().toInt();
854 }
855 
856 //--------------------------------------------------------------------------
857 
858 //--------------------------------------------------------------------------
859 std::string QGoTableWidget::GetMeanValue(const std::string & iColumnNameOne,
860  const std::string & iColumnNameTwo,
861  unsigned int iRowIndex)
862 {
863  int ValueOne = this->GetValueForItem(iColumnNameOne, iRowIndex);
864  int ValueTwo = this->GetValueForItem(iColumnNameTwo, iRowIndex);
865  int meanValue = ( ValueOne + ValueTwo ) / 2;
866 
867  return ConvertToString< int >(meanValue);
868 }
869 
870 //--------------------------------------------------------------------------
871 
872 //--------------------------------------------------------------------------
874  Qt::CheckState iState,
875  bool EmitSignal)
876 {
877  if( iItem )
878  {
879  if ( this->setCheckStateCheckBox(iItem, iState) && EmitSignal )
880  {
881  int Row = iItem->row();
882  emit CheckedRowsChanged( this->item(Row, 1)->text().toInt() );
883  }
884  }
885 }
886 
887 //--------------------------------------------------------------------------
888 
889 //--------------------------------------------------------------------------
891  Qt::CheckState iState,
892  bool EmitSignal)
893 {
894  if( iItem )
895  {
896  if ( this->setCheckStateCheckBox(iItem, iState) )
897  {
898  QIcon Icon;
899  if ( iState == Qt::Checked )
900  {
901  Icon.addPixmap(QPixmap( QString::fromUtf8(":/fig/EyeIcon.png") ),
902  QIcon::Normal, QIcon::Off);
903  }
904  else
905  {
906  Icon.addPixmap(QPixmap( QString::fromUtf8(":/fig/BlankIcon.png") ),
907  QIcon::Normal, QIcon::Off);
908  }
909  if ( EmitSignal )
910  {
911  int Row = iItem->row();
912  VisibleRowsChanged( this->item(Row, 1)->text().toInt() );
913  }
914  iItem->setIcon(Icon);
915  }
916  }
917 }
918 
919 //--------------------------------------------------------------------------
920 
921 //--------------------------------------------------------------------------
923  Qt::CheckState iState)
924 {
925  bool oModification = false;
926 
927  if( iItem )
928  {
929  if ( iState == Qt::Checked )
930  {
931  //if the row is already checked, no need to do anything:
932  if ( iItem->checkState() != 2 )
933  {
934  iItem->setCheckState(Qt::Checked);
935  iItem->setText("1");
936  oModification = true;
937  }
938  }
939  else
940  {
941  //if the row is already unchecked, no need to do anything:
942  if ( iItem->checkState() != Qt::Unchecked )
943  {
944  iItem->setCheckState(Qt::Unchecked);
945  iItem->setText("0");
946  oModification = true;
947  }
948  }
949  }
950  return oModification;
951 }
952 
953 //--------------------------------------------------------------------------
954 
955 //--------------------------------------------------------------------------
956 std::map< unsigned int, std::string >
957 QGoTableWidget::GetTraceIDAndColumnsValues(const std::string & iTraceIDName,
958  std::string & ioColumnName)
959 {
960  std::map< unsigned int, std::string > oMapValues =
961  std::map< unsigned int, std::string >();
963  if ( Ranges.size() > 1 || Ranges[0].columnCount() > 1 )
964  {
965  QMessageBox msgBox;
966  msgBox.setText(
967  tr("Please choose only one column to color code") );
968  msgBox.exec();
969  return oMapValues;
970  }
971  unsigned int ColumnIndex = Ranges[0].leftColumn();
972  ioColumnName = this->horizontalHeaderItem(ColumnIndex)->text().toStdString();
973  int NbOfRows = this->rowCount();
974  unsigned int IndexTraceID = this->findColumnName( iTraceIDName.c_str() );
975  for ( int i = 0; i < NbOfRows; i++ )
976  {
977  if ( this->item(i, ColumnIndex) )
978  {
979  std::string Text = this->item(i, ColumnIndex)->text().toStdString(); //for
980  //
981  //
982  //
983  //
984  //
985  // test
986  //
987  //
988  //
989  //
990  //
991  // purpose
992  oMapValues[this->item(i, IndexTraceID)->text().toUInt()] =
993  this->item(i, ColumnIndex)->text().toStdString();
994  }
995  else
996  {
997  oMapValues[this->item(i, IndexTraceID)->text().toUInt()] = "";
998  }
999  }
1000  return oMapValues;
1001 }
1002 
1003 //--------------------------------------------------------------------------
1004 
1005 //--------------------------------------------------------------------------
1006 void QGoTableWidget::ShowOnlyRowsForTimePoint(unsigned int iTimePoint)
1007 {
1008  int ColumnIndex = this->findColumnName("TimePoint");
1009 
1010  if ( ColumnIndex == -1 )
1011  {
1012  std::cout << "column TimePoint not found in the table widget";
1013  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
1014  std::cout << std::endl;
1015  return;
1016  }
1017  for ( int i = 0; i < this->rowCount(); i++ )
1018  {
1019  if ( this->item(i, ColumnIndex)->text().toUInt() != iTimePoint )
1020  {
1021  this->hideRow(i);
1022  }
1023  else
1024  {
1025  this->showRow(i);
1026  }
1027  }
1028 }
1029 
1030 //--------------------------------------------------------------------------
1031 
1032 //--------------------------------------------------------------------------
1034 {
1035  for ( int i = 0; i < this->rowCount(); i++ )
1036  {
1037  this->showRow(i);
1038  }
1039 }
1040 //--------------------------------------------------------------------------
1041 
1042 //--------------------------------------------------------------------------
1044 {
1045  int IndexColumnTime = this->findColumnName("TimePoint");
1046 
1047  if (IndexColumnTime != -1)
1048  {
1049  this->setSortingEnabled(false);
1050  for (int i = rowCount()-1; i>=0; --i)
1051  {
1052  if (iListTPs.contains(this->item(i, IndexColumnTime)->text() ) )
1053  {
1054  this->removeRow(i);
1055  }
1056  }
1057  this->setSortingEnabled(true);
1058  }
1059 
1060 }
1061 //--------------------------------------------------------------------------
1062 
1063 //--------------------------------------------------------------------------
1064 void
1067 {
1068  for(int i=rowCount()-1; i>=0; --i)
1069  {
1070  this->removeRow(i);
1071  }
1072 
1073  for(int i=columnCount()-1; i>=0; --i)
1074  {
1075  this->removeColumn(i);
1076  }
1077 }
1078 //--------------------------------------------------------------------------
void ShowOnlyRowsForTimePoint(unsigned int iTimePoint)
hide all rows who have a timepoint different than iTimePoint
std::string toStdString() const
void SetColorForTable(const TWContainerType &iTWRowContainer, unsigned int iIndexTWRowContainer, const std::vector< int > &iIndexColorRowContainer, const std::string &iNameGroupColor, unsigned int iIndexRowTW)
get the rgba values from the iTWRowContainer and display them in the column NameGroupColorID ...
QByteArray toByteArray() const
void setMovable(bool movable)
void DeleteRowsWithSpecificTimePoints(const QStringList &iListTPs)
void cellClicked(int row, int column)
void removeRow(int row)
bool setCheckStateCheckBox(QTableWidgetItem *iItem, Qt::CheckState iState)
change the state for the checkbox to iState and return true if the state has been changed ...
Qt::CheckState checkState() const
void ChangeCheckStateSelectedRows(std::string iTraceName, std::string iTraceNameID, Qt::CheckState iState)
uncheck/check the boxes in the check/uncheck column for the rows where at least one cell is selected ...
void resizeColumnsToContents()
void setItem(int row, int column, QTableWidgetItem *item)
void PrepareRangeToCopy(const QTableWidgetSelectionRange &iRange, QString &istr)
put the text in the cells which are part of the range in a QString and insert antislash n and antisla...
QGoTableWidget(QWidget *parent=0)
const T & at(int i) const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
void setSortingEnabled(bool enable)
GoDBTableWidgetContainer::TWContainerType TWContainerType
void setSortIndicatorShown(bool show)
void setIcon(const QIcon &icon)
void setBackgroundColor(const QColor &color)
void setRgb(int r, int g, int b, int a)
manages a map with keys matching fields of the gofiguredatabase Coordinate table and values of the ma...
void DisplayInitialContent(const TWContainerType &iTWRowContainer, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName, const std::list< std::pair< std::string, std::string > > &iColumnNames, Qt::CheckState iState, int iIndexShowColumn=0)
display the columns names and the content of iTWRowContainer in the table
void UpdateRow(const TWContainerType &iTWRowContainer, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName, int iTraceID)
Replace the data in the cells corresponding to the traceID with the new data contained in the RowCont...
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual ~QGoTableWidget()
void sortItems(int column, Qt::SortOrder order)
virtual void setData(int role, const QVariant &value)
int size() const
void CopyTable()
convert the text in the all table and the columns namse to a QString with anti slash n and anti slash...
std::map< unsigned int, std::string > GetTraceIDAndColumnsValues(const std::string &iTraceIDName, std::string &ioColumnName)
std::string GetMeanValue(const std::string &iColumnNameOne, const std::string &iColumnNameTwo, unsigned int iRowIndex)
calculate the mean value for both columns in the given row
QString fromStdString(const std::string &str)
void append(const T &value)
QString fromUtf8(const char *str, int size)
bool empty() const
QString GetValue(unsigned int iTraceID, const std::string &iTraceName, const std::string &iColumn)
QClipboard * clipboard()
int toInt(bool *ok, int base) const
void CheckedRowsChanged(int iTraceID)
bool isEmpty() const
void setText(const QString &text)
int GetValueForItem(const std::string &iColumnName, int iRowIndex)
get the value in the table for the given iRowIndex and for the given column name
QStringList recordHeaderNamesOrder()
void SetVisibleStateForListTraceIDs(const std::list< unsigned int > &iListTraceIDs, Qt::CheckState iState, const std::string &iTraceName)
update the checkboxes and icon of the visible column for the iListTraceIDs following iState ...
void ModifyHighlightListTraces(QStringList, Qt::CheckState)
int findColumnName(const QString &iColumnName)
return the column index who has a column header matching ColumnName
void addPixmap(const QPixmap &pixmap, Mode mode, State state)
void UpdateColumnsWithCheckBoxes(int iRow, int iColumn)
check if the cell clicked is from the check/uncheck column or the "Show" column,change the state of t...
void DisplayDataForOneRow(const TWContainerType &iTWRowContainer, unsigned int iIndexTWRowContainer, int iIndexTWRow, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName)
int GetRowForTraceID(unsigned int iTraceID, const std::string &iTraceName)
return the RowIndex corresponding to the TraceID
int row() const
void setCheckedUncheckedStateCheckBox(QTableWidgetItem *iItem, Qt::CheckState iState, bool EmitSignal)
change the state for the check/uncheck checkbox to iState and emit a signal if EmitSignal = true ...
GoDBCoordinateRow GetCoordinateCenterBoundingBox(unsigned int iTraceID, const std::string &iTraceName)
calculate the center of the bounding box for the only selected trace and return it as a GoDBCoordinat...
void sortItems(int iColumn, Qt::SortOrder iOrder)
sort items given one column and one sort order.
QTableWidgetItem * horizontalHeaderItem(int column) const
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
bool CheckValueToDisplayData(const std::string &iValue, const std::string &iHeaderCol)
check if the value is suitable to be displayed, if yes, return true, if not return false so the QTabl...
void setTextColor(const QColor &color)
QString text() const
void InsertNewRows(const TWContainerType &iTWRowContainer, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName, Qt::CheckState iVisible=Qt::Checked)
void SetVisibleColumn(unsigned int iIndexRow, Qt::CheckState iState=Qt::Checked)
Put checkboxes and icons in the column "Show".
int columnCount() const
void InsertOnlyOneNewRow(const TWContainerType &iTWRowContainer, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName, Qt::CheckState iVisible=Qt::Checked)
Insert a new row and fill the cells with the data contained in the RowContainer.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setFlags(QFlags< Qt::ItemFlag > flags)
QList< QTableWidgetSelectionRange > selectedRanges() const
void SetField(const std::string &key, const T &value)
convert the value into a string and assign it to the key in the map
Definition: GoDBRow.h:73
void hideRow(int row)
void setText(const QString &text)
QTableWidgetItem * item(int row, int column) const
int findValueGivenColumn(int iValue, const QString &iColumn)
return the row index where the given value is located when specifying the column name.
void ChangeVisibilityStateSelectedRows(std::string iTraceName, std::string iTraceNameID, Qt::CheckState iState)
check/uncheck the visible boxes for the rows where at least one cell is selected
void AddValuesForID(const std::vector< std::string > &iColumnsNames, const std::vector< std::string > &iValues, unsigned int iID, const std::string &iColumnNameForTraceID)
add values in the table for the corresponding traceID and column names
void DisplayColumnNames(const std::list< std::pair< std::string, std::string > > &iColumnNamesAndToolTip)
create the table widget items for the columns Header and set the corresponding tooltips for them ...
void removeColumn(int column)
int indexOf(const QRegExp &rx, int from) const
void VisibleRowsChanged(int iTraceID)
void setText(const QString &text, Mode mode)
void SetSelectedColumn(unsigned int iIndexRow)
Put checkboxes in the column "check/uncheck".
void SetCheckStateForTraceID(unsigned int iTraceID, const std::string &iTraceName, Qt::CheckState iState, bool EmitSignal=true)
set the state of the checkbox for the check/uncheck column and the TraceID row to iState ...
void showRow(int row)
void setVisibleStateCheckBox(QTableWidgetItem *iItem, Qt::CheckState iState, bool EmitSignal=true)
change the state and the icon for the "IsVisible" checkbox depending on iState and emit a signal if E...
void DeleteCheckedRows(const std::string &iTraceNameID, const std::list< unsigned int > &iTraceIDs)
delete the rows previously checked by the user
void SetVisibleStateForTraceID(unsigned int iTraceID, const std::string &iTraceName, Qt::CheckState iState, bool EmitSignal=true)
set the state and icon of the checkbox for the IsVisible column and the TraceID row to iState ...
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int rowCount() const
QHeaderView * horizontalHeader() const
void CopySelection()
convert the text in the selection to a QString with anti slash n and anti slash t and put it in the C...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QStringList ValuesForSelectedRows(const QString &iColumnName)
return a list of the values of a specific column for the rows where the user has selected at least on...
void setToolTip(const QString &toolTip)
void ModifyVisibilityListTraces(QStringList, Qt::CheckState)
void setFont(const QFont &font)
void setTextAlignment(int alignment)
uint toUInt(bool *ok, int base) const
void InsertNewRow(const TWContainerType &iTWRowContainer, unsigned int iIndexTWRowContainer, const std::vector< int > &iIndexColorTraceRowContainer, const std::vector< int > &iIndexColorCollectionRowContainer, const std::string &iTraceName, const std::string &iCollectionName, Qt::CheckState iVisible=Qt::Checked)
void setHorizontalHeaderItem(int column, QTableWidgetItem *item)
void setCheckState(Qt::CheckState state)