sqlqueryresultlist searchhandler::perform_grep(QString searchtxt, sqlqueryresultlist searchlist, bool exactmatch) { QVector<QString> strvec; sqlqueryresultlist resultlist; QFutureWatcher<sqlqueryresultlist> futureWatcher; QProgressDialog dialog; unsigned int n = searchlist.resultlist.size(); if (n == 0) return resultlist; strvec.resize(n); for (unsigned int i=0; i < n; i++) { strvec.replace(i, str2qt(searchlist.resultlist[i].filepath)); } dialog.setAutoReset(false); dialog.setLabelText(QString("Grep ").append(QString(tr("in progress"))).append(QString(" ..."))); dialog.setCancelButtonText(tr("Cancel")); QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset())); QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel())); QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), &dialog, SLOT(setRange(int,int))); QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, SLOT(setValue(int))); m_grepExactMatch = exactmatch; (*m_grepRegExp) = QRegExp(searchtxt.toAscii().data(), Qt::CaseInsensitive); m_grepRegExp->setPatternSyntax(QRegExp::RegExp2); futureWatcher.setFuture(QtConcurrent::mappedReduced(strvec, doGrep, collateGrep, QtConcurrent::SequentialReduce)); dialog.exec(); futureWatcher.waitForFinished(); if (futureWatcher.isCanceled() == false) resultlist = futureWatcher.result(); return resultlist; }
QVariant ThumbnailModel::data(const QModelIndex &index, int role) const { if (role == Qt::DecorationRole && index.isValid()) { QString id = itemId(index).toString(); QFutureWatcher<QImage> *future = cache.object(id); if (!future) { future = new QFutureWatcher<QImage>; QString path = imagePath(index); if (!path.isEmpty()) { future->setFuture(QtConcurrent::run(ThumbnailModel::load, path)); connect(future, SIGNAL(finished()), this, SLOT(thumbnailLoaded())); } cache.insert(id, future); } return !future->isCanceled() ? future->result() : QVariant(); } else { return QGalleryQueryModel::data(index, role); } }
void SpellCheckerCore::futureFinished() { /* Get the watcher from the sender() of the signal that invoked this slot. * reinterpret_cast is used since qobject_cast is not valid of template * classes since the template class does not have the Q_OBJECT macro. */ QFutureWatcher<WordList>* watcher = reinterpret_cast<QFutureWatcher<WordList>*>( sender() ); if( watcher == nullptr ) { return; } if( d->shuttingDown == true ) { /* Application shutting down, should not try something */ return; } if( watcher->isCanceled() == true ) { /* Application is shutting down */ return; } /* Get the list of words with spelling mistakes from the future. */ WordList checkedWords = watcher->result(); QMutexLocker locker( &d->futureMutex ); /* Recheck again after getting the lock. */ if( d->shuttingDown == true ) { return; } /* Get the file name associated with this future and the misspelled * words. */ FutureWatcherMapIter iter = d->futureWatchers.find( watcher ); if( iter == d->futureWatchers.end() ) { return; } QString fileName = iter.value(); /* Remove the watcher from the list of running watchers and the file that * kept track of the file getting spell checked. */ d->futureWatchers.erase( iter ); d->filesInProcess.removeAll( fileName ); /* Check if the file was scheduled for a re-check. As discussed previously, * if a spell check was requested for a file that had a future already in * progress, it was scheduled for a re-check as soon as the in progress one * completes. If it was scheduled, restart it using the normal slot. */ QHash<QString, WordList>::iterator waitingIter = d->filesWaitingForProcess.find( fileName ); if( waitingIter != d->filesWaitingForProcess.end() ) { WordList wordsToSpellCheck = waitingIter.value(); /* remove the file and words from the scheduled list. */ d->filesWaitingForProcess.erase( waitingIter ); locker.unlock(); /* Invoke the method to make sure that it gets called from the main thread. * This will most probably be already in the main thread, but to make sure * it is done like this. */ this->metaObject()->invokeMethod( this , "spellcheckWordsFromParser" , Qt::QueuedConnection , Q_ARG( QString, fileName ) , Q_ARG( SpellChecker::WordList, wordsToSpellCheck ) ); } locker.unlock(); watcher->deleteLater(); /* Add the list of misspelled words to the mistakes model */ addMisspelledWords( fileName, checkedWords ); }
void SymbolsFindFilter::finish() { QFutureWatcher<SearchResultItem> *watcher = static_cast<QFutureWatcher<SearchResultItem> *>(sender()); SearchResult *search = m_watchers.value(watcher); if (search) search->finishSearch(watcher->isCanceled()); m_watchers.remove(watcher); watcher->deleteLater(); }
void QgsGeometryCheckerSetupTab::runChecks() { // Get selected layer QList<QgsVectorLayer *> layers = getSelectedLayers(); if ( layers.isEmpty() ) return; if ( ui.radioButtonOutputNew->isChecked() ) { for ( QgsVectorLayer *layer : layers ) { if ( layer->dataProvider()->dataSourceUri().startsWith( ui.lineEditOutputDirectory->text() ) ) { QMessageBox::critical( this, tr( "Invalid Output Directory" ), tr( "The chosen output directory contains one or more input layers." ) ); return; } } } QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ) : nullptr; QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) ) : nullptr; if ( layers.contains( lineLayerCheckLayer ) || layers.contains( followBoundaryCheckLayer ) ) { QMessageBox::critical( this, tr( "Error" ), tr( "The test layer set contains a layer selected for a topology check." ) ); return; } for ( QgsVectorLayer *layer : layers ) { if ( layer->isEditable() ) { QMessageBox::critical( this, tr( "Editable Input Layer" ), tr( "Input layer are not allowed to be in editing mode." ) ); return; } } bool selectedOnly = ui.checkBoxInputSelectedOnly->isChecked(); // Set window busy setCursor( Qt::WaitCursor ); mRunButton->setEnabled( false ); ui.labelStatus->setText( tr( "<b>Preparing output...</b>" ) ); ui.labelStatus->show(); QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); QList<QgsVectorLayer *> processLayers; if ( ui.radioButtonOutputNew->isChecked() ) { // Get output directory and file extension QDir outputDir = QDir( ui.lineEditOutputDirectory->text() ); QString outputDriverName = ui.comboBoxOutputFormat->currentText(); QgsVectorFileWriter::MetaData metadata; if ( !QgsVectorFileWriter::driverMetadata( outputDriverName, metadata ) ) { QMessageBox::critical( this, tr( "Unknown Output Format" ), tr( "The specified output format cannot be recognized." ) ); mRunButton->setEnabled( true ); ui.labelStatus->hide(); unsetCursor(); return; } QString outputExtension = metadata.ext; // List over input layers, check which existing project layers need to be removed and create output layers QString filenamePrefix = ui.lineEditFilenamePrefix->text(); QSettings().setValue( "/geometry_checker/previous_values/filename_prefix", filenamePrefix ); QStringList toRemove; QStringList createErrors; for ( QgsVectorLayer *layer : layers ) { QString outputPath = outputDir.absoluteFilePath( filenamePrefix + layer->name() + "." + outputExtension ); // Remove existing layer with same uri from project for ( QgsVectorLayer *projectLayer : QgsProject::instance()->layers<QgsVectorLayer *>() ) { if ( projectLayer->dataProvider()->dataSourceUri().startsWith( outputPath ) ) { toRemove.append( projectLayer->id() ); } } // Create output layer QString errMsg; QgsVectorFileWriter::WriterError err = QgsVectorFileWriter::writeAsVectorFormat( layer, outputPath, layer->dataProvider()->encoding(), layer->crs(), outputDriverName, selectedOnly, &errMsg ); if ( err != QgsVectorFileWriter::NoError ) { createErrors.append( errMsg ); continue; } QgsVectorLayer *newlayer = new QgsVectorLayer( outputPath, QFileInfo( outputPath ).completeBaseName(), QStringLiteral( "ogr" ) ); if ( selectedOnly ) { QgsFeature feature; // Get features to select (only selected features were written up to this point) QgsFeatureIds selectedFeatures = newlayer->allFeatureIds(); // Write non-selected feature ids QgsFeatureList features; QgsFeatureIterator it = layer->getFeatures(); while ( it.nextFeature( feature ) ) { if ( !layer->selectedFeatureIds().contains( feature.id() ) ) { features.append( feature ); } } newlayer->dataProvider()->addFeatures( features ); // Set selected features newlayer->selectByIds( selectedFeatures ); } processLayers.append( newlayer ); } // Remove layers from project if ( !toRemove.isEmpty() ) { QgsProject::instance()->removeMapLayers( toRemove ); } // Error if an output layer could not be created if ( !createErrors.isEmpty() ) { QMessageBox::critical( this, tr( "Layer Creation Failed" ), tr( "Failed to create one or moure output layers:\n%1" ).arg( createErrors.join( "\n" ) ) ); mRunButton->setEnabled( true ); ui.labelStatus->hide(); unsetCursor(); return; } } else { processLayers = layers; } // Check if output layers are editable QList<QgsVectorLayer *> nonEditableLayers; for ( QgsVectorLayer *layer : processLayers ) { if ( ( layer->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeGeometries ) == 0 ) { nonEditableLayers.append( layer ); } } if ( !nonEditableLayers.isEmpty() ) { QStringList nonEditableLayerNames; for ( QgsVectorLayer *layer : nonEditableLayers ) { nonEditableLayerNames.append( layer->name() ); } if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Non-editable Output Layers" ), tr( "The following output layers are ina format that does not support editing features:\n%1\n\nThe geometry check can be performed, but it will not be possible to fix any errors. Do you want to continue?" ).arg( nonEditableLayerNames.join( "\n" ) ), QMessageBox::Yes, QMessageBox::No ) ) { if ( ui.radioButtonOutputNew->isChecked() ) { for ( QgsVectorLayer *layer : processLayers ) { QString layerPath = layer->dataProvider()->dataSourceUri(); delete layer; if ( ui.comboBoxOutputFormat->currentText() == QLatin1String( "ESRI Shapefile" ) ) { QgsVectorFileWriter::deleteShapeFile( layerPath ); } else { QFile( layerPath ).remove(); } } mRunButton->setEnabled( true ); ui.labelStatus->hide(); unsetCursor(); } return; } } // Setup checker ui.labelStatus->setText( tr( "<b>Building spatial index...</b>" ) ); QApplication::processEvents( QEventLoop::ExcludeUserInputEvents ); QMap<QString, QgsFeaturePool *> featurePools; for ( QgsVectorLayer *layer : processLayers ) { double layerToMapUntis = mIface->mapCanvas()->mapSettings().layerToMapUnits( layer ); QgsCoordinateTransform layerToMapTransform = QgsCoordinateTransformCache::instance()->transform( layer->crs().authid(), mIface->mapCanvas()->mapSettings().destinationCrs().authid() ); featurePools.insert( layer->id(), new QgsFeaturePool( layer, layerToMapUntis, layerToMapTransform, selectedOnly ) ); } // LineLayerIntersection check is enabled, make sure there is also a feature pool for that layer if ( ui.checkLineLayerIntersection->isChecked() && !featurePools.keys().contains( ui.comboLineLayerIntersection->currentData().toString() ) ) { QgsVectorLayer *layer = dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ); Q_ASSERT( layer ); double layerToMapUntis = mIface->mapCanvas()->mapSettings().layerToMapUnits( layer ); QgsCoordinateTransform layerToMapTransform = QgsCoordinateTransformCache::instance()->transform( layer->crs().authid(), mIface->mapCanvas()->mapSettings().destinationCrs().authid() ); featurePools.insert( layer->id(), new QgsFeaturePool( layer, layerToMapUntis, layerToMapTransform, selectedOnly ) ); } QgsGeometryCheckerContext *context = new QgsGeometryCheckerContext( ui.spinBoxTolerance->value(), mIface->mapCanvas()->mapSettings().destinationCrs().authid(), featurePools ); QList<QgsGeometryCheck *> checks; for ( const QgsGeometryCheckFactory *factory : QgsGeometryCheckFactoryRegistry::getCheckFactories() ) { QgsGeometryCheck *check = factory->createInstance( context, ui ); if ( check ) { checks.append( check ); } } QgsGeometryChecker *checker = new QgsGeometryChecker( checks, context ); emit checkerStarted( checker ); if ( ui.radioButtonOutputNew->isChecked() ) { QList<QgsMapLayer *> addLayers; for ( QgsVectorLayer *layer : processLayers ) { addLayers.append( layer ); } QgsProject::instance()->addMapLayers( addLayers ); } // Run ui.buttonBox->addButton( mAbortButton, QDialogButtonBox::ActionRole ); mRunButton->hide(); ui.progressBar->setRange( 0, 0 ); ui.labelStatus->hide(); ui.progressBar->show(); ui.widgetInputs->setEnabled( false ); QEventLoop evLoop; QFutureWatcher<void> futureWatcher; connect( checker, &QgsGeometryChecker::progressValue, ui.progressBar, &QProgressBar::setValue ); connect( &futureWatcher, &QFutureWatcherBase::finished, &evLoop, &QEventLoop::quit ); connect( mAbortButton, &QAbstractButton::clicked, &futureWatcher, &QFutureWatcherBase::cancel ); connect( mAbortButton, &QAbstractButton::clicked, this, &QgsGeometryCheckerSetupTab::showCancelFeedback ); int maxSteps = 0; futureWatcher.setFuture( checker->execute( &maxSteps ) ); ui.progressBar->setRange( 0, maxSteps ); evLoop.exec(); // Restore window unsetCursor(); mAbortButton->setEnabled( true ); ui.buttonBox->removeButton( mAbortButton ); mRunButton->setEnabled( true ); mRunButton->show(); ui.progressBar->hide(); ui.labelStatus->hide(); ui.widgetInputs->setEnabled( true ); // Show result emit checkerFinished( !futureWatcher.isCanceled() ); }