Skip to content

Commit

Permalink
Add a color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Jun 30, 2024
1 parent 91e2912 commit 542796f
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/tlPlayApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(HEADERS
AudioMenu.h
AudioPopup.h
AudioTool.h
ColorPickerTool.h
ColorTool.h
CompareActions.h
CompareMenu.h
Expand Down Expand Up @@ -57,6 +58,7 @@ set(SOURCE
AudioMenu.cpp
AudioPopup.cpp
AudioTool.cpp
ColorPickerTool.cpp
ColorTool.cpp
CompareActions.cpp
CompareMenu.cpp
Expand Down
115 changes: 115 additions & 0 deletions lib/tlPlayApp/ColorPickerTool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2021-2024 Darby Johnston
// All rights reserved.

#include <tlPlayApp/ColorPickerTool.h>

#include <tlPlayApp/MainWindow.h>
#include <tlPlayApp/Viewport.h>

#include <tlUI/ColorSwatch.h>
#include <tlUI/GridLayout.h>
#include <tlUI/Label.h>
#include <tlUI/RowLayout.h>

#include <tlCore/StringFormat.h>

namespace tl
{
namespace play_app
{
struct ColorPickerTool::Private
{
image::Color4f color;
std::shared_ptr<ui::ColorSwatch> swatch;
std::vector<std::shared_ptr<ui::Label> > labels;
std::shared_ptr<ui::VerticalLayout> layout;
std::shared_ptr<observer::ValueObserver<image::Color4f> > colorPickerObserver;
};

void ColorPickerTool::_init(
const std::shared_ptr<MainWindow>& mainWindow,
const std::shared_ptr<App>& app,
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
{
IToolWidget::_init(
Tool::ColorPicker,
"tl::play_app::ColorPickerTool",
app,
context,
parent);
TLRENDER_P();

p.swatch = ui::ColorSwatch::create(context);
p.swatch->setSizeRole(ui::SizeRole::SwatchLarge);

p.labels.push_back(ui::Label::create(context));
p.labels.push_back(ui::Label::create(context));
p.labels.push_back(ui::Label::create(context));
p.labels.push_back(ui::Label::create(context));

p.layout = ui::VerticalLayout::create(context);
p.layout->setMarginRole(ui::SizeRole::MarginSmall);
p.layout->setSpacingRole(ui::SizeRole::SpacingSmall);
p.swatch->setParent(p.layout);
auto gridLayout = ui::GridLayout::create(context, p.layout);
gridLayout->setSpacingRole(ui::SizeRole::SpacingSmall);
auto label = ui::Label::create("Red:", context, gridLayout);
gridLayout->setGridPos(label, 0, 0);
p.labels[0]->setParent(gridLayout);
gridLayout->setGridPos(p.labels[0], 0, 1);
label = ui::Label::create("Green:", context, gridLayout);
gridLayout->setGridPos(label, 1, 0);
p.labels[1]->setParent(gridLayout);
gridLayout->setGridPos(p.labels[1], 1, 1);
label = ui::Label::create("Blue:", context, gridLayout);
gridLayout->setGridPos(label, 2, 0);
p.labels[2]->setParent(gridLayout);
gridLayout->setGridPos(p.labels[2], 2, 1);
label = ui::Label::create("Alpha:", context, gridLayout);
gridLayout->setGridPos(label, 3, 0);
p.labels[3]->setParent(gridLayout);
gridLayout->setGridPos(p.labels[3], 3, 1);
_setWidget(p.layout);

_widgetUpdate();

p.colorPickerObserver = observer::ValueObserver<image::Color4f>::create(
mainWindow->getViewport()->observeColorPicker(),
[this](const image::Color4f& value)
{
_p->color = value;
_widgetUpdate();
});
}

ColorPickerTool::ColorPickerTool() :
_p(new Private)
{}

ColorPickerTool::~ColorPickerTool()
{}

std::shared_ptr<ColorPickerTool> ColorPickerTool::create(
const std::shared_ptr<MainWindow>& mainWindow,
const std::shared_ptr<App>& app,
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
{
auto out = std::shared_ptr<ColorPickerTool>(new ColorPickerTool);
out->_init(mainWindow, app, context, parent);
return out;
}

void ColorPickerTool::_widgetUpdate()
{
TLRENDER_P();
p.swatch->setColor(p.color);
p.labels[0]->setText(string::Format("{0}").arg(p.color.r));
p.labels[1]->setText(string::Format("{0}").arg(p.color.g));
p.labels[2]->setText(string::Format("{0}").arg(p.color.b));
p.labels[3]->setText(string::Format("{0}").arg(p.color.a));
}
}
}
47 changes: 47 additions & 0 deletions lib/tlPlayApp/ColorPickerTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2021-2024 Darby Johnston
// All rights reserved.

#pragma once

#include <tlPlayApp/IToolWidget.h>

#include <tlTimeline/Player.h>

namespace tl
{
namespace play_app
{
class App;
class MainWindow;

//! Color picker tool.
class ColorPickerTool : public IToolWidget
{
TLRENDER_NON_COPYABLE(ColorPickerTool);

protected:
void _init(
const std::shared_ptr<MainWindow>&,
const std::shared_ptr<App>&,
const std::shared_ptr<system::Context>&,
const std::shared_ptr<IWidget>& parent);

ColorPickerTool();

public:
virtual ~ColorPickerTool();

static std::shared_ptr<ColorPickerTool> create(
const std::shared_ptr<MainWindow>&,
const std::shared_ptr<App>&,
const std::shared_ptr<system::Context>&,
const std::shared_ptr<IWidget>& parent = nullptr);

private:
void _widgetUpdate();

TLRENDER_PRIVATE();
};
}
}
14 changes: 9 additions & 5 deletions lib/tlPlayApp/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ namespace tl
p.infoLabel->setHAlign(ui::HAlign::Right);
p.infoLabel->setMarginRole(ui::SizeRole::MarginInside);

p.toolsWidget = ToolsWidget::create(app, context);
p.toolsWidget = ToolsWidget::create(
std::dynamic_pointer_cast<MainWindow>(shared_from_this()),
app,
context);
p.toolsWidget->hide();

p.layout = ui::VerticalLayout::create(context, shared_from_this());
Expand Down Expand Up @@ -459,11 +462,12 @@ namespace tl
hLayout->setSpacingRole(ui::SizeRole::SpacingTool);
p.speedEdit->setParent(hLayout);
p.speedButton->setParent(hLayout);
auto spacer = ui::Spacer::create(ui::Orientation::Horizontal, context);
auto spacer = ui::Spacer::create(ui::Orientation::Horizontal, context, p.bottomLayout);
spacer->setHStretch(ui::Stretch::Expanding);
spacer->setParent(p.bottomLayout);
p.audioButton->setParent(p.bottomLayout);
p.muteButton->setParent(p.bottomLayout);
hLayout = ui::HorizontalLayout::create(context, p.bottomLayout);
hLayout->setSpacingRole(ui::SizeRole::SpacingTool);
p.audioButton->setParent(hLayout);
p.muteButton->setParent(hLayout);
p.dividers["Status"] = ui::Divider::create(ui::Orientation::Vertical, context, p.layout);
p.statusLayout = ui::HorizontalLayout::create(context, p.layout);
p.statusLayout->setSpacingRole(ui::SizeRole::None);
Expand Down
7 changes: 6 additions & 1 deletion lib/tlPlayApp/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace tl
"Files",
"View",
"Color",
"ColorPicker",
"Info",
"Audio",
"Devices",
Expand All @@ -34,6 +35,7 @@ namespace tl
"Files",
"View",
"Color",
"Color Picker",
"Information",
"Audio",
"Devices",
Expand All @@ -51,6 +53,7 @@ namespace tl
"Files",
"View",
"Color",
"ColorPicker",
"Info",
"Audio",
"Devices",
Expand All @@ -73,7 +76,8 @@ namespace tl
ui::Key::F6,
ui::Key::F7,
ui::Key::F8,
ui::Key::F9
ui::Key::F9,
ui::Key::F10
};
return data[static_cast<size_t>(value)];
}
Expand All @@ -85,6 +89,7 @@ namespace tl
Tool::Files,
Tool::View,
Tool::Color,
Tool::ColorPicker,
Tool::Info,
Tool::Audio,
Tool::Devices,
Expand Down
2 changes: 1 addition & 1 deletion lib/tlPlayApp/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace tl
{
//! Tools.
//!
//! \todo Add a color picker.
//! \todo Add a magnifier.
enum class Tool
{
Files,
View,
Color,
ColorPicker,
Info,
Audio,
Devices,
Expand Down
3 changes: 3 additions & 0 deletions lib/tlPlayApp/ToolsActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace tl
"Show the color tool\n"
"\n"
"Shortcut: {0}",
"Show the color picker tool\n"
"\n"
"Shortcut: {0}",
"Show the information tool\n"
"\n"
"Shortcut: {0}",
Expand Down
12 changes: 8 additions & 4 deletions lib/tlPlayApp/ToolsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tlPlayApp/App.h>
#include <tlPlayApp/AudioTool.h>
#include <tlPlayApp/ColorTool.h>
#include <tlPlayApp/ColorPickerTool.h>
#include <tlPlayApp/DevicesTool.h>
#include <tlPlayApp/FilesTool.h>
#include <tlPlayApp/InfoTool.h>
Expand All @@ -30,6 +31,7 @@ namespace tl
};

void ToolsWidget::_init(
const std::shared_ptr<MainWindow>& mainWindow,
const std::shared_ptr<App>& app,
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
Expand All @@ -42,6 +44,7 @@ namespace tl

p.toolWidgets[Tool::Audio] = AudioTool::create(app, context);
p.toolWidgets[Tool::Color] = ColorTool::create(app, context);
p.toolWidgets[Tool::ColorPicker] = ColorPickerTool::create(mainWindow, app, context);
p.toolWidgets[Tool::Devices] = DevicesTool::create(app, context);
p.toolWidgets[Tool::Files] = FilesTool::create(app, context);
p.toolWidgets[Tool::Info] = InfoTool::create(app, context);
Expand Down Expand Up @@ -73,12 +76,13 @@ namespace tl
{}

std::shared_ptr<ToolsWidget> ToolsWidget::create(
const std::shared_ptr<App>&app,
const std::shared_ptr<system::Context>&context,
const std::shared_ptr<IWidget>&parent)
const std::shared_ptr<MainWindow>& mainWindow,
const std::shared_ptr<App>& app,
const std::shared_ptr<system::Context>& context,
const std::shared_ptr<IWidget>& parent)
{
auto out = std::shared_ptr<ToolsWidget>(new ToolsWidget);
out->_init(app, context, parent);
out->_init(mainWindow, app, context, parent);
return out;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/tlPlayApp/ToolsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace tl
namespace play_app
{
class App;
class MainWindow;

//! Tools widget.
class ToolsWidget : public ui::IWidget
Expand All @@ -21,6 +22,7 @@ namespace tl

protected:
void _init(
const std::shared_ptr<MainWindow>&,
const std::shared_ptr<App>&,
const std::shared_ptr<system::Context>&,
const std::shared_ptr<IWidget>& parent);
Expand All @@ -32,6 +34,7 @@ namespace tl

//! Create a new widget.
static std::shared_ptr<ToolsWidget> create(
const std::shared_ptr<MainWindow>&,
const std::shared_ptr<App>&,
const std::shared_ptr<system::Context>&,
const std::shared_ptr<IWidget>& parent = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion lib/tlPlayQtApp/AudioTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace tl
setWidget(audioTool);

toggleViewAction()->setIcon(QIcon(":/Icons/Audio.svg"));
toggleViewAction()->setShortcut(QKeySequence(Qt::Key_F5));
toggleViewAction()->setShortcut(QKeySequence(Qt::Key_F6));
toggleViewAction()->setToolTip(tr("Show audio controls"));
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/tlPlayQtApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(HEADERS
App.h
AudioActions.h
AudioTool.h
ColorPickerTool.h
ColorTool.h
CompareActions.h
DevicesTool.h
Expand Down Expand Up @@ -33,6 +34,7 @@ set(SOURCE
App.cpp
AudioActions.cpp
AudioTool.cpp
ColorPickerTool.cpp
ColorTool.cpp
CompareActions.cpp
DevicesTool.cpp
Expand Down
Loading

0 comments on commit 542796f

Please sign in to comment.