Stellarium  0.16.1
StoredViewDialog_p.hpp
1 /*
2  * Stellarium Scenery3d Plug-in
3  *
4  * Copyright (C) 2011-2015 Simon Parzer, Peter Neubauer, Georg Zotti, Andrei Borza, Florian Schaukowitsch
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19  */
20 
21 #ifndef _STOREDVIEWDIALOG_P_HPP_
22 #define _STOREDVIEWDIALOG_P_HPP_
23 
24 #include "SceneInfo.hpp"
25 
26 #include <QDebug>
27 #include <QTextEdit>
28 #include <QAbstractListModel>
29 
31 class CustomTextEdit : public QTextEdit
32 {
33  Q_OBJECT
34 public:
35  CustomTextEdit(QWidget* parent = Q_NULLPTR) : QTextEdit(parent), textChanged(false), trackChange(false)
36  {
37  connect(this,&QTextEdit::textChanged,this,&CustomTextEdit::handleTextChange);
38  }
39 protected:
40  void focusInEvent(QFocusEvent* e)
41  {
42  trackChange = true;
43  textChanged = false;
44  QTextEdit::focusInEvent(e);
45  }
46 
47  void focusOutEvent(QFocusEvent *e)
48  {
49  QTextEdit::focusOutEvent(e);
50  trackChange = false;
51 
52  if(textChanged)
53  {
54  textChanged = false;
55  emit editingFinished();
56  }
57  }
58 signals:
60  void editingFinished();
61 private slots:
62  void handleTextChange()
63  {
64  if(trackChange)
65  textChanged = true;
66  }
67 
68 private:
69  bool textChanged, trackChange;
70 };
71 
72 class StoredViewModel : public QAbstractListModel
73 {
74  Q_OBJECT
75 public:
76  StoredViewModel(QObject* parent = Q_NULLPTR) : QAbstractListModel(parent)
77  { }
78 
79  int rowCount(const QModelIndex &parent) const
80  {
81  if(parent.isValid())
82  return 0;
83  return global.size() + user.size();
84  }
85 
86  QVariant data(const QModelIndex &index, int role) const
87  {
88  if(role == Qt::DisplayRole || role == Qt::EditRole)
89  {
90  return getViewAtIdx(index.row()).label;
91  }
92  if(role == Qt::DecorationRole)
93  {
94  if(getViewAtIdx(index.row()).isGlobal)
95  {
96  //TODO use a proper lock icon or something here
97  return QIcon(":/graphicGui/folder.png");
98  }
99  }
100  return QVariant();
101  }
102 
103  const StoredView& getViewAtIdx(int idx) const
104  {
105  if(idx >= global.size())
106  {
107  return user[idx-global.size()];
108  }
109  else
110  {
111  return global[idx];
112  }
113  }
114 
115  StoredView& getViewAtIdx(int idx)
116  {
117  if(idx >= global.size())
118  {
119  return user[idx-global.size()];
120  }
121  else
122  {
123  return global[idx];
124  }
125  }
126 
127 
128  QModelIndex addUserView(StoredView v)
129  {
130  int idx = global.size() + user.size();
131  beginInsertRows(QModelIndex(),idx,idx);
132  user.append(v);
133  endInsertRows();
134  persistUserViews();
135  return index(idx);
136  }
137 
138  void deleteView(int idx)
139  {
140  const StoredView& v = getViewAtIdx(idx);
141  if(!v.isGlobal)
142  {
143  int useridx = idx - global.size();
144 
145  beginRemoveRows(QModelIndex(),idx,idx);
146  user.removeAt(useridx);
147  endRemoveRows();
148 
149  persistUserViews();
150  }
151  else
152  {
153  qWarning()<<"[StoredViewDialog] Cannot delete global view";
154  }
155  }
156 
157  void persistUserViews()
158  {
159  qDebug()<<"[StoredViewDialog] Persisting user views...";
160  StoredView::saveUserViews(currentScene,user);
161  }
162 
163  void updatedAtIdx(int idx)
164  {
165  QModelIndex mIdx = index(idx);
166  persistUserViews();
167  emit dataChanged(mIdx,mIdx);
168  }
169 
170  SceneInfo getScene() { return currentScene; }
171 
172 public slots:
173  void setScene(const SceneInfo& scene)
174  {
175  qDebug()<<"[StoredViewDialog] Loading stored views for"<<scene.name;
176  this->currentScene = scene;
177  resetData(StoredView::getGlobalViewsForScene(currentScene),StoredView::getUserViewsForScene(currentScene));
178  qDebug()<<"[StoredViewDialog]"<<rowCount(QModelIndex())<<"entries loaded";
179  }
180 
181 private:
182  void resetData(const StoredViewList& global, const StoredViewList& user)
183  {
184  this->beginResetModel();
185  this->global = global;
186  this->user = user;
187  this->endResetModel();
188  }
189 
190  SceneInfo currentScene;
191  StoredViewList global, user;
192 };
193 
194 
195 #endif
Contains all the metadata necessary for a Scenery3d scene, and can be loaded from special ...
Definition: SceneInfo.hpp:37
QString name
Name of the scene.
Definition: SceneInfo.hpp:51
static StoredViewList getGlobalViewsForScene(const SceneInfo &scene)
Returns a list of all global views of a scene.
bool isGlobal
True if this is a position stored next to the scene definition (viewpoints.ini). If false...
Definition: SceneInfo.hpp:172
static StoredViewList getUserViewsForScene(const SceneInfo &scene)
Returns a list of all user-generated views of a scene.
A structure which stores a specific view position, view direction and FOV, together with a textual de...
Definition: SceneInfo.hpp:158
void editingFinished()
Emitted when focus lost and text was changed.
A custom QTextEdit subclass that has an editingFinished() signal like QLineEdit.
static void saveUserViews(const SceneInfo &scene, const StoredViewList &list)
Saves the given user views to userviews.ini, replacing all views existing for this scene The scene MU...