Removed find color checkbox.

This commit is contained in:
Leonardo Zide 2021-01-31 17:32:38 -08:00
parent dc0ffbc8bb
commit c182daa10d
10 changed files with 197 additions and 121 deletions

View file

@ -395,7 +395,7 @@ bool lcLoadColorFile(lcFile& File, lcStudStyle StudStyle)
if (Valid) if (Valid)
lcAdjustStudStyleColors(Colors, StudStyle); lcAdjustStudStyleColors(Colors, StudStyle);
bool FoundMain = false, FoundEdge = false, FoundStud = false; bool FoundMain = false, FoundEdge = false, FoundStud = false, FoundNoColor = false;
for (const lcColor& Color : Colors) for (const lcColor& Color : Colors)
{ {
@ -412,6 +412,10 @@ bool lcLoadColorFile(lcFile& File, lcStudStyle StudStyle)
case 4242: case 4242:
FoundStud = true; FoundStud = true;
break; break;
case LC_COLOR_NOCOLOR:
FoundNoColor = true;
break;
} }
} }
@ -473,6 +477,27 @@ bool lcLoadColorFile(lcFile& File, lcStudStyle StudStyle)
Colors.push_back(StudCylinderColor); Colors.push_back(StudCylinderColor);
} }
if (!FoundNoColor)
{
lcColor NoColor;
NoColor.Code = LC_COLOR_NOCOLOR;
NoColor.Translucent = false;
NoColor.Group = LC_NUM_COLORGROUPS;
NoColor.Value[0] = 0.5f;
NoColor.Value[1] = 0.5f;
NoColor.Value[2] = 0.5f;
NoColor.Value[3] = 1.0f;
NoColor.Edge[0] = 0.2f;
NoColor.Edge[1] = 0.2f;
NoColor.Edge[2] = 0.2f;
NoColor.Edge[3] = 1.0f;
strcpy(NoColor.Name, "No Color");
strcpy(NoColor.SafeName, "No_Color");
Colors.push_back(NoColor);
}
for (lcColor& Color : gColorList) for (lcColor& Color : gColorList)
Color.Group = LC_NUM_COLORGROUPS; Color.Group = LC_NUM_COLORGROUPS;

View file

