Add window titles
[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(updateMotifs()));
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();
41    
42   new_model->push_empty();
43
44   // if there was an old model, delete it
45   if (model) {
46     delete model;
47   }
48   // update the QTableView
49   model = new_model;  
50   table->setModel(model);
51   updateView();
52 }
53
54 void MotifEditor::updateMotifs()
55 {
56   // This function is _sooo_ not thread safe
57   // erase motifs
58   Color motif_default = analysis->colorMapper()->typeColor("motif");
59   analysis->colorMapper()->erase("motif");
60   analysis->colorMapper()->appendTypeColor("motif", motif_default);
61
62   // add our motifs back
63   vector<Sequence> new_motifs;
64   vector<Color> new_colors;
65
66   for(MotifModel::iterator md_i =  model->begin();
67       md_i != model->end();
68       ++md_i)
69   {
70     if (md_i->getSequence().size() > 0 && md_i->isEnabled()) {
71       new_motifs.push_back(md_i->getSequence());
72       new_colors.push_back(md_i->getColor());
73     }
74   }
75   analysis->set_motifs(new_motifs, new_colors);
76
77   emit changedMotifs();
78 }
79
80 void MotifEditor::updateTitle() 
81 {
82   std::string title("Motif Editor: ");
83   if (analysis) {
84     title += analysis->get_title();
85   }
86   setWindowTitle(title.c_str());
87 }
88
89 void MotifEditor::updateView()
90 {
91    for (int row = 0; row < model->rowCount(QModelIndex()); ++row)
92        table->resizeRowToContents(row);
93    for (int column = 0; column < model->columnCount(QModelIndex()); ++column)
94        table->resizeColumnToContents(column);
95 }
96
97