TextEditor: Use QtColorButton in ColorSchemeEdit

Replaces the style-sheet based drawing. Reduces handling code. Adds
Drag'n'drop capabilities.

Change-Id: I7e7ff06df61ffead00d4c747c8b0f5e81de169ae
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Alessandro Portale
2023-03-28 14:30:42 +02:00
parent 3aa0291cd1
commit 556768b293
2 changed files with 25 additions and 43 deletions

View File

@@ -6,13 +6,13 @@
#include "texteditortr.h" #include "texteditortr.h"
#include <utils/layoutbuilder.h> #include <utils/layoutbuilder.h>
#include <utils/qtcolorbutton.h>
#include <utils/theme/theme.h> #include <utils/theme/theme.h>
#include <utils/utilsicons.h> #include <utils/utilsicons.h>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QApplication> #include <QApplication>
#include <QCheckBox> #include <QCheckBox>
#include <QColorDialog>
#include <QComboBox> #include <QComboBox>
#include <QDoubleSpinBox> #include <QDoubleSpinBox>
#include <QLabel> #include <QLabel>
@@ -25,14 +25,6 @@ namespace TextEditor::Internal {
const int layoutSpacing = 6; const int layoutSpacing = 6;
static QString colorButtonStyleSheet(const QColor &bgColor)
{
QString rc("border-width: 2px; border-radius: 2px; border-color: black; ");
rc += bgColor.isValid() ? "border-style: solid; background:" + bgColor.name() + ";"
: QString("border-style: dotted;");
return rc;
}
class FormatsModel : public QAbstractListModel class FormatsModel : public QAbstractListModel
{ {
public: public:
@@ -132,7 +124,7 @@ ColorSchemeEdit::ColorSchemeEdit(QWidget *parent) :
resize(513, 416); resize(513, 416);
auto colorButton = [] () { auto colorButton = [] () {
auto tb = new QToolButton; auto tb = new Utils::QtColorButton;
tb->setMinimumWidth(56); tb->setMinimumWidth(56);
return tb; return tb;
}; };
@@ -245,9 +237,9 @@ ColorSchemeEdit::ColorSchemeEdit(QWidget *parent) :
connect(m_itemList->selectionModel(), &QItemSelectionModel::currentRowChanged, connect(m_itemList->selectionModel(), &QItemSelectionModel::currentRowChanged,
this, &ColorSchemeEdit::currentItemChanged); this, &ColorSchemeEdit::currentItemChanged);
connect(m_foregroundToolButton, &QAbstractButton::clicked, connect(m_foregroundToolButton, &Utils::QtColorButton::colorChanged,
this, &ColorSchemeEdit::changeForeColor); this, &ColorSchemeEdit::changeForeColor);
connect(m_backgroundToolButton, &QAbstractButton::clicked, connect(m_backgroundToolButton, &Utils::QtColorButton::colorChanged,
this, &ColorSchemeEdit::changeBackColor); this, &ColorSchemeEdit::changeBackColor);
connect(m_eraseBackgroundToolButton, &QAbstractButton::clicked, connect(m_eraseBackgroundToolButton, &QAbstractButton::clicked,
this, &ColorSchemeEdit::eraseBackColor); this, &ColorSchemeEdit::eraseBackColor);
@@ -265,7 +257,7 @@ ColorSchemeEdit::ColorSchemeEdit(QWidget *parent) :
this, &ColorSchemeEdit::checkCheckBoxes); this, &ColorSchemeEdit::checkCheckBoxes);
connect(m_italicCheckBox, &QAbstractButton::toggled, connect(m_italicCheckBox, &QAbstractButton::toggled,
this, &ColorSchemeEdit::checkCheckBoxes); this, &ColorSchemeEdit::checkCheckBoxes);
connect(m_underlineColorToolButton, &QToolButton::clicked, connect(m_underlineColorToolButton, &Utils::QtColorButton::colorChanged,
this, &ColorSchemeEdit::changeUnderlineColor); this, &ColorSchemeEdit::changeUnderlineColor);
connect(m_eraseUnderlineColorToolButton, &QToolButton::clicked, connect(m_eraseUnderlineColorToolButton, &QToolButton::clicked,
this, &ColorSchemeEdit::eraseUnderlineColor); this, &ColorSchemeEdit::eraseUnderlineColor);
@@ -347,7 +339,7 @@ void ColorSchemeEdit::updateForegroundControls()
m_foregroundToolButton->setVisible(isVisible); m_foregroundToolButton->setVisible(isVisible);
m_eraseForegroundToolButton->setVisible(isVisible); m_eraseForegroundToolButton->setVisible(isVisible);
m_foregroundToolButton->setStyleSheet(colorButtonStyleSheet(format.foreground())); m_foregroundToolButton->setColor(format.foreground());
m_eraseForegroundToolButton->setEnabled(!m_readOnly m_eraseForegroundToolButton->setEnabled(!m_readOnly
&& m_curItem > 0 && m_curItem > 0
&& format.foreground().isValid()); && format.foreground().isValid());
@@ -366,7 +358,7 @@ void ColorSchemeEdit::updateBackgroundControls()
m_backgroundToolButton->setVisible(isVisible); m_backgroundToolButton->setVisible(isVisible);
m_eraseBackgroundToolButton->setVisible(isVisible); m_eraseBackgroundToolButton->setVisible(isVisible);
m_backgroundToolButton->setStyleSheet(colorButtonStyleSheet(format.background())); m_backgroundToolButton->setColor(format.background());
m_eraseBackgroundToolButton->setEnabled(!m_readOnly m_eraseBackgroundToolButton->setEnabled(!m_readOnly
&& m_curItem > 0 && m_curItem > 0
&& format.background().isValid()); && format.background().isValid());
@@ -466,7 +458,7 @@ void ColorSchemeEdit::updateUnderlineControls()
m_eraseUnderlineColorToolButton->setVisible(isVisible); m_eraseUnderlineColorToolButton->setVisible(isVisible);
m_underlineComboBox->setVisible(isVisible); m_underlineComboBox->setVisible(isVisible);
m_underlineColorToolButton->setStyleSheet(colorButtonStyleSheet(format.underlineColor())); m_underlineColorToolButton->setColor(format.underlineColor());
m_eraseUnderlineColorToolButton->setEnabled(!m_readOnly m_eraseUnderlineColorToolButton->setEnabled(!m_readOnly
&& m_curItem > 0 && m_curItem > 0
&& format.underlineColor().isValid()); && format.underlineColor().isValid());
@@ -478,11 +470,8 @@ void ColorSchemeEdit::changeForeColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor color = m_scheme.formatFor(m_descriptions[m_curItem].id()).foreground();
const QColor newColor = QColorDialog::getColor(color, m_boldCheckBox->window()); const QColor newColor = m_foregroundToolButton->color();
if (!newColor.isValid())
return;
m_foregroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseForegroundToolButton->setEnabled(true); m_eraseForegroundToolButton->setEnabled(true);
for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) { for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) {
@@ -498,11 +487,8 @@ void ColorSchemeEdit::changeBackColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor color = m_scheme.formatFor(m_descriptions[m_curItem].id()).background();
const QColor newColor = QColorDialog::getColor(color, m_boldCheckBox->window()); const QColor newColor = m_backgroundToolButton->color();
if (!newColor.isValid())
return;
m_backgroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseBackgroundToolButton->setEnabled(true); m_eraseBackgroundToolButton->setEnabled(true);
for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) { for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) {
@@ -521,14 +507,13 @@ void ColorSchemeEdit::eraseBackColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor newColor; m_backgroundToolButton->setColor({});
m_backgroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseBackgroundToolButton->setEnabled(false); m_eraseBackgroundToolButton->setEnabled(false);
const QList<QModelIndex> indexes = m_itemList->selectionModel()->selectedRows(); const QList<QModelIndex> indexes = m_itemList->selectionModel()->selectedRows();
for (const QModelIndex &index : indexes) { for (const QModelIndex &index : indexes) {
const TextStyle category = m_descriptions[index.row()].id(); const TextStyle category = m_descriptions[index.row()].id();
m_scheme.formatFor(category).setBackground(newColor); m_scheme.formatFor(category).setBackground({});
m_formatsModel->emitDataChanged(index); m_formatsModel->emitDataChanged(index);
} }
@@ -539,14 +524,13 @@ void ColorSchemeEdit::eraseForeColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor newColor; m_foregroundToolButton->setColor({});
m_foregroundToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseForegroundToolButton->setEnabled(false); m_eraseForegroundToolButton->setEnabled(false);
const QList<QModelIndex> indexes = m_itemList->selectionModel()->selectedRows(); const QList<QModelIndex> indexes = m_itemList->selectionModel()->selectedRows();
for (const QModelIndex &index : indexes) { for (const QModelIndex &index : indexes) {
const TextStyle category = m_descriptions[index.row()].id(); const TextStyle category = m_descriptions[index.row()].id();
m_scheme.formatFor(category).setForeground(newColor); m_scheme.formatFor(category).setForeground({});
m_formatsModel->emitDataChanged(index); m_formatsModel->emitDataChanged(index);
} }
@@ -635,11 +619,8 @@ void ColorSchemeEdit::changeUnderlineColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor color = m_scheme.formatFor(m_descriptions[m_curItem].id()).underlineColor();
const QColor newColor = QColorDialog::getColor(color, m_boldCheckBox->window()); const QColor newColor = m_underlineColorToolButton->color();
if (!newColor.isValid())
return;
m_underlineColorToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseUnderlineColorToolButton->setEnabled(true); m_eraseUnderlineColorToolButton->setEnabled(true);
for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) { for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) {
@@ -653,13 +634,12 @@ void ColorSchemeEdit::eraseUnderlineColor()
{ {
if (m_curItem == -1) if (m_curItem == -1)
return; return;
QColor newColor; m_underlineColorToolButton->setColor({});
m_underlineColorToolButton->setStyleSheet(colorButtonStyleSheet(newColor));
m_eraseUnderlineColorToolButton->setEnabled(false); m_eraseUnderlineColorToolButton->setEnabled(false);
for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) { for (const QModelIndex &index : m_itemList->selectionModel()->selectedRows()) {
const TextStyle category = m_descriptions[index.row()].id(); const TextStyle category = m_descriptions[index.row()].id();
m_scheme.formatFor(category).setUnderlineColor(newColor); m_scheme.formatFor(category).setUnderlineColor({});
m_formatsModel->emitDataChanged(index); m_formatsModel->emitDataChanged(index);
} }
} }

