GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
color-picker.cc
Go to the documentation of this file.
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-2022 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
35namespace octave
36{
37 // Constructor with initial color as parameter
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
45 connect (this, &color_picker::clicked, this, &color_picker::select_color);
46 }
47
48 // Slot for button clicked: select a new color using QColorDialog
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;
57 }
58 }
59
60 // Set the color of the button
61 void color_picker::set_color (QColor new_color)
62 {
63 m_color = new_color;
65 }
66
67 // Draw the button with the actual color (using a stylesheet)
69 {
70 // Is this the right place to look for a "foreground" color that would
71 // provide a reasonable border for the color swatches?
72 QWidget *p = parentWidget ();
73
74 QString bordercolor
75 = (p ? p->palette ().text ().color ().name () : QString ("#000000"));
76
77 setStyleSheet (QString ("background-color: %1; border: 1px solid %2;")
78 .arg (m_color.name ())
79 .arg (bordercolor));
80
81 repaint ();
82 }
83}
virtual void update_button(void)
Definition: color-picker.cc:68
void set_color(QColor new_color)
Definition: color-picker.cc:61
void select_color(void)
Definition: color-picker.cc:49
color_picker(QColor color=QColor(0, 0, 0), QWidget *parent=nullptr)
Definition: color-picker.cc:38