test motif editor
[mussa.git] / qui / motif_editor / MotifEditor.cpp
1 #include "qui/motif_editor/MotifEditor.hpp"
2 #include "alg/sequence.hpp"
3
4 #include <set>
5 #include <string>
6
7 using namespace std;
8
9 MotifEditor::MotifEditor(MussaRef m, QWidget *parent)
10   : QWidget(parent),
11     analysis(m),
12     editor_layout(0),
13     button_layout(0),
14     table(0),
15     delegate(0),
16     applyButton(0),
17     model(0)
18 {
19   setupWidgets();
20   
21   assert (m != 0);
22   const set<Sequence> &motif = analysis->motifs();
23   vector<Sequence> motif_seq(motif.begin(), motif.end());
24
25   applyButton->setFocusPolicy(Qt::StrongFocus);
26   connect(applyButton, SIGNAL(clicked()), this, SLOT(updateAnalysisMotifs()));
27   button_layout->addStretch();
28   button_layout->addWidget(applyButton);
29   
30   table->setItemDelegate(delegate);
31
32   editor_layout->addWidget(table);
33   editor_layout->addLayout(button_layout);
34   setLayout(editor_layout);
35   
36   updateTitle();
37   updateModel();
38 }
39
40 void MotifEditor::setupWidgets()
41 {
42   editor_layout = new QVBoxLayout(this);
43   button_layout = new QHBoxLayout(this);
44   table = new QTableView(this);
45   delegate = new MotifEditorDelegate(this);
46   applyButton = new QPushButton("apply");
47 }
48
49 void MotifEditor::updateModel()
50 {
51   // if our current analysis doesn't match the one in our
52   // model, we probalby changed the analysis.
53   // so delete and restart
54   // DET 2007mar30 (can this if statement ever be true? it doesn't look 
55   //                like there's a way to change the analysis?)
56   if (model and model->getAnalysis() != analysis) {
57     delete model;
58   }
59
60   // if we don't have a model, create a new one
61   if (not model) {
62     model = new MotifModel(analysis);
63   }
64   table->setModel(model);
65   updateView();
66 }
67
68 void MotifEditor::updateAnalysisMotifs()
69 {
70   // This function is _sooo_ not thread safe
71   // erase motifs
72   Color motif_default = analysis->colorMapper()->typeColor("motif");
73   analysis->colorMapper()->erase("motif");
74   analysis->colorMapper()->appendTypeColor("motif", motif_default);
75
76   // add our motifs back
77   vector<Sequence> new_motifs;
78   vector<Color> new_colors;
79
80   for(MotifModel::iterator md_i =  model->begin();
81       md_i != model->end();
82       ++md_i)
83   {
84     if (md_i->getSequence().size() > 0 && md_i->isEnabled()) {
85       new_motifs.push_back(md_i->getSequence());
86       new_colors.push_back(md_i->getColor());
87     }
88   }
89   analysis->set_motifs(new_motifs, new_colors);
90
91   emit changedMotifs();
92 }
93
94 void MotifEditor::updateTitle() 
95 {
96   std::string title("Motif Editor: ");
97   if (analysis) {
98     title += analysis->get_title();
99   }
100   setWindowTitle(title.c_str());
101 }
102
103 void MotifEditor::updateView()
104 {
105    for (int row = 0; row < model->rowCount(QModelIndex()); ++row)
106        table->resizeRowToContents(row);
107    for (int column = 0; column < model->columnCount(QModelIndex()); ++column)
108        table->resizeColumnToContents(column);
109 }
110
111