GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
DelegateDate.cpp
Go to the documentation of this file.
1#line 2 "src/Gui/AccountRegister/DelegateDate.cpp"
2
3#include <Wt/WDateEdit.h>
4#include <Wt/WHBoxLayout.h>
5
6#include "Editor.h"
7#include "DelegateDate.h"
8
9/* * * * * * * * * * * * * * * * * * * * * * * * * * */
10
13{
14// std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "(): " << std::endl;
15
16 /*
17 ** setting the text format here can work, except
18 ** it requires that _every_ row have the WDateTime value
19 ** set as its value, otherwise the display-conversion
20 ** will throw.
21 */
22 setTextFormat( GCW_DATE_FORMAT_DISPLAY ); // use this DATE format for the conversion
23
24} // endDateDelegate()
25
28{
29// std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "(): " << std::endl;
30}
31
32auto
34createEditor( const Wt::WModelIndex & _index, Wt::WFlags< Wt::ViewItemRenderFlag > _flags ) const-> std::unique_ptr< Wt::WWidget >
35{
36#ifdef NEVER
37 std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__
38 << " (" << _index.row() << "," << _index.column() << ")"
39 << std::endl;
40#endif
41
42#ifdef NEVER
43 std::cout << __FILE__ << ":" << __LINE__
44 << "(): " << _index.row() << "," << _index.column()
45 << " DateDelegate::" << __FUNCTION__
46 << " " << _index.data( Wt::ItemDataRole::Edit ).type().name()
47 << " '" << Wt::asString( _index.data( Wt::ItemDataRole::Edit ) ) << "'"
48 << std::endl;
49#endif
50
51 /*
52 ** The editor is placed in to a container for layout
53 ** management
54 */
55 auto retVal = std::make_unique< Wt::WContainerWidget >();
56 retVal-> setSelectable( true );
57
58 /*
59 ** Build an editor
60 **
61 ** Hitting the 'enter' key or the 'esc' key closes the editor
62 */
63 auto dateEdit = std::make_unique< Wt::WDateEdit >();
64 dateEdit-> setFormat( GCW::Cfg::date_format() );
65// m_dateEdit-> enterPressed ().connect( [&](){ doCloseEditor( dateEdit.get(), true ); });
66// m_dateEdit-> escapePressed ().connect( [&](){ doCloseEditor( dateEdit.get(), false ); });
67// m_dateEdit-> keyWentDown ().connect( [&]( Wt::WKeyEvent _keyEvent ){ doTabAction( _keyEvent ); });
68
69 setDate( dateEdit.get(), _index.data( Wt::ItemDataRole::Edit ) );
70
71 /*
72 ** Stuff it in to the layout
73 */
74 retVal-> setLayout( std::make_unique< Wt::WHBoxLayout >() );
75 retVal-> layout()-> setContentsMargins( 1,1,1,1 );
76 retVal-> layout()-> addWidget( std::move( dateEdit ) );
77
78 return std::move( retVal );
79
80} // endcreateEditor( const Wt::WModelIndex & _index, Wt::WFlags< Wt::ViewItemRenderFlag > _flags ) const-> std::unique_ptr< Wt::WWidget >
81
82auto
84setDate( Wt::WDateEdit * _dateEdit, Wt::cpp17::any _value ) const-> void
85{
86 /*
87 ** this just verifies we have the right type
88 */
89 if( _value.type() == typeid( Wt::WDateTime ) )
90 {
91#ifdef NEVER
92 std::cout << __FILE__ << ":" << __LINE__
93 << " " << GCW::Cfg::date_format()
94 << " " << Wt::asString( _value )
95 << std::endl;
96#endif
97
98 /*
99 ** Get the date from the value
100 */
101 auto dateTime = Wt::cpp17::any_cast< Wt::WDateTime >( _value );
102
103 _dateEdit-> setDate( dateTime.date() );
104
105 } // endif( _value.type() == typeid( Wt::WDateTime ) )
106
107} // endsetDate( Wt::cpp17::any & _value )-> void
108
109auto
111doCloseEditor( Wt::WDateEdit * _dateEdit, bool _save ) const-> void
112{
113#ifndef NEVER
114 std::cout << __FILE__ << ":" << __LINE__ << " DateDelegate::doCloseEditor()" << std::endl;
115#endif
116
117 closeEditor().emit( _dateEdit, _save );
118
119} // enddoCloseEditor( Wt::WDateEdit * _dateEdit, bool save ) const-> void
120
121auto
123doTabAction( Wt::WKeyEvent _keyEvent ) const-> void
124{
125#ifndef NEVER
126 std::cout << __FILE__ << ":" << __LINE__ << " DateDelegate::doTabAction()" << std::endl;
127#endif
128
129} // enddoTabAction( Wt::WKeyEvent _keyEvent ) const-> void
130
131auto
133editState( Wt::WWidget * _editor, const Wt::WModelIndex & _index ) const-> Wt::cpp17::any
134{
135 Wt::WDateTime retVal;
136
137 auto cw = dynamic_cast< Wt::WContainerWidget* >( _editor );
138 if( cw && cw-> layout()-> count() > 0 )
139 {
140 auto de = dynamic_cast< Wt::WDateEdit* >( cw-> layout()-> itemAt(0)-> widget() );
141 if( de )
142 {
143 /*
144 ** the date editor only returns a 'date' component
145 ** so in order to return the correct data type
146 ** the date component must be upgraded to a datetime
147 ** element for return. Set the default time upon
148 ** doing so.
149 */
150 retVal.setDate( de-> date() );
152
153 }
154
155 }
156
157 return retVal ;
158
159} // endeditState( Wt::WWidget * _editor, const Wt::WModelIndex & _index ) const-> Wt::cpp17::any
160
161auto
163setEditState( Wt::WWidget * _editor, const Wt::WModelIndex & _index, const Wt::cpp17::any & _value ) const-> void
164{
165#ifdef NEVER
166 std::cout << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__
167 << "(): " << _editor
168 << " " << _index.row() << "," << _index.column()
169 << " " << _value.type().name()
170 << " '" << Wt::asString( _value ) << "'"
171 << std::endl;
172#endif
173
174 auto cw = dynamic_cast< Wt::WContainerWidget* >( _editor );
175 if( cw && cw-> layout()-> count() > 0 )
176 {
177 auto de = dynamic_cast< Wt::WDateEdit* >( cw-> layout()-> itemAt(0)-> widget() );
178 if( de )
179 {
180 setDate( de, _value );
181 }
182 }
183
184} // endsetEditState( Wt::WWidget * _editor, const Wt::WModelIndex & _index, const Wt::cpp17::any & _value ) const-> void
185
186auto
188setModelData( const Wt::cpp17::any & _editState, Wt::WAbstractItemModel * _model, const Wt::WModelIndex & _index ) const-> void
189{
190#ifdef NEVER
191 std::cout << __FILE__ << ":" << __LINE__
192 << " setModelData()"
193 << " " << _index.row()
194 << " " << _index.column()
195 << " " << _editState.type().name()
196 << " " << Wt::asString( _editState )
197 << " " << _model
198 << std::endl;
199#endif
200
201 DelegateBase::setModelData( _editState, _model, _index );
202
203} // endsetModelData( const Wt::cpp17::any & _editState, Wt::WAbstractItemModel * _model, const Wt::WModelIndex & _index ) const-> void
204
205
206
virtual auto setModelData(const Wt::cpp17::any &_editState, Wt::WAbstractItemModel *_model, const Wt::WModelIndex &_index) const -> void
virtual auto setModelData(const Wt::cpp17::any &_editState, Wt::WAbstractItemModel *_model, const Wt::WModelIndex &_index) const -> void
auto doTabAction(Wt::WKeyEvent _keyEvent) const -> void
auto setDate(Wt::WDateEdit *_dateEdit, Wt::cpp17::any _value) const -> void
auto doCloseEditor(Wt::WDateEdit *_dateEdit, bool _save) const -> void
virtual auto editState(Wt::WWidget *_editor, const Wt::WModelIndex &_index) const -> Wt::cpp17::any override
Edit State.
virtual auto createEditor(const Wt::WModelIndex &_index, Wt::WFlags< Wt::ViewItemRenderFlag > _flags) const -> std::unique_ptr< Wt::WWidget >
virtual auto setEditState(Wt::WWidget *_editor, const Wt::WModelIndex &_index, const Wt::cpp17::any &_value) const -> void
static constexpr const int Edit
void setDate(const WDate &date)
void setTime(const WTime &time)
void setTextFormat(const WString &format)
#define GCW_DATE_DEFAULT_TIME
Default Time.
Definition gcwglobal.h:25
#define GCW_DATE_FORMAT_DISPLAY
Definition gcwglobal.h:14
WString asString(const cpp17::any &v, const WString &formatString=WString())
std::string date_format()
Date Format Specifier.
Definition GnuCashew.cpp:27