void image::sSave() { XSqlQuery newImage; if (__image.isNull()) { QMessageBox::warning(this, tr("No Image Specified"), tr("You must load an image before you may save this record.") ); return; } if (_mode == cNew) { XSqlQuery imageid("SELECT NEXTVAL('image_image_id_seq') AS _image_id"); if (imageid.first()) _imageid = imageid.value("_image_id").toInt(); // ToDo QImageWriter imageIo; QBuffer imageBuffer; QString imageString; imageBuffer.open(QIODevice::ReadWrite); imageIo.setDevice(&imageBuffer); imageIo.setFormat("PNG"); if (!imageIo.write(__image)) { QMessageBox::critical(this, tr("Error Saving Image"), tr("There was an error trying to save the image.") ); return; } imageBuffer.close(); imageString = QUUEncode(imageBuffer); newImage.prepare( "INSERT INTO image " "(image_id, image_name, image_descrip, image_data) " "VALUES " "(:image_id, :image_name, :image_descrip, :image_data);" ); newImage.bindValue(":image_id", _imageid); newImage.bindValue(":image_name", _name->text()); newImage.bindValue(":image_descrip", _descrip->toPlainText()); newImage.bindValue(":image_data", imageString); } else if (_mode == cEdit) { newImage.prepare( "UPDATE image " "SET image_name=:image_name, image_descrip=:image_descrip " "WHERE (image_id=:image_id);" ); newImage.bindValue(":image_id", _imageid); newImage.bindValue(":image_name", _name->text()); newImage.bindValue(":image_descrip", _descrip->toPlainText()); } newImage.exec(); done(_imageid); }
void imageview::sSave() { XSqlQuery newImage; if (_mode == cNew) { if (!__imageview.isNull()) { XSqlQuery imageid("SELECT NEXTVAL('image_image_id_seq') AS _image_id"); if (imageid.first()) _imageviewid = imageid.value("_image_id").toInt(); // ToDo QImageWriter imageIo; QBuffer imageBuffer; QString imageString; imageBuffer.open(QIODevice::ReadWrite); imageIo.setDevice(&imageBuffer); imageIo.setFormat("PNG"); if (!imageIo.write(__imageview)) { // ToDo - should issue an error here reject(); return; } imageBuffer.close(); imageString = QUUEncode(imageBuffer); newImage.prepare( "INSERT INTO image " "(image_id, image_name, image_descrip, image_data) " "VALUES " "(:image_id, :image_name, :image_descrip, :image_data);" ); newImage.bindValue(":image_id", _imageviewid); newImage.bindValue(":image_name", _name->text()); newImage.bindValue(":image_descrip", _descrip->toPlainText()); newImage.bindValue(":image_data", imageString); } } else if (_mode == cEdit) { newImage.prepare( "UPDATE image " "SET image_name=:image_name, image_descrip=:image_descrip " "WHERE (image_id=:image_id);" ); newImage.bindValue(":image_id", _imageviewid); newImage.bindValue(":image_name", _name->text()); newImage.bindValue(":image_descrip", _descrip->toPlainText()); } newImage.exec(); done(_imageviewid); }
static QByteArray randomJPEGByteArray(int w=512, int h=512, int q=85 ) { QImage image=randomImage(w,h); QBuffer bu; bu.open(QBuffer::WriteOnly); QImageWriter iw; iw.setFormat("jpeg"); iw.setDevice(&bu); iw.setOptimizedWrite(true ); iw.setQuality(q); iw.setCompression(9); iw.setProgressiveScanWrite(true ); iw.write( image ); bu.close(); QByteArray ba=bu.buffer(); //utility::byteArrayToFile("random_test.jpeg",ba ); //qDebug()<<"Random JPEG image of size "<<w<<"x"<<h<<" was "<< utility::humanReadableSize(ba.size())<< "("<<(qreal)ba.size()/((qreal)w*h) << " bytes/pixel)"; return ba; }
int LoadImage::writeToDB(const QByteArray &pdata, const QString pkgname, QString &errMsg) { if (pdata.isEmpty()) { errMsg = TR("<font color=orange>The image %1 is empty.</font>") .arg(_name); return -2; } QString encodeddata; if (DEBUG) qDebug("LoadImage::writeToDB(): image starts with %s", pdata.left(10).data()); if (QString(pdata.left(pdata.indexOf("\n"))).contains(QRegExp("^\\s*begin \\d+ \\S+"))) { if (DEBUG) qDebug("LoadImage::writeToDB() image is already uuencoded"); encodeddata = pdata; } else { // there's just GOT to be a better way to do this QImageWriter imageIo; QBuffer imageBuffer; imageBuffer.open(QIODevice::ReadWrite); imageIo.setDevice(&imageBuffer); imageIo.setFormat(_filename.right(_filename.size() - _filename.lastIndexOf(".") - 1).toAscii()); if (DEBUG) qDebug("LoadImage::writeToDB() image has format %s", imageIo.format().data()); QImage image; image.loadFromData(pdata); if (!imageIo.write(image)) { errMsg = TR("<font color=orange>Error processing image %1: " "<br>%2</font>") .arg(_name).arg(imageIo.errorString()); return -3; } imageBuffer.close(); encodeddata = QUUEncode(imageBuffer); if (DEBUG) qDebug("LoadImage::writeToDB() image was uuencoded: %s", qPrintable(encodeddata.left(160))); } QSqlQuery select; QSqlQuery upsert; int imageid = -1; int pkgheadid = -1; int pkgitemid = -1; if (pkgname.isEmpty()) select.prepare(QString("SELECT image_id, -1, -1" " FROM %1image " " WHERE (image_name=:name);") .arg(_system ? "" : "pkg")); else select.prepare(_pkgitemQueryStr); select.bindValue(":name", _name); select.bindValue(":pkgname", pkgname); select.bindValue(":grade", _grade); select.bindValue(":type", _pkgitemtype); select.exec(); if(select.first()) { imageid = select.value(0).toInt(); pkgheadid = select.value(1).toInt(); pkgitemid = select.value(2).toInt(); } else if (select.lastError().type() != QSqlError::NoError) { QSqlError err = select.lastError(); errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText()); return -5; } if (imageid >= 0) upsert.prepare(QString("UPDATE %1image " " SET image_data=:source," " image_descrip=:notes " " WHERE (image_id=:id); ") .arg(_system ? "" : "pkg")); else { upsert.prepare("SELECT NEXTVAL('image_image_id_seq');"); upsert.exec(); if (upsert.first()) imageid = upsert.value(0).toInt(); else if (upsert.lastError().type() != QSqlError::NoError) { QSqlError err = upsert.lastError(); errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText()); return -6; } upsert.prepare(QString("INSERT INTO %1image " "(image_id, image_name, image_data, image_descrip) " "VALUES (:id, :name, :source, :notes);") .arg(_system ? "" : "pkg")); } upsert.bindValue(":id", imageid); upsert.bindValue(":source", encodeddata); upsert.bindValue(":notes", _comment); upsert.bindValue(":name", _name); if (!upsert.exec()) { QSqlError err = upsert.lastError(); errMsg = _sqlerrtxt.arg(_filename).arg(err.driverText()).arg(err.databaseText()); return -7; } if (pkgheadid >= 0) { int tmp = upsertPkgItem(pkgitemid, pkgheadid, imageid, errMsg); if (tmp < 0) return tmp; } return imageid; }
int LoadImage::writeToDB(const QByteArray &pdata, const QString pkgname, QString &errMsg) { if (pdata.isEmpty()) { errMsg = TR("<font color=orange>The image %1 is empty.</font>") .arg(_name); return -2; } QByteArray encodeddata; if (DEBUG) qDebug("LoadImage::writeToDB(): image starts with %s", pdata.left(10).data()); if (QString(pdata.left(pdata.indexOf("\n"))).contains(QRegExp("^\\s*begin \\d+ \\S+"))) { if (DEBUG) qDebug("LoadImage::writeToDB() image is already uuencoded"); encodeddata = pdata; } else { // there's just GOT to be a better way to do this QImageWriter imageIo; QBuffer imageBuffer; imageBuffer.open(QIODevice::ReadWrite); imageIo.setDevice(&imageBuffer); imageIo.setFormat(_filename.right(_filename.size() - _filename.lastIndexOf(".") - 1).toAscii()); if (DEBUG) qDebug("LoadImage::writeToDB() image has format %s", imageIo.format().data()); QImage image; image.loadFromData(pdata); if (!imageIo.write(image)) { errMsg = TR("<font color=orange>Error processing image %1: " "<br>%2</font>") .arg(_name).arg(imageIo.errorString()); return -3; } imageBuffer.close(); encodeddata = QUUEncode(imageBuffer).toAscii(); if (DEBUG) qDebug("LoadImage::writeToDB() image was uuencoded: %s", encodeddata.left(160).data()); } _selectMql = new MetaSQLQuery("SELECT image_id, -1, -1" " FROM <? literal(\"tablename\") ?> " " WHERE (image_name=<? value(\"name\") ?>);"); _updateMql = new MetaSQLQuery("UPDATE <? literal(\"tablename\") ?> " " SET image_data=<? value(\"source\") ?>," " image_descrip=<? value(\"notes\") ?> " " WHERE (image_id=<? value(\"id\") ?>) " "RETURNING image_id AS id;"); _insertMql = new MetaSQLQuery("INSERT INTO <? literal(\"tablename\") ?> (" " image_id, image_name, image_data, image_descrip" ") VALUES (" " DEFAULT, <? value(\"name\") ?>," " <? value(\"source\") ?>, <? value(\"notes\") ?>) " "RETURNING image_id AS id;"); ParameterList params; params.append("tablename", "image"); return Loadable::writeToDB(encodeddata, pkgname, errMsg, params); }
QVariant QXcbMime::mimeConvertToFormat(QXcbConnection *connection, xcb_atom_t a, const QByteArray &data, const QString &format, QVariant::Type requestedType, const QByteArray &encoding) { QString atomName = mimeAtomToString(connection, a); // qDebug() << "mimeConvertDataToFormat" << format << atomName << data; if (!encoding.isEmpty() && atomName == format + QLatin1String(";charset=") + QString::fromLatin1(encoding)) { #ifndef QT_NO_TEXTCODEC if (requestedType == QVariant::String) { QTextCodec *codec = QTextCodec::codecForName(encoding); if (codec) return codec->toUnicode(data); } #endif return data; } // special cases for string types if (format == QLatin1String("text/plain")) { if (a == connection->atom(QXcbAtom::UTF8_STRING)) return QString::fromUtf8(data); if (a == XCB_ATOM_STRING || a == connection->atom(QXcbAtom::TEXT)) return QString::fromLatin1(data); } // special case for uri types if (format == QLatin1String("text/uri-list")) { if (atomName == QLatin1String("text/x-moz-url")) { // we expect this as utf16 <url><space><title> // the first part is a url that should only contain ascci char // so it should be safe to check that the second char is 0 // to verify that it is utf16 if (data.size() > 1 && data.at(1) == 0) return QString::fromRawData((const QChar *)data.constData(), data.size() / 2).split(QLatin1Char('\n')).first().toLatin1(); } } if (atomName == format) return data; #if 0 // ### // special case for images if (format == QLatin1String("image/ppm")) { if (a == XCB_ATOM_PIXMAP && data.size() == sizeof(Pixmap)) { Pixmap xpm = *((Pixmap*)data.data()); if (!xpm) return QByteArray(); Window root; int x; int y; uint width; uint height; uint border_width; uint depth; XGetGeometry(display, xpm, &root, &x, &y, &width, &height, &border_width, &depth); XImage *ximg = XGetImage(display,xpm,x,y,width,height,AllPlanes,depth==1 ? XYPixmap : ZPixmap); QImage qimg = QXlibStatic::qimageFromXImage(ximg); XDestroyImage(ximg); QImageWriter imageWriter; imageWriter.setFormat("PPMRAW"); QBuffer buf; buf.open(QIODevice::WriteOnly); imageWriter.setDevice(&buf); imageWriter.write(qimg); return buf.buffer(); } } #endif return QVariant(); }