BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
qdesigner_internal::WidgetBoxTreeWidget Class Reference

WidgetBoxTreeWidget: A tree of categories. More...

Inheritance diagram for qdesigner_internal::WidgetBoxTreeWidget:
[legend]
Collaboration diagram for qdesigner_internal::WidgetBoxTreeWidget:
[legend]

Public Types

typedef QDesignerWidgetBoxInterface::Category Category
 
typedef QDesignerWidgetBoxInterface::CategoryList CategoryList
 
typedef QDesignerWidgetBoxInterface::Widget Widget
 

Public Slots

void filter (const QString &)
 

Signals

void pressed (const QString name, const QString dom_xml, const QPoint &global_mouse_pos)
 

Public Member Functions

 WidgetBoxTreeWidget (SampleDesignerInterface *core, QWidget *parent=0)
 
virtual ~WidgetBoxTreeWidget ()
 
void addCategory (const Category &cat)
 
void addWidget (int cat_idx, const Widget &wgt)
 
Category category (int cat_idx) const
 
int categoryCount () const
 
void dropWidgets (const QList< QDesignerDnDItemInterface * > &item_list)
 
QString fileName () const
 
QIcon iconForWidget (QString iconName) const
 
bool load (QDesignerWidgetBox::LoadMode loadMode)
 
bool loadContents (const QString &contents)
 
void removeCategory (int cat_idx)
 
void removeWidget (int cat_idx, int wgt_idx)
 
bool save ()
 
void setFileName (const QString &file_name)
 
Widget widget (int cat_idx, int wgt_idx) const
 
int widgetCount (int cat_idx) const
 

Protected Member Functions

virtual void contextMenuEvent (QContextMenuEvent *e)
 
virtual void resizeEvent (QResizeEvent *e)
 

Private Types

typedef QHash< QString, QIcon > IconCache
 

Private Slots

void deleteScratchpad ()
 
void handleMousePress (QTreeWidgetItem *item)
 
void slotIconMode ()
 
void slotLastScratchPadItemDeleted ()
 
void slotListMode ()
 
void slotSave ()
 
void slotScratchPadItemDeleted ()
 

Private Member Functions

WidgetBoxCategoryListViewaddCategoryView (QTreeWidgetItem *parent, bool iconMode)
 
void addCustomCategories (bool replace)
 
void adjustSubListSize (QTreeWidgetItem *cat_item)
 
WidgetBoxCategoryListViewcategoryViewAt (int idx) const
 
int ensureScratchpad ()
 
int indexOfCategory (const QString &name) const
 
int indexOfScratchpad () const
 
CategoryList loadCustomCategoryList () const
 
void restoreExpandedState ()
 
void saveExpandedState () const
 
void updateViewMode ()
 
void writeCategories (QXmlStreamWriter &writer, const CategoryList &cat_list) const
 

Static Private Member Functions

static bool readCategories (const QString &fileName, const QString &xml, CategoryList *cats, QString *errorMessage)
 
static bool readWidget (Widget *w, const QString &xml, QXmlStreamReader &r)
 

Private Attributes

SampleDesignerInterfacem_core
 
QString m_file_name
 
bool m_iconMode
 
IconCache m_pluginIcons
 
QTimer * m_scratchPadDeleteTimer
 

Detailed Description

WidgetBoxTreeWidget: A tree of categories.

Definition at line 68 of file widgetboxtreewidget.h.

Member Typedef Documentation

◆ Category

typedef QDesignerWidgetBoxInterface::Category qdesigner_internal::WidgetBoxTreeWidget::Category

Definition at line 73 of file widgetboxtreewidget.h.

◆ CategoryList

typedef QDesignerWidgetBoxInterface::CategoryList qdesigner_internal::WidgetBoxTreeWidget::CategoryList

Definition at line 74 of file widgetboxtreewidget.h.

◆ IconCache

typedef QHash<QString, QIcon> qdesigner_internal::WidgetBoxTreeWidget::IconCache
private

Definition at line 141 of file widgetboxtreewidget.h.

◆ Widget

typedef QDesignerWidgetBoxInterface::Widget qdesigner_internal::WidgetBoxTreeWidget::Widget

Definition at line 72 of file widgetboxtreewidget.h.

Constructor & Destructor Documentation

◆ WidgetBoxTreeWidget()

qdesigner_internal::WidgetBoxTreeWidget::WidgetBoxTreeWidget ( SampleDesignerInterface core,
QWidget *  parent = 0 
)
explicit

Definition at line 111 of file widgetboxtreewidget.cpp.

112  : QTreeWidget(parent), m_core(core), m_iconMode(false), m_scratchPadDeleteTimer(nullptr)
113 {
114  setFocusPolicy(Qt::NoFocus);
115  setIndentation(0);
116  setRootIsDecorated(false);
117  setColumnCount(1);
118  header()->hide();
119 #if QT_VERSION >= 0x050000
120  header()->setSectionResizeMode(QHeaderView::Stretch);
121 #endif
122  setTextElideMode(Qt::ElideMiddle);
123  setVerticalScrollMode(ScrollPerPixel);
124 
125  setItemDelegate(new SheetDelegate(this, this));
126 
127  connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this,
128  SLOT(handleMousePress(QTreeWidgetItem*)));
129 }
void handleMousePress(QTreeWidgetItem *item)

References handleMousePress().

Here is the call graph for this function:

◆ ~WidgetBoxTreeWidget()

qdesigner_internal::WidgetBoxTreeWidget::~WidgetBoxTreeWidget ( )
virtual

Definition at line 165 of file widgetboxtreewidget.cpp.

166 {
168 }

References saveExpandedState().

Here is the call graph for this function:

Member Function Documentation

◆ addCategory()

void qdesigner_internal::WidgetBoxTreeWidget::addCategory ( const Category cat)

Definition at line 630 of file widgetboxtreewidget.cpp.

