Watching current dir for changes to reload file list
This commit is contained in:
+43
-28
@@ -70,6 +70,7 @@ void Image::imageLoaded(QImage img)
|
||||
|
||||
ImageRingList::ImageRingList(QObject *parent) : QObject(parent)
|
||||
{
|
||||
connect(&m_fileSystemWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(dirChanged(QString)));
|
||||
}
|
||||
|
||||
bool ImageRingList::setDir(const QString path, const QString ¤tFile)
|
||||
@@ -88,39 +89,14 @@ bool ImageRingList::setDir(const QString path, const QString ¤tFile)
|
||||
absolutePaths.append(dir.absoluteFilePath(file));
|
||||
}
|
||||
setFiles(absolutePaths, currentFile);
|
||||
|
||||
m_fileSystemWatcher.removePaths(m_fileSystemWatcher.directories());
|
||||
m_fileSystemWatcher.addPath(path);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImageRingList::setFiles(const QStringList files, const QString ¤tFile)
|
||||
{
|
||||
m_images.clear();
|
||||
foreach(const QString &file, files)
|
||||
{
|
||||
ImagePtr ptr = make_shared<Image>(file);
|
||||
connect(ptr.get(), SIGNAL(pixmapLoaded(Image*)), this, SLOT(imageLoaded(Image*)));
|
||||
m_images.append(ptr);
|
||||
}
|
||||
|
||||
int index = files.indexOf(currentFile);
|
||||
if(index < 0)
|
||||
index = 0;
|
||||
|
||||
m_firstImage = m_currImage = m_lastImage = m_images.begin()+index;
|
||||
(*m_currImage)->load();
|
||||
|
||||
m_width = DEFAULT_WIDTH<m_images.size()/2 ? DEFAULT_WIDTH : m_images.size()/2;
|
||||
|
||||
for(int i=0; i<m_width; i++)
|
||||
{
|
||||
m_firstImage = decrement(m_firstImage);
|
||||
(*m_firstImage)->load();
|
||||
m_lastImage = increment(m_lastImage);
|
||||
(*m_lastImage)->load();
|
||||
}
|
||||
}
|
||||
|
||||
void ImageRingList::setFile(const QString &file)
|
||||
{
|
||||
QFileInfo info(file);
|
||||
@@ -161,6 +137,34 @@ void ImageRingList::decrement()
|
||||
}
|
||||
}
|
||||
|
||||
void ImageRingList::setFiles(const QStringList files, const QString ¤tFile)
|
||||
{
|
||||
m_images.clear();
|
||||
foreach(const QString &file, files)
|
||||
{
|
||||
ImagePtr ptr = make_shared<Image>(file);
|
||||
connect(ptr.get(), SIGNAL(pixmapLoaded(Image*)), this, SLOT(imageLoaded(Image*)));
|
||||
m_images.append(ptr);
|
||||
}
|
||||
|
||||
int index = files.indexOf(currentFile);
|
||||
if(index < 0)
|
||||
index = 0;
|
||||
|
||||
m_firstImage = m_currImage = m_lastImage = m_images.begin()+index;
|
||||
(*m_currImage)->load();
|
||||
|
||||
m_width = DEFAULT_WIDTH<m_images.size()/2 ? DEFAULT_WIDTH : m_images.size()/2;
|
||||
|
||||
for(int i=0; i<m_width; i++)
|
||||
{
|
||||
m_firstImage = decrement(m_firstImage);
|
||||
(*m_firstImage)->load();
|
||||
m_lastImage = increment(m_lastImage);
|
||||
(*m_lastImage)->load();
|
||||
}
|
||||
}
|
||||
|
||||
QList<ImagePtr>::iterator ImageRingList::increment(QList<ImagePtr>::iterator iter)
|
||||
{
|
||||
iter++;
|
||||
@@ -184,3 +188,14 @@ void ImageRingList::imageLoaded(Image *image)
|
||||
emit pixmapLoaded(image->pixmap());
|
||||
}
|
||||
}
|
||||
|
||||
void ImageRingList::dirChanged(QString dir)
|
||||
{
|
||||
QString currentFile;
|
||||
|
||||
if(m_images.size())
|
||||
currentFile = (*m_currImage)->name();
|
||||
|
||||
setDir(dir, currentFile);
|
||||
emit currentImageChanged();
|
||||
}
|
||||
|
||||
+4
-1
@@ -36,21 +36,24 @@ class ImageRingList : public QObject
|
||||
QList<ImagePtr>::iterator m_firstImage;
|
||||
QList<ImagePtr>::iterator m_currImage;
|
||||
QList<ImagePtr>::iterator m_lastImage;
|
||||
QFileSystemWatcher m_fileSystemWatcher;
|
||||
public:
|
||||
explicit ImageRingList(QObject *parent = 0);
|
||||
bool setDir(const QString path, const QString ¤tFile = QString());
|
||||
void setFiles(const QStringList files, const QString ¤tFile = QString());
|
||||
void setFile(const QString &file);
|
||||
ImagePtr currentImage();
|
||||
void increment();
|
||||
void decrement();
|
||||
protected:
|
||||
void setFiles(const QStringList files, const QString ¤tFile = QString());
|
||||
QList<ImagePtr>::iterator increment(QList<ImagePtr>::iterator iter);
|
||||
QList<ImagePtr>::iterator decrement(QList<ImagePtr>::iterator iter);
|
||||
signals:
|
||||
void pixmapLoaded(QPixmap pix);
|
||||
void currentImageChanged();
|
||||
protected slots:
|
||||
void imageLoaded(Image *image);
|
||||
void dirChanged(QString dir);
|
||||
};
|
||||
|
||||
#endif // IMAGERINGLIST_H
|
||||
|
||||
+2
-1
@@ -20,6 +20,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
|
||||
|
||||
m_ringList = new ImageRingList(this);
|
||||
connect(m_ringList, SIGNAL(pixmapLoaded(QPixmap)), this, SLOT(pixmapLoaded(QPixmap)));
|
||||
connect(m_ringList, SIGNAL(currentImageChanged()), this, SLOT(updateWindowTitle()));
|
||||
|
||||
QMenu *fileMenu = new QMenu(tr("File"), this);
|
||||
fileMenu->addAction(tr("Open"), this, SLOT(openFile()), QKeySequence("Ctrl+O"));
|
||||
@@ -90,7 +91,7 @@ void MainWindow::openFile()
|
||||
if(standardLocations.size())
|
||||
path = standardLocations.first();
|
||||
|
||||
QString file = QFileDialog::getOpenFileName(this, tr("Open file"), path, tr("Images (*.jpg *.png)"));
|
||||
QString file = QFileDialog::getOpenFileName(this, tr("Open file"), path, tr("Images (*.jpg *.png *.JPG *.PNG)"));
|
||||
if(!file.isEmpty())
|
||||
{
|
||||
QFileInfo info(file);
|
||||
|
||||
+1
-1
@@ -20,8 +20,8 @@ public:
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event);
|
||||
void keyReleaseEvent(QKeyEvent *event);
|
||||
void updateWindowTitle();
|
||||
protected slots:
|
||||
void updateWindowTitle();
|
||||
void pixmapLoaded(QPixmap pix);
|
||||
void openFile();
|
||||
void markImage();
|
||||
|
||||
Reference in New Issue
Block a user