ticket:104 fix some pointer problems with MussaAlignedWindow
authorDiane Trout <diane@caltech.edu>
Fri, 16 Jun 2006 00:33:49 +0000 (00:33 +0000)
committerDiane Trout <diane@caltech.edu>
Fri, 16 Jun 2006 00:33:49 +0000 (00:33 +0000)
the vector of MussaAlignedWindow * was having prolems with a double delete.
when a MussaAlignedWindow was open and one tried to load a different
analysis, the code that was closing all of the open windows crashed.

when I found boost::shared_ptr to fix it, I then found a problem
with the Qt trying to delete my shared zoom object which was also
causing a crash. This should have all of that fixed, and as a bonus
the new analysis doesn't have any of the old analysis' selections open.

alg/glseqbrowser.cpp
alg/glseqbrowser.hpp
alg/sequence.hpp
qui/MussaAlignedWindow.cpp
qui/MussaAlignedWindow.hpp
qui/MussaWindow.cpp
qui/MussaWindow.hpp
qui/ZoomWidget.cpp

index 30d96f892ad7d0fdce06fc888bb8506c078b34d5..a0fc6794c605121331956fec62adbb25d406462b 100644 (file)
@@ -297,11 +297,20 @@ AnnotationColors& GlSeqBrowser::colorMapper()
 
 void GlSeqBrowser::clear()
 {
+  clear_selection();
   clear_links();
   path_segments.clear();
   track_container.clear();
 }
 
+void GlSeqBrowser::clear_selection()
+{
+  selectedMode = false;
+  selectedRegion.clear();
+  selected_paths.clear();
+  selected_tracks.clear();
+}
+
 void GlSeqBrowser::push_sequence(const Sequence &s)
 {
   GlSequence gs(s, color_mapper);
index 109bc5055b0d1d5ed46f3c810de5ddf41cebadaa..8ad16c4bdbcd9d1dc32013518c46cf82b536831a 100644 (file)
@@ -63,6 +63,8 @@ public:
 
   //! clear our tracks and connections
   void clear();
+  //! clear everything related to a selection
+  void clear_selection();
 
   //! add a sequence to the back of our track container
   void push_sequence(const Sequence &s);
@@ -109,6 +111,7 @@ public:
     rect(T t, T l, T b, T r) : top(t), left(l), bottom(b), right(r) {}
     T width() { return right - left; }
     T height() { return top - bottom; }
+    void clear() { top=0; left=0; bottom=0; right=0; }
   };
 
   struct Segment
