1#include <Gui/Widgets/ControlPanel.hpp>
3#include <Gui/Widgets/MatrixEditor.hpp>
5#include <PowerSlider/PowerSlider.hpp>
13#include <QPlainTextEdit>
18namespace Ra::Gui::Widgets {
20ControlPanel::ControlPanel(
const std::string& name,
bool hline, QWidget* parent ) :
22 setObjectName( name.
c_str() );
23 m_mainLayout =
new QVBoxLayout();
24 m_mainLayout->setObjectName(
"main layout" );
25 m_contentLayout =
new QGridLayout();
26 m_contentLayout->setObjectName(
"first content layout" );
28 if ( !name.
empty() ) {
29 auto panelName =
new QLabel();
30 panelName->setText( name.
c_str() );
31 m_mainLayout->addWidget( panelName );
36 line->setFrameShape( QFrame::HLine );
37 line->setFrameShadow( QFrame::Sunken );
38 m_mainLayout->addWidget( line );
41 m_mainLayout->addLayout( m_contentLayout );
43 setLayout( m_mainLayout );
51 auto label =
new QLabel( name.
c_str() );
52 auto button =
new QCheckBox();
53 button->setAutoExclusive(
false );
54 button->setChecked( set );
55 if ( !tooltip.
empty() ) {
57 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() );
58 label->setToolTip( tooltipString );
59 button->setToolTip( tooltipString );
61 connect( button, &QCheckBox::stateChanged,
std::move( callback ) );
63 auto index = m_contentLayout->rowCount();
64 m_contentLayout->addWidget( label, index, 0 );
65 m_contentLayout->addWidget( button, index, 1 );
69 auto label =
new QLabel( text.
c_str() );
70 auto index = m_contentLayout->rowCount();
71 m_contentLayout->addWidget( label, index, 0, 1, -1 );
77 auto button =
new QPushButton( name.
c_str(),
nullptr );
78 if ( !tooltip.
empty() ) {
80 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() ) );
83 connect( button, &QPushButton::clicked,
std::move( callback ) );
85 auto index = m_contentLayout->rowCount();
86 m_contentLayout->addWidget( button, index, 1, 1, -1 );
95 auto inputLabel =
new QLabel( tr( name.
c_str() ) );
96 auto sliderLayout =
new QHBoxLayout();
97 auto inputField =
new QSlider( Qt::Horizontal );
98 if ( !tooltip.
empty() ) {
100 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() );
101 inputLabel->setToolTip( tooltipString );
102 inputField->setToolTip( tooltipString );
104 auto spinbox =
new QSpinBox();
105 inputField->setRange( min, max );
106 inputField->setValue( initial );
107 inputField->setTickPosition( QSlider::TicksAbove );
108 inputField->setTickInterval( 1 );
109 inputField->setFocusPolicy( Qt::StrongFocus );
110 spinbox->setRange( min, max );
111 spinbox->setValue( initial );
112 connect( inputField, &QSlider::valueChanged, spinbox, &QSpinBox::setValue );
113 connect( spinbox, &QSpinBox::valueChanged, inputField, &QSlider::setValue );
114 connect( inputField, &QSlider::valueChanged,
std::move( callback ) );
115 sliderLayout->addWidget( inputField );
116 sliderLayout->addWidget( spinbox );
118 auto index = m_contentLayout->rowCount();
119 m_contentLayout->addWidget( inputLabel, index, 0 );
120 m_contentLayout->addLayout( sliderLayout, index, 1 );
129 auto inputLabel =
new QLabel( tr( name.
c_str() ) );
130 auto inputField =
new PowerSlider();
131 if ( !tooltip.
empty() ) {
133 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() );
134 inputLabel->setToolTip( tooltipString );
135 inputField->setToolTip( tooltipString );
137 inputField->setObjectName( name.
c_str() );
138 inputField->setRange( min, max );
139 inputField->setValue( initial );
140 inputField->setSingleStep( 0.01 );
141 connect( inputField, &PowerSlider::valueChanged,
std::move( callback ) );
143 auto index = m_contentLayout->rowCount();
144 m_contentLayout->addWidget( inputLabel, index, 0 );
145 m_contentLayout->addWidget( inputField, index, 1 );
150 const Ra::Core::MatrixN& initial,
154 auto inputLabel =
new QLabel( tr( name.
c_str() ) );
157 if ( !tooltip.
empty() ) {
159 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() );
160 inputLabel->setToolTip( tooltipString );
161 inputField->setToolTip( tooltipString );
163 connect( inputField, &MatrixEditor::valueChanged,
std::move( callback ) );
165 auto index = m_contentLayout->rowCount();
166 m_contentLayout->addWidget( inputLabel, index, 0 );
167 m_contentLayout->addWidget( inputField, index, 1 );
170void ControlPanel::addColorInput(
177 auto button =
new QPushButton( name.
c_str() );
179 auto clrBttn = QColor::fromRgbF( srgbColor[0], srgbColor[1], srgbColor[2], srgbColor[3] );
180 auto clrDlg = [callback, clrBttn, withAlpha, button, name]()
mutable {
181 clrBttn = QColorDialog::getColor(
185 ( withAlpha ? QColorDialog::ShowAlphaChannel : QColorDialog::ColorDialogOptions() )
187 | QColorDialog::DontUseNativeDialog
190 if ( clrBttn.isValid() ) {
192 auto lum = 0.2126_ra * Scalar( clrBttn.redF() ) +
193 0.7151_ra * Scalar( clrBttn.greenF() ) +
194 0.0721_ra * Scalar( clrBttn.blueF() );
197 Scalar( clrBttn.greenF() ),
198 Scalar( clrBttn.blueF() ),
199 Scalar( clrBttn.alphaF() ) ) );
201 QString qss = QString(
"background-color: %1" ).arg( clrBttn.name() );
202 if ( lum > 1_ra / 3_ra ) { qss += QString(
"; color: #000000" ); }
203 else { qss += QString(
"; color: #FFFFFF" ); }
204 button->setStyleSheet( qss );
208 if ( !tooltip.
empty() ) {
210 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() ) );
214 auto lum = 0.2126_ra * srgbColor[0] + 0.7151_ra * srgbColor[1] + 0.0721_ra * srgbColor[2];
215 QString qss = QString(
"background-color: %1" ).arg( clrBttn.name() );
216 if ( lum > 1_ra / 3_ra ) { qss += QString(
"; color: #000000" ); }
217 else { qss += QString(
"; color: #FFFFFF" ); }
218 button->setStyleSheet( qss );
220 connect( button, &QPushButton::clicked, clrDlg );
222 auto index = m_contentLayout->rowCount();
223 m_contentLayout->addWidget( button, index, 0, 1, -1 );
232 auto openFile = [
this, callback, rootDirectory, filters]() {
233 auto fltrs = QString::fromStdString( filters );
234 auto pathList = QFileDialog::getOpenFileNames(
235 this,
"Open files", QString::fromStdString( rootDirectory ), fltrs );
236 if ( !pathList.empty() ) {
238 for (
const auto& file : pathList ) {
239 fileList += file.toStdString() +
";";
242 callback( fileList );
244 else { callback(
"" ); }
247 addButton( name, openFile, tooltip );
256 auto saveFile = [
this, callback, rootDirectory, filters]() {
257 auto fltrs = QString::fromStdString( filters );
258 auto filename = QFileDialog::getSaveFileName(
259 this,
"Save file", QString::fromStdString( rootDirectory ), fltrs );
260 if ( !filename.isEmpty() ) { callback( filename.toStdString() ); }
262 addButton( name, saveFile, tooltip );
265void ControlPanel::addSeparator() {
266 QFrame* line =
new QFrame();
267 line->setFrameShape( QFrame::HLine );
268 line->setFrameShadow( QFrame::Sunken );
269 m_mainLayout->addWidget( line );
270 m_contentLayout =
new QGridLayout();
271 m_mainLayout->addLayout( m_contentLayout );
279 auto inputLabel =
new QLabel( tr( name.
c_str() ) );
280 auto inputField =
new QComboBox();
281 for (
auto v : items ) {
282 inputField->addItem( QString::fromStdString( v ) );
284 if ( !tooltip.
empty() ) {
285 inputLabel->setToolTip(
286 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() ) );
288 inputField->setCurrentIndex( initial );
290 inputField, QOverload<int>::of( &QComboBox::currentIndexChanged ),
std::move( callback ) );
292 auto index = m_contentLayout->rowCount();
293 m_contentLayout->addWidget( inputLabel, index, 0 );
294 m_contentLayout->addWidget( inputField, index, 1 );
297void ControlPanel::addComboBox(
const std::string& name,
302 auto inputLabel =
new QLabel( tr( name.
c_str() ) );
303 auto inputField =
new QComboBox();
304 for (
auto v : items ) {
305 inputField->addItem( QString::fromStdString( v ) );
307 if ( !tooltip.
empty() ) {
308 inputLabel->setToolTip(
309 QString(
"<qt>%1</qt>" ).arg( QString( tooltip.
c_str() ).toHtmlEscaped() ) );
311 inputField->setCurrentText( QString::fromStdString( initial ) );
313 QOverload<const QString&>::of( &QComboBox::currentTextChanged ),
316 auto index = m_contentLayout->rowCount();
317 m_contentLayout->addWidget( inputLabel, index, 0 );
318 m_contentLayout->addWidget( inputField, index, 1 );
321void ControlPanel::addStretch(
int stretch ) {
322 m_mainLayout->addStretch( stretch );
323 m_contentLayout =
new QGridLayout();
324 m_contentLayout->setObjectName(
"stretch layout" );
325 m_mainLayout->addLayout( m_contentLayout );
328void ControlPanel::addWidget( QWidget* newWidget ) {
329 m_mainLayout->addWidget( newWidget );
330 m_contentLayout =
new QGridLayout();
331 m_contentLayout->setObjectName(
"Widget content layout" );
332 m_mainLayout->addLayout( m_contentLayout );
static ColorBase sRGBToLinearRGB(const ColorBase &srgb)
static ColorBase linearRGBTosRGB(const ColorBase &lrgb)