//---->filename qimages.cpp#include "getfile.h"
#include "qimages.h"
#include <QtGui>
void MyWindow::paint(const std::string &str,
const QPixmap image,
const int num)
{
static char ch[100];
sprintf(ch, "The %d'th file", num);
label.move(0,160);
label.setText(ch);
update();
QPainter painter(this);
painter.setPen(Qt::blue);
painter.drawText(100,20,QString(str.c_str()));
painter.drawPixmap(100,40,image);
}
void MyWindow::auto_display()
{
image.load(name_itr->c_str());
if(image.isNull()){
files.erase(name_itr);
}
if(++name_itr == files.end()){
name_itr = files.begin();
picture_num = 0;
}
paint(*name_itr, image,picture_num++);
}
void MyWindow::timer_control()
{
static bool auto_display_button_pushed = false;
if(auto_display_button_pushed == true){
auto_display_button_pushed = false;
}else{
auto_display_button_pushed = true;
}
if(auto_display_button_pushed){
timer.start(1000);
}else{
timer.stop();
}
}
void MyWindow::paintEvent(QPaintEvent *)
{
image.load(name_itr->c_str());
if(image.isNull()){
std::cout<<"this is not a picture"<<std::endl;
files.erase(name_itr);
}else{
}
if(picture_num == 0) picture_num++;
paint(*name_itr, image, picture_num);
}
void MyWindow::mousePressEvent(QMouseEvent * e ){
if(e->x() > 100 && e->y() > 30){
QPainter painter(this);
image.load(name_itr->c_str());
if(image.isNull()){
std::cout<<"this is not a picture"<<std::endl;
}
if(++name_itr == files.end()){
name_itr = files.begin();
picture_num = 0;
}
paint(*name_itr, image, picture_num++);
}
}
void MyWindow::dirChanged(QString out)
{
if("" != out){
QDir dirPath(out);
QStringList filters;
filters.clear();
filters<<"*.png"<<"*.bmp"<<"*jpg";
dirPath.setNameFilters(filters);
QStringList imageList = dirPath.entryList();
files.erase(files.begin(), files.end());
for(QStringList::const_iterator itmp = imageList.begin();
itmp != imageList.end(); ++itmp){
files.push_back(itmp->toStdString());
}
for(std::vector<std::string>::iterator itr = files.begin();
itr != files.end(); ++itr){
std::cout<<*itr<<" ";
}
myShow();
}
}
void MyWindow::myShow()
{
name_itr = files.begin();
image.load(name_itr->c_str());
if(image.isNull()){
std::cout<<"this is not a picture"<<std::endl;
files.erase(name_itr);
}else{
}
paint(*name_itr, image, picture_num);
}
//---->filename qimages.h
#ifndef QIMAGES_H
#define QIMAGES_H
#include <cstdlib>
#include <vector>
#include <iterator>
#include <QtGui>
class MyWindow : public QWidget
{
Q_OBJECT
public:
MyWindow(const std::vector<std::string> & );
~MyWindow();
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
void myShow();
void paint(const std::string &str,
const QPixmap image,
const int num);
private:
std::vector<std::string> files;
typedef std::vector<std::string>::iterator VSI;
VSI name_itr;
QPixmap image;
QPushButton full_screen_button;
QPushButton normal_screen_button;
QPushButton auto_display_button;
QPushButton open_button;
QFileDialog dialog;
QTimer timer;
QLabel label;
int picture_num;
private slots:
void dirChanged(QString);
void timer_control();
void auto_display();
virtual void repaint(){
update();
this->paintEvent(0);
}
};
inline MyWindow::MyWindow(const std::vector<std::string> &names)
:QWidget(0),files(names),
name_itr(files.begin()),
full_screen_button("full screen", this),
normal_screen_button("normal screen", this),
auto_display_button("auto play",this),
open_button("Open",this),
dialog(this),
timer(this),
label(this),
picture_num( 0)
{
image.load((*name_itr).c_str() );
full_screen_button.setGeometry(0,0,100,30);
full_screen_button.setFont(QFont("Times", 12, QFont::Bold));
connect(&full_screen_button, SIGNAL(clicked()),
this, SLOT(showFullScreen()) );
normal_screen_button.setGeometry(0,40,100,30);
normal_screen_button.setFont(QFont("Times", 12, QFont::Bold));
connect(&normal_screen_button, SIGNAL(clicked()),
this, SLOT(showNormal()) );
auto_display_button.setGeometry(0,80,100,30);
auto_display_button.setFont(QFont("Times", 12, QFont::Bold));
connect(&auto_display_button, SIGNAL(clicked()),
this, SLOT(timer_control()) );
open_button.setGeometry(0,120,100,30);
open_button.setFont(QFont("Times", 12, QFont::Bold));
connect(&open_button, SIGNAL(clicked()),
&dialog, SLOT(show()) );
connect(&timer, SIGNAL(timeout()),
this, SLOT(auto_display()) );
dialog.setFileMode(QFileDialog::Directory);
connect(&dialog, SIGNAL(currentChanged(QString)),
this, SLOT(dirChanged(QString)) );
label.setGeometry(0,160,100,30);
}
inline MyWindow::~MyWindow(){
files.~vector();
name_itr.~VSI();
image.~QPixmap();
}
#endif //QIMAGES_H
//---->filename getfile.h
#ifndef GETFILE_H
#define GETFILE_H
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> getfile();
#endif //GETFILE_H
//---->filename getfile.cpp
#include "getfile.h"
#include <QtGui>
std::vector<std::string> getfile()
{
std::vector<std::string> vs;
QDir dirPath(QDir::currentPath());
QStringList filters;
filters<<"*.png"<<"*.mmp"<<"*.jpg";
dirPath.setNameFilters(filters);
QStringList file_name_list = dirPath.entryList();
for(QStringList::const_iterator it = file_name_list.begin();
it != file_name_list.end(); ++it){
vs.push_back(it->toStdString());
}
return vs;
}
//---->filename drive.cpp
#include "qimages.h"
#include "getfile.h"
#include <algorithm>
#include <qtextcodec.h>
template<class Type>
class print{
public:
void operator()(Type & str){
std::cout<<str<<std::endl;
}
};
int main(int argc, char *argv[])
{
// QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QApplication a(argc, argv);
std::vector<std::string> names = getfile();
if (-1 == system("rm -rf LIST")) {exit(1);}
std::for_each(names.begin(), names.end(),print<std::string>());
MyWindow w(names);
w.setGeometry(0,0,800,600);
w.show();
return a.exec();
}