OpenWalnut  1.5.0dev
WQtDataModuleInput.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <string>
26 #include <vector>
27 
28 #include <QAction>
29 #include <QFileDialog>
30 
31 #include "../WQtGui.h"
32 #include "../WIconManager.h"
33 #include "../controlPanel/WQtPropertyGroupWidget.h"
34 
35 #include "core/kernel/WDataModuleInputFilterFile.h"
36 #include "core/kernel/WDataModuleInputFile.h"
37 
38 #include "WQtDataModuleInput.h"
39 
41  QWidget( parent ),
42  m_module( module )
43 {
44  setContentsMargins( QMargins( 0, 0, 0, 0 ) );
45  m_layout = new QVBoxLayout();
46  m_layout->setMargin( 0 );
47  m_layout->setContentsMargins( QMargins( 0, 0, 0, 0 ) );
48  setLayout( m_layout );
49 
50  m_mainLayout = new QHBoxLayout();
51  m_mainLayout->setMargin( 0 );
52  m_mainLayout->setContentsMargins( QMargins( 0, 0, 0, 0 ) );
53 
54  m_mainWidget = new QWidget();
55  m_mainWidget->setLayout( m_mainLayout );
56 
57  // create the clickable toolbutton to open the file
58  m_changeButton = new QToolButton();
59  m_reloadButton = new QToolButton();
60  m_clearButton = new QToolButton();
61 
62  m_label = new WScaleLabel();
63  m_label->setText( "UNSET" );
64 
65  m_changeButton->setAutoRaise( true );
66  m_reloadButton->setAutoRaise( true );
67  m_clearButton->setAutoRaise( true );
68  m_reloadButton->setToolTip( "Force a reload of the data." );
69  m_changeButton->setToolTip( "Change the source." );
70  m_clearButton->setToolTip( "Clear the source and set no source." );
71 
72  m_mainLayout->addWidget( m_reloadButton );
73  m_mainLayout->addWidget( m_clearButton );
74  m_mainLayout->addWidget( m_changeButton );
75  m_mainLayout->addWidget( m_label );
76 
77  // design the actions for both buttons
79  QAction* changeAction = new QAction( iconMgr->getIcon( "load" ), "Change Source", this );
80  m_changeButton->setDefaultAction( changeAction );
81  QAction* reloadAction = new QAction( iconMgr->getIcon( "reload" ), "Reload Source", this );
82  m_reloadButton->setDefaultAction( reloadAction );
83  QAction* clearAction = new QAction( iconMgr->getIcon( "clear" ), "Clear Source", this );
84  m_clearButton->setDefaultAction( clearAction );
85 
87  m_layout->addWidget( m_container );
88 
89  // get notified when the input changes:
90  m_inputChangeConnection = module->getInputChangedCondition()->subscribeSignal( boost::bind( &WQtDataModuleInput::onInputChange, this ) );
92 
93  // connect the actions
94  connect( changeAction, SIGNAL( triggered( bool ) ), this, SLOT( onChange() ) );
95  connect( reloadAction, SIGNAL( triggered( bool ) ), this, SLOT( onReload() ) );
96  connect( clearAction, SIGNAL( triggered( bool ) ), this, SLOT( onClear() ) );
97 }
98 
100 {
101  m_inputChangeConnection.disconnect();
102 }
103 
105 {
107 }
108 
110 {
111  if( m_module->getInput() )
112  {
113  m_label->setText( QString::fromStdString( m_module->getInput()->asString() ) );
114  m_label->setToolTip( QString::fromStdString( m_module->getInput()->getExtendedInfo() ) );
115  }
116  else
117  {
118  m_label->setText( "UNSET" );
119  }
120 }
121 
123 {
124  QString result;
125  QString all;
126 
127  std::vector< WDataModuleInputFilter::ConstSPtr > filters = m_module->getInputFilter();
128  for( std::vector< WDataModuleInputFilter::ConstSPtr >::const_iterator filterIter = filters.begin(); filterIter != filters.end();
129  ++filterIter )
130  {
131  WDataModuleInputFilterFile::ConstSPtr ff = std::dynamic_pointer_cast< const WDataModuleInputFilterFile >( *filterIter );
132  if( ff )
133  {
134  QString description = QString::fromStdString( ff->getDescription() );
135  QString extension = QString::fromStdString( ff->getExtension() );
136  all += QString( " *." ) + extension;
137  result += description + QString( "(*." ) + extension + QString( ");;" );
138  }
139  }
140 
141  result = QString( "Known file types (" ) + all + QString( ");;" ) + result;
142  result += QString( "Any files (*)" );
143 
144  // any current path?
145  WDataModuleInputFile::SPtr inFile = m_module->getInputAs< WDataModuleInputFile >();
146  QString defaultPath = "";
147  if( inFile )
148  {
149  boost::filesystem::path p = inFile->getFilename();
150  defaultPath = QString::fromStdString( p.parent_path().string() );
151  }
152  else
153  {
154  defaultPath = WQtGui::getSettings().value( "LastOpenPath", "" ).toString();
155  }
156 
157  QString filename = QFileDialog::getOpenFileName( this, "Open Data", defaultPath, result );
158  if( filename == "" )
159  {
160  return;
161  }
162 
163  // apply
164  boost::filesystem::path p( filename.toStdString() );
165  WQtGui::getSettings().setValue( "LastOpenPath", QString::fromStdString( p.parent_path().string() ) );
166 
167  m_module->setInput( WDataModuleInputFile::SPtr( new WDataModuleInputFile( filename.toStdString() ) ) );
168 }
169 
171 {
172  m_module->reload();
173 }
174 
176 {
177  m_module->setInput( WDataModuleInput::SPtr() );
178 }
179 
Implements a file based input for the WDataModule.
boost::filesystem::path getFilename() const
Get the filename to load.
std::shared_ptr< WDataModuleInputFile > SPtr
Convenience typedef for a std::shared_ptr< WDataModuleInputFile >.
std::shared_ptr< const WDataModuleInputFilterFile > ConstSPtr
Convenience typedef for a std::shared_ptr< const WDataModuleInputFilterFile >.
std::shared_ptr< WDataModuleInput > SPtr
Convenience typedef for a std::shared_ptr< WDataModuleInput >.
std::shared_ptr< WDataModule > SPtr
Convenience typedef for a std::shared_ptr< WDataModule >.
Definition: WDataModule.h:52
Manages icon access.
Definition: WIconManager.h:41
QIcon getIcon(const std::string name)
Searches icons in the internal map and all modules for the given icon name.
WQtDataModuleInput(WDataModule::SPtr module, QWidget *parent=NULL)
Constructor.
void onChange()
Called when the user presses the change button.
virtual ~WQtDataModuleInput()
Destructor.
QVBoxLayout * m_layout
Layout.
WScaleLabel * m_label
The label.
boost::signals2::connection m_inputChangeConnection
Stay informed about changes in the input.
void onInputChangeGUI()
Called by input change condition in GUI thread.
QToolButton * m_reloadButton
The reload button.
void onReload()
Triggered for reload.
QWidget * m_mainWidget
This is the main widget containing the reload button, label and the change button it is nested in the...
void onClear()
Clear the source.
QToolButton * m_changeButton
The button to open the input dialog.
QHBoxLayout * m_mainLayout
Layout.
WDataModule::SPtr m_module
The input to manage.
QWidget * m_container
Container for all.
void onInputChange()
Called by the input change condition.
QToolButton * m_clearButton
The clear button.
static QSettings & getSettings()
Returns the settings object.
Definition: WQtGui.cpp:394
static WIconManager * getIconManager()
Get the icon manager of this gui instance.
Definition: WQtGui.cpp:93
static void execInGUIThreadAsync(boost::function< void(void) > functor, WCondition::SPtr notify=WCondition::SPtr())
Call a given function from within the GUI thread.
Definition: WQtGui.cpp:421
static QWidget * createPropertyGroupBox(QWidget *widget, bool asScrollArea=false, QWidget *parent=NULL, const QString &title="", int nestingLevel=0)
This function creates the fancy box around your specified group widget.
Special Label that can shrink and expand in a layout.
Definition: WScaleLabel.h:37
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93