Wt
3.2.2
|
A widget which popups to assist in editing a textarea or lineedit. More...
#include <Wt/WSuggestionPopup>
Classes | |
struct | Options |
A configuration object to generate a matcher and replacer JavaScript function. More... |
Public Types | |
enum | PopupTrigger { Editing = 0x1, DropDownIcon = 0x2 } |
Enumeration that defines a trigger for showing the popup. More... |
Public Member Functions | |
WSuggestionPopup (const Options &options, WContainerWidget *parent=0) | |
Creates a suggestion popup. | |
WSuggestionPopup (const std::string &matcherJS, const std::string &replacerJS, WContainerWidget *parent=0) | |
Creates a suggestion popup with given matcherJS and replacerJS. | |
void | forEdit (WFormWidget *edit, WFlags< PopupTrigger > popupTriggers=Editing) |
Lets this suggestion popup assist in editing an edit field. | |
void | removeEdit (WFormWidget *edit) |
Removes the edit field from the list of assisted editors. | |
void | showAt (WFormWidget *edit) |
Shows the suggestion popup at an edit field. | |
void | clearSuggestions () |
Clears the list of suggestions. | |
void | addSuggestion (const WString &suggestionText, const WString &suggestionValue) |
Adds a new suggestion. | |
void | setModel (WAbstractItemModel *model) |
Sets the model to be used for the suggestions. | |
WAbstractItemModel * | model () const |
Returns the data model. | |
void | setModelColumn (int index) |
Sets the column in the model to be used for the items. | |
void | setDefaultIndex (int row) |
Sets a default selected value. | |
int | defaultIndex () const |
Returns the default value. | |
void | setFilterLength (int count) |
Sets the minimum input length before showing the popup. | |
int | filterLength () const |
Returns the filter length. | |
Signal< WString > & | filterModel () |
Signal that indicates that the model should be filtered. | |
Signal< int, WFormWidget * > & | activated () |
Signal emitted when a suggestion was selected. | |
void | setGlobalPopup (bool global) |
Controls how the suggestion popup is positioned. | |
virtual void | setMaximumSize (const WLength &width, const WLength &height) |
Sets a maximum size. | |
virtual void | setMinimumSize (const WLength &width, const WLength &height) |
Sets a minimum size. | |
![]() | |
WCompositeWidget (WContainerWidget *parent=0) | |
Creates a WCompositeWidget. | |
WCompositeWidget (WWidget *implementation, WContainerWidget *parent) | |
Creates a WCompositeWidget with given implementation. | |
virtual const std::string | id () const |
Returns the (unique) identifier for this object. | |
virtual void | setPositionScheme (PositionScheme scheme) |
Sets the CSS position scheme. | |
virtual PositionScheme | positionScheme () const |
Returns the CSS position scheme. | |
virtual void | setOffsets (const WLength &offset, WFlags< Side > sides=All) |
Sets CSS offsets for a non-statically positioned widget. | |
virtual WLength | offset (Side s) const |
Returns a CSS offset. | |
virtual void | resize (const WLength &width, const WLength &height) |
Resizes the widget. | |
virtual WLength | width () const |
Returns the width. | |
virtual WLength | height () const |
Returns the height. | |
virtual WLength | minimumWidth () const |
Returns the minimum width. | |
virtual WLength | minimumHeight () const |
Returns the minimum height. | |
virtual WLength | maximumWidth () const |
Returns the maximum width. | |
virtual WLength | maximumHeight () const |
Returns the maximum height. | |
virtual void | setLineHeight (const WLength &height) |
Sets the CSS line height for contained text. | |
virtual WLength | lineHeight () const |
Returns the CSS line height for contained text. | |
virtual void | setFloatSide (Side s) |
Specifies a CSS float side. | |
virtual Side | floatSide () const |
Returns the CSS float side. | |
virtual void | setClearSides (WFlags< Side > sides) |
Sets the sides that should be cleared of floats. | |
virtual WFlags< Side > | clearSides () const |
Returns the sides that should remain empty. | |
virtual void | setMargin (const WLength &margin, WFlags< Side > sides=All) |
Sets CSS margins around the widget. | |
virtual WLength | margin (Side side) const |
Returns a CSS margin set. | |
virtual void | setHiddenKeepsGeometry (bool enabled) |
Sets whether the widget keeps its geometry when hidden. | |
virtual bool | hiddenKeepsGeometry () const |
Returns whether the widget keeps its geometry when hidden. | |
virtual void | setHidden (bool hidden, const WAnimation &animation=WAnimation()) |
Hides or shows the widget. | |
virtual bool | isHidden () const |
Returns whether the widget is set hidden. | |
virtual bool | isVisible () const |
Returns whether the widget is visible. | |
virtual void | setDisabled (bool disabled) |
Sets whether the widget is disabled. | |
virtual bool | isDisabled () const |
Returns whether the widget is set disabled. | |
virtual bool | isEnabled () const |
Returns whether the widget is enabled. | |
virtual void | setPopup (bool popup) |
Lets the widget overlay over other sibling widgets. | |
virtual bool | isPopup () const |
Returns whether the widget is overlayed. | |
virtual void | setInline (bool isInline) |
Sets whether the widget is displayed inline or as a block. | |
virtual bool | isInline () const |
Returns whether the widget is displayed inline or as block. | |
virtual void | setDecorationStyle (const WCssDecorationStyle &style) |
Sets a CSS decoration style. | |
virtual WCssDecorationStyle & | decorationStyle () |
Returns the decoration style of this widget. | |
virtual void | setStyleClass (const WString &styleClass) |
Sets (one or more) CSS style classes. | |
virtual WString | styleClass () const |
Returns the CSS style class. | |
virtual void | addStyleClass (const WString &styleClass, bool force=false) |
Adds a CSS style class. | |
virtual void | removeStyleClass (const WString &styleClass, bool force=false) |
Removes a CSS style class. | |
virtual void | setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength::Auto) |
Sets the vertical alignment. | |
virtual AlignmentFlag | verticalAlignment () const |
Returns the vertical alignment. | |
virtual WLength | verticalAlignmentLength () const |
Returns the fixed vertical alignment that was set. | |
virtual void | setToolTip (const WString &text, TextFormat textFormat=PlainText) |
Sets a tooltip. | |
virtual WString | toolTip () const |
Returns the tooltip. | |
virtual void | refresh () |
Refresh the widget. | |
virtual void | setAttributeValue (const std::string &name, const WString &value) |
Sets an attribute value. | |
virtual WString | attributeValue (const std::string &name) const |
Returns an attribute value. | |
virtual void | setJavaScriptMember (const std::string &name, const std::string &value) |
Sets a JavaScript member. | |
virtual std::string | javaScriptMember (const std::string &name) const |
Returns the value of a JavaScript member. | |
virtual void | callJavaScriptMember (const std::string &name, const std::string &args) |
Calls a JavaScript member. | |
virtual void | load () |
Loads content just before the widget is used. | |
virtual bool | loaded () const |
Returns whether this widget has been loaded. | |
virtual void | setTabIndex (int index) |
Sets the tab index. | |
virtual int | tabIndex () const |
Returns the tab index. | |
virtual void | setId (const std::string &id) |
Sets the CSS Id. | |
virtual WWidget * | find (const std::string &name) |
Finds a descendent widget by name. | |
virtual void | setSelectable (bool selectable) |
Sets as selectable. | |
virtual void | doJavaScript (const std::string &js) |
Executes the given JavaScript statements when the widget is rendered or updated. | |
virtual void | propagateSetEnabled (bool enabled) |
Propagates that a widget was enabled or disabled through children. | |
![]() | |
virtual | ~WWidget () |
Destructor. | |
WWidget * | parent () const |
Returns the parent widget. | |
void | setWidth (const WLength &width) |
Sets the width. | |
void | setHeight (const WLength &height) |
Sets the height. | |
virtual void | positionAt (const WWidget *widget, Orientation orientation=Vertical) |
Positions a widget next to another widget. | |
virtual void | toggleStyleClass (const WString &styleClass, bool add, bool force=false) |
Toggles a CSS style class. | |
std::string | jsRef () const |
Returns a JavaScript expression to the corresponding DOM node. | |
virtual void | acceptDrops (const std::string &mimeType, const WString &hoverStyleClass=WString()) |
Sets a mime type to be accepted for dropping. | |
virtual void | stopAcceptDrops (const std::string &mimeType) |
Indicates that a mime type is no longer accepted for dropping. | |
virtual void | htmlText (std::ostream &out) |
Streams the (X)HTML representation. | |
bool | isRendered () const |
Returns whether the widget is rendered. | |
void | hide () |
Hides the widget. | |
void | animateHide (const WAnimation &animation) |
Hides the widget using an animation. | |
void | show () |
Shows the widget. | |
void | animateShow (const WAnimation &animation) |
Shows the widget using an animation. | |
void | enable () |
Enables the widget. | |
void | disable () |
Disable thes widget. | |
bool | layoutSizeAware () const |
Returns whether the widget is layout size aware. | |
![]() | |
WObject (WObject *parent=0) | |
Create a WObject with a given parent object. | |
virtual | ~WObject () |
Destructor. | |
void | setObjectName (const std::string &name) |
Sets an object name. | |
virtual std::string | objectName () const |
Returns the object name. | |
void | resetLearnedSlots () |
Resets learned stateless slot implementations. | |
template<class T > | |
void | resetLearnedSlot (void(T::*method)()) |
Resets a learned stateless slot implementation. | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)()) |
Declares a slot to be stateless and learn client-side behaviour on first invocation. | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)()) |
Declares a slot to be stateless and learn client-side behaviour in advance. | |
template<class T > | |
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode) |
Provides a JavaScript implementation for a method. | |
void | addChild (WObject *child) |
Adds a child object. | |
virtual void | removeChild (WObject *child) |
Removes a child object. | |
const std::vector< WObject * > & | children () const |
Returns the children. |
Static Public Member Functions | |
static std::string | generateMatcherJS (const Options &options) |
Creates a standard matcher JavaScript function. | |
static std::string | generateReplacerJS (const Options &options) |
Creates a standard replacer JavaScript function. |
Additional Inherited Members | |
![]() | |
virtual void | enableAjax () |
Progresses to an Ajax-enabled widget. | |
void | setImplementation (WWidget *widget) |
Set the implementation widget. | |
WWidget * | implementation () |
Get the implementation widget. | |
virtual int | boxPadding (Orientation orientation) const |
Returns the widget's built-in padding. | |
virtual int | boxBorder (Orientation orientation) const |
Returns the widget's built-in border width. | |
![]() | |
static WObject * | sender () |
Returns the sender of the current slot call. |
A widget which popups to assist in editing a textarea or lineedit.
This widget may be associated with one or more WFormWidgets (typically a WLineEdit or a WTextArea).
The popup provides the user with suggestions to enter input. The popup can be used by one or more editors, using forEdit(). The popup will show when the user starts editing the edit field, or when the user opens the suggestions explicitly using a drop down icon or with the down key. The popup positions itself intelligently just below or just on top of the edit field. It offers a list of suggestions that match in some way with the current edit field, and dynamically adjusts this list. The implementation for matching individual suggestions with the current text is provided through a JavaScript function. This function may also highlight part(s) of the suggestions to provide feed-back on how they match.
WSuggestionPopup is an MVC view class, using a simple WStringListModel by default. You can set a custom model using setModel(). The model can provide different text for the suggestion text (Wt::DisplayRole) and value (Wt::UserRole). The member methods clearSuggestions() and addSuggestion() manipulate this model. Note that a WStringListModel does not support Wt::UserRole data, so you may need to use a WStandardItemModel instead.
By default, the popup implements all filtering client-side. To support large datasets, you may enable server-side filtering of suggestions based on the input. The server-side filtering may provide a coarse filtering using a fixed size prefix of the entered text, and complement the client-side filtering. To enable server-side filtering, use setFilterLength() and listen to filter notification using the modelFilter() signal. Whenever a filter event is generated you can adjust the model's content according to the filter (e.g. using a WSortFilterProxyModel). By using setMaximumSize() you can also limit the maximum height of the popup, in which case scrolling is supported (similar to a combo-box).
The class is initialized with an Options struct which configures how suggestion filtering and result editing is done. Alternatively, you can provide two JavaScript functions, one for filtering the suggestions, and one for editing the value of the textarea when a suggestion is selected.
The matcherJS function must have the following JavaScript signature:
The replacerJS function that edits the value has the following JavaScript signature.
To style the suggestions, you should style the <span> element inside this widget, and the <span>."sel" element to style the current selection.
Usage example:
A screenshot of this example:
![]()
An example WSuggestionPopup (default) | ![]()
An example WSuggestionPopup (polished) |
The suggestion popup is styled by the current CSS theme. The look can be overridden using the Wt-suggest
CSS class and the following selectors:
.Wt-suggest .content div : A suggestion element .Wt-suggest .sel : A selected suggestion element
When using the DropDownIcon trigger, an additional style class is provided for the edit field: Wt-suggest-dropdown
, which renders the icon to the right inside the edit field. This class may be used to customize how the drop down icon is rendered.
Enumeration that defines a trigger for showing the popup.
Editing |
Shows popup when the user starts editing. The popup is shown when the currently edited text has a length longer than the filter length. |
DropDownIcon |
Shows popup when user clicks a drop down icon. The lineedit is modified to show a drop down icon, and clicking the icon shows the suggestions, very much like a WComboCox. |
Wt::WSuggestionPopup::WSuggestionPopup | ( | const Options & | options, |
WContainerWidget * | parent = 0 |
||
) |
Creates a suggestion popup.
The popup using a standard matcher and replacer implementation that is configured using the provided options
.
Wt::WSuggestionPopup::WSuggestionPopup | ( | const std::string & | matcherJS, |
const std::string & | replacerJS, | ||
WContainerWidget * | parent = 0 |
||
) |
Creates a suggestion popup with given matcherJS and replacerJS.
See supra for the expected signature of the matcher and replace JavaScript functions.
Signal<int, WFormWidget *>& Wt::WSuggestionPopup::activated | ( | ) |
Signal emitted when a suggestion was selected.
The selected item is passed as the first argument and the editor as the second.
void Wt::WSuggestionPopup::addSuggestion | ( | const WString & | suggestionText, |
const WString & | suggestionValue | ||
) |
Adds a new suggestion.
This adds an entry to the underlying model. The suggestionText
is set as Wt::DisplayRole and the suggestionValue
(which is inserted into the edit field on selection) is set as Wt::UserRole.
void Wt::WSuggestionPopup::clearSuggestions | ( | ) |
int Wt::WSuggestionPopup::defaultIndex | ( | ) | const |
Returns the default value.
int Wt::WSuggestionPopup::filterLength | ( | ) | const |
Returns the filter length.
Signal that indicates that the model should be filtered.
The argument is the initial input. When Editing is used as edit trigger, its length will always equal the filterLength(). When DropDownIcon is used as edit trigger, the input length may be less than filterLength(), and the the signal will be called repeatedly as the user provides more input.
For example, if you are using a WSortFilterProxyModel, you could react to this signal with:
void Wt::WSuggestionPopup::forEdit | ( | WFormWidget * | edit, |
WFlags< PopupTrigger > | popupTriggers = Editing |
||
) |
Lets this suggestion popup assist in editing an edit field.
A single suggestion popup may assist in several edits by repeated calls of this method.
The popupTriggers
control how editing is triggered (either by the user editing the field by entering keys or by an explicit drop down menu that is shown inside the edit).
|
static |
Creates a standard matcher JavaScript function.
This returns a JavaScript function that provides a standard implementation for the matching input, based on the given options
.
|
static |
Creates a standard replacer JavaScript function.
This returns a JavaScript function that provides a standard implementation for the matching input, based on the given options
.
WAbstractItemModel* Wt::WSuggestionPopup::model | ( | ) | const |
Returns the data model.
void Wt::WSuggestionPopup::removeEdit | ( | WFormWidget * | edit | ) |
Removes the edit field from the list of assisted editors.
The editor will no longer be assisted by this popup widget.
void Wt::WSuggestionPopup::setDefaultIndex | ( | int | row | ) |
Sets a default selected value.
row
is the model row that is selected by default (only if it matches the current input).
The default value is -1, indicating no default.
void Wt::WSuggestionPopup::setFilterLength | ( | int | count | ) |
Sets the minimum input length before showing the popup.
When the user has typed this much characters, filterModel() is emitted which allows you to filter the model based on the initial input.
The default value is 0, which has the effect of always showing the entire model.
A value of -1 indicates that server-side filtering is continuously applied, no matter the length of the text input.
void Wt::WSuggestionPopup::setGlobalPopup | ( | bool | global | ) |
Controls how the suggestion popup is positioned.
When global
is true
, then the popup will position itself globally. This avoids that the popup is affected by enclosing parents with overflow settings that clip the popup. This makes the popup however no longer follow the popup line edit when this line edit moves.
The default is false
.
Sets a maximum size.
Specifies a maximum size for this widget, setting CSS max-width
and max-height
properties.
The default the maximum width and height are WLength::Auto, indicating no maximum size. A WLength::Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.
When the widget is a container widget that contains a layout manager, then setting a maximum size will have the effect of letting the size of the container to reflect the preferred size of the contents (rather than constraining the size of the children based on the size of the container), up to the specified maximum size.
Reimplemented from Wt::WCompositeWidget.
Sets a minimum size.
Specifies a minimum size for this widget, setting CSS min-width
and min-height
properties.
The default minimum width and height is 0. The special value WLength::Auto indicates that the initial width is used as minimum size. A WLength::Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.
When the widget is inserted in a layout manager, then the minimum size will be taken into account.
Reimplemented from Wt::WCompositeWidget.
void Wt::WSuggestionPopup::setModel | ( | WAbstractItemModel * | model | ) |
Sets the model to be used for the suggestions.
The model
may not be 0
, and ownership of the model is not transferred.
The default value is a WStringListModel that is owned by the suggestion popup.
The Wt::DisplayRole is used for the suggestion text. The Wt::UserRole is used for the suggestion value, unless empty, in which case the suggestion text is used as value.
Note that since the default WStringListModel does not support UserRole data, you will want to change it to a more general model (e.g. WStandardItemModel) if you want suggestion values that are different from display values.
void Wt::WSuggestionPopup::setModelColumn | ( | int | index | ) |
Sets the column in the model to be used for the items.
The column index
in the model will be used to retrieve data.
The default value is 0.
void Wt::WSuggestionPopup::showAt | ( | WFormWidget * | edit | ) |
Shows the suggestion popup at an edit field.
This is equivalent to the user triggering the suggestion popup to be shown.