33 #include <QMessageBox>
34 #include <QProgressDialog>
37 : QStackedWidget( parent )
41 , mFeatureListModel( 0 )
45 , mFeatureSelectionManager( 0 )
49 mPreviewActionMapper =
new QSignalMapper(
this );
51 mPreviewColumnsMenu =
new QMenu(
this );
52 mActionPreviewColumnsMenu->setMenu( mPreviewColumnsMenu );
58 connect( mActionExpressionPreview, SIGNAL( triggered() ), SLOT( previewExpressionBuilder() ) );
59 connect( mPreviewActionMapper, SIGNAL( mapped( QObject* ) ), SLOT( previewColumnChanged( QObject* ) ) );
60 connect( mFeatureList, SIGNAL(
displayExpressionChanged( QString ) ),
this, SLOT( previewExpressionChanged( QString ) ) );
65 mEditorContext = context;
67 connect( mTableView, SIGNAL( willShowContextMenu( QMenu*, QModelIndex ) ),
this, SLOT( viewWillShowContextMenu( QMenu*, QModelIndex ) ) );
70 initModels( mapCanvas, request );
72 mTableView->setModel( mFilterModel );
73 mFeatureList->setModel( mFeatureListModel );
75 mAttributeEditorScrollArea->setLayout(
new QGridLayout() );
76 mAttributeEditorScrollArea->layout()->addWidget( mAttributeForm );
77 mAttributeEditorScrollArea->setWidget( mAttributeForm );
81 connect( mAttributeForm, SIGNAL( attributeChanged( QString, QVariant ) ),
this, SLOT( featureFormAttributeChanged() ) );
83 if ( mFeatureListPreviewButton->defaultAction() )
84 mFeatureList->setDisplayExpression( mDisplayExpression );
102 if ( displayExpression ==
"" )
107 displayExpression = QString(
"COALESCE(\"%1\", '<NULL>')" ).arg( defaultField );
112 if ( displayExpression ==
"" )
116 if ( pkAttrs.size() > 0 )
118 if ( pkAttrs.size() == 1 )
119 defaultField = pkAttrs.at( 0 );
122 QStringList pkFields;
124 Q_FOREACH (
int attr, pkAttrs )
126 pkFields.append(
"COALESCE(\"" + fields[attr].name() +
"\", '<NULL>')" );
129 displayExpression = pkFields.join(
"||', '||" );
131 else if ( fields.size() > 0 )
133 if ( fields.size() == 1 )
134 defaultField = fields.at( 0 ).name();
137 QStringList fieldNames;
140 fieldNames.append(
"COALESCE(\"" + field.
name() +
"\", '<NULL>')" );
143 displayExpression = fieldNames.join(
"||', '||" );
148 displayExpression =
"'[Please define preview text]'";
152 mFeatureListPreviewButton->addAction( mActionExpressionPreview );
153 mFeatureListPreviewButton->addAction( mActionPreviewColumnsMenu );
155 Q_FOREACH (
const QgsField& field, fields )
158 if ( fieldIndex == -1 )
164 QString text = field.
name();
167 QAction* previewAction =
new QAction( icon, text, mFeatureListPreviewButton );
168 mPreviewActionMapper->setMapping( previewAction, previewAction );
169 connect( previewAction, SIGNAL( triggered() ), mPreviewActionMapper, SLOT( map() ) );
170 mPreviewColumnsMenu->addAction( previewAction );
172 if ( text == defaultField )
174 mFeatureListPreviewButton->setDefaultAction( previewAction );
180 if ( !mFeatureListPreviewButton->defaultAction() )
182 mFeatureList->setDisplayExpression( displayExpression );
183 mFeatureListPreviewButton->setDefaultAction( mActionExpressionPreview );
184 mDisplayExpression = mFeatureList->displayExpression();
188 mFeatureListPreviewButton->defaultAction()->trigger();
194 setCurrentIndex( view );
208 void QgsDualView::initLayerCache(
QgsVectorLayer* layer,
bool cacheGeometry )
212 int cacheSize = settings.value(
"/qgis/attributeTableRowCache",
"10000" ).toInt();
217 connect( mLayerCache, SIGNAL( progress(
int,
bool & ) ),
this, SLOT( progress(
int,
bool & ) ) );
218 connect( mLayerCache, SIGNAL( finished() ),
this, SLOT( finished() ) );
226 delete mFeatureListModel;
234 connect( mMasterModel, SIGNAL( progress(
int,
bool & ) ),
this, SLOT( progress(
int,
bool & ) ) );
235 connect( mMasterModel, SIGNAL( finished() ),
this, SLOT( finished() ) );
246 void QgsDualView::on_mFeatureList_aboutToChangeEditSelection(
bool& ok )
252 void QgsDualView::on_mFeatureList_currentEditSelectionChanged(
const QgsFeature &feat )
267 mFeatureList->setCurrentFeatureEdited(
false );
268 mFeatureList->setEditSelection( fids );
273 return mAttributeForm->
save();
276 void QgsDualView::previewExpressionBuilder()
280 dlg.setWindowTitle(
tr(
"Expression based preview" ) );
283 if ( dlg.exec() == QDialog::Accepted )
285 mFeatureList->setDisplayExpression( dlg.expressionText() );
286 mFeatureListPreviewButton->setDefaultAction( mActionExpressionPreview );
287 mFeatureListPreviewButton->setPopupMode( QToolButton::MenuButtonPopup );
290 mDisplayExpression = mFeatureList->displayExpression();
293 void QgsDualView::previewColumnChanged( QObject* action )
295 QAction* previewAction = qobject_cast< QAction* >( action );
299 if ( !mFeatureList->setDisplayExpression( QString(
"COALESCE( \"%1\", '<NULL>' )" ).arg( previewAction->text() ) ) )
301 QMessageBox::warning(
this,
302 tr(
"Could not set preview column" ),
303 tr(
"Could not set column '%1' as preview column.\nParser error:\n%2" )
304 .arg( previewAction->text() )
305 .arg( mFeatureList->parserErrorString() )
310 mFeatureListPreviewButton->setDefaultAction( previewAction );
311 mFeatureListPreviewButton->setPopupMode( QToolButton::InstantPopup );
315 mDisplayExpression = mFeatureList->displayExpression();
317 Q_ASSERT( previewAction );
327 return mFilterModel->rowCount();
330 void QgsDualView::viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex )
332 QModelIndex sourceIndex = mFilterModel->mapToSource( atIndex );
338 QAction *a = menu->addAction(
tr(
"Run layer action" ) );
339 a->setEnabled(
false );
349 menu->addAction( action.
name(), a, SLOT( execute() ) );
355 if ( registeredActions.size() > 0 )
358 menu->addSeparator();
360 QList<QgsMapLayerAction*>::iterator actionIt;
361 for ( actionIt = registeredActions.begin(); actionIt != registeredActions.end(); ++actionIt )
364 menu->addAction(( *actionIt )->text(), a, SLOT( execute() ) );
368 menu->addSeparator();
370 menu->addAction(
tr(
"Open form" ), a, SLOT( featureForm() ) );
373 void QgsDualView::previewExpressionChanged(
const QString expression )
378 void QgsDualView::featureFormAttributeChanged()
380 mFeatureList->setCurrentFeatureEdited(
true );
395 mTableView->setFeatureSelectionManager( featureSelectionManager );
398 if ( mFeatureSelectionManager && mFeatureSelectionManager->parent() == this )
399 delete mFeatureSelectionManager;
401 mFeatureSelectionManager = featureSelectionManager;
404 void QgsDualView::progress(
int i,
bool& cancel )
408 mProgressDlg =
new QProgressDialog(
tr(
"Loading features..." ),
tr(
"Abort" ), 0, 0,
this );
409 mProgressDlg->setWindowTitle(
tr(
"Attribute table" ) );
410 mProgressDlg->setWindowModality( Qt::WindowModal );
411 mProgressDlg->show();
414 mProgressDlg->setValue( i );
415 mProgressDlg->setLabelText(
tr(
"%1 features loaded." ).arg( i ) );
417 QCoreApplication::processEvents();
419 cancel = mProgressDlg->wasCanceled();
422 void QgsDualView::finished()