Engauge Digitizer  2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GeometryModel.cpp
Go to the documentation of this file.
1 /******************************************************************************************************
2  * (C) 2016 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "GeometryModel.h"
8 #include "GeometryWindow.h"
9 #include "Logger.h"
10 #include <QTableView>
11 
12 const int NO_HIGHLIGHTED_ROW = -1;
13 
15  m_rowToBeHighlighted (NO_HIGHLIGHTED_ROW)
16 {
17 }
18 
20 {
21 }
22 
23 QVariant GeometryModel::data(const QModelIndex &index, int role) const
24 {
25  const int HEADER_OFFSET = 1;
26  const int NUM_LEGEND_ROWS_UNSPANNED = 2; // Match with GeometryWindow::NUM_LEGEND_ROWS_UNSPANNED
27 
28 // LOG4CPP_DEBUG_S ((*mainCat)) << "GeometryModel::data"
29 // << " rowHighlighted=" << m_rowToBeHighlighted
30 // << " index=(row=" << index.row() << ",col=" << index.column() << ",role=" << role << ")="
31 // << " rows=" << rowCount()
32 // << " cols=" << columnCount();
33 
34  if ((role == Qt::BackgroundRole) &&
35  !m_pointIdentifier.isEmpty () &&
36  (index.row () == m_rowToBeHighlighted)) {
37 
38  // This row is to be highlighted with gray
39  return QVariant (QColor (230, 230, 230));
40  }
41 
42  bool ambiguousSegment = ((role == Qt::BackgroundRole) &&
43  (m_ambiguousRows.contains (index.row () - HEADER_OFFSET)));
44  bool ambiguousFootnote = ((role == Qt::BackgroundRole) &&
45  (m_ambiguousRows.size () > 0) &&
46  (index.row () >= rowCount () - NUM_LEGEND_ROWS_UNSPANNED));
47  if (ambiguousSegment || ambiguousFootnote) {
48 
49  // This row is to be highlighted with light red. Note that gray color preempts this behavior
50  return QVariant (QColor (255, 0, 0, 50));
51  }
52 
53  // Standard behavior
54  return QStandardItemModel::data (index, role);
55 }
56 
57 int GeometryModel::rowToBeHighlighted () const
58 {
59  LOG4CPP_INFO_S ((*mainCat)) << "GeometryModel::rowToBeHighlighted"
60  << " rows=" << rowCount()
61  << " cols=" << columnCount();
62 
63  for (int row = 0; row < rowCount(); row++) {
64 
65  // Look at the point identifier in the hidden column
66  QModelIndex indexPointIdentifier = index (row,
68  QVariant var = QStandardItemModel::data (indexPointIdentifier, Qt::DisplayRole);
69  if (var.isValid()) {
70  QString pointIdentifierGot = var.toString();
71  if (pointIdentifierGot == m_pointIdentifier) {
72 
73  // Found it
74  return row;
75  }
76  }
77  }
78 
79  // Fail
80  return NO_HIGHLIGHTED_ROW;
81 }
82 
83 void GeometryModel::setCurrentPointIdentifier (const QString &pointIdentifier)
84 {
85  LOG4CPP_INFO_S ((*mainCat)) << "GeometryModel::setCurrentPointIdentifier"
86  << " rows=" << rowCount()
87  << " cols=" << columnCount()
88  << " identifier=" << pointIdentifier.toLatin1().data();
89 
90  m_pointIdentifier = pointIdentifier;
91 
92  int rowTransitioned;
93  if (!m_pointIdentifier.isEmpty ()) {
94 
95  // Get new row. It will transition from unhighlighted to highlighted
96  m_rowToBeHighlighted = rowToBeHighlighted();
97  rowTransitioned = m_rowToBeHighlighted;
98 
99  } else {
100 
101  // Old row will transition from highlighted to unhighlighted
102  rowTransitioned = m_rowToBeHighlighted;
103  m_rowToBeHighlighted = NO_HIGHLIGHTED_ROW;
104 
105  }
106 
107  QModelIndex indexTopLeft = createIndex (rowTransitioned, 0);
108  QModelIndex indexBottomRight = createIndex (rowTransitioned, columnCount() - 1);
109 
110  QVector<int> roles;
111  roles << Qt::BackgroundRole;
112 
113  emit dataChanged (indexTopLeft,
114  indexBottomRight,
115  roles);
116 }
117 
118 void GeometryModel::setPotentialExportAmbiguity (const QVector<bool> &isPotentialExportAmbiguity)
119 {
120  // Save row numbers with ambiguities
121  m_ambiguousRows.clear ();
122  for (int i = 0; i < isPotentialExportAmbiguity.size (); i++) {
123  if (isPotentialExportAmbiguity.at (i)) {
124  m_ambiguousRows [i] = true;
125  }
126  }
127 }
void setCurrentPointIdentifier(const QString &pointIdentifier)
Set the point identifier to be highlighted. Value is empty for no highlighting.
virtual ~GeometryModel()
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Override for special processing.
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
const int NO_HIGHLIGHTED_ROW
static int columnBodyPointIdentifiers()
Hidden column that has the point identifiers.
log4cpp::Category * mainCat
Definition: Logger.cpp:14
void setPotentialExportAmbiguity(const QVector< bool > &isPotentialExportAmbiguity)
Remember which rows could have ambiguities during export - these will be highlighted.
GeometryModel()
Single constructor.