All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HighlightDelegate.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 #include "HighlightDelegate.h"
21 #include "Promoter.h"
22 
23 // TerraLib
24 #include "../../../dataaccess/dataset/ObjectIdSet.h"
25 #include "../../../dataaccess/dataset/ObjectId.h"
26 #include "../../../dataaccess/dataset/DataSet.h"
27 #include "../../../dataaccess/utils/Utils.h"
28 
29 bool ToHighlight(te::da::DataSet* dset, const te::da::ObjectIdSet* objs, const int& row)
30 {
31  if(objs == 0)
32  return false;
33 
34  dset->move(row);
35 
36  std::vector<std::string> pNames = objs->getPropertyNames();
37  te::da::ObjectId* oId = te::da::GenerateOID(dset, pNames);
38 
39  bool res = objs->contains(oId);
40 
41  delete oId;
42 
43  return res;
44 }
45 
46 bool ToHighlight(te::da::DataSet* dset, const std::vector<size_t>& cols, const std::set<std::string>& data, const int& row)
47 {
48  dset->move(row);
49 
50  std::string pkey;
51 
52  for(size_t i=0; i<cols.size(); i++)
53  {
54  if(dset->isNull(cols[i]) == false)
55  pkey += dset->getAsString(cols[i]) + ";";
56  }
57 
58  return (data.find(pkey) != data.end());
59 }
60 
61 std::string GetOidAsString(const te::da::ObjectId* oid)
62 {
63  boost::ptr_vector<te::dt::AbstractData> data = oid->getValue();
64  boost::ptr_vector<te::dt::AbstractData>::iterator it;
65 
66  std::string pkey;
67 
68  for(it=data.begin(); it!=data.end(); ++it)
69  pkey += it->toString() + ";";
70 
71  return pkey;
72 }
73 
74 std::set<std::string> GetOidsAsString(const te::da::ObjectIdSet* objs)
75 {
76  std::set<std::string> res;
77 
78  if(objs != 0)
79  {
80  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
81 
82  for(it=objs->begin(); it!=objs->end(); ++it)
83  res.insert(GetOidAsString(*it));
84  }
85 
86  return res;
87 }
88 
90 QItemDelegate(parent),
91  m_objs(0),
92  m_dset(0),
93  m_promoter(0)
94 {
95 }
96 
98 {
99  delete m_objs;
100 }
101 
102 void te::qt::widgets::HighlightDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
103 {
104  if(!index.isValid())
105  return;
106 
107  QStyleOptionViewItem opt = option;
108 
109  if(m_objs != 0)
110  if(ToHighlight(m_dset, m_objs->getPropertyPos(), m_oids, (int)m_promoter->getLogicalRow(index.row())))
111  {
112  opt.showDecorationSelected = true;
113  opt.state |= QStyle::State_Selected;
114  opt.palette.setColor(QPalette::Highlight, m_color);
115  opt.palette.setColor(QPalette::HighlightedText, Qt::black);
116  }
117 
118  QItemDelegate::paint(painter, opt, index);
119 }
120 
121 
123 {
124  m_color = c;
125 }
126 
128 {
129  return m_color;
130 }
131 
133 {
134  m_dset = dset;
135 }
136 
138 {
139  if(m_objs != 0)
140  delete m_objs;
141 
142  if(objs == 0)
143  {
144  m_objs = 0;
145  return;
146  }
147 
148  m_objs = objs->clone();
149 
150  m_oids = GetOidsAsString(objs);
151 }
152 
154 {
155  return m_objs;
156 }
157 
159 {
160  m_promoter = promoter;
161 }
virtual void setColor(const QColor &c)
Update the color group.
void setPromoter(Promoter *promoter)
Sets the promoter being used.
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:446
Defines an mechanism for logical ordering of rows.
std::set< std::string > GetOidsAsString(const te::da::ObjectIdSet *objs)
const boost::ptr_vector< te::dt::AbstractData > & getValue() const
It gets the properties values used to uniquely identify a data set element.
Definition: ObjectId.cpp:46
HighlightDelegate(QObject *parent=0)
Constructor.
virtual bool move(std::size_t i)=0
It moves the dataset internal pointer to a given position.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
virtual ~HighlightDelegate()
Destructor.
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
A class used for logical ordering of rows.
Definition: Promoter.h:69
QColor getColor()
Returns the color.
virtual void setObjectIdSet(const te::da::ObjectIdSet *objs)
Sets the object id set. It WILL NOT TAKE the ownership of the objs.
bool ToHighlight(te::da::DataSet *dset, const te::da::ObjectIdSet *objs, const int &row)
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
virtual void setDataSet(te::da::DataSet *dset)
Sets the current data set being used. This method DOES NOT take the ownership of the dset...
bool contains(ObjectId *oid) const
It returns if the object id set contains the given oid.
A delegate for highlight the selected object ids.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual const te::da::ObjectIdSet * getSelected() const
Returns the identifiers of the rows highlighted.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
ObjectIdSet * clone() const
std::string GetOidAsString(const te::da::ObjectId *oid)