image_strip_scene.h 3.05 KB
Newer Older
1 2 3
/*
 *  Copyright (c) 2011 Silvio Heinrich <plassy@web.de>
 *
4
 *  This library is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU Lesser General Public License as published by
6
 *  the Free Software Foundation; version 2.1 of the License.
7
 *
8
 *  This library is distributed in the hope that it will be useful,
9 10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU Lesser General Public License for more details.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef H_IMAGE_STRIP_SCENE_H_
#define H_IMAGE_STRIP_SCENE_H_

#include <QGraphicsScene>
#include <QGraphicsWidget>
#include <QImageReader>
#include <QThread>
#include <QAtomicInt>
#include <QMutex>

28
class ImageItem;
29

30
class ImageLoader: public QThread
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
    Q_OBJECT
    
    struct Data
    {
        Data() { }
        Data(const QString& p):
            path(p), isLoaded(false) { };
        Data(const Data& d):
            image(d.image), path(d.path), isLoaded(d.isLoaded) { };
        
        QImage     image;
        QString    path;
        QAtomicInt isLoaded;
    };
    
47
Q_SIGNALS:
48
    void sigItemContentChanged(ImageItem* item);
49 50
    
public:
51
    ImageLoader(float size);
52
    
53
    void addPath(ImageItem* item, const QString& path) {
54 55 56
        m_data[item] = Data(path);
    }
    
57
    bool isImageLoaded(ImageItem* item) const {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
58
        return m_data[item].isLoaded != 0;
59 60
    }
    
61
    QImage getImage(ImageItem* item) const {
62 63
        return m_data[item].image;
    }
64

65
    virtual void run();
66

67
public Q_SLOTS:
68 69 70

    void stopExecution();

71 72
private:
    float                     m_size;
73
    QHash<ImageItem*,Data> m_data;
74 75 76
    QAtomicInt                m_run;
};

77
class ImageItem: public QGraphicsWidget
78 79
{
public:
80
    ImageItem(float size, const QString& path, ImageLoader* loader):
81 82 83 84 85 86 87 88 89 90 91
        m_size(size), m_loader(loader), m_path(path)
    {
        setFlag(QGraphicsItem::ItemIsSelectable, true);
    }
    
    const QString& path() const { return m_path; }
    
    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint=QSizeF()) const;
    virtual void   paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget=0);
    
private:
Boudewijn Rempt's avatar
Boudewijn Rempt committed
92
    float m_size;
93
    ImageLoader* m_loader;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
94
    QString m_path;
95 96
};

97
class ImageStripScene: public QGraphicsScene
98 99 100 101
{
    Q_OBJECT
    
public:
102
    ImageStripScene();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
103
    ~ImageStripScene();
104
    bool setCurrentDirectory(const QString& path);
105
    QString currentPath() { return m_path; }
106
Q_SIGNALS:
107 108 109 110 111
    void sigImageActivated(const QString& path);
    
private:
    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
    
112
private Q_SLOTS:
113
    void slotItemContentChanged(ImageItem* item);
114 115
    
private:
Boudewijn Rempt's avatar
Boudewijn Rempt committed
116 117
    float  m_imgSize;
    quint32 m_numItems;
118
    ImageLoader* m_loader;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
119
    QMutex m_mutex;
120
    QString m_path;
121 122 123
};

#endif // H_IMAGE_STRIP_SCENE_H_