File indexing completed on 2024-04-21 05:43:21

0001 /***************************************************************************
0002  *   Copyright (C) 2006 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
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 #ifndef DPIMAGE_H
0012 #define DPIMAGE_H
0013 
0014 #include "drawpart.h"
0015 
0016 #include <QImage>
0017 #include <QPixmap>
0018 #include <QThread>
0019 
0020 /**
0021 @short Thread to perform quick and then good image scaling.
0022 @author David Saxton
0023 */
0024 class ImageScaleThread : public QThread
0025 {
0026 public:
0027     enum BestScaling { Unscaled, NormalScaled, SmoothScaled };
0028 
0029     ImageScaleThread();
0030     /**
0031      * Use the given settings.
0032      * @return if any of the settings changed.
0033      */
0034     bool updateSettings(const QString &imageURL, int width, int height);
0035     /**
0036      * @param scaling is set to the type of scaling that this image has had.
0037      * @return the best image done so far.
0038      */
0039     QImage bestScaling(BestScaling *scaling = nullptr) const;
0040 
0041 protected:
0042     /**
0043      * Start scaling.
0044      */
0045     void run() override;
0046 
0047     QImage m_image;
0048     QImage m_normalScaled;
0049     QImage m_smoothScaled;
0050 
0051     bool m_bDoneNormalScale;
0052     bool m_bDoneSmoothScale;
0053 
0054     int m_width;
0055     int m_height;
0056     QString m_imageURL;
0057     bool m_bSettingsChanged;
0058 };
0059 
0060 /**
0061 @short Represents editable text on the canvas
0062 @author David Saxton
0063  */
0064 class DPImage : public DrawPart
0065 {
0066     Q_OBJECT
0067 public:
0068     DPImage(ItemDocument *itemDocument, bool newItem, const char *id = nullptr);
0069     ~DPImage() override;
0070 
0071     static Item *construct(ItemDocument *itemDocument, bool newItem, const char *id);
0072     static LibraryItem *libraryItem();
0073 
0074     void setSelected(bool yes) override;
0075 
0076 protected:
0077     void postResize() override;
0078 
0079 protected slots:
0080     /**
0081      * Called from a timeout event after resizing to see if the image
0082      * resizing thread has done anything useful yet.
0083      */
0084     void checkImageScaling();
0085 
0086 private:
0087     void drawShape(QPainter &p) override;
0088     void dataChanged() override;
0089 
0090     ImageScaleThread::BestScaling m_imageScaling;
0091     QPixmap m_image;
0092     ImageScaleThread m_imageScaleThread;
0093     RectangularOverlay *m_pRectangularOverlay;
0094     QTimer *m_pCheckImageScalingTimer;
0095     QString m_imageURL;
0096     bool m_bSettingsChanged;
0097 
0098     /**
0099      * If we have been loaded from a file, etc, then we want to keep the
0100      * previous size instead of resizing ourselves to the new image size
0101      * like we would do normally if the user loads an image.
0102      */
0103     bool m_bResizeToImage;
0104 };
0105 
0106 #endif