diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c2ba41932de7a652ebb357ecc380906f1077297..d4a3f237e2af836d20d1204a558b69f208afa2a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ else() else() add_executable(stickers-renomator-2000 ${PROJECT_SOURCES} + lineEditEventFilter.h ) endif() endif() diff --git a/lineEditEventFilter.h b/lineEditEventFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..d6ab5b37ab6ec2b99b86e4ce75a6b1a1b9460d12 --- /dev/null +++ b/lineEditEventFilter.h @@ -0,0 +1,31 @@ +#ifndef LINEEDITEVENTFILTER_H +#define LINEEDITEVENTFILTER_H +#include <QObject> +#include <QLineEdit> +#include <QKeyEvent> +#include <QEvent> +class LineEditEventFilter : public QObject +{ +public: + explicit LineEditEventFilter(QLineEdit *parent) : QObject(parent) + {} + + bool eventFilter(QObject *obj, QEvent *e) + { + switch (e->type()) + { + case QEvent::KeyPress: + { + QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e); + if (keyEvent->key() == Qt::Key_Escape) + { + reinterpret_cast<QLineEdit *>(parent())->clearFocus(); + } + break; + } + } + // standard event processing + return QObject::eventFilter(obj, e); + } +}; +#endif // LINEEDITEVENTFILTER_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 0b5bbd58fe42a6024f554045bd2271a54e95ec1d..77fec0b83b493840e1e66a8b03aca86b74c5f456 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -6,6 +6,7 @@ #include <QFile> #include <QPixmap> #include <QKeyEvent> +#include "lineEditEventFilter.h" MainWindow::MainWindow(QString dir, QWidget *parent) : QMainWindow(parent) @@ -15,7 +16,11 @@ MainWindow::MainWindow(QString dir, QWidget *parent) MainWindow::setWindowTitle("Stickers Renomator 2000"); ui->label_2->setScaledContents(false); connect(ui->lineEdit, SIGNAL(returnPressed()),this,SLOT(on_pushButton_clicked())); + connect(ui->defautPrefixEdit, SIGNAL(textEdited(const QString)), this, SLOT(on_defaultPrefixEdit_textEdited())); this->imageFolder = dir; + auto lineEditEventFilter = new LineEditEventFilter(ui->lineEdit); + ui->lineEdit->installEventFilter(lineEditEventFilter); + ui->lineEdit->setFocus(); } MainWindow::~MainWindow() @@ -88,53 +93,67 @@ void MainWindow::mousePressEvent(QMouseEvent *event) void MainWindow::on_pushButton_clicked() { - QString newName = ui->lineEdit->text(); - QFile::rename(this->currentImagePath, this->imageFolder+"/"+newName+".png"); - ui->lineEdit->setText(""); - this->imageList[this->currentImageIndex] = newName+".png"; - this->currentImageIndex++; - if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; - this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; - ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); - this->currentImage = QPixmap(this->currentImagePath); - setImage(); + if(this->imageFolder != ""){ + QString newName = ui->lineEdit->text(); + QFile::rename(this->currentImagePath, this->imageFolder+"/"+newName+".png"); + ui->lineEdit->setText(defaultPrefix); + this->imageList[this->currentImageIndex] = newName+".png"; + this->currentImageIndex++; + if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; + this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; + ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); + this->currentImage = QPixmap(this->currentImagePath); + setImage(); + } } void MainWindow::on_deleteButton_clicked() { - if(!QDir(this->imageFolder+"/deletedStickers").exists()){ + if(this->imageFolder != ""){ + if(!QDir(this->imageFolder+"/deletedStickers").exists()){ QDir().mkdir(this->imageFolder+"/deletedStickers"); + } + QFile::rename(this->currentImagePath, this->imageFolder+"/deletedStickers/"+this->imageList[this->currentImageIndex]); + this->imageList.removeAt(this->currentImageIndex); + this->currentImageIndex++; + if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; + this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; + ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); + this->currentImage = QPixmap(this->currentImagePath); + setImage(); } - QFile::rename(this->currentImagePath, this->imageFolder+"/deletedStickers/"+this->imageList[this->currentImageIndex]); - this->imageList.removeAt(this->currentImageIndex); - this->currentImageIndex++; - if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; - this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; - ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); - this->currentImage = QPixmap(this->currentImagePath); - setImage(); } void MainWindow::on_prev_clicked() { - this->currentImageIndex--; - if(this->currentImageIndex<0) this->currentImageIndex = this->imageList.size()-1; - this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; - ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); - this->currentImage = QPixmap(this->currentImagePath); - setImage(); + if(this->imageFolder != ""){ + this->currentImageIndex--; + if(this->currentImageIndex<0) this->currentImageIndex = this->imageList.size()-1; + this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; + ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); + this->currentImage = QPixmap(this->currentImagePath); + setImage(); + } } void MainWindow::on_next_clicked() { - this->currentImageIndex++; - if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; - this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; - ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); - this->currentImage = QPixmap(this->currentImagePath); - setImage(); + if(this->imageFolder != ""){ + this->currentImageIndex++; + if(this->currentImageIndex>=this->imageList.size()) this->currentImageIndex = 0; + this->currentImagePath = this->imageFolder+"/"+this->imageList[this->currentImageIndex]; + ui->label_3->setText("Nom actuel: "+this->imageList[this->currentImageIndex].split(".").at(0)); + this->currentImage = QPixmap(this->currentImagePath); + setImage(); + } +} + +void MainWindow::on_defaultPrefixEdit_textEdited() +{ + defaultPrefix = ui->defautPrefixEdit->text(); + ui->lineEdit->setText(defaultPrefix); } diff --git a/mainwindow.h b/mainwindow.h index accfcf5a4f575899df4a228d8ee82de0cf0ba240..326a0150c4faa5a3b9d1a9ac3a45168e9585e5dc 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -30,6 +30,8 @@ private slots: void on_next_clicked(); + void on_defaultPrefixEdit_textEdited(); + private: Ui::StickersRenomator2000 *ui; QStringList imageList; @@ -37,6 +39,7 @@ private: QString currentImagePath; QPixmap currentImage; bool isFirstResize=true; + QString defaultPrefix; void resizeEvent(QResizeEvent *event) override; void keyPressEvent(QKeyEvent *event) override; void mousePressEvent ( QMouseEvent * event ) override; diff --git a/mainwindow.ui b/mainwindow.ui index 88404a8a561e57a866c5dc4d3462c28d70702714..5e3b75eabe69e2338243598aa78013504f74383c 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -522,16 +522,6 @@ <item row="2" column="0"> <widget class="QLineEdit" name="lineEdit"/> </item> - <item row="2" column="1"> - <widget class="QToolButton" name="deleteButton"> - <property name="text"> - <string comment="Supprimer">X</string> - </property> - <property name="icon"> - <iconset theme="dialog-error"/> - </property> - </widget> - </item> <item row="4" column="0"> <widget class="QPushButton" name="pushButton"> <property name="text"> @@ -563,9 +553,72 @@ </property> </widget> </item> + <item row="2" column="1"> + <widget class="QToolButton" name="deleteButton"> + <property name="text"> + <string comment="Supprimer">X</string> + </property> + <property name="icon"> + <iconset theme="dialog-error"/> + </property> + </widget> + </item> </layout> </widget> </item> + <item row="0" column="0"> + <widget class="QFrame" name="frame_3"> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <widget class="QLabel" name="label_4"> + <property name="geometry"> + <rect> + <x>10</x> + <y>10</y> + <width>61</width> + <height>21</height> + </rect> + </property> + <property name="font"> + <font> + <pointsize>11</pointsize> + <weight>75</weight> + <bold>true</bold> + </font> + </property> + <property name="text"> + <string>Config</string> + </property> + </widget> + <widget class="QLabel" name="label_5"> + <property name="geometry"> + <rect> + <x>10</x> + <y>50</y> + <width>111</width> + <height>17</height> + </rect> + </property> + <property name="text"> + <string>Default prefix:</string> + </property> + </widget> + <widget class="QLineEdit" name="defautPrefixEdit"> + <property name="geometry"> + <rect> + <x>10</x> + <y>70</y> + <width>113</width> + <height>25</height> + </rect> + </property> + </widget> + </widget> + </item> </layout> </widget> <widget class="QMenuBar" name="menubar">