initialize the motif_editor with analysis motifs
[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(new QVBoxLayout(parent)),
13     button_layout(new QHBoxLayout(parent)),
14     table(new QTableView(this)),
15     delegate(new MotifEditorDelegate(this)),
16     applyButton(new QPushButton("apply")),
17     model(0)
18 {
19   assert (m != 0);
20   const set<Sequence> &motif = analysis->motifs();
21   vector<Sequence> motif_seq(motif.begin(), motif.end());
22
23   applyButton->setFocusPolicy(Qt::StrongFocus);
24   connect(applyButton, SIGNAL(clicked()), this, SLOT(updateAnalysisMotifs()));
25   button_layout->addStretch();
26   button_layout->addWidget(applyButton);
27   
28   table->setItemDelegate(delegate);
29
30   editor_layout->addWidget(table);
31   editor_layout->addLayout(button_layout);
32   setLayout(editor_layout);
33   
34   updateTitle();
35   updateModel();
36 }
37
38 void MotifEditor::updateModel()
39 {
40   MotifModel *new_model = new MotifModel(analysis);
41    
42   // if there was an old model, delete it
43   if (model) {
44     delete model;
45   }
46   // update the QTableView
47   model = new_model;  
48   table->setModel(model);
49   updateView();
50 }
51
52 void MotifEditor::updateAnalysisMotifs()
53 {
54   // This function is _sooo_ not thread safe
55   // erase motifs
56   Color motif_default = analysis->colorMapper()->typeColor("motif");
57   analysis->colorMapper()->erase("motif");
58   analysis->colorMapper()->appendTypeColor("motif", motif_default);
59
60   // add our motifs back
61   vector<Sequence> new_motifs;
62   vector<Color> new_colors;
63
64   for(MotifModel::iterator md_i =  model->begin();
65       md_i != model->end();
66       ++md_i)
67   {
68     if (md_i->getSequence().size() > 0 && md_i->isEnabled()) {
69       new_motifs.push_back(md_i->getSequence());
70       new_colors.push_back(md_i->getColor());
71     }
72   }
73   analysis->set_motifs(new_motifs, new_colors);
74
75   emit changedMotifs();
76 }
77
78 void MotifEditor::updateTitle() 
79 {
80   std::string title("Motif Editor: ");
81   if (analysis) {
82     title += analysis->get_title();
83   }
84   setWindowTitle(title.c_str());
85 }
86
87 void MotifEditor::updateView()
88 {
89    for (int row = 0; row < model->rowCount(QModelIndex()); ++row)
90        table->resizeRowToContents(row);
91    for (int column = 0; column < model->columnCount(QModelIndex()); ++column)
92        table->resizeColumnToContents(column);
93 }
94
95