diff --git a/Richard_photoList/Richard_photoList.pro b/Richard_photoList/Richard_photoList.pro index 9aa4b5c..2a8235c 100644 --- a/Richard_photoList/Richard_photoList.pro +++ b/Richard_photoList/Richard_photoList.pro @@ -14,10 +14,14 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ - mylabel.cpp + mylabel.cpp \ + label.cpp \ + clickmenu.cpp HEADERS += mainwindow.h \ - mylabel.h + mylabel.h \ + label.h \ + clickmenu.h FORMS += mainwindow.ui diff --git a/Richard_photoList/Richard_photoList.pro.user b/Richard_photoList/Richard_photoList.pro.user index 84210d5..dffdd4b 100644 --- a/Richard_photoList/Richard_photoList.pro.user +++ b/Richard_photoList/Richard_photoList.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/Richard_photoList/clickmenu.cpp b/Richard_photoList/clickmenu.cpp new file mode 100644 index 0000000..4219a47 --- /dev/null +++ b/Richard_photoList/clickmenu.cpp @@ -0,0 +1,8 @@ +#include "clickmenu.h" + +clickMenu::clickMenu() +{ +} + +clickMenu::~clickMenu() { +} diff --git a/Richard_photoList/clickmenu.h b/Richard_photoList/clickmenu.h new file mode 100644 index 0000000..83b68dc --- /dev/null +++ b/Richard_photoList/clickmenu.h @@ -0,0 +1,17 @@ +#ifndef CLICKMENU_H +#define CLICKMENU_H + +#include + + +class clickMenu +{ +public: + clickMenu(); + ~clickMenu(); + // Copy Constructors and Assignment Operator not defined + // They only create shallow copies + QAction** actionList; +}; + +#endif // CLICKMENU_H diff --git a/Richard_photoList/label.cpp b/Richard_photoList/label.cpp new file mode 100644 index 0000000..d5b88f3 --- /dev/null +++ b/Richard_photoList/label.cpp @@ -0,0 +1,2 @@ +#include "label.h" + diff --git a/Richard_photoList/label.h b/Richard_photoList/label.h new file mode 100644 index 0000000..87e67d3 --- /dev/null +++ b/Richard_photoList/label.h @@ -0,0 +1,26 @@ +#ifndef LABEL_H +#define LABEL_H + +#include +#include +#include +#include + +class Label : public QLabel +{ +public: + Label(QWidget* pParent=0, Qt::WindowFlags f=0) : QLabel(pParent, f) {}; + Label(const QString& text, QWidget* pParent = 0, Qt::WindowFlags f = 0) : QLabel(text, pParent, f){}; + +protected : + virtual void mouseReleaseEvent ( QMouseEvent * ev ) { + //fprintf(stderr, "%d\n", ev->globalX()); + //fprintf(stderr, "%d\n\n", ev->globalY()); + + QMenu MyMenu(this); + MyMenu.addActions(this->actions()); + MyMenu.exec(ev->globalPos()); + } +}; + +#endif // LABEL_H diff --git a/Richard_photoList/main.cpp b/Richard_photoList/main.cpp index d0923ee..8dff162 100644 --- a/Richard_photoList/main.cpp +++ b/Richard_photoList/main.cpp @@ -1,6 +1,7 @@ #include "mainwindow.h" #include #include +#include int main(int argc, char *argv[]) { diff --git a/Richard_photoList/mainwindow.cpp b/Richard_photoList/mainwindow.cpp index 98de8eb..89ed9d6 100644 --- a/Richard_photoList/mainwindow.cpp +++ b/Richard_photoList/mainwindow.cpp @@ -1,5 +1,13 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "stdio.h" + + +// I want to display 100 rows of pictures +// each row contains 8 columns; +const int rows = 100; +const int columns = 6; +long totalElements = rows * columns; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -28,25 +36,24 @@ MainWindow::MainWindow(QWidget *parent) : const int w = 300; const int h = 300; - // I want to display 100 rows of pictures - // each row contains 8 columns; - const int rows = 100; - const int columns = 6; - dir = new QDir("/Users/richardyu/Pictures/photolist"); QStringList filters; - filters << "*png" << "*jpg" << "*JPEG" << "*.bmp"; + filters << "*png" << "*jpg" << "*JPEG" << "*.bmp" << "deleted"; dir->setNameFilters(filters); - QFileInfoList list = dir->entryInfoList(); - - long listCount = 0; + fileInfoList = new QFileInfoList(dir->entryInfoList()); - imageLabel = new QLabel*[rows * columns]; - imageMap = new QPixmap*[rows * columns]; - imageName = new QLabel*[rows * columns]; + int listCount = 0; + imageLabel = new Label*[totalElements]; + imageMap = new QPixmap*[totalElements]; + imageName = new Label*[totalElements]; + //menuList = new clickMenu*[totalElements]; + //for (int i = 0; i < totalElements; i++) menuList[i] = NULL; + actionList = new QAction**[totalElements]; + for (int i = 0; i < totalElements; i++) actionList[i] = NULL; //QLabel* filename; + // double for loop, treat the image grid like a matrix // for every row, display each column in the current row for (int k = 0; k < rows; k=k+2) { @@ -54,21 +61,29 @@ MainWindow::MainWindow(QWidget *parent) : // When all files in the directory are added to the screen // Jump out of the loop using 'goto' - if (listCount == list.size()) goto jump; + if (listCount == fileInfoList->size()) goto jump; // fileinfo contains the path of every image - QFileInfo fileinfo = list.at(listCount); + QFileInfo fileinfo = fileInfoList->at(listCount); auto int index = k*columns + j; - imageLabel[index] = new QLabel(); - imageName[index] = new QLabel(); + imageLabel[index] = new Label(); + imageName[index] = new Label(); imageMap[index] = new QPixmap(fileinfo.absoluteFilePath()); imageLabel[index]->setPixmap(imageMap[index]->scaled(w,h,Qt::KeepAspectRatio)); //Widget* tmp(imageLabel[index]); //filename = new QLabel; - QString temp = "" + fileinfo.fileName(); + QString temp = "" + fileinfo.fileName(); imageName[index]->setText(temp); + actionList[index] = new QAction*[2]; + actionList[index][0] = new QAction("Rename", imageLabel[index]); + actionList[index][1] = new QAction("Delete", imageLabel[index]); + imageLabel[index]->addAction(actionList[index][0]); + imageLabel[index]->addAction(actionList[index][1]); + connect(actionList[index][0], SIGNAL(triggered()), this, SLOT(onAction1())); + connect(actionList[index][1], SIGNAL(triggered()), this, SLOT(onAction2())); + fprintf(stderr, "hello\n"); gridLayout->addWidget(imageLabel[index],k,j); //gridLayout->addWidget(tmp,k,j); gridLayout->addWidget(imageName[index],k+1,j); @@ -78,13 +93,6 @@ MainWindow::MainWindow(QWidget *parent) : jump: listCount = 0; - - //QPixmap* tmpmap = new QPixmap("/Users/richardyu/Pictures/conduct.png"); - //QLabel* tmplabel; - //tmplabel->setPixmap(tmpmap->scaled(w,h,Qt::KeepAspectRatioByExpanding)); - //Widget* tmpwidget = new Widget(tmplabel); - //gridLayout->addWidget(tmpwidget, 0, 80); - area->setWidget(gridWidget); entireLayout->setAlignment(Qt::AlignTop); entireLayout->addWidget(ui->hideImage); @@ -99,9 +107,16 @@ MainWindow::MainWindow(QWidget *parent) : MainWindow::~MainWindow() { delete dir; + if (actionList != NULL) + for (int i = 0; i < totalElements; i++) { + if (actionList[i] != NULL) + delete [] (actionList[i]); + } + delete [] actionList; delete []imageMap; delete []imageName; delete []imageLabel; + delete fileInfoList; delete gridLayout; delete gridWidget; delete area; @@ -115,7 +130,24 @@ void MainWindow::on_hideImage_released() clearImage(); } + +void MainWindow::onAction2() { + /* + QFileInfo fileInfo = fileInfoList->at(listCount); + QString fileOldName = fileInfo.fileName(); + QString fileNewName = "_deleted" + fileOldName; + dir->rename(fileOldName, fileNewName); + */ + clearImage(); + //addImage(); +} + +void MainWindow::onAction1() { + +} + void MainWindow::clearImage() { + delete []imageMap; imageMap = NULL; delete []imageLabel; @@ -136,29 +168,65 @@ void MainWindow::addImage() { area->setBackgroundRole(QPalette::Dark); // image hight and width - const int w = 150; - const int h = 150; + const int w = 300; + const int h = 300; // I want to display 100 rows of pictures // each row contains 8 columns; - const int rows = 100; - const int columns = 8; + //const int rows = 100; + //const int columns = 8; + dir = new QDir("/Users/richardyu/Pictures/photolist"); + QStringList filters; + filters << "*png" << "*jpg" << "*JPEG" << "*.bmp" << "deleted"; + dir->setNameFilters(filters); + fileInfoList = new QFileInfoList(dir->entryInfoList()); - imageLabel = new QLabel*[rows * columns]; - imageMap = new QPixmap*[rows * columns]; + long listCount = 0; + imageLabel = new Label*[totalElements]; + imageMap = new QPixmap*[totalElements]; + imageName = new Label*[totalElements]; - // double for loop, treat the image grid like a matrix - // for every row, display each column in the current row - for (int k = 0; k < rows; k++) { + actionList = new QAction**[totalElements]; + for (int i = 0; i < totalElements; i++) actionList[i] = NULL; + + for (int k = 0; k < rows; k=k+2) { for (int j = 0; j < columns; j++) { + + // When all files in the directory are added to the screen + // Jump out of the loop using 'goto' + if (listCount == fileInfoList->size()) goto jump; + + // fileinfo contains the path of every image + QFileInfo fileinfo = fileInfoList->at(listCount); + auto int index = k*columns + j; - imageLabel[index] = new QLabel(); - imageMap[index] = new QPixmap("/Users/richardyu/Pictures/conduct.png"); - imageLabel[index]->setPixmap(imageMap[index] ->scaled(w,h,Qt::KeepAspectRatioByExpanding)); + imageLabel[index] = new Label(); + imageName[index] = new Label(); + imageMap[index] = new QPixmap(fileinfo.absoluteFilePath()); + imageLabel[index]->setPixmap(imageMap[index]->scaled(w,h,Qt::KeepAspectRatio)); + //Widget* tmp(imageLabel[index]); + //filename = new QLabel; + QString temp = "" + fileinfo.fileName(); + imageName[index]->setText(temp); + + actionList[index] = new QAction*[2]; + actionList[index][0] = new QAction("Rename", imageLabel[index]); + actionList[index][1] = new QAction("Delete", imageLabel[index]); + imageLabel[index]->addAction(actionList[index][0]); + imageLabel[index]->addAction(actionList[index][1]); + connect(actionList[index][0], SIGNAL(triggered()), this, SLOT(onAction1())); + connect(actionList[index][1], SIGNAL(triggered()), this, SLOT(onAction2())); + gridLayout->addWidget(imageLabel[index],k,j); + //gridLayout->addWidget(tmp,k,j); + gridLayout->addWidget(imageName[index],k+1,j); + listCount++; } } + jump: listCount = 0; + + area->setWidget(gridWidget); entireLayout->addWidget(area); } diff --git a/Richard_photoList/mainwindow.h b/Richard_photoList/mainwindow.h index 93a1ba1..293ec07 100644 --- a/Richard_photoList/mainwindow.h +++ b/Richard_photoList/mainwindow.h @@ -16,11 +16,22 @@ #include #include #include "mylabel.h" +#include "label.h" namespace Ui { class MainWindow; } +typedef struct clickMenu { + QAction* action1; + QAction* action2; + QAction* action3; + QAction* action4; + QAction* action5; + +} clickMenu; + + class MainWindow : public QMainWindow { Q_OBJECT @@ -35,6 +46,8 @@ private slots: void on_showImage_released(); void on_hello_released(); + void onAction2(); + void onAction1(); private: Ui::MainWindow *ui; @@ -43,10 +56,13 @@ private slots: QWidget* gridWidget; QScrollArea* area; QVBoxLayout* entireLayout; - QLabel** imageLabel; - QLabel** imageName; + Label** imageLabel; + Label** imageName; QPixmap** imageMap; QDir* dir; + clickMenu** menuList; + QAction*** actionList; + QFileInfoList* fileInfoList; void clearImage(); void addImage();