leocad/common/lc_colorpicker.cpp

205 lines
4.5 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
2023-05-16 06:12:08 +02:00
#include "lc_colorpicker.h"
2023-04-30 04:48:30 +02:00
#include "lc_colorlist.h"
2013-08-09 06:57:18 +02:00
#include "lc_colors.h"
2023-05-16 06:02:17 +02:00
lcColorPickerPopup::lcColorPickerPopup(QWidget* Parent, int ColorIndex, bool AllowNoColor)
2021-02-01 02:32:38 +01:00
: QFrame(Parent, Qt::Popup)
2013-08-09 06:57:18 +02:00
{
setFrameStyle(QFrame::StyledPanel);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
QGridLayout *layout = new QGridLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
2023-05-16 06:02:17 +02:00
mColorList = new lcColorList(this, AllowNoColor);
connect(mColorList, &lcColorList::ColorChanged, this, &lcColorPickerPopup::ColorChanged);
connect(mColorList, &lcColorList::ColorSelected, this, &lcColorPickerPopup::ColorSelected);
layout->addWidget(mColorList);
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
mColorList->blockSignals(true);
mColorList->SetCurrentColor(ColorIndex);
mColorList->blockSignals(false);
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
mEventLoop = nullptr;
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
lcColorPickerPopup::~lcColorPickerPopup()
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
if (mEventLoop)
mEventLoop->exit();
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::exec()
2013-08-09 06:57:18 +02:00
{
show();
2023-05-16 06:02:17 +02:00
QEventLoop EventLoop;
mEventLoop = &EventLoop;
(void) EventLoop.exec();
mEventLoop = nullptr;
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::mouseReleaseEvent(QMouseEvent* MouseEvent)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
if (!rect().contains(MouseEvent->pos()))
2013-08-09 06:57:18 +02:00
hide();
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::hideEvent(QHideEvent* HideEvent)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
if (mEventLoop)
mEventLoop->exit();
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
emit Hid();
QFrame::hideEvent(HideEvent);
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::ColorChanged(int ColorIndex)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
emit Changed(ColorIndex);
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::ColorSelected(int ColorIndex)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
emit Selected(ColorIndex);
2013-08-09 06:57:18 +02:00
hide();
}
2023-05-16 06:02:17 +02:00
void lcColorPickerPopup::showEvent(QShowEvent* ShowEvent)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
Q_UNUSED(ShowEvent);
mColorList->setFocus();
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
lcColorPicker::lcColorPicker(QWidget* Parent, bool AllowNoColor)
2021-02-01 02:32:38 +01:00
: QPushButton(Parent), mAllowNoColor(AllowNoColor)
2013-08-09 06:57:18 +02:00
{
setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setAutoDefault(false);
setCheckable(true);
2021-02-01 02:32:38 +01:00
UpdateIcon();
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
connect(this, &QPushButton::toggled, this, &lcColorPicker::ButtonPressed);
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
lcColorPicker::~lcColorPicker()
2013-08-09 06:57:18 +02:00
{
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::SetCurrentColor(int ColorIndex)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
Selected(ColorIndex);
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::SetCurrentColorCode(int ColorCode)
{
2023-05-16 06:02:17 +02:00
SetCurrentColor(lcGetColorIndex(ColorCode));
}
2023-05-16 06:02:17 +02:00
int lcColorPicker::GetCurrentColor() const
2013-08-09 06:57:18 +02:00
{
2021-02-01 02:32:38 +01:00
return mCurrentColorIndex;
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
int lcColorPicker::GetCurrentColorCode() const
{
2021-02-01 02:32:38 +01:00
return gColorList[mCurrentColorIndex].Code;
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::ButtonPressed(bool Toggled)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
if (!Toggled)
2013-08-09 06:57:18 +02:00
return;
2023-05-16 06:02:17 +02:00
lcColorPickerPopup* Popup = new lcColorPickerPopup(this, mCurrentColorIndex, mAllowNoColor);
connect(Popup, &lcColorPickerPopup::Changed, this, &lcColorPicker::Changed);
connect(Popup, &lcColorPickerPopup::Selected, this, &lcColorPicker::Selected);
connect(Popup, &lcColorPickerPopup::Hid, this, &lcColorPicker::PopupClosed);
Popup->setMinimumSize(300, 200);
2013-08-09 06:57:18 +02:00
2021-07-06 02:00:41 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QScreen* Screen = screen();
2023-05-16 06:02:17 +02:00
const QRect Desktop = Screen ? Screen->geometry() : QRect();
2021-07-06 02:12:06 +02:00
#else
2023-05-16 06:02:17 +02:00
const QRect Desktop = QApplication::desktop()->geometry();
2021-07-06 02:00:41 +02:00
#endif
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
QPoint Pos = mapToGlobal(rect().bottomLeft());
if (Pos.x() < Desktop.left())
Pos.setX(Desktop.left());
if (Pos.y() < Desktop.top())
Pos.setY(Desktop.top());
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
if ((Pos.x() + Popup->width()) > Desktop.width())
Pos.setX(Desktop.width() - Popup->width());
if ((Pos.y() + Popup->height()) > Desktop.bottom())
Pos.setY(Desktop.bottom() - Popup->height());
Popup->move(Pos);
2013-08-09 06:57:18 +02:00
clearFocus();
update();
2023-05-16 06:02:17 +02:00
Popup->setFocus();
Popup->show();
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::UpdateIcon()
2013-08-09 06:57:18 +02:00
{
2021-02-01 22:49:20 +01:00
const int IconSize = 14;//style()->pixelMetric(QStyle::PM_SmallIconSize);
2021-02-01 02:32:38 +01:00
QPixmap Pixmap(IconSize, IconSize);
2013-08-09 06:57:18 +02:00
2021-02-01 02:32:38 +01:00
QPainter Painter(&Pixmap);
2013-08-09 06:57:18 +02:00
2021-02-01 02:32:38 +01:00
Painter.setPen(Qt::darkGray);
2013-08-09 06:57:18 +02:00
2021-02-01 02:32:38 +01:00
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
2021-02-01 22:49:20 +01:00
lcDrawNoColorRect(Painter, QRect(0, 0, Pixmap.width() - 1, Pixmap.height() - 1));
2021-02-01 02:32:38 +01:00
Painter.end();
setIcon(QIcon(Pixmap));
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::PopupClosed()
2013-08-09 06:57:18 +02:00
{
2021-02-01 02:32:38 +01:00
if (mInitialColorIndex != mCurrentColorIndex)
2023-05-16 06:02:17 +02:00
Changed(mInitialColorIndex);
2013-08-09 06:57:18 +02:00
setChecked(false);
setFocus();
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::Changed(int ColorIndex)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
if (ColorIndex == mCurrentColorIndex)
2013-08-09 06:57:18 +02:00
return;
2023-05-16 06:02:17 +02:00
mCurrentColorIndex = ColorIndex;
2021-02-01 02:32:38 +01:00
UpdateIcon();
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
update();
2013-08-09 06:57:18 +02:00
2023-05-16 06:02:17 +02:00
emit ColorChanged(mCurrentColorIndex);
2013-08-09 06:57:18 +02:00
}
2023-05-16 06:02:17 +02:00
void lcColorPicker::Selected(int ColorIndex)
2013-08-09 06:57:18 +02:00
{
2023-05-16 06:02:17 +02:00
mInitialColorIndex = ColorIndex;
Changed(ColorIndex);
2013-08-09 06:57:18 +02:00
}