index 41c80cdce00b954a76be18ec8b96e76eacebc39d..86acee503b4726697de7e2acb6b19ccedde8dd52 100644 (file)
@@ -49,8 +49,6 @@ struct motif : public annot
 //! sequence track for mussa.
 class Sequence
 {
-  friend class ConnView;
-  friend class SeqView;
   private:
     std::string sequence;
     std::string header;
index a85975ad5208b8b22126c1c5ad2c5aad10ebec89..d54ef3d445a3fc49d745216e05fa512de311165a 100644 (file)
@@ -18,26 +18,30 @@ MussaAlignedWindow::MussaAlignedWindow(Mussa& m,
   : QMainWindow(parent),
     analysis(m),
     pick_align_menu(tr("Choose Alignment")),
-    view_align_menu(tr("View Alignment"))
+    view_align_menu(tr("View Alignment")),
+    zoom(0),
+    alignTB(0)
 {
   setupActions();
   browser.setSequences(analysis.sequences(), analysis.colorMapper());
   setSelectedPaths(m, sel_paths);
   setAlignment(0);
   double zoom_level = browser.zoomToSequence();
-  zoom.setValue(zoom_level);
+
+  zoom = new ZoomWidget();
+  connect(zoom, SIGNAL(valueChanged(double)), 
+          &browser, SLOT(setZoom(double)));
+  zoom->setValue(zoom_level);
   computeMatchLines();
   setupMenus();
   setupAlignmentMenus();
   
   setCentralWidget(&browser);
 
-  addToolBar(&alignTB);
-  alignTB.addWidget(&zoom);
+  alignTB = new QToolBar();
+  alignTB->addWidget(zoom);
+  addToolBar(alignTB);
   
-  connect(&zoom, SIGNAL(valueChanged(double)), 
-          &browser, SLOT(setZoom(double)));
   ostringstream message;
   message << "Selected " << selected_paths.size() << " paths";
   statusBar()->showMessage(message.str().c_str(), 5000);
index 7b140a60bfdae81f7f4b8dba4026da51ef1e902b..1e7139053776deeb493c6483a3037426c6d6f623 100644 (file)
@@ -53,7 +53,7 @@ protected:
 
   std::vector <IntAction *> pick_actions;
   std::vector <IntAction *> view_actions;
-  ZoomWidget zoom;
-  QToolBar alignTB;
+  ZoomWidget *zoom;
+  QToolBar *alignTB;
 };
 #endif
index d83e0b569f73862d64636922b514b579d22c4952..31d197bd1da6ee2b5ae7eb523cfe03fd0baa3cae 100644 (file)
 #include "qui/MussaWindow.hpp"
 #include "mussa_exceptions.hpp"
 
+#include <memory>
 #include <iostream>
 
 #include <boost/filesystem/path.hpp>
 namespace fs = boost::filesystem;
+#include <boost/bind.hpp>
 
 using namespace std;
 
@@ -55,7 +57,6 @@ MussaWindow::MussaWindow(Mussa *analysis_, QWidget *parent) :
   viewMussaAlignmentAction(0),
   manualAssistant(0)
 {
-
   setupActions();
   setupMainMenu();
   setupAssistant();
@@ -125,6 +126,7 @@ void MussaWindow::setAnalysis(Mussa *new_analysis)
 {
   if (new_analysis != 0) {
     // only switch mussas if we loaded without error
+    clear();
     delete analysis;
     analysis = new_analysis;
     setWindowTitle(analysis->get_name().c_str());
@@ -300,6 +302,12 @@ void MussaWindow::about()
    QMessageBox::about(this, tr("About mussa"), msg);
 }
 
+void MussaWindow::clear()
+{
+  aligned_windows.clear();
+  browser.clear();
+}
+
 void MussaWindow::createNewAnalysis()
 {
   try {
@@ -480,27 +488,21 @@ void MussaWindow::viewMussaAlignment()
                          QObject::tr("you should probably select some paths "
                                      "first"));
   } else {
-    MussaAlignedWindow *ma_win = new MussaAlignedWindow(*analysis, 
-                                                        selected_paths);
-    connect(this, SIGNAL(changedAnnotations()), 
-            ma_win, SLOT(update()));
+    boost::shared_ptr<MussaAlignedWindow> ma_win( 
+      new MussaAlignedWindow(*analysis, selected_paths)
+    );
+
     aligned_windows.push_back(ma_win);
-    ma_win->show();
+    connect(this, SIGNAL(changedAnnotations()), 
+            aligned_windows.back().get(), SLOT(update()));
+    aligned_windows.back()->show();
   }
 }
                         
 void MussaWindow::updateAnalysis()
 {
   threshold.setRange(analysis->get_threshold(),analysis->get_window());
-  for (list<MussaAlignedWindow *>::iterator maw_i = aligned_windows.begin();
-       maw_i != aligned_windows.end();
-       ++maw_i)
-  {
-    (*maw_i)->hide();
-    delete *maw_i;
-  }
 
-  browser.clear();
   const vector<Sequence>& seqs = analysis->sequences();
   browser.setSequences(seqs, analysis->colorMapper());
   updateLinks();
index 42c90a2ee59bd23cb3131881350c4d0aa6488af6..dfbe89cc7719fbcbd87ff14e2a884f948aa68fcd 100644 (file)
@@ -2,6 +2,7 @@
 #define _MUSSAWINDOW_H_
 
 #include <list>
+#include <boost/shared_ptr.hpp>
 
 #include <QMainWindow>
 #include <QPixmap>
@@ -31,6 +32,9 @@ public:
   MussaWindow(Mussa* analysis=0, QWidget *parent=0);
   ~MussaWindow();
 
+  //! reset any attached window
+  void clear();
+
   //! switch to a new analysis
   void setAnalysis(Mussa *new_analysis);
 public slots:
@@ -81,7 +85,7 @@ signals:
 
 protected:
   Mussa *analysis;
-  std::list<MussaAlignedWindow *> aligned_windows;
+  std::list<boost::shared_ptr<MussaAlignedWindow> > aligned_windows;
   MotifEditor *motif_editor;
   MussaSetupDialog setup_analysis_dialog;
 
index b32efeafda798b9a8e7cbdc5056c5492ddec9e35..24c71a1de06cb161bc15c8e9e712c64f255036e3 100644 (file)
@@ -14,10 +14,10 @@ ZoomWidget::ZoomWidget(QWidget *parent)
   zoom.setSingleStep(0.1);
   connect(&zoom, SIGNAL(valueChanged(double)), this, SLOT(setValue(double)));
 
-  QLabel *zoomLabel = new QLabel();
+  auto_ptr<QLabel> zoomLabel(new QLabel());
   zoomLabel->setPixmap(QIcon(":/icons/viewmag.png").pixmap(16, 16));
   QHBoxLayout *layout = new QHBoxLayout;
-  layout->addWidget(zoomLabel);
+  layout->addWidget(zoomLabel.get());
   layout->addWidget(&zoom);
   layout->addWidget(new QLabel("bp/pix"));
   setLayout(layout);