QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsfieldvalueslineedit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldvalueslineedit.cpp
3  -------------------------
4  Date : 20-08-2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsfieldvalueslineedit.h"
17 #include "qgsvectorlayer.h"
18 #include "qgsfloatingwidget.h"
19 #include <QCompleter>
20 #include <QStringListModel>
21 #include <QTimer>
22 #include <QHBoxLayout>
23 
25  : QgsFilterLineEdit( parent )
26 {
27  QCompleter *c = new QCompleter( this );
28  c->setCaseSensitivity( Qt::CaseInsensitive );
29  c->setFilterMode( Qt::MatchContains );
30  setCompleter( c );
31  connect( this, &QgsFieldValuesLineEdit::textEdited, this, &QgsFieldValuesLineEdit::requestCompleterUpdate );
32  mShowPopupTimer.setSingleShot( true );
33  mShowPopupTimer.setInterval( 100 );
34  connect( &mShowPopupTimer, &QTimer::timeout, this, &QgsFieldValuesLineEdit::triggerCompleterUpdate );
35 }
36 
38 {
39  if ( mGatherer )
40  {
41  mGatherer->stop();
42  mGatherer->wait(); // mGatherer is deleted when wait completes
43  }
44 }
45 
47 {
48  if ( mLayer == layer )
49  return;
50 
51  mLayer = layer;
52  emit layerChanged( layer );
53 }
54 
56 {
57  if ( mAttributeIndex == index )
58  return;
59 
60  mAttributeIndex = index;
61  emit attributeIndexChanged( index );
62 }
63 
64 void QgsFieldValuesLineEdit::requestCompleterUpdate()
65 {
66  mUpdateRequested = true;
67  mShowPopupTimer.start();
68 }
69 
70 void QgsFieldValuesLineEdit::triggerCompleterUpdate()
71 {
72  mShowPopupTimer.stop();
73  QString currentText = text();
74 
75  if ( currentText.isEmpty() )
76  {
77  if ( mGatherer )
78  mGatherer->stop();
79  return;
80  }
81 
82  updateCompletionList( currentText );
83 }
84 
85 void QgsFieldValuesLineEdit::updateCompletionList( const QString &text )
86 {
87  if ( text.isEmpty() )
88  {
89  if ( mGatherer )
90  mGatherer->stop();
91  return;
92  }
93 
94  mUpdateRequested = true;
95  if ( mGatherer )
96  {
97  mRequestedCompletionText = text;
98  mGatherer->stop();
99  return;
100  }
101 
102  mGatherer = new QgsFieldValuesLineEditValuesGatherer( mLayer, mAttributeIndex );
103  mGatherer->setSubstring( text );
104 
105  connect( mGatherer, &QgsFieldValuesLineEditValuesGatherer::collectedValues, this, &QgsFieldValuesLineEdit::updateCompleter );
106  connect( mGatherer, &QgsFieldValuesLineEditValuesGatherer::finished, this, &QgsFieldValuesLineEdit::gathererThreadFinished );
107 
108  mGatherer->start();
109 }
110 
111 void QgsFieldValuesLineEdit::gathererThreadFinished()
112 {
113  bool wasCanceled = mGatherer->wasCanceled();
114 
115  delete mGatherer;
116  mGatherer = nullptr;
117 
118  if ( wasCanceled )
119  {
120  QString text = mRequestedCompletionText;
121  mRequestedCompletionText.clear();
122  updateCompletionList( text );
123  return;
124  }
125 }
126 
127 void QgsFieldValuesLineEdit::updateCompleter( const QStringList &values )
128 {
129  mUpdateRequested = false;
130  completer()->setModel( new QStringListModel( values ) );
131  completer()->complete();
132 }
133 
QgsVectorLayer * layer() const
Returns the layer containing the field that values will be shown from.
void setAttributeIndex(int index)
Sets the attribute index for the field containing values to show in the widget.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsFieldValuesLineEdit(QWidget *parent=nullptr)
Constructor for QgsFieldValuesLineEdit.
void setLayer(QgsVectorLayer *layer)
Sets the layer containing the field that values will be shown from.
void layerChanged(QgsVectorLayer *layer)
Emitted when the layer associated with the widget changes.
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
void attributeIndexChanged(int index)
Emitted when the field associated with the widget changes.
Represents a vector layer which manages a vector based data sets.