color_button.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE 
00021 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00023 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00024 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 // DAMAGE.
00028 //
00029 // *****************************************************************************
00030 #include <mapviz/color_button.h>
00031 
00032 #include <QColorDialog>
00033 
00034 namespace mapviz
00035 {
00036 ColorButton::ColorButton(QWidget *parent)
00037   :
00038   QPushButton(parent)
00039 {
00040   setColor(Qt::black);
00041   QObject::connect(this, SIGNAL(clicked(bool)),
00042                    this, SLOT(handleClicked()));
00043 }
00044 
00045 void ColorButton::setColor(const QColor &color)
00046 {
00047   if (!color.isValid() || color == color_) {
00048     return;
00049   }
00050   
00051   color_ = color;
00052   // This was a very strange bug.  On initialization, the constructor
00053   // would set the stylesheet to black, then the external owner would
00054   // call setColor to change the color to something else.  We would
00055   // properly set the internal color_ and stylesheet, but it would
00056   // continue to display black.  If the user changed the color, it
00057   // would change properly.  Calling setStylesheet() twice fixes the
00058   // behavior.
00059   setStyleSheet("background: " + color_.name());
00060   setStyleSheet("background: " + color_.name());
00061   Q_EMIT colorChanged(color_);
00062 }
00063 
00064 void ColorButton::handleClicked()
00065 {
00066   // Note: We do not pass ourself as the parent or else the dialog
00067   // will inherit our color as the background!
00068   QColor new_color = QColorDialog::getColor(color_);
00069   if (!new_color.isValid() || new_color == color_) {
00070     return;
00071   }
00072   setColor(new_color);
00073   Q_EMIT colorEdited(new_color);
00074 }
00075 }  // namespace mapviz


mapviz
Author(s): Marc Alban
autogenerated on Thu Aug 24 2017 02:45:59