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