Coverage Report

Created: 2026-09-23 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WStringListModel.h
Line
Count
Source
1
// This may look like C code, but it's really -*- C++ -*-
2
/*
3
 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4
 *
5
 * See the LICENSE file for terms of use.
6
 */
7
#ifndef WSTRING_LIST_MODEL_H_
8
#define WSTRING_LIST_MODEL_H_
9
10
#include <Wt/WAbstractListModel.h>
11
12
namespace Wt {
13
14
/*! \class WStringListModel Wt/WStringListModel.h Wt/WStringListModel.h
15
 *  \brief An model that manages a list of strings.
16
 *
17
 * This model only manages a unidimensional list of items and is
18
 * optimized for usage by view widgets such as combo-boxes.
19
 *
20
 * It supports all features of a typical item model, including data
21
 * for multiple roles, editing and addition and removal of data rows.
22
 *
23
 * You can populate the model by passing a list of strings to its
24
 * consructor, or by using the setStringList() method. You can set or
25
 * retrieve data using the setData() and data() methods, and add or
26
 * remove data using the insertRows() and removeRows() methods.
27
 *
28
 * \sa WComboBox, WSelectionBox
29
 *
30
 * \ingroup modelview
31
 */
32
class WT_API WStringListModel : public WAbstractListModel
33
{
34
public:
35
  /*! \brief Creates a new empty string list model.
36
   */
37
  WStringListModel();
38
39
  /*! \brief Creates a new string list model.
40
   */
41
  WStringListModel(const std::vector<WString>& strings);
42
43
  /*! \brief Destructor.
44
   */
45
  ~WStringListModel();
46
47
  /*! \brief Sets a new string list.
48
   *
49
   * Replaces the current string list with a new list.
50
   *
51
   * \sa dataChanged()
52
   * \sa addString()
53
   */
54
  void setStringList(const std::vector<WString>& strings);
55
56
  /*! \brief Inserts a string.
57
   *
58
   * \sa setStringList()
59
   */
60
  void insertString(int row, const WString& string);
61
62
  /*! \brief Adds a string.
63
   *
64
   * \sa setStringList()
65
   */
66
  void addString(const WString& string);
67
68
  /*! \brief Returns the string list.
69
   *
70
   * \sa setStringList()
71
   */
72
0
  const std::vector<WString>& stringList() const { return displayData_; }
73
74
  /*! \brief Sets model flags for an item.
75
   *
76
   * The default item flags are \link Wt::ItemFlag::Selectable
77
   * ItemFlag::Selectable\endlink | \link Wt::ItemFlag::Editable
78
   * ItemFlag::Editable\endlink.
79
   */
80
  void setFlags(int index, WFlags<ItemFlag> flags);
81
82
  /*! \brief Returns the flags for an item.
83
   *
84
   * This method is reimplemented to return flags set in setFlags().
85
   *
86
   * \sa setFlags()
87
   */
88
  virtual WFlags<ItemFlag> flags(const WModelIndex& index) const override;
89
90
  using WAbstractListModel::setData;
91
92
  /* \brief Returns the model index.
93
   *
94
   * Since the model only contains a single row, the \p column will
95
   * always be changed to \p 0.
96
   *
97
   * An invalid WModelIndex is returned when the \p row is out of
98
   * bounds.
99
   */
100
  WModelIndex index(int row, int column = 0, const WModelIndex& parent = WModelIndex()) const override;
101
102
  virtual bool setData(const WModelIndex& index, const cpp17::any& value,
103
                       ItemDataRole role = ItemDataRole::Edit) override;
104
105
  virtual cpp17::any data(const WModelIndex& index, ItemDataRole role = ItemDataRole::Display)
106
    const override;
107
108
  virtual int rowCount(const WModelIndex& parent = WModelIndex())
109
    const override;
110
111
  virtual bool insertRows(int row, int count,
112
                          const WModelIndex& parent = WModelIndex()) override;
113
114
  virtual bool removeRows(int row, int count,
115
                          const WModelIndex& parent = WModelIndex()) override;
116
117
  /*! \brief Sorts the model.
118
   *
119
   * The \p column is ignored, as this model only holds a singular list.
120
   * It will always result in the first (and only) column of the model
121
   * being sorted.
122
   */
123
  virtual void sort(int column,
124
                    SortOrder order = SortOrder::Ascending) override;
125
126
private:
127
#ifndef WT_TARGET_JAVA
128
  typedef std::map<ItemDataRole, cpp17::any> DataMap;
129
#else
130
  typedef std::treemap<ItemDataRole, cpp17::any> DataMap;
131
#endif
132
133
  std::vector<WString> displayData_;
134
  std::vector<DataMap> *otherData_;
135
  std::vector<WFlags<ItemFlag> > flags_;
136
137
  void createOtherDataMap();
138
};
139
140
}
141
142
#endif // WSTRING_LIST_MODEL_H_