svgui  1.9
ColourNameDialog.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "ColourNameDialog.h"
00017 
00018 #include <QGridLayout>
00019 #include <QLabel>
00020 #include <QDialogButtonBox>
00021 #include <QLineEdit>
00022 #include <QIcon>
00023 #include <QCheckBox>
00024 #include <QPainter>
00025 #include <QPushButton>
00026 
00027 ColourNameDialog::ColourNameDialog(QString title, QString message,
00028                                    QColor colour, QString defaultName,
00029                                    QWidget *parent) :
00030     QDialog(parent),
00031     m_colour(colour)
00032 {
00033     setWindowTitle(title);
00034 
00035     QGridLayout *layout = new QGridLayout(this);
00036     
00037     QLabel *label = new QLabel(message, this);
00038     layout->addWidget(label, 0, 0, 1, 2);
00039     
00040     m_colourLabel = new QLabel(this);
00041     layout->addWidget(m_colourLabel, 1, 1);
00042 
00043     m_textField = new QLineEdit(defaultName, this);
00044     layout->addWidget(m_textField, 1, 0);
00045 
00046     connect(m_textField, SIGNAL(textChanged(const QString &)),
00047             this, SLOT(textChanged(const QString &)));
00048     
00049     m_darkBackground = new QCheckBox(this);
00050     layout->addWidget(m_darkBackground, 2, 0);
00051     m_darkBackground->setChecked
00052         (colour.red() + colour.green() + colour.blue() > 384);
00053     fillColourLabel();
00054 
00055     connect(m_darkBackground, SIGNAL(stateChanged(int)),
00056             this, SLOT(darkBackgroundChanged(int)));
00057 
00058     QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok |
00059                                                 QDialogButtonBox::Cancel);
00060     layout->addWidget(bb, 3, 0, 1, 2);
00061     connect(bb, SIGNAL(accepted()), this, SLOT(accept()));
00062     connect(bb, SIGNAL(rejected()), this, SLOT(reject()));
00063     m_okButton = bb->button(QDialogButtonBox::Ok);
00064     m_okButton->setEnabled(defaultName != "");
00065 }
00066 
00067 void
00068 ColourNameDialog::showDarkBackgroundCheckbox(QString text)
00069 {
00070     m_darkBackground->setText(text);
00071     m_darkBackground->show();
00072 }
00073 
00074 bool
00075 ColourNameDialog::isDarkBackgroundChecked() const
00076 {
00077     return m_darkBackground->isChecked();
00078 }
00079 
00080 void
00081 ColourNameDialog::darkBackgroundChanged(int)
00082 {
00083     fillColourLabel();
00084 }
00085 
00086 void
00087 ColourNameDialog::textChanged(const QString &text)
00088 {
00089     m_okButton->setEnabled(text != "");
00090 }
00091 
00092 void
00093 ColourNameDialog::fillColourLabel()
00094 {
00095     QPixmap pmap(20, 20);
00096     pmap.fill(m_darkBackground->isChecked() ? Qt::black : Qt::white);
00097     QPainter paint(&pmap);
00098     paint.setPen(m_colour);
00099     paint.setBrush(m_colour);
00100     paint.drawRect(2, 2, 15, 15);
00101     m_colourLabel->setPixmap(pmap);
00102 }
00103 
00104 QString
00105 ColourNameDialog::getColourName() const
00106 {
00107     return m_textField->text();
00108 }