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) 2011-2012 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  pkey += dset->getAsString(cols[i]) + ";";
54 
55  return (data.find(pkey) != data.end());
56 }
57 
58 std::string GetOidAsString(const te::da::ObjectId* oid)
59 {
60  boost::ptr_vector<te::dt::AbstractData> data = oid->getValue();
61  boost::ptr_vector<te::dt::AbstractData>::iterator it;
62 
63  std::string pkey;
64 
65  for(it=data.begin(); it!=data.end(); ++it)
66  pkey += it->toString() + ";";
67 
68  return pkey;
69 }
70 
71 std::set<std::string> GetOidsAsString(const te::da::ObjectIdSet* objs)
72 {
73  std::set<std::string> res;
74 
75  if(objs != 0)
76  {
77  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator it;
78 
79  for(it=objs->begin(); it!=objs->end(); ++it)
80  res.insert(GetOidAsString(*it));
81  }
82 
83  return res;
84 }
85 
87 QItemDelegate(parent),
88  m_objs(0),
89  m_dset(0),
90  m_promoter(0)
91 {
92 }
93 
95 {
96  delete m_objs;
97 }
98 
99 void te::qt::widgets::HighlightDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
100 {
101  if(!index.isValid())
102  return;
103 
104  QStyleOptionViewItem opt = option;
105 
106  if(m_objs != 0)
107  if(ToHighlight(m_dset, m_objs->getPropertyPos(), m_oids, (int)m_promoter->getLogicalRow(index.row())))
108  {
109  opt.showDecorationSelected = true;
110  opt.state |= QStyle::State_Selected;
111  opt.palette.setColor(QPalette::Highlight, m_color);
112  opt.palette.setColor(QPalette::HighlightedText, Qt::black);
113  }
114 
115  QItemDelegate::paint(painter, opt, index);
116 }
117 
118 
120 {
121  m_color = c;
122 }
123 
125 {
126  return m_color;
127 }
128 
130 {
131  m_dset = dset;
132 }
133 
135 {
136  if(m_objs != 0)
137  delete m_objs;
138 
139  if(objs == 0)
140  {
141  m_objs = 0;
142  return;
143  }
144 
145  m_objs = objs->clone();
146 
147  m_oids = GetOidsAsString(objs);
148 }
149 
151 {
152  return m_objs;
153 }
154 
156 {
157  m_promoter = promoter;
158 }
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:397
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:53
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)