631 {
632  if (cat.widgetCount() == 0)
633  return;
634 
635  const bool isScratchPad = cat.type() == Category::Scratchpad;
636  WidgetBoxCategoryListView* categoryView;
637  QTreeWidgetItem* cat_item;
638 
639  if (isScratchPad) {
640  const int idx = ensureScratchpad();
641  categoryView = categoryViewAt(idx);
642  cat_item = topLevelItem(idx);
643  } else {
644  const int existingIndex = indexOfCategory(cat.name());
645  if (existingIndex == -1) {
646  cat_item = new QTreeWidgetItem();
647  cat_item->setText(0, cat.name());
648  setTopLevelRole(NORMAL_ITEM, cat_item);
649  // insert before scratchpad
650  const int scratchPadIndex = indexOfScratchpad();
651  if (scratchPadIndex == -1) {
652  addTopLevelItem(cat_item);
653  } else {
654  insertTopLevelItem(scratchPadIndex, cat_item);
655  }
656 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
657  cat_item->setExpanded(true);
658 #else
659  setItemExpanded(cat_item, true);
660 #endif
661 
662  categoryView = addCategoryView(cat_item, m_iconMode);
663  } else {
664  categoryView = categoryViewAt(existingIndex);
665  cat_item = topLevelItem(existingIndex);
666  }
667  }
668  // The same categories are read from the file $HOME, avoid duplicates
669  const int widgetCount = cat.widgetCount();
670  for (int i = 0; i < widgetCount; ++i) {
671  const Widget w = cat.widget(i);
672  if (!categoryView->containsWidget(w.name()))
673  categoryView->addWidget(w, iconForWidget(w.iconName()), isScratchPad);
674  }
675  adjustSubListSize(cat_item);
676 }
void adjustSubListSize(QTreeWidgetItem *cat_item)
WidgetBoxCategoryListView * addCategoryView(QTreeWidgetItem *parent, bool iconMode)
WidgetBoxCategoryListView * categoryViewAt(int idx) const
QIcon iconForWidget(QString iconName) const
QDesignerWidgetBoxInterface::Widget Widget
int indexOfCategory(const QString &name) const
static QT_BEGIN_NAMESPACE void setTopLevelRole(ETopLevelRole tlr, QTreeWidgetItem *item)
@ NORMAL_ITEM

References addCategoryView(), qdesigner_internal::WidgetBoxCategoryListView::addWidget(), adjustSubListSize(), categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::containsWidget(), ensureScratchpad(), iconForWidget(), indexOfCategory(), indexOfScratchpad(), m_iconMode, NORMAL_ITEM, setTopLevelRole(), and widgetCount().

Referenced by qdesigner_internal::WidgetBox::addCategory(), addCustomCategories(), and loadContents().

Here is the call graph for this function:

◆ addCategoryView()

WidgetBoxCategoryListView * qdesigner_internal::WidgetBoxTreeWidget::addCategoryView ( QTreeWidgetItem *  parent,
bool  iconMode 
)
private

Definition at line 240 of file widgetboxtreewidget.cpp.

242 {
243  QTreeWidgetItem* embed_item = new QTreeWidgetItem(parent);
244  embed_item->setFlags(Qt::ItemIsEnabled);
245  WidgetBoxCategoryListView* categoryView = new WidgetBoxCategoryListView(m_core, this);
246  categoryView->setViewMode(iconMode ? QListView::IconMode : QListView::ListMode);
247  connect(categoryView, SIGNAL(scratchPadChanged()), this, SLOT(slotSave()));
248  connect(categoryView, SIGNAL(pressed(QString, QString, QPoint)), this,
249  SIGNAL(pressed(QString, QString, QPoint)));
250  connect(categoryView, SIGNAL(itemRemoved()), this, SLOT(slotScratchPadItemDeleted()));
251  connect(categoryView, SIGNAL(lastItemRemoved()), this, SLOT(slotLastScratchPadItemDeleted()));
252  setItemWidget(embed_item, 0, categoryView);
253  return categoryView;
254 }
void pressed(const QString name, const QString dom_xml, const QPoint &global_mouse_pos)

References m_core, pressed(), qdesigner_internal::WidgetBoxCategoryListView::setViewMode(), slotLastScratchPadItemDeleted(), slotSave(), and slotScratchPadItemDeleted().

Referenced by addCategory(), and ensureScratchpad().

Here is the call graph for this function:

◆ addCustomCategories()

void qdesigner_internal::WidgetBoxTreeWidget::addCustomCategories ( bool  replace)
private

Definition at line 314 of file widgetboxtreewidget.cpp.

315 {
316  if (replace) {
317  // clear out all existing custom widgets
318  if (const int numTopLevels = topLevelItemCount()) {
319  for (int t = 0; t < numTopLevels; ++t)
321  }
322  }
323  // re-add
324  const CategoryList customList = loadCustomCategoryList();
325  const CategoryList::const_iterator cend = customList.constEnd();
326  for (CategoryList::const_iterator it = customList.constBegin(); it != cend; ++it)
327  addCategory(*it);
328 }
QDesignerWidgetBoxInterface::CategoryList CategoryList

References addCategory(), categoryViewAt(), loadCustomCategoryList(), and qdesigner_internal::WidgetBoxCategoryListView::removeCustomWidgets().

Referenced by load().

Here is the call graph for this function:

◆ addWidget()

void qdesigner_internal::WidgetBoxTreeWidget::addWidget ( int  cat_idx,
const Widget wgt 
)

Definition at line 702 of file widgetboxtreewidget.cpp.

703 {
704  if (cat_idx >= topLevelItemCount())
705  return;
706 
707  QTreeWidgetItem* cat_item = topLevelItem(cat_idx);
708  WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx);
709 
710  const bool scratch = topLevelRole(cat_item) == SCRATCHPAD_ITEM;
711  categoryView->addWidget(wgt, iconForWidget(wgt.iconName()), scratch);
712  adjustSubListSize(cat_item);
713 }
static ETopLevelRole topLevelRole(const QTreeWidgetItem *item)
@ SCRATCHPAD_ITEM

References qdesigner_internal::WidgetBoxCategoryListView::addWidget(), adjustSubListSize(), categoryViewAt(), iconForWidget(), SCRATCHPAD_ITEM, and topLevelRole().

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

Here is the call graph for this function:

◆ adjustSubListSize()

void qdesigner_internal::WidgetBoxTreeWidget::adjustSubListSize ( QTreeWidgetItem *  cat_item)
private

Definition at line 585 of file widgetboxtreewidget.cpp.

