diff --git a/GUI/Views/SampleDesigner/SampleWidgetBox.cpp b/GUI/Views/SampleDesigner/SampleWidgetBox.cpp index 68a2d066875a714650c921ce497497f15f078161..bace2dfc1e86f242b404df525abacf34f133e067 100644 --- a/GUI/Views/SampleDesigner/SampleWidgetBox.cpp +++ b/GUI/Views/SampleDesigner/SampleWidgetBox.cpp @@ -22,19 +22,15 @@ SampleWidgetBox::SampleWidgetBox(QWidget* parent) : QWidget(parent), m_widgetBox(0) { - setWindowTitle(QLatin1String("Widget Box")); setObjectName(QLatin1String("WidgetBox")); - setWindowTitle("Items Toolbox"); - m_widgetBox = new qdesigner_internal::WidgetBox(this); - m_widgetBox->setFileName(":/widgetbox/widgetbox.xml"); - m_widgetBox->load(); + m_widgetBox = new WidgetBox(this); + m_widgetBox->load(":/widgetbox/widgetbox.xml"); QVBoxLayout* layout = new QVBoxLayout; layout->setMargin(0); layout->setSpacing(0); - // layout->addWidget(bar); layout->addWidget(m_widgetBox); setLayout(layout); } diff --git a/GUI/Views/SampleDesigner/SampleWidgetBox.h b/GUI/Views/SampleDesigner/SampleWidgetBox.h index f0797fcc990ff23e6f1f8e7051e129a84895b659..294db17fb96efeecce88026582831c991eb7cca3 100644 --- a/GUI/Views/SampleDesigner/SampleWidgetBox.h +++ b/GUI/Views/SampleDesigner/SampleWidgetBox.h @@ -17,7 +17,7 @@ #include <QWidget> -class QDesignerWidgetBoxInterface; +class WidgetBox; //! widget box and tool window on the left side of SampleView class SampleWidgetBox : public QWidget { @@ -27,7 +27,7 @@ public: QSize minimumSizeHint() const override { return QSize(300, 300); } private: - QDesignerWidgetBoxInterface* m_widgetBox; + WidgetBox* m_widgetBox; }; #endif // BORNAGAIN_GUI_VIEWS_SAMPLEDESIGNER_SAMPLEWIDGETBOX_H diff --git a/GUI/Views/widgetbox/widgetbox.cpp b/GUI/Views/widgetbox/widgetbox.cpp index c31423bd79e9724eb2b1618aa7c51aaeaabc35b0..d81e8ad8c4a894bcf2a375c1c36b6e7111a2b573 100644 --- a/GUI/Views/widgetbox/widgetbox.cpp +++ b/GUI/Views/widgetbox/widgetbox.cpp @@ -1,73 +1,32 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Designer of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Views/widgetbox/widgetbox.cpp +//! @brief Implements class WidgetBox +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ #include "GUI/Views/widgetbox/widgetbox.h" -#include "GUI/Views/widgetbox/qdesigner_dnditem_p.h" +#include "GUI/Views/widgetbox/DesignerMimeData.h" #include "GUI/Views/widgetbox/widgetboxtreewidget.h" -#include <QtDesigner/QDesignerFormEditorInterface> -#include <QtDesigner/QDesignerFormWindowManagerInterface> - - #include <QApplication> -#include <QDrag> +#include <QFocusEvent> #include <QLineEdit> #include <QToolBar> #include <QVBoxLayout> -#include <QtGui/QDropEvent> - -#include <QtGui/QIcon> - -#include "GUI/Views/widgetbox/DesignerMimeData.h" - -#include <iostream> -QT_BEGIN_NAMESPACE - -namespace qdesigner_internal { +namespace { class WidgetBoxFilterLineEdit : public QLineEdit { public: - explicit WidgetBoxFilterLineEdit(QWidget* parent = 0) - : QLineEdit(parent), m_defaultFocusPolicy(focusPolicy()) + explicit WidgetBoxFilterLineEdit(QWidget* parent = 0) : QLineEdit(parent) { setFocusPolicy(Qt::NoFocus); } @@ -75,9 +34,6 @@ public: protected: virtual void mousePressEvent(QMouseEvent* event); virtual void focusInEvent(QFocusEvent* e); - -private: - const Qt::FocusPolicy m_defaultFocusPolicy; }; void WidgetBoxFilterLineEdit::mousePressEvent(QMouseEvent* e) @@ -104,8 +60,10 @@ void WidgetBoxFilterLineEdit::focusInEvent(QFocusEvent* e) QLineEdit::focusInEvent(e); } +} // namespace + WidgetBox::WidgetBox(QWidget* parent, Qt::WindowFlags flags) - : QDesignerWidgetBox(parent, flags), m_view(new WidgetBoxTreeWidget) + : QWidget(parent, flags), m_view(new qdesigner_internal::WidgetBoxTreeWidget) { QVBoxLayout* l = new QVBoxLayout(this); l->setMargin(0); @@ -124,32 +82,8 @@ WidgetBox::WidgetBox(QWidget* parent, Qt::WindowFlags flags) connect(m_view, SIGNAL(pressed(QString, QString, QPoint)), this, SLOT(handleMousePress(QString, QString, QPoint))); l->addWidget(m_view); - - setAcceptDrops(true); - - // QVBoxLayout *l = new QVBoxLayout(this); - // l->setMargin(0); - // l->setSpacing(0); - - // // Prevent the filter from grabbing focus since Our view has Qt::NoFocus - // FilterWidget *filterWidget = new FilterWidget(0, FilterWidget::LayoutAlignNone); - // filterWidget->setRefuseFocus(true); - // connect(filterWidget, SIGNAL(filterChanged(QString)), m_view, SLOT(filter(QString))); - - // QToolBar *toolBar = new QToolBar(this); - // toolBar->addWidget(filterWidget); - // l->addWidget(toolBar); - - // // View - // connect(m_view, SIGNAL(pressed(QString,QString,QPoint)), - // this, SLOT(handleMousePress(QString,QString,QPoint))); - // l->addWidget(m_view); - - // setAcceptDrops (true); } -WidgetBox::~WidgetBox() = default; - void WidgetBox::handleMousePress(const QString& name, const QString& xml, const QPoint& global_mouse_pos) { @@ -157,132 +91,10 @@ void WidgetBox::handleMousePress(const QString& name, const QString& xml, if (QApplication::mouseButtons() != Qt::LeftButton) return; - // std::cout << "WidgetBox::handleMousePress() -> name:" << name.toStdString() << std::endl; DesignerMimeData::execDrag(name, xml, this); } -int WidgetBox::categoryCount() const -{ - return m_view->categoryCount(); -} - -QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const +bool WidgetBox::load(const QString& file_name) { - return m_view->category(cat_idx); + return m_view->load(file_name); } - -void WidgetBox::addCategory(const Category& cat) -{ - m_view->addCategory(cat); -} - -void WidgetBox::removeCategory(int cat_idx) -{ - m_view->removeCategory(cat_idx); -} - -int WidgetBox::widgetCount(int cat_idx) const -{ - return m_view->widgetCount(cat_idx); -} - -QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const -{ - return m_view->widget(cat_idx, wgt_idx); -} - -void WidgetBox::addWidget(int cat_idx, const Widget& wgt) -{ - m_view->addWidget(cat_idx, wgt); -} - -void WidgetBox::removeWidget(int cat_idx, int wgt_idx) -{ - m_view->removeWidget(cat_idx, wgt_idx); -} - -void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*>& item_list, const QPoint&) -{ - m_view->dropWidgets(item_list); -} - -void WidgetBox::setFileName(const QString& file_name) -{ - m_view->setFileName(file_name); -} - -QString WidgetBox::fileName() const -{ - return m_view->fileName(); -} - -bool WidgetBox::load() -{ - // std::cout << "WidgetBox::load() -> We are here" << std::endl; - return m_view->load(loadMode()); -} - -bool WidgetBox::loadContents(const QString& contents) -{ - return m_view->loadContents(contents); -} - -bool WidgetBox::save() -{ - return m_view->save(); -} - -static const QDesignerMimeData* checkDragEvent(QDropEvent* event, bool acceptEventsFromWidgetBox) -{ - // std::cout << "QDesignerMimeData *checkDragEvent() -> ?" << std::endl; - const QDesignerMimeData* mimeData = qobject_cast<const QDesignerMimeData*>(event->mimeData()); - if (!mimeData) { - event->ignore(); - return 0; - } - // If desired, ignore a widget box drag and drop, where widget==0. - if (!acceptEventsFromWidgetBox) { - const bool fromWidgetBox = !mimeData->items().first()->widget(); - if (fromWidgetBox) { - event->ignore(); - return 0; - } - } - - mimeData->acceptEvent(event); - return mimeData; -} - -void WidgetBox::dragEnterEvent(QDragEnterEvent* event) -{ - // We accept event originating from the widget box also here, - // because otherwise Windows will not show the DnD pixmap. - checkDragEvent(event, true); -} - -void WidgetBox::dragMoveEvent(QDragMoveEvent* event) -{ - checkDragEvent(event, true); -} - -void WidgetBox::dropEvent(QDropEvent* event) -{ - const QDesignerMimeData* mimeData = checkDragEvent(event, false); - if (!mimeData) - return; - - dropWidgets(mimeData->items(), event->pos()); - QDesignerMimeData::removeMovedWidgetsFromSourceForm(mimeData->items()); -} - -QIcon WidgetBox::iconForWidget(const QString& className, const QString& category) const -{ - Widget widgetData; - if (!findWidget(this, className, category, &widgetData)) - return QIcon(); - return m_view->iconForWidget(widgetData.iconName()); -} - -} // namespace qdesigner_internal - -QT_END_NAMESPACE diff --git a/GUI/Views/widgetbox/widgetbox.h b/GUI/Views/widgetbox/widgetbox.h index 623e4c107696bc0c825a0e7d988562a3a4ebdf9a..0fe254af7129cb97f6536e9d4839e8e4e6482691 100644 --- a/GUI/Views/widgetbox/widgetbox.h +++ b/GUI/Views/widgetbox/widgetbox.h @@ -1,101 +1,40 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Designer of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -// Patched JWu 12nov20 because QFlags(0) was deprecated +// ************************************************************************************************ +// +// BornAgain: simulate and fit reflection and scattering +// +//! @file GUI/Views/widgetbox/widgetbox.h +//! @brief Defines class WidgetBox +//! +//! @homepage http://www.bornagainproject.org +//! @license GNU General Public License v3 or higher (see COPYING) +//! @copyright Forschungszentrum Jülich GmbH 2021 +//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) +// +// ************************************************************************************************ #ifndef BORNAGAIN_GUI_VIEWS_WIDGETBOX_WIDGETBOX_H #define BORNAGAIN_GUI_VIEWS_WIDGETBOX_WIDGETBOX_H -#include "GUI/Views/widgetbox/qdesigner_widgetbox_p.h" -#include "GUI/Views/widgetbox/widgetbox_global.h" - -QT_BEGIN_NAMESPACE - -class QDesignerFormWindowInterface; +#include <QWidget> namespace qdesigner_internal { class WidgetBoxTreeWidget; -class QT_WIDGETBOX_EXPORT WidgetBox : public QDesignerWidgetBox { +} + +class WidgetBox : public QWidget { Q_OBJECT public: explicit WidgetBox(QWidget* parent = nullptr, Qt::WindowFlags flags = {}); - virtual ~WidgetBox(); - - virtual int categoryCount() const; - virtual Category category(int cat_idx) const; - virtual void addCategory(const Category& cat); - virtual void removeCategory(int cat_idx); - - virtual int widgetCount(int cat_idx) const; - virtual Widget widget(int cat_idx, int wgt_idx) const; - virtual void addWidget(int cat_idx, const Widget& wgt); - virtual void removeWidget(int cat_idx, int wgt_idx); - void dropWidgets(const QList<QDesignerDnDItemInterface*>& item_list, - const QPoint& global_mouse_pos); - - virtual void setFileName(const QString& file_name); - virtual QString fileName() const; - virtual bool load(); - virtual bool save(); - - virtual bool loadContents(const QString& contents); - virtual QIcon iconForWidget(const QString& className, const QString& category = "") const; - -protected: - virtual void dragEnterEvent(QDragEnterEvent* event); - virtual void dragMoveEvent(QDragMoveEvent* event); - virtual void dropEvent(QDropEvent* event); + bool load(const QString& file_name); private slots: void handleMousePress(const QString& name, const QString& xml, const QPoint& global_mouse_pos); private: - WidgetBoxTreeWidget* m_view; + qdesigner_internal::WidgetBoxTreeWidget* m_view; }; -} // namespace qdesigner_internal - -QT_END_NAMESPACE - #endif // BORNAGAIN_GUI_VIEWS_WIDGETBOX_WIDGETBOX_H diff --git a/GUI/Views/widgetbox/widgetboxcategorylistview.cpp b/GUI/Views/widgetbox/widgetboxcategorylistview.cpp index 2f2f67c41b6121cf99ed60f9b22269777e9a9580..d9ed2c4b298c0149d0ffe3790d6ad4427a09fe93 100644 --- a/GUI/Views/widgetbox/widgetboxcategorylistview.cpp +++ b/GUI/Views/widgetbox/widgetboxcategorylistview.cpp @@ -97,21 +97,20 @@ namespace qdesigner_internal { struct WidgetBoxCategoryEntry { WidgetBoxCategoryEntry(); explicit WidgetBoxCategoryEntry(const QDesignerWidgetBoxInterface::Widget& widget, - const QString& filter, const QIcon& icon, bool editable); + const QString& filter, const QIcon& icon); QDesignerWidgetBoxInterface::Widget widget; QString toolTip; QString whatsThis; QString filter; QIcon icon; - bool editable; }; -WidgetBoxCategoryEntry::WidgetBoxCategoryEntry() : editable(false) {} +WidgetBoxCategoryEntry::WidgetBoxCategoryEntry() {} WidgetBoxCategoryEntry::WidgetBoxCategoryEntry(const QDesignerWidgetBoxInterface::Widget& w, - const QString& filterIn, const QIcon& i, bool e) - : widget(w), filter(filterIn), icon(i), editable(e) + const QString& filterIn, const QIcon& i) + : widget(w), filter(filterIn), icon(i) { } @@ -134,8 +133,7 @@ public: QListView::ViewMode viewMode() const; void setViewMode(QListView::ViewMode vm); - void addWidget(const QDesignerWidgetBoxInterface::Widget& widget, const QIcon& icon, - bool editable); + void addWidget(const QDesignerWidgetBoxInterface::Widget& widget, const QIcon& icon); QDesignerWidgetBoxInterface::Widget widgetAt(const QModelIndex& index) const; QDesignerWidgetBoxInterface::Widget widgetAt(int row) const; @@ -225,7 +223,7 @@ bool WidgetBoxCategoryModel::removeCustomWidgets() } void WidgetBoxCategoryModel::addWidget(const QDesignerWidgetBoxInterface::Widget& widget, - const QIcon& icon, bool editable) + const QIcon& icon) { // build item. Filter on name + class name if it is different and not a layout. QString filter = widget.name(); @@ -234,7 +232,7 @@ void WidgetBoxCategoryModel::addWidget(const QDesignerWidgetBoxInterface::Widget if (!filter.contains(className)) filter += className; } - WidgetBoxCategoryEntry item(widget, filter, icon, editable); + WidgetBoxCategoryEntry item(widget, filter, icon); QXmlStreamReader reader(widget.domXml()); while (!reader.atEnd()) { @@ -330,16 +328,7 @@ bool WidgetBoxCategoryModel::setData(const QModelIndex& index, const QVariant& v Qt::ItemFlags WidgetBoxCategoryModel::flags(const QModelIndex& index) const { - Qt::ItemFlags rc = Qt::ItemIsEnabled; - const int row = index.row(); - if (row >= 0 && row < m_items.size()) - if (m_items.at(row).editable) { - rc |= Qt::ItemIsSelectable; - // Can change name in list mode only - if (m_viewMode == QListView::ListMode) - rc |= Qt::ItemIsEditable; - } - return rc; + return Qt::ItemIsEnabled; } int WidgetBoxCategoryModel::rowCount(const QModelIndex& /*parent*/) const @@ -374,28 +363,6 @@ QDesignerWidgetBoxInterface::Widget WidgetBoxCategoryModel::widgetAt(int row) co return m_items.at(row).widget; } -/* WidgetSubBoxItemDelegate, ensures a valid name using a regexp validator */ - -class WidgetBoxCategoryEntryDelegate : public QItemDelegate { -public: - explicit WidgetBoxCategoryEntryDelegate(QWidget* parent = 0) : QItemDelegate(parent) {} - QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, - const QModelIndex& index) const; -}; - -QWidget* WidgetBoxCategoryEntryDelegate::createEditor(QWidget* parent, - const QStyleOptionViewItem& option, - const QModelIndex& index) const -{ - QWidget* result = QItemDelegate::createEditor(parent, option, index); - if (QLineEdit* line_edit = qobject_cast<QLineEdit*>(result)) { - QRegExp re = QRegExp("[_a-zA-Z][_a-zA-Z0-9]*"); - ASSERT(re.isValid()); - line_edit->setValidator(new QRegExpValidator(re, line_edit)); - } - return result; -} - // ---------------------- WidgetBoxCategoryListView WidgetBoxCategoryListView::WidgetBoxCategoryListView(QWidget* parent) @@ -414,8 +381,6 @@ WidgetBoxCategoryListView::WidgetBoxCategoryListView(QWidget* parent) setResizeMode(QListView::Adjust); setUniformItemSizes(true); - setItemDelegate(new WidgetBoxCategoryEntryDelegate(this)); - connect(this, SIGNAL(pressed(QModelIndex)), this, SLOT(slotPressed(QModelIndex))); setEditTriggers(QAbstractItemView::AnyKeyPressed); @@ -465,13 +430,6 @@ void WidgetBoxCategoryListView::removeCurrentItem() } } -void WidgetBoxCategoryListView::editCurrentItem() -{ - const QModelIndex index = currentIndex(); - if (index.isValid()) - edit(index); -} - int WidgetBoxCategoryListView::count(EAccessMode am) const { return am == FILTERED ? m_proxyModel->rowCount() : m_model->rowCount(); @@ -507,9 +465,9 @@ bool WidgetBoxCategoryListView::containsWidget(const QString& name) } void WidgetBoxCategoryListView::addWidget(const QDesignerWidgetBoxInterface::Widget& widget, - const QIcon& icon, bool editable) + const QIcon& icon) { - m_model->addWidget(widget, icon, editable); + m_model->addWidget(widget, icon); } QString WidgetBoxCategoryListView::widgetDomXml(const QDesignerWidgetBoxInterface::Widget& widget) diff --git a/GUI/Views/widgetbox/widgetboxcategorylistview.h b/GUI/Views/widgetbox/widgetboxcategorylistview.h index 4ee2e74ea52ca28592a4e6452379967a6e34d85b..50fd41b06cd468978dcae19e25dabf9321db2615 100644 --- a/GUI/Views/widgetbox/widgetboxcategorylistview.h +++ b/GUI/Views/widgetbox/widgetboxcategorylistview.h @@ -82,8 +82,7 @@ public: void setCurrentItem(EAccessMode am, int row); // These methods operate on the unfiltered model and are used for serialization - void addWidget(const QDesignerWidgetBoxInterface::Widget& widget, const QIcon& icon, - bool editable); + void addWidget(const QDesignerWidgetBoxInterface::Widget& widget, const QIcon& icon); bool containsWidget(const QString& name); QDesignerWidgetBoxInterface::Category category() const; bool removeCustomWidgets(); @@ -103,7 +102,6 @@ public slots: private slots: void slotPressed(const QModelIndex& index); void removeCurrentItem(); - void editCurrentItem(); private: int mapRowToSource(int filterRow) const; diff --git a/GUI/Views/widgetbox/widgetboxtreewidget.cpp b/GUI/Views/widgetbox/widgetboxtreewidget.cpp index 06e4daa75cceec09d0ed0f3dbdd37755f89c795e..6953dd4b87f632b746343f5d44730ae8df1e8f9b 100644 --- a/GUI/Views/widgetbox/widgetboxtreewidget.cpp +++ b/GUI/Views/widgetbox/widgetboxtreewidget.cpp @@ -83,7 +83,6 @@ static const char* iconAttributeC = "icon"; static const char* defaultTypeValueC = "default"; static const char* customValueC = "custom"; static const char* iconPrefixC = "__qt_icon__"; -static const char* scratchPadValueC = "scratchpad"; static const char* qtLogoC = "qtlogo.png"; static const char* invisibleNameC = "[invisible]"; @@ -108,8 +107,7 @@ static ETopLevelRole topLevelRole(const QTreeWidgetItem* item) namespace qdesigner_internal { -WidgetBoxTreeWidget::WidgetBoxTreeWidget(QWidget* parent) - : QTreeWidget(parent), m_iconMode(false), m_scratchPadDeleteTimer(nullptr) +WidgetBoxTreeWidget::WidgetBoxTreeWidget(QWidget* parent) : QTreeWidget(parent), m_iconMode(false) { setFocusPolicy(Qt::NoFocus); setIndentation(0); @@ -167,45 +165,6 @@ WidgetBoxTreeWidget::~WidgetBoxTreeWidget() saveExpandedState(); } -void WidgetBoxTreeWidget::setFileName(const QString& file_name) -{ - m_file_name = file_name; -} - -QString WidgetBoxTreeWidget::fileName() const -{ - return m_file_name; -} - -bool WidgetBoxTreeWidget::save() -{ - if (fileName().isEmpty()) - return false; - - QFile file(fileName()); - if (!file.open(QIODevice::WriteOnly)) - return false; - - CategoryList cat_list; - const int count = categoryCount(); - for (int i = 0; i < count; ++i) - cat_list.append(category(i)); - - QXmlStreamWriter writer(&file); - writer.setAutoFormatting(true); - writer.setAutoFormattingIndent(1); - writer.writeStartDocument(); - writeCategories(writer, cat_list); - writer.writeEndDocument(); - - return true; -} - -void WidgetBoxTreeWidget::slotSave() -{ - save(); -} - void WidgetBoxTreeWidget::handleMousePress(QTreeWidgetItem* item) { if (item == nullptr) @@ -224,19 +183,6 @@ void WidgetBoxTreeWidget::handleMousePress(QTreeWidgetItem* item) } } -int WidgetBoxTreeWidget::ensureScratchpad() -{ - const int existingIndex = indexOfScratchpad(); - if (existingIndex != -1) - return existingIndex; - - QTreeWidgetItem* scratch_item = new QTreeWidgetItem(this); - scratch_item->setText(0, "Scratchpad"); - setTopLevelRole(SCRATCHPAD_ITEM, scratch_item); - addCategoryView(scratch_item, false); // Scratchpad in list mode. - return categoryCount() - 1; -} - WidgetBoxCategoryListView* WidgetBoxTreeWidget::addCategoryView(QTreeWidgetItem* parent, bool iconMode) { @@ -248,19 +194,12 @@ WidgetBoxCategoryListView* WidgetBoxTreeWidget::addCategoryView(QTreeWidgetItem* connect(categoryView, SIGNAL(pressed(QString, QString, QPoint)), this, SIGNAL(pressed(QString, QString, QPoint))); connect(categoryView, SIGNAL(itemRemoved()), this, SLOT(slotScratchPadItemDeleted())); - connect(categoryView, SIGNAL(lastItemRemoved()), this, SLOT(slotLastScratchPadItemDeleted())); setItemWidget(embed_item, 0, categoryView); return categoryView; } int WidgetBoxTreeWidget::indexOfScratchpad() const { - if (const int numTopLevels = topLevelItemCount()) { - for (int i = numTopLevels - 1; i >= 0; --i) { - if (topLevelRole(topLevelItem(i)) == SCRATCHPAD_ITEM) - return i; - } - } return -1; } @@ -274,22 +213,11 @@ int WidgetBoxTreeWidget::indexOfCategory(const QString& name) const return -1; } -bool WidgetBoxTreeWidget::load(QDesignerWidgetBox::LoadMode loadMode) +bool WidgetBoxTreeWidget::load(const QString& file_name) { - switch (loadMode) { - case QDesignerWidgetBox::LoadReplace: - clear(); - break; - case QDesignerWidgetBox::LoadCustomWidgetsOnly: - addCustomCategories(true); - updateGeometries(); - return true; - default: - break; - } - const QString name = fileName(); + m_file_name = file_name; - QFile f(name); + QFile f(m_file_name); if (!f.open(QIODevice::ReadOnly)) // Might not exist at first startup return false; @@ -311,22 +239,6 @@ bool WidgetBoxTreeWidget::loadContents(const QString& contents) return true; } -void WidgetBoxTreeWidget::addCustomCategories(bool replace) -{ - if (replace) { - // clear out all existing custom widgets - if (const int numTopLevels = topLevelItemCount()) { - for (int t = 0; t < numTopLevels; ++t) - categoryViewAt(t)->removeCustomWidgets(); - } - } - // re-add - const CategoryList customList = loadCustomCategoryList(); - const CategoryList::const_iterator cend = customList.constEnd(); - for (CategoryList::const_iterator it = customList.constBegin(); it != cend; ++it) - addCategory(*it); -} - static inline QString msgXmlError(const QString& fileName, const QXmlStreamReader& r) { return QString("An error has been encountered at line %1 of %2: %3") @@ -367,9 +279,6 @@ bool WidgetBoxTreeWidget::readCategories(const QString& fileName, const QString& ignoreEntries = true; } else { Category category(categoryName); - if (attributes.value(QLatin1String(typeAttributeC)) - == QLatin1String(scratchPadValueC)) - category.setType(Category::Scratchpad); cats->push_back(category); } continue; @@ -381,10 +290,7 @@ bool WidgetBoxTreeWidget::readCategories(const QString& fileName, const QString& const QString widgetName = attr.value(QLatin1String(nameAttributeC)).toString(); const QString widgetIcon = attr.value(QLatin1String(iconAttributeC)).toString(); const WidgetBoxTreeWidget::Widget::Type widgetType = - attr.value(QLatin1String(typeAttributeC)).toString() - == QLatin1String(customValueC) - ? WidgetBoxTreeWidget::Widget::Custom - : WidgetBoxTreeWidget::Widget::Default; + WidgetBoxTreeWidget::Widget::Default; Widget w; w.setName(widgetName); @@ -513,75 +419,6 @@ bool WidgetBoxTreeWidget::readWidget(Widget* w, const QString& xml, QXmlStreamRe return true; } -void WidgetBoxTreeWidget::writeCategories(QXmlStreamWriter& writer, - const CategoryList& cat_list) const -{ - const QString widgetbox = QLatin1String(widgetBoxRootElementC); - const QString name = QLatin1String(nameAttributeC); - const QString type = QLatin1String(typeAttributeC); - const QString icon = QLatin1String(iconAttributeC); - const QString defaultType = QLatin1String(defaultTypeValueC); - const QString category = QLatin1String(categoryElementC); - const QString categoryEntry = QLatin1String(categoryEntryElementC); - const QString iconPrefix = QLatin1String(iconPrefixC); - const QString widgetTag = QLatin1String(widgetElementC); - - // - // <widgetbox> - // <category name="Layouts"> - // <categoryEntry name="Vertical Layout" type="default" icon="win/editvlayout.png"> - // <ui> - // ... - // </ui> - // </categoryEntry> - // ... - // </category> - // ... - // </widgetbox> - // - - writer.writeStartElement(widgetbox); - - for (const Category& cat : cat_list) { - writer.writeStartElement(category); - writer.writeAttribute(name, cat.name()); - if (cat.type() == Category::Scratchpad) - writer.writeAttribute(type, QLatin1String(scratchPadValueC)); - - const int widgetCount = cat.widgetCount(); - for (int i = 0; i < widgetCount; ++i) { - const Widget wgt = cat.widget(i); - if (wgt.type() == Widget::Custom) - continue; - - writer.writeStartElement(categoryEntry); - writer.writeAttribute(name, wgt.name()); - if (!wgt.iconName().startsWith(iconPrefix)) - writer.writeAttribute(icon, wgt.iconName()); - writer.writeAttribute(type, defaultType); - - const DomUI* domUI = QDesignerWidgetBox::xmlToUi( - wgt.name(), WidgetBoxCategoryListView::widgetDomXml(wgt), false); - if (domUI) { - domUI->write(writer); - delete domUI; - } - writer.writeEndElement(); // categoryEntry - } - writer.writeEndElement(); // categoryEntry - } - writer.writeEndElement(); // widgetBox -} - -WidgetBoxTreeWidget::CategoryList WidgetBoxTreeWidget::loadCustomCategoryList() const -{ - CategoryList result; - - std::cout << "WidgetBoxTreeWidget::loadCustomCategoryList() -> XXX Not implemented." - << std::endl; - return result; -} - void WidgetBoxTreeWidget::adjustSubListSize(QTreeWidgetItem* cat_item) { QTreeWidgetItem* embedItem = cat_item->child(0); @@ -597,108 +434,42 @@ void WidgetBoxTreeWidget::adjustSubListSize(QTreeWidgetItem* cat_item) embedItem->setSizeHint(0, QSize(-1, height - 1)); } -int WidgetBoxTreeWidget::categoryCount() const -{ - return topLevelItemCount(); -} - -WidgetBoxTreeWidget::Category WidgetBoxTreeWidget::category(int cat_idx) const -{ - if (cat_idx >= topLevelItemCount()) - return Category(); - - QTreeWidgetItem* cat_item = topLevelItem(cat_idx); - - QTreeWidgetItem* embedItem = cat_item->child(0); - WidgetBoxCategoryListView* categoryView = - static_cast<WidgetBoxCategoryListView*>(itemWidget(embedItem, 0)); - - Category result = categoryView->category(); - result.setName(cat_item->text(0)); - - switch (topLevelRole(cat_item)) { - case SCRATCHPAD_ITEM: - result.setType(Category::Scratchpad); - break; - default: - result.setType(Category::Default); - break; - } - return result; -} - void WidgetBoxTreeWidget::addCategory(const Category& cat) { if (cat.widgetCount() == 0) return; - const bool isScratchPad = cat.type() == Category::Scratchpad; WidgetBoxCategoryListView* categoryView; QTreeWidgetItem* cat_item; - if (isScratchPad) { - const int idx = ensureScratchpad(); - categoryView = categoryViewAt(idx); - cat_item = topLevelItem(idx); - } else { - const int existingIndex = indexOfCategory(cat.name()); - if (existingIndex == -1) { - cat_item = new QTreeWidgetItem(); - cat_item->setText(0, cat.name()); - setTopLevelRole(NORMAL_ITEM, cat_item); - // insert before scratchpad - const int scratchPadIndex = indexOfScratchpad(); - if (scratchPadIndex == -1) { - addTopLevelItem(cat_item); - } else { - insertTopLevelItem(scratchPadIndex, cat_item); - } + const int existingIndex = indexOfCategory(cat.name()); + if (existingIndex == -1) { + cat_item = new QTreeWidgetItem(); + cat_item->setText(0, cat.name()); + setTopLevelRole(NORMAL_ITEM, cat_item); + addTopLevelItem(cat_item); #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) - cat_item->setExpanded(true); + cat_item->setExpanded(true); #else - setItemExpanded(cat_item, true); + setItemExpanded(cat_item, true); #endif - categoryView = addCategoryView(cat_item, m_iconMode); - } else { - categoryView = categoryViewAt(existingIndex); - cat_item = topLevelItem(existingIndex); - } + categoryView = addCategoryView(cat_item, m_iconMode); + } else { + categoryView = categoryViewAt(existingIndex); + cat_item = topLevelItem(existingIndex); } + // The same categories are read from the file $HOME, avoid duplicates const int widgetCount = cat.widgetCount(); for (int i = 0; i < widgetCount; ++i) { const Widget w = cat.widget(i); if (!categoryView->containsWidget(w.name())) - categoryView->addWidget(w, iconForWidget(w.iconName()), isScratchPad); + categoryView->addWidget(w, iconForWidget(w.iconName())); } adjustSubListSize(cat_item); } -void WidgetBoxTreeWidget::removeCategory(int cat_idx) -{ - if (cat_idx >= topLevelItemCount()) - return; - delete takeTopLevelItem(cat_idx); -} - -int WidgetBoxTreeWidget::widgetCount(int cat_idx) const -{ - if (cat_idx >= topLevelItemCount()) - return 0; - // SDK functions want unfiltered access - return categoryViewAt(cat_idx)->count(WidgetBoxCategoryListView::UNFILTERED); -} - -WidgetBoxTreeWidget::Widget WidgetBoxTreeWidget::widget(int cat_idx, int wgt_idx) const -{ - if (cat_idx >= topLevelItemCount()) - return Widget(); - // SDK functions want unfiltered access - WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx); - return categoryView->widgetAt(WidgetBoxCategoryListView::UNFILTERED, wgt_idx); -} - void WidgetBoxTreeWidget::addWidget(int cat_idx, const Widget& wgt) { if (cat_idx >= topLevelItemCount()) @@ -707,56 +478,10 @@ void WidgetBoxTreeWidget::addWidget(int cat_idx, const Widget& wgt) QTreeWidgetItem* cat_item = topLevelItem(cat_idx); WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx); - const bool scratch = topLevelRole(cat_item) == SCRATCHPAD_ITEM; - categoryView->addWidget(wgt, iconForWidget(wgt.iconName()), scratch); + categoryView->addWidget(wgt, iconForWidget(wgt.iconName())); adjustSubListSize(cat_item); } -void WidgetBoxTreeWidget::removeWidget(int cat_idx, int wgt_idx) -{ - if (cat_idx >= topLevelItemCount()) - return; - - WidgetBoxCategoryListView* categoryView = categoryViewAt(cat_idx); - - // SDK functions want unfiltered access - const WidgetBoxCategoryListView::EAccessMode am = WidgetBoxCategoryListView::UNFILTERED; - if (wgt_idx >= categoryView->count(am)) - return; - - categoryView->removeRow(am, wgt_idx); -} - -void WidgetBoxTreeWidget::slotScratchPadItemDeleted() -{ - const int scratch_idx = indexOfScratchpad(); - QTreeWidgetItem* scratch_item = topLevelItem(scratch_idx); - adjustSubListSize(scratch_item); - save(); -} - -void WidgetBoxTreeWidget::slotLastScratchPadItemDeleted() -{ - // Remove the scratchpad in the next idle loop - if (!m_scratchPadDeleteTimer) { - m_scratchPadDeleteTimer = new QTimer(this); - m_scratchPadDeleteTimer->setSingleShot(true); - m_scratchPadDeleteTimer->setInterval(0); - connect(m_scratchPadDeleteTimer, SIGNAL(timeout()), this, SLOT(deleteScratchpad())); - } - if (!m_scratchPadDeleteTimer->isActive()) - m_scratchPadDeleteTimer->start(); -} - -void WidgetBoxTreeWidget::deleteScratchpad() -{ - const int idx = indexOfScratchpad(); - if (idx == -1) - return; - delete takeTopLevelItem(idx); - save(); -} - void WidgetBoxTreeWidget::slotListMode() { m_iconMode = false; @@ -773,11 +498,8 @@ void WidgetBoxTreeWidget::updateViewMode() { if (const int numTopLevels = topLevelItemCount()) { for (int i = numTopLevels - 1; i >= 0; --i) { - QTreeWidgetItem* topLevel = topLevelItem(i); - // Scratch pad stays in list mode. const QListView::ViewMode viewMode = - m_iconMode && (topLevelRole(topLevel) != SCRATCHPAD_ITEM) ? QListView::IconMode - : QListView::ListMode; + m_iconMode ? QListView::IconMode : QListView::ListMode; WidgetBoxCategoryListView* categoryView = categoryViewAt(i); if (viewMode != categoryView->viewMode()) { categoryView->setViewMode(viewMode); @@ -801,9 +523,6 @@ void WidgetBoxTreeWidget::contextMenuEvent(QContextMenuEvent* e) { QTreeWidgetItem* item = itemAt(e->pos()); - const bool scratchpad_menu = item != nullptr && item->parent() != nullptr - && topLevelRole(item->parent()) == SCRATCHPAD_ITEM; - QMenu menu; menu.addAction("Expand all", this, SLOT(expandAll())); menu.addAction("Collapse all", this, SLOT(collapseAll())); @@ -823,81 +542,10 @@ void WidgetBoxTreeWidget::contextMenuEvent(QContextMenuEvent* e) connect(listModeAction, SIGNAL(triggered()), SLOT(slotListMode())); connect(iconModeAction, SIGNAL(triggered()), SLOT(slotIconMode())); - if (scratchpad_menu) { - menu.addSeparator(); - menu.addAction("Remove", itemWidget(item, 0), SLOT(removeCurrentItem())); - if (!m_iconMode) - menu.addAction("Edit name", itemWidget(item, 0), SLOT(editCurrentItem())); - } e->accept(); menu.exec(mapToGlobal(e->pos())); } -void WidgetBoxTreeWidget::dropWidgets(const QList<QDesignerDnDItemInterface*>& item_list) -{ - QTreeWidgetItem* scratch_item = nullptr; - WidgetBoxCategoryListView* categoryView = nullptr; - bool added = false; - - for (QDesignerDnDItemInterface* item : item_list) { - QWidget* w = item->widget(); - if (w == nullptr) - continue; - - DomUI* dom_ui = item->domUi(); - if (dom_ui == nullptr) - continue; - - const int scratch_idx = ensureScratchpad(); - scratch_item = topLevelItem(scratch_idx); - categoryView = categoryViewAt(scratch_idx); - - // Temporarily remove the fake toplevel in-between - DomWidget* fakeTopLevel = dom_ui->takeElementWidget(); - DomWidget* firstWidget = nullptr; - if (fakeTopLevel && !fakeTopLevel->elementWidget().isEmpty()) { - firstWidget = fakeTopLevel->elementWidget().first(); - dom_ui->setElementWidget(firstWidget); - } else { - dom_ui->setElementWidget(fakeTopLevel); - continue; - } - // Serialize to XML - QString xml; - { - QXmlStreamWriter writer(&xml); - writer.setAutoFormatting(true); - writer.setAutoFormattingIndent(1); - writer.writeStartDocument(); - dom_ui->write(writer); - writer.writeEndDocument(); - } - // Insert fake toplevel again - dom_ui->takeElementWidget(); - dom_ui->setElementWidget(fakeTopLevel); - - const Widget wgt = Widget(w->objectName(), xml); - categoryView->addWidget(wgt, iconForWidget(wgt.iconName()), true); -#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) - scratch_item->setExpanded(true); -#else - setItemExpanded(scratch_item, true); -#endif - - added = true; - } - if (added) { - save(); - QApplication::setActiveWindow(this); - // Is the new item visible in filtered mode? - const WidgetBoxCategoryListView::EAccessMode am = WidgetBoxCategoryListView::FILTERED; - if (const int count = categoryView->count(am)) - categoryView->setCurrentItem(am, count - 1); - categoryView->adjustSize(); // XXX - adjustSubListSize(scratch_item); - } -} - void WidgetBoxTreeWidget::filter(const QString& f) { const bool empty = f.isEmpty(); diff --git a/GUI/Views/widgetbox/widgetboxtreewidget.h b/GUI/Views/widgetbox/widgetboxtreewidget.h index c44e7d655a111bddef0f2f7f8f694ffa17b91702..d524455bfb615ed94ad1968193e815ae28f06163 100644 --- a/GUI/Views/widgetbox/widgetboxtreewidget.h +++ b/GUI/Views/widgetbox/widgetboxtreewidget.h @@ -74,24 +74,10 @@ public: explicit WidgetBoxTreeWidget(QWidget* parent = 0); virtual ~WidgetBoxTreeWidget(); - int categoryCount() const; - Category category(int cat_idx) const; void addCategory(const Category& cat); - void removeCategory(int cat_idx); - - int widgetCount(int cat_idx) const; - Widget widget(int cat_idx, int wgt_idx) const; void addWidget(int cat_idx, const Widget& wgt); - void removeWidget(int cat_idx, int wgt_idx); - - void dropWidgets(const QList<QDesignerDnDItemInterface*>& item_list); - void setFileName(const QString& file_name); - QString fileName() const; - bool load(QDesignerWidgetBox::LoadMode loadMode); - bool loadContents(const QString& contents); - bool save(); - QIcon iconForWidget(QString iconName) const; + bool load(const QString& file_name); signals: void pressed(const QString name, const QString dom_xml, const QPoint& global_mouse_pos); @@ -104,16 +90,14 @@ protected: virtual void resizeEvent(QResizeEvent* e); private slots: - void slotSave(); - void slotScratchPadItemDeleted(); - void slotLastScratchPadItemDeleted(); void handleMousePress(QTreeWidgetItem* item); - void deleteScratchpad(); void slotListMode(); void slotIconMode(); private: + bool loadContents(const QString& contents); + QIcon iconForWidget(QString iconName) const; WidgetBoxCategoryListView* addCategoryView(QTreeWidgetItem* parent, bool iconMode); WidgetBoxCategoryListView* categoryViewAt(int idx) const; void adjustSubListSize(QTreeWidgetItem* cat_item); @@ -122,13 +106,8 @@ private: QString* errorMessage); static bool readWidget(Widget* w, const QString& xml, QXmlStreamReader& r); - CategoryList loadCustomCategoryList() const; - void writeCategories(QXmlStreamWriter& writer, const CategoryList& cat_list) const; - int indexOfCategory(const QString& name) const; int indexOfScratchpad() const; - int ensureScratchpad(); - void addCustomCategories(bool replace); void saveExpandedState() const; void restoreExpandedState(); @@ -138,7 +117,6 @@ private: typedef QHash<QString, QIcon> IconCache; mutable IconCache m_pluginIcons; bool m_iconMode; - QTimer* m_scratchPadDeleteTimer; }; } // namespace qdesigner_internal