Marble

FileViewWidget.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2010 Bastian Holst <bastianholst@gmx.de>
4//
5
6// Self
7#include "FileViewWidget.h"
8
9// Qt
10#include <QFileDialog>
11#include <QMenu>
12#include <QAction>
13#include <QPointer>
14
15// Marble
16#include "GeoDataLatLonAltBox.h"
17#include "GeoDataContainer.h"
18#include "GeoDataDocument.h"
19#include "GeoDataDocumentWriter.h"
20#include "GeoDataPlacemark.h"
21#include "GeoDataTreeModel.h"
22#include "FileManager.h"
23#include "KmlElementDictionary.h"
24#include "MarblePlacemarkModel.h"
25#include "MarbleModel.h"
26#include "MarbleWidget.h"
27#include "TreeViewDecoratorModel.h"
28#include "EditPlacemarkDialog.h"
29
30using namespace Marble;
31// Ui
32#include "ui_FileViewWidget.h"
33
34namespace Marble
35{
36
37class FileViewWidgetPrivate
38{
39
40 public:
41 explicit FileViewWidgetPrivate( FileViewWidget *parent );
42 void setTreeModel( GeoDataTreeModel *model );
43 void setFileManager( FileManager *manager );
44
45
46 public Q_SLOTS:
47 void saveFile();
48 void closeFile();
49 void enableFileViewActions();
50 void contextMenu(const QPoint &pt);
51 void showPlacemarkDialog();
52
53 public:
54 FileViewWidget *q;
55 Ui::FileViewWidget m_fileViewUi;
56 MarbleWidget *m_widget;
57 TreeViewDecoratorModel m_treeSortProxy;
58 FileManager *m_fileManager;
59
60 QMenu *m_contextMenu;
61 QAction *m_viewPropertiesAction;
62};
63
64FileViewWidgetPrivate::FileViewWidgetPrivate( FileViewWidget *parent )
65 :q( parent ),
66 m_widget( nullptr ),
67 m_fileManager( nullptr )
68{
69 m_contextMenu = new QMenu(q);
70 m_viewPropertiesAction = new QAction(q);
71 m_viewPropertiesAction->setText(QObject::tr("View Properties"));
72 m_contextMenu->addAction(m_viewPropertiesAction);
73 QObject::connect(m_viewPropertiesAction, SIGNAL(triggered()),
74 q, SLOT(showPlacemarkDialog()));
75}
76
77FileViewWidget::FileViewWidget( QWidget *parent, Qt::WindowFlags f )
78 : QWidget( parent, f ),
79 d( new FileViewWidgetPrivate( this ) )
80{
81 d->m_fileViewUi.setupUi( this );
82 layout()->setMargin( 0 );
83}
84
85FileViewWidget::~FileViewWidget()
86{
87 delete d;
88}
89
90void FileViewWidget::setMarbleWidget( MarbleWidget *widget )
91{
92 d->m_widget = widget;
93 d->setTreeModel( d->m_widget->model()->treeModel() );
94 d->setFileManager( d->m_widget->model()->fileManager() );
95
96 connect( this, SIGNAL(centerOn(GeoDataPlacemark,bool)),
97 d->m_widget, SLOT(centerOn(GeoDataPlacemark,bool)) );
98 connect( this, SIGNAL(centerOn(GeoDataLatLonBox,bool)),
99 d->m_widget, SLOT(centerOn(GeoDataLatLonBox,bool)) );
100}
101
102void FileViewWidgetPrivate::setTreeModel( GeoDataTreeModel *model )
103{
104 m_treeSortProxy.setSourceModel( model );
105 m_treeSortProxy.setDynamicSortFilter( true );
106 m_fileViewUi.m_treeView->setModel( &m_treeSortProxy );
107 m_fileViewUi.m_treeView->setSortingEnabled( true );
108 m_fileViewUi.m_treeView->sortByColumn( 0, Qt::AscendingOrder );
109 m_fileViewUi.m_treeView->resizeColumnToContents( 3 );
110 m_fileViewUi.m_treeView->setColumnWidth(0, 200);
111 m_fileViewUi.m_treeView->setColumnWidth(1, 80);
112 m_fileViewUi.m_treeView->setColumnWidth(2, 50);
113 m_fileViewUi.m_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
114
115 QObject::connect( m_fileViewUi.m_treeView,
116 SIGNAL(expanded(QModelIndex)),
117 &m_treeSortProxy, SLOT(trackExpandedState(QModelIndex)) );
118 QObject::connect( m_fileViewUi.m_treeView,
119 SIGNAL(collapsed(QModelIndex)),
120 &m_treeSortProxy, SLOT(trackCollapsedState(QModelIndex)) );
121 QObject::connect( m_fileViewUi.m_treeView->selectionModel(),
122 SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
123 q, SLOT(enableFileViewActions()) );
124 QObject::connect( m_fileViewUi.m_treeView, SIGNAL(activated(QModelIndex)),
125 q, SLOT(mapCenterOnTreeViewModel(QModelIndex)) );
126 QObject::connect( m_fileViewUi.m_treeView, SIGNAL(customContextMenuRequested(QPoint)),
127 q, SLOT(contextMenu(QPoint)) );
128}
129
130void FileViewWidgetPrivate::setFileManager( FileManager *manager )
131{
132 m_fileManager = manager;
133 QObject::connect( m_fileViewUi.m_saveButton, SIGNAL(clicked()) ,
134 q, SLOT(saveFile()) );
135 QObject::connect( m_fileViewUi.m_closeButton, SIGNAL(clicked()) ,
136 q, SLOT(closeFile()) );
137}
138
139void FileViewWidgetPrivate::saveFile()
140{
141 QModelIndex index = m_fileViewUi.m_treeView->selectionModel()->selectedRows().first();
142 const GeoDataObject *object
144 const GeoDataDocument *document = geodata_cast<GeoDataDocument>(object);
145 if ( document && !document->fileName().isEmpty() ) {
146 const QString saveFileName = QFileDialog::getSaveFileName(q, QObject::tr("Select filename for KML document"));
147 GeoDataDocumentWriter::write(saveFileName, *document, kml::kmlTag_nameSpaceOgc22);
148 }
149}
150
151void FileViewWidgetPrivate::closeFile()
152{
153 QModelIndex index = m_fileViewUi.m_treeView->selectionModel()->selectedRows().first();
154 GeoDataObject *object
156 GeoDataDocument *document = geodata_cast<GeoDataDocument>(object);
157 if ( document ) {
158 m_fileManager->closeFile( document );
159 }
160}
161
162void FileViewWidgetPrivate::enableFileViewActions()
163{
164 bool isUserDocument = false;
165 if ( !m_fileViewUi.m_treeView->selectionModel()->selectedRows().isEmpty() ) {
166 QModelIndex index = m_fileViewUi.m_treeView->selectionModel()->selectedRows().first();
167 const GeoDataObject *object
169 const GeoDataDocument *document = geodata_cast<GeoDataDocument>(object);
170 if ( document ) {
171 isUserDocument = document->documentRole() == Marble::UserDocument;
172 }
173 }
174 m_fileViewUi.m_saveButton->setEnabled( isUserDocument );
175 m_fileViewUi.m_closeButton->setEnabled( isUserDocument );
176}
177
178void FileViewWidgetPrivate::contextMenu(const QPoint &pt)
179{
180 const QModelIndex index = m_fileViewUi.m_treeView->indexAt(pt);
181 const QAbstractItemModel *model = m_fileViewUi.m_treeView->model();
182 if (index.isValid()) {
184 const GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(obj);
185
186 if (placemark) {
187 m_contextMenu->popup(m_fileViewUi.m_treeView->mapToGlobal(pt));
188 }
189 }
190}
191
192void FileViewWidgetPrivate::showPlacemarkDialog()
193{
194 const QModelIndex index = m_fileViewUi.m_treeView->currentIndex();
195 const QAbstractItemModel *model = m_fileViewUi.m_treeView->model();
196
198 GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(obj);
199 if (placemark) {
200 QPointer<EditPlacemarkDialog> dialog = new EditPlacemarkDialog(placemark, nullptr, q);
201 dialog->setReadOnly(true);
202 dialog->exec();
203 delete dialog;
204 }
205}
206
207void FileViewWidget::mapCenterOnTreeViewModel( const QModelIndex &index )
208{
209 if( !index.isValid() ) {
210 return;
211 }
212
213 const GeoDataObject *object
215 if (const GeoDataPlacemark *placemark = geodata_cast<GeoDataPlacemark>(object)) {
216 d->m_widget->model()->placemarkSelectionModel()->select( index, QItemSelectionModel::ClearAndSelect );
217 emit centerOn( *placemark, true );
218 }
219 else if (const GeoDataContainer *container = dynamic_cast<const GeoDataContainer *>(object)) {
220 const GeoDataLatLonAltBox box = container->latLonAltBox();
221 emit centerOn( box, true );
222 }
223}
224
225}
226
227#include "moc_FileViewWidget.cpp"
This file contains the headers for MarbleModel.
This file contains the headers for MarbleWidget.
The EditPlacemarkDialog class deals with customizing placemarks.
This class is responsible for loading the different files into Geodata model.
Definition FileManager.h:33
A base class that can hold GeoDataFeatures.
static bool write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
Write the content of the given GeoDataDocument to the given I/O device.
A container for Features, Styles and in the future Schemas.
A class that defines a 3D bounding box for geographic data.
A class that defines a 2D bounding box for geographic data.
A base class for all geodata objects.
a class representing a point of interest on the map
The representation of GeoData in a model This class represents all available data given by kml-data f...
GeoDataTreeModel * treeModel()
Return the list of Placemarks as a QAbstractItemModel *.
@ ObjectPointerRole
The pointer to a specific object.
A widget class that displays a view of the earth.
MarbleModel * model()
Return the model that this view shows.
Binds a QML item to a specific geodetic location in screen coordinates.
virtual QVariant data(const QModelIndex &index, int role) const const=0
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
void popup(const QPoint &p, QAction *atAction)
bool isValid() const const
const QAbstractItemModel * model() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
QString tr(const char *sourceText, const char *disambiguation, int n)
void setDynamicSortFilter(bool enable)
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
CustomContextMenu
AscendingOrder
typedef WindowFlags
T value() const const
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.