586 {
587  QTreeWidgetItem* embedItem = cat_item->child(0);
588  if (embedItem == nullptr)
589  return;
590 
591  WidgetBoxCategoryListView* list_widget =
592  static_cast<WidgetBoxCategoryListView*>(itemWidget(embedItem, 0));
593  list_widget->setFixedWidth(header()->width());
594  list_widget->doItemsLayout();
595  const int height = qMax(list_widget->contentsSize().height(), 1);
596  list_widget->setFixedHeight(height);
597  embedItem->setSizeHint(0, QSize(-1, height - 1));
598 }

Referenced by addCategory(), addWidget(), dropWidgets(), filter(), resizeEvent(), slotScratchPadItemDeleted(), and updateViewMode().

◆ category()

WidgetBoxTreeWidget::Category qdesigner_internal::WidgetBoxTreeWidget::category ( int  cat_idx) const

Definition at line 605 of file widgetboxtreewidget.cpp.

606 {
607  if (cat_idx >= topLevelItemCount())
608  return Category();
609 
610  QTreeWidgetItem* cat_item = topLevelItem(cat_idx);
611 
612  QTreeWidgetItem* embedItem = cat_item->child(0);
613  WidgetBoxCategoryListView* categoryView =
614  static_cast<WidgetBoxCategoryListView*>(itemWidget(embedItem, 0));
615 
616  Category result = categoryView->category();
617  result.setName(cat_item->text(0));
618 
619  switch (topLevelRole(cat_item)) {
620  case SCRATCHPAD_ITEM:
621  result.setType(Category::Scratchpad);
622  break;
623  default:
624  result.setType(Category::Default);
625  break;
626  }
627  return result;
628 }
QDesignerWidgetBoxInterface::Category Category

References qdesigner_internal::WidgetBoxCategoryListView::category(), SCRATCHPAD_ITEM, and topLevelRole().

Referenced by qdesigner_internal::WidgetBox::category(), readCategories(), save(), and writeCategories().

Here is the call graph for this function:

◆ categoryCount()

int qdesigner_internal::WidgetBoxTreeWidget::categoryCount ( ) const

Definition at line 600 of file widgetboxtreewidget.cpp.

601 {
602  return topLevelItemCount();
603 }

Referenced by qdesigner_internal::WidgetBox::categoryCount(), ensureScratchpad(), and save().

◆ categoryViewAt()

WidgetBoxCategoryListView * qdesigner_internal::WidgetBoxTreeWidget::categoryViewAt ( int  idx) const
private

Definition at line 144 of file widgetboxtreewidget.cpp.

145 {
146  WidgetBoxCategoryListView* rc = nullptr;
147  if (QTreeWidgetItem* cat_item = topLevelItem(idx))
148  if (QTreeWidgetItem* embedItem = cat_item->child(0))
149  rc = qobject_cast<WidgetBoxCategoryListView*>(itemWidget(embedItem, 0));
150  ASSERT(rc);
151  return rc;
152 }
#define ASSERT(condition)
Definition: Assert.h:31

References ASSERT.

Referenced by addCategory(), addCustomCategories(), addWidget(), dropWidgets(), filter(), removeWidget(), updateViewMode(), widget(), and widgetCount().

◆ contextMenuEvent()

void qdesigner_internal::WidgetBoxTreeWidget::contextMenuEvent ( QContextMenuEvent *  e)
protectedvirtual

Definition at line 800 of file widgetboxtreewidget.cpp.

801 {
802  QTreeWidgetItem* item = itemAt(e->pos());
803 
804  const bool scratchpad_menu = item != nullptr && item->parent() != nullptr
805  && topLevelRole(item->parent()) == SCRATCHPAD_ITEM;
806 
807  QMenu menu;
808  menu.addAction("Expand all", this, SLOT(expandAll()));
809  menu.addAction("Collapse all", this, SLOT(collapseAll()));
810  menu.addSeparator();
811 
812  QAction* listModeAction = menu.addAction("List View");
813  QAction* iconModeAction = menu.addAction("Icon View");
814  listModeAction->setCheckable(true);
815  iconModeAction->setCheckable(true);
816  QActionGroup* viewModeGroup = new QActionGroup(&menu);
817  viewModeGroup->addAction(listModeAction);
818  viewModeGroup->addAction(iconModeAction);
819  if (m_iconMode)
820  iconModeAction->setChecked(true);
821  else
822  listModeAction->setChecked(true);
823  connect(listModeAction, SIGNAL(triggered()), SLOT(slotListMode()));
824  connect(iconModeAction, SIGNAL(triggered()), SLOT(slotIconMode()));
825 
826  if (scratchpad_menu) {
827  menu.addSeparator();
828  menu.addAction("Remove", itemWidget(item, 0), SLOT(removeCurrentItem()));
829  if (!m_iconMode)
830  menu.addAction("Edit name", itemWidget(item, 0), SLOT(editCurrentItem()));
831  }
832  e->accept();
833  menu.exec(mapToGlobal(e->pos()));
834 }

References m_iconMode, SCRATCHPAD_ITEM, slotIconMode(), slotListMode(), and topLevelRole().

Here is the call graph for this function:

◆ deleteScratchpad

void qdesigner_internal::WidgetBoxTreeWidget::deleteScratchpad ( )
privateslot

Definition at line 751 of file widgetboxtreewidget.cpp.

752 {
753  const int idx = indexOfScratchpad();
754  if (idx == -1)
755  return;
756  delete takeTopLevelItem(idx);
757  save();
758 }

References indexOfScratchpad(), and save().

Referenced by slotLastScratchPadItemDeleted().

Here is the call graph for this function:

◆ dropWidgets()

void qdesigner_internal::WidgetBoxTreeWidget::dropWidgets ( const QList< QDesignerDnDItemInterface * > &  item_list)

Definition at line 836 of file widgetboxtreewidget.cpp.

