File indexing completed on 2024-04-28 04:31:54

0001 /*
0002  * Copyright (C) 2010-2015 by Stephen Allewell
0003  * steve.allewell@gmail.com
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  */
0010 
0011 /** @file
0012  * This file defines a background image to be used as an overlay on a canvas for
0013  * the purpose of tracing.
0014  */
0015 
0016 #ifndef BackgroundImage_H
0017 #define BackgroundImage_H
0018 
0019 // Qt includes
0020 #include <QIcon>
0021 #include <QImage>
0022 #include <QRect>
0023 #include <QUrl>
0024 
0025 // Forward declaration of Qt classes
0026 class QDataStream;
0027 
0028 /**
0029  * This class defines a background image including the area it occupies on the
0030  * canvas, its visibility state and its original source url.  It also stores an
0031  * icon for display in the menus and a Qimage which is scaled to fit the canvas
0032  * zoom factor.
0033  */
0034 class BackgroundImage
0035 {
0036 public:
0037     /**
0038      * Constructor to initialise an empty background image to allow streaming
0039      * from a file. Use the default implementation for this.
0040      */
0041     BackgroundImage() = default;
0042 
0043     /**
0044      * Constructor to initialise a background image from a source image file and
0045      * a target rectangle. This image defaults to being visible.
0046      *
0047      * @param url is a const reference to a QUrl for the source file
0048      * @param location is a const reference to a QRect that the background should fit into
0049      */
0050     BackgroundImage(const QUrl &url, const QRect &location);
0051 
0052     /**
0053      * Get the url of the background image.
0054      *
0055      * @return a const reference to the QUrl representing the source file URL
0056      */
0057     const QUrl &url() const;
0058 
0059     /**
0060      * Get the location occupied by the background image on the canvas.
0061      *
0062      * @return a const reference to a QRect representing the area occupied
0063      */
0064     const QRect &location() const;
0065 
0066     /**
0067      * Get the visible state of the background image.
0068      *
0069      * @return @c true if the image is visible, @c false otherwise
0070      */
0071     bool isVisible() const;
0072 
0073     /**
0074      * Get the validity status of the background image.  When the image is read
0075      * from a file, errors may occur that prevent the correct initialisation of
0076      * the class instance. This needs to be checked for after loading.
0077      *
0078      * @return @c true if the image is valid, @c false otherwise
0079      */
0080     bool isValid() const;
0081 
0082     /**
0083      * Get the QImage of the background image. This will be used to paint onto
0084      * the canvas.
0085      *
0086      * @return a const reference to a QImage representing the image
0087      */
0088     const QImage &image() const;
0089 
0090     /**
0091      * Get the QIcon of the background image. This is used in the menus to show
0092      * which image any action would apply to.
0093      *
0094      * @return a const reference to a QIcon representing the image
0095      */
0096     const QIcon &icon() const;
0097 
0098     /**
0099      * Set the target area of the canvas that the background image should occupy.
0100      *
0101      * @param location is a const reference to a QRect representing the target area
0102      */
0103     void setLocation(const QRect &location);
0104 
0105     /**
0106      * Set the visibility status of the background image to show or hide it.
0107      *
0108      * @param visible @c true to show the image, @c false to hide it
0109      */
0110     void setVisible(bool visible);
0111 
0112     /**
0113      * Operator to stream out the class instance to a QDataStream.
0114      *
0115      * @param stream is a reference to the QDataStream to write to
0116      * @param backgroundImage is a const reference to the class instance to write
0117      *
0118      * @return a reference to the QDataStream allowing chaining
0119      */
0120     friend QDataStream &operator<<(QDataStream &stream, const BackgroundImage &backgroundImage);
0121 
0122     /**
0123      * Operator to stream in the class instance from a QDataStream. The class
0124      * instance will have been created previously as an empty instance.
0125      *
0126      * @param stream is a reference to the QDataStream to read from
0127      * @param backgroundImage is a reference to the class instance to read into
0128      *
0129      * @return a reference to the QDataStream allowing chaining
0130      */
0131     friend QDataStream &operator>>(QDataStream &stream, BackgroundImage &backgroundImage);
0132 
0133 private:
0134     /**
0135      * Generate the QIcon from the image data. Used after reading from a file.
0136      */
0137     void generateIcon();
0138 
0139     static const int version = 101; /**< The version of the streamed object */
0140     // no longer store m_icon, generate it on loading
0141 
0142     QUrl m_url; /**< The URL of the source file */
0143     QRect m_location; /**< The area of the canvas occupied by the image */
0144     bool m_visible; /**< The visibility state, @c true if visible, @c false otherwise */
0145     bool m_status; /**< The validity state of the class instance, @c true if valid, @c false otherwise */
0146     QImage m_image; /**< The image read from the URL */
0147     QIcon m_icon; /**< An icon of the image */
0148 };
0149 
0150 /**
0151  * Allows the QSharedPointer<BackgroundImage> to be stored in a QAction QVariant value
0152  * for showing in the menus.
0153  */
0154 Q_DECLARE_METATYPE(QSharedPointer<BackgroundImage>)
0155 
0156 QDataStream &operator<<(QDataStream &, const BackgroundImage &);
0157 QDataStream &operator>>(QDataStream &, BackgroundImage &);
0158 
0159 #endif // BackgroundImage_H