delete default layout in MotifEditor
[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   updateTitle();
26   updateModel();
27 }
28
29 void MotifEditor::setupWidgets()
30 {
31   editor_layout = new QVBoxLayout;
32   button_layout = new QHBoxLayout;
33   // remove default layout (very safely) 
34   if (layout()) delete layout();
35   
36   table = new QTableView;
37   delegate = new MotifEditorDelegate(this);
38   applyButton = new QPushButton("apply");
39
40   applyButton->setFocusPolicy(Qt::StrongFocus);
41   connect(applyButton, SIGNAL(clicked()), this, SLOT(updateAnalysisMotifs()));
42   button_layout->addStretch();
43   button_layout->addWidget(applyButton);
44
45   table->setItemDelegate(delegate);
46   
47   editor_layout->addWidget(table);
48   editor_layout->addLayout(button_layout);
49   setLayout(editor_layout);
50 }
51
52 void MotifEditor::updateModel()
53 {
54   // if our current analysis doesn't match the one in our
55   // model, we probalby changed the analysis.
56   // so delete and restart
57   // DET 2007mar30 (can this if statement ever be true? it doesn't look 
58   //                like there's a way to change the analysis?)
59   if (model and model->getAnalysis() != analysis) {
60     delete model;
61   }
62
63   // if we don't have a model, create a new one
64   if (not model) {
65     model = new MotifModel(analysis);
66   }
67   table->setModel(model);
68   updateView();
69 }
70
71 void MotifEditor::updateAnalysisMotifs()
72 {
73   // This function is _sooo_ not thread safe
74   // erase motifs
75   Color motif_default = analysis->colorMapper()->typeColor("motif");
76   analysis->colorMapper()->erase("motif");
77   analysis->colorMapper()->appendTypeColor("motif", motif_default);
78
79   // add our motifs back
80   vector<Sequence> new_motifs;
81   vector<Color> new_colors;
82
83   for(MotifModel::iterator md_i =  model->begin();
84       md_i != model->end();
85       ++md_i)
86   {
87     if (md_i->getSequence().size() > 0 && md_i->isEnabled()) {
88       new_motifs.push_back(md_i->getSequence());
89       new_colors.push_back(md_i->getColor());
90     }
91   }
92   analysis->set_motifs(new_motifs, new_colors);
93
94   emit changedMotifs();
95 }
96
97 void MotifEditor::updateTitle() 
98 {
99   std::string title("Motif Editor: ");
100   if (analysis) {
101     title += analysis->get_title();
102   }
103   setWindowTitle(title.c_str());
104 }
105
106 void MotifEditor::updateView()
107 {
108    for (int row = 0; row < model->rowCount(QModelIndex()); ++row)
109        table->resizeRowToContents(row);
110    for (int column = 0; column < model->columnCount(QModelIndex()); ++column)
111        table->resizeColumnToContents(column);
112 }
113
114