

Public Slots | |
| void | getColorFromDialog () |
Signals | |
| void | selected (const QColor &) |
| void | hid () |
Public Member Functions | |
| ColorPickerPopup (int width, bool withColorDialog, QWidget *parent=0) | |
| ~ColorPickerPopup () | |
| 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 |
Protected Slots | |
| void | updateSelected () |
Protected Member Functions | |
| void | keyPressEvent (QKeyEvent *e) |
| void | showEvent (QShowEvent *e) |
| void | hideEvent (QHideEvent *e) |
| void | mouseReleaseEvent (QMouseEvent *e) |
| void | regenerateGrid () |
| ColorPickerPopup::ColorPickerPopup | ( | int | width, | |
| bool | withColorDialog, | |||
| QWidget * | parent = 0 | |||
| ) |
: 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, SIGNAL(clicked()), SLOT(getColorFromDialog())); } else { moreButton = 0; } eventLoop = 0; grid = 0; regenerateGrid(); }
| ColorPickerPopup::~ColorPickerPopup | ( | ) |
{
if (eventLoop)
eventLoop->exit();
}
| QColor ColorPickerPopup::color | ( | int | index | ) | const |
Referenced by QtColorPicker::color().
{
if (index < 0 || index > (int) items.count() - 1)
return QColor();
ColorPickerPopup *that = (ColorPickerPopup *)this;
return that->items.at(index)->color();
}
| void ColorPickerPopup::exec | ( | ) |
Referenced by QtColorPicker::getColor().
{
show();
QEventLoop e;
eventLoop = &e;
(void) e.exec();
eventLoop = 0;
}
| ColorPickerItem * ColorPickerPopup::find | ( | const QColor & | col | ) | const |
Referenced by insertColor(), and QtColorPicker::setCurrentColor().
| void ColorPickerPopup::getColorFromDialog | ( | ) | [slot] |
Referenced by ColorPickerPopup().
{
bool ok;
QRgb rgb = QColorDialog::getRgba(lastSel.rgba(), &ok, parentWidget());
if (!ok)
return;
QColor col = QColor::fromRgba(rgb);
insertColor(col, tr("Custom"), -1);
lastSel = col;
emit selected(col);
}
| void ColorPickerPopup::hid | ( | ) | [signal] |
Referenced by hideEvent().
| void ColorPickerPopup::hideEvent | ( | QHideEvent * | e | ) | [protected] |
{
if (eventLoop) {
eventLoop->exit();
}
setFocus();
emit hid();
QFrame::hideEvent(e);
}
Referenced by QtColorPicker::getColor(), getColorFromDialog(), and QtColorPicker::insertColor().
{
// Don't add colors that we have already.
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, SIGNAL(selected()), SLOT(updateSelected()));
if (index == -1)
index = items.count();
items.insert((unsigned int)index, item);
regenerateGrid();
update();
}
| void ColorPickerPopup::keyPressEvent | ( | QKeyEvent * | e | ) | [protected] |
{
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)) != 0) {
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();
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)) != 0) {
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();
emit selected(wi->color());
hide();
}
}
break;
case Qt::Key_Escape:
hide();
break;
default:
e->ignore();
break;
}
widgetAt[curRow][curCol]->setFocus();
}
| QColor ColorPickerPopup::lastSelected | ( | ) | const |
Referenced by QtColorPicker::getColor(), and insertColor().
{
return lastSel;
}
| void ColorPickerPopup::mouseReleaseEvent | ( | QMouseEvent * | e | ) | [protected] |
{
if (!rect().contains(e->pos()))
hide();
}
| void ColorPickerPopup::regenerateGrid | ( | ) | [protected] |
Referenced by ColorPickerPopup(), and insertColor().
{
widgetAt.clear();
int columns = cols;
if (columns == -1)
columns = (int) ceil(sqrt((float) items.count()));
// When the number of columns grows, the number of rows will
// fall. There's no way to shrink a grid, so we create a new
// one.
if (grid) delete grid;
grid = new QGridLayout(this);
grid->setMargin(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::selected | ( | const QColor & | ) | [signal] |
Referenced by getColorFromDialog(), insertColor(), keyPressEvent(), and updateSelected().
| void ColorPickerPopup::setExecFlag | ( | ) |
| void ColorPickerPopup::showEvent | ( | QShowEvent * | e | ) | [protected] |
{
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.count() == 0)
setFocus();
else
widgetAt[0][0]->setFocus();
}
}
| void ColorPickerPopup::updateSelected | ( | ) | [protected, slot] |
Referenced by insertColor().
{
QLayoutItem *layoutItem;
int i = 0;
while ((layoutItem = grid->itemAt(i)) != 0) {
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();
emit selected(item->color());
}
hide();
}
1.7.1