#4256 Update fonts in Project and views when changing preferences

This commit is contained in:
Gaute Lindkvist
2019-04-10 12:49:20 +02:00
parent 0e0d78104b
commit 798e3ff19e
32 changed files with 411 additions and 129 deletions

View File

@@ -47,6 +47,7 @@
#include "cafFactory.h"
#include <QApplication>
#include <QColor>
#include <QColorDialog>
#include <QHBoxLayout>
@@ -81,36 +82,75 @@ void PdmUiColorEditor::configureAndUpdateUi(const QString& uiConfigName)
if (uiObject)
{
uiObject->editorAttribute(uiField()->fieldHandle(), uiConfigName, &m_attributes);
if (m_attributes.showLabel)
{
m_colorTextLabel->show();
}
else
{
m_colorTextLabel->hide();
}
}
QColor col = uiField()->uiValue().value<QColor>();
setColorOnWidget(col);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QMargins PdmUiColorEditor::calculateLabelContentMargins() const
{
QSize editorSize = m_colorSelectionButton->sizeHint();
QSize labelSize = m_label->sizeHint();
int heightDiff = editorSize.height() - labelSize.height();
QMargins contentMargins = m_label->contentsMargins();
if (heightDiff > 0)
{
contentMargins.setTop(contentMargins.top() + heightDiff / 2);
contentMargins.setBottom(contentMargins.bottom() + heightDiff / 2);
}
return contentMargins;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiColorEditor::createEditorWidget(QWidget * parent)
{
{
QWidget* placeholder = new QWidget(parent);
QHBoxLayout* layout = new QHBoxLayout(placeholder);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);
m_colorPixmapLabel = new QLabel(parent);
m_colorTextLabel = new QLabel(parent);
QToolButton* button = new QToolButton(parent);
button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
button->setText(QLatin1String("..."));
m_colorSelectionButton = new QToolButton(parent);
m_colorSelectionButton->setObjectName("ColorSelectionButton");
m_colorSelectionButton->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
QHBoxLayout* buttonLayout = new QHBoxLayout;
m_colorSelectionButton->setLayout(buttonLayout);
QMargins buttonMargins(3, 3, 3, 3);
buttonLayout->setContentsMargins(buttonMargins);
m_colorPreviewLabel = new QLabel(m_colorSelectionButton);
m_colorPreviewLabel->setObjectName("ColorPreviewLabel");
m_colorPreviewLabel->setText(QLatin1String("..."));
m_colorPreviewLabel->setAlignment(Qt::AlignCenter);
QFontMetrics fontMetrics = QApplication::fontMetrics();
buttonLayout->addWidget(m_colorPreviewLabel);
m_colorSelectionButton->setMinimumWidth(fontMetrics.width(m_colorPreviewLabel->text()) + 20);
layout->addWidget(m_colorPixmapLabel);
layout->addWidget(m_colorTextLabel);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
layout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(colorSelectionClicked()));
layout->addWidget(m_colorSelectionButton);
connect(m_colorSelectionButton, SIGNAL(clicked()), this, SLOT(colorSelectionClicked()));
return placeholder;
}
@@ -135,7 +175,7 @@ void PdmUiColorEditor::colorSelectionClicked()
flags |= QColorDialog::ShowAlphaChannel;
}
QColor newColor = QColorDialog::getColor(m_color, m_colorPixmapLabel, "Select color", flags);
QColor newColor = QColorDialog::getColor(m_color, m_colorSelectionButton, "Select color", flags);
if (newColor.isValid() && newColor != m_color)
{
setColorOnWidget(newColor);
@@ -154,17 +194,19 @@ void PdmUiColorEditor::setColorOnWidget(const QColor& color)
{
m_color = color;
QPixmap tmp(16, 16);
tmp.fill(m_color);
m_colorPixmapLabel->setPixmap(tmp);
QString colorString;
if (!color.isValid())
{
colorString = "Undefined";
m_colorSelectionButton->setStyleSheet("");
}
else
{
QColor fontColor = getFontColor(m_color);
QString styleTemplate = "QLabel#ColorPreviewLabel { background-color: %1; color: %2; border: 1px solid %2; }";
QString styleSheet = QString(styleTemplate).arg(m_color.name()).arg(fontColor.name());
m_colorPreviewLabel->setStyleSheet(styleSheet);
colorString = QString("[%1, %2, %3]").arg(QString::number(color.red())).arg(QString::number(color.green())).arg(QString::number(color.blue()));
if (m_attributes.showAlpha)
@@ -172,11 +214,23 @@ void PdmUiColorEditor::setColorOnWidget(const QColor& color)
colorString += QString(" (%4)").arg(QString::number(color.alpha()));
}
}
m_colorTextLabel->setText(colorString);
if (m_attributes.showLabel)
{
m_colorTextLabel->setText(colorString);
}
}
}
//--------------------------------------------------------------------------------------------------
/// Based on http://www.codeproject.com/cs/media/IdealTextColor.asp
//--------------------------------------------------------------------------------------------------
QColor PdmUiColorEditor::getFontColor(const QColor& backgroundColor) const
{
const int THRESHOLD = 105;
int backgroundDelta = (backgroundColor.red() * 0.299) + (backgroundColor.green() * 0.587) + (backgroundColor.blue() * 0.114);
return QColor((255 - backgroundDelta < THRESHOLD) ? Qt::black : Qt::white);
}
} // end namespace caf

View File

@@ -56,11 +56,13 @@ class PdmUiColorEditorAttribute : public PdmUiEditorAttribute
{
public:
bool showAlpha;
bool showLabel;
public:
PdmUiColorEditorAttribute()
{
showAlpha = false;
showLabel = true;
}
};
@@ -82,19 +84,23 @@ protected:
QWidget* createLabelWidget(QWidget * parent) override;
void configureAndUpdateUi(const QString& uiConfigName) override;
QMargins calculateLabelContentMargins() const override;
protected slots:
void colorSelectionClicked();
private:
void setColorOnWidget(const QColor& c);
QColor getFontColor(const QColor& backgroundColor) const;
private:
QPointer<QShortenedLabel> m_label;
QColor m_color;
QPointer<QLabel> m_colorPixmapLabel;
QPointer<QLabel> m_colorTextLabel;
QColor m_color;
QPointer<QLabel> m_colorTextLabel;
QPointer<QToolButton> m_colorSelectionButton;
QPointer<QLabel> m_colorPreviewLabel;
PdmUiColorEditorAttribute m_attributes;
};

View File

@@ -87,6 +87,14 @@ namespace caf {
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setFont(cvf::Font* font)
{
m_font = font;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -62,6 +62,7 @@ namespace caf {
cvf::Vec2ui renderSize() const;
void setTextColor(const cvf::Color3f& color);
void setFont(cvf::Font* font);
void setLineColor(const cvf::Color3f& lineColor);
void setLineWidth(int lineWidth);