File: Source.cpp

package info (click to toggle)
quickplot 0.8.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 1,019
  • sloc: cpp: 10,051; sh: 7,597; makefile: 176
file content (224 lines) | stat: -rw-r--r-- 5,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
#include "config.h"

#include <iostream>

#ifdef QP_ARCH_DARWIN
# include <limits.h>
# include <float.h>
#else
# include <values.h>
#endif

#include <list>
#include <iomanip>

#include <gtkmm.h>

using namespace Gtk;
#include "errorStr.h"
#include "value_t.h"
#include "Field.h"
#include "Plot.h"
#include "ColorGen.h"
#include "Graph.h"
#include "PlotSelector.h"
#include "ValueSlider.h"
#include "PlotLister.h"
#include "PlotConfig.h"
#include "GraphConfig.h"

#include "MainMenuBar.h"
#include "ButtonBar.h"
#include "StatusBar.h"
#include "MainWindow.h"
#include "App.h"

#include "Source.h"
#include "Globel.h"



std::list<Source *> sources;


const char *Source::TYPE_STRING[] =
{
  "NONE",
  "SNDFILE",
  "ASCII_FILE"
};


Source::Source(void):
  fileName(NULL),
  baseFileName(NULL)
{
  // The inheriting class object will validate this or not.
  isValid = false;

  setType(NONE);

  // Add this to the list of all Source objects.
  sources.push_back(this);
};

void Source::addCloseMenus(const char *label)
{
  std::list<MainWindow *>::const_iterator win = app->begin();
  for(;win != app->end(); win++)
  {
    (new CloseSourceMenuItem(this, *win))->show();
  }
}

Source::~Source(void)
{
  closeSourceMenuItems.clear();
  if(isValid)
  {

    // delete all plots that use fields from this Source.

    if(app->size() > 0)
    {
      std::list<MainWindow *>::const_iterator win = app->begin();
      for(;win != app->end(); win++)
      {
        int i;
        int n = (*win)->graphsNotebook.get_n_pages();
        for(i=0;i<n;i++)
        {
          Graph *graph = dynamic_cast<Graph *>
            ((*win)->graphsNotebook.get_nth_page(i));
          if(graph)
          {
            
            std::list<Field *>::const_iterator field = begin();
            while(field != end())
            {
              
              bool got_plot = false;
              
              //opSpew << " file=" << __FILE__ << " line=" << __LINE__ << std::endl;
              std::list<Plot *>::const_iterator plot;
              for(plot=graph->begin(); plot != graph->end(); plot++)
              {
                
                if((*plot)->x() == (*field) || (*plot)->y() == (*field))
                {
                  delete (*plot);
                  got_plot = true;
                  // Each time we delete a Plot the Graph Plot list
                  // breaks and must be reinitialized so we can look
                  // for more plots with this Field.  It's because
                  // Plot::~Plot() uses the Graph list to remove
                  // itself.
                  break;
                }
              }
              // If we deleted no more Plots we go to the next Graph.
              if(!got_plot) field++;
              
            } // for(;field != end();)
            
          }
        }        
      }
    }
    
    // Remove and delete the fields in reverse order so that any
    // dependent fields can clean up.
    while(size() > 0) // We don't use an iterator here because the
                        // Field destructor removes itself from the
                        // list, and so invalidates the iterator if it
                        // was used.
    {
      delete (back()); // this will remove it fom the list.
    }
    
  }
 
  if(fileName)
    free(fileName);

  if(baseFileName)
    free(baseFileName);

  sources.remove(this);

  // If this was a valid loaded source let what-ever is lessoning
  // know that we are about to delete all the fields that are listed
  // in this.
  m_signal_removedSource.emit(this);



};


extern "C"
{  
  static gboolean deleteSLater(gpointer data)
  {
    gtk_idle_remove_by_data(data);
    Source *source = ((struct SourceDeleteLater *) data)->source;
    
    std::list<CloseSourceMenuItem *>::const_iterator it =
      source->closeSourceMenuItems.begin();
    for(;it != source->closeSourceMenuItems.end(); it++)
    {
      // remove the close file menu
      (*it)->mainWindow->menuBar.getFileMenu().items().remove(*(*it));
      // delete the close file menu
      delete (*it);
    }
    
    // delete the source.
    delete source;
    return ((gint) 0);
  }
}



CloseSourceMenuItem::CloseSourceMenuItem(Source *s, MainWindow *mainWindow_in):
  closeImage(Stock::CLOSE, ICON_SIZE_MENU),
  mainWindow(mainWindow_in)
{
  Glib::ustring str;
  str += "Close ";
  str += s->getFileName();
  add_label(str);
  
  set_image(closeImage);
  mainWindow->menuBar.getFileMenu().items().push_back(*this);
  signal_activate().
    connect(sigc::mem_fun(*this, &CloseSourceMenuItem::deleteSourceLater));
  closeImage.show();
  s->closeSourceMenuItems.push_back(this);
  dl.source = s;
}

void CloseSourceMenuItem::deleteSourceLater(void)
{
  gtk_idle_add(deleteSLater, &dl);
}


SigC::Signal1<void, Source *> Source::signal_addedSource()
{
  return m_signal_addedSource;
}

SigC::Signal1<void, Source *> Source::m_signal_addedSource;


SigC::Signal1<void, Source *> Source::signal_removedSource()
{
  return m_signal_removedSource;
}

SigC::Signal1<void, Source *> Source::m_signal_removedSource;