Public Slots | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Properties

QtColorPicker Class Reference

The QtColorPicker class provides a widget for selecting colors from a popup color grid. More...

#include <qtcolorpicker.h>

Inheritance diagram for QtColorPicker:
Inheritance graph
[legend]
Collaboration diagram for QtColorPicker:
Collaboration graph
[legend]

List of all members.

Public Slots

void setCurrentColor (const QColor &col)

Signals

void colorChanged (const QColor &)

Public Member Functions

 QtColorPicker (QWidget *parent=0, int columns=-1, bool enableColorDialog=true)
 ~QtColorPicker ()
void insertColor (const QColor &color, const QString &text=QString::null, int index=-1)
QColor currentColor () const
QColor color (int index) const
void setColorDialogEnabled (bool enabled)
bool colorDialogEnabled () const
void setStandardColors ()

Static Public Member Functions

static QColor getColor (const QPoint &pos, bool allowCustomColors=true)

Protected Member Functions

void paintEvent (QPaintEvent *e)

Properties

bool colorDialog
 Whether the ellipsis "..." (more) button is available.

Detailed Description

The QtColorPicker class provides a widget for selecting colors from a popup color grid.

Users can invoke the color picker by clicking on it, or by navigating to it and pressing Space. They can use the mouse or arrow keys to navigate between colors on the grid, and select a color by clicking or by pressing Enter or Space. The colorChanged() signal is emitted whenever the color picker's color changes.

The widget also supports negative selection: Users can click and hold the mouse button on the QtColorPicker widget, then move the mouse over the color grid and release the mouse button over the color they wish to select.

The color grid shows a customized selection of colors. An optional ellipsis "..." button (signifying "more") can be added at the bottom of the grid; if the user presses this, a QColorDialog pops up and lets them choose any color they like. This button is made available by using setColorDialogEnabled().

When a color is selected, the QtColorPicker widget shows the color and its name. If the name cannot be determined, the translatable name "Custom" is used.

The QtColorPicker object is optionally initialized with the number of columns in the color grid. Colors are then added left to right, top to bottom using insertColor(). If the number of columns is not set, QtColorPicker calculates the number of columns and rows that will make the grid as square as possible.

    DrawWidget::DrawWidget(QWidget *parent, const char *name)
    {
        QtColorPicker *picker = new QtColorPicker(this);
        picker->insertColor(red, "Red"));
        picker->insertColor(QColor("green"), "Green"));
        picker->insertColor(QColor(0, 0, 255), "Blue"));
        picker->insertColor(white);

        connect(colors, SIGNAL(colorChanged(const QColor &)), SLOT(setCurrentColor(const QColor &)));
    }

An alternative to adding colors manually is to initialize the grid with QColorDialog's standard colors using setStandardColors().

QtColorPicker also provides a the static function getColor(), which pops up the grid of standard colors at any given point.

colorpicker1.png colorpicker2.png

See also:
QColorDialog

Constructor & Destructor Documentation

QtColorPicker::QtColorPicker ( QWidget parent = 0,
int  cols = -1,
bool  enableColorDialog = true 
)

Constructs a QtColorPicker widget. The popup will display a grid with cols columns, or if cols is -1, the number of columns will be calculated automatically.

If enableColorDialog is true, the popup will also have a "More" button (signified by an ellipsis "...") that presents a QColorDialog when clicked.

After constructing a QtColorPicker, call insertColor() to add individual colors to the popup grid, or call setStandardColors() to add all the standard colors in one go.

The parent argument is passed to QFrame's constructor.

See also:
QFrame

    : QPushButton(parent), popup(0), withColorDialog(enableColorDialog)
{
    setFocusPolicy(Qt::StrongFocus);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    setAutoDefault(false);
    setAutoFillBackground(true);
    setCheckable(true);

    // Set text
    setToolTip(tr("Black"));
    firstInserted = false;

    // Create and set icon
    col = Qt::black;
    dirty = true;

    // Create color grid popup and connect to it.
    popup = new ColorPickerPopup(cols, withColorDialog, this);
    connect(popup, SIGNAL(selected(const QColor &)),
            SLOT(setCurrentColor(const QColor &)));
    connect(popup, SIGNAL(hid()), SLOT(popupClosed()));

    // Connect this push button's pressed() signal.
    connect(this, SIGNAL(toggled(bool)), SLOT(buttonPressed(bool)));
}

