Skip to content

Commit

Permalink
Spreadsheet: fix color pick disappearing in toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed Nov 28, 2021
1 parent fd1ae04 commit 60fcc6a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
98 changes: 46 additions & 52 deletions src/Mod/Spreadsheet/Gui/Workbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,59 +68,53 @@ Workbench::~Workbench()

void Workbench::activated()
{
auto bar = Gui::getMainWindow()->findChild<QToolBar*>(QString::fromLatin1("Spreadsheet"));
if (!bar)
return;

QPalette palette = Gui::getMainWindow()->palette();
if (!foregroundColor) {
foregroundColor = new QtColorPicker();
foregroundColor->setObjectName(QString::fromLatin1("Spreadsheet_ForegroundColor"));
foregroundColor->setStandardColors();
QObject::connect(foregroundColor, SIGNAL(colorSet(QColor)), workbenchHelper.get(), SLOT(setForegroundColor(QColor)));
}
QSignalBlocker guard(foregroundColor);
foregroundColor->setCurrentColor(palette.color(QPalette::WindowText));
foregroundColor->setToolTip(QObject::tr("Set cell(s) foreground color"));
foregroundColor->setWhatsThis(QObject::tr("Sets the Spreadsheet cell(s) foreground color"));
foregroundColor->setStatusTip(QObject::tr("Set cell(s) foreground color"));
bar->addWidget(foregroundColor);

if (!backgroundColor) {
backgroundColor = new QtColorPicker();
backgroundColor->setObjectName(QString::fromLatin1("Spreadsheet_BackgroundColor"));
backgroundColor->setStandardColors();
QObject::connect(backgroundColor, SIGNAL(colorSet(QColor)), workbenchHelper.get(), SLOT(setBackgroundColor(QColor)));
}
QSignalBlocker guard2(backgroundColor);
backgroundColor->setCurrentColor(palette.color(QPalette::Base));
backgroundColor->setToolTip(QObject::tr("Set cell(s) background color"));
backgroundColor->setWhatsThis(QObject::tr("Sets the Spreadsheet cell(s) background color"));
backgroundColor->setStatusTip(QObject::tr("Set cell(s) background color"));
bar->addWidget(backgroundColor);

if (!initialized) {
QList<QToolBar*> bars = Gui::getMainWindow()->findChildren<QToolBar*>(QString::fromLatin1("Spreadsheet"));

if (bars.size() == 1) {
QToolBar * bar = bars[0];
QtColorPicker * foregroundColor;
QtColorPicker * backgroundColor;
QPalette palette = Gui::getMainWindow()->palette();

QList<QtColorPicker*> fgList = Gui::getMainWindow()->findChildren<QtColorPicker*>(QString::fromLatin1("Spreadsheet_ForegroundColor"));
if (fgList.size() > 0)
foregroundColor = fgList[0];
else {
foregroundColor = new QtColorPicker();
foregroundColor->setObjectName(QString::fromLatin1("Spreadsheet_ForegroundColor"));
foregroundColor->setStandardColors();
foregroundColor->setCurrentColor(palette.color(QPalette::WindowText));
QObject::connect(foregroundColor, SIGNAL(colorSet(QColor)), workbenchHelper.get(), SLOT(setForegroundColor(QColor)));
}
foregroundColor->setToolTip(QObject::tr("Set cell(s) foreground color"));
foregroundColor->setWhatsThis(QObject::tr("Sets the Spreadsheet cell(s) foreground color"));
foregroundColor->setStatusTip(QObject::tr("Set cell(s) foreground color"));
bar->addWidget(foregroundColor);

QList<QtColorPicker*> bgList = Gui::getMainWindow()->findChildren<QtColorPicker*>(QString::fromLatin1("Spreadsheet_BackgroundColor"));
if (bgList.size() > 0)
backgroundColor = bgList[0];
else {
backgroundColor = new QtColorPicker();
backgroundColor->setObjectName(QString::fromLatin1("Spreadsheet_BackgroundColor"));
backgroundColor->setStandardColors();
backgroundColor->setCurrentColor(palette.color(QPalette::Base));
QObject::connect(backgroundColor, SIGNAL(colorSet(QColor)), workbenchHelper.get(), SLOT(setBackgroundColor(QColor)));
}
backgroundColor->setToolTip(QObject::tr("Set cell(s) background color"));
backgroundColor->setWhatsThis(QObject::tr("Sets the Spreadsheet cell(s) background color"));
backgroundColor->setStatusTip(QObject::tr("Set cell(s) background color"));
bar->addWidget(backgroundColor);

auto toggleVisible = [bar]() {
bool visible = bar->orientation() == Qt::Horizontal;
auto actions = bar->actions();
for (auto picker : bar->findChildren<QtColorPicker*>()) {
for (auto action : actions) {
if (bar->widgetForAction(action) == picker)
action->setVisible(visible);
}
}
};
toggleVisible();
QObject::connect(bar, &QToolBar::orientationChanged, toggleVisible);

initialized = true;
initialized = true;
QObject::connect(bar, &QToolBar::orientationChanged,
std::bind(&Workbench::onToolbarOrientationChange, this, bar));
}
onToolbarOrientationChange(bar);
}

void Workbench::onToolbarOrientationChange(QToolBar *bar)
{
bool visible = bar->orientation() == Qt::Horizontal;
auto actions = bar->actions();
for (auto action : actions) {
if (auto widget = bar->widgetForAction(action)) {
if (widget == foregroundColor || widget == backgroundColor)
action->setVisible(visible);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Mod/Spreadsheet/Gui/Workbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#ifndef SPREADSHEET_WORKBENCH_H
#define SPREADSHEET_WORKBENCH_H

#include <QPointer>
#include <Gui/Workbench.h>
#include "qtcolorpicker.h"

class QtColorPicker;
class QColor;
class QToolBar;

namespace SpreadsheetGui {

Expand All @@ -38,8 +40,8 @@ namespace SpreadsheetGui {

class SpreadsheetGuiExport WorkbenchHelper : public QObject
{
Q_OBJECT
protected Q_SLOTS:
Q_OBJECT
public Q_SLOTS:
void setForegroundColor(const QColor &color);
void setBackgroundColor(const QColor &color);
};
Expand All @@ -56,11 +58,14 @@ class SpreadsheetGuiExport Workbench : public Gui::StdWorkbench
private:
bool initialized;
std::unique_ptr<WorkbenchHelper> workbenchHelper;
QPointer<QtColorPicker> foregroundColor;
QPointer<QtColorPicker> backgroundColor;

protected:
Gui::MenuItem *setupMenuBar() const;
Gui::ToolBarItem* setupToolBars() const;
Gui::ToolBarItem* setupCommandBars() const;
void onToolbarOrientationChange(QToolBar *);
};

} // namespace SpreadsheetModGui
Expand Down

0 comments on commit 60fcc6a

Please sign in to comment.