26 #include <QMouseEvent> 28 #include <QApplication> 32 #include <QDragEnterEvent> 33 #include <QStyleOption> 68 drop_overwrite(false),
77 int count = palette.
count();
82 return QSize(std::ceil(
float(count) / forced_rows ), forced_rows);
84 int columns = palette.
columns();
88 else if ( columns == 0 )
89 columns = qMin(palette.
count(), owner->width() / color_size.width());
91 int rows = std::ceil(
float(count) / columns );
93 return QSize(columns, rows);
102 drop_index = owner->
indexAt(event->pos());
103 if ( drop_index == -1 )
104 drop_index = palette.
count();
107 if ( event->mimeData()->hasColor() )
109 drop_color =
event->mimeData()->colorData().value<QColor>();
110 drop_color.setAlpha(255);
112 else if ( event->mimeData()->hasText() )
114 drop_color = QColor(event->mimeData()->text());
117 drop_overwrite =
false;
118 QRectF drop_rect =
indexRect(drop_index);
119 if ( drop_index < palette.
count() && drop_rect.isValid() )
122 if ( palette.
columns() == 1 || forced_columns == 1 )
125 if ( event->posF().y() >= drop_rect.top() + drop_rect.height() * 3.0 / 4 )
128 else if ( event->posF().x() > drop_rect.top() + drop_rect.height() / 4 &&
129 (
event->dropAction() != Qt::MoveAction ||
event->source() !=
owner ) )
130 drop_overwrite =
true;
135 if ( event->posF().x() >= drop_rect.left() + drop_rect.width() * 3.0 / 4 )
138 else if ( event->posF().x() > drop_rect.left() + drop_rect.width() / 4 &&
139 (
event->dropAction() != Qt::MoveAction ||
event->source() !=
owner ) )
140 drop_overwrite =
true;
153 drop_color = QColor();
154 drop_overwrite =
false;
165 if ( !rowcols.isValid() )
176 return QSizeF (
float(owner->width()) / rowcols.width(),
177 float(owner->height()) / rowcols.height());
192 index % rowcols.width() * color_size.width(),
193 index / rowcols.width() * color_size.height(),
204 if ( index == -1 || !rc.isValid() )
211 : QWidget(parent),
p(new
Private(this))
217 if ( index == p->selected )
218 emit colorSelected( p->palette.colorAt(index) );
221 if ( index == p->selected )
224 setFocusPolicy(Qt::StrongFocus);
225 setAcceptDrops(
true);
226 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
227 setAttribute(Qt::WA_Hover,
true);
239 if ( !
p->
color_size.isValid() || !rowcols.isValid() )
278 if ( rowcols.isEmpty() )
284 qBound<int>(0, pt.x() / color_size.width(), rowcols.width() - 1),
285 qBound<int>(0, pt.y() / color_size.height(), rowcols.height() - 1)
288 int index = point.y() * rowcols.width() + point.x();
315 if ( selected != -1 )
329 if ( rowcols.isEmpty() )
333 QPainter painter(
this);
335 QStyleOptionFrame panel;
336 panel.initFrom(
this);
338 panel.midLineWidth = 0;
339 panel.state |= QStyle::State_Sunken;
340 style()->drawPrimitive(QStyle::PE_Frame, &panel, &painter,
this);
341 QRect r = style()->subElementRect(QStyle::SE_FrameContents, &panel,
this);
342 painter.setClipRect(r);
346 for (
int y = 0,
i = 0;
i < count;
y++ )
348 for (
int x = 0;
x < rowcols.width() &&
i < count;
x++,
i++ )
351 painter.drawRect(
p->
indexRect(
i, rowcols, color_size));
355 painter.setClipping(
false);
363 painter.setPen(QPen(Qt::gray));
364 painter.drawRect(drop_area);
366 else if ( rowcols.width() == 1 )
370 painter.setBrush(Qt::transparent);
371 painter.drawLine(drop_area.topLeft(), drop_area.topRight());
376 painter.setBrush(Qt::transparent);
377 painter.drawLine(drop_area.topLeft(), drop_area.bottomLeft());
382 drop_area.translate(color_size.width(), 0);
383 painter.drawLine(drop_area.topLeft(), drop_area.bottomLeft());
391 painter.setBrush(Qt::transparent);
392 painter.setPen(QPen(Qt::darkGray, 2));
393 painter.drawRect(rect);
394 painter.setPen(QPen(Qt::gray, 2, Qt::DotLine));
395 painter.drawRect(rect);
402 QWidget::keyPressEvent(event);
407 int columns = rowcols.width();
408 int rows = rowcols.height();
409 switch ( event->key() )
412 QWidget::keyPressEvent(event);
416 if ( selected == -1 )
417 selected = count - 1;
418 else if ( selected > 0 )
423 if ( selected == -1 )
425 else if ( selected < count - 1 )
430 if ( selected == -1 )
431 selected = count - 1;
432 else if ( selected >= columns )
437 if ( selected == -1 )
439 else if ( selected < count - columns )
444 if ( event->modifiers() & Qt::ControlModifier )
447 selected -= selected % columns;
451 if ( event->modifiers() & Qt::ControlModifier )
452 selected = count - 1;
454 selected += columns - (selected % columns) - 1;
461 case Qt::Key_Backspace:
468 selected = qMax(selected - 1, 0);
473 if ( selected == -1 )
476 selected = selected % columns;
478 case Qt::Key_PageDown:
479 if ( selected == -1 )
481 selected = count - 1;
485 selected = columns * (rows-1) + selected % columns;
486 if ( selected >= count )
506 if ( event->button() == Qt::LeftButton )
512 else if ( event->button() == Qt::RightButton )
514 int index =
indexAt(event->pos());
522 if (
p->
drag_index != -1 && (event->buttons() & Qt::LeftButton) &&
523 (
p->
drag_pos - event->pos()).manhattanLength() >= QApplication::startDragDistance() )
527 QPixmap preview(24,24);
530 QMimeData *mimedata =
new QMimeData;
531 mimedata->setColorData(color);
534 QDrag *drag =
new QDrag(
this);
535 drag->setMimeData(mimedata);
536 drag->setPixmap(preview);
537 Qt::DropActions actions = Qt::CopyAction;
539 actions |= Qt::MoveAction;
546 if ( event->button() == Qt::LeftButton )
554 if ( event->button() == Qt::LeftButton )
556 int index =
indexAt(event->pos());
564 if ( event->delta() > 0 )
582 if ( event->proposedAction() == Qt::MoveAction &&
event->source() == this )
583 event->setDropAction(Qt::MoveAction);
585 event->setDropAction(Qt::CopyAction);
611 if ( event->mimeData()->hasColor() &&
event->mimeData()->hasText() )
612 name = event->mimeData()->text();
621 if ( event->dropAction() == Qt::MoveAction &&
event->source() == this )
686 setFixedSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
704 if ( forcedColumns <= 0 )
716 if ( forcedRows <= 0 )
742 if(event->type() == QEvent::ToolTip)
744 QHelpEvent* help_ev =
static_cast<QHelpEvent*
>(
event);
745 int index =
indexAt(help_ev->pos());
750 QString message = color.name();
751 if ( !name.isEmpty() )
752 message = tr(
"%1 (%2)").arg(name).arg(message);
753 message =
"<tt style='background-color:"+color.name()+
";color:"+color.name()+
";'>MM</tt> "+message.toHtmlEscaped();
754 QToolTip::showText(help_ev->globalPos(), message,
this,
760 QToolTip::hideText();
766 return QWidget::event(event);
TFSIMD_FORCE_INLINE const tfScalar & y() const
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
TFSIMD_FORCE_INLINE const tfScalar & x() const