View File

@@ -20,6 +20,8 @@ class QScrollArea;
class QToolButton; class QToolButton;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { class QtColorButton; }
namespace TextEditor::Internal { namespace TextEditor::Internal {
class FormatsModel; class FormatsModel;
@@ -80,10 +82,10 @@ private:
QLabel *m_builtinSchemeLabel; QLabel *m_builtinSchemeLabel;
QWidget *m_fontProperties; QWidget *m_fontProperties;
QLabel *m_foregroundLabel; QLabel *m_foregroundLabel;
QToolButton *m_foregroundToolButton; Utils::QtColorButton *m_foregroundToolButton;
QAbstractButton *m_eraseForegroundToolButton; QAbstractButton *m_eraseForegroundToolButton;
QLabel *m_backgroundLabel; QLabel *m_backgroundLabel;
QToolButton *m_backgroundToolButton; Utils::QtColorButton *m_backgroundToolButton;
QAbstractButton *m_eraseBackgroundToolButton; QAbstractButton *m_eraseBackgroundToolButton;
QLabel *m_relativeForegroundHeadline; QLabel *m_relativeForegroundHeadline;
QLabel *m_foregroundLightnessLabel; QLabel *m_foregroundLightnessLabel;
@@ -100,7 +102,7 @@ private:
QCheckBox *m_italicCheckBox; QCheckBox *m_italicCheckBox;
QLabel *m_underlineHeadline; QLabel *m_underlineHeadline;
QLabel *m_underlineLabel; QLabel *m_underlineLabel;
QToolButton *m_underlineColorToolButton; Utils::QtColorButton *m_underlineColorToolButton;
QAbstractButton *m_eraseUnderlineColorToolButton; QAbstractButton *m_eraseUnderlineColorToolButton;
QComboBox *m_underlineComboBox; QComboBox *m_underlineComboBox;