From kde-commits Sat Jun 30 22:54:26 2007 From: =?utf-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Sat, 30 Jun 2007 22:54:26 +0000 To: kde-commits Subject: KDE/kdegraphics/gwenview Message-Id: <1183244066.539403.1964.nullmailer () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=118324407510189 SVN commit 681947 by gateau: Finished implementation of Document::modified property. M +1 -1 lib/abstractdocumentimpl.cpp M +2 -0 lib/abstractdocumentimpl.h M +23 -2 lib/document.cpp M +14 -1 lib/document.h M +5 -0 lib/documentloadedimpl.cpp M +1 -0 lib/documentloadedimpl.h M +4 -0 lib/emptydocumentimpl.cpp M +1 -0 lib/emptydocumentimpl.h M +8 -0 lib/jpegdocumentloadedimpl.cpp M +1 -0 lib/jpegdocumentloadedimpl.h M +6 -1 lib/loadingdocumentimpl.cpp M +1 -0 lib/loadingdocumentimpl.h M +20 -0 tests/documenttest.cpp M +1 -0 tests/documenttest.h --- trunk/KDE/kdegraphics/gwenview/lib/abstractdocumentimpl.cpp #681946:681947 @@ -57,7 +57,7 @@ void AbstractDocumentImpl::setDocumentImage(const QImage& image) { - d->mDocument->setImage(image); + d->mDocument->setImageInternal(image); } --- trunk/KDE/kdegraphics/gwenview/lib/abstractdocumentimpl.h #681946:681947 @@ -53,6 +53,8 @@ virtual Document::SaveResult save(const KUrl&, const QByteArray& format) = 0; + virtual void setImage(const QImage&) = 0; + Q_SIGNALS: void loaded(); --- trunk/KDE/kdegraphics/gwenview/lib/document.cpp #681946:681947 @@ -38,6 +38,7 @@ KUrl mUrl; QImage mImage; QByteArray mFormat; + bool mModified; }; @@ -45,6 +46,7 @@ : QObject() , d(new DocumentPrivate) { d->mImpl = new EmptyDocumentImpl(this); + d->mModified = false; } @@ -83,6 +85,16 @@ void Document::setImage(const QImage& image) { + // Don't init mImage directly, because: + // - This should not be called until document has finished loading. + // - Some impl will want to do special stuff (ex: jpegloaded implementation will + // switch to loaded implementation since it won't hold valid raw data + // anymore) + d->mImpl->setImage(image); +} + + +void Document::setImageInternal(const QImage& image) { d->mImage = image; } @@ -95,7 +107,12 @@ while (!isLoaded()) { qApp->processEvents(QEventLoop::ExcludeUserInputEvents); } - return d->mImpl->save(url, format.toAscii()); + Document::SaveResult result = d->mImpl->save(url, format.toAscii()); + if (result == SR_OK) { + d->mModified = false; + } + + return result; } QByteArray Document::format() const { @@ -107,7 +124,11 @@ } bool Document::isModified() const { - return false; + return d->mModified; } +void Document::setModified(bool modified) { + d->mModified = modified; +} + } // namespace --- trunk/KDE/kdegraphics/gwenview/lib/document.h #681946:681947 @@ -57,8 +57,21 @@ bool isModified() const; + /** + * Mark the image as modified. Should be called when image pixels have been + * altered outside Document. + */ + void setModified(bool modified); + QImage& image(); + /** + * Replaces the current image with image. + * Calling this while the document is loaded won't do anything. + * isModified() will return true after this. + */ + void setImage(const QImage& image); + KUrl url() const; SaveResult save(const KUrl& url, const QString& format); @@ -72,7 +85,7 @@ friend class DocumentFactory; friend class AbstractDocumentImpl; - void setImage(const QImage&); + void setImageInternal(const QImage&); void setFormat(const QByteArray&); void switchToImpl(AbstractDocumentImpl* impl); --- trunk/KDE/kdegraphics/gwenview/lib/documentloadedimpl.cpp #681946:681947 @@ -84,4 +84,9 @@ } +void DocumentLoadedImpl::setImage(const QImage& image) { + setDocumentImage(image); + document()->setModified(true); +} + } // namespace --- trunk/KDE/kdegraphics/gwenview/lib/documentloadedimpl.h #681946:681947 @@ -42,6 +42,7 @@ virtual void init(); virtual bool isLoaded() const; virtual Document::SaveResult save(const KUrl&, const QByteArray& format); + virtual void setImage(const QImage&); protected: virtual bool saveInternal(QIODevice* device, const QByteArray& format); --- trunk/KDE/kdegraphics/gwenview/lib/emptydocumentimpl.cpp #681946:681947 @@ -49,4 +49,8 @@ return Document::SR_OtherError; } +void EmptyDocumentImpl::setImage(const QImage&) { + // Don't do anything for now, but we could imagine switching to loaded impl +} + } // namespace --- trunk/KDE/kdegraphics/gwenview/lib/emptydocumentimpl.h #681946:681947 @@ -38,6 +38,7 @@ virtual void init(); virtual bool isLoaded() const; virtual Document::SaveResult save(const KUrl&, const QByteArray& format); + virtual void setImage(const QImage&); }; --- trunk/KDE/kdegraphics/gwenview/lib/jpegdocumentloadedimpl.cpp #681946:681947 @@ -57,4 +57,12 @@ } } + +void JpegDocumentLoadedImpl::setImage(const QImage& image) { + DocumentLoadedImpl::setImage(image); + // mData is no longer relevant, so we'd better switch to a normal loaded + // impl + switchToImpl(new DocumentLoadedImpl(document())); +} + } // namespace --- trunk/KDE/kdegraphics/gwenview/lib/jpegdocumentloadedimpl.h #681946:681947 @@ -38,6 +38,7 @@ public: JpegDocumentLoadedImpl(Document*, const QByteArray& data); ~JpegDocumentLoadedImpl(); + virtual void setImage(const QImage&); protected: virtual bool saveInternal(QIODevice* device, const QByteArray& format); --- trunk/KDE/kdegraphics/gwenview/lib/loadingdocumentimpl.cpp #681946:681947 @@ -31,7 +31,8 @@ #include // KDE -#include +#include +#include // Local #include "document.h" @@ -149,4 +150,8 @@ return Document::SR_OtherError; } +void LoadingDocumentImpl::setImage(const QImage&) { + kWarning() << k_funcinfo << " should not be called\n"; +} + } // namespace --- trunk/KDE/kdegraphics/gwenview/lib/loadingdocumentimpl.h #681946:681947 @@ -41,6 +41,7 @@ virtual void init(); virtual bool isLoaded() const; virtual Document::SaveResult save(const KUrl&, const QByteArray& format); + virtual void setImage(const QImage&); private Q_SLOTS: void slotImageLoaded(); --- trunk/KDE/kdegraphics/gwenview/tests/documenttest.cpp #681946:681947 @@ -122,3 +122,23 @@ QByteArray resultData = resultFile.readAll(); QCOMPARE(originalData, resultData); } + +void DocumentTest::testModify() { + KUrl url("orient6.jpg"); + Document::Ptr doc = DocumentFactory::instance()->load(url); + while (!doc->isLoaded()) { + QTest::qWait(30); + } + QVERIFY(!doc->isModified()); + + QImage image(10, 10, QImage::Format_ARGB32); + image.fill(QColor(Qt::white).rgb()); + + doc->setImage(image); + QVERIFY(doc->isModified()); + + KUrl destUrl(QDir::currentPath() + "/modify.png"); + QCOMPARE(doc->save(destUrl, "png"), Document::SR_OK); + + QVERIFY(!doc->isModified()); +} --- trunk/KDE/kdegraphics/gwenview/tests/documenttest.h #681946:681947 @@ -30,4 +30,5 @@ void testMultipleLoads(); void testSave(); void testLosslessSave(); + void testModify(); };