837 {
838  QTreeWidgetItem* scratch_item = nullptr;
839  WidgetBoxCategoryListView* categoryView = nullptr;
840  bool added = false;
841 
842  for (QDesignerDnDItemInterface* item : item_list) {
843  QWidget* w = item->widget();
844  if (w == nullptr)
845  continue;
846 
847  DomUI* dom_ui = item->domUi();
848  if (dom_ui == nullptr)
849  continue;
850 
851  const int scratch_idx = ensureScratchpad();
852  scratch_item = topLevelItem(scratch_idx);
853  categoryView = categoryViewAt(scratch_idx);
854 
855  // Temporarily remove the fake toplevel in-between
856  DomWidget* fakeTopLevel = dom_ui->takeElementWidget();
857  DomWidget* firstWidget = nullptr;
858  if (fakeTopLevel && !fakeTopLevel->elementWidget().isEmpty()) {
859  firstWidget = fakeTopLevel->elementWidget().first();
860  dom_ui->setElementWidget(firstWidget);
861  } else {
862  dom_ui->setElementWidget(fakeTopLevel);
863  continue;
864  }
865  // Serialize to XML
866  QString xml;
867  {
868  QXmlStreamWriter writer(&xml);
869  writer.setAutoFormatting(true);
870  writer.setAutoFormattingIndent(1);
871  writer.writeStartDocument();
872  dom_ui->write(writer);
873  writer.writeEndDocument();
874  }
875  // Insert fake toplevel again
876  dom_ui->takeElementWidget();
877  dom_ui->setElementWidget(fakeTopLevel);
878 
879  const Widget wgt = Widget(w->objectName(), xml);
880  categoryView->addWidget(wgt, iconForWidget(wgt.iconName()), true);
881 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
882  scratch_item->setExpanded(true);
883 #else
884  setItemExpanded(scratch_item, true);
885 #endif
886 
887  added = true;
888  }
889  if (added) {
890  save();
891  QApplication::setActiveWindow(this);
892  // Is the new item visible in filtered mode?
894  if (const int count = categoryView->count(am))
895  categoryView->setCurrentItem(am, count - 1);
896  categoryView->adjustSize(); // XXX
897  adjustSubListSize(scratch_item);
898  }
899 }
Definition: ui4_p.h:160
void setElementWidget(DomWidget *a)
DomWidget * takeElementWidget()
void write(QXmlStreamWriter &writer, const QString &tagName="") const
QList< DomWidget * > elementWidget() const
Definition: ui4_p.h:1691

References qdesigner_internal::WidgetBoxCategoryListView::addWidget(), adjustSubListSize(), categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::count(), DomWidget::elementWidget(), ensureScratchpad(), qdesigner_internal::WidgetBoxCategoryListView::FILTERED, iconForWidget(), save(), qdesigner_internal::WidgetBoxCategoryListView::setCurrentItem(), DomUI::setElementWidget(), DomUI::takeElementWidget(), and DomUI::write().

Referenced by qdesigner_internal::WidgetBox::dropWidgets().

Here is the call graph for this function:

◆ ensureScratchpad()

int qdesigner_internal::WidgetBoxTreeWidget::ensureScratchpad ( )
private

Definition at line 227 of file widgetboxtreewidget.cpp.

228 {
229  const int existingIndex = indexOfScratchpad();
230  if (existingIndex != -1)
231  return existingIndex;
232 
233  QTreeWidgetItem* scratch_item = new QTreeWidgetItem(this);
234  scratch_item->setText(0, "Scratchpad");
235  setTopLevelRole(SCRATCHPAD_ITEM, scratch_item);
236  addCategoryView(scratch_item, false); // Scratchpad in list mode.
237  return categoryCount() - 1;
238 }

References addCategoryView(), categoryCount(), indexOfScratchpad(), SCRATCHPAD_ITEM, and setTopLevelRole().

Referenced by addCategory(), and dropWidgets().

Here is the call graph for this function:

◆ fileName()

QString qdesigner_internal::WidgetBoxTreeWidget::fileName ( ) const

Definition at line 175 of file widgetboxtreewidget.cpp.

176 {
177  return m_file_name;
178 }

References m_file_name.

Referenced by qdesigner_internal::WidgetBox::fileName(), load(), readCategories(), and save().

◆ filter

void qdesigner_internal::WidgetBoxTreeWidget::filter ( const QString &  f)
slot

Definition at line 901 of file widgetboxtreewidget.cpp.

902 {
903  const bool empty = f.isEmpty();
904  QRegExp re = empty ? QRegExp() : QRegExp(f, Qt::CaseInsensitive, QRegExp::FixedString);
905  const int numTopLevels = topLevelItemCount();
906  bool changed = false;
907  for (int i = 0; i < numTopLevels; i++) {
908  QTreeWidgetItem* tl = topLevelItem(i);
909  WidgetBoxCategoryListView* categoryView = categoryViewAt(i);
910  // Anything changed? -> Enable the category
911  const int oldCount = categoryView->count(WidgetBoxCategoryListView::FILTERED);
912  categoryView->filter(re);
913  const int newCount = categoryView->count(WidgetBoxCategoryListView::FILTERED);
914  if (oldCount != newCount) {
915  changed = true;
916  const bool categoryEnabled = newCount > 0 || empty;
917  if (categoryEnabled) {
918  categoryView->adjustSize();
919  adjustSubListSize(tl);
920  }
921  setRowHidden(i, QModelIndex(), !categoryEnabled);
922  }
923  }
924  if (changed)
925  updateGeometries();
926 }

References adjustSubListSize(), categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::count(), qdesigner_internal::WidgetBoxCategoryListView::filter(), and qdesigner_internal::WidgetBoxCategoryListView::FILTERED.

Here is the call graph for this function:

◆ handleMousePress

void qdesigner_internal::WidgetBoxTreeWidget::handleMousePress ( QTreeWidgetItem *  item)
privateslot

Definition at line 209 of file widgetboxtreewidget.cpp.

210 {
211  if (item == nullptr)
212  return;
213 
214  if (QApplication::mouseButtons() != Qt::LeftButton)
215  return;
216 
217  if (item->parent() == nullptr) {
218 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
219  item->setExpanded(!item->isExpanded());
220 #else
221  setItemExpanded(item, !isItemExpanded(item));
222 #endif
223  return;
224  }
225 }

Referenced by WidgetBoxTreeWidget().

◆ iconForWidget()

QIcon qdesigner_internal::WidgetBoxTreeWidget::iconForWidget ( QString  iconName) const

Definition at line 131 of file widgetboxtreewidget.cpp.