QtColorPicker::~QtColorPicker (  ) 

Destructs the QtColorPicker.

{
}


Member Function Documentation

QColor QtColorPicker::color ( int  index  )  const

Returns the color at position index.

Referenced by paintEvent(), and LegendItem::setCurrentAxis().

{
    return popup->color(index);
}

QtColorPicker::colorChanged ( const QColor color  )  [signal]

This signal is emitted when the QtColorPicker's color is changed. color is the new color.

To obtain the color's name, use text().

Referenced by setCurrentColor().

bool QtColorPicker::colorDialogEnabled (  )  const

{
    return withColorDialog;
}

QColor QtColorPicker::currentColor (  )  const

Returns the currently selected color.

See also:
text()

Referenced by LegendItem::setCurrentColor().

{
    return col;
}

QColor QtColorPicker::getColor ( const QPoint point,
bool  allowCustomColors = true 
) [static]

Pops up a color grid with Qt default colors at point, using global coordinates. If allowCustomColors is true, there will also be a button on the popup that invokes QColorDialog.

For example:

        void Drawer::mouseReleaseEvent(QMouseEvent *e)
        {
            if (e->button() & RightButton) {
                QColor color = QtColorPicker::getColor(mapToGlobal(e->pos()));
            }
        }

{
    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();
}

void QtColorPicker::insertColor ( const QColor color,
const QString text = QString::null,
int  index = -1 
)

Adds the color color with the name text to the color grid, at position index. If index is -1, the color is assigned automatically assigned a position, starting from left to right, top to bottom.

Referenced by setCurrentColor(), and setStandardColors().

{
    popup->insertColor(color, text, index);
    if (!firstInserted) {
        col = color;
        setToolTip(text);
        firstInserted = true;
    }
}

void QtColorPicker::paintEvent ( QPaintEvent e  )  [protected]

Reimplemented from QPushButton.

{
    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();                    // width of cell in pixels
        int h = pix.height();                   // height of cell in pixels
        p.setPen(QPen(Qt::gray));
        p.setBrush(col);
        p.drawRect(0, 0, w, h);
        setIcon(QIcon(pix));

        dirty = false;
    }
    QPushButton::paintEvent(e);
}

void QtColorPicker::setColorDialogEnabled ( bool  enabled  ) 

{
    withColorDialog = enabled;
}

void QtColorPicker::setCurrentColor ( const QColor color  )  [slot]

Makes color current. If color is not already in the color grid, it is inserted with the text "Custom".

This function emits the colorChanged() signal if the new color is valid, and different from the old one.

Referenced by LegendItem::color(), and QtColorPicker().

{
    if (col == color || !color.isValid())
        return;

    ColorPickerItem *item = popup->find(color);
    if (!item) {
        insertColor(color, tr("Custom"));
        item = popup->find(color);
    }

    col = color;
    setToolTip(item->text());

    dirty = true;

    popup->hide();
    repaint();

    item->setSelected(true);
    emit colorChanged(color);
}

void QtColorPicker::setStandardColors (  ) 

Adds the 17 predefined colors from the Qt namespace.

(The names given to the colors, "Black", "White", "Red", etc., are all translatable.)

See also:
insertColor()

{
    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"));
}


Property Documentation

QtColorPicker::colorDialog [read, write]

Whether the ellipsis "..." (more) button is available.

If this property is set to TRUE, the color grid popup will include a "More" button (signified by an ellipsis, "...") which pops up a QColorDialog when clicked. The user will then be able to select any custom color they like.


The documentation for this class was generated from the following files: