| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <cmath> |
| |
|
| | #include <QApplication> |
| | #include <QColorDialog> |
| | #include <QGridLayout> |
| | #include <QLayout> |
| | #include <QPainter> |
| | #include <QPushButton> |
| | #include <QStyle> |
| | #include <QtCore/QMap> |
| | #include <QtGui/QFocusEvent> |
| | #include <QtGui/QHideEvent> |
| | #include <QtGui/QKeyEvent> |
| | #include <QtGui/QMouseEvent> |
| | #include <QtGui/QPaintEvent> |
| | #include <QtGui/QPixmap> |
| | #include <QtGui/QShowEvent> |
| |
|
| | #include <Gui/FileDialog.h> |
| |
|
| | #include "qtcolorpicker.h" |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | class ColorPickerButton : public QFrame |
| | { |
| | Q_OBJECT |
| |
|
| | public: |
| | explicit ColorPickerButton(QWidget *parent); |
| |
|
| | Q_SIGNALS: |
| | void clicked(); |
| |
|
| | protected: |
| | void mousePressEvent(QMouseEvent *e) override; |
| | void mouseMoveEvent(QMouseEvent *e) override; |
| | void mouseReleaseEvent(QMouseEvent *e) override; |
| | void keyPressEvent(QKeyEvent *e) override; |
| | void keyReleaseEvent(QKeyEvent *e) override; |
| | void paintEvent(QPaintEvent *e) override; |
| | void focusInEvent(QFocusEvent *e) override; |
| | void focusOutEvent(QFocusEvent *e) override; |
| | }; |
| |
|
| | |
| | |
| | |
| | class ColorPickerItem : public QFrame |
| | { |
| | Q_OBJECT |
| |
|
| | public: |
| | ColorPickerItem(const QColor &color = Qt::white, const QString &text = QString(), |
| | QWidget *parent = nullptr); |
| | ~ColorPickerItem() override; |
| |
|
| | QColor color() const; |
| | QString text() const; |
| |
|
| | void setSelected(bool); |
| | bool isSelected() const; |
| | Q_SIGNALS: |
| | void clicked(); |
| | void selected(); |
| |
|
| | public Q_SLOTS: |
| | void setColor(const QColor &color, const QString &text = QString()); |
| |
|
| | protected: |
| | void mousePressEvent(QMouseEvent *e) override; |
| | void mouseReleaseEvent(QMouseEvent *e) override; |
| | void mouseMoveEvent(QMouseEvent *e) override; |
| | void paintEvent(QPaintEvent *e) override; |
| |
|
| | private: |
| | QColor c; |
| | QString t; |
| | bool sel; |
| | }; |
| |
|
| | |
| | |
| | |
| | class ColorPickerPopup : public QFrame |
| | { |
| | Q_OBJECT |
| |
|
| | public: |
| | ColorPickerPopup(int width, bool withColorDialog, |
| | QWidget *parent = nullptr); |
| | ~ColorPickerPopup() override; |
| |
|
| | void insertColor(const QColor &col, const QString &text, int index); |
| | void exec(); |
| |
|
| | void setExecFlag(); |
| |
|
| | QColor lastSelected() const; |
| |
|
| | ColorPickerItem *find(const QColor &col) const; |
| | QColor color(int index) const; |
| |
|
| | void setLastSel(const QColor & col); |
| |
|
| | Q_SIGNALS: |
| | void selected(const QColor &); |
| | void hid(); |
| |
|
| | public Q_SLOTS: |
| | void getColorFromDialog(); |
| |
|
| | protected Q_SLOTS: |
| | void updateSelected(); |
| |
|
| | protected: |
| | void keyPressEvent(QKeyEvent *e) override; |
| | void showEvent(QShowEvent *e) override; |
| | void hideEvent(QHideEvent *e) override; |
| | void mouseReleaseEvent(QMouseEvent *e) override; |
| |
|
| | void regenerateGrid(); |
| |
|
| | private: |
| | QMap<int, QMap<int, QWidget *> > widgetAt; |
| | QList<ColorPickerItem *> items; |
| | QGridLayout *grid; |
| | ColorPickerButton *moreButton; |
| | QEventLoop *eventLoop; |
| |
|
| | int lastPos; |
| | int cols; |
| | QColor lastSel; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | QtColorPicker::QtColorPicker(QWidget *parent, |
| | int cols, bool enableColorDialog) |
| | : QPushButton(parent), popup(nullptr), withColorDialog(enableColorDialog) |
| | { |
| | setFocusPolicy(Qt::StrongFocus); |
| | setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
| | setAutoDefault(false); |
| | setAutoFillBackground(true); |
| | setCheckable(true); |
| |
|
| | |
| | setText(tr("Black")); |
| | firstInserted = false; |
| |
|
| | |
| | col = Qt::black; |
| | dirty = true; |
| |
|
| | |
| | popup = new ColorPickerPopup(cols, withColorDialog, this); |
| | connect(popup, &ColorPickerPopup::selected, this, &QtColorPicker::setCurrentColor); |
| | connect(popup, &ColorPickerPopup::hid, this, &QtColorPicker::popupClosed); |
| |
|
| | |
| | connect(this, &QtColorPicker::toggled, this, &QtColorPicker::buttonPressed); |
| | } |
| |
|
| | |
| | |
| | |
| | QtColorPicker::~QtColorPicker() |
| | { |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void QtColorPicker::buttonPressed(bool toggled) |
| | { |
| | if (!toggled) |
| | return; |
| |
|
| | const QRect desktop = QApplication::activeWindow()->geometry(); |
| |
|
| | |
| | QPoint pos = mapToGlobal(rect().bottomLeft()); |
| | if (pos.x() < desktop.left()) |
| | pos.setX(desktop.left()); |
| | if (pos.y() < desktop.top()) |
| | pos.setY(desktop.top()); |
| |
|
| | if ((pos.x() + popup->sizeHint().width()) > desktop.right()) |
| | pos.setX(desktop.right() - popup->sizeHint().width()); |
| | if ((pos.y() + popup->sizeHint().height()) > desktop.bottom()) |
| | pos.setY(desktop.bottom() - popup->sizeHint().height()); |
| | popup->move(pos); |
| |
|
| | if (ColorPickerItem *item = popup->find(col)) |
| | item->setSelected(true); |
| |
|
| | |
| | |
| | |
| | clearFocus(); |
| | update(); |
| |
|
| | |
| | popup->setFocus(); |
| |
|
| | |
| | popup->show(); |
| | } |
| |
|
| | |
| | |
| | |
| | void QtColorPicker::paintEvent(QPaintEvent *e) |
| | { |
| | if (dirty) { |
| | int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize); |
| | QPixmap pix(iconSize, iconSize); |
| | pix.fill(palette().button().color()); |
| |
|
| | QPainter p(&pix); |
| |
|
| | int w = pix.width(); |
| | int h = pix.height(); |
| | p.setPen(QPen(Qt::gray)); |
| | p.setBrush(col); |
| | p.drawRect(2, 2, w - 5, h - 5); |
| | setIcon(QIcon(pix)); |
| |
|
| | dirty = false; |
| | } |
| | QPushButton::paintEvent(e); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | void QtColorPicker::popupClosed() |
| | { |
| | setChecked(false); |
| | setFocus(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | QColor QtColorPicker::currentColor() const |
| | { |
| | return col; |
| | } |
| |
|
| | |
| | |
| | |
| | QColor QtColorPicker::color(int index) const |
| | { |
| | return popup->color(index); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void QtColorPicker::setStandardColors() |
| | { |
| | insertColor(Qt::black, tr("Black")); |
| | insertColor(Qt::white, tr("White")); |
| | insertColor(Qt::red, tr("Red")); |
| | insertColor(Qt::darkRed, tr("Dark red")); |
| | insertColor(Qt::green, tr("Green")); |
| | insertColor(Qt::darkGreen, tr("Dark green")); |
| | insertColor(Qt::blue, tr("Blue")); |
| | insertColor(Qt::darkBlue, tr("Dark blue")); |
| | insertColor(Qt::cyan, tr("Cyan")); |
| | insertColor(Qt::darkCyan, tr("Dark cyan")); |
| | insertColor(Qt::magenta, tr("Magenta")); |
| | insertColor(Qt::darkMagenta, tr("Dark magenta")); |
| | insertColor(Qt::yellow, tr("Yellow")); |
| | insertColor(Qt::darkYellow, tr("Dark yellow")); |
| | insertColor(Qt::gray, tr("Gray")); |
| | insertColor(Qt::darkGray, tr("Dark gray")); |
| | insertColor(Qt::lightGray, tr("Light gray")); |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void QtColorPicker::setCurrentColor(const QColor &color) |
| | { |
| | if (color.isValid() && col == color) { |
| | Q_EMIT colorSet(color); |
| | return; |
| | } |
| | if (col == color || !color.isValid()) |
| | return; |
| |
|
| | ColorPickerItem *item = popup->find(color); |
| | if (!item) { |
| | insertColor(color, tr("Custom Color")); |
| | item = popup->find(color); |
| | } |
| |
|
| | popup->setLastSel(color); |
| |
|
| | col = color; |
| | setText(item->text()); |
| |
|
| | dirty = true; |
| |
|
| | popup->hide(); |
| | repaint(); |
| |
|
| | item->setSelected(true); |
| | Q_EMIT colorChanged(color); |
| | Q_EMIT colorSet(color); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | void QtColorPicker::insertColor(const QColor &color, const QString &text, int index) |
| | { |
| | popup->insertColor(color, text, index); |
| | if (!firstInserted) { |
| | col = color; |
| | setText(text); |
| | firstInserted = true; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void QtColorPicker::setColorDialogEnabled(bool enabled) |
| | { |
| | withColorDialog = enabled; |
| | } |
| | bool QtColorPicker::colorDialogEnabled() const |
| | { |
| | return withColorDialog; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | QColor QtColorPicker::getColor(const QPoint &point, bool allowCustomColors) |
| | { |
| | ColorPickerPopup popup(-1, allowCustomColors); |
| |
|
| | popup.insertColor(Qt::black, tr("Black"), 0); |
| | popup.insertColor(Qt::white, tr("White"), 1); |
| | popup.insertColor(Qt::red, tr("Red"), 2); |
| | popup.insertColor(Qt::darkRed, tr("Dark red"), 3); |
| | popup.insertColor(Qt::green, tr("Green"), 4); |
| | popup.insertColor(Qt::darkGreen, tr("Dark green"), 5); |
| | popup.insertColor(Qt::blue, tr("Blue"), 6); |
| | popup.insertColor(Qt::darkBlue, tr("Dark blue"), 7); |
| | popup.insertColor(Qt::cyan, tr("Cyan"), 8); |
| | popup.insertColor(Qt::darkCyan, tr("Dark cyan"), 9); |
| | popup.insertColor(Qt::magenta, tr("Magenta"), 10); |
| | popup.insertColor(Qt::darkMagenta, tr("Dark magenta"), 11); |
| | popup.insertColor(Qt::yellow, tr("Yellow"), 12); |
| | popup.insertColor(Qt::darkYellow, tr("Dark yellow"), 13); |
| | popup.insertColor(Qt::gray, tr("Gray"), 14); |
| | popup.insertColor(Qt::darkGray, tr("Dark gray"), 15); |
| | popup.insertColor(Qt::lightGray, tr("Light gray"), 16); |
| |
|
| | popup.move(point); |
| | popup.exec(); |
| | return popup.lastSelected(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | ColorPickerPopup::ColorPickerPopup(int width, bool withColorDialog, |
| | QWidget *parent) |
| | : QFrame(parent, Qt::Popup) |
| | { |
| | setFrameStyle(QFrame::StyledPanel); |
| | setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| |
|
| | setFocusPolicy(Qt::StrongFocus); |
| | setMouseTracking(true); |
| | cols = width; |
| |
|
| | if (withColorDialog) { |
| | moreButton = new ColorPickerButton(this); |
| | moreButton->setFixedWidth(24); |
| | moreButton->setFixedHeight(21); |
| | moreButton->setFrameRect(QRect(2, 2, 20, 17)); |
| | connect(moreButton, &ColorPickerButton::clicked, this, &ColorPickerPopup::getColorFromDialog); |
| | } |
| | else { |
| | moreButton = nullptr; |
| | } |
| |
|
| | eventLoop = nullptr; |
| | grid = nullptr; |
| | regenerateGrid(); |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | ColorPickerPopup::~ColorPickerPopup() |
| | { |
| | if (eventLoop) |
| | eventLoop->exit(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | ColorPickerItem *ColorPickerPopup::find(const QColor &col) const |
| | { |
| | for (int i = 0; i < items.size(); ++i) { |
| | if (items.at(i) && items.at(i)->color() == col) |
| | return items.at(i); |
| | } |
| |
|
| | return nullptr; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void ColorPickerPopup::insertColor(const QColor &col, const QString &text, int index) |
| | { |
| | |
| | ColorPickerItem *existingItem = find(col); |
| | ColorPickerItem *lastSelectedItem = find(lastSelected()); |
| |
|
| | if (existingItem) { |
| | if (lastSelectedItem && existingItem != lastSelectedItem) |
| | lastSelectedItem->setSelected(false); |
| | existingItem->setFocus(); |
| | existingItem->setSelected(true); |
| | return; |
| | } |
| |
|
| | ColorPickerItem *item = new ColorPickerItem(col, text, this); |
| |
|
| | if (lastSelectedItem) { |
| | lastSelectedItem->setSelected(false); |
| | } |
| | else { |
| | item->setSelected(true); |
| | lastSel = col; |
| | } |
| | item->setFocus(); |
| |
|
| | connect(item, &ColorPickerItem::selected, this, &ColorPickerPopup::updateSelected); |
| |
|
| | if (index == -1) |
| | index = items.count(); |
| |
|
| | items.insert((unsigned int)index, item); |
| | regenerateGrid(); |
| |
|
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | QColor ColorPickerPopup::color(int index) const |
| | { |
| | if (index < 0 || index > (int) items.count() - 1) |
| | return QColor(); |
| |
|
| | ColorPickerPopup *that = (ColorPickerPopup *)this; |
| | return that->items.at(index)->color(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerPopup::exec() |
| | { |
| | show(); |
| |
|
| | QEventLoop e; |
| | eventLoop = &e; |
| | (void) e.exec(); |
| | eventLoop = nullptr; |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerPopup::updateSelected() |
| | { |
| | QLayoutItem *layoutItem; |
| | int i = 0; |
| | while ((layoutItem = grid->itemAt(i)) != nullptr) { |
| | QWidget *w = layoutItem->widget(); |
| | if (w && w->inherits("ColorPickerItem")) { |
| | ColorPickerItem *litem = reinterpret_cast<ColorPickerItem *>(layoutItem->widget()); |
| | if (litem != sender()) |
| | litem->setSelected(false); |
| | } |
| | ++i; |
| | } |
| |
|
| | if (sender() && sender()->inherits("ColorPickerItem")) { |
| | ColorPickerItem *item = (ColorPickerItem *)sender(); |
| | lastSel = item->color(); |
| | Q_EMIT selected(item->color()); |
| | } |
| |
|
| | hide(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerPopup::mouseReleaseEvent(QMouseEvent *e) |
| | { |
| | if (!rect().contains(e->pos())) |
| | hide(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | void ColorPickerPopup::keyPressEvent(QKeyEvent *e) |
| | { |
| | int curRow = 0; |
| | int curCol = 0; |
| |
|
| | bool foundFocus = false; |
| | for (int j = 0; !foundFocus && j < grid->rowCount(); ++j) { |
| | for (int i = 0; !foundFocus && i < grid->columnCount(); ++i) { |
| | if (widgetAt[j][i] && widgetAt[j][i]->hasFocus()) { |
| | curRow = j; |
| | curCol = i; |
| | foundFocus = true; |
| | break; |
| | } |
| | } |
| | } |
| |
|
| | switch (e->key()) { |
| | case Qt::Key_Left: |
| | if (curCol > 0) --curCol; |
| | else if (curRow > 0) { --curRow; curCol = grid->columnCount() - 1; } |
| | break; |
| | case Qt::Key_Right: |
| | if (curCol < grid->columnCount() - 1 && widgetAt[curRow][curCol + 1]) ++curCol; |
| | else if (curRow < grid->rowCount() - 1) { ++curRow; curCol = 0; } |
| | break; |
| | case Qt::Key_Up: |
| | if (curRow > 0) --curRow; |
| | else curCol = 0; |
| | break; |
| | case Qt::Key_Down: |
| | if (curRow < grid->rowCount() - 1) { |
| | QWidget *w = widgetAt[curRow + 1][curCol]; |
| | if (w) { |
| | ++curRow; |
| | } else for (int i = 1; i < grid->columnCount(); ++i) { |
| | if (!widgetAt[curRow + 1][i]) { |
| | curCol = i - 1; |
| | ++curRow; |
| | break; |
| | } |
| | } |
| | } |
| | break; |
| | case Qt::Key_Space: |
| | case Qt::Key_Return: |
| | case Qt::Key_Enter: { |
| | QWidget *w = widgetAt[curRow][curCol]; |
| | if (w && w->inherits("ColorPickerItem")) { |
| | ColorPickerItem *wi = reinterpret_cast<ColorPickerItem *>(w); |
| | wi->setSelected(true); |
| |
|
| | QLayoutItem *layoutItem; |
| | int i = 0; |
| | while ((layoutItem = grid->itemAt(i)) != nullptr) { |
| | QWidget *w = layoutItem->widget(); |
| | if (w && w->inherits("ColorPickerItem")) { |
| | ColorPickerItem *litem |
| | = reinterpret_cast<ColorPickerItem *>(layoutItem->widget()); |
| | if (litem != wi) |
| | litem->setSelected(false); |
| | } |
| | ++i; |
| | } |
| |
|
| | lastSel = wi->color(); |
| | Q_EMIT selected(wi->color()); |
| | hide(); |
| | } else if (w && w->inherits("QPushButton")) { |
| | ColorPickerItem *wi = reinterpret_cast<ColorPickerItem *>(w); |
| | wi->setSelected(true); |
| |
|
| | QLayoutItem *layoutItem; |
| | int i = 0; |
| | while ((layoutItem = grid->itemAt(i)) != nullptr) { |
| | QWidget *w = layoutItem->widget(); |
| | if (w && w->inherits("ColorPickerItem")) { |
| | ColorPickerItem *litem |
| | = reinterpret_cast<ColorPickerItem *>(layoutItem->widget()); |
| | if (litem != wi) |
| | litem->setSelected(false); |
| | } |
| | ++i; |
| | } |
| |
|
| | lastSel = wi->color(); |
| | Q_EMIT selected(wi->color()); |
| | hide(); |
| | } |
| | } |
| | break; |
| | case Qt::Key_Escape: |
| | hide(); |
| | break; |
| | default: |
| | e->ignore(); |
| | break; |
| | } |
| |
|
| | widgetAt[curRow][curCol]->setFocus(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerPopup::hideEvent(QHideEvent *e) |
| | { |
| | if (eventLoop) { |
| | eventLoop->exit(); |
| | } |
| |
|
| | setFocus(); |
| |
|
| | Q_EMIT hid(); |
| | QFrame::hideEvent(e); |
| | } |
| |
|
| | |
| | |
| | |
| | QColor ColorPickerPopup::lastSelected() const |
| | { |
| | return lastSel; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void ColorPickerPopup::showEvent(QShowEvent *) |
| | { |
| | bool foundSelected = false; |
| | for (int i = 0; i < grid->columnCount(); ++i) { |
| | for (int j = 0; j < grid->rowCount(); ++j) { |
| | QWidget* w = widgetAt[j][i]; |
| | if (w && w->inherits("ColorPickerItem")) { |
| | if (((ColorPickerItem*)w)->isSelected()) { |
| | w->setFocus(); |
| | foundSelected = true; |
| | break; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | if (!foundSelected) { |
| | if (items.isEmpty()) |
| | setFocus(); |
| | else |
| | widgetAt[0][0]->setFocus(); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerPopup::regenerateGrid() |
| | { |
| | widgetAt.clear(); |
| |
|
| | int columns = cols; |
| | if (columns == -1) |
| | columns = (int) ceil(sqrt((float) items.count())); |
| |
|
| | |
| | |
| | |
| | if (grid) delete grid; |
| | grid = new QGridLayout(this); |
| | grid->setContentsMargins(1, 1, 1, 1); |
| | grid->setSpacing(0); |
| |
|
| | int ccol = 0, crow = 0; |
| | for (int i = 0; i < items.size(); ++i) { |
| | if (items.at(i)) { |
| | widgetAt[crow][ccol] = items.at(i); |
| | grid->addWidget(items.at(i), crow, ccol++); |
| | if (ccol == columns) { |
| | ++crow; |
| | ccol = 0; |
| | } |
| | } |
| | } |
| |
|
| | if (moreButton) { |
| | grid->addWidget(moreButton, crow, ccol); |
| | widgetAt[crow][ccol] = moreButton; |
| | } |
| | updateGeometry(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void ColorPickerPopup::getColorFromDialog() |
| | { |
| | |
| | |
| | QColor col; |
| | if (Gui::DialogOptions::dontUseNativeColorDialog()){ |
| | col = QColorDialog::getColor(lastSel, parentWidget(), QString(), |
| | QColorDialog::ShowAlphaChannel | QColorDialog::DontUseNativeDialog); |
| | } else { |
| | col = QColorDialog::getColor(lastSel, parentWidget(), QString(), |
| | QColorDialog::ShowAlphaChannel); |
| | } |
| | if (!col.isValid()) |
| | return; |
| |
|
| | |
| | insertColor(col, tr("Custom Color"), -1); |
| | lastSel = col; |
| | Q_EMIT selected(col); |
| | } |
| |
|
| | void ColorPickerPopup::setLastSel(const QColor & col) { lastSel = col; } |
| |
|
| | |
| | |
| | |
| | |
| | ColorPickerItem::ColorPickerItem(const QColor &color, const QString &text, |
| | QWidget *parent) |
| | : QFrame(parent), c(color), t(text), sel(false) |
| | { |
| | setToolTip(t); |
| | setFixedWidth(24); |
| | setFixedHeight(21); |
| | } |
| |
|
| | |
| | |
| | |
| | ColorPickerItem::~ColorPickerItem() |
| | { |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | QColor ColorPickerItem::color() const |
| | { |
| | return c; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | QString ColorPickerItem::text() const |
| | { |
| | return t; |
| | } |
| |
|
| | |
| | |
| | |
| | bool ColorPickerItem::isSelected() const |
| | { |
| | return sel; |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::setSelected(bool selected) |
| | { |
| | sel = selected; |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::setColor(const QColor &color, const QString &text) |
| | { |
| | c = color; |
| | t = text; |
| | setToolTip(t); |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::mouseMoveEvent(QMouseEvent *) |
| | { |
| | setFocus(); |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::mouseReleaseEvent(QMouseEvent *) |
| | { |
| | sel = true; |
| | Q_EMIT selected(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::mousePressEvent(QMouseEvent *) |
| | { |
| | setFocus(); |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerItem::paintEvent(QPaintEvent *) |
| | { |
| | QPainter p(this); |
| | int w = width(); |
| | int h = height(); |
| |
|
| | p.setPen( QPen( Qt::gray, 0, Qt::SolidLine ) ); |
| |
|
| | if (sel) |
| | p.drawRect(1, 1, w - 3, h - 3); |
| |
|
| | p.setPen( QPen( Qt::black, 0, Qt::SolidLine ) ); |
| | p.drawRect(3, 3, w - 7, h - 7); |
| | p.fillRect(QRect(4, 4, w - 8, h - 8), QBrush(c)); |
| |
|
| | if (hasFocus()) |
| | p.drawRect(0, 0, w - 1, h - 1); |
| | } |
| |
|
| | |
| | |
| | |
| | ColorPickerButton::ColorPickerButton(QWidget *parent) |
| | : QFrame(parent) |
| | { |
| | setFrameStyle(StyledPanel); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::mousePressEvent(QMouseEvent *) |
| | { |
| | setFrameShadow(Sunken); |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::mouseMoveEvent(QMouseEvent *) |
| | { |
| | setFocus(); |
| | update(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::mouseReleaseEvent(QMouseEvent *) |
| | { |
| | setFrameShadow(Raised); |
| | repaint(); |
| | Q_EMIT clicked(); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::keyPressEvent(QKeyEvent *e) |
| | { |
| | if (e->key() == Qt::Key_Up |
| | || e->key() == Qt::Key_Down |
| | || e->key() == Qt::Key_Left |
| | || e->key() == Qt::Key_Right) { |
| | qApp->sendEvent(parent(), e); |
| | } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Space || e->key() == Qt::Key_Return) { |
| | setFrameShadow(Sunken); |
| | update(); |
| | } else { |
| | QFrame::keyPressEvent(e); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::keyReleaseEvent(QKeyEvent *e) |
| | { |
| | if (e->key() == Qt::Key_Up |
| | || e->key() == Qt::Key_Down |
| | || e->key() == Qt::Key_Left |
| | || e->key() == Qt::Key_Right) { |
| | qApp->sendEvent(parent(), e); |
| | } else if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Space || e->key() == Qt::Key_Return) { |
| | setFrameShadow(Raised); |
| | repaint(); |
| | Q_EMIT clicked(); |
| | } else { |
| | QFrame::keyReleaseEvent(e); |
| | } |
| |
|
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::focusInEvent(QFocusEvent *e) |
| | { |
| | setFrameShadow(Raised); |
| | update(); |
| | QFrame::focusOutEvent(e); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::focusOutEvent(QFocusEvent *e) |
| | { |
| | setFrameShadow(Raised); |
| | update(); |
| | QFrame::focusOutEvent(e); |
| | } |
| |
|
| | |
| | |
| | |
| | void ColorPickerButton::paintEvent(QPaintEvent *e) |
| | { |
| | QFrame::paintEvent(e); |
| |
|
| | QPainter p(this); |
| | p.fillRect(contentsRect(), palette().button()); |
| |
|
| | QRect r = rect(); |
| |
|
| | int offset = frameShadow() == Sunken ? 1 : 0; |
| |
|
| | QPen pen(palette().buttonText(), 1); |
| | p.setPen(pen); |
| |
|
| | p.drawRect(r.center().x() + offset - 4, r.center().y() + offset, 1, 1); |
| | p.drawRect(r.center().x() + offset , r.center().y() + offset, 1, 1); |
| | p.drawRect(r.center().x() + offset + 4, r.center().y() + offset, 1, 1); |
| | if (hasFocus()) { |
| | p.setPen( QPen( Qt::black, 0, Qt::SolidLine ) ); |
| | p.drawRect(0, 0, width() - 1, height() - 1); |
| | } |
| |
|
| | p.end(); |
| |
|
| | } |
| | |
| | #include "moc_qtcolorpicker.cpp" |
| | #include <moc_qtcolorpicker-internal.cpp> |
| |
|