scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

AspectPrivate.cpp
Go to the documentation of this file.
1 
11 
29 #include "AbstractAspect.h"
30 #include "AspectPrivate.h"
31 #include <QRegExp>
32 #include <QStringList>
33 #include <stdexcept>
34 using namespace std;
35 
37 #ifdef Q_OS_MAC
38  new QSettings(QSettings::IniFormat, QSettings::UserScope, "SciDAVis", "SciDAVis");
39 #else
40  new QSettings(QSettings::NativeFormat, QSettings::UserScope, "SciDAVis", "SciDAVis");
41 #endif
42 QHash<QString, QVariant> AbstractAspect::Private::g_defaults;
43 
45  : d_name(name.isEmpty() ? "1" : name), d_caption_spec("%n%C{ - }%c"), d_owner(owner), d_parent(0)
46 {
47  d_creation_time = QDateTime::currentDateTime();
48 }
49 
51 {
52  foreach(AbstractAspect * child, d_children)
53  delete child;
54 }
55 
57 {
58  insertChild(d_children.count(), child);
59 }
60 
62 {
63  emit d_owner->aspectAboutToBeAdded(d_owner, index);
64  d_children.insert(index, child);
65  // Always remove from any previous parent before adding to a new one!
66  // Can't handle this case here since two undo commands have to be created.
67  Q_ASSERT(child->d_aspect_private->d_parent == 0);
68  child->d_aspect_private->d_parent = d_owner;
69  connect(child, SIGNAL(aspectDescriptionAboutToChange(const AbstractAspect *)),
70  d_owner, SIGNAL(aspectDescriptionAboutToChange(const AbstractAspect *)));
71  connect(child, SIGNAL(aspectDescriptionChanged(const AbstractAspect *)),
72  d_owner, SIGNAL(aspectDescriptionChanged(const AbstractAspect *)));
73  connect(child, SIGNAL(aspectAboutToBeAdded(const AbstractAspect *, int)),
74  d_owner, SIGNAL(aspectAboutToBeAdded(const AbstractAspect *, int)));
75  connect(child, SIGNAL(aspectAboutToBeRemoved(const AbstractAspect *, int)),
76  d_owner, SIGNAL(aspectAboutToBeRemoved(const AbstractAspect *, int)));
77  connect(child, SIGNAL(aspectAdded(const AbstractAspect *, int)),
78  d_owner, SIGNAL(aspectAdded(const AbstractAspect *, int)));
79  connect(child, SIGNAL(aspectRemoved(const AbstractAspect *, int)),
80  d_owner, SIGNAL(aspectRemoved(const AbstractAspect *, int)));
81  connect(child, SIGNAL(aspectAboutToBeRemoved(const AbstractAspect *)),
82  d_owner, SIGNAL(aspectAboutToBeRemoved(const AbstractAspect *)));
83  connect(child, SIGNAL(aspectAdded(const AbstractAspect *)),
84  d_owner, SIGNAL(aspectAdded(const AbstractAspect *)));
85  connect(child, SIGNAL(statusInfo(const QString&)),
86  d_owner, SIGNAL(statusInfo(const QString&)));
87  emit d_owner->aspectAdded(d_owner, index);
88  emit child->aspectAdded(child);
89 }
90 
92 {
93  for(int i=0; i<d_children.size(); i++)
94  if(d_children.at(i) == child) return i;
95  return -1;
96 }
97 
99 {
100  int index = indexOfChild(child);
101  Q_ASSERT(index != -1);
102  emit d_owner->aspectAboutToBeRemoved(d_owner, index);
104  d_children.removeAll(child);
105  QObject::disconnect(child, 0, d_owner, 0);
107  emit d_owner->aspectRemoved(d_owner, index);
108  return index;
109 }
110 
112 {
113  return d_children.count();
114 }
115 
117 {
118  Q_ASSERT(index >= 0 && index <= childCount());
119  return d_children.at(index);
120 }
121 
123 {
124  return d_name;
125 }
126 
127 void AbstractAspect::Private::setName(const QString &value)
128 {
129  emit d_owner->aspectDescriptionAboutToChange(d_owner);
130  d_name = value;
131  emit d_owner->aspectDescriptionChanged(d_owner);
132 }
133 
135 {
136  return d_comment;
137 }
138 
139 void AbstractAspect::Private::setComment(const QString &value)
140 {
141  emit d_owner->aspectDescriptionAboutToChange(d_owner);
142  d_comment = value;
143  emit d_owner->aspectDescriptionChanged(d_owner);
144 }
145 
147 {
148  return d_caption_spec;
149 }
150 
151 void AbstractAspect::Private::setCaptionSpec(const QString &value)
152 {
153  emit d_owner->aspectDescriptionAboutToChange(d_owner);
154  d_caption_spec = value;
155  emit d_owner->aspectDescriptionChanged(d_owner);
156 }
157 
158 void AbstractAspect::Private::setCreationTime(const QDateTime &time)
159 {
160  d_creation_time = time;
161 }
162 
164 {
165  int result = str.indexOf('}', start);
166  if (result < 0)
167  result = start;
168  return result;
169 }
170 
172 {
173  QString result = d_caption_spec;
174  QRegExp magic("%(.)");
175  for(int pos=magic.indexIn(result, 0); pos >= 0; pos=magic.indexIn(result, pos)) {
176  QString replacement;
177  int length;
178  switch(magic.cap(1).at(0).toLatin1()) {
179  case '%': replacement = "%"; length=2; break;
180  case 'n': replacement = d_name; length=2; break;
181  case 'c': replacement = d_comment; length=2; break;
182  case 't': replacement = d_creation_time.toString(); length=2; break;
183  case 'C':
184  length = indexOfMatchingBrace(result, pos) - pos + 1;
185  replacement = d_comment.isEmpty() ? "" : result.mid(pos+3, length-4);
186  break;
187  default:
188  throw runtime_error("Invalid magic cap");
189  }
190  result.replace(pos, length, replacement);
191  pos += replacement.size();
192  }
193  return result;
194 }
195 
197 {
198  return d_creation_time;
199 }
200 
201 QString AbstractAspect::Private::uniqueNameFor(const QString &current_name) const
202 {
203  QStringList child_names;
204  foreach(AbstractAspect * child, d_children)
205  child_names << child->name();
206 
207  if (!child_names.contains(current_name))
208  return current_name;
209 
210  QString base = current_name;
211  int last_non_digit;
212  for (last_non_digit = base.size()-1; last_non_digit>=0 &&
213  base[last_non_digit].category() == QChar::Number_DecimalDigit; --last_non_digit)
214  base.chop(1);
215  if (last_non_digit >=0 && base[last_non_digit].category() != QChar::Separator_Space)
216  base.append(" ");
217 
218  int new_nr = current_name.right(current_name.size() - base.size()).toInt();
219  QString new_name;
220  do
221  new_name = base + QString::number(++new_nr);
222  while (child_names.contains(new_name));
223 
224  return new_name;
225 }
AbstractAspect::Private::d_parent
AbstractAspect * d_parent
Definition: AspectPrivate.h:78
AbstractAspect::Private::comment
QString comment() const
Definition: AspectPrivate.cpp:134
AbstractAspect::Private::addChild
void addChild(AbstractAspect *child)
Definition: AspectPrivate.cpp:56
AbstractAspect::Private::captionSpec
QString captionSpec() const
Definition: AspectPrivate.cpp:146
AbstractAspect::Private::caption
QString caption() const
Definition: AspectPrivate.cpp:171
AbstractAspect::aspectAboutToBeAdded
void aspectAboutToBeAdded(const AbstractAspect *parent, int index)
Emit this when a parent aspect is about to get a new child inserted.
AbstractAspect::aspectRemoved
void aspectRemoved(const AbstractAspect *parent, int index)
Emit this from the parent after removing a child.
str
#define str(x)
Definition: PythonScripting.cpp:41
AbstractAspect::insertChild
void insertChild(AbstractAspect *child, int index)
Insert the given Aspect at a specific position in my list of children.
Definition: AbstractAspect.cpp:128
AbstractAspect::Private::uniqueNameFor
QString uniqueNameFor(const QString &current_name) const
Definition: AspectPrivate.cpp:201
AbstractAspect::Private::indexOfMatchingBrace
static int indexOfMatchingBrace(const QString &str, int start)
Definition: AspectPrivate.cpp:163
AbstractAspect::Private::childCount
int childCount() const
Definition: AspectPrivate.cpp:111
AbstractAspect::index
int index() const
Return my position in my parent's list of children.
Definition: AbstractAspect.h:137
AbstractAspect::d_aspect_private
Private * d_aspect_private
Definition: AbstractAspect.h:310
AbstractAspect::Private::indexOfChild
int indexOfChild(const AbstractAspect *child) const
Definition: AspectPrivate.cpp:91
AbstractAspect::Private::~Private
~Private()
Definition: AspectPrivate.cpp:50
AbstractAspect::indexOfChild
int indexOfChild(const AbstractAspect *child) const
Return the position of child in my list of children.
Definition: AbstractAspect.cpp:191
AbstractAspect::Private::setComment
void setComment(const QString &value)
Definition: AspectPrivate.cpp:139
AbstractAspect::Private::removeChild
int removeChild(AbstractAspect *child)
Definition: AspectPrivate.cpp:98
AbstractAspect::Private::g_defaults
static QHash< QString, QVariant > g_defaults
Definition: AspectPrivate.h:70
AbstractAspect::statusInfo
void statusInfo(const QString &text)
Emit this to give status information to the user.
AbstractAspect::childCount
int childCount() const
Return the number of child Aspects.
Definition: AbstractAspect.cpp:186
AspectPrivate.h
AbstractAspect::name
QString name() const
Definition: AbstractAspect.cpp:229
AbstractAspect::child
AbstractAspect * child(int index) const
Get a child by its position in my list of children.
Definition: AbstractAspect.cpp:180
AbstractAspect::Private::name
QString name() const
Definition: AspectPrivate.cpp:122
AbstractAspect::aspectDescriptionAboutToChange
void aspectDescriptionAboutToChange(const AbstractAspect *aspect)
Emit this before the name, comment or caption spec is changed.
AbstractAspect::aspectDescriptionChanged
void aspectDescriptionChanged(const AbstractAspect *aspect)
Emit this when the name, comment or caption spec changed.
AbstractAspect::Private::creationTime
QDateTime creationTime() const
Definition: AspectPrivate.cpp:196
name
char * name()
Definition: exp_saturation.c:45
AbstractAspect::Private::setCaptionSpec
void setCaptionSpec(const QString &value)
Definition: AspectPrivate.cpp:151
AbstractAspect::Private::insertChild
void insertChild(int index, AbstractAspect *child)
Definition: AspectPrivate.cpp:61
AbstractAspect::Private::child
AbstractAspect * child(int index)
Definition: AspectPrivate.cpp:116
AbstractAspect::Private::setName
void setName(const QString &value)
Definition: AspectPrivate.cpp:127
AbstractAspect::Private::setCreationTime
void setCreationTime(const QDateTime &time)
Definition: AspectPrivate.cpp:158
AbstractAspect::aspectAboutToBeRemoved
void aspectAboutToBeRemoved(const AbstractAspect *aspect)
Emit this from an aspect about to be removed from its parent's children.
AbstractAspect.h
AbstractAspect::Private::d_creation_time
QDateTime d_creation_time
Definition: AspectPrivate.h:76
AbstractAspect::Private::g_settings
static QSettings * g_settings
Definition: AspectPrivate.h:69
AbstractAspect::aspectAdded
void aspectAdded(const AbstractAspect *aspect)
Emit this from a newly added aspect.
AbstractAspect
Base class of all persistent objects in a Project.
Definition: AbstractAspect.h:86
AbstractAspect::Private::Private
Private(AbstractAspect *owner, const QString &name)
Definition: AspectPrivate.cpp:44