132 {
133  if (iconName.isEmpty())
134  iconName = QLatin1String(qtLogoC);
135 
136  if (iconName.startsWith(QLatin1String(iconPrefixC))) {
137  const IconCache::const_iterator it = m_pluginIcons.constFind(iconName);
138  if (it != m_pluginIcons.constEnd())
139  return it.value();
140  }
141  return createIconSet(iconName);
142 }
static const char * iconPrefixC
QIcon createIconSet(const QString &name)
static const char * qtLogoC

References createIconSet(), iconPrefixC, m_pluginIcons, and qtLogoC.

Referenced by addCategory(), addWidget(), dropWidgets(), and qdesigner_internal::WidgetBox::iconForWidget().

Here is the call graph for this function:

◆ indexOfCategory()

int qdesigner_internal::WidgetBoxTreeWidget::indexOfCategory ( const QString &  name) const
private

Definition at line 267 of file widgetboxtreewidget.cpp.

268 {
269  const int topLevelCount = topLevelItemCount();
270  for (int i = 0; i < topLevelCount; ++i) {
271  if (topLevelItem(i)->text(0) == name)
272  return i;
273  }
274  return -1;
275 }
QString const & name(EShape k)
Definition: particles.cpp:21

References RealSpace::Particles::name().

Referenced by addCategory().

Here is the call graph for this function:

◆ indexOfScratchpad()

int qdesigner_internal::WidgetBoxTreeWidget::indexOfScratchpad ( ) const
private

Definition at line 256 of file widgetboxtreewidget.cpp.

257 {
258  if (const int numTopLevels = topLevelItemCount()) {
259  for (int i = numTopLevels - 1; i >= 0; --i) {
260  if (topLevelRole(topLevelItem(i)) == SCRATCHPAD_ITEM)
261  return i;
262  }
263  }
264  return -1;
265 }

References SCRATCHPAD_ITEM, and topLevelRole().

Referenced by addCategory(), deleteScratchpad(), ensureScratchpad(), and slotScratchPadItemDeleted().

Here is the call graph for this function:

◆ load()

bool qdesigner_internal::WidgetBoxTreeWidget::load ( QDesignerWidgetBox::LoadMode  loadMode)

Definition at line 277 of file widgetboxtreewidget.cpp.

278 {
279  switch (loadMode) {
281  clear();
282  break;
284  addCustomCategories(true);
285  updateGeometries();
286  return true;
287  default:
288  break;
289  }
290  const QString name = fileName();
291 
292  QFile f(name);
293  if (!f.open(QIODevice::ReadOnly)) // Might not exist at first startup
294  return false;
295 
296  const QString contents = QString::fromUtf8(f.readAll());
297  return loadContents(contents);
298 }
bool loadContents(const QString &contents)

References addCustomCategories(), fileName(), loadContents(), qdesigner_internal::QDesignerWidgetBox::LoadCustomWidgetsOnly, qdesigner_internal::QDesignerWidgetBox::LoadReplace, and RealSpace::Particles::name().

Referenced by qdesigner_internal::WidgetBox::load().

Here is the call graph for this function:

◆ loadContents()

bool qdesigner_internal::WidgetBoxTreeWidget::loadContents ( const QString &  contents)

Definition at line 300 of file widgetboxtreewidget.cpp.

301 {
302  QString errorMessage;
303  CategoryList cat_list;
304  if (!readCategories(m_file_name, contents, &cat_list, &errorMessage)) {
306  return false;
307  }
308  for (const Category& cat : cat_list)
309  addCategory(cat);
310 
311  return true;
312 }
static bool readCategories(const QString &fileName, const QString &xml, CategoryList *cats, QString *errorMessage)
QDESIGNER_SHARED_EXPORT void designerWarning(const QString &message)

References addCategory(), qdesigner_internal::designerWarning(), m_file_name, and readCategories().

Referenced by load(), and qdesigner_internal::WidgetBox::loadContents().

Here is the call graph for this function:

◆ loadCustomCategoryList()

WidgetBoxTreeWidget::CategoryList qdesigner_internal::WidgetBoxTreeWidget::loadCustomCategoryList ( ) const
private

Definition at line 576 of file widgetboxtreewidget.cpp.

577 {
578  CategoryList result;
579 
580  std::cout << "WidgetBoxTreeWidget::loadCustomCategoryList() -> XXX Not implemented."
581  << std::endl;
582  return result;
583 }

Referenced by addCustomCategories().

◆ pressed

void qdesigner_internal::WidgetBoxTreeWidget::pressed ( const QString  name,
const QString  dom_xml,
const QPoint &  global_mouse_pos 
)
signal

Referenced by addCategoryView().

◆ readCategories()

bool qdesigner_internal::WidgetBoxTreeWidget::readCategories ( const QString &  fileName,
const QString &  xml,
CategoryList cats,
QString *  errorMessage 
)
staticprivate

Definition at line 337 of file widgetboxtreewidget.cpp.

