OpenWalnut  1.5.0dev
WPropertyFilenameWidget.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 <cmath>
26 #include <string>
27 
28 #include <QFileDialog>
29 
30 #include "core/common/WLogger.h"
31 #include "core/common/WPropertyVariable.h"
32 #include "core/common/constraints/WPropertyConstraintTypes.h"
33 #include "../WGuiConsts.h"
34 
35 #include "WPropertyFilenameWidget.h"
36 
37 WPropertyFilenameWidget::WPropertyFilenameWidget( WPropFilename property, QGridLayout* propertyGrid, QWidget* parent ):
38  WPropertyWidget( property, propertyGrid, parent ),
39  m_fnProperty( property ),
40  m_button( &m_parameterWidgets ),
41  m_layout( &m_parameterWidgets ),
42  m_asText( &m_informationWidgets ),
43  m_infoLayout( &m_informationWidgets )
44 {
45  // initialize members
46  m_parameterWidgets.setLayout( &m_layout );
47 
48  // layout both against each other
49  m_layout.addWidget( &m_button );
50  m_layout.setMargin( WGLOBAL_MARGIN );
51  m_layout.setSpacing( WGLOBAL_SPACING );
52 
53  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
54  m_infoLayout.addWidget( &m_asText );
55  m_infoLayout.setMargin( WGLOBAL_MARGIN );
56  m_infoLayout.setSpacing( WGLOBAL_SPACING );
57  m_informationWidgets.setLayout( &m_infoLayout );
58 
59  m_button.setMinimumHeight( WMIN_WIDGET_HEIGHT );
60 
61  // accept drag and drop
62  setAcceptDrops( true );
63 
64  // set the initial values
65  update();
66 
67  // connect the modification signal of the edit and slider with our callback
68  connect( &m_button, SIGNAL( released() ), this, SLOT( buttonReleased() ) );
69 }
70 
72 {
73  // cleanup
74 }
75 
77 {
78  QString val = QString::fromStdString( m_fnProperty->get().filename().string() );
79  m_button.setText( val );
80  m_asText.setText( val );
81 }
82 
84 {
85  QString path;
86 
87  QFileDialog::Option options = m_fnProperty->countConstraint( PC_CONFIRMOVERWRITE ) != 0 ?
88  ( QFileDialog::Option ) 0 : QFileDialog::DontConfirmOverwrite;
89 
90  // if there is a "IsDirectory" constraint -> set a special option in the dialog
91  if( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 )
92  {
93  // OK there should only be directories allowed
94  path = QFileDialog::getExistingDirectory( this,
95  QString::fromStdString( "Select directory for " + m_fnProperty->getName() ),
96  QString::fromStdString( m_fnProperty->get().string() ),
97  options );
98  }
99  else if( m_fnProperty->countConstraint( PC_PATHEXISTS ) != 0 )
100  {
101  path = QFileDialog::getOpenFileName( this,
102  QString::fromStdString( "Select existing file for " + m_fnProperty->getName() ),
103  QString::fromStdString( m_fnProperty->get().string() ), QString(), 0,
104  options );
105  }
106  else
107  {
108  path = QFileDialog::getSaveFileName( this,
109  QString::fromStdString( "Select file for " + m_fnProperty->getName() ),
110  QString::fromStdString( m_fnProperty->get().string() ), QString(), 0,
111  options );
112  }
113 
114  // convert it back to a filename property
115  invalidate( !m_fnProperty->set( boost::filesystem::path( path.toStdString() ) ) ); // NOTE: set automatically checks the validity of the value
116 
117  // set button
118  m_button.setText( QString::fromStdString( m_fnProperty->get().filename().string() ) );
119 }
120 
121 
122 void WPropertyFilenameWidget::dragEnterEvent( QDragEnterEvent* event )
123 {
124  if( event->mimeData()->hasUrls() && ( event->mimeData()->urls().size() == 1 ) )
125  {
126  // accept only a single url to a local file
127  QUrl url = *( event->mimeData()->urls().begin() );
128  QString path = url.toLocalFile();
129  QFileInfo info( path );
130 
131  bool shouldBeDir = ( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 );
132  bool shouldExist = ( m_fnProperty->countConstraint( PC_PATHEXISTS ) != 0 );
133 
134  // needs to be a directory?
135  bool accept = ( shouldBeDir && info.isDir() ) || !shouldBeDir;
136  accept &= ( shouldExist && info.exists() ) || !shouldExist;
137 
138  event->setAccepted( accept );
139  }
140 }
141 
142 void WPropertyFilenameWidget::dropEvent( QDropEvent* event )
143 {
144  if( event->mimeData()->hasUrls() && ( event->mimeData()->urls().size() == 1 ) )
145  {
146  // accept only a single url to a local file
147  QUrl url = *( event->mimeData()->urls().begin() );
148  QString path = url.toLocalFile();
149  QFileInfo info( path );
150 
151  bool shouldBeDir = ( m_fnProperty->countConstraint( PC_ISDIRECTORY ) != 0 );
152  bool shouldExist = ( m_fnProperty->countConstraint( PC_PATHEXISTS ) != 0 );
153 
154  // needs to be a directory?
155  bool accept = ( shouldBeDir && info.isDir() ) || !shouldBeDir;
156  accept &= ( shouldExist && info.exists() ) || !shouldExist;
157 
158  // use new path/filename
159 
160  // convert it back to a filename property
161  invalidate( !m_fnProperty->set( boost::filesystem::path( path.toStdString() ) ) ); // NOTE: set automatically checks the validity of the value
162 
163  // set button
164  m_button.setText( QString::fromStdString( m_fnProperty->get().filename().string() ) );
165  }
166 }
167 
QHBoxLayout m_layout
Layout used to position the label and the checkbox.
WPropFilename m_fnProperty
The filename property represented by this widget.
void buttonReleased()
Called when the m_button was pressed.
QPushButton m_button
The button field showing the value.
virtual void update()
Called whenever the widget should update.
WPropertyFilenameWidget(WPropFilename property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
virtual ~WPropertyFilenameWidget()
Destructor.
WScaleLabel m_asText
Used to show the property as text.
virtual void dropEvent(QDropEvent *event)
Reimplemented to accept color drops.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
virtual void dragEnterEvent(QDragEnterEvent *event)
Reimplemented to accept color drops.
Class building the base for all widgets representing properties.
QWidget m_informationWidgets
The widget containing a layout and provides the widgets for showing information properties.
QWidget m_parameterWidgets
The widget containing a layout and provides the edit widgets for the property.
virtual void invalidate(bool invalid=true)
This method marks this widget as invalid.
virtual bool event(QEvent *event)
Custom event dispatcher.
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93