OrderByWidget.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 /*!
21  \file terralib/qt/widgets/query/OrderByWidget.cpp
22 
23  \brief This file has the OrderByWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/query/OrderByItem.h"
28 #include "../../../dataaccess/query/PropertyName.h"
29 #include "OrderByWidget.h"
30 #include "ui_OrderByWidgetForm.h"
31 
32 
33 // Qt
34 #include <QIcon>
35 #include <QMessageBox>
36 
38  : QWidget(parent, f),
39  m_ui(new Ui::OrderByWidgetForm)
40 {
41  m_ui->setupUi(this);
42 
43  //set order types
44  m_orderTypes.insert(std::map<std::string, int>::value_type(tr("ASC").toUtf8().data(), te::da::ASC));
45  m_orderTypes.insert(std::map<std::string, int>::value_type(tr("DESC").toUtf8().data(), te::da::DESC));
46 
47  std::map<std::string, int>::iterator it = m_orderTypes.begin();
48 
49  while(it != m_orderTypes.end())
50  {
51  m_ui->m_orderComboBox->addItem(it->first.c_str());
52 
53  ++it;
54  }
55 
56  // set icons
57  m_ui->m_addOrderPushButton->setIcon(QIcon::fromTheme("list-add"));
58  m_ui->m_removeOrderPushButton->setIcon(QIcon::fromTheme("list-remove"));
59 
60  //connects
61  connect(m_ui->m_addOrderPushButton, SIGNAL(clicked()), this, SLOT(onAddOrderPushButton()));
62  connect(m_ui->m_removeOrderPushButton, SIGNAL(clicked()), this, SLOT(onRemoveOrderPushButton()));
63 }
64 
66 
67 Ui::OrderByWidgetForm* te::qt::widgets::OrderByWidget::getForm() const
68 {
69  return m_ui.get();
70 }
71 
73 {
74  std::vector<std::pair<std::string, std::string> > vec;
75 
76  getOrderItems(vec);
77 
78  if(vec.empty())
79  return nullptr;
80 
81  te::da::OrderBy* order = new te::da::OrderBy;
82 
83  for(size_t t = 0; t < vec.size(); ++t)
84  {
85  std::map<std::string, int>::iterator it = m_orderTypes.find(vec[t].second);
86 
87  if(it != m_orderTypes.end())
88  {
89  te::da::OrderByItem* o = new te::da::OrderByItem(vec[t].first, (te::da::SortOrder)it->second);
90 
91  order->push_back(o);
92  }
93  }
94 
95  return order;
96 }
97 
98 void te::qt::widgets::OrderByWidget::setAttributeList(const std::vector<std::string>& vec)
99 {
100  m_ui->m_orderPropertyComboBox->clear();
101 
102  for(size_t t = 0; t <vec.size(); ++t)
103  {
104  m_ui->m_orderPropertyComboBox->addItem(vec[t].c_str());
105  }
106 }
107 
108 void te::qt::widgets::OrderByWidget::getOrderItems(std::vector<std::pair<std::string, std::string> >& list)
109 {
110  int row = m_ui->m_orderTableWidget->rowCount();
111 
112  //get properties for each data set
113  for(int i = 0; i < row; ++i)
114  {
115  //alias name
116  QTableWidgetItem* itemType = m_ui->m_orderTableWidget->item(i, 1);
117  std::string type = itemType->text().toUtf8().data();
118 
119  //data set name
120  QTableWidgetItem* itemName = m_ui->m_orderTableWidget->item(i, 0);
121  std::string name = itemName->text().toUtf8().data();
122 
123  list.push_back(std::pair<std::string, std::string> (name, type));
124  }
125 }
126 
128 {
129  if(m_ui->m_orderPropertyComboBox->currentText().isEmpty())
130  {
131  QMessageBox::warning(this, tr("Query Builder"), tr("Property value not defined."));
132  return;
133  }
134 
135  int newrow = m_ui->m_orderTableWidget->rowCount();
136 
137  std::string propertyValue = m_ui->m_orderPropertyComboBox->currentText().toUtf8().data();
138  std::string order = m_ui->m_orderComboBox->currentText().toUtf8().data();
139 
140 //new entry
141  m_ui->m_orderTableWidget->insertRow(newrow);
142 
143  QTableWidgetItem* itemProperty = new QTableWidgetItem(QString::fromUtf8(propertyValue.c_str()));
144  m_ui->m_orderTableWidget->setItem(newrow, 0, itemProperty);
145 
146  QTableWidgetItem* itemOrder = new QTableWidgetItem(QString::fromUtf8(order.c_str()));
147  m_ui->m_orderTableWidget->setItem(newrow, 1, itemOrder);
148 
149  m_ui->m_orderTableWidget->resizeColumnsToContents();
150 }
151 
153 {
154  int row = m_ui->m_orderTableWidget->currentRow();
155 
156  if(row >= 0)
157  m_ui->m_orderTableWidget->removeRow(row);
158 
159  m_ui->m_orderTableWidget->resizeColumnsToContents();
160 }
OrderByWidget(QWidget *parent=0, Qt::WindowFlags f=0)
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
Definition: OrderBy.h:37
Ui::OrderByWidgetForm * getForm() const
std::map< std::string, int > m_orderTypes
Definition: OrderByWidget.h:91
This file has the OrderByWidget class.
SortOrder
Sort order type: asc or desc.
std::unique_ptr< Ui::OrderByWidgetForm > m_ui
Definition: OrderByWidget.h:89
te::da::OrderBy * gerOrderBy()
void getOrderItems(std::vector< std::pair< std::string, std::string > > &list)
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
Definition: OrderByItem.h:53
void setAttributeList(const std::vector< std::string > &vec)