File indexing completed on 2024-05-05 04:48:48

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2010 Soren Harward <stharward@gmail.com>                          *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #define DEBUG_PREFIX "APG::PresetEditDialog"
0018 
0019 #include "PresetEditDialog.h"
0020 
0021 #include "ConstraintNode.h"
0022 #include "ConstraintFactory.h"
0023 #include "TreeController.h"
0024 #include "TreeModel.h"
0025 
0026 #include "core/support/Debug.h"
0027 
0028 #include <QMenu>
0029 #include <QMenuBar>
0030 #include <QSignalMapper>
0031 #include <QWhatsThis>
0032 
0033 APG::PresetEditDialog::PresetEditDialog( const PresetPtr &p )
0034             : QDialog( nullptr )
0035             , m_preset( p )
0036 {
0037     DEBUG_BLOCK
0038 
0039     ui.setupUi( this );
0040 
0041     TreeModel* model = new TreeModel( m_preset->constraintTreeRoot(), this );
0042     m_controller = new TreeController( model, ui.constraintTreeView, this );
0043 
0044     ui.lineEdit_Title->setText( m_preset->title() );
0045 
0046     ui.constraintTreeView->setModel( model );
0047     ui.constraintTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
0048     ui.constraintTreeView->setHeaderHidden( true );
0049     connect( ui.constraintTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
0050              this, &PresetEditDialog::currentNodeChanged );
0051     ui.constraintTreeView->setCurrentIndex( model->index( 0, 0 ) ); // select the visible root constraint
0052     ui.constraintTreeView->expandAll();
0053 
0054     QSignalMapper* adderMapper = new QSignalMapper( this );
0055     connect( adderMapper, QOverload<const QString&>::of(&QSignalMapper::mapped),
0056              this, &PresetEditDialog::addNode );
0057 
0058     QMenuBar* menuBar_Actions = new QMenuBar( this );
0059     menuBar_Actions->setNativeMenuBar( false );  //required to make the menu work on OS X
0060 
0061     QAction* a;
0062     QMenu* m = new QMenu( i18n("Add new"), this );
0063     a = m->addAction( i18n("Constraint Group") );
0064     connect( a, &QAction::triggered, adderMapper, QOverload<>::of(&QSignalMapper::map) );
0065     adderMapper->setMapping( a, i18n("Constraint Group") );
0066     foreach( const QString& name, ConstraintFactory::instance()->i18nNames() ) {
0067         a = m->addAction( name );
0068         connect( a, &QAction::triggered, adderMapper, QOverload<>::of(&QSignalMapper::map) );
0069         adderMapper->setMapping( a, name );
0070     }
0071     menuBar_Actions->addMenu( m );
0072 
0073     a = menuBar_Actions->addAction( i18n("Remove selected") );
0074     connect( a, &QAction::triggered, m_controller, &APG::TreeController::removeNode );
0075 
0076     menuBar_Actions->addSeparator();
0077 
0078     a = QWhatsThis::createAction( this );
0079     a->setIcon( QIcon() );
0080     menuBar_Actions->addAction( a );
0081     ui.treeLayout->insertWidget( 0, menuBar_Actions );
0082 
0083     connect( ui.buttonBox, &QDialogButtonBox::accepted, this, &APG::PresetEditDialog::accept );
0084     connect( ui.buttonBox, &QDialogButtonBox::rejected, this, &APG::PresetEditDialog::reject );
0085 
0086     QMetaObject::connectSlotsByName( this );
0087 }
0088 
0089 void
0090 APG::PresetEditDialog::addNode( const QString& name )
0091 {
0092     debug() << "Adding new" << name;
0093     if ( name == i18n("Constraint Group") ) {
0094         m_controller->addGroup();
0095     } else {
0096         m_controller->addConstraint( name );
0097     }
0098 }
0099 
0100 void
0101 APG::PresetEditDialog::removeNode()
0102 {
0103     debug() << "Removing selected node";
0104     m_controller->removeNode();
0105 }
0106 
0107 void
0108 APG::PresetEditDialog::currentNodeChanged( const QModelIndex& index )
0109 {
0110     if( index.isValid() )
0111     {
0112         ConstraintNode* n = static_cast<ConstraintNode*>( index.internalPointer() );
0113         if ( !m_widgetStackPages.contains( n ) ) {
0114             debug() << "Inserting new constraint edit widget into the stack";
0115             QWidget* w = n->editWidget();
0116             m_widgetStackPages.insert( n, ui.stackedWidget_Editors->addWidget( w ) );
0117         }
0118         ui.stackedWidget_Editors->setCurrentIndex( m_widgetStackPages.value( n ) );
0119     }
0120 }
0121 
0122 void
0123 APG::PresetEditDialog::accept()
0124 {
0125     QDialog::accept();
0126 }
0127 
0128 void
0129 APG::PresetEditDialog::reject()
0130 {
0131     QDialog::reject();
0132 }
0133 
0134 void
0135 APG::PresetEditDialog::on_lineEdit_Title_textChanged( const QString& t )
0136 {
0137     m_preset->setTitle( t );
0138 }