12 #include <QApplication>
13 #include <QColorDialog>
14 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
15 #include <QDesktopWidget>
17 #include <QFocusEvent>
18 #include <QGridLayout>
24 #include <QMouseEvent>
25 #include <QPaintEvent>
28 #include <QPushButton>
139 const QString &
text = QString(),
140 QWidget *parent = 0);
143 QColor
color()
const;
144 QString
text()
const;
178 void insertColor(
const QColor &col,
const QString &text,
int index);
186 QColor
color(
int index)
const;
207 QMap<int, QMap<int, QWidget *>> widgetAt;
208 QList<ColorPickerItem *> items;
211 QEventLoop *eventLoop;
236 : QPushButton(parent), popup(0), withColorDialog(enableColorDialog) {
237 setFocusPolicy(Qt::StrongFocus);
239 setAutoDefault(
false);
240 setAutoFillBackground(
true);
244 setText(tr(
"Black"));
245 firstInserted =
false;
253 connect(popup, SIGNAL(selected(
const QColor &)),
255 connect(popup, SIGNAL(hid()), SLOT(popupClosed()));
258 connect(
this, SIGNAL(toggled(
bool)), SLOT(buttonPressed(
bool)));
271 void QtColorPicker::buttonPressed(
bool toggled) {
272 if (!toggled)
return;
274 const QRect desktop = QGuiApplication::primaryScreen()->geometry();
276 QPoint pos = mapToGlobal(rect().bottomLeft());
277 if (pos.x() < desktop.left()) pos.setX(desktop.left());
278 if (pos.y() < desktop.top()) pos.setY(desktop.top());
280 if ((pos.x() + popup->sizeHint().width()) > desktop.width())
281 pos.setX(desktop.width() - popup->sizeHint().width());
282 if ((pos.y() + popup->sizeHint().height()) > desktop.bottom())
283 pos.setY(desktop.bottom() - popup->sizeHint().height());
306 int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
307 QPixmap pix(iconSize, iconSize);
308 pix.fill(palette().button().
color());
313 int h = pix.height();
314 p.setPen(QPen(Qt::gray));
316 p.drawRect(2, 2, w - 5, h - 5);
321 QPushButton::paintEvent(
e);
328 void QtColorPicker::popupClosed() {
390 setText(item->
text());
411 if (!firstInserted) {
414 firstInserted =
true;
427 withColorDialog = enabled;
456 popup.
insertColor(Qt::darkGreen, tr(
"Dark green"), 5);
460 popup.
insertColor(Qt::darkCyan, tr(
"Dark cyan"), 9);
462 popup.
insertColor(Qt::darkMagenta, tr(
"Dark magenta"), 11);
464 popup.
insertColor(Qt::darkYellow, tr(
"Dark yellow"), 13);
466 popup.
insertColor(Qt::darkGray, tr(
"Dark gray"), 15);
467 popup.
insertColor(Qt::lightGray, tr(
"Light gray"), 16);
479 bool withColorDialog,
481 : QFrame(parent, Qt::Popup) {
482 setFrameStyle(QFrame::StyledPanel);
485 setFocusPolicy(Qt::StrongFocus);
486 setMouseTracking(
true);
489 if (withColorDialog) {
491 moreButton->setFixedWidth(24);
492 moreButton->setFixedHeight(21);
493 moreButton->setFrameRect(QRect(2, 2, 20, 17));
509 if (eventLoop) eventLoop->exit();
518 for (
int i = 0; i < items.size(); ++i) {
519 if (items.at(i) && items.at(i)->color() == col)
return items.at(i);
538 if (lastSelectedItem && existingItem != lastSelectedItem)
540 existingItem->setFocus();
547 if (lastSelectedItem) {
556 connect(item, SIGNAL(clicked(QColor)),
this, SIGNAL(
selected(QColor)));
558 if (index == -1) index = items.count();
560 items.insert((
unsigned int)index, item);
570 if (index < 0 || index > (
int)items.count() - 1)
return QColor();
573 return that->items.at(index)->color();
592 QLayoutItem *layoutItem;
594 while ((layoutItem = grid->itemAt(i)) != 0) {
595 QWidget *w = layoutItem->widget();
596 if (w && w->inherits(
"ColorPickerItem")) {
604 if (sender() && sender()->inherits(
"ColorPickerItem")) {
606 lastSel = item->
color();
617 if (!rect().contains(
e->pos())) hide();
628 bool foundFocus =
false;
629 for (
int j = 0; !foundFocus && j < grid->rowCount(); ++j) {
630 for (
int i = 0; !foundFocus && i < grid->columnCount(); ++i) {
631 if (widgetAt[j][i] && widgetAt[j][i]->hasFocus()) {
644 else if (curRow > 0) {
646 curCol = grid->columnCount() - 1;
651 widgetAt[curRow][curCol + 1])
653 else if (curRow < grid->rowCount() - 1) {
665 if (curRow < grid->rowCount() - 1) {
666 QWidget *w = widgetAt[curRow + 1][curCol];
670 for (
int i = 1; i < grid->columnCount(); ++i) {
671 if (!widgetAt[curRow + 1][i]) {
681 case Qt::Key_Enter: {
682 QWidget *w = widgetAt[curRow][curCol];
683 if (w && w->inherits(
"ColorPickerItem")) {
687 QLayoutItem *layoutItem;
689 while ((layoutItem = grid->itemAt(i)) != 0) {
690 QWidget *w = layoutItem->widget();
691 if (w && w->inherits(
"ColorPickerItem")) {
694 layoutItem->widget());
700 lastSel = wi->
color();
703 }
else if (w && w->inherits(
"QPushButton")) {
707 QLayoutItem *layoutItem;
709 while ((layoutItem = grid->itemAt(i)) != 0) {
710 QWidget *w = layoutItem->widget();
711 if (w && w->inherits(
"ColorPickerItem")) {
714 layoutItem->widget());
720 lastSel = wi->
color();
733 widgetAt[curRow][curCol]->setFocus();
747 QFrame::hideEvent(
e);
761 bool foundSelected =
false;
762 for (
int i = 0; i < grid->columnCount(); ++i) {
763 for (
int j = 0; j < grid->rowCount(); ++j) {
764 QWidget *w = widgetAt[j][i];
765 if (w && w->inherits(
"ColorPickerItem")) {
768 foundSelected =
true;
775 if (!foundSelected) {
776 if (items.count() == 0)
779 widgetAt[0][0]->setFocus();
790 if (columns == -1) columns = (int)
ceil(sqrt((
float)items.count()));
795 if (grid)
delete grid;
796 grid =
new QGridLayout(
this);
797 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
800 grid->setContentsMargins(1, 1, 1, 1);
803 int ccol = 0, crow = 0;
804 for (
int i = 0; i < items.size(); ++i) {
806 widgetAt[crow][ccol] = items.at(i);
807 grid->addWidget(items.at(i), crow, ccol++);
808 if (ccol == columns) {
816 grid->addWidget(moreButton, crow, ccol);
817 widgetAt[crow][ccol] = moreButton;
830 QRgb
rgb = QColorDialog::getColor(lastSel, parentWidget()).rgb();
833 QColor col = QColor::fromRgba(
rgb);
846 : QFrame(parent), c(
color), t(text), sel(false) {
927 p.setPen(QPen(Qt::gray, 0, Qt::SolidLine));
929 if (sel) p.drawRect(1, 1, w - 3, h - 3);
931 p.setPen(QPen(
Qt::black, 0, Qt::SolidLine));
932 p.drawRect(3, 3, w - 7, h - 7);
933 p.fillRect(QRect(4, 4, w - 8, h - 8), QBrush(c));
935 if (hasFocus()) p.drawRect(0, 0, w - 1, h - 1);
942 setFrameStyle(StyledPanel);
949 setFrameShadow(Sunken);
965 setFrameShadow(Raised);
974 if (
e->key() == Qt::Key_Up ||
e->key() == Qt::Key_Down ||
975 e->key() == Qt::Key_Left ||
e->key() == Qt::Key_Right) {
976 qApp->sendEvent(parent(),
e);
977 }
else if (
e->key() == Qt::Key_Enter ||
e->key() == Qt::Key_Space ||
978 e->key() == Qt::Key_Return) {
979 setFrameShadow(Sunken);
982 QFrame::keyPressEvent(
e);
990 if (
e->key() == Qt::Key_Up ||
e->key() == Qt::Key_Down ||
991 e->key() == Qt::Key_Left ||
e->key() == Qt::Key_Right) {
992 qApp->sendEvent(parent(),
e);
993 }
else if (
e->key() == Qt::Key_Enter ||
e->key() == Qt::Key_Space ||
994 e->key() == Qt::Key_Return) {
995 setFrameShadow(Raised);
999 QFrame::keyReleaseEvent(
e);
1007 setFrameShadow(Raised);
1009 QFrame::focusOutEvent(
e);
1016 setFrameShadow(Raised);
1018 QFrame::focusOutEvent(
e);
1025 QFrame::paintEvent(
e);
1028 p.fillRect(contentsRect(), palette().button());
1032 int offset = frameShadow() == Sunken ? 1 : 0;
1034 QPen pen(palette().buttonText(), 1);
1037 p.drawRect(r.center().x() +
offset - 4, r.center().y() +
offset, 1, 1);
1038 p.drawRect(r.center().x() +
offset, r.center().y() +
offset, 1, 1);
1039 p.drawRect(r.center().x() +
offset + 4, r.center().y() +
offset, 1, 1);
1041 p.setPen(QPen(
Qt::black, 0, Qt::SolidLine));
1049 #include "qtcolorpicker.moc"
Tensor Minimum(const Tensor &input, const Tensor &other)
Computes the element-wise minimum of input and other. The tensors must have same data type and device...
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
constexpr Rgb cyan(0, MAX, MAX)
constexpr Rgb black(0, 0, 0)
constexpr Rgb magenta(MAX, 0, MAX)
constexpr Rgb darkBlue(0, 0, MAX/2)
constexpr Rgb white(MAX, MAX, MAX)
constexpr Rgb red(MAX, 0, 0)
constexpr Rgb blue(0, 0, MAX)
constexpr Rgb green(0, MAX, 0)
constexpr Rgb yellow(MAX, MAX, 0)