BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
qdesigner_internal::WidgetBoxCategoryModel Class Reference
Inheritance diagram for qdesigner_internal::WidgetBoxCategoryModel:
[legend]
Collaboration diagram for qdesigner_internal::WidgetBoxCategoryModel:
[legend]

Public Member Functions

 WidgetBoxCategoryModel (SampleDesignerInterface *core, QObject *parent=0)
 
void addWidget (const QDesignerWidgetBoxInterface::Widget &widget, const QIcon &icon, bool editable)
 
QDesignerWidgetBoxInterface::Category category () const
 
virtual QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const
 
virtual Qt::ItemFlags flags (const QModelIndex &index) const
 
int indexOfWidget (const QString &name)
 
bool removeCustomWidgets ()
 
virtual bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex())
 
virtual int rowCount (const QModelIndex &parent=QModelIndex()) const
 
virtual bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
 
void setViewMode (QListView::ViewMode vm)
 
QListView::ViewMode viewMode () const
 
QDesignerWidgetBoxInterface::Widget widgetAt (const QModelIndex &index) const
 
QDesignerWidgetBoxInterface::Widget widgetAt (int row) const
 

Private Types

typedef QList< WidgetBoxCategoryEntryWidgetBoxCategoryEntrys
 

Private Attributes

QRegExp m_classNameRegExp
 
WidgetBoxCategoryEntrys m_items
 
QListView::ViewMode m_viewMode
 

Detailed Description

Definition at line 122 of file widgetboxcategorylistview.cpp.

Member Typedef Documentation

◆ WidgetBoxCategoryEntrys

Constructor & Destructor Documentation

◆ WidgetBoxCategoryModel()

qdesigner_internal::WidgetBoxCategoryModel::WidgetBoxCategoryModel ( SampleDesignerInterface core,
QObject *  parent = 0 
)
explicit

Definition at line 161 of file widgetboxcategorylistview.cpp.

162  : QAbstractListModel(parent)
163  ,
164 #if QT_VERSION >= 0x050000
165  m_classNameRegExp("<widget +class *= *\"([^\"]+)\"")
166  ,
167 #else
168  m_classNameRegExp(QString("<widget +class *= *\"([^\"]+)\""))
169  ,
170 #endif
171 
172  // m_core(core),
173  m_viewMode(QListView::ListMode)
174 {
175  ASSERT(m_classNameRegExp.isValid());
176  Q_UNUSED(core);
177 }
#define ASSERT(condition)
Definition: Assert.h:31

References ASSERT, and m_classNameRegExp.

Member Function Documentation

◆ addWidget()

void qdesigner_internal::WidgetBoxCategoryModel::addWidget ( const QDesignerWidgetBoxInterface::Widget &  widget,
const QIcon &  icon,
bool  editable 
)

Definition at line 233 of file widgetboxcategorylistview.cpp.

235 {
236  // build item. Filter on name + class name if it is different and not a layout.
237  QString filter = widget.name();
238  if (!filter.contains("Layout") && m_classNameRegExp.indexIn(widget.domXml()) != -1) {
239  const QString className = m_classNameRegExp.cap(1);
240  if (!filter.contains(className))
241  filter += className;
242  }
243  WidgetBoxCategoryEntry item(widget, filter, icon, editable);
244 
245  QXmlStreamReader reader(widget.domXml());
246  while (!reader.atEnd()) {
247  switch (reader.readNext()) {
248  case QXmlStreamReader::StartElement: {
249  const QStringRef tag = reader.name();
250  if (tag == "widget") {
251  const QXmlStreamAttributes attributes = reader.attributes();
252  QString className = attributes.value("class").toString();
253  QString toolTip = ToolTipDataBase::widgetboxToolTip(className);
254  if (!toolTip.isEmpty())
255  item.toolTip = toolTip;
256  }
257  break;
258  }
259  default:
260  break;
261  }
262  }
263 
264  // const QDesignerWidgetDataBaseInterface *db = m_core->widgetDataBase();
265  // const int dbIndex = db->indexOfClassName(widget.name());
266  // if (dbIndex != -1) {
267  // const QDesignerWidgetDataBaseItemInterface *dbItem = db->item(dbIndex);
268  // const QString toolTip = dbItem->toolTip();
269  // if (!toolTip.isEmpty())
270  // item.toolTip = toolTip;
271  // const QString whatsThis = dbItem->whatsThis();
272  // if (!whatsThis.isEmpty())
273  // item.whatsThis = whatsThis;
274  // }
275  // insert
276  const int row = m_items.size();
277  beginInsertRows(QModelIndex(), row, row);
278  m_items.push_back(item);
279  endInsertRows();
280 }
static QString widgetboxToolTip(const QString &className)

References m_classNameRegExp, m_items, qdesigner_internal::WidgetBoxCategoryEntry::toolTip, and ToolTipDataBase::widgetboxToolTip().

