File indexing completed on 2025-02-02 04:11:25

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "select_shape_dialog.hpp"
0008 #include "ui_select_shape_dialog.h"
0009 #include <QEvent>
0010 
0011 #include <QtColorWidgets/ColorDelegate>
0012 
0013 #include "item_models/node_type_proxy_model.hpp"
0014 
0015 class glaxnimate::gui::SelectShapeDialog::Private
0016 {
0017 public:
0018     Private(item_models::DocumentNodeModel* model)
0019         : proxy(model)
0020     {
0021         proxy.allow<model::Shape>();
0022     }
0023 
0024     color_widgets::ReadOnlyColorDelegate color_delegate;
0025     item_models::NodeTypeProxyModel proxy;
0026     Ui::SelectShapeDialog ui;
0027     model::Shape* selected = nullptr;
0028 };
0029 
0030 glaxnimate::gui::SelectShapeDialog::SelectShapeDialog(item_models::DocumentNodeModel* model, QWidget* parent)
0031     : QDialog(parent), d(std::make_unique<Private>(model))
0032 {
0033     d->ui.setupUi(this);
0034     d->ui.view_document_node->setModel(&d->proxy);
0035     d->ui.view_document_node->expandAll();
0036     d->ui.view_document_node->header()->setSectionResizeMode(item_models::DocumentNodeModel::ColumnName, QHeaderView::Stretch);
0037     d->ui.view_document_node->header()->hideSection(item_models::DocumentNodeModel::ColumnVisible);
0038     d->ui.view_document_node->header()->hideSection(item_models::DocumentNodeModel::ColumnLocked);
0039     d->ui.view_document_node->header()->hideSection(item_models::DocumentNodeModel::ColumnUsers);
0040     d->ui.view_document_node->header()->setSectionResizeMode(item_models::DocumentNodeModel::ColumnColor, QHeaderView::ResizeToContents);
0041     d->ui.view_document_node->setItemDelegateForColumn(item_models::DocumentNodeModel::ColumnColor, &d->color_delegate);
0042 }
0043 
0044 glaxnimate::gui::SelectShapeDialog::~SelectShapeDialog() = default;
0045 
0046 void glaxnimate::gui::SelectShapeDialog::changeEvent ( QEvent* e )
0047 {
0048     QDialog::changeEvent(e);
0049 
0050     if ( e->type() == QEvent::LanguageChange)
0051     {
0052         d->ui.retranslateUi(this);
0053     }
0054 }
0055 
0056 void glaxnimate::gui::SelectShapeDialog::select(const QModelIndex& index)
0057 {
0058     auto node = d->proxy.node(index);
0059     if ( !node )
0060         d->selected = nullptr;
0061     else
0062         d->selected = node->cast<model::Shape>();
0063 }
0064 
0065 
0066 glaxnimate::model::Shape * glaxnimate::gui::SelectShapeDialog::shape() const
0067 {
0068     return d->selected;
0069 }
0070 
0071 void glaxnimate::gui::SelectShapeDialog::set_shape(model::Shape* shape)
0072 {
0073     if ( shape )
0074     {
0075         auto index = d->proxy.mapFromSource(
0076             d->proxy.source_model()->node_index(shape)
0077         );
0078         if ( index.isValid() )
0079         {
0080             d->selected = shape;
0081             d->ui.view_document_node->setCurrentIndex(index);
0082         }
0083     }
0084     else
0085     {
0086         d->selected = nullptr;
0087         d->ui.view_document_node->setCurrentIndex({});
0088     }
0089 }