1 /*
2 *  qm_colorpicker.cpp
3 *  QUIMUP window to select custom colors
4 *  © 2008-2018 Johan Spee
5 *
6 *  This file is part of Quimup
7 *
8 *  QUIMUP is free software: you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation, either version 3 of the License, or
11 *  (at your option) any later version.
12 *
13 *  QUIMUP is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program. If not, see http://www.gnu.org/licenses/.
20 */
21 
22 
23 #include "qm_colorpicker.h"
24 
qm_clickprogressbar(QWidget * parent)25 qm_colorpicker::qm_colorpicker(QWidget *parent)
26 {
27     setParent(parent);
28 
29     QPixmap qpx;
30     qpx = QPixmap(":/mn_icon.png");
31     setWindowIcon(qpx);
32     setWindowTitle(tr("Custom colors") );
33 
34     sl_H_front = new QSlider(Qt::Horizontal, this);
35     sl_H_front->setRange(0, 255);
36     sl_H_front->setSingleStep(1);
37     sl_H_front->setPageStep(10);
38 
39     sl_S_front = new QSlider(Qt::Horizontal, this);
40     sl_S_front->setRange(0, 255);
41     sl_S_front->setSingleStep(1);
42     sl_S_front->setPageStep(10);
43 
44     sl_V_front = new QSlider(Qt::Horizontal, this);
45     sl_V_front->setRange(0, 255);
46     sl_V_front->setSingleStep(1);
47     sl_V_front->setPageStep(10);
48 
49     sl_H_back = new QSlider(Qt::Horizontal, this);
50     sl_H_back->setRange(0, 255);
51     sl_H_back->setSingleStep(1);
52     sl_H_back->setPageStep(10);
53 
54     sl_S_back = new QSlider(Qt::Horizontal, this);
55     sl_S_back->setRange(0, 255);
56     sl_S_back->setSingleStep(1);
57     sl_S_back->setPageStep(10);
58 
59     sl_V_back = new QSlider(Qt::Horizontal, this);
60     sl_V_back->setRange(0, 255);
61     sl_V_back->setSingleStep(1);
62     sl_V_back->setPageStep(10);
63 
64     lb_cust_color = new QLabel;
65     lb_cust_color->setFrameShape(QFrame::StyledPanel);
66     lb_cust_color->setFrameShadow(QFrame::Sunken);
67     lb_cust_color->setAlignment(Qt::AlignCenter);
68     lb_cust_color->setTextFormat(Qt::RichText);
69     lb_cust_color->setMinimumSize(QSize(240, 36));
70     lb_cust_color->setAutoFillBackground(true);
71     lb_cust_color->setText("<b>" + tr("Custom Colors") + "</b>");
72 
73     bt_ok = new QPushButton(tr("OK"), this);
74     bt_cancel = new QPushButton(tr("Cancel"), this);
75 
76 
77     QWidget *main_widget = new QWidget();
78     QVBoxLayout *mainLayout = new QVBoxLayout(main_widget);
79     setCentralWidget(main_widget);
80 
81     QGridLayout *HSV_front_GridBox = new QGridLayout();
82     HSV_front_GridBox->addWidget(new QLabel(tr("Text"), this), 0, 1);
83     HSV_front_GridBox->addWidget(new QLabel("H", this), 1, 0);
84     HSV_front_GridBox->addWidget(new QLabel("S", this), 2, 0);
85     HSV_front_GridBox->addWidget(new QLabel("V", this), 3, 0);
86 
87     HSV_front_GridBox->addWidget(sl_H_front, 1, 1);
88     HSV_front_GridBox->addWidget(sl_S_front, 2, 1);
89     HSV_front_GridBox->addWidget(sl_V_front, 3 ,1);
90 
91     mainLayout->addLayout(HSV_front_GridBox);
92 
93     QGridLayout *HSV_back_GridBox = new QGridLayout();
94     HSV_back_GridBox->addWidget(new QLabel(tr("Backround"), this), 0, 1);
95     HSV_back_GridBox->addWidget(new QLabel("H", this), 1, 0);
96     HSV_back_GridBox->addWidget(new QLabel("S", this), 2, 0);
97     HSV_back_GridBox->addWidget(new QLabel("V", this), 3, 0);
98 
99     HSV_back_GridBox->addWidget(sl_H_back, 1, 1);
100     HSV_back_GridBox->addWidget(sl_S_back, 2, 1);
101     HSV_back_GridBox->addWidget(sl_V_back, 3 ,1);
102 
103     mainLayout->addLayout(HSV_back_GridBox);
104 
105     mainLayout->addWidget(lb_cust_color);
106 
107     QHBoxLayout *ok_cancel_Layout = new QHBoxLayout();
108     ok_cancel_Layout->setAlignment(Qt::AlignRight);
109     ok_cancel_Layout->setMargin(10);
110     ok_cancel_Layout->setSpacing(10);
111     ok_cancel_Layout->addWidget(bt_ok);
112     ok_cancel_Layout->addWidget(bt_cancel);
113 
114     mainLayout->addLayout(ok_cancel_Layout);
115 
116     connect( sl_H_front, SIGNAL(valueChanged(int)), SLOT(set_custom_fg()) );
117     connect( sl_S_front, SIGNAL(valueChanged(int)), SLOT(set_custom_fg()) );
118     connect( sl_V_front, SIGNAL(valueChanged(int)), SLOT(set_custom_fg()) );
119 
120     connect( sl_H_back, SIGNAL(valueChanged(int)), SLOT(set_custom_bg()) );
121     connect( sl_S_back, SIGNAL(valueChanged(int)), SLOT(set_custom_bg()) );
122     connect( sl_V_back, SIGNAL(valueChanged(int)), SLOT(set_custom_bg()) );
123 
124     connect( bt_cancel, SIGNAL(clicked()), this, SLOT(on_cancel()) );
125     connect( bt_ok, SIGNAL(clicked()), this, SLOT(on_ok()) );
126 }
127 
128 
129 void qm_colorpicker::on_cancel()
130 {
131     this->hide();
132 }
133 
134 
135 void qm_colorpicker::on_ok()
136 {
137     emit sgnl_newcolors(fgcolor, bgcolor);
138     this->hide();
139 }
140 
141 
142 void qm_colorpicker::set_colors(QColor fg, QColor bg)
143 {
144     sl_H_front->setValue(fg.hue());
145     sl_S_front->setValue(fg.saturation());
146     sl_V_front->setValue(fg.value());
147     sl_H_back->setValue(bg.hue());
148     sl_S_back->setValue(bg.saturation());
149     sl_V_back->setValue(bg.value());
150 }
151 
152 
153 void qm_colorpicker::set_custom_fg()
154 {
155     // h,s,v > 0 to work around a setHsv bug
156     QPalette pal (lb_cust_color->palette());
157     int h = sl_H_front->value();
158     if (h == 0) h = 1;
159     int s = sl_S_front->value();
160     if (s == 0) s = 1;
161     int v = sl_V_front->value();
162     if (v == 0) v = 1;
163     fgcolor.setHsv(h, s, v);
164     pal.setColor(QPalette::Foreground, fgcolor);
165     lb_cust_color->setPalette(pal);
166 }
167 
168 
169 void qm_colorpicker::set_custom_bg()
170 {
171     // h,s,v > 0 to work around a setHsv bug
172     QPalette pal (lb_cust_color->palette());
173     int h = sl_H_back->value();
174     if (h == 0) h = 1;
175     int s = sl_S_back->value();
176     if (s == 0) s = 1;
177     int v = sl_V_back->value();
178     if (v == 0) v = 1;
179     bgcolor.setHsv(h, s, v);
180     pal.setColor(QPalette::Window, bgcolor);
181     lb_cust_color->setPalette(pal);
182 }
183 
184 
185 qm_colorpicker::~qm_colorpicker()
186 {}
187