otbWrapperQtWidgetInputFilenameParameter.cxx 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21
#include "otbWrapperQtWidgetInputFilenameParameter.h"
22

23 24
#include <otbQtAdapters.h>

25 26
namespace otb
{
27

28 29 30
namespace Wrapper
{

31
QtWidgetInputFilenameParameter::QtWidgetInputFilenameParameter(InputFilenameParameter* param, QtWidgetModel* m)
32
: QtWidgetParameterBase(param, m),
33
  m_FilenameParam(param),
34 35 36
  m_HLayout( ITK_NULLPTR ),
  m_Input( ITK_NULLPTR ),
  m_Button( ITK_NULLPTR )
37 38 39
{
}

40
QtWidgetInputFilenameParameter::~QtWidgetInputFilenameParameter()
41 42 43
{
}

44
void QtWidgetInputFilenameParameter::DoUpdateGUI()
45
{
46
  // Update the lineEdit
47 48 49 50 51 52
  QString text(
    QFile::decodeName(
      m_FilenameParam->GetValue().c_str()
    )
  );

53 54
  if (text != m_Input->text())
    m_Input->setText(text);
55 56
}

57
void QtWidgetInputFilenameParameter::DoCreateWidget()
58 59 60 61 62 63
{
  // Set up input text edit
  m_HLayout = new QHBoxLayout;
  m_HLayout->setSpacing(0);
  m_HLayout->setContentsMargins(0, 0, 0, 0);
  m_Input = new QLineEdit;
64 65 66
  m_Input->setToolTip(
    QString::fromStdString( m_FilenameParam->GetDescription() )
  );
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  connect( m_Input, SIGNAL(textChanged(const QString&)), this, SLOT(SetFileName(const QString&)) );
  connect( m_Input, SIGNAL(textChanged(const QString&)), GetModel(), SLOT(NotifyUpdate()) );

  m_HLayout->addWidget(m_Input);

  // Set up input text edit
  m_Button = new QPushButton;
  m_Button->setText("...");
  m_Button->setToolTip("Select file...");
  m_Button->setMaximumWidth(m_Button->width());
  connect( m_Button, SIGNAL(clicked()), this, SLOT(SelectFile()) );
  m_HLayout->addWidget(m_Button);

  this->setLayout(m_HLayout);
}

83

84 85 86 87
void
QtWidgetInputFilenameParameter
::SelectFile()
{
88 89
  assert( m_Input!=NULL );

90 91 92 93 94 95 96 97 98
  QString filename(
    GetOpenFileName(
      this,
      QString(),
      m_Input->text(),
      tr( "All files (*)" ),
      NULL,
      QFileDialog::ReadOnly )
  );
99

100 101
  if( filename.isEmpty() )
    return;
102

103
  SetFileName( filename );
104

105
  m_Input->setText( filename  );
106 107
}

108

109
void QtWidgetInputFilenameParameter::SetFileName(const QString& value)
110 111
{
  // save value
112 113 114
  m_FilenameParam->SetValue(
    QFile::encodeName( value ).constData()
  );
115 116

  // notify of value change
117
  QString key( m_FilenameParam->GetKey() );
118 119 120 121 122
  emit ParameterChanged(key);
}

}
}