Results 1 to 8 of 8

Thread: DragDrop Color

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default DragDrop Color

    I am creating a custom color picker by sub classing QFrame. There are three main functionality I would be needing.

    1. A color picker
    2. dragdrop color to other picker
    3. right click menu

    There won't be any problem in 1 and 3. How ever for 2 there are:

    When you drop color to target, a menu appears with two options: swap & replace
    If user select "swap", it should swap the colors of source and target
    If user select "replace", it should replace the color of target with source.

    drag & drop is having some problem:
    Qt Code:
    1. void DragWidget::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (event->mimeData()->hasColor()) {
    4. if (event->source() == this) {
    5. event->setDropAction(Qt::MoveAction);
    6. event->accept();
    7. }
    8. else {
    9. event->acceptProposedAction();
    10. }
    11. }
    12. else {
    13. event->ignore();
    14. }
    15. }
    16.  
    17. void DragWidget::dragMoveEvent(QDragMoveEvent *event)
    18. {
    19. if (event->mimeData()->hasColor()) {
    20. if (event->source() == this) {
    21. event->setDropAction(Qt::MoveAction);
    22. event->accept();
    23. }
    24. else {
    25. event->acceptProposedAction();
    26. }
    27. }
    28. else {
    29. event->ignore();
    30. }
    31.  
    32. }
    33.  
    34. void DragWidget::dropEvent(QDropEvent *event)
    35. {
    36. if (event->mimeData()->hasColor()) {
    37. myColor = qvariant_cast<QColor>(event->mimeData()->colorData());
    38.  
    39. if (event->source() == this) {
    40. event->setDropAction(Qt::MoveAction);
    41. event->accept();
    42. } else {
    43. event->acceptProposedAction();
    44. }
    45. } else {
    46. event->ignore();
    47. }
    48. }
    49.  
    50.  
    51. void DragWidget::mousePressEvent(QMouseEvent *event)
    52. {
    53. QMimeData *mimeData = new QMimeData;
    54. mimeData->setColorData(QVariant(myColor));
    55.  
    56. QDrag *drag = new QDrag(this);
    57. drag->setMimeData(mimeData);
    58. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DragDrop Color

    What problem is the drag & drop having?

    http://catb.org/~esr/faqs/smart-questions.html
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: DragDrop Color

    Nothing is happening.
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DragDrop Color

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: DragDrop Color

    Got it.....

    in "mousePressEvent" I have to add this:
    Qt Code:
    1. Qt::DropAction dropAction = drag->exec(Qt::CopyAction,Qt::CopyAction);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DragDrop Color

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: DragDrop Color

    It seems there is something wrong here and it's creating another problem.

    Qt Code:
    1. void DragWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3.  
    4. // Pack mime data
    5. QMimeData *mimeData = new QMimeData;
    6. mimeData->setColorData(QVariant(myColor));
    7.  
    8. // Prepare cursor image
    9. QPixmap pixmap(this->size());
    10. QPainter painter;
    11. painter.begin(&pixmap);
    12. painter.fillRect(pixmap.rect(), myColor);
    13. painter.end();
    14.  
    15. // We establish QDrag object, draw cursor and embed data
    16. QDrag *drag = new QDrag(this);
    17. drag->setMimeData(mimeData);
    18. drag->setPixmap(pixmap);
    19. drag->setHotSpot(event->pos());
    20.  
    21. sourcePicker = drag->source();
    22.  
    23. // We send it
    24. drag->exec(Qt::CopyAction);
    25.  
    26.  
    27. }
    28.  
    29. void DragWidget::mouseReleaseEvent(QMouseEvent *event)
    30. {
    31.  
    32. if (event->type() == QEvent::MouseButtonRelease){
    33. QColor color = QColorDialog::getColor(QColor(myColor), this);
    34. if (color.isValid()) {
    35. setPalette(QPalette(color));
    36. setAutoFillBackground(true);
    37. myColor = color;
    38. }
    39. }
    40. else
    41. event->ignore();
    42. }
    To copy to clipboard, switch view to plain text mode 

    With this line in "mousePressEvent":
    Qt Code:
    1. drag->exec(Qt::CopyAction);
    To copy to clipboard, switch view to plain text mode 
    drag n drop is working fine but "mouseReleaseEvent" is not getting called. If I comment this
    line "releaseEvent" is getting called.

    Secondly, I have to add the functionality to make this widget as color picker also, I added color picking dialog in "releaseEvent". This won't work as expected because the "dropping"
    event is actually a "releaseEvent" and as you drop on target picker dialog shows up.

    A hack to this problem is to show color dialog when there is "down" and "up" on same widget. Using a bool var, you set state true, when there is a mouse down and in mouse up
    event, check the state of this var, if it's true, show color dialog and set it again to false.
    Am I right here or there is a better way of doing this?

    How do I setup a menu, which appears at dropping? "swap color" or "replace color"

    Prashant

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: DragDrop Color

    What's the point of checking the event type in mouseReleaseEvent()?

    Drop event is a drop event and not mouse release event. Here is an extract from my QwwTwoColorIndicator:
    Qt Code:
    1. void QwwTwoColorIndicator::mouseMoveEvent(QMouseEvent *ev) {
    2. Q_D(QwwTwoColorIndicator);
    3. if (d->fgP || d->bgP) {
    4. if (dragEnabled() && (ev->pos() - d->pressPos).manhattanLength() >= QApplication::startDragDistance()) {
    5. QColor col = d->fgP ? fgColor() : bgColor();
    6. ColorDrag *drag = new ColorDrag(this, col, col.name());
    7. drag->exec();
    8. d->fgP = d->bgP = false;
    9. update();
    10. }
    11. } else if (testAttribute(Qt::WA_Hover)) update();
    12. QWidget::mouseMoveEvent(ev);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void QwwTwoColorIndicator::mouseReleaseEvent(QMouseEvent *ev) {
    2. Q_D(QwwTwoColorIndicator);
    3. if (ev->button() == Qt::LeftButton) {
    4. if (d->fgP && d->foregroundRect().contains(ev->pos())) {
    5. emit fgClicked();
    6. if (d->active) {
    7. #if QT_VERSION >= 0x040500
    8. QColor c = QColorDialog::getColor(d->fg, this, tr("Choose foreground color"), QColorDialog::ShowAlphaChannel);
    9. #else
    10. QColor c = QColorDialog::getColor(d->fg, this);
    11. #endif
    12. if (c.isValid()) {
    13. setFgColor(c);
    14. }
    15. }
    16. } else if (d->bgP && d->backgroundRect().contains(ev->pos())) {
    17. emit bgClicked();
    18. if (d->active) {
    19. #if QT_VERSION >= 0x040500
    20. QColor c = QColorDialog::getColor(d->bg, this, tr("Choose background color"), QColorDialog::ShowAlphaChannel);
    21. #else
    22. QColor c = QColorDialog::getColor(d->bg, this);
    23. #endif
    24. if (c.isValid()) {
    25. setBgColor(c);
    26. }
    27. }
    28. }
    29. d->fgP = d->bgP = false;
    30. update();
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void QwwTwoColorIndicator::dropEvent(QDropEvent *ev) {
    2. Q_D(QwwTwoColorIndicator);
    3. QColor col;
    4. if (ev->mimeData()->hasColor())
    5. col = qvariant_cast<QColor>(ev->mimeData()->colorData());
    6. else
    7. col.setNamedColor(ev->mimeData()->text());
    8. if (d->foregroundRect().contains(ev->pos())) {
    9. setFgColor(col);
    10. } else if (d->backgroundRect().contains(ev->pos())) {
    11. setBgColor(col);
    12. }
    13. ev->setDropAction(Qt::CopyAction);
    14. }
    To copy to clipboard, switch view to plain text mode 
    There is also dragEnterEvent and dragMoveEvent implemented.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 24th June 2024, 12:09
  2. Using a QFrame as a color selection indicator
    By KShots in forum Qt Tools
    Replies: 8
    Last Post: 14th June 2011, 23:55
  3. QTableWidget, grid color bug?
    By greenvirag in forum Qt Programming
    Replies: 6
    Last Post: 18th June 2009, 10:38
  4. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25
  5. Replies: 1
    Last Post: 1st February 2007, 14:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.