@ -5,6 +5,7 @@
#define LC_MAX_COLOR_NAME 64 #define LC_MAX_COLOR_NAME 64
#define LC_COLOR_DIRECT 0x80000000 #define LC_COLOR_DIRECT 0x80000000
#define LC_COLOR_NOCOLOR 0xffffffff
struct lcColor struct lcColor
{ {

View file

@ -20,10 +20,7 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R
Layout->addWidget(new QLabel(tr("Find:")), 0, 0); Layout->addWidget(new QLabel(tr("Find:")), 0, 0);
QCheckBox* FindColorCheckBox = new QCheckBox(this); lcQColorPicker* FindColorPicker = new lcQColorPicker(this, true);
Layout->addWidget(FindColorCheckBox, 0, 1);
lcQColorPicker* FindColorPicker = new lcQColorPicker(this);
Layout->addWidget(FindColorPicker, 0, 2); Layout->addWidget(FindColorPicker, 0, 2);
mFindPartComboBox = new QComboBox(this); mFindPartComboBox = new QComboBox(this);
@ -41,19 +38,10 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R
FindAllButton ->setDefaultAction(gMainWindow->mActions[LC_EDIT_FIND_ALL]); FindAllButton ->setDefaultAction(gMainWindow->mActions[LC_EDIT_FIND_ALL]);
Layout->addWidget(FindAllButton, 0, 6); Layout->addWidget(FindAllButton, 0, 6);
connect(FindColorCheckBox, &QCheckBox::toggled, [this](bool Checked) connect(FindColorPicker, &lcQColorPicker::colorChanged, this, &lcFindReplaceWidget::FindColorIndexChanged);
{
mFindReplaceParams.MatchColor = Checked;
});
connect(FindColorPicker, &lcQColorPicker::colorChanged, [this](int ColorIndex)
{
mFindReplaceParams.ColorIndex = ColorIndex;
});
connect(mFindPartComboBox->lineEdit(), &QLineEdit::returnPressed, gMainWindow->mActions[LC_EDIT_FIND_NEXT], &QAction::trigger); connect(mFindPartComboBox->lineEdit(), &QLineEdit::returnPressed, gMainWindow->mActions[LC_EDIT_FIND_NEXT], &QAction::trigger);
connect(mFindPartComboBox->lineEdit(), &QLineEdit::textEdited, this, &lcFindReplaceWidget::FindTextEdited); connect(mFindPartComboBox->lineEdit(), &QLineEdit::textEdited, this, &lcFindReplaceWidget::FindTextEdited);
connect(mFindPartComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &lcFindReplaceWidget::FindIndexChanged); connect(mFindPartComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &lcFindReplaceWidget::FindActivated);
QCheckBox* ReplaceColorCheckBox = nullptr; QCheckBox* ReplaceColorCheckBox = nullptr;
lcQColorPicker* ReplaceColorPicker = nullptr; lcQColorPicker* ReplaceColorPicker = nullptr;
@ -139,37 +127,43 @@ lcFindReplaceWidget::lcFindReplaceWidget(QWidget* Parent, lcModel* Model, bool R
if (Focus) if (Focus)
{ {
mFindReplaceParams.FindInfo = Focus->mPieceInfo; mFindReplaceParams.FindInfo = Focus->mPieceInfo;
mFindReplaceParams.FindColorIndex = Focus->GetColorIndex();
mFindReplaceParams.ColorIndex = Focus->GetColorIndex(); FindColorPicker->setCurrentColor(mFindReplaceParams.FindColorIndex);
FindColorCheckBox->setChecked(true);
FindColorPicker->setCurrentColor(mFindReplaceParams.ColorIndex);
mFindPartComboBox->setCurrentIndex(mFindPartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo))); mFindPartComboBox->setCurrentIndex(mFindPartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo)));
if (Replace) if (Replace)
{ {
ReplaceColorCheckBox->setChecked(true); ReplaceColorCheckBox->setChecked(true);
ReplaceColorPicker->setCurrentColor(mFindReplaceParams.ColorIndex); ReplaceColorPicker->setCurrentColor(mFindReplaceParams.FindColorIndex);
ReplacePartCheckBox->setChecked(true); ReplacePartCheckBox->setChecked(true);
ReplacePartComboBox->setCurrentIndex(ReplacePartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo))); ReplacePartComboBox->setCurrentIndex(ReplacePartComboBox->findData(QVariant::fromValue((void*)mFindReplaceParams.FindInfo)));
} }
} }
else else
{ {
FindColorPicker->setCurrentColor(lcGetColorIndex(LC_COLOR_NOCOLOR));
mFindPartComboBox->setEditText(QString()); mFindPartComboBox->setEditText(QString());
} }
mFindPartComboBox->setFocus();
adjustSize(); adjustSize();
move(1, 1); move(1, 1);
show(); show();
} }
void lcFindReplaceWidget::FindColorIndexChanged(int ColorIndex)
{
mFindReplaceParams.FindColorIndex = ColorIndex;
}
void lcFindReplaceWidget::FindTextEdited(const QString& Text) void lcFindReplaceWidget::FindTextEdited(const QString& Text)
{ {
mFindReplaceParams.FindString = Text; mFindReplaceParams.FindString = Text;
mFindReplaceParams.FindInfo = nullptr; mFindReplaceParams.FindInfo = nullptr;
} }
void lcFindReplaceWidget::FindIndexChanged(int Index) void lcFindReplaceWidget::FindActivated(int Index)
{ {
mFindReplaceParams.FindString.clear(); mFindReplaceParams.FindString.clear();
mFindReplaceParams.FindInfo = (PieceInfo*)mFindPartComboBox->itemData(Index).value<void*>(); mFindReplaceParams.FindInfo = (PieceInfo*)mFindPartComboBox->itemData(Index).value<void*>();

View file

@ -4,11 +4,10 @@ struct lcFindReplaceParams
{ {
PieceInfo* FindInfo = nullptr; PieceInfo* FindInfo = nullptr;
QString FindString; QString FindString;
int FindColorIndex = 0;
bool MatchColor = false;
bool ReplaceInfo = false; bool ReplaceInfo = false;
bool ReplaceColor = false; bool ReplaceColor = false;
int ColorIndex = 0;
PieceInfo* ReplacePieceInfo = nullptr; PieceInfo* ReplacePieceInfo = nullptr;
int ReplaceColorIndex = 0; int ReplaceColorIndex = 0;
}; };
@ -26,8 +25,9 @@ public:
} }
protected slots: protected slots:
void FindColorIndexChanged(int ColorIndex);
void FindTextEdited(const QString& Text); void FindTextEdited(const QString& Text);
void FindIndexChanged(int Index); void FindActivated(int Index);
protected: protected:
QComboBox* mFindPartComboBox = nullptr; QComboBox* mFindPartComboBox = nullptr;

