File indexing completed on 2024-04-21 03:49:53

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Mihail Ivchenko <ematirov@gmail.com>
0004 //
0005 
0006 #include "RemoveItemEditWidget.h"
0007 
0008 #include <QLabel>
0009 #include <QHBoxLayout>
0010 
0011 #include "MarbleWidget.h"
0012 #include "geodata/data/GeoDataAnimatedUpdate.h"
0013 #include "GeoDataUpdate.h"
0014 #include "GeoDataDelete.h"
0015 #include "MarblePlacemarkModel.h"
0016 
0017 namespace Marble {
0018 
0019 RemoveItemEditWidget::RemoveItemEditWidget( const QModelIndex &index, QWidget *parent ) :
0020     QWidget( parent ),
0021     m_index( index ),
0022     m_button( new QToolButton ),
0023     m_comboBox( new QComboBox )
0024 {
0025     QHBoxLayout *layout = new QHBoxLayout;
0026     layout->setSpacing( 5 );
0027 
0028     QLabel* iconLabel = new QLabel;
0029     iconLabel->setPixmap(QPixmap(QStringLiteral(":/icons/remove.png")));
0030     layout->addWidget( iconLabel );
0031 
0032     QLabel* comboBoxLabel = new QLabel;
0033     comboBoxLabel->setText( tr( "Choose item:" ) );
0034     layout->addWidget( comboBoxLabel );
0035 
0036     layout->addWidget( m_comboBox );
0037 
0038     m_button->setIcon(QIcon(QStringLiteral(":/marble/document-save.png")));
0039     connect(m_button, SIGNAL(clicked()), this, SLOT(save()));
0040     layout->addWidget( m_button );
0041 
0042     setLayout( layout );
0043 }
0044 
0045 bool RemoveItemEditWidget::editable() const
0046 {
0047     return m_button->isEnabled();
0048 }
0049 
0050 void RemoveItemEditWidget::setFeatureIds( const QStringList &ids )
0051 {
0052     QString id = animatedUpdateElement()->update()->getDelete()->first().targetId();
0053     QString current = m_comboBox->currentIndex() == -1 ? id : m_comboBox->currentText();
0054     m_comboBox->clear();
0055     m_comboBox->addItems( ids );
0056     m_comboBox->setCurrentIndex( m_comboBox->findText( current ) );
0057 }
0058 
0059 void RemoveItemEditWidget::setDefaultFeatureId( const QString &featureId )
0060 {
0061     if( m_comboBox->currentIndex() == -1 ) {
0062         m_comboBox->setCurrentIndex( m_comboBox->findText( featureId ) );
0063     }
0064 }
0065 
0066 void RemoveItemEditWidget::setEditable( bool editable )
0067 {
0068     m_button->setEnabled( editable );
0069 }
0070 
0071 void RemoveItemEditWidget::save()
0072 {
0073     animatedUpdateElement()->update()->getDelete()->child(0)->setTargetId( m_comboBox->currentText() );
0074     emit editingDone(m_index);
0075 }
0076 
0077 GeoDataAnimatedUpdate* RemoveItemEditWidget::animatedUpdateElement()
0078 {
0079     GeoDataObject *object = qvariant_cast<GeoDataObject*>(m_index.data( MarblePlacemarkModel::ObjectPointerRole ) );
0080     Q_ASSERT( object );
0081     auto animatedUpdate = geodata_cast<GeoDataAnimatedUpdate>(object);
0082     Q_ASSERT(animatedUpdate);
0083     return animatedUpdate;
0084 }
0085 
0086 } // namespace Marble
0087 
0088 #include "moc_RemoveItemEditWidget.cpp"