Referenced by qdesigner_internal::WidgetBoxCategoryListView::addWidget().

Here is the call graph for this function:

◆ category()

QDesignerWidgetBoxInterface::Category qdesigner_internal::WidgetBoxCategoryModel::category ( ) const

Definition at line 205 of file widgetboxcategorylistview.cpp.

206 {
207  QDesignerWidgetBoxInterface::Category rc;
208  const WidgetBoxCategoryEntrys::const_iterator cend = m_items.constEnd();
209  for (WidgetBoxCategoryEntrys::const_iterator it = m_items.constBegin(); it != cend; ++it)
210  rc.addWidget(it->widget);
211  return rc;
212 }

References m_items.

Referenced by qdesigner_internal::WidgetBoxCategoryListView::category().

◆ data()

QVariant qdesigner_internal::WidgetBoxCategoryModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
virtual

Definition at line 282 of file widgetboxcategorylistview.cpp.

283 {
284  const int row = index.row();
285  if (row < 0 || row >= m_items.size())
286  return QVariant();
287 
288  const WidgetBoxCategoryEntry& item = m_items.at(row);
289  switch (role) {
290  case Qt::DisplayRole:
291  // No text in icon mode
292  return QVariant(m_viewMode == QListView::ListMode ? item.widget.name() : QString());
293  case Qt::DecorationRole:
294  return QVariant(item.icon);
295  case Qt::EditRole:
296  return QVariant(item.widget.name());
297  case Qt::ToolTipRole: {
298  if (m_viewMode == QListView::ListMode)
299  return QVariant(item.toolTip);
300  // Icon mode tooltip should contain the class name
301  QString tt = item.widget.name();
302  if (!item.toolTip.isEmpty()) {
303  tt += QLatin1Char('\n');
304  tt += item.toolTip;
305  }
306  return QVariant(tt);
307  }
308  case Qt::WhatsThisRole:
309  return QVariant(item.whatsThis);
310  case FILTER_ROLE:
311  return item.filter;
312  }
313  return QVariant();
314 }
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.

References SessionItemUtils::DecorationRole(), qdesigner_internal::WidgetBoxCategoryEntry::filter, FILTER_ROLE, qdesigner_internal::WidgetBoxCategoryEntry::icon, m_items, m_viewMode, qdesigner_internal::WidgetBoxCategoryEntry::toolTip, SessionItemUtils::ToolTipRole(), qdesigner_internal::WidgetBoxCategoryEntry::whatsThis, and qdesigner_internal::WidgetBoxCategoryEntry::widget.

Here is the call graph for this function:

◆ flags()

Qt::ItemFlags qdesigner_internal::WidgetBoxCategoryModel::flags ( const QModelIndex &  index) const
virtual

Definition at line 337 of file widgetboxcategorylistview.cpp.

338 {
339  Qt::ItemFlags rc = Qt::ItemIsEnabled;
340  const int row = index.row();
341  if (row >= 0 && row < m_items.size())
342  if (m_items.at(row).editable) {
343  rc |= Qt::ItemIsSelectable;
344  // Can change name in list mode only
345  if (m_viewMode == QListView::ListMode)
346  rc |= Qt::ItemIsEditable;
347  }
348  return rc;
349 }

References m_items, and m_viewMode.

◆ indexOfWidget()

int qdesigner_internal::WidgetBoxCategoryModel::indexOfWidget ( const QString &  name)

Definition at line 196 of file widgetboxcategorylistview.cpp.

197 {
198  const int count = m_items.size();
199  for (int i = 0; i < count; i++)
200  if (m_items.at(i).widget.name() == name)
201  return i;
202  return -1;
203 }
QString const & name(EShape k)
Definition: particles.cpp:21

References m_items, and RealSpace::Particles::name().

Referenced by qdesigner_internal::WidgetBoxCategoryListView::containsWidget().

Here is the call graph for this function:

◆ removeCustomWidgets()

bool qdesigner_internal::WidgetBoxCategoryModel::removeCustomWidgets ( )

Definition at line 214 of file widgetboxcategorylistview.cpp.

215 {
216  // Typically, we are a whole category of custom widgets, so, remove all
217  // and do reset.
218  bool changed = false;
219  for (WidgetBoxCategoryEntrys::iterator it = m_items.begin(); it != m_items.end();)
220  if (it->widget.type() == QDesignerWidgetBoxInterface::Widget::Custom) {
221  if (!changed)
222  beginResetModel();
223  it = m_items.erase(it);
224  changed = true;
225  } else {
226  ++it;
227  }
228  if (changed)
229  endResetModel();
230  return changed;
231 }

References m_items.

Referenced by qdesigner_internal::WidgetBoxCategoryListView::removeCustomWidgets().

◆ removeRows()

