Load saved analysis can open new windows
[mussa.git] / qui / SubanalysisWindow.cpp
1 #include "qui/SubanalysisWindow.hpp"
2 #include "qui/MussaWindow.hpp"
3
4 #include "mussa_exceptions.hpp"
5 #include "alg/mussa.hpp"
6
7 #include <QVBoxLayout>
8 #include <QHBoxLayout>
9 #include <QGridLayout>
10
11 SubanalysisWindow::SubanalysisWindow(QWidget *parent)
12   : QWidget(parent),
13     window(0),
14     threshold(0),
15     table(0),
16     ok(0),
17     cancel(0)
18 {
19   QGridLayout *parameterLayout = new QGridLayout;
20
21   QLabel *thresholdLabel = new QLabel(tr("threshold (bp)"));
22   parameterLayout->addWidget(thresholdLabel, 0, 0);
23   threshold = new QSpinBox(this);
24   threshold->setValue(8);
25   parameterLayout->addWidget(threshold, 1, 0);
26   QLabel *windowLabel = new QLabel(tr("window (bp)"));
27   parameterLayout->addWidget(windowLabel, 0, 1);
28   window = new QSpinBox(this);
29   window->setValue(10);
30   parameterLayout->addWidget(window, 1, 1);
31
32   ok = new QPushButton(tr("&OK"), this);
33   ok->setEnabled( false );
34   connect(ok, SIGNAL(clicked()), this, SLOT(run()));
35
36   cancel = new QPushButton(tr("Cancel"), this);
37   connect(cancel, SIGNAL(clicked()), this, SLOT(abort()));
38
39   table = new QTableView(this);
40   table->setModel(&model);
41
42   // layout buttons
43   QHBoxLayout *buttonLayout = new QHBoxLayout;
44   buttonLayout->addWidget(ok);
45   buttonLayout->addWidget(cancel);
46
47   // layout verticle space
48   QVBoxLayout *verticalLayout = new QVBoxLayout;
49   verticalLayout->addLayout(parameterLayout);
50   verticalLayout->addWidget(table);
51   verticalLayout->addLayout(buttonLayout);
52   setLayout(verticalLayout);
53
54   // now that we're all setup lets get notices when we're updated
55   connect(&model, SIGNAL(rowsInserted(const QModelIndex&, int, int)), 
56           this, SLOT(modelUpdated(const QModelIndex&, int, int)));
57   connect(&model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), 
58           this, SLOT(modelUpdated(const QModelIndex&, int, int)));
59 }
60
61 SequenceLocationModel& SubanalysisWindow::getModel()
62 {
63   return model;
64 }
65
66 void SubanalysisWindow::abort()
67 {
68   model.clear();
69   hide();
70 }
71
72 void SubanalysisWindow::run()
73 {
74   if (window == 0 or threshold == 0) {
75     throw std::runtime_error("SubanalysisWindow misconstructed");
76   }
77
78   if (model.size() == 0) {
79     throw std::runtime_error("It shouldn't be possible to call run with an "
80                              "empty model now.");
81   }
82
83   std::auto_ptr<Mussa> m(new Mussa);
84
85   for(SequenceLocationModel::iterator itor = model.begin();
86       itor != model.end();
87       ++itor)
88   {
89     // append_sequence from a const Sequence & will make a copy 
90     // for the shared pointer.
91     m->append_sequence(itor->getSelectedSequence());
92   }
93
94   m->set_window(window->value());
95   m->set_threshold(threshold->value());
96   m->analyze();
97   MussaWindow *mw = new MussaWindow(m.get());
98   mw->show();
99   model.clear();
100   hide();
101 }
102
103 void SubanalysisWindow::modelUpdated(const QModelIndex&, int, int )
104 {
105   // if the model is empty we shouldn't be able to click ok
106   if (ok) ok->setEnabled(not model.empty());
107 }