File indexing completed on 2024-05-05 03:50:39

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Stanciu Marius-Valeriu <stanciumarius94@gmail.com>
0004 //
0005 
0006 // Self
0007 #include "NodeItemDelegate.h"
0008 
0009 // Qt
0010 #include <QSize>
0011 #include <QStyleOptionViewItem>
0012 
0013 // Marble
0014 #include "LatLonEdit.h"
0015 #include "GeoDataPlacemark.h"
0016 #include "GeoDataLineString.h"
0017 #include "GeoDataLinearRing.h"
0018 #include "GeoDataPolygon.h"
0019 
0020 namespace  Marble
0021 {
0022 
0023 QSize NodeItemDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
0024 {
0025     Q_UNUSED( option );
0026     Q_UNUSED( index );
0027     return QSize( 25, 25 );
0028 }
0029 
0030 NodeItemDelegate::NodeItemDelegate( GeoDataPlacemark* placemark, QTreeView* view ):
0031     m_placemark( placemark ), m_view( view )
0032 {
0033 }
0034 
0035 QWidget* NodeItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
0036 {
0037     Q_UNUSED( option );
0038     Q_UNUSED( index );
0039     LatLonEdit *editor = new LatLonEdit( parent );
0040     connect( this, SIGNAL(closeEditor(QWidget*)),
0041              this, SLOT(unsetCurrentEditor(QWidget*)) );
0042     return editor;
0043 }
0044 
0045 void NodeItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0046 {
0047 
0048     LatLonEdit *latLonEditWidget = static_cast<LatLonEdit*>(editor);
0049     qreal value = 0;
0050 
0051     if (const auto polygon = geodata_cast<GeoDataPolygon>(m_placemark->geometry())) {
0052         GeoDataLinearRing outerBoundary = polygon->outerBoundary();
0053 
0054         // Setting the latlonedit spinboxes values
0055         if( index.column() == 1 ) {
0056             latLonEditWidget->setDimension(LatLonEdit::Longitude);
0057             value = outerBoundary.at( index.row() ).longitude( GeoDataCoordinates::Degree );
0058         }
0059         else {
0060             latLonEditWidget->setDimension(LatLonEdit::Latitude);
0061             value = outerBoundary.at( index.row() ).latitude( GeoDataCoordinates::Degree );
0062         }
0063     }
0064     else if (const auto lineString = geodata_cast<GeoDataLineString>(m_placemark->geometry())) {
0065         // Setting the latlonedit spinboxes values
0066         if( index.column() == 1 ) {
0067             latLonEditWidget->setDimension(LatLonEdit::Longitude);
0068             value = lineString->at( index.row() ).longitude( GeoDataCoordinates::Degree );
0069         }
0070         else {
0071             latLonEditWidget->setDimension(LatLonEdit::Latitude);
0072             value = lineString->at( index.row() ).latitude(GeoDataCoordinates::Degree );
0073         }
0074     }
0075 
0076     latLonEditWidget->setValue( value );
0077 
0078     connect( latLonEditWidget, SIGNAL(valueChanged(qreal)),
0079              this, SLOT(previewNodeMove(qreal)) );
0080     m_indexBeingEdited = index;
0081 
0082 }
0083 
0084 void NodeItemDelegate::setModelData( QWidget* editor, QAbstractItemModel *model, const QModelIndex &index ) const
0085 {
0086     Q_UNUSED( editor );
0087     Q_UNUSED( model );
0088     Q_UNUSED( index );
0089 
0090     // The dialogs already have a function that updates the NodeModel
0091     emit modelChanged( m_placemark );
0092 }
0093 
0094 void NodeItemDelegate::previewNodeMove( qreal value )
0095 {
0096     if (const auto polygon = geodata_cast<GeoDataPolygon>(m_placemark->geometry())) {
0097         GeoDataLinearRing outerBoundary = polygon->outerBoundary();
0098 
0099         GeoDataCoordinates* coordinates = new GeoDataCoordinates( outerBoundary[m_indexBeingEdited.row()] );
0100 
0101         if( m_indexBeingEdited.column() == 1) {
0102             coordinates->setLongitude( value, GeoDataCoordinates::Degree );
0103         }
0104         else {
0105             coordinates->setLatitude( value, GeoDataCoordinates::Degree );
0106         }
0107 
0108         outerBoundary[ m_indexBeingEdited.row() ] = *coordinates;
0109         polygon->setOuterBoundary( outerBoundary );
0110     }
0111     else if (const auto lineString = geodata_cast<GeoDataLineString>(m_placemark->geometry())) {
0112         GeoDataCoordinates* coordinates = new GeoDataCoordinates( lineString->at( m_indexBeingEdited.row() ) );
0113 
0114         if( m_indexBeingEdited.column() == 1) {
0115             coordinates->setLongitude( value, GeoDataCoordinates::Degree );
0116         }
0117         else {
0118             coordinates->setLatitude( value, GeoDataCoordinates::Degree );
0119         }
0120 
0121         lineString->at( m_indexBeingEdited.row() ) = *coordinates;
0122 
0123     }
0124 
0125     // Updating changes ( repainting graphics )
0126     emit geometryChanged();
0127 }
0128 
0129 void NodeItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
0130 {
0131     if( index.column() == 1) {
0132          m_view->setColumnWidth( 1, 200 );
0133          m_view->setColumnWidth( 2, 100 );
0134     }
0135     else {
0136         m_view->setColumnWidth( 2, 200 );
0137         m_view->setColumnWidth( 1, 100 );
0138     }
0139 
0140     editor->setGeometry( option.rect );
0141 }
0142 
0143 void NodeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0144 {
0145     drawBackground( painter, option, index );
0146 
0147     // The LatLonEdit widget is transparent, so we have to disable the text behind it
0148     // for esthetic reasons.
0149     if ( !( index == m_indexBeingEdited ) || !( index == m_view->currentIndex() ) ) {
0150         drawDisplay( painter, option, option.rect, index.data().toString() );
0151     }
0152 }
0153 
0154 void NodeItemDelegate::unsetCurrentEditor(QWidget* widget)
0155 {
0156     Q_UNUSED( widget );
0157     m_indexBeingEdited = QModelIndex();
0158     m_view->viewport()->update();
0159 }
0160 
0161 }
0162 
0163 #include "moc_NodeItemDelegate.cpp"