void NyanCatAnalyzer::analyze(QPainter& p, const Analyzer::Scope& s, bool new_frame) { // Discard the second half of the transform const int scope_size = s.size() / 2; if ((new_frame && is_playing_) || (buffer_[0].isNull() && buffer_[1].isNull())) { // Transform the music into rainbows! for (int band = 0; band < kRainbowBands; ++band) { float* band_start = history_ + band * kHistorySize; // Move the history of each band across by 1 frame. memmove(band_start, band_start + 1, (kHistorySize - 1) * sizeof(float)); } // Now accumulate the scope data into each band. Should maybe use a series // of band pass filters for this, so bands can leak into neighbouring bands, // but for now it's a series of separate square filters. const int samples_per_band = scope_size / kRainbowBands; int sample = 0; for (int band = 0; band < kRainbowBands; ++band) { float accumulator = 0.0; for (int i = 0; i < samples_per_band; ++i) { accumulator += s[sample++]; } history_[(band + 1) * kHistorySize - 1] = accumulator * band_scale_[band]; } // Create polylines for the rainbows. QPointF polyline[kRainbowBands * kHistorySize]; QPointF* dest = polyline; float* source = history_; const float top_of_cat = float(height()) / 2 - float(kCatHeight) / 2; for (int band = 0; band < kRainbowBands; ++band) { // Calculate the Y position of this band. const float y = float(kCatHeight) / (kRainbowBands + 1) * (band + 0.5) + top_of_cat; // Add each point in the line. for (int x = 0; x < kHistorySize; ++x) { *dest = QPointF(px_per_frame_ * x, y + *source * kPixelScale); ++dest; ++source; } } // Do we have to draw the whole rainbow into the buffer? if (buffer_[0].isNull()) { for (int i = 0; i < 2; ++i) { buffer_[i] = QPixmap(QSize(width() + x_offset_, height())); buffer_[i].fill(background_brush_.color()); } current_buffer_ = 0; QPainter buffer_painter(&buffer_[0]); buffer_painter.setRenderHint(QPainter::Antialiasing); for (int band = kRainbowBands - 1; band >= 0; --band) { buffer_painter.setPen(colors_[band]); buffer_painter.drawPolyline(&polyline[band * kHistorySize], kHistorySize); buffer_painter.drawPolyline(&polyline[band * kHistorySize], kHistorySize); } } else { const int last_buffer = current_buffer_; current_buffer_ = (current_buffer_ + 1) % 2; // We can just shuffle the buffer along a bit and draw the new frame's // data. QPainter buffer_painter(&buffer_[current_buffer_]); buffer_painter.setRenderHint(QPainter::Antialiasing); buffer_painter.drawPixmap( 0, 0, buffer_[last_buffer], px_per_frame_, 0, x_offset_ + available_rainbow_width_ - px_per_frame_, 0); buffer_painter.fillRect( x_offset_ + available_rainbow_width_ - px_per_frame_, 0, kCatWidth - kRainbowOverlap + px_per_frame_, height(), background_brush_); for (int band = kRainbowBands - 1; band >= 0; --band) { buffer_painter.setPen(colors_[band]); buffer_painter.drawPolyline(&polyline[(band + 1) * kHistorySize - 3], 3); } } } // Draw the buffer on to the widget p.drawPixmap(0, 0, buffer_[current_buffer_], x_offset_, 0, 0, 0); // Draw nyan cat (he's been waiting for this for 75 lines). // Nyan nyan nyan nyan. if (!is_playing_) { // Ssshhh! p.drawPixmap(SleepingCatDestRect(), cat_, SleepingCatSourceRect()); } else { p.drawPixmap(CatDestRect(), cat_, CatSourceRect()); } }
void TwoPlayerTimingGameWidget::makeBasicPixmap( #ifdef USE_PIXMAP QPixmap& pixmap, #else QPainter* painter, #endif int width, int height) { #ifdef USE_PIXMAP pixmap = QPixmap(width, height); // Fill the pixmap with black background pixmap.fill(Qt::black); // Get the painter QPainter *painter = new QPainter(&pixmap); #else painter->fillRect(0,0,width,height,QColor(0,0,0)); #endif double xRate = 1.0 * width / LOGICAL_WIDTH; double yRate = 1.0 * height / LOGICAL_HEIGHT; // if (beginAnim > 0) // { // } // else { painter->translate(GAME1_X_TO * xRate, 0); painter->rotate(90); if (game1) { #ifdef USE_PIXMAP QPixmap tmp; game1->makePixmap(tmp, GAME1_WIDTH * yRate, GAME1_HEIGHT * xRate); painter->drawPixmap(0, 0, tmp); #else game1->makePixmap(painter, GAME1_WIDTH * yRate, GAME1_HEIGHT * xRate); #endif } else { painter->setOpacity(0.5); drawPixmapAt(painter, game1End, yRate, xRate, QPointF(0, 0), true, false); painter->setOpacity(1); } painter->rotate(-90); painter->translate(-GAME1_X_TO * xRate, 0); painter->translate(GAME2_X_FROM * xRate, LOGICAL_HEIGHT * yRate); painter->rotate(-90); if (game2) { #ifdef USE_PIXMAP QPixmap tmp; game2->makePixmap(tmp, GAME2_WIDTH * yRate, GAME2_HEIGHT * xRate); painter->drawPixmap(0, 0, tmp); #else game2->makePixmap(painter, GAME2_WIDTH * yRate, GAME2_HEIGHT * xRate); #endif } else { painter->setOpacity(0.5); drawPixmapAt(painter, game2End, yRate, xRate, QPointF(0, 0), true, false); painter->setOpacity(1); } painter->rotate(90); painter->translate(-GAME2_X_FROM * xRate, -LOGICAL_HEIGHT * yRate); if (endAnim >= 0) { painter->scale(xRate, yRate); painter->setOpacity(qMin(1.0, 1.0 * (END_ENIM_LAST - endAnim) * 2 / END_ENIM_LAST)); QPointF gFrom = QPointF(frameCount * 3, 0); QPointF gTo = QPointF(frameCount * 3 - 100, -100); QLinearGradient gradient = QLinearGradient(gFrom, gTo); gradient.setColorAt(0, LINENEAR_COLOR_0); gradient.setColorAt(1, LINENEAR_COLOR_1); gradient.setSpread(QGradient::ReflectSpread); QBrush brush = QBrush(gradient); painter->setPen(QPen(brush, 4)); painter->translate((GAME1_X_FROM + GAME1_X_TO) / 2, LOGICAL_HEIGHT / 2); painter->rotate(90); if (result1 > result2) { painter->drawPath(youWin); painter->fillPath(youWin, brush); } else if (result1 < result2) { painter->drawPath(youLose); painter->fillPath(youLose, brush); } else { painter->drawPath(drawGame); painter->fillPath(drawGame, brush); } painter->rotate(-90); painter->translate(-(GAME1_X_FROM + GAME1_X_TO) / 2, -LOGICAL_HEIGHT / 2); painter->translate((GAME2_X_FROM + GAME2_X_TO) / 2, LOGICAL_HEIGHT / 2); painter->rotate(-90); if (result2 > result1) { painter->drawPath(youWin); painter->fillPath(youWin, brush); } else if (result2 < result1) { painter->drawPath(youLose); painter->fillPath(youLose, brush); } else { painter->drawPath(drawGame); painter->fillPath(drawGame, brush); } painter->rotate(90); painter->translate(-(GAME2_X_FROM + GAME2_X_TO) / 2, -LOGICAL_HEIGHT / 2); painter->scale(1.0 / xRate, 1.0 / yRate); } } ++frameCount; #ifdef USE_PIXMAP // End the paint and release the space painter->end(); delete painter; #endif }
void MediaSourceDesktop::CreateScreenshot() { AVFrame *tRGBFrame; int tCaptureResX = mSourceResX; int tCaptureResY = mSourceResY; mMutexGrabberActive.lock(); // LOG(LOG_VERBOSE, "Source: %d * %d", mSourceResX, mSourceResY); // LOG(LOG_VERBOSE, "Target: %d * %d", mTargetResX, mTargetResY); if (!mMediaSourceOpened) { mMutexGrabberActive.unlock(); return; } if (mWidget == NULL) { LOG(LOG_ERROR, "Capture widget is invalid"); mMutexGrabberActive.unlock(); return; } QTime tCurrentTime = QTime::currentTime(); int tTimeDiff = mLastTimeGrabbed.msecsTo(tCurrentTime); //### skip capturing when we are too slow if (tTimeDiff < 1000 / (mInputFrameRate + 0.5 /* some tolerance! */)) { #ifdef MSD_DEBUG_PACKETS LOG(LOG_VERBOSE, "Screen capturing skipped because system is too fast"); #endif mMutexGrabberActive.unlock(); return; } if (mLastTimeGrabbed == QTime(0, 0, 0, 0)) { mLastTimeGrabbed = tCurrentTime; mMutexGrabberActive.unlock(); return; }else mLastTimeGrabbed = tCurrentTime; //### skip capturing when we are too slow if (tTimeDiff > 1000 / MIN_GRABBING_FPS) { LOG(LOG_WARN, "Screen capturing skipped because system is too busy"); mMutexGrabberActive.unlock(); return; } //#################################################################### //### AUTO DESKTOP //#################################################################### QDesktopWidget *tDesktop = QApplication::desktop(); if (mAutoDesktop) { tCaptureResX = tDesktop->availableGeometry(tDesktop->primaryScreen()).width(); tCaptureResY = tDesktop->availableGeometry(tDesktop->primaryScreen()).height(); } if (mAutoScreen) { #ifdef APPLE tCaptureResX = CGDisplayPixelsWide(CGMainDisplayID()); tCaptureResY = CGDisplayPixelsHigh(CGMainDisplayID()); #else tCaptureResX = tDesktop->screenGeometry(tDesktop->primaryScreen()).width(); tCaptureResY = tDesktop->screenGeometry(tDesktop->primaryScreen()).height(); #endif } //#################################################################### //### GRABBING //#################################################################### QPixmap tSourcePixmap; // screen capturing #if !defined(APPLE) || defined(HOMER_QT5) tSourcePixmap = QPixmap::grabWindow(mWidget->winId(), mGrabOffsetX, mGrabOffsetY, tCaptureResX, tCaptureResY); #else CGImageRef tOSXWindowImage = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, mWidget->winId(), kCGWindowImageDefault); tSourcePixmap = QPixmap::fromMacCGImageRef(tOSXWindowImage).copy(mGrabOffsetX, mGrabOffsetY, tCaptureResX, tCaptureResY); CGImageRelease(tOSXWindowImage); #endif //#################################################################### //### SCALING to source resolution //#################################################################### if ((tSourcePixmap.width() != mSourceResX) || (tSourcePixmap.height() != mSourceResY)) {// we have to adapt the assumed source resolution //LOG(LOG_VERBOSE, "Have to rescale from %d*%d to %d*%d", tSourcePixmap.width(), tSourcePixmap.height(), mSourceResX, mSourceResY); tSourcePixmap = tSourcePixmap.scaled(mSourceResX, mSourceResY); } //#################################################################### //### MOUSE VISUALIZATION //#################################################################### if (mMouseVisualization) { QPoint tMousePos = QCursor::pos(); if ((tMousePos.x() < tSourcePixmap.width()) && (tMousePos.y() < tSourcePixmap.height())) {// mouse is in visible area int tMousePosInSourcePixmapX = mSourceResX * tMousePos.x() / tCaptureResX; int tMousePosInSourcePixmapY = mSourceResY * tMousePos.y() / tCaptureResY; //LOG(LOG_VERBOSE, "Mouse position: %d*%d", tMousePosInSourcePixmapX, tMousePosInSourcePixmapY); QPainter *tPainter = new QPainter(&tSourcePixmap); //TODO: add support for click visualization tPainter->drawPixmap(tMousePosInSourcePixmapX, tMousePosInSourcePixmapY, QPixmap(":/images/MouseBlack.png").scaled(16, 32)); delete tPainter; } } if(!tSourcePixmap.isNull()) { // record screenshot via ffmpeg if (mRecording) { if ((tRGBFrame = AllocFrame()) == NULL) { LOG(LOG_ERROR, "Unable to allocate memory for RGB frame"); }else { QImage tSourceImage = QImage((unsigned char*)mOriginalScreenshot, mSourceResX, mSourceResY, QImage::Format_RGB32); QPainter *tSourcePainter = new QPainter(&tSourceImage); tSourcePainter->drawPixmap(0, 0, tSourcePixmap); delete tSourcePainter; // Assign appropriate parts of buffer to image planes in tRGBFrame FillFrame(tRGBFrame, mOriginalScreenshot, PIX_FMT_RGB32, mSourceResX, mSourceResY); // set frame number in corresponding entries within AVFrame structure tRGBFrame->pts = mRecorderChunkNumber; tRGBFrame->coded_picture_number = mRecorderChunkNumber; tRGBFrame->display_picture_number = mRecorderChunkNumber; mRecorderChunkNumber++; // emulate set FPS tRGBFrame->pts = GetPtsFromFpsEmulator(); // re-encode the frame and write it to file RecordFrame(tRGBFrame); } } // lock screenshot buffer mMutexScreenshot.lock(); if (mOutputScreenshot == NULL) { LOG(LOG_ERROR, "Invalid screenshot buffer: %p %d*%d", mOutputScreenshot, mTargetResX, mTargetResY); mMutexScreenshot.unlock(); mMutexGrabberActive.unlock(); return; } int tTargetResX = mTargetResX; if (tTargetResX > MAX_WIDTH) tTargetResX = MAX_WIDTH; int tTargetResY = mTargetResY; if (tTargetResY > MAX_HEIGHT) tTargetResY = MAX_HEIGHT; QImage tTargetImage = QImage((unsigned char*)mOutputScreenshot, tTargetResX, tTargetResY, QImage::Format_RGB32); QPainter *tTargetPainter = new QPainter(&tTargetImage); QPixmap tScaledSourcePixmap = tSourcePixmap.scaled(tTargetResX, tTargetResY); tTargetPainter->drawPixmap(0, 0, tScaledSourcePixmap); delete tTargetPainter; RelayChunkToMediaFilters((char*)mOutputScreenshot, tTargetResX * tTargetResY * MSD_BYTES_PER_PIXEL, 1); mScreenshotUpdated = true; // notify consumer about new screenshot mWaitConditionScreenshotUpdated.wakeAll(); // unlock screenshot buffer again mMutexScreenshot.unlock(); }else LOG(LOG_ERROR, "Source pixmap is invalid"); mMutexGrabberActive.unlock(); }
void ImagePortOverlay::paintContent(QPainter& p) { mMutex.lock(); if (mLastValue.isValid()) { cv::Mat mat = port::Image::fromVariant(mLastValue); if(mat.data == 0) { DepthMap dm = port::DepthMap::fromVariant(mLastValue); if(dm.data != 0) mat = dm.toImage(); } if (mat.size().width != 0 && mat.size().height != 0) { // Color conversion if (mat.type() == CV_8UC1 || mat.type() == CV_16UC1) { cv::cvtColor(mat, mConverted, CV_GRAY2BGRA); } else if (mat.type() == CV_8UC3 || mat.type() == CV_16UC3) { cv::cvtColor(mat, mConverted, CV_BGR2BGRA); } else if(mat.type() == CV_8UC4 || mat.type() == CV_16UC4) { mConverted = mat; } else { qCritical() << tr("VideoDisplayWidget: unknown image format received") << mat.type(); } // Bit width conversion (if required) // We can only display 8Bit images if(mConverted.depth() == CV_16U){ // Use a scale factor to bring 0 -> 65535 to 0 -> 255 // otherwise convertTo will only chop of the highest bits mConverted.convertTo(mConverted, CV_8U, 1/255.0); } // Convert to QImage QImage::Format format = QImage::Format_ARGB32; QImage image = QImage(mConverted.data, mConverted.size().width, mConverted.size().height, mConverted.step, format); mPixmap = QPixmap::fromImage(image); mLastValue = QVariant(); // Set ascpect ratio QSize imageSize = mPixmap.size(); if (mState == Scaling){ mScale = mRect.width() / (double) imageSize.width(); } else { mRect.setSize(imageSize * mScale); } } else { qWarning() << tr("VideoDisplay: image without dimensions received"); } } mMutex.unlock(); QRect rect = (mRect.isEmpty() ? QRect(0, 0, 640, 480) : mRect); mPen.setColor(Qt::black); p.setPen(mPen); if (mPixmap.isNull()){ p.setBrush(Qt::black); } p.drawRect(rect); if (!mPixmap.isNull()) { p.drawPixmap(rect, mPixmap); } }
void ArthurFrame::paintEvent(QPaintEvent *e) { #ifdef Q_WS_QWS static QPixmap *static_image = 0; #else static QImage *static_image = 0; #endif QPainter painter; if (preferImage() #ifdef QT_OPENGL_SUPPORT && !m_use_opengl #endif ) { if (!static_image || static_image->size() != size()) { delete static_image; #ifdef Q_WS_QWS static_image = new QPixmap(size()); #else static_image = new QImage(size(), QImage::Format_RGB32); #endif } painter.begin(static_image); int o = 10; QBrush bg = palette().brush(QPalette::Background); painter.fillRect(0, 0, o, o, bg); painter.fillRect(width() - o, 0, o, o, bg); painter.fillRect(0, height() - o, o, o, bg); painter.fillRect(width() - o, height() - o, o, o, bg); } else { #ifdef QT_OPENGL_SUPPORT if (m_use_opengl) { painter.begin(glw); painter.fillRect(QRectF(0, 0, glw->width(), glw->height()), palette().color(backgroundRole())); } else { painter.begin(this); } #else painter.begin(this); #endif } painter.setClipRect(e->rect()); painter.setRenderHint(QPainter::Antialiasing); QPainterPath clipPath; QRect r = rect(); qreal left = r.x() + 1; qreal top = r.y() + 1; qreal right = r.right(); qreal bottom = r.bottom(); qreal radius2 = 8 * 2; clipPath.moveTo(right - radius2, top); clipPath.arcTo(right - radius2, top, radius2, radius2, 90, -90); clipPath.arcTo(right - radius2, bottom - radius2, radius2, radius2, 0, -90); clipPath.arcTo(left, bottom - radius2, radius2, radius2, 270, -90); clipPath.arcTo(left, top, radius2, radius2, 180, -90); clipPath.closeSubpath(); painter.save(); painter.setClipPath(clipPath, Qt::IntersectClip); painter.drawTiledPixmap(rect(), m_tile); // client painting paint(&painter); painter.restore(); painter.save(); if (m_show_doc) paintDescription(&painter); painter.restore(); int level = 180; painter.setPen(QPen(QColor(level, level, level), 2)); painter.setBrush(Qt::NoBrush); painter.drawPath(clipPath); if (preferImage() #ifdef QT_OPENGL_SUPPORT && !m_use_opengl #endif ) { painter.end(); painter.begin(this); #ifdef Q_WS_QWS painter.drawPixmap(e->rect(), *static_image, e->rect()); #else painter.drawImage(e->rect(), *static_image, e->rect()); #endif } #ifdef QT_OPENGL_SUPPORT if (m_use_opengl && (inherits("PathDeformRenderer") || inherits("PathStrokeRenderer") || inherits("CompositionRenderer") || m_show_doc)) glw->swapBuffers(); #endif }
/*!\reimp */ void QRadioButton::drawButton( QPainter *paint ) { QPainter *p = paint; const QColorGroup & g = colorGroup(); int x, y; QFontMetrics fm = fontMetrics(); QSize lsz = fm.size(ShowPrefix, text()); QSize sz = style().exclusiveIndicatorSize(); x = text().isEmpty() ? 1 : 0; y = (height() - lsz.height() + fm.height() - sz.height())/2; #ifndef QT_NO_TEXTSTREAM #define SAVE_RADIOBUTTON_PIXMAPS #endif #if defined(SAVE_RADIOBUTTON_PIXMAPS) QString pmkey; // pixmap key int kf = 0; if ( isDown() ) kf |= 1; if ( isOn() ) kf |= 2; if ( isEnabled() ) kf |= 4; QTextOStream os(&pmkey); os << "$qt_radio_" << style().className() << "_" << palette().serialNumber() << "_" << kf; QPixmap *pm = QPixmapCache::find( pmkey ); if ( pm ) { // pixmap exists drawButtonLabel( p ); p->drawPixmap( x, y, *pm ); return; } bool use_pm = TRUE; QPainter pmpaint; int wx, wy; if ( use_pm ) { pm = new QPixmap( sz ); // create new pixmap CHECK_PTR( pm ); pmpaint.begin( pm ); p = &pmpaint; // draw in pixmap wx=x; wy=y; // save x,y coords x = y = 0; p->setBackgroundColor( g.background() ); } #endif #define QCOORDARRLEN(x) sizeof(x)/(sizeof(QCOORD)*2) style().drawExclusiveIndicator(p, x, y, sz.width(), sz.height(), g, isOn(), isDown(), isEnabled() ); #if defined(SAVE_RADIOBUTTON_PIXMAPS) if ( use_pm ) { pmpaint.end(); if ( backgroundPixmap() || backgroundMode() == X11ParentRelative ) { QBitmap bm( pm->size() ); bm.fill( color0 ); pmpaint.begin( &bm ); style().drawExclusiveIndicatorMask( &pmpaint, 0, 0, bm.width(), bm.height(), isOn() ); pmpaint.end(); pm->setMask( bm ); } p = paint; // draw in default device p->drawPixmap( wx, wy, *pm ); if (!QPixmapCache::insert(pmkey, pm) ) // save in cache delete pm; } #endif drawButtonLabel( p ); }
void ribi::Chess::QtChessBoardWidget::DrawChessBoard( QPainter& painter, const Chess::BoardWidget * const widget) { const int w = widget->GetWidth(); const int h = widget->GetHeight(); //Draw the plain chessboard DrawChessBoard( painter, widget->GetLeft(), widget->GetTop(), w, h, widget->GetBoard().get()); //Draw the selected square static const Chess::QtResources r; const int square_w = w / 8; const int square_h = h / 8; const boost::shared_ptr<const Chess::Square> selected = widget->GetSelector()->GetSelected(); if (selected) { TRACE_FUNC(); const int x_co = selected->GetFile().ToInt() * square_w; const int y_co = selected->GetRank().ToInt() * square_h; if (widget->GetBoard()->GetPiece(selected)) { const std::string filename = Chess::Resources::Find( widget->GetBoard()->GetPiece(selected), Chess::SquareSelector::m_selected_color, true); TRACE(filename); assert(fileio::FileIo().IsRegularFile(filename)); const QPixmap p(filename.c_str()); painter.drawPixmap(x_co,y_co,square_w,square_h,p); } else { assert(!"Should not get here"); } //Draw the possible moves const std::vector<boost::shared_ptr<Move> > moves = widget->GetBoard()->GetMoves(selected); for(const boost::shared_ptr<Move> move: moves) { if (move->To()) { const int x_co = move->To()->GetFile().ToInt() * square_w; const int y_co = move->To()->GetRank().ToInt() * square_h; if (widget->GetBoard()->GetPiece(move->To())) { const std::string filename = Chess::Resources::Find( widget->GetBoard()->GetPiece(move->To()), Chess::SquareSelector::m_moves_color); assert(fileio::FileIo().IsRegularFile(filename)); const QPixmap p(filename.c_str()); painter.drawPixmap(x_co,y_co,square_w,square_h,p); } else { const boost::shared_ptr<Square> square { SquareFactory().Create(move->To()->GetFile(),move->To()->GetRank()) }; const std::string filename = Chess::Resources::Find( square, Chess::SquareSelector::m_moves_color); assert(fileio::FileIo().IsRegularFile(filename)); const QPixmap p(filename.c_str()); painter.drawPixmap(x_co,y_co,square_w,square_h,p); } } } } //Draw cursor const boost::shared_ptr<const Chess::Square> cursor = widget->GetSelector()->GetCursor(); assert(cursor); { const int x_co = cursor->GetFile().ToInt() * square_w; const int y_co = cursor->GetRank().ToInt() * square_h; if (widget->GetBoard()->GetPiece(cursor)) { const std::string filename = Chess::Resources::Find(widget->GetBoard()->GetPiece(cursor), Chess::SquareSelector::m_cursor_color, selected && *selected == *cursor ); assert(fileio::FileIo().IsRegularFile(filename)); const QPixmap p(filename.c_str()); painter.drawPixmap(x_co,y_co,square_w,square_h,p); } else { const std::string filename = Chess::Resources::Find( cursor, Chess::SquareSelector::m_cursor_color); assert(fileio::FileIo().IsRegularFile(filename)); const QPixmap p(filename.c_str()); painter.drawPixmap(x_co,y_co,square_w,square_h,p); } } }
void LightMaps::paintEvent(QPaintEvent *event) { QPainter p; p.begin(this); m_normalMap->render(&p, event->rect()); p.setPen(Qt::black); p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap, "Map data CCBYSA 2009 OpenStreetMap.org contributors"); p.end(); if (zoomed) { int dim = qMin(width(), height()); int magnifierSize = qMin(MAX_MAGNIFIER, dim * 2 / 3); int radius = magnifierSize / 2; int ring = radius - 15; QSize box = QSize(magnifierSize, magnifierSize); // reupdate our mask if (maskPixmap.size() != box) { maskPixmap = QPixmap(box); maskPixmap.fill(Qt::transparent); QRadialGradient g; g.setCenter(radius, radius); g.setFocalPoint(radius, radius); g.setRadius(radius); g.setColorAt(1.0, QColor(255, 255, 255, 0)); g.setColorAt(0.5, QColor(128, 128, 128, 255)); QPainter mask(&maskPixmap); mask.setRenderHint(QPainter::Antialiasing); mask.setCompositionMode(QPainter::CompositionMode_Source); mask.setBrush(g); mask.setPen(Qt::NoPen); mask.drawRect(maskPixmap.rect()); mask.setBrush(QColor(Qt::transparent)); mask.drawEllipse(g.center(), ring, ring); mask.end(); } QPoint center = dragPos - QPoint(0, radius); center = center + QPoint(0, radius / 2); QPoint corner = center - QPoint(radius, radius); QPoint xy = center * 2 - QPoint(radius, radius); // only set the dimension to the magnified portion if (zoomPixmap.size() != box) { zoomPixmap = QPixmap(box); zoomPixmap.fill(Qt::lightGray); } if (true) { QPainter p(&zoomPixmap); p.translate(-xy); m_largeMap->render(&p, QRect(xy, box)); p.end(); } QPainterPath clipPath; clipPath.addEllipse(center, ring, ring); QPainter p(this); p.setRenderHint(QPainter::Antialiasing); p.setClipPath(clipPath); p.drawPixmap(corner, zoomPixmap); p.setClipping(false); p.drawPixmap(corner, maskPixmap); p.setPen(Qt::gray); p.drawPath(clipPath); } if (invert) { QPainter p(this); p.setCompositionMode(QPainter::CompositionMode_Difference); p.fillRect(event->rect(), Qt::white); p.end(); } }
void PseudoStateCanvas::draw(QPainter & p) { if (!visible() || ((xpm == 0) && !manual_size)) return; QRect r = rect(); QRect intern_r; p.setBackgroundMode(::Qt::OpaqueMode); if (xpm != 0) p.drawPixmap(r.topLeft(), *xpm); else { // jork join manually sized if (horiz) { intern_r.setX(r.x() + 1); intern_r.setWidth(r.width() - 2); intern_r.setY(r.y() + 6); intern_r.setHeight(3); } else { intern_r.setX(r.x() + 6); intern_r.setWidth(3); intern_r.setY(r.y() + 1); intern_r.setHeight(r.height() - 2); } p.fillRect(intern_r, ::Qt::black); } if (selected()) show_mark(p, r); FILE * fp = svg(); if (fp != 0) { bool big = the_canvas()->zoom() >= 1.0; int px = (int) x(); int py = (int) y(); switch (browser_node->get_type()) { case InitialPS: if (big) fprintf(fp, "<ellipse fill=\"black\" cx=\"%d\" cy=\"%d\" rx=\"8.5\" ry=\"8.5\" />\n", px + 9, py + 9); else fprintf(fp, "<ellipse fill=\"black\" cx=\"%d\" cy=\"%d\" rx=\"5.5\" ry=\"5.5\" />\n", px + 7, py + 7); break; case EntryPointPS: if (big) fprintf(fp, "<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"8.5\" ry=\"8.5\" />\n", px + 9, py + 9); else fprintf(fp, "<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"5.5\" ry=\"5.5\" />\n", px + 7, py + 7); break; case FinalPS: if (big) { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"11.5\" ry=\"11.5\" />\n", px + 12, py + 12); fprintf(fp, "\t<ellipse fill=\"black\" cx=\"%d\" cy=\"%d\" rx=\"8.5\" ry=\"8.5\" />\n" "</g>\n", px + 12, py + 12); } else { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"7.5\" ry=\"7.5\" />\n", px + 8, py + 8); fprintf(fp, "\t<ellipse fill=\"black\" cx=\"%d\" cy=\"%d\" rx=\"4.5\" ry=\"4.5\" />\n" "</g>\n", px + 8, py + 8); } break; case TerminatePS: if (big) { fprintf(fp, "<g>\n" "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 1, py + 1, px + 19, py + 19); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 19, py + 1, px + 1, py + 19); } else{ fprintf(fp, "<g>\n" "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 2, py + 2, px + 12, py + 12); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 12, py + 2, px + 2, py + 12); } break; case ExitPointPS: if (big) { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"8.5\" ry=\"8.5\" />\n", px + 9, py + 9); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 4, px + 14, py + 14); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 14, py + 4, px + 4, py + 14); } else { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"5.5\" ry=\"5.5\" />\n", px + 7, py + 7); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 4, px + 10, py + 10); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 10, py + 4, px + 4, py + 10); } break; case DeepHistoryPS: if (big) { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"12.5\" ry=\"12.5\" />\n", px + 12, py + 12); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 8, px + 4, py + 16); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 10, py + 8, px + 10, py + 16); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 12, px + 10, py + 12); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 12, py + 9, px + 20, py + 9); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 13, py + 6, px + 19, py + 12); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 13, py + 12, px + 19, py + 6); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 16, py + 5, px + 16, py + 13); } else { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"7.5\" ry=\"7.5\" />\n", px + 8, py + 8); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 6, px + 4, py + 10); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 7, py + 6, px + 7, py + 10); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 4, py + 8, px + 7, py + 8); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 9, py + 7, px + 13, py + 7); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 9, py + 5, px + 13, py + 9); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 9, py + 9, px + 13, py + 5); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 11, py + 5, px + 11, py + 9); } break; case ShallowHistoryPS: if (big) { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"12.5\" ry=\"12.5\" />\n", px + 12, py + 12); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 8, py + 8, px + 8, py + 16); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 15, py + 8, px + 15, py + 16); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 8, py + 12, px + 15, py + 12); } else { fprintf(fp, "<g>\n" "\t<ellipse fill=\"white\" stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" cx=\"%d\" cy=\"%d\" rx=\"7.5\" ry=\"7.5\" />\n", px + 8, py + 8); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 5, py + 5, px + 5, py + 11); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 11, py + 5, px + 11, py + 11); fprintf(fp, "\t<line stroke=\"black\" stroke-width=\"1\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n" "</g>\n", px + 5, py + 8, px + 11, py + 8); } break; case JunctionPS: if (big) fprintf(fp, "<ellipse fill=\"black\" stroke=\"none\" cx=\"%d\" cy=\"%d\" rx=\"6.5\" ry=\"6.5\" />\n", px + 7, py + 7); else fprintf(fp, "<ellipse fill=\"black\" stroke=\"none\" cx=\"%d\" cy=\"%d\" rx=\"4.5\" ry=\"4.5\" />\n", px + 5, py + 5); break; case ChoicePS: // note : shadow not produced if (big) fprintf(fp, "<polygon fill=\"white\" stroke=\"black\" stroke-opacity=\"1\" points =\"%d,%d %d,%d %d,%d %d,%d\" />\n", px + 3, py + 18, px + 12, py + 1, px + 21, py + 18, px + 12, py + 35); else fprintf(fp, "<polygon fill=\"white\" stroke=\"black\" stroke-opacity=\"1\" points =\"%d,%d %d,%d %d,%d %d,%d\" />\n", px + 2, py + 12, px + 8, py + 1, px + 14, py + 12, px + 8, py + 23); break; case ForkPS: case JoinPS: if (horiz) { if (manual_size) fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", intern_r.x(), intern_r.y(), intern_r.x() + intern_r.width() - 1, intern_r.y()); else if (big) fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 1, py + 6, px + 23, py + 6); else fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 1, py + 6, px + 16, py + 6); } else { if (manual_size) fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", intern_r.x(), intern_r.y(), intern_r.x(), intern_r.y() + intern_r.height() - 1); else if (big) fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 6, py + 1, px + 6, py + 24); else fprintf(fp, "<line stroke=\"black\" stroke-width=\"3\" stroke-opacity=\"1\" x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" />\n", px + 6, py + 1, px + 6, py + 17); } break; default: break; } } }
/* Prints a QPixmap, optionally displaying the print setup dialog first */ void PrintPixmap(QPixmap pixmap, bool displayDialog, QWidget *pParent, QPrinter *pPrinter) { bool deletePrinter = false; if (pixmap.isNull() == true) { return; } // Create a printer device. Analogous to a Windows device context if (pPrinter == NULL) { deletePrinter = true; pPrinter = new QPrinter(); if (pPrinter == NULL) { return; } } bool bPrint = true; if (displayDialog) { QPrintDialog dlg(pPrinter, pParent); if (dlg.exec() == QDialog::Rejected) { bPrint = false; } } if (bPrint == true) { double dAspect = 1.0; // the aspect ratio of the pixmap = width / height dAspect = static_cast<double>(pixmap.width()) / static_cast<double>(pixmap.height()); // the QPainter provides an interface for drawing to a device, analogous // to Windows GDI, with the printer being the device context in this case QPainter p; if (p.begin(pPrinter) == false) { if (deletePrinter) { delete pPrinter; } return; } QRect rcViewport = p.viewport(); // the printable area in device coords // Determine how large we can make the pixmap on the paper without // losing any of it off the edges of the paper. iPrintWidth and iPrintHeight // will be the size of the pixmap on the paper, in printer device coords. int iPrintWidth = rcViewport.width(); int iPrintHeight = rcViewport.height(); double pAspect = static_cast<double>(iPrintWidth) / static_cast<double>(iPrintHeight); // aspect ratio of the paper // unless the aspect ratios of the paper and pixmap are equal, we will have unused // space above and below or left and right of the printed image. double ratioAspects = pAspect / dAspect; if (ratioAspects > 1.0) { // paper is wider than the image: empty space left and right // reduce iPrintWidth accordingly iPrintWidth = iPrintWidth / ratioAspects; } else { // paper is taller than the image: empty space above and below // reduce iPrintHeight accordingly iPrintHeight = iPrintHeight * ratioAspects; } // specify the pixel dimensions of the pixmap p.setWindow(pixmap.rect()); // specify the location and size to draw the pixmap on the paper p.setViewport(rcViewport.left() + (rcViewport.width() - iPrintWidth) / 2, rcViewport.top() + (rcViewport.height() - iPrintHeight) / 2, iPrintWidth, iPrintHeight); // draw the pixmap to the print device p.drawPixmap(0, 0, pixmap); // tell the printer that we are done; this will trigger a form feed p.end(); } if (deletePrinter) { delete pPrinter; } }
void Right_platform::PaintPlatform (QPainter &p){ static QPixmap pix(":/Platform/Resources/Right_Platform.png"); p.drawPixmap(x,y,143,Height,pix); }
/*! Событие нажатия какой-либо клавиши мыши * \param [in|out] pe Указатель на событие мыши */ /*virtual*/ void bomb_item::mousePressEvent(QGraphicsSceneMouseEvent* pe) { dynamic_cast<Field*>(this->scene())->sendStartGame(); //Если нажата левая кнопка мыши if(pe->button() == Qt::LeftButton && !isFlagged && !isPressed) { cur_block = block_pressed; isPressed = true; if(type == 'b') {//если бомба, заканчиваем игру //Дорисовываем бомбу QPixmap img = block_pressed; QPixmap bomb(":/Saper/bomb.png"); bomb = bomb.scaled(cur_block.size()); QPainter pr; pr.begin(&img); pr.drawPixmap(bomb.rect(),bomb); pr.end(); cur_block = img; //Меняем тип type = 'B'; //Сообщаем о проигрыше dynamic_cast<Field*>(this->scene())->sendEndGame(); } else if(type != '0') { QPixmap img = block_pressed; QPainter pr; pr.begin(&img); pr.setFont(QFont("Times",20,QFont::Normal)); int r,g,b; r = g = b = 0; //Выставлем цвет цифре смешивая цвета rgb if(type == '1' || type == '3' || type == '5' || type == '7') b = 110; if(type == '2' || type == '3' || type == '6' || type == '7') g = 130; if(type == '4' || type == '5' || type == '6' || type == '7') r = 130; pr.setPen(QColor(r,g,b)); pr.drawText(img.rect(),QString(type),QTextOption(Qt::AlignCenter)); pr.end(); cur_block = img; } else {//Пустая клетка //Открываем соседние клетки //Вычисляем размеры блока int picHeight = this->cur_block.height(); int picWidth = this->cur_block.width(); float x = this->pos().x(); float y = this->pos().y(); //Проходим по координатам вокруг клетки for(int i =-picHeight; i <= picHeight; i += picHeight) { for(int j =-picWidth; j <= picWidth; j += picWidth) { if(i || j) {//Середину не проверяем //Отправляем ивент о нажатии выбраной клетке QGraphicsItem * item = this->scene()->itemAt(x+(float)j,y+(float)i,QTransform()); if(item != NULL) this->scene()->sendEvent(item,pe); } } } } //Добавляем в открытые, этот блок dynamic_cast<Field*>(this->scene())->openBlock(); //Открываем блок if(dynamic_cast<Field*>(this->scene())->checkLastMines())//Проверяем окончена ли игра dynamic_cast<Field*>(this->scene())->sendEndGame(2);//Если победа, то сообщаем } else if(pe->button() == Qt::RightButton && !isPressed) { //Рисуем флажок if(!isFlagged) { isFlagged = true; QPixmap img = block; QPixmap flag(":/Saper/flag.png"); flag = flag.scaled(cur_block.size()); QPainter pr; pr.begin(&img); pr.drawPixmap(flag.rect(),flag); pr.end(); cur_block = img; } else { //Убираем флажок если он был cur_block = block; isPressed = isFlagged = false; } dynamic_cast<Field*>(this->scene())->sendMines(isFlagged); } //Перересовываем update(); }
void Widget::drawShape( QPainter &p ) { // initPainter(p); p.drawPixmap( int(x()), int(y()), QPixmap::grabWidget( widget() ) ); // deinitPainter(p); }