bool qdesigner_internal::WidgetBoxCategoryModel::removeRows ( int  row,
int  count,
const QModelIndex &  parent = QModelIndex() 
)
virtual

Definition at line 356 of file widgetboxcategorylistview.cpp.

357 {
358  if (row < 0 || count < 1)
359  return false;
360  const int size = m_items.size();
361  const int last = row + count - 1;
362  if (row >= size || last >= size)
363  return false;
364  beginRemoveRows(parent, row, last);
365  for (int r = last; r >= row; r--)
366  m_items.removeAt(r);
367  endRemoveRows();
368  return true;
369 }

References m_items.

◆ rowCount()

int qdesigner_internal::WidgetBoxCategoryModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
virtual

◆ setData()

bool qdesigner_internal::WidgetBoxCategoryModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role = Qt::EditRole 
)
virtual

Definition at line 316 of file widgetboxcategorylistview.cpp.

317 {
318  const int row = index.row();
319  if (role != Qt::EditRole || row < 0 || row >= m_items.size()
320  || value.type() != QVariant::String)
321  return false;
322  // Set name and adapt Xml
323  WidgetBoxCategoryEntry& item = m_items[row];
324  const QString newName = value.toString();
325  item.widget.setName(newName);
326 
327  const QDomDocument doc = stringToDom(WidgetBoxCategoryListView::widgetDomXml(item.widget));
328  QDomElement widget_elt = doc.firstChildElement(QLatin1String(widgetElementC));
329  if (!widget_elt.isNull()) {
330  widget_elt.setAttribute(QLatin1String(nameAttributeC), newName);
331  item.widget.setDomXml(domToString(widget_elt));
332  }
333  emit dataChanged(index, index);
334  return true;
335 }
static QString widgetDomXml(const QDesignerWidgetBoxInterface::Widget &widget)
static QDomDocument stringToDom(const QString &xml)
static QString domToString(const QDomElement &elt)
static const char * nameAttributeC
static const char * widgetElementC

References domToString(), m_items, nameAttributeC, stringToDom(), qdesigner_internal::WidgetBoxCategoryEntry::widget, qdesigner_internal::WidgetBoxCategoryListView::widgetDomXml(), and widgetElementC.

Here is the call graph for this function:

◆ setViewMode()

void qdesigner_internal::WidgetBoxCategoryModel::setViewMode ( QListView::ViewMode  vm)

Definition at line 184 of file widgetboxcategorylistview.cpp.

185 {
186  if (m_viewMode == vm)
187  return;
188  const bool empty = m_items.isEmpty();
189  if (!empty)
190  beginResetModel();
191  m_viewMode = vm;
192  if (!empty)
193  endResetModel();
194 }

References m_items, and m_viewMode.

Referenced by qdesigner_internal::WidgetBoxCategoryListView::setViewMode().

◆ viewMode()

QListView::ViewMode qdesigner_internal::WidgetBoxCategoryModel::viewMode ( ) const

Definition at line 179 of file widgetboxcategorylistview.cpp.

180 {
181  return m_viewMode;
182 }

References m_viewMode.

◆ widgetAt() [1/2]

QDesignerWidgetBoxInterface::Widget qdesigner_internal::WidgetBoxCategoryModel::widgetAt ( const QModelIndex &  index) const

Definition at line 371 of file widgetboxcategorylistview.cpp.

372 {
373  return widgetAt(index.row());
374 }
QDesignerWidgetBoxInterface::Widget widgetAt(const QModelIndex &index) const

Referenced by qdesigner_internal::WidgetBoxCategoryListView::slotPressed(), and qdesigner_internal::WidgetBoxCategoryListView::widgetAt().

◆ widgetAt() [2/2]

QDesignerWidgetBoxInterface::Widget qdesigner_internal::WidgetBoxCategoryModel::widgetAt ( int  row) const

Definition at line 376 of file widgetboxcategorylistview.cpp.

377 {
378  if (row < 0 || row >= m_items.size())
379  return QDesignerWidgetBoxInterface::Widget();
380  return m_items.at(row).widget;
381 }

References m_items.

Member Data Documentation

◆ m_classNameRegExp

QRegExp qdesigner_internal::WidgetBoxCategoryModel::m_classNameRegExp
private

Definition at line 152 of file widgetboxcategorylistview.cpp.

Referenced by WidgetBoxCategoryModel(), and addWidget().

◆ m_items

WidgetBoxCategoryEntrys qdesigner_internal::WidgetBoxCategoryModel::m_items
private

◆ m_viewMode

QListView::ViewMode qdesigner_internal::WidgetBoxCategoryModel::m_viewMode
private

Definition at line 156 of file widgetboxcategorylistview.cpp.

Referenced by data(), flags(), setViewMode(), and viewMode().


The documentation for this class was generated from the following file: