provide a user interface to edit motifs
[mussa.git] / qui / motif_editor / MotifDetail.cpp
1 #include <QColorDialog>
2 #include <QHBoxLayout>
3 #include <QRegExp>
4
5 #include "qui/motif_editor/MotifDetail.hpp"
6
7 #include <iostream>
8 using namespace std;
9
10 static const QRegExp iupacAlphabet("[ACTGUWRKYSMBHDVN]*", Qt::CaseInsensitive);
11 static const QRegExpValidator iupacValidator(iupacAlphabet, 0);
12
13 MotifDetail::MotifDetail(QWidget *parent)
14   : QWidget(parent),
15     motif_color(1.0, 1.0, 1.0)
16 {
17   setupWidget();
18 }
19
20 MotifDetail::MotifDetail(const MotifDetail &md)
21   : QWidget((QWidget *)md.parent()),
22     motif_color(md.motif_color),
23     motifText(md.motifText.displayText())
24 {
25 }
26
27 MotifDetail::MotifDetail(std::string& m, Color& c, QWidget *parent)
28   : QWidget(parent),
29     motif_color(c),
30     motifText(m.c_str())
31 {
32   setupWidget();
33 }
34
35 void MotifDetail::setupWidget()
36 {
37   QHBoxLayout *layout = new QHBoxLayout;
38
39   colorButton.setFlat(true);
40     
41   colorButton.setPalette(QPalette(qcolor()));
42   colorButton.setAutoFillBackground(true);
43   connect(&colorButton, SIGNAL(clicked()), this, SLOT(promptColor()));
44   layout->addWidget(&colorButton);
45   motifText.setValidator(&iupacValidator);
46   layout->addWidget(&motifText);
47   
48   setLayout(layout);
49 }
50
51 void MotifDetail::setMotif(const string &m)
52 {
53   motifText.setText(m.c_str());
54 }
55
56 string MotifDetail::motif() const
57 {
58   return motifText.text().toStdString();
59 }
60
61 void MotifDetail::setColor(const Color &c)
62 {
63   motif_color = c;
64   colorButton.setPalette(QPalette(qcolor()));
65 }
66
67 Color MotifDetail::color() const
68 {
69   return motif_color;
70 }
71
72 QColor MotifDetail::qcolor() const
73 {
74   QColor qc;
75   qc.setRedF(motif_color.r());
76   qc.setGreenF(motif_color.g());
77   qc.setBlueF(motif_color.b());
78   return qc;
79 }
80
81 void MotifDetail::promptColor()
82 {
83   QColor new_qcolor = QColorDialog::getColor(qcolor(), this);
84   Color new_color(new_qcolor.redF(), new_qcolor.greenF(), new_qcolor.blueF());
85   setColor(new_color);
86 }