339 {
340  // Read widget box XML:
341  //
342  //<widgetbox version="4.5">
343  // <category name="Layouts">
344  // <categoryentry name="Vertical Layout" icon="win/editvlayout.png" type="default">
345  // <widget class="QListWidget" ...>
346  // ...
347 
348  QXmlStreamReader reader(contents);
349 
350  // Entries of category with name="invisible" should be ignored
351  bool ignoreEntries = false;
352 
353  while (!reader.atEnd()) {
354  switch (reader.readNext()) {
355  case QXmlStreamReader::StartElement: {
356  const QStringRef tag = reader.name();
357  if (tag == QLatin1String(widgetBoxRootElementC)) {
358  //<widgetbox version="4.5">
359  continue;
360  }
361  if (tag == QLatin1String(categoryElementC)) {
362  // <category name="Layouts">
363  const QXmlStreamAttributes attributes = reader.attributes();
364  const QString categoryName =
365  attributes.value(QLatin1String(nameAttributeC)).toString();
366  if (categoryName == QLatin1String(invisibleNameC)) {
367  ignoreEntries = true;
368  } else {
369  Category category(categoryName);
370  if (attributes.value(QLatin1String(typeAttributeC))
371  == QLatin1String(scratchPadValueC))
372  category.setType(Category::Scratchpad);
373  cats->push_back(category);
374  }
375  continue;
376  }
377  if (tag == QLatin1String(categoryEntryElementC)) {
378  // <categoryentry name="Vertical Layout" icon="win/editvlayout.png" type="default">
379  if (!ignoreEntries) {
380  QXmlStreamAttributes attr = reader.attributes();
381  const QString widgetName = attr.value(QLatin1String(nameAttributeC)).toString();
382  const QString widgetIcon = attr.value(QLatin1String(iconAttributeC)).toString();
383  const WidgetBoxTreeWidget::Widget::Type widgetType =
384  attr.value(QLatin1String(typeAttributeC)).toString()
385  == QLatin1String(customValueC)
386  ? WidgetBoxTreeWidget::Widget::Custom
387  : WidgetBoxTreeWidget::Widget::Default;
388 
389  Widget w;
390  w.setName(widgetName);
391  w.setIconName(widgetIcon);
392  w.setType(widgetType);
393  if (!readWidget(&w, contents, reader))
394  continue;
395 
396  cats->back().addWidget(w);
397  } // ignoreEntries
398  continue;
399  }
400  break;
401  }
402  case QXmlStreamReader::EndElement: {
403  const QStringRef tag = reader.name();
404  if (tag == QLatin1String(widgetBoxRootElementC)) {
405  continue;
406  }
407  if (tag == QLatin1String(categoryElementC)) {
408  ignoreEntries = false;
409  continue;
410  }
411  if (tag == QLatin1String(categoryEntryElementC)) {
412  continue;
413  }
414  break;
415  }
416  default:
417  break;
418  }
419  }
420 
421  if (reader.hasError()) {
422  *errorMessage = msgXmlError(fileName, reader);
423  return false;
424  }
425 
426  return true;
427 }
static bool readWidget(Widget *w, const QString &xml, QXmlStreamReader &r)
static QString msgXmlError(const QString &fileName, const QXmlStreamReader &r)
static const char * invisibleNameC
static const char * widgetBoxRootElementC
static const char * customValueC
static const char * scratchPadValueC
static const char * typeAttributeC
static const char * nameAttributeC
static const char * categoryEntryElementC
static const char * iconAttributeC
static const char * categoryElementC

References category(), categoryElementC, categoryEntryElementC, customValueC, fileName(), iconAttributeC, invisibleNameC, qdesigner_internal::msgXmlError(), nameAttributeC, readWidget(), scratchPadValueC, typeAttributeC, and widgetBoxRootElementC.

Referenced by loadContents().

Here is the call graph for this function:

◆ readWidget()

bool qdesigner_internal::WidgetBoxTreeWidget::readWidget ( Widget w,
const QString &  xml,
QXmlStreamReader &  r 
)
staticprivate

Read out a widget within a category. This can either be enclosed in a <ui> element or a (legacy) <widget> element which may contain nested <widget> elements.

Examples:

<ui language="c++"> <widget class="MultiPageWidget" name="multipagewidget"> ... </widget> <customwidgets>...</customwidgets> <ui>

or

<widget> <widget> ... </widget> ... <widget>

Returns true on success, false if end was reached or an error has been encountered in which case the reader has its error flag set. If successful, the current item of the reader will be the closing element (</ui> or </widget>)

Definition at line 452 of file widgetboxtreewidget.cpp.

453 {
454  qint64 startTagPosition = 0, endTagPosition = 0;
455 
456  int nesting = 0;
457  bool endEncountered = false;
458  bool parsedWidgetTag = false;
459  QString outmostElement;
460  while (!endEncountered) {
461  const qint64 currentPosition = r.characterOffset();
462  switch (r.readNext()) {
463  case QXmlStreamReader::StartElement:
464  if (nesting++ == 0) {
465  // First element must be <ui> or (legacy) <widget>
466  const QStringRef name = r.name();
467  if (name == QLatin1String(uiElementC)) {
468  startTagPosition = currentPosition;
469  } else {
470  if (name == QLatin1String(widgetElementC)) {
471  startTagPosition = currentPosition;
472  parsedWidgetTag = true;
473  } else {
474  r.raiseError(QString("Unexpected element <%1> encountered when "
475  "parsing for <widget> or <ui>")
476  .arg(name.toString()));
477  return false;
478  }
479  }
480  } else {
481  // We are within <ui> looking for the first <widget> tag
482  if (!parsedWidgetTag && r.name() == QLatin1String(widgetElementC)) {
483  parsedWidgetTag = true;
484  }
485  }
486  break;
487  case QXmlStreamReader::EndElement:
488  // Reached end of widget?
489  if (--nesting == 0) {
490  endTagPosition = r.characterOffset();
491  endEncountered = true;
492  }
493  break;
494  case QXmlStreamReader::EndDocument:
495  r.raiseError("Unexpected end of file encountered when parsing widgets.");
496  return false;
497  case QXmlStreamReader::Invalid:
498  return false;
499  default:
500  break;
501  }
502  }
503  if (!parsedWidgetTag) {
504  r.raiseError("A widget element could not be found.");
505  return false;
506  }
507  // Oddity: Startposition is 1 off
508  QString widgetXml = xml.mid(startTagPosition, endTagPosition - startTagPosition);
509  const QChar lessThan = QLatin1Char('<');
510  if (!widgetXml.startsWith(lessThan))
511  widgetXml.prepend(lessThan);
512  w->setDomXml(widgetXml);
513  return true;
514 }
static const char * uiElementC
static const char * widgetElementC

References RealSpace::Particles::name(), uiElementC, and widgetElementC.

Referenced by readCategories().

Here is the call graph for this function:

◆ removeCategory()

void qdesigner_internal::WidgetBoxTreeWidget::removeCategory ( int  cat_idx)

Definition at line 678 of file widgetboxtreewidget.cpp.

679 {
680  if (cat_idx >= topLevelItemCount())
681  return;
682  delete takeTopLevelItem(cat_idx);
683 }

Referenced by qdesigner_internal::WidgetBox::removeCategory().

◆ removeWidget()

void qdesigner_internal::WidgetBoxTreeWidget::removeWidget ( int  cat_idx,
int  wgt_idx 
)

Definition at line 715 of file widgetboxtreewidget.cpp.

