int main(int argc, char *argv[]) { QApplication a{argc, argv}; QLabel label; QPicture pic; pic.setBoundingRect({-100, -100, 200, 200}); QPainter p(&pic); const QPointF pt; p.drawEllipse(pt, 3, 3); p.setFont({"Helvetica", 40}); p.setPen({128, 0, 0, 128}); drawText(p, pt, Qt::AlignBottom, "_LB"); drawText(p, pt, Qt::AlignVCenter, "_LC"); drawText(p, pt, Qt::AlignTop, "_LT"); p.setPen({0, 128, 0, 128}); drawText(p, pt, Qt::AlignBottom | Qt::AlignHCenter, "MB"); drawText(p, pt, Qt::AlignVCenter | Qt::AlignHCenter, "MC"); drawText(p, pt, Qt::AlignTop | Qt::AlignHCenter, "MT"); p.setPen({0, 0, 128, 128}); drawText(p, pt, Qt::AlignBottom | Qt::AlignRight, "RB_"); drawText(p, pt, Qt::AlignVCenter | Qt::AlignRight, "RC_"); drawText(p, pt, Qt::AlignTop | Qt::AlignRight, "RT_"); p.end(); label.setPicture(pic); label.show(); return a.exec(); }
bool loadThemeFromBlob(const QString &themeBlob, QHash<QString, QPicture> &partPictures, QHash<QPair<QString, int>, QColor> &colors) { QFile blob(themeBlob); if (!blob.open(QIODevice::ReadOnly)) { qWarning() << __FUNCTION__ << ": Could not read blob: " << themeBlob; return false; } QDataStream blobIn(&blob); quint32 version; blobIn >> version; if (version != blobVersion) { qWarning() << __FUNCTION__ << ": Invalid blob version: " << version << " ...expected: " << blobVersion; return false; } QByteArray data; blobIn >> data; data = qUncompress(data); QBuffer dataBuffer(&data); dataBuffer.open(QIODevice::ReadOnly); QDataStream dataIn(&dataBuffer); int colorsCount; dataIn >> colorsCount; for (int i = 0; i < colorsCount; ++i) { QPair<QString, int> key; dataIn >> key; QColor value; dataIn >> value; colors.insert(key, value); } int picturesCount; dataIn >> picturesCount; for (int i = 0; i < picturesCount; ++i) { QString key; dataIn >> key; QPicture value; dataIn >> value; value.setBoundingRect(QRect(0, 0, pictureSize, pictureSize)); // Bug? The forced bounding rect was not deserialized. partPictures.insert(key, value); } if (dataIn.status() != QDataStream::Ok) { qWarning() << __FUNCTION__ << ": Invalid data blob: " << themeBlob; return false; } return true; }
void FLReportViewer::saveSVGStyle() { if (report) { QString fileName = QFileDialog::getSaveFileName("", tr("Fichero SVG (*.svg)"), this, tr("Guardar en SVG"), tr("Guardar en SVG")); if (fileName.isEmpty()) return; if (!fileName.upper().contains(".SVG")) fileName += ".svg"; if (QFile::exists(fileName) && QMessageBox::question(this, tr("Sobreescribir %1").arg(fileName), tr("Ya existe un fichero llamado %1. ¿ Desea sobreescribirlo ?").arg(fileName), tr("&Sí"), tr("&No"), QString::null, 0, 1)) return; FLStylePainter::setSVGMode(true); updateReport(); FLStylePainter::setSVGMode(false); QStringList fileNames; QString fname; QPicture *page; QSize psize; for (int i = 0; i < report->pageCount(); ++i) { fname = fileName + QString::number(i); fileNames.append(fname); page = report->getPageAt(i); psize = report->pageDimensions(); page->setBoundingRect(QRect(QPoint(0, 0), psize)); page->save(fname, "svg"); } FLStylePainter::normalizeSVGFile(fileName, fileNames); updateReport(); } }
void MainWindow::on_divideButton_clicked() { cubeRow = ui->cubeRow->value(); cubeCol = ui->cubeCol->value(); if(cubeRow*cubeCol >= 4 && !filename.isEmpty() && image_origin.rows >= cubeRow*3 && image_origin.cols >= cubeCol*3) { //I. divide image //1.prepare //int largeSquare = (int)floor(sqrt(cubeNum));//get the largest square that can be made //qDebug() << "largeSquare:" <<largeSquare; float picRatio = (float)image_origin.rows / (float)image_origin.cols;//row行数col列数 //qDebug() << "picRatio:" <<picRatio; float cubeRatio = (float)cubeRow / (float)cubeCol; int scale, fixedCol, fixedRow; if(picRatio >= cubeRatio)//row long { scale = image_origin.cols/(cubeCol*3);//img size devide cube's totol col fixedRow = scale*cubeRow*3;//set the size corresponding cube's fixedCol = scale*cubeCol*3; } else//col long { scale = image_origin.rows/(cubeRow*3); fixedRow = scale*cubeRow*3; fixedCol = scale*cubeCol*3; } int removeRows = image_origin.rows - fixedRow; int removeCols = image_origin.cols - fixedCol; //qDebug()<<"scale:"<<scale<<" row:"<<fixedRow<<" col:"<<fixedCol<<" Original resolution:"<<image_origin.rows<<image_origin.cols<<" remove:"<<removeRows<<removeCols; int removeRows_top,removeRows_down,removeCols_left,removeCols_right; if(removeRows%2 != 0)//the remove num of rows is not even number { //up need remove one more than down removeRows_top = removeRows/2 + 1; removeRows_down = removeRows/2; } else removeRows_top = removeRows_down = removeRows/2; if(removeCols%2 != 0)//the remove num of cols is not even number { //left need remove one more than right removeCols_left = removeCols/2 + 1; removeCols_right = removeCols/2; } else removeCols_left = removeCols_right = removeCols/2; //qDebug()<<"removeRow_fixed:"<<removeRows_top<<removeRows_down<<"removeCol_fixed:"<<removeCols_left<<removeCols_right; cv::Mat fixedImage = image_origin(Rect(removeCols_left,removeRows_top,fixedCol,fixedRow));//改好大小的图片 //resize the image cv::Size dsize = cv::Size(cubeCol*3,cubeRow*3);//Image resolution of the match cube ratio //cv::Mat image_resize = fixedImage; cv::resize(fixedImage, image_resize, dsize); image_resize = changeRubixColor(image_resize); cv::cvtColor(image_resize, image_resize, CV_BGR2RGB); //image_resize---->this is what we want!!!! QImage QimgResize = QImage((const unsigned char*)(image_resize.data),image_resize.cols,image_resize.rows, image_resize.cols*image_resize.channels(), QImage::Format_RGB888); //qDebug() << "QimgResizeRect:" << QimgResize.rect(); ui->picLabel->clear(); imgScaled = QimgResize.scaled(ui->picLabel->size(),Qt::KeepAspectRatio); //qDebug() << "imgScaledRect:" << imgScaled.rect(); ui->picLabel->setPixmap(QPixmap::fromImage(imgScaled));//image insert into label /*--------------------------drawing grids----------------------------*/ QPicture picture; QPainter painter; QPen pen_thick,pen_thin; pen_thick.setWidth(3); pen_thick.setColor(Qt::gray); pen_thin.setWidth(1); pen_thin.setColor(Qt::gray); painter.begin(&picture); picture.setBoundingRect(imgScaled.rect()); //set the boundary of painting //draw col line painter.setPen(pen_thin); for(int i = 0; i <= cubeCol*3; i++) painter.drawLine(QLineF((float)imgScaled.rect().width()/cubeCol/3.0*i, 0, (float)imgScaled.rect().width()/cubeCol/3.0*i, imgScaled.rect().height())); painter.setPen(pen_thick); for(int i = 0; i <= cubeCol; i++) painter.drawLine(QLineF((float)imgScaled.rect().width()/cubeCol*i, 0, (float)imgScaled.rect().width()/cubeCol*i, imgScaled.rect().height())); //draw row line painter.setPen(pen_thin); for(int i = 0; i <= cubeRow*3; i++) painter.drawLine(QLineF(0, (float)imgScaled.rect().height()/cubeRow/3.0*i, imgScaled.rect().width(), (float)imgScaled.rect().height()/cubeRow/3.0*i)); painter.setPen(pen_thick); for(int i = 0; i <= cubeRow; i++) painter.drawLine(QLineF(0, (float)imgScaled.rect().height()/cubeRow*i, imgScaled.rect().width(), (float)imgScaled.rect().height()/cubeRow*i)); painter.end(); picture.save("draw_record.pic"); ui->picLabelUp->setPicture(picture); } else if(cubeRow*cubeCol < 4) { int ret = QMessageBox::warning(this, "Warning", "The number of cubes is at least four!", QMessageBox::Abort); if (ret == QMessageBox::Abort) qDebug() << "WARNING!!"; } else if(filename.isEmpty()) { int ret = QMessageBox::warning(this, "Warning", "You need choose an image file!", QMessageBox::Abort); if (ret == QMessageBox::Abort) qDebug() << "WARNING!!"; } else if(image_origin.rows < cubeRow*3 || image_origin.cols < cubeCol*3) { int ret = QMessageBox::warning(this, "Warning", "Too many cubes!", QMessageBox::Abort); if (ret == QMessageBox::Abort) qDebug() << "WARNING!!"; } }
void SvgCanvas::actionExport_to_callback() { QDialog dialog; ExportToDialog export_to_dialog; export_to_dialog.setupUi(&dialog); QListWidget *list = export_to_dialog.formats_listWidget; QListWidgetItem *item; //Pixmaps formats QList<QByteArray> formats=QImageWriter::supportedImageFormats(); for(int i=0;i<formats.size();i++) { QString text(formats[i]); item=new QListWidgetItem(text,list); item->setData(1,QVariant(PIXMAP)); } //Vector formats formats= QPicture::outputFormats(); for(int i=0;i<formats.size();i++) { QString text(formats[i]); item=new QListWidgetItem(text,list); item->setData(1,QVariant(PICTURE)); } item=new QListWidgetItem("ps",list); item->setData(1,QVariant(PRINTER)); item=new QListWidgetItem("pdf",list); item->setData(1,QVariant(PDF)); item=new QListWidgetItem("svg",list); item->setData(1,QVariant(SVG)); int ok=dialog.exec(); if(ok==QDialog::Rejected) return; item =list->currentItem(); int format=item->data(1).toInt(); QPainter plot; switch(format) { case PIXMAP: { bool ok; int h, w = QInputDialog::getInteger(this, tr("Width"), tr("Width:"), 300, 0, 2147483647, 1, &ok); if(!ok) return; h=QInputDialog::getInteger(this, tr("Height"), tr("Height:"), 200, 0, 2147483647, 1, &ok); if(!ok) return; QString s = QFileDialog::getSaveFileName(this, "Choose a filename to save"); if(s.isEmpty()) return; QImage image(w,h,QImage::Format_RGB32); plot.begin(&image); svg_plot->renderer()->render(&plot); plot.end(); image.save(s,item->data(0).toString().toLocal8Bit().data()); } break; case PICTURE: { bool ok; int h, w = QInputDialog::getInteger(this, tr("Width"), tr("Width:"), 300, 0, 2147483647, 1, &ok); if(!ok) return; h=QInputDialog::getInteger(this, tr("Height"), tr("Height:"), 200, 0, 2147483647, 1, &ok); if(!ok) return; QString s = QFileDialog::getSaveFileName(this, "Choose a filename to save"); if(s.isEmpty()) return; QPicture image; const QRect r(0,0,w,h); image.setBoundingRect(r); plot.begin(&image); svg_plot->renderer()->render(&plot); plot.end(); image.save(s,item->data(0).toString().toLocal8Bit().data()); } break; case PRINTER: { QPrinter p; QPrintDialog printDialog(&p, this); if (printDialog.exec() != QDialog::Accepted) return; plot.begin(&p); svg_plot->renderer()->render(&plot); plot.end(); } break; case PDF: { QPrinter p; QPrintDialog printDialog(&p, this); p.setOutputFormat(QPrinter::PdfFormat); if (printDialog.exec() != QDialog::Accepted) return; plot.begin(&p); svg_plot->renderer()->render(&plot); plot.end(); } break; case SVG: { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("*.svg")); QSvgGenerator p; p.setFileName(fileName); plot.begin(&p); svg_plot->renderer()->render(&plot); plot.end(); } } }