Engauge Digitizer  2
GeometryModel.cpp
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 
19 GeometryModel::~GeometryModel()
20 {
21 }
22 
23 QVariant GeometryModel::data(const QModelIndex &index, int role) const
24 {
25 // LOG4CPP_DEBUG_S ((*mainCat)) << "GeometryModel::data"
26 // << " rowHighlighted=" << m_rowToBeHighlighted
27 // << " index=(row=" << index.row() << ",col=" << index.column() << ",role=" << role << ")="
28 // << " rows=" << rowCount()
29 // << " cols=" << columnCount();
30 
31  if ((role == Qt::BackgroundRole) &&
32  !m_pointIdentifier.isEmpty () &&
33  (index.row () == m_rowToBeHighlighted)) {
34 
35  // This row is to be highlighted
36  return QVariant (QColor (230, 230, 230));
37  }
38 
39  // Standard behavior
40  return QStandardItemModel::data (index, role);
41 }
42 
43 int GeometryModel::rowToBeHighlighted () const
44 {
45  LOG4CPP_INFO_S ((*mainCat)) << "GeometryModel::rowToBeHighlighted"
46  << " rows=" << rowCount()
47  << " cols=" << columnCount();
48 
49  for (int row = 0; row < rowCount(); row++) {
50 
51  // Look at the point identifier in the hidden column
52  QModelIndex indexPointIdentifier = index (row,
54  QVariant var = QStandardItemModel::data (indexPointIdentifier, Qt::DisplayRole);
55  if (var.isValid()) {
56  QString pointIdentifierGot = var.toString();
57  if (pointIdentifierGot == m_pointIdentifier) {
58 
59  // Found it
60  return row;
61  }
62  }
63  }
64 
65  // Fail
66  return NO_HIGHLIGHTED_ROW;
67 }
68 
69 void GeometryModel::setCurrentPointIdentifier (const QString &pointIdentifier)
70 {
71  LOG4CPP_INFO_S ((*mainCat)) << "GeometryModel::setCurrentPointIdentifier"
72  << " rows=" << rowCount()
73  << " cols=" << columnCount()
74  << " identifier=" << pointIdentifier.toLatin1().data();
75 
76  m_pointIdentifier = pointIdentifier;
77 
78  int rowTransitioned;
79  if (!m_pointIdentifier.isEmpty ()) {
80 
81  // Get new row. It will transition from unhighlighted to highlighted
82  m_rowToBeHighlighted = rowToBeHighlighted();
83  rowTransitioned = m_rowToBeHighlighted;
84 
85  } else {
86 
87  // Old row will transition from highlighted to unhighlighted
88  rowTransitioned = m_rowToBeHighlighted;
89  m_rowToBeHighlighted = NO_HIGHLIGHTED_ROW;
90 
91  }
92 
93  QModelIndex indexTopLeft = createIndex (rowTransitioned, 0);
94  QModelIndex indexBottomRight = createIndex (rowTransitioned, columnCount() - 1);
95 
96  QVector<int> roles;
97  roles << Qt::BackgroundRole;
98 
99  emit dataChanged (indexTopLeft,
100  indexBottomRight,
101  roles);
102 }
void setCurrentPointIdentifier(const QString &pointIdentifier)
Set the point identifier to be highlighted. Value is empty for no highlighting.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Override for special processing.
static int columnBodyPointIdentifiers()
Hidden column that has the point identifiers.
GeometryModel()
Single constructor.