716 {
717  if (cat_idx >= topLevelItemCount())
718  return;
719 
720  WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx);
721 
722  // SDK functions want unfiltered access
724  if (wgt_idx >= categoryView->count(am))
725  return;
726 
727  categoryView->removeRow(am, wgt_idx);
728 }

References categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::count(), qdesigner_internal::WidgetBoxCategoryListView::removeRow(), and qdesigner_internal::WidgetBoxCategoryListView::UNFILTERED.

Referenced by qdesigner_internal::WidgetBox::removeWidget().

Here is the call graph for this function:

◆ resizeEvent()

void qdesigner_internal::WidgetBoxTreeWidget::resizeEvent ( QResizeEvent *  e)
protectedvirtual

Definition at line 791 of file widgetboxtreewidget.cpp.

792 {
793  QTreeWidget::resizeEvent(e);
794  if (const int numTopLevels = topLevelItemCount()) {
795  for (int i = numTopLevels - 1; i >= 0; --i)
796  adjustSubListSize(topLevelItem(i));
797  }
798 }

References adjustSubListSize().

Here is the call graph for this function:

◆ restoreExpandedState()

void qdesigner_internal::WidgetBoxTreeWidget::restoreExpandedState ( )
private

Definition at line 159 of file widgetboxtreewidget.cpp.

160 {
161  std::cout << "WidgetBoxTreeWidget::restoreExpandedState() -> XXX Not implemented." << std::endl;
162  return;
163 }

◆ save()

bool qdesigner_internal::WidgetBoxTreeWidget::save ( )

Definition at line 180 of file widgetboxtreewidget.cpp.

181 {
182  if (fileName().isEmpty())
183  return false;
184 
185  QFile file(fileName());
186  if (!file.open(QIODevice::WriteOnly))
187  return false;
188 
189  CategoryList cat_list;
190  const int count = categoryCount();
191  for (int i = 0; i < count; ++i)
192  cat_list.append(category(i));
193 
194  QXmlStreamWriter writer(&file);
195  writer.setAutoFormatting(true);
196  writer.setAutoFormattingIndent(1);
197  writer.writeStartDocument();
198  writeCategories(writer, cat_list);
199  writer.writeEndDocument();
200 
201  return true;
202 }
void writeCategories(QXmlStreamWriter &writer, const CategoryList &cat_list) const

References category(), categoryCount(), fileName(), and writeCategories().

Referenced by deleteScratchpad(), dropWidgets(), qdesigner_internal::WidgetBox::save(), slotSave(), and slotScratchPadItemDeleted().

Here is the call graph for this function:

◆ saveExpandedState()

void qdesigner_internal::WidgetBoxTreeWidget::saveExpandedState ( ) const
private

Definition at line 154 of file widgetboxtreewidget.cpp.

155 {
156  return;
157 }

Referenced by ~WidgetBoxTreeWidget().

◆ setFileName()

void qdesigner_internal::WidgetBoxTreeWidget::setFileName ( const QString &  file_name)

Definition at line 170 of file widgetboxtreewidget.cpp.

171 {
172  m_file_name = file_name;
173 }

References m_file_name.

Referenced by qdesigner_internal::WidgetBox::setFileName().

◆ slotIconMode

void qdesigner_internal::WidgetBoxTreeWidget::slotIconMode ( )
privateslot

Definition at line 766 of file widgetboxtreewidget.cpp.

767 {
768  m_iconMode = true;
769  updateViewMode();
770 }

References m_iconMode, and updateViewMode().

Referenced by contextMenuEvent().

Here is the call graph for this function:

◆ slotLastScratchPadItemDeleted

void qdesigner_internal::WidgetBoxTreeWidget::slotLastScratchPadItemDeleted ( )
privateslot

Definition at line 738 of file widgetboxtreewidget.cpp.

739 {
740  // Remove the scratchpad in the next idle loop
742  m_scratchPadDeleteTimer = new QTimer(this);
743  m_scratchPadDeleteTimer->setSingleShot(true);
744  m_scratchPadDeleteTimer->setInterval(0);
745  connect(m_scratchPadDeleteTimer, SIGNAL(timeout()), this, SLOT(deleteScratchpad()));
746  }
747  if (!m_scratchPadDeleteTimer->isActive())
748  m_scratchPadDeleteTimer->start();
749 }

References deleteScratchpad(), and m_scratchPadDeleteTimer.

Referenced by addCategoryView().

Here is the call graph for this function:

◆ slotListMode

void qdesigner_internal::WidgetBoxTreeWidget::slotListMode ( )
privateslot

Definition at line 760 of file widgetboxtreewidget.cpp.

761 {
762  m_iconMode = false;
763  updateViewMode();
764 }

References m_iconMode, and updateViewMode().

Referenced by contextMenuEvent().

Here is the call graph for this function:

◆ slotSave

void qdesigner_internal::WidgetBoxTreeWidget::slotSave ( )
privateslot

Definition at line 204 of file widgetboxtreewidget.cpp.

205 {
206  save();
207 }

References save().

Referenced by addCategoryView().

Here is the call graph for this function:

◆ slotScratchPadItemDeleted

void qdesigner_internal::WidgetBoxTreeWidget::slotScratchPadItemDeleted ( )
privateslot

Definition at line 730 of file widgetboxtreewidget.cpp.

731 {
732  const int scratch_idx = indexOfScratchpad();
733  QTreeWidgetItem* scratch_item = topLevelItem(scratch_idx);
734  adjustSubListSize(scratch_item);
735  save();
736 }

References adjustSubListSize(), indexOfScratchpad(), and save().

Referenced by addCategoryView().

Here is the call graph for this function:

◆ updateViewMode()

void qdesigner_internal::WidgetBoxTreeWidget::updateViewMode ( )
private

Definition at line 772 of file widgetboxtreewidget.cpp.

773 {
774  if (const int numTopLevels = topLevelItemCount()) {
775  for (int i = numTopLevels - 1; i >= 0; --i) {
776  QTreeWidgetItem* topLevel = topLevelItem(i);
777  // Scratch pad stays in list mode.
778  const QListView::ViewMode viewMode =
779  m_iconMode && (topLevelRole(topLevel) != SCRATCHPAD_ITEM) ? QListView::IconMode
780  : QListView::ListMode;
781  WidgetBoxCategoryListView* categoryView = categoryViewAt(i);
782  if (viewMode != categoryView->viewMode()) {
783  categoryView->setViewMode(viewMode);
784  adjustSubListSize(topLevelItem(i));
785  }
786  }
787  }
788  updateGeometries();
789 }

