Skip to content

Commit

Permalink
ColorToolButton: fix popup #251
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Sep 23, 2024
1 parent 171e1ad commit 238c947
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
89 changes: 39 additions & 50 deletions src/ui/widgets/colortoolbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Animators/coloranimator.h"
#include "Animators/outlinesettingsanimator.h"
#include "Animators/paintsettingsanimator.h"
#include "GUI/global.h"
#include "Private/document.h"
#include "colorsetting.h"
#include "themesupport.h"
Expand All @@ -35,72 +36,43 @@

using namespace Friction::Ui;

class AWidget : public QWidget
{
public:
AWidget(QWidget *parent = nullptr) : QWidget(parent) {}

protected:
void showEvent(QShowEvent *e) override
{
adjustSize(); // force resize since it's a popup
QWidget::showEvent(e);
}
};

class AScrollArea : public QScrollArea
{
public:
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
{
widget()->update();
widget()->adjustSize();
setWidget(takeWidget());
QWidget::showEvent(e);
}
};

ColorToolButton::ColorToolButton(Document& document,
QWidget *parent,
const bool fillOnly,
const bool strokeOnly,
const bool flatOnly)
: ToolButton(parent)
: QToolButton(parent)
, mIsFillOnly(fillOnly)
, mIsStrokeOnly(strokeOnly)
, mIsFlatOnly(flatOnly)
, mPop(nullptr)
, mScroll(nullptr)
, mColor(Qt::transparent)
, mColorLabel(nullptr)
, mBackgroundWidget(nullptr)
, mFillStrokeWidget(nullptr)
, mColorAct(nullptr)
, mDocument(document)
{
setAutoPopup(false); // TODO: add to settings (auto/click popup)
setFocusPolicy(Qt::NoFocus);
setPopupMode(ToolButtonPopupMode::InstantPopup);

const auto pop = new AWidget(this);
const auto popLay = new QVBoxLayout(pop);
mPop = new QWidget(this);
mPop->setObjectName("DarkWidget");
const auto popLay = new QVBoxLayout(mPop);

mPop->setMinimumSize({300, 235});

pop->setContentsMargins(10, 10, 10, 10);
pop->setMinimumSize({300, mIsFlatOnly ? 200 : 260});
popLay->setMargin(0);
mScroll = new QScrollArea(this);
mScroll->setObjectName("DarkWidget");
mScroll->setFocusPolicy(Qt::NoFocus);

const auto area = new AScrollArea(this);
popLay->addWidget(area);
mScroll->setBackgroundRole(QPalette::Window);
mScroll->setFrameShadow(QFrame::Plain);
mScroll->setFrameShape(QFrame::NoFrame);
mScroll->setWidgetResizable(true);

popLay->addWidget(mScroll);

if (!mIsFlatOnly) {
mFillStrokeWidget = new FillStrokeSettingsWidget(mDocument,
Expand All @@ -109,15 +81,16 @@ ColorToolButton::ColorToolButton(Document& document,
mIsFillOnly,
mIsStrokeOnly);
mFillStrokeWidget->setContentsMargins(0, 0, 0, 0);
area->setWidget(mFillStrokeWidget);
mScroll->setWidget(mFillStrokeWidget);
} else {
mBackgroundWidget = new ColorSettingsWidget(this);
mBackgroundWidget->setObjectName("DarkWidget");
mBackgroundWidget->setColorModeVisible(false);
area->setWidget(mBackgroundWidget);
mScroll->setWidget(mBackgroundWidget);
}

mColorAct = new QWidgetAction(this);
mColorAct->setDefaultWidget(pop);
mColorAct->setDefaultWidget(mPop);

mColorLabel = new ColorLabel(this, false);

Expand Down Expand Up @@ -196,3 +169,19 @@ QColor ColorToolButton::color() const
if (mColorTarget) { return mColorTarget->getColor(); }
return mColor;
}

void ColorToolButton::mousePressEvent(QMouseEvent *e)
{
if (mFillStrokeWidget) { mFillStrokeWidget->adjustSize(); }
else if (mBackgroundWidget) { mBackgroundWidget->adjustSize(); }

mScroll->setWidget(mScroll->takeWidget());
mScroll->adjustSize();
mScroll->setMinimumHeight(mScroll->widget()->sizeHint().height() + 10);

mPop->adjustSize();

qDebug() << "pop" << mPop->sizeHint() << "scroll" << mScroll->sizeHint();

QToolButton::mousePressEvent(e);
}
11 changes: 8 additions & 3 deletions src/ui/widgets/colortoolbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
#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 <QToolButton>
#include <QWidgetAction>
#include <QPushButton>
#include <QScrollArea>

namespace Friction
{
namespace Ui
{
class UI_EXPORT ColorToolButton : public ToolButton
class UI_EXPORT ColorToolButton : public QToolButton
{
public:
ColorToolButton(Document& document,
Expand All @@ -57,10 +57,15 @@ namespace Friction
void updateColor();
QColor color() const;

protected:
void mousePressEvent(QMouseEvent *e) override;

private:
bool mIsFillOnly;
bool mIsStrokeOnly;
bool mIsFlatOnly;
QWidget *mPop;
QScrollArea *mScroll;
QColor mColor;
ColorLabel *mColorLabel;
ColorSettingsWidget *mBackgroundWidget;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets/fillstrokesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ FillStrokeSettingsWidget::FillStrokeSettingsWidget(Document &document,
mLayout->setContentsMargins(0, 0, 0, 0);
mLayout->setMargin(0);

mFillAndStrokeWidget->setObjectName("DarkWidget");
if (noScroll) {
mLayout->addWidget(mFillAndStrokeWidget);
} else {
const auto mFillStrokeArea = new ScrollArea(this);
mFillAndStrokeWidget->setObjectName("DarkWidget");
mFillStrokeArea->setWidget(mFillAndStrokeWidget);
mLayout->addWidget(mFillStrokeArea);
}
Expand Down

0 comments on commit 238c947

Please sign in to comment.