1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include "fsimagemodel.h"
#include <QFileInfo>
#include <QImage>
#include <QDebug>
#include <QThread>
struct FsImageModelImage
{
QFileInfo info;
QImage image;
};
FsImageModel::FsImageModel(QString path,QObject * parent ):QAbstractItemModel(parent)
{
dir = QDir::current();
if(!path.isEmpty())
dir = QDir(path);
connect(&watcher,SIGNAL(directoryChanged ( const QString & )),this,SLOT(directoryChanged ( const QString & )));
connect(&watcher,SIGNAL(fileChanged ( const QString & )),this,SLOT(fileChanged ( const QString & )));
watcher.addPath(dir.absolutePath());
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
FsImageModelImage * img= new FsImageModelImage;
img->info=list.at(i);
img->image.load(dir.absoluteFilePath(img->info.fileName()));
files.append(img);
watcher.addPath(dir.absoluteFilePath(img->info.fileName()));
}
}
FsImageModel::~FsImageModel()
{
qDeleteAll(files);
}
QModelIndex FsImageModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex ( row, column, files.at(row) );
}
QModelIndex FsImageModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int FsImageModel::rowCount(const QModelIndex & parent ) const
{
if(parent.isValid())
return 0;
return files.count();
}
int FsImageModel::columnCount(const QModelIndex & parent ) const
{
return 1;
}
QVariant FsImageModel::data(const QModelIndex & index, int role ) const
{
FsImageModelImage* img=static_cast<FsImageModelImage*>(index.internalPointer());
if(role==Qt::DisplayRole)
return img->info.fileName();
if(role==Qt::DecorationRole)
return img->image;
return QVariant();
}
class Thread : public QThread
{
friend class FsImageModel;
};
void FsImageModel::directoryChanged ( const QString & path )
{
Q_ASSERT(path==dir.absolutePath());
QStringList org=watcher.files ();
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
QString path= dir.absoluteFilePath(list.at(i).fileName());
if(org.contains(path))
{
org.removeAll(path);
}
else
{
beginInsertRows (QModelIndex(),files.count(),files.count());
FsImageModelImage * img= new FsImageModelImage;
img->info=list.at(i);
int retry=100;
while(!img->image.load(path))
{
Thread::usleep(1000);
if(--retry<0)
{
break;
}
}; ///hax
files.append(img);
watcher.addPath(path);
endInsertRows ();
}
}
foreach(const QString &a, org)
fileChanged(a);
}
void FsImageModel::fileChanged ( const QString & path )
{
int i=0;
foreach(FsImageModelImage* img,files)
{
if(dir.absoluteFilePath(img->info.fileName())==path)
{
if(img->info.exists())
{
emit dataChanged ( index(i,0), index(i,0));
}
else
{
beginRemoveRows (QModelIndex(),i,i);
watcher.removePath(path);
delete files.at(i);
files.removeAt(i);
endRemoveRows ();
break;
}
break;
}
++i;
}
}
|