File indexing completed on 2024-12-22 04:17:38
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "pictureitem.h" 0014 0015 #include "debug.h" 0016 #include "dialogdefaults.h" 0017 0018 #include <QDebug> 0019 #include <QFileDialog> 0020 #include <QGraphicsItem> 0021 #include <QGraphicsScene> 0022 #include <QBuffer> 0023 #include <QImageReader> 0024 0025 namespace Kst { 0026 0027 PictureItem::PictureItem(View *parent, const QImage &image) 0028 : ViewItem(parent) { 0029 if (!image.isNull()) { 0030 _image = QPixmap::fromImage(image); 0031 } 0032 setTypeName(tr("Picture", "a picture or an image")); 0033 setLockAspectRatio(true); 0034 //setLockAspectRatioFixed(true); 0035 } 0036 0037 0038 PictureItem::~PictureItem() { 0039 } 0040 0041 0042 void PictureItem::save(QXmlStreamWriter &xml) { 0043 if (isVisible()) { 0044 xml.writeStartElement("picture"); 0045 ViewItem::save(xml); 0046 xml.writeStartElement("data"); 0047 QByteArray qba; 0048 QBuffer buffer(&qba); 0049 buffer.open(QIODevice::WriteOnly); 0050 _image.toImage().save(&buffer, "PNG"); // writes image into ba in PNG format 0051 xml.writeCharacters(qCompress(qba).toBase64()); 0052 xml.writeEndElement(); 0053 xml.writeEndElement(); 0054 } 0055 } 0056 0057 0058 void PictureItem::setImage(const QImage &image) 0059 { 0060 _image = QPixmap::fromImage(image); 0061 } 0062 0063 0064 void PictureItem::paint(QPainter *painter) { 0065 // We can do better here. Cache the scaled pixmap also. 0066 if (!_image.isNull() && rect().isValid()) { 0067 const qreal w = pen().widthF(); 0068 painter->drawPixmap(rect().adjusted(w, w, -w, -w), _image, _image.rect()); 0069 } 0070 } 0071 0072 0073 void CreatePictureCommand::createItem() { 0074 QString start_dir = dialogDefaults().value("picture/startdir", ".").toString(); 0075 QString filter = "Images ("; 0076 QList<QByteArray> formats = QImageReader::supportedImageFormats (); 0077 for (int i=0; i<formats.size(); i++) { 0078 filter += " *."+QString(formats.at(i)).toUpper() + " *." + QString(formats.at(i)).toLower(); 0079 } 0080 filter += ')'; 0081 QString file = QFileDialog::getOpenFileName(_view, tr("Kst: Open Image"), start_dir, filter); 0082 if (file.isEmpty()) 0083 return; 0084 dialogDefaults().setValue("picture/startdir", QFileInfo(file).path()); 0085 _item = new PictureItem(_view, QImage(file)); 0086 _view->setCursor(Qt::CrossCursor); 0087 0088 CreateCommand::createItem(); 0089 } 0090 0091 void PictureItem::creationPolygonChanged(View::CreationEvent event) { 0092 0093 double aspect = 1.0; 0094 if ((_image.width()>0) && (_image.height()>0)) { 0095 aspect = double(_image.width())/double(_image.height()); 0096 } 0097 0098 creationPolygonChangedFixedAspect(event, aspect); 0099 0100 } 0101 0102 0103 PictureItemFactory::PictureItemFactory() 0104 : GraphicsFactory() { 0105 registerFactory("picture", this); 0106 } 0107 0108 0109 PictureItemFactory::~PictureItemFactory() { 0110 } 0111 0112 0113 ViewItem* PictureItemFactory::generateGraphics(QXmlStreamReader& xml, ObjectStore *store, View *view, ViewItem *parent) { 0114 PictureItem *rc = 0; 0115 while (!xml.atEnd()) { 0116 bool validTag = true; 0117 if (xml.isStartElement()) { 0118 if (!rc && xml.name().toString() == "picture") { 0119 Q_ASSERT(!rc); 0120 rc = new PictureItem(view); 0121 if (parent) { 0122 rc->setParentViewItem(parent); 0123 } 0124 // Add any new specialized PictureItem Properties here. 0125 } else if (xml.name().toString() == "data") { 0126 Q_ASSERT(rc); 0127 xml.readNext(); 0128 QImage loadedImage; 0129 QByteArray qbca = QByteArray::fromBase64(xml.text().toString().toLatin1()); 0130 loadedImage.loadFromData(qUncompress(qbca)); 0131 rc->setImage(loadedImage); 0132 xml.readNext(); 0133 if (!xml.isEndElement() || (xml.name().toString() != "data")) { 0134 validTag = false; 0135 } 0136 xml.readNext(); 0137 } else { 0138 Q_ASSERT(rc); 0139 if (!rc->parse(xml, validTag) && validTag) { 0140 ViewItem *i = GraphicsFactory::parse(xml, store, view, rc); 0141 if (!i) { 0142 } 0143 } 0144 } 0145 } else if (xml.isEndElement()) { 0146 if (xml.name().toString() == "picture") { 0147 break; 0148 } else { 0149 validTag = false; 0150 } 0151 } 0152 if (!validTag) { 0153 qDebug("invalid Tag\n"); 0154 Debug::self()->log(QObject::tr("Error creating picture object from Kst file."), Debug::Warning); 0155 delete rc; 0156 return 0; 0157 } 0158 xml.readNext(); 0159 } 0160 return rc; 0161 } 0162 0163 } 0164 0165 // vim: ts=2 sw=2 et