QGIS API Documentation  3.0.2-Girona (307d082)
qgsfieldvalueslineedit.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldvalueslineedit.h
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 #ifndef QGSFIELDVALUESLINEEDIT_H
16 #define QGSFIELDVALUESLINEEDIT_H
17 
18 #include "qgsfilterlineedit.h"
19 #include "qgis.h"
20 #include "qgsfeedback.h"
21 #include "qgsvectorlayer.h"
22 #include <QStringListModel>
23 #include <QTreeView>
24 #include <QFocusEvent>
25 #include <QHeaderView>
26 #include <QTimer>
27 #include <QThread>
28 #include "qgis_gui.h"
29 
30 class QgsFloatingWidget;
31 
32 
33 #ifndef SIP_RUN
34 
35 // just internal guff - definitely not for exposing to public API!
37 
42 class QgsFieldValuesLineEditValuesGatherer: public QThread
43 {
44  Q_OBJECT
45 
46  public:
47  QgsFieldValuesLineEditValuesGatherer( QgsVectorLayer *layer, int attributeIndex )
48  : mLayer( layer )
49  , mAttributeIndex( attributeIndex )
50  , mWasCanceled( false )
51  {}
52 
56  void setSubstring( const QString &string ) { mSubstring = string; }
57 
58  void run() override
59  {
60  mWasCanceled = false;
61  if ( mSubstring.isEmpty() )
62  {
63  emit collectedValues( QStringList() );
64  return;
65  }
66 
67  // allow responsive cancelation
68  mFeedback = new QgsFeedback();
69  // just get 100 values... maybe less/more would be useful?
70  mValues = mLayer->uniqueStringsMatching( mAttributeIndex, mSubstring, 100, mFeedback );
71 
72  // be overly cautious - it's *possible* stop() might be called between deleting mFeedback and nulling it
73  mFeedbackMutex.lock();
74  delete mFeedback;
75  mFeedback = nullptr;
76  mFeedbackMutex.unlock();
77 
78  emit collectedValues( mValues );
79  }
80 
82  void stop()
83  {
84  // be cautious, in case gatherer stops naturally just as we are canceling it and mFeedback gets deleted
85  mFeedbackMutex.lock();
86  if ( mFeedback )
87  mFeedback->cancel();
88  mFeedbackMutex.unlock();
89 
90  mWasCanceled = true;
91  }
92 
94  bool wasCanceled() const { return mWasCanceled; }
95 
96  signals:
97 
102  void collectedValues( const QStringList &values );
103 
104  private:
105 
106  QgsVectorLayer *mLayer = nullptr;
107  int mAttributeIndex;
108  QString mSubstring;
109  QStringList mValues;
110  QgsFeedback *mFeedback = nullptr;
111  QMutex mFeedbackMutex;
112  bool mWasCanceled;
113 };
114 
116 
117 #endif
118 
128 {
129  Q_OBJECT
130 
131  Q_PROPERTY( QgsVectorLayer *layer READ layer WRITE setLayer NOTIFY layerChanged )
132  Q_PROPERTY( int attributeIndex READ attributeIndex WRITE setAttributeIndex NOTIFY attributeIndexChanged )
133 
134  public:
135 
140  QgsFieldValuesLineEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );
141 
142  ~QgsFieldValuesLineEdit() override;
143 
150  void setLayer( QgsVectorLayer *layer );
151 
157  QgsVectorLayer *layer() const { return mLayer; }
158 
165  void setAttributeIndex( int index );
166 
172  int attributeIndex() const { return mAttributeIndex; }
173 
174  signals:
175 
180  void layerChanged( QgsVectorLayer *layer );
181 
186  void attributeIndexChanged( int index );
187 
188  private slots:
189 
194  void requestCompleterUpdate();
195 
200  void triggerCompleterUpdate();
201 
206  void updateCompleter( const QStringList &values );
207 
213  void gathererThreadFinished();
214 
215  private:
216 
217  QgsVectorLayer *mLayer = nullptr;
218  int mAttributeIndex = -1;
219 
221  bool mUpdateRequested = false;
222 
224  QTimer mShowPopupTimer;
225 
227  QgsFieldValuesLineEditValuesGatherer *mGatherer = nullptr;
228 
230  QString mRequestedCompletionText;
231 
233  void updateCompletionList( const QString &substring );
234 
235 };
236 
237 
238 #endif //QGSFIELDVALUESLINEEDIT_H
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A QWidget subclass for creating widgets which float outside of the normal Qt layout system...
int attributeIndex() const
Returns the attribute index for the field containing values shown in the widget.
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
A line edit with an autocompleter which takes unique values from a vector layer&#39;s fields...
Represents a vector layer which manages a vector based data sets.