References adjustSubListSize(), categoryViewAt(), m_iconMode, SCRATCHPAD_ITEM, and topLevelRole().

Referenced by slotIconMode(), and slotListMode().

Here is the call graph for this function:

◆ widget()

WidgetBoxTreeWidget::Widget qdesigner_internal::WidgetBoxTreeWidget::widget ( int  cat_idx,
int  wgt_idx 
) const

Definition at line 693 of file widgetboxtreewidget.cpp.

694 {
695  if (cat_idx >= topLevelItemCount())
696  return Widget();
697  // SDK functions want unfiltered access
698  WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx);
699  return categoryView->widgetAt(WidgetBoxCategoryListView::UNFILTERED, wgt_idx);
700 }

References categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::UNFILTERED, and qdesigner_internal::WidgetBoxCategoryListView::widgetAt().

Referenced by qdesigner_internal::WidgetBox::widget().

Here is the call graph for this function:

◆ widgetCount()

int qdesigner_internal::WidgetBoxTreeWidget::widgetCount ( int  cat_idx) const

Definition at line 685 of file widgetboxtreewidget.cpp.

686 {
687  if (cat_idx >= topLevelItemCount())
688  return 0;
689  // SDK functions want unfiltered access
691 }

References categoryViewAt(), qdesigner_internal::WidgetBoxCategoryListView::count(), and qdesigner_internal::WidgetBoxCategoryListView::UNFILTERED.

Referenced by addCategory(), qdesigner_internal::WidgetBox::widgetCount(), and writeCategories().

Here is the call graph for this function:

◆ writeCategories()

void qdesigner_internal::WidgetBoxTreeWidget::writeCategories ( QXmlStreamWriter &  writer,
const CategoryList cat_list 
) const
private

Definition at line 516 of file widgetboxtreewidget.cpp.

518 {
519  const QString widgetbox = QLatin1String(widgetBoxRootElementC);
520  const QString name = QLatin1String(nameAttributeC);
521  const QString type = QLatin1String(typeAttributeC);
522  const QString icon = QLatin1String(iconAttributeC);
523  const QString defaultType = QLatin1String(defaultTypeValueC);
524  const QString category = QLatin1String(categoryElementC);
525  const QString categoryEntry = QLatin1String(categoryEntryElementC);
526  const QString iconPrefix = QLatin1String(iconPrefixC);
527  const QString widgetTag = QLatin1String(widgetElementC);
528 
529  //
530  // <widgetbox>
531  // <category name="Layouts">
532  // <categoryEntry name="Vertical Layout" type="default" icon="win/editvlayout.png">
533  // <ui>
534  // ...
535  // </ui>
536  // </categoryEntry>
537  // ...
538  // </category>
539  // ...
540  // </widgetbox>
541  //
542 
543  writer.writeStartElement(widgetbox);
544 
545  for (const Category& cat : cat_list) {
546  writer.writeStartElement(category);
547  writer.writeAttribute(name, cat.name());
548  if (cat.type() == Category::Scratchpad)
549  writer.writeAttribute(type, QLatin1String(scratchPadValueC));
550 
551  const int widgetCount = cat.widgetCount();
552  for (int i = 0; i < widgetCount; ++i) {
553  const Widget wgt = cat.widget(i);
554  if (wgt.type() == Widget::Custom)
555  continue;
556 
557  writer.writeStartElement(categoryEntry);
558  writer.writeAttribute(name, wgt.name());
559  if (!wgt.iconName().startsWith(iconPrefix))
560  writer.writeAttribute(icon, wgt.iconName());
561  writer.writeAttribute(type, defaultType);
562 
563  const DomUI* domUI = QDesignerWidgetBox::xmlToUi(
564  wgt.name(), WidgetBoxCategoryListView::widgetDomXml(wgt), false);
565  if (domUI) {
566  domUI->write(writer);
567  delete domUI;
568  }
569  writer.writeEndElement(); // categoryEntry
570  }
571  writer.writeEndElement(); // categoryEntry
572  }
573  writer.writeEndElement(); // widgetBox
574 }
static DomUI * xmlToUi(const QString &name, const QString &xml, bool insertFakeTopLevel, QString *errorMessage)
static QString widgetDomXml(const QDesignerWidgetBoxInterface::Widget &widget)
static const char * defaultTypeValueC

References category(), categoryElementC, categoryEntryElementC, defaultTypeValueC, iconAttributeC, iconPrefixC, RealSpace::Particles::name(), nameAttributeC, scratchPadValueC, typeAttributeC, widgetBoxRootElementC, widgetCount(), qdesigner_internal::WidgetBoxCategoryListView::widgetDomXml(), widgetElementC, DomUI::write(), and qdesigner_internal::QDesignerWidgetBox::xmlToUi().

Referenced by save().

Here is the call graph for this function:

Member Data Documentation

◆ m_core

SampleDesignerInterface* qdesigner_internal::WidgetBoxTreeWidget::m_core
private

Definition at line 139 of file widgetboxtreewidget.h.

Referenced by addCategoryView().

◆ m_file_name

QString qdesigner_internal::WidgetBoxTreeWidget::m_file_name
private

Definition at line 140 of file widgetboxtreewidget.h.

Referenced by fileName(), loadContents(), and setFileName().

◆ m_iconMode

bool qdesigner_internal::WidgetBoxTreeWidget::m_iconMode
private

◆ m_pluginIcons

IconCache qdesigner_internal::WidgetBoxTreeWidget::m_pluginIcons
mutableprivate

Definition at line 142 of file widgetboxtreewidget.h.

Referenced by iconForWidget().

◆ m_scratchPadDeleteTimer

QTimer* qdesigner_internal::WidgetBoxTreeWidget::m_scratchPadDeleteTimer
private

Definition at line 144 of file widgetboxtreewidget.h.

Referenced by slotLastScratchPadItemDeleted().


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