Skip to content

Commit

Permalink
New Color ToolBar works!
Browse files Browse the repository at this point in the history
The new color toolbar now works, still need some UI fixes.
  • Loading branch information
rodlie committed Sep 17, 2024
1 parent 6fd1575 commit 171e1ad
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/app/GUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ MainWindow::MainWindow(Document& document,
mStackIndexScene = mStackWidget->addWidget(mLayoutHandler->sceneLayout());
mStackIndexWelcome = mStackWidget->addWidget(mWelcomeDialog);

mColorToolBar = new Ui::ColorToolBar(this);
mColorToolBar = new Ui::ColorToolBar(mDocument, this);
mCanvasToolBar = new Ui::CanvasToolBar(this);
mColorToolBar->setMovable(false);
installNumericFilter(mCanvasToolBar->getResolutionComboBox());
Expand Down
27 changes: 14 additions & 13 deletions src/ui/widgets/colortoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

using namespace Friction::Ui;

ColorToolBar::ColorToolBar(QWidget *parent)
ColorToolBar::ColorToolBar(Document &document,
QWidget *parent)
: QToolBar(parent)
, mColorFill(nullptr)
, mColorStroke(nullptr)
Expand All @@ -39,7 +40,7 @@ ColorToolBar::ColorToolBar(QWidget *parent)
setObjectName("ColorToolBar");
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
setEnabled(false);
setupWidgets();
setupWidgets(document);

eSizesUI::widget.add(this, [this](const int size) {
this->setIconSize({size, size});
Expand Down Expand Up @@ -77,20 +78,20 @@ void ColorToolBar::setCurrentBox(BoundingBox *target)
mColorFill->setEnabled(enabled);
mColorStroke->setEnabled(enabled);

if (!target) {
mColorFill->setColorFillTarget(nullptr);
mColorStroke->setColorStrokeTarget(nullptr);
} else {
mColorFill->setColorFillTarget(target->getFillSettings());
mColorStroke->setColorStrokeTarget(target->getStrokeSettings());
}
mColorFill->setCurrentBox(target);
mColorStroke->setCurrentBox(target);
mColorFill->setColorFillTarget(target ? target->getFillSettings() : nullptr);
mColorStroke->setColorStrokeTarget(target ? target->getStrokeSettings() : nullptr);
}

void ColorToolBar::setupWidgets()
void ColorToolBar::setupWidgets(Document &document)
{
mColorFill = new ColorToolButton(this);
mColorStroke = new ColorToolButton(this);
mColorBackground = new ColorToolButton(this, true);
mColorFill = new ColorToolButton(document, this,
true, false, false);
mColorStroke = new ColorToolButton(document, this,
false, true, false);
mColorBackground = new ColorToolButton(document, this,
false, false, true);

mColorFillAct = new QAction(QIcon::fromTheme("color"),
tr("Fill"), this);
Expand Down
6 changes: 4 additions & 2 deletions src/ui/widgets/colortoolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "ui_global.h"

#include "canvas.h"
#include "Private/document.h"
#include "widgets/toolbar.h"
#include "widgets/colortoolbutton.h"

Expand All @@ -40,12 +41,13 @@ namespace Friction
class UI_EXPORT ColorToolBar : public QToolBar
{
public:
explicit ColorToolBar(QWidget *parent = nullptr);
explicit ColorToolBar(Document& document,
QWidget *parent = nullptr);
void setCurrentCanvas(Canvas * const target);
void setCurrentBox(BoundingBox *target);

private:
void setupWidgets();
void setupWidgets(Document& document);
void adjustWidgets();
void addSpacer();
ColorToolButton *mColorFill;
Expand Down
158 changes: 43 additions & 115 deletions src/ui/widgets/colortoolbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Animators/paintsettingsanimator.h"
#include "Private/document.h"
#include "colorsetting.h"
#include "themesupport.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
Expand All @@ -42,36 +43,51 @@ class AWidget : public QWidget
protected:
void showEvent(QShowEvent *e) override
{
adjustSize(); // force resize since we live in a popup
adjustSize(); // force resize since it's a popup
QWidget::showEvent(e);
}
};

class AScrollArea : public QScrollArea
{
public:
AScrollArea(QWidget *parent = nullptr) : QScrollArea(parent) {}
AScrollArea(QWidget *parent = nullptr) : QScrollArea(parent)
{
setObjectName("NoMarginVerticalScrollBar");
setPalette(ThemeSupport::getDarkerPalette());
setAutoFillBackground(true);
setWidgetResizable(true);
setContentsMargins(0, 0, 0, 0);
setBackgroundRole(QPalette::Window);
setFrameShadow(QFrame::Plain);
setFrameShape(QFrame::NoFrame);
}

protected:
void showEvent(QShowEvent *e) override
{
setWidget(takeWidget()); // yeah, I know ... only way to force adjustSize(?)
widget()->update();
widget()->adjustSize();
setWidget(takeWidget());
QWidget::showEvent(e);
}
};

ColorToolButton::ColorToolButton(QWidget * const parent,
ColorToolButton::ColorToolButton(Document& document,
QWidget *parent,
const bool fillOnly,
const bool strokeOnly,
const bool flatOnly)
: ToolButton(parent)
, mIsFillOnly(fillOnly)
, mIsStrokeOnly(strokeOnly)
, mIsFlatOnly(flatOnly)
, mColor(Qt::transparent)
, mColorLabel(nullptr)
, mColorNoneButton(nullptr)
, mColorFlatButton(nullptr)
, mColorGradientButton(nullptr)
, mColorWidget(nullptr)
, mGradientWidget(nullptr)
, mBackgroundWidget(nullptr)
, mFillStrokeWidget(nullptr)
, mColorAct(nullptr)
, mDocument(document)
{
setAutoPopup(false); // TODO: add to settings (auto/click popup)
setPopupMode(ToolButtonPopupMode::InstantPopup);
Expand All @@ -80,62 +96,25 @@ ColorToolButton::ColorToolButton(QWidget * const parent,
const auto popLay = new QVBoxLayout(pop);

pop->setContentsMargins(10, 10, 10, 10);
pop->setMinimumWidth(250);
pop->setMinimumSize({300, mIsFlatOnly ? 200 : 260});
popLay->setMargin(0);

const auto popInner = new QWidget(this);
const auto popInnerLay = new QVBoxLayout(popInner);

popInner->setContentsMargins(0, 0, 0, 0);
popInnerLay->setMargin(0);

const auto area = new AScrollArea(this);
area->setWidgetResizable(true);
area->setContentsMargins(0, 0, 0, 0);
area->setFrameShape(QFrame::NoFrame);
area->setWidget(popInner);

mColorWidget = new ColorSettingsWidget(this);
mColorWidget->setColorModeVisible(false);
popLay->addWidget(area);

if (!mIsFlatOnly) {
mColorNoneButton = new QPushButton(QIcon::fromTheme("fill_none_2"),
tr("None"), this);
mColorFlatButton = new QPushButton(QIcon::fromTheme("fill_flat_2"),
tr("Flat"), this);
mColorGradientButton = new QPushButton(QIcon::fromTheme("fill_gradient_2"),
tr("Gradient"), this);

connect(mColorNoneButton, &QPushButton::released,
this, [this]() { setColorType(PaintType::NOPAINT); });
connect(mColorFlatButton, &QPushButton::released,
this, [this]() { setColorType(PaintType::FLATPAINT); });
connect(mColorGradientButton, &QPushButton::released,
this, [this]() { setColorType(PaintType::GRADIENTPAINT); });

mColorNoneButton->setCheckable(true);
mColorFlatButton->setCheckable(true);
mColorGradientButton->setCheckable(true);

mGradientWidget = new GradientWidget(this);

const auto buttWid = new QWidget(this);
const auto buttLay = new QHBoxLayout(buttWid);

buttWid->setContentsMargins(0, 0, 0, 0);
buttLay->setMargin(0);

buttLay->addWidget(mColorNoneButton);
buttLay->addWidget(mColorFlatButton);
buttLay->addWidget(mColorGradientButton);

popLay->addWidget(buttWid);
popInnerLay->addWidget(mGradientWidget);
mFillStrokeWidget = new FillStrokeSettingsWidget(mDocument,
this,
true,
mIsFillOnly,
mIsStrokeOnly);
mFillStrokeWidget->setContentsMargins(0, 0, 0, 0);
area->setWidget(mFillStrokeWidget);
} else {
mBackgroundWidget = new ColorSettingsWidget(this);
mBackgroundWidget->setColorModeVisible(false);
area->setWidget(mBackgroundWidget);
}
popInnerLay->addWidget(mColorWidget);
popLay->addWidget(area);

updateColorTypeWidgets(mIsFlatOnly ? PaintType::FLATPAINT : PaintType::NOPAINT);

mColorAct = new QWidgetAction(this);
mColorAct->setDefaultWidget(pop);
Expand All @@ -151,38 +130,28 @@ ColorToolButton::ColorToolButton(QWidget * const parent,
updateColor();
}

ColorToolButton::ColorToolButton(ColorAnimator * const colorTarget,
QWidget * const parent)
: ColorToolButton(parent)
void ColorToolButton::setCurrentBox(BoundingBox *target)
{
setColorTarget(colorTarget);
mFillStrokeWidget->setCurrentBox(target);
}

void ColorToolButton::setColorFillTarget(FillSettingsAnimator * const target)
{
if (mIsFlatOnly) { return; }
mColorFillTarget.assign(target);
mGradientWidget->setCurrentGradient(target ? target->getGradient() : nullptr);
setColorTarget(target ?
(target->getPaintType() == PaintType::GRADIENTPAINT ?
mGradientWidget->getColorAnimator() : target->getColorAnimator())
: nullptr);
setColorTarget(target ? target->getColorAnimator() : nullptr);
}

void ColorToolButton::setColorStrokeTarget(OutlineSettingsAnimator * const target)
{
if (mIsFlatOnly) { return; }
mColorStrokeTarget.assign(target);
mGradientWidget->setCurrentGradient(target ? target->getGradient() : nullptr);
setColorTarget(target ?
(target->getPaintType() == PaintType::GRADIENTPAINT ?
mGradientWidget->getColorAnimator() : target->getColorAnimator())
: nullptr);
setColorTarget(target ? target->getColorAnimator() : nullptr);
}

void ColorToolButton::setColorTarget(ColorAnimator * const target)
{
mColorWidget->setTarget(target);
if (mBackgroundWidget) { mBackgroundWidget->setTarget(target); }
mColorTarget.assign(target);
if (target) {
mColorTarget << connect(target->getVal1Animator(),
Expand All @@ -198,46 +167,6 @@ void ColorToolButton::setColorTarget(ColorAnimator * const target)
updateColor();
}

void ColorToolButton::setColorType(const PaintType &type)
{
if (mIsFlatOnly) { return; }
if (mColorFillTarget) {
mColorFillTarget->setPaintType(type);
Document::sInstance->actionFinished();
setColorTarget(type == PaintType::FLATPAINT ?
mColorFillTarget->getColorAnimator() :
type == PaintType::GRADIENTPAINT ?
mGradientWidget->getColorAnimator() : nullptr);
} else if (mColorStrokeTarget) {
mColorStrokeTarget->setPaintType(type);
Document::sInstance->actionFinished();
setColorTarget(type == PaintType::FLATPAINT ?
mColorStrokeTarget->getColorAnimator() :
type == PaintType::GRADIENTPAINT ?
mGradientWidget->getColorAnimator() : nullptr);
}
updateColorTypeWidgets(type);
}

void ColorToolButton::updateColorTypeWidgets(const PaintType &type)
{
if (mIsFlatOnly) { return; }
mColorNoneButton->setChecked(type == PaintType::NOPAINT);
mColorFlatButton->setChecked(type == PaintType::FLATPAINT);
mColorGradientButton->setChecked(type == PaintType::GRADIENTPAINT);
mGradientWidget->setEnabled(type == PaintType::GRADIENTPAINT);
mGradientWidget->setVisible(type == PaintType::GRADIENTPAINT);
mColorWidget->setEnabled(type == PaintType::FLATPAINT ||
type == PaintType::GRADIENTPAINT);

if (type != PaintType::GRADIENTPAINT) { return ;}
if (mColorFillTarget) {
mGradientWidget->setCurrentGradient(mColorFillTarget->getGradient());
} else if (mColorStrokeTarget) {
mGradientWidget->setCurrentGradient(mColorStrokeTarget->getGradient());
}
}

void ColorToolButton::updateColor()
{
PaintType type = mIsFlatOnly ? PaintType::FLATPAINT : PaintType::NOPAINT;
Expand All @@ -260,7 +189,6 @@ void ColorToolButton::updateColor()
mColor = type == PaintType::NOPAINT ? Qt::transparent : color;
mColorLabel->setColor(mColor);
mColorLabel->setAlpha(mColor.alphaF());
updateColorTypeWidgets(type);
}

QColor ColorToolButton::color() const
Expand Down
24 changes: 13 additions & 11 deletions src/ui/widgets/colortoolbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
#ifndef FRICTION_COLOR_TOOLBUTTON_H
#define FRICTION_COLOR_TOOLBUTTON_H

#include "gradientwidgets/gradientwidget.h"
#include "ui_global.h"

#include "Private/document.h"
#include "gradientwidgets/gradientwidget.h"
#include "Animators/coloranimator.h"
#include "widgets/colorlabel.h"
#include "widgets/toolbutton.h"
#include "conncontextptr.h"
#include "widgets/colorsettingswidget.h"
#include "widgets/fillstrokesettings.h"

#include <QWidgetAction>
#include <QPushButton>
Expand All @@ -43,28 +45,28 @@ namespace Friction
class UI_EXPORT ColorToolButton : public ToolButton
{
public:
ColorToolButton(QWidget * const parent = nullptr,
ColorToolButton(Document& document,
QWidget *parent = nullptr,
const bool fillOnly = false,
const bool strokeOnly = false,
const bool flatOnly = false);
ColorToolButton(ColorAnimator * const colorTarget,
QWidget * const parent = nullptr);
void setCurrentBox(BoundingBox *target);
void setColorFillTarget(FillSettingsAnimator * const target);
void setColorStrokeTarget(OutlineSettingsAnimator * const target);
void setColorTarget(ColorAnimator * const target);
void setColorType(const PaintType &type);
void updateColorTypeWidgets(const PaintType &type);
void updateColor();
QColor color() const;

private:
bool mIsFillOnly;
bool mIsStrokeOnly;
bool mIsFlatOnly;
QColor mColor;
ColorLabel *mColorLabel;
QPushButton *mColorNoneButton;
QPushButton *mColorFlatButton;
QPushButton *mColorGradientButton;
ColorSettingsWidget *mColorWidget;
GradientWidget *mGradientWidget;
ColorSettingsWidget *mBackgroundWidget;
FillStrokeSettingsWidget *mFillStrokeWidget;
QWidgetAction *mColorAct;
Document& mDocument;
ConnContextQPtr<ColorAnimator> mColorTarget;
ConnContextQPtr<FillSettingsAnimator> mColorFillTarget;
ConnContextQPtr<OutlineSettingsAnimator> mColorStrokeTarget;
Expand Down
Loading

0 comments on commit 171e1ad

Please sign in to comment.