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