View file

@ -3789,15 +3789,17 @@ void lcModel::FindReplacePiece(bool SearchForward, bool FindAll)
if (!Params) if (!Params)
return; return;
auto PieceMatches = [Params](const lcPiece* Piece) auto PieceMatches = [](const lcPiece* Piece)
{ {
const lcFindReplaceParams* Params = lcView::GetFindReplaceParams();
if (Params->FindInfo && Params->FindInfo != Piece->mPieceInfo) if (Params->FindInfo && Params->FindInfo != Piece->mPieceInfo)
return false; return false;
if (!Params->FindString.isEmpty() && !strcasestr(Piece->mPieceInfo->m_strDescription, Params->FindString.toLatin1())) if (!Params->FindString.isEmpty() && !strcasestr(Piece->mPieceInfo->m_strDescription, Params->FindString.toLatin1()))
return false; return false;
return !Params->MatchColor || Piece->GetColorIndex() == Params->ColorIndex; return (lcGetColorCode(Params->FindColorIndex) == LC_COLOR_NOCOLOR) || (Piece->GetColorIndex() == Params->FindColorIndex);
}; };
int StartIdx = mPieces.GetSize() - 1; int StartIdx = mPieces.GetSize() - 1;

View file

@ -16,7 +16,7 @@ qtHaveModule(gamepad) {
} }
INCLUDEPATH += qt common INCLUDEPATH += qt common
CONFIG += precompile_header incremental c++14 force_debug_info CONFIG += precompile_header incremental c++11 force_debug_info
win32 { win32 {
RC_ICONS = resources/leocad.ico RC_ICONS = resources/leocad.ico

View file

@ -4,8 +4,8 @@
#include "lc_library.h" #include "lc_library.h"
#include "lc_colors.h" #include "lc_colors.h"
lcQColorList::lcQColorList(QWidget *parent) lcQColorList::lcQColorList(QWidget* Parent, bool AllowNoColor)
: QWidget(parent) : QWidget(Parent), mAllowNoColor(AllowNoColor)
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
@ -17,31 +17,43 @@ lcQColorList::lcQColorList(QWidget *parent)
void lcQColorList::UpdateCells() void lcQColorList::UpdateCells()
{ {
mCells.clear(); mCells.clear();
mGroupRects.clear(); mGroups.clear();
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++)
{
lcColorGroup* Group = &gColorGroups[GroupIdx];
lcColorListGroup ListGroup;
for (int ColorIndex : Group->Colors)
{
mCells.emplace_back(lcColorListCell{ QRect(), ColorIndex });
ListGroup.Name = Group->Name;
ListGroup.Cells.emplace_back(mCells.size());
}
mGroups.emplace_back(std::move(ListGroup));
}
if (mAllowNoColor)
{
mCells.emplace_back(lcColorListCell{ QRect(), lcGetColorIndex(LC_COLOR_NOCOLOR) });
mGroups[LC_COLORGROUP_SPECIAL].Cells.emplace_back(mCells.size());
}
mColumns = 14; mColumns = 14;
mRows = 0; mRows = 0;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (const lcColorListGroup& Group : mGroups)
{ mRows += ((int)Group.Cells.size() + mColumns - 1) / mColumns;
lcColorGroup* Group = &gColorGroups[GroupIdx];
for (int ColorIndex : Group->Colors)
mCells.emplace_back(lcColorListCell{ QRect(), ColorIndex });
mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
}
QFontMetrics Metrics(font()); QFontMetrics Metrics(font());
int TextHeight = 0; int TextHeight = 0;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (lcColorListGroup& Group : mGroups)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx]; Group.Rect = Metrics.boundingRect(rect(), Qt::TextSingleLine | Qt::AlignCenter, Group.Name);
mGroupRects.emplace_back(Metrics.boundingRect(rect(), Qt::TextSingleLine | Qt::AlignCenter, Group->Name)); TextHeight += Group.Rect.height();
TextHeight += mGroupRects[GroupIdx].height();
} }
mPreferredHeight = TextHeight + 10 * mRows; mPreferredHeight = TextHeight + 10 * mRows;
@ -54,13 +66,11 @@ void lcQColorList::UpdateRects()
QFontMetrics Metrics(font()); QFontMetrics Metrics(font());
int TextHeight = 0; int TextHeight = 0;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (lcColorListGroup& Group : mGroups)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx]; Group.Rect = Metrics.boundingRect(rect(), Qt::TextSingleLine | Qt::AlignCenter, Group.Name);
mGroupRects[GroupIdx] = Metrics.boundingRect(rect(), Qt::TextSingleLine | Qt::AlignCenter, Group->Name); TextHeight += Group.Rect.height();
TextHeight += mGroupRects[GroupIdx].height();
} }
mPreferredHeight = TextHeight + 10 * mRows; mPreferredHeight = TextHeight + 10 * mRows;
@ -73,11 +83,8 @@ void lcQColorList::UpdateRects()
mColumns++; mColumns++;
mRows = 0; mRows = 0;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (const lcColorListGroup& Group : mGroups)
{ mRows += ((int)Group.Cells.size() + mColumns - 1) / mColumns;
lcColorGroup* Group = &gColorGroups[GroupIdx];
mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
}
CellWidth = (float)(width() + 1) / (float)mColumns; CellWidth = (float)(width() + 1) / (float)mColumns;
CellHeight = (float)(height() - TextHeight) / (float)mRows; CellHeight = (float)(height() - TextHeight) / (float)mRows;
@ -91,11 +98,8 @@ void lcQColorList::UpdateRects()
mColumns--; mColumns--;
mRows = 0; mRows = 0;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (const lcColorListGroup& Group : mGroups)
{ mRows += ((int)Group.Cells.size() + mColumns - 1) / mColumns;
lcColorGroup* Group = &gColorGroups[GroupIdx];
mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
}
CellWidth = (float)(width() + 1) / (float)mColumns; CellWidth = (float)(width() + 1) / (float)mColumns;
CellHeight = (float)(height() - TextHeight) / (float)mRows; CellHeight = (float)(height() - TextHeight) / (float)mRows;
@ -108,16 +112,15 @@ void lcQColorList::UpdateRects()
float GroupY = 0.0f; float GroupY = 0.0f;
int TotalRows = 1; int TotalRows = 1;
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (lcColorListGroup& Group : mGroups)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx];
int CurColumn = 0; int CurColumn = 0;
int NumRows = 0; int NumRows = 0;
mGroupRects[GroupIdx] = QRect(0, (int)GroupY, width(), mGroupRects[GroupIdx].height()); Group.Rect = QRect(0, (int)GroupY, width(), Group.Rect.height());
GroupY += mGroupRects[GroupIdx].height(); GroupY += Group.Rect.height();
for (size_t ColorIdx = 0; ColorIdx < Group->Colors.size(); ColorIdx++) for (size_t ColorIdx = 0; ColorIdx < Group.Cells.size(); ColorIdx++)
{ {
const int Left = CellWidth * CurColumn - 1; const int Left = CellWidth * CurColumn - 1;
const int Right = (CurColumn + 1) * CellWidth - 1; const int Right = (CurColumn + 1) * CellWidth - 1;
@ -192,7 +195,17 @@ bool lcQColorList::event(QEvent *event)
image.fill(rgb); image.fill(rgb);
QPainter painter(&image); QPainter painter(&image);
painter.setPen(Qt::darkGray); painter.setPen(Qt::darkGray);
if (color->Code != LC_COLOR_NOCOLOR)
painter.drawRect(0, 0, image.width() - 1, image.height() - 1); painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
else
{
painter.setBrush(Qt::black);
painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
const int SquareSize = image.width() / 2 - 1;
painter.fillRect(1, 1, SquareSize, SquareSize, Qt::white);
painter.fillRect(1 + SquareSize, 1 + SquareSize, SquareSize, SquareSize, Qt::white);
}
painter.end(); painter.end();
QByteArray ba; QByteArray ba;
@ -202,8 +215,17 @@ bool lcQColorList::event(QEvent *event)
buffer.close(); buffer.close();
int colorIndex = mCells[CellIndex].ColorIndex; int colorIndex = mCells[CellIndex].ColorIndex;
QString text;
if (color->Code != LC_COLOR_NOCOLOR)
{
const char* format = "<table><tr><td style=\"vertical-align:middle\"><img src=\"data:image/png;base64,%1\"/></td><td>%2 (%3)</td></tr></table>"; const char* format = "<table><tr><td style=\"vertical-align:middle\"><img src=\"data:image/png;base64,%1\"/></td><td>%2 (%3)</td></tr></table>";
QString text = QString(format).arg(QString(buffer.data().toBase64()), gColorList[colorIndex].Name, QString::number(gColorList[colorIndex].Code)); text = QString(format).arg(QString(buffer.data().toBase64()), gColorList[colorIndex].Name, QString::number(gColorList[colorIndex].Code));
}
else
{
const char* format = "<table><tr><td style=\"vertical-align:middle\"><img src=\"data:image/png;base64,%1\"/></td><td>%2</td></tr></table>";
text = QString(format).arg(QString(buffer.data().toBase64()), gColorList[colorIndex].Name);
}
QToolTip::showText(helpEvent->globalPos(), text); QToolTip::showText(helpEvent->globalPos(), text);
return true; return true;
@ -291,9 +313,9 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
size_t CurGroup = 0; size_t CurGroup = 0;
size_t NumCells = 0; size_t NumCells = 0;
for (CurGroup = 0; CurGroup < LC_NUM_COLORGROUPS; CurGroup++) for (CurGroup = 0; CurGroup < mGroups.size(); CurGroup++)
{ {
int NumColors = (int)gColorGroups[CurGroup].Colors.size(); int NumColors = (int)mGroups[CurGroup].Cells.size();
if (mCurrentCell < NumCells + NumColors) if (mCurrentCell < NumCells + NumColors)
break; break;
@ -310,7 +332,7 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
NewCell = mCurrentCell - mColumns; NewCell = mCurrentCell - mColumns;
else if (CurGroup > 0) else if (CurGroup > 0)
{ {
size_t NumColors = gColorGroups[CurGroup - 1].Colors.size(); size_t NumColors = mGroups[CurGroup - 1].Cells.size();
size_t NumColumns = NumColors % mColumns; size_t NumColumns = NumColors % mColumns;
if (NumColumns < Column + 1) if (NumColumns < Column + 1)
@ -321,7 +343,7 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
} }
else if (event->key() == Qt::Key_Down) else if (event->key() == Qt::Key_Down)
{ {
int NumColors = (int)gColorGroups[CurGroup].Colors.size(); int NumColors = (int)mGroups[CurGroup].Cells.size();
if (mCurrentCell + mColumns < NumCells + NumColors) if (mCurrentCell + mColumns < NumCells + NumColors)
NewCell = mCurrentCell + mColumns; NewCell = mCurrentCell + mColumns;
@ -374,12 +396,8 @@ void lcQColorList::paintEvent(QPaintEvent* Event)
Painter.setFont(font()); Painter.setFont(font());
Painter.setPen(palette().color(QPalette::Text)); Painter.setPen(palette().color(QPalette::Text));
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (const lcColorListGroup& Group : mGroups)
{ Painter.drawText(Group.Rect, Qt::TextSingleLine | Qt::AlignLeft, Group.Name);
lcColorGroup* Group = &gColorGroups[GroupIdx];
Painter.drawText(mGroupRects[GroupIdx], Qt::TextSingleLine | Qt::AlignLeft, Group->Name);
}
Painter.setPen(palette().color(QPalette::Shadow)); Painter.setPen(palette().color(QPalette::Shadow));
@ -389,7 +407,20 @@ void lcQColorList::paintEvent(QPaintEvent* Event)
QColor CellColor(Color->Value[0] * 255, Color->Value[1] * 255, Color->Value[2] * 255); QColor CellColor(Color->Value[0] * 255, Color->Value[1] * 255, Color->Value[2] * 255);
Painter.setBrush(CellColor); Painter.setBrush(CellColor);
Painter.drawRect(mCells[CellIndex].Rect); const QRect& Rect = mCells[CellIndex].Rect;
if (Color->Code != LC_COLOR_NOCOLOR)
Painter.drawRect(Rect);
else
{
Painter.setBrush(Qt::black);
Painter.drawRect(Rect);
const int SquareWidth = Rect.width() / 2 - 1;
const int SquareHeight = Rect.height() / 2 - 1;
Painter.fillRect(Rect.x() + 1, Rect.y() + 1, SquareWidth, SquareHeight, Qt::white);
Painter.fillRect(Rect.x() + 1 + SquareWidth, Rect.y() + 1 + SquareHeight, Rect.width() - SquareWidth - 1, SquareHeight, Qt::white);
}
} }
if (mCurrentCell < mCells.size()) if (mCurrentCell < mCells.size())
@ -399,7 +430,10 @@ void lcQColorList::paintEvent(QPaintEvent* Event)
QColor CellColor(Color->Value[0] * 255, Color->Value[1] * 255, Color->Value[2] * 255); QColor CellColor(Color->Value[0] * 255, Color->Value[1] * 255, Color->Value[2] * 255);
Painter.setPen(EdgeColor); Painter.setPen(EdgeColor);
if (Color->Code != LC_COLOR_NOCOLOR)
Painter.setBrush(CellColor); Painter.setBrush(CellColor);
else
Painter.setBrush(Qt::NoBrush);
QRect CellRect = mCells[mCurrentCell].Rect; QRect CellRect = mCells[mCurrentCell].Rect;
CellRect.adjust(1, 1, -1, -1); CellRect.adjust(1, 1, -1, -1);

View file

@ -6,12 +6,19 @@ struct lcColorListCell
int ColorIndex; int ColorIndex;
}; };
struct lcColorListGroup
{
QRect Rect;
QString Name;
std::vector<size_t> Cells;
};
class lcQColorList : public QWidget class lcQColorList : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
lcQColorList(QWidget* Parent = nullptr); lcQColorList(QWidget* Parent = nullptr, bool AllowNoColor = false);
~lcQColorList() = default; ~lcQColorList() = default;
QSize sizeHint() const override; QSize sizeHint() const override;
@ -38,7 +45,7 @@ protected:
void keyPressEvent(QKeyEvent* KeyEvent) override; void keyPressEvent(QKeyEvent* KeyEvent) override;
std::vector<lcColorListCell> mCells; std::vector<lcColorListCell> mCells;
std::vector<QRect> mGroupRects; std::vector<lcColorListGroup> mGroups;
size_t mCurrentCell = 0; size_t mCurrentCell = 0;
quint32 mColorCode = 0; quint32 mColorCode = 0;
@ -48,6 +55,7 @@ protected:
int mWidth = 0; int mWidth = 0;
int mHeight = 0; int mHeight = 0;
int mPreferredHeight = 0; int mPreferredHeight = 0;
bool mAllowNoColor;
QPoint mDragStartPosition; QPoint mDragStartPosition;
}; };

