1 //////////////////////////////////////////////////////////////////////// 2 // 3 // This class provides a simple color picker based on tQColorButton 4 // by Harald Jedele, 23.03.01, GPL version 2 or any later version. 5 // 6 // Copyright (C) 2013-2021 The Octave Project Developers 7 // 8 // See the file COPYRIGHT.md in the top-level directory of this 9 // distribution or <https://octave.org/copyright/>. 10 // 11 // This file is part of Octave. 12 // 13 // Octave is free software: you can redistribute it and/or modify it 14 // under the terms of the GNU General Public License as published by 15 // the Free Software Foundation, either version 3 of the License, or 16 // (at your option) any later version. 17 // 18 // Octave is distributed in the hope that it will be useful, but 19 // WITHOUT ANY WARRANTY; without even the implied warranty of 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 // GNU General Public License for more details. 22 // 23 // You should have received a copy of the GNU General Public License 24 // along with Octave; see the file COPYING. If not, see 25 // <https://www.gnu.org/licenses/>. 26 // 27 //////////////////////////////////////////////////////////////////////// 28 29 #if defined (HAVE_CONFIG_H) 30 # include "config.h" 31 #endif 32 33 #include "color-picker.h" 34 35 namespace octave 36 { 37 // Constructor with initial color as parameter color_picker(QColor old_color,QWidget * p)38 color_picker::color_picker (QColor old_color, QWidget *p) 39 : QPushButton (p) 40 { 41 m_color = old_color; 42 setFlat (true); 43 setFocusPolicy (Qt::NoFocus); // no focus, would change the color 44 update_button (); 45 connect (this, SIGNAL (clicked (void)), SLOT (select_color (void))); 46 } 47 48 // Slot for button clicked: select a new color using QColorDialog select_color(void)49 void color_picker::select_color (void) 50 { 51 QColor new_color = QColorDialog::getColor (m_color); 52 53 if (new_color.isValid () && new_color != m_color) 54 { 55 m_color = new_color; 56 update_button (); 57 } 58 } 59 60 // Draw the button with the actual color (using a stylesheet) update_button(void)61 void color_picker::update_button (void) 62 { 63 // Is this the right place to look for a "foreground" color that would 64 // provide a reasonable border for the color swatches? 65 QWidget *p = parentWidget (); 66 67 QString bordercolor 68 = (p ? p->palette ().text ().color ().name () : QString ("#000000")); 69 70 setStyleSheet (QString ("background-color: %1; border: 1px solid %2;") 71 .arg (m_color.name ()) 72 .arg (bordercolor)); 73 74 repaint (); 75 } 76 } 77