Qt-UI

A Painter Application

Drag and Dropping URLs | | QGL

PainterAPP
#include <iostream>

#include "painter.h"

const double MyQPainterWidget::fps=30.0; // animated frames per second

MyQPainterWidget::MyQPainterWidget(QWidget *parent)
   : QWidget(parent)
{
   // init background
   pix=NULL;

   // init brush parameters
   brushSize=50;
   mousePos=QPoint(-brushSize,-brushSize);

   // accept drag and drop
   setAcceptDrops(true);

   // start timer for periodic repainting
   startTimer((int)(1000.0/fps)); // ms=1000/fps
}

MyQPainterWidget::~MyQPainterWidget()
{
   if (pix!=NULL)
      delete pix;
}

QSize MyQPainterWidget::minimumSizeHint() const
{
   return(QSize(100, 100));
}

QSize MyQPainterWidget::sizeHint() const
{
   return(QSize(512, 512));
}

void MyQPainterWidget::setBackground(QPixmap *p)
   {
   if (pix!=NULL)
      delete pix;

   pix=p;

   *pix=pix->scaled(size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);

   repaint();
   }

void MyQPainterWidget::paint(QPoint pos, int size, QColor color, double opacity)
{
   QPainter painter(pix);

   painter.setRenderHint(QPainter::Antialiasing);

   // paint into background pixmap
   painter.setPen(QColor(0,0,0,0)); // border color
   painter.setBrush(color); // fill color
   painter.setOpacity(opacity); // filling opacity
   painter.drawEllipse(pos, size, size);
}

void MyQPainterWidget::brush()
{
   if (buttonDown)
   {
      paint(mousePos, brushSize, QColor(255,0,0), 0.25);
      repaint();
   }
}

void MyQPainterWidget::paintEvent(QPaintEvent *)
{
   // create paintable pixmap as window background
   if (pix==NULL)
   {
      pix=new QPixmap(size());
      pix->fill(Qt::white);
   }

   // draw painted pixmap as background
   QPainter painter(this);
   painter.drawPixmap(rect(), *pix, pix->rect());

   // draw brush proxy as foreground
   painter.setOpacity(0.25); // drawing opacity
   painter.setPen(QColor(0,0,0)); // border color
   painter.setBrush(QColor(0,0,0)); // fill color
   painter.drawEllipse(mousePos, brushSize, brushSize);
}

void MyQPainterWidget::resizeEvent(QResizeEvent *event)
{
   if (pix!=NULL)
      setBackground(new QPixmap(*pix));
}

void MyQPainterWidget::mousePressEvent(QMouseEvent *event)
{
   mousePos = event->pos();

   if (event->buttons() & Qt::LeftButton)
      buttonDown = true;
   else
      event->ignore(); // propagate to parent

   brush();
}

void MyQPainterWidget::mouseReleaseEvent(QMouseEvent *event)
{
   buttonDown = false;
}

void MyQPainterWidget::mouseMoveEvent(QMouseEvent *event)
{
   mousePos = event->pos();

   brush();
}

void MyQPainterWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
   mousePressEvent(event);
   buttonDown = false;
}

void MyQPainterWidget::keyPressEvent(QKeyEvent *event)
{
   if (event->key() == Qt::Key_Greater)
      brushSize+=10;
   else if (event->key() == Qt::Key_Less)
      if (brushSize>10) brushSize-=10;

   brush();
}

void MyQPainterWidget::keyReleaseEvent(QKeyEvent *event)
{
   event->ignore(); // propagate to parent
}

void MyQPainterWidget::wheelEvent(QWheelEvent *event)
{
   event->accept(); // do not propagate to parent
}

void MyQPainterWidget::timerEvent(QTimerEvent *event)
{
   mousePos = mapFromGlobal(QCursor::pos());

   repaint();
}

void MyQPainterWidget::dragEnterEvent(QDragEnterEvent *event)
{
   event->acceptProposedAction();
}

void MyQPainterWidget::dragMoveEvent(QDragMoveEvent *event)
{
   event->acceptProposedAction();
}

void MyQPainterWidget::dropEvent(QDropEvent *event)
{
   const QMimeData *mimeData = event->mimeData();

   if (mimeData->hasUrls())
   {
      event->acceptProposedAction();

      QList<QUrl> urlList = mimeData->urls();

      QUrl qurl = urlList.at(0);
      QString url = qurl.toString();

      QImage img;

      if (url.startsWith("file://"))
          url=url.remove("file://");

      if (img.load(url))
      {
         QPixmap pixmap;
         pixmap.convertFromImage(img);
         setBackground(new QPixmap(pixmap));
      }
   }
}

void MyQPainterWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
   event->accept();
}

Access the example via WebSVN:

Checkout the Qt example via SVN:

svn co svn://schorsch.efi.fh-nuernberg.de/qt-examples/example-06


Drag and Dropping URLs | | QGL

Options: