19
19
#include " qgssettings.h"
20
20
#include " qgsgui.h"
21
21
#include " qgis.h"
22
+ #include " qgsbrowsermodel.h"
22
23
23
24
#include < QPushButton>
25
+ #include < QMenu>
24
26
25
27
QgsDataSourceSelectDialog::QgsDataSourceSelectDialog (
26
28
QgsBrowserModel *browserModel,
@@ -32,6 +34,7 @@ QgsDataSourceSelectDialog::QgsDataSourceSelectDialog(
32
34
if ( ! browserModel )
33
35
{
34
36
mBrowserModel = qgis::make_unique<QgsBrowserModel>();
37
+ mBrowserModel ->initialize ();
35
38
mOwnModel = true ;
36
39
}
37
40
else
@@ -44,20 +47,68 @@ QgsDataSourceSelectDialog::QgsDataSourceSelectDialog(
44
47
setWindowTitle ( tr ( " Select a Data Source" ) );
45
48
QgsGui::enableAutoGeometryRestore ( this );
46
49
47
- mBrowserModel ->initialize ();
48
50
mBrowserProxyModel .setBrowserModel ( mBrowserModel .get () );
49
51
mBrowserTreeView ->setHeaderHidden ( true );
50
52
51
53
if ( setFilterByLayerType )
52
54
{
55
+ // This will also set the (proxy) model
53
56
setLayerTypeFilter ( layerType );
54
57
}
55
58
else
56
59
{
57
60
mBrowserTreeView ->setModel ( &mBrowserProxyModel );
58
61
buttonBox->button ( QDialogButtonBox::StandardButton::Ok )->setEnabled ( false );
59
62
}
63
+
64
+ mBrowserTreeView ->setBrowserModel ( mBrowserModel .get () );
65
+
66
+ mWidgetFilter ->hide ();
67
+ mLeFilter ->setPlaceholderText ( tr ( " Type here to filter visible items…" ) );
68
+ // icons from http://www.fatcow.com/free-icons License: CC Attribution 3.0
69
+
70
+ QMenu *menu = new QMenu ( this );
71
+ menu->setSeparatorsCollapsible ( false );
72
+ mBtnFilterOptions ->setMenu ( menu );
73
+ QAction *action = new QAction ( tr ( " Case Sensitive" ), menu );
74
+ action->setData ( " case" );
75
+ action->setCheckable ( true );
76
+ action->setChecked ( false );
77
+ connect ( action, &QAction::toggled, this , &QgsDataSourceSelectDialog::setCaseSensitive );
78
+ menu->addAction ( action );
79
+ QActionGroup *group = new QActionGroup ( menu );
80
+ action = new QAction ( tr ( " Filter Pattern Syntax" ), group );
81
+ action->setSeparator ( true );
82
+ menu->addAction ( action );
83
+ action = new QAction ( tr ( " Normal" ), group );
84
+ action->setData ( QgsBrowserProxyModel::Normal );
85
+ action->setCheckable ( true );
86
+ action->setChecked ( true );
87
+ menu->addAction ( action );
88
+ action = new QAction ( tr ( " Wildcard(s)" ), group );
89
+ action->setData ( QgsBrowserProxyModel::Wildcards );
90
+ action->setCheckable ( true );
91
+ menu->addAction ( action );
92
+ action = new QAction ( tr ( " Regular Expression" ), group );
93
+ action->setData ( QgsBrowserProxyModel::RegularExpression );
94
+ action->setCheckable ( true );
95
+ menu->addAction ( action );
96
+
97
+ mBrowserTreeView ->setExpandsOnDoubleClick ( false );
98
+
99
+ connect ( mActionRefresh , &QAction::triggered, [ = ] { refreshModel ( QModelIndex () ); } );
60
100
connect ( mBrowserTreeView , &QgsBrowserTreeView::clicked, this , &QgsDataSourceSelectDialog::onLayerSelected );
101
+ connect ( mActionCollapse , &QAction::triggered, mBrowserTreeView , &QgsBrowserTreeView::collapseAll );
102
+ connect ( mActionShowFilter , &QAction::triggered, this , &QgsDataSourceSelectDialog::showFilterWidget );
103
+ connect ( mLeFilter , &QgsFilterLineEdit::returnPressed, this , &QgsDataSourceSelectDialog::setFilter );
104
+ connect ( mLeFilter , &QgsFilterLineEdit::cleared, this , &QgsDataSourceSelectDialog::setFilter );
105
+ connect ( mLeFilter , &QgsFilterLineEdit::textChanged, this , &QgsDataSourceSelectDialog::setFilter );
106
+ connect ( group, &QActionGroup::triggered, this , &QgsDataSourceSelectDialog::setFilterSyntax );
107
+
108
+ if ( QgsSettings ().value ( QStringLiteral ( " datasourceSelectFilterVisible" ), false , QgsSettings::Section::Gui ).toBool () )
109
+ {
110
+ mActionShowFilter ->trigger ();
111
+ }
61
112
}
62
113
63
114
QgsDataSourceSelectDialog::~QgsDataSourceSelectDialog ()
@@ -66,6 +117,107 @@ QgsDataSourceSelectDialog::~QgsDataSourceSelectDialog()
66
117
mBrowserModel .release ();
67
118
}
68
119
120
+
121
+ void QgsDataSourceSelectDialog::showEvent ( QShowEvent *e )
122
+ {
123
+ QDialog::showEvent ( e );
124
+ QString lastSelectedPath ( QgsSettings ().value ( QStringLiteral ( " datasourceSelectLastSelectedItem" ),
125
+ QString (), QgsSettings::Section::Gui ).toString () );
126
+ if ( ! lastSelectedPath.isEmpty () )
127
+ {
128
+ QModelIndexList items = mBrowserProxyModel .match (
129
+ mBrowserProxyModel .index ( 0 , 0 ),
130
+ QgsBrowserModel::PathRole,
131
+ QVariant::fromValue ( lastSelectedPath ),
132
+ 1 ,
133
+ Qt::MatchRecursive );
134
+ if ( items.count ( ) > 0 )
135
+ {
136
+ QModelIndex expandIndex = items.at ( 0 );
137
+ if ( expandIndex.isValid () )
138
+ {
139
+ mBrowserTreeView ->scrollTo ( expandIndex, QgsBrowserTreeView::QgsBrowserTreeView::ScrollHint::PositionAtTop );
140
+ mBrowserTreeView ->expand ( expandIndex );
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+ void QgsDataSourceSelectDialog::showFilterWidget ( bool visible )
147
+ {
148
+ QgsSettings ().setValue ( QStringLiteral ( " datasourceSelectFilterVisible" ), visible, QgsSettings::Section::Gui );
149
+ mWidgetFilter ->setVisible ( visible );
150
+ if ( ! visible )
151
+ {
152
+ mLeFilter ->setText ( QString () );
153
+ setFilter ();
154
+ }
155
+ else
156
+ {
157
+ mLeFilter ->setFocus ();
158
+ }
159
+ }
160
+
161
+ void QgsDataSourceSelectDialog::setFilter ()
162
+ {
163
+ QString filter = mLeFilter ->text ();
164
+ mBrowserProxyModel .setFilterString ( filter );
165
+ }
166
+
167
+
168
+ void QgsDataSourceSelectDialog::refreshModel ( const QModelIndex &index )
169
+ {
170
+
171
+ QgsDataItem *item = mBrowserModel ->dataItem ( index );
172
+ if ( item )
173
+ {
174
+ QgsDebugMsg ( " path = " + item->path () );
175
+ }
176
+ else
177
+ {
178
+ QgsDebugMsg ( QStringLiteral ( " invalid item" ) );
179
+ }
180
+
181
+ if ( item && ( item->capabilities2 () & QgsDataItem::Fertile ) )
182
+ {
183
+ mBrowserModel ->refresh ( index );
184
+ }
185
+
186
+ for ( int i = 0 ; i < mBrowserModel ->rowCount ( index ); i++ )
187
+ {
188
+ QModelIndex idx = mBrowserModel ->index ( i, 0 , index );
189
+ QModelIndex proxyIdx = mBrowserProxyModel .mapFromSource ( idx );
190
+ QgsDataItem *child = mBrowserModel ->dataItem ( idx );
191
+
192
+ // Check also expanded descendants so that the whole expanded path does not get collapsed if one item is collapsed.
193
+ // Fast items (usually root items) are refreshed so that when collapsed, it is obvious they are if empty (no expand symbol).
194
+ if ( mBrowserTreeView ->isExpanded ( proxyIdx ) || mBrowserTreeView ->hasExpandedDescendant ( proxyIdx ) || ( child && child->capabilities2 () & QgsDataItem::Fast ) )
195
+ {
196
+ refreshModel ( idx );
197
+ }
198
+ else
199
+ {
200
+ if ( child && ( child->capabilities2 () & QgsDataItem::Fertile ) )
201
+ {
202
+ child->depopulate ();
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+
209
+ void QgsDataSourceSelectDialog::setFilterSyntax ( QAction *action )
210
+ {
211
+ if ( !action )
212
+ return ;
213
+ mBrowserProxyModel .setFilterSyntax ( static_cast < QgsBrowserProxyModel::FilterSyntax >( action->data ().toInt () ) );
214
+ }
215
+
216
+ void QgsDataSourceSelectDialog::setCaseSensitive ( bool caseSensitive )
217
+ {
218
+ mBrowserProxyModel .setFilterCaseSensitivity ( caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
219
+ }
220
+
69
221
void QgsDataSourceSelectDialog::setLayerTypeFilter ( QgsMapLayer::LayerType layerType )
70
222
{
71
223
mBrowserProxyModel .setFilterByLayerType ( true );
@@ -95,6 +247,8 @@ void QgsDataSourceSelectDialog::onLayerSelected( const QModelIndex &index )
95
247
{
96
248
isLayerCompatible = true ;
97
249
mUri = layerItem->mimeUri ();
250
+ // Store last viewed item
251
+ QgsSettings ().setValue ( QStringLiteral ( " datasourceSelectLastSelectedItem" ), mBrowserProxyModel .data ( index, QgsBrowserModel::PathRole ).toString (), QgsSettings::Section::Gui );
98
252
}
99
253
}
100
254
}
0 commit comments