View file

@ -3,8 +3,8 @@
#include "lc_qcolorlist.h" #include "lc_qcolorlist.h"
#include "lc_colors.h" #include "lc_colors.h"
lcQColorPickerPopup::lcQColorPickerPopup(QWidget *parent, int colorIndex) lcQColorPickerPopup::lcQColorPickerPopup(QWidget* Parent, int ColorIndex, bool AllowNoColor)
: QFrame(parent, Qt::Popup) : QFrame(Parent, Qt::Popup)
{ {
setFrameStyle(QFrame::StyledPanel); setFrameStyle(QFrame::StyledPanel);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
@ -16,13 +16,13 @@ lcQColorPickerPopup::lcQColorPickerPopup(QWidget *parent, int colorIndex)
layout->setContentsMargins(0, 0, 0, 0); layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout); setLayout(layout);
colorList = new lcQColorList(this); colorList = new lcQColorList(this, AllowNoColor);
connect(colorList, SIGNAL(colorChanged(int)), this, SLOT(colorChanged(int))); connect(colorList, SIGNAL(colorChanged(int)), this, SLOT(colorChanged(int)));
connect(colorList, SIGNAL(colorSelected(int)), this, SLOT(colorSelected(int))); connect(colorList, SIGNAL(colorSelected(int)), this, SLOT(colorSelected(int)));
layout->addWidget(colorList); layout->addWidget(colorList);
colorList->blockSignals(true); colorList->blockSignals(true);
colorList->setCurrentColor(colorIndex); colorList->setCurrentColor(ColorIndex);
colorList->blockSignals(false); colorList->blockSignals(false);
eventLoop = nullptr; eventLoop = nullptr;
@ -75,17 +75,15 @@ void lcQColorPickerPopup::showEvent(QShowEvent *)
colorList->setFocus(); colorList->setFocus();
} }
lcQColorPicker::lcQColorPicker(QWidget *parent) lcQColorPicker::lcQColorPicker(QWidget* Parent, bool AllowNoColor)
: QPushButton(parent) : QPushButton(Parent), mAllowNoColor(AllowNoColor)
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setAutoDefault(false); setAutoDefault(false);
setCheckable(true); setCheckable(true);
initialColorIndex = 0; UpdateIcon();
currentColorIndex = 0;
updateIcon();
connect(this, SIGNAL(toggled(bool)), SLOT(buttonPressed(bool))); connect(this, SIGNAL(toggled(bool)), SLOT(buttonPressed(bool)));
} }
@ -106,12 +104,12 @@ void lcQColorPicker::setCurrentColorCode(int colorCode)
int lcQColorPicker::currentColor() const int lcQColorPicker::currentColor() const
{ {
return currentColorIndex; return mCurrentColorIndex;
} }
int lcQColorPicker::currentColorCode() const int lcQColorPicker::currentColorCode() const
{ {
return gColorList[currentColorIndex].Code; return gColorList[mCurrentColorIndex].Code;
} }
void lcQColorPicker::buttonPressed(bool toggled) void lcQColorPicker::buttonPressed(bool toggled)
@ -119,7 +117,7 @@ void lcQColorPicker::buttonPressed(bool toggled)
if (!toggled) if (!toggled)
return; return;
lcQColorPickerPopup *popup = new lcQColorPickerPopup(this, currentColorIndex); lcQColorPickerPopup *popup = new lcQColorPickerPopup(this, mCurrentColorIndex, mAllowNoColor);
connect(popup, SIGNAL(changed(int)), SLOT(changed(int))); connect(popup, SIGNAL(changed(int)), SLOT(changed(int)));
connect(popup, SIGNAL(selected(int)), SLOT(selected(int))); connect(popup, SIGNAL(selected(int)), SLOT(selected(int)));
connect(popup, SIGNAL(hid()), SLOT(popupClosed())); connect(popup, SIGNAL(hid()), SLOT(popupClosed()));
@ -146,26 +144,41 @@ void lcQColorPicker::buttonPressed(bool toggled)
popup->show(); popup->show();
} }
void lcQColorPicker::updateIcon() void lcQColorPicker::UpdateIcon()
{ {
int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize); const int IconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
QPixmap pix(iconSize, iconSize); QPixmap Pixmap(IconSize, IconSize);
QPainter p(&pix); QPainter Painter(&Pixmap);
lcColor* color = &gColorList[currentColorIndex]; Painter.setPen(Qt::darkGray);
p.setPen(Qt::darkGray);
p.setBrush(QColor::fromRgbF(color->Value[0], color->Value[1], color->Value[2]));
p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
p.end();
setIcon(QIcon(pix)); const lcColor* Color = &gColorList[mCurrentColorIndex];
if (Color->Code != LC_COLOR_NOCOLOR)
{
Painter.setBrush(QColor::fromRgbF(Color->Value[0], Color->Value[1], Color->Value[2]));
Painter.drawRect(0, 0, Pixmap.width() - 1, Pixmap.height() - 1);
}
else
{
Painter.setBrush(Qt::black);
Painter.drawRect(0, 0, Pixmap.width() - 1, Pixmap.height() - 1);
const int SquareSize = IconSize / 2 - 1;
Painter.fillRect(1, 1, SquareSize, SquareSize, Qt::white);
Painter.fillRect(1 + SquareSize, 1 + SquareSize, SquareSize, SquareSize, Qt::white);
}
Painter.end();
setIcon(QIcon(Pixmap));
} }
void lcQColorPicker::popupClosed() void lcQColorPicker::popupClosed()
{ {
if (initialColorIndex != currentColorIndex) if (mInitialColorIndex != mCurrentColorIndex)
changed(initialColorIndex); changed(mInitialColorIndex);
setChecked(false); setChecked(false);
setFocus(); setFocus();
@ -173,19 +186,19 @@ void lcQColorPicker::popupClosed()
void lcQColorPicker::changed(int colorIndex) void lcQColorPicker::changed(int colorIndex)
{ {
if (colorIndex == currentColorIndex) if (colorIndex == mCurrentColorIndex)
return; return;
currentColorIndex = colorIndex; mCurrentColorIndex = colorIndex;
updateIcon(); UpdateIcon();
repaint(); repaint();
emit colorChanged(currentColorIndex); emit colorChanged(mCurrentColorIndex);
} }
void lcQColorPicker::selected(int colorIndex) void lcQColorPicker::selected(int colorIndex)
{ {
initialColorIndex = colorIndex; mInitialColorIndex = colorIndex;
changed(colorIndex); changed(colorIndex);
} }

View file

@ -8,7 +8,7 @@ class lcQColorPickerPopup : public QFrame
Q_OBJECT Q_OBJECT
public: public:
lcQColorPickerPopup(QWidget *parent = nullptr, int colorIndex = 0); lcQColorPickerPopup(QWidget* Parent = nullptr, int ColorIndex = 0, bool AllowNoColor = false);
~lcQColorPickerPopup(); ~lcQColorPickerPopup();
void exec(); void exec();
@ -28,8 +28,8 @@ protected:
void mouseReleaseEvent(QMouseEvent* MouseEvent) override; void mouseReleaseEvent(QMouseEvent* MouseEvent) override;
private: private:
QEventLoop *eventLoop; QEventLoop* eventLoop;
lcQColorList *colorList; lcQColorList* colorList;
}; };
class lcQColorPicker : public QPushButton class lcQColorPicker : public QPushButton
@ -37,7 +37,7 @@ class lcQColorPicker : public QPushButton
Q_OBJECT Q_OBJECT
public: public:
lcQColorPicker(QWidget *parent = 0); lcQColorPicker(QWidget* Parent = nullptr, bool AllowNoColor = false);
~lcQColorPicker(); ~lcQColorPicker();
int currentColor() const; int currentColor() const;
@ -52,15 +52,14 @@ public slots:
signals: signals:
void colorChanged(int colorIndex); void colorChanged(int colorIndex);
protected:
void updateIcon();
private slots: private slots:
void buttonPressed(bool toggled); void buttonPressed(bool toggled);
void popupClosed(); void popupClosed();
private: protected:
int currentColorIndex; void UpdateIcon();
int initialColorIndex;
};
int mCurrentColorIndex = 0;
int mInitialColorIndex = 0;
bool mAllowNoColor = false;
};