All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LocalImageWidget.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 /*!
21  \file terralib/qt/widgets/se/LocalImageWidget.cpp
22 
23  \brief A widget used to build an external graphic element that references a local image. e.g. a SVG file, a PNG file, etc.
24 */
25 
26 // TerraLib
27 #include "../../../se/ExternalGraphic.h"
28 #include "../../../xlink/SimpleLink.h"
29 #include "LocalImageWidget.h"
30 #include "ui_LocalImageWidgetForm.h"
31 
32 // Qt
33 #include <QFileDialog>
34 #include <QImageReader>
35 #include <QMessageBox>
36 
37 // STL
38 #include <cassert>
39 
40 te::qt::widgets::LocalImageWidget::LocalImageWidget(QWidget* parent, Qt::WindowFlags f)
41  : QWidget(parent, f),
42  m_ui(new Ui::LocalImageWidgetForm),
43  m_eg(new te::se::ExternalGraphic)
44 {
45  m_ui->setupUi(this);
46 
47  // Bulding the filter string
48  m_filter = tr("Images") + " ( ";
49  QList<QByteArray> formats = QImageReader::supportedImageFormats();
50  for(int i = 0; i < formats.size() - 1; ++i)
51  m_filter += "*." + formats[i] + " ";
52  m_filter += ")";
53 
54  // Signals & slots
55  connect(m_ui->m_browsePushButton, SIGNAL(pressed()), this, SLOT(onBrowsePushButtonPressed()));
56 }
57 
59 {
60  delete m_eg;
61 }
62 
64 {
65  assert(eg);
66 
67  delete m_eg;
68 
69  m_eg = eg->clone();
70 
71  updateUi();
72 }
73 
75 {
76  return m_eg->clone();
77 }
78 
80 {
81  // Updating path
82  const te::xl::SimpleLink* link = m_eg->getOnlineResource();
83  assert(link);
84  QString path = link->getHref().c_str();
85  m_ui->m_pathLineEdit->setText(path);
86 
87  // Loading image...
88  QImage img;
89  if(!img.load(path))
90  {
91  QMessageBox::critical(this, tr("Error"), tr("The referenced image cannot be loaded."));
92  return;
93  }
94 
95  // Bulding image details...
96  QString details = QString::number(img.width()) + " x " + QString::number(img.height()) + " pixels\n";
97  details += tr("Format: ") + QImageReader::imageFormat(path).toUpper() + "\n";
98  details += QString::number(img.depth()) + " " + tr("bits per pixel") + "\n";
99  QString alpha;
100  img.hasAlphaChannel() ? alpha = tr("Yes") : alpha = tr("No");
101  details += tr("Alpha Channel: ") + alpha;
102  m_ui->m_detailsTextEdit->setText(details);
103 
104  // Adjusting image preview...
105  if(img.width() > m_ui->m_imageLabel->size().width())
106  img = img.scaledToWidth(m_ui->m_imageLabel->size().width(), Qt::SmoothTransformation);
107  m_ui->m_imageLabel->setPixmap(QPixmap::fromImage(img));
108 }
109 
111 {
112  QString path = QFileDialog::getOpenFileName(this, tr("Select an Image File"), "", m_filter);
113  if(path.isNull())
114  return;
115 
116  // Try load image
117  QImage img;
118  if(!img.load(path))
119  {
120  QMessageBox::critical(this, tr("Error"), tr("The selected image cannot be loaded."));
121  return;
122  }
123 
124  // Updating external graphic path
126  link->setHref(path.toStdString());
127  m_eg->setOnlineResource(link);
128 
129  // Updating external graphic format
130  QString f(QImageReader::imageFormat(path));
131  m_eg->setFormat("image/" + f.toStdString()); // It's correct?!
132 
133  emit externalGraphicChanged(img.size());
134 
135  updateUi();
136 }
A widget used to build an external graphic element that references a local image. e...
The ExternalGraphic allows a reference to be made to an external graphic file with a Web URL or to in...
ExternalGraphic * clone() const
It creates a new copy of this object.
std::auto_ptr< Ui::LocalImageWidgetForm > m_ui
Widget form.
void setExternalGraphic(const te::se::ExternalGraphic *eg)
Sets an external graphic element to this widget.
LocalImageWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a local image widget which is a child of parent, with widget flags set to f...
QString m_filter
String to filter the supported image formats by Qt.
te::se::ExternalGraphic * getExternalGraphic() const
Gets the configured external graphic element.
void updateUi()
Updates